-
Notifications
You must be signed in to change notification settings - Fork 2
/
pjb-cl.el
2204 lines (1779 loc) · 83.9 KB
/
pjb-cl.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;; -*- mode:emacs-lisp;coding:utf-8;lexical-binding:t -*-
;;;;*****************************************************************************
;;;;FILE: pjb-cl.el
;;;;LANGUAGE: emacs lisp
;;;;SYSTEM: emacs
;;;;USER-INTERFACE: emacs
;;;;DESCRIPTION
;;;;
;;;; This module exports a few Common Lisp operators missing from cl.
;;;;
;;;;AUTHORS
;;;; <PJB> Pascal J. Bourguignon
;;;;MODIFICATIONS
;;;; 2006-11-14 <PJB> Removed most of CL functions and downcased the rest
;;;; to avoid collision with emacs-cl.
;;;; 2004-11-01 <PJB> Upcased COMMON-LISP symbols. This will avoid name
;;;; collision between Common-Lisp and emacs-lisp.
;;;; 2003-01-20 <PJB> Replaced pjb-cl%%string-replace
;;;; by regexp-replace-in-string.
;;;; 2002-04-10 <PJB> Creation.
;;;;BUGS
;;;;
;;;; encode-time/decode-time have a range too limited (32-bit around 1/1/1970)
;;;; we should implement our own version.
;;;;
;;;;
;;;;LEGAL
;;;; LGPL
;;;;
;;;; Copyright Pascal J. Bourguignon 2002 - 2011
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 2 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
;;;;
;;;;*****************************************************************************
(require 'cl)
(load "cl-seq" nil t)
(load "cl-extra" nil t)
(require 'eieio)
(require 'eieio-opt)
(require 'parse-time)
(setf lexical-binding t)
;; Let's teach emacs how to format Common-Lisp:
(mapc (lambda (sym) (put sym 'lisp-indent-function 1))
'(concatenate with-open-file with-open-stream
with-input-from-string with-output-to-string dolist lambda
when unless while until if let let* handler-case))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Private functions.
(defconst pjb-cl%%epoch 2208988800.0
"I compute 25567 days, or 2208988800 seconds from 1900-01-01 00:00:00
to 1970-01-01 00:00:00. Please correct me if I'm wrong.")
(defun pjb-cl%%leap-year (year)
"PRIVATE.
RETURN: Whether the `year' is a leap-year.
NOTE: `year' should be 2002 if it's the third year of the XXth century...
"
(or (= 0 (mod year 400))
(and (/= 0 (mod year 100))
(= 0 (mod year 4)) )))
(defun pjb-cl%%days-in-year (year)
"PRIVATE.
RETURN: The number of days in the given year.
"
(if (pjb-cl%%leap-year year) 366 365))
(defun pjb-cl%%seconds-to-emacs-time (secs)
"PRIVATE.
PRE: secs is a number of seconds.
RETURN: The time represented by secs in emacs time format, ie.
a list ( h l us ) with h=secs/2^16, l=secs%2^16, us=(secs*1e6)%1e6.
"
(let* ( (h (truncate (/ secs 65536)))
(lf (- secs (* h 65536.0)))
(l (truncate lf))
(us (truncate (* 1000000.0 (- lf l)))) )
(list h l us) ))
(defun pjb-cl%%emacs-time-to-seconds (et)
"PRIVATE.
PRE: et is a time in emacs time format, ie. a list ( h l [us])
with h and l being in [0..65535] and us in [0..999999].
RETURN: et expressed as a scalar."
(+ (let ((h (nth 0 et))) (if (< h 1024) (* h 65536) (* h 65536.0)))
(nth 1 et)
(let ((us (nth 2 et))) (if (or (null us) (= 0 us)) 0 (/ us 1000000.0)))))
(defun pjb-cl%%char-in-bag (char bag)
"PRIVATE.
RETURN: Whether `char' is in `bag'.
"
(let ( (len (length bag))
(i 0) )
(while (and (< i len) (/= char (elt bag i)))
(setq i (1+ i)))
(< i len)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Common-Lisp
;; -----------------------------
;; - 5 - Data and Control Flow -
;; -----------------------------
(defmacro defconstant (symbol initvalue &optional docstring)
`(defconst ,symbol ,initvalue ,docstring))
(defmacro defparameter (symbol &optional initvalue docstring)
`(progn
(defvar ,symbol nil ,docstring)
(setq ,symbol ,initvalue)))
(defmacro multiple-value-prog1 (first-form &rest forms)
;; Multiple values are implemented as list of values. BAD.
`(prog1 ,first-form ,@forms))
(defconstant multiple-value-limit 4098)
;; From: Stefan Monnier <[email protected]>
;; Subject: Re: condition-case
;; Newsgroups: gnu.emacs.help
;; Date: Thu, 09 Dec 2010 10:04:12 -0500
;; Organization: A noiseless patient Spider
;; Message-ID: <[email protected]>
;;
;; Oh wait, I just noticed this one: `subst' is wrong here. I know CL
;; already uses it for similar purposes elsewhere, but it's simply wrong
;; because `subst' doesn't know about Elisp binding rules.
;; So (subst 'b 'a '(lambda () '(a b c))) will happily return
;; (lambda () '(b b c)). Better simply use `let', even if it has
;; a performance cost.
;; (defmacro handler-case (expression &rest clauses)
;; "Evaluate expression with `condition-case' and catch errors with CLAUSES.
;;
;; Longer explanation here..."
;; (let* ((var (gensym))
;; (neclause (assoc :NO-ERROR clauses))
;; (nell (cadr neclause))
;; (nebody (cddr neclause))
;; (handlers (mapcar (lambda (clause)
;; (let ((typespec (car clause))
;; (clausvar (cadr clause))
;; (body (cddr clause)))
;; (cons (if (and (consp typespec)
;; (eq 'or (car typespec)))
;; (cdr typespec)
;; typespec)
;; (if (null clausvar)
;; body
;; (let ((var (car clausvar)))
;; body)))))
;; (remove neclause clauses))))
;; (if neclause
;; `(condition-case ,var
;; (multiple-value-bind ,nell ,expression ,@nebody)
;; ,@handlers)
;; `(condition-case ,var
;; ,expression
;; ,@handlers))))
(defun complement (fun)
(lambda (&rest arguments)
(not (apply fun arguments))))
;; ------------------
;; - 9 - Conditions -
;; ------------------
;; (condition-case VAR BODYFORM HANDLERS...)
;;
;; Regain control when an error is signaled.
;; Executes BODYFORM and returns its value if no error happens.
;; Each element of HANDLERS looks like (CONDITION-NAME BODY...)
;; where the BODY is made of Lisp expressions.
;;
;; A handler is applicable to an error
;; if CONDITION-NAME is one of the error's condition names.
;; If an error happens, the first applicable handler is run.
;;
;; The car of a handler may be a list of condition names
;; instead of a single condition name.
;;
;; When a handler handles an error,
;; control returns to the condition-case and the handler BODY... is executed
;; with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).
;; VAR may be nil; then you do not get access to the signal information.
;;
;; The value of the last BODY form is returned from the condition-case.
;; See also the function `signal' for more info.
(defmacro handler-case (expression &rest clauses)
"Common-Lisp
IMPLEMENTATION: The clause variable symbols are substituted by one single
condition-case variable symbol. This may cause problems
if the same symbol is used as data or if it's a dynamic
variable.
"
(let* ((var (gensym))
(neclause (assoc :NO-ERROR clauses))
(nell (cadr neclause))
(nebody (cddr neclause))
(handlers (mapcar (lambda (clause)
(let ((typespec (car clause))
(clausvar (cadr clause))
(body (cddr clause)))
(cons (if (and (consp typespec)
(eq 'or (car typespec)))
(cdr typespec)
typespec)
(if (null clausvar)
body
(subst var (car clausvar) body)))))
(remove neclause clauses))))
(if neclause
`(condition-case ,var
(multiple-value-bind ,nell ,expression ,@nebody)
,@handlers)
`(condition-case ,var
,expression
,@handlers))))
(defmacro cl:ignore-errors (&rest body)
`(handler-case
(progn ,@body)
(error (err)
(message "cl:ignore-errors %S" err)
(values nil err))))
;; ----------------
;; - 10 - Symbols -
;; ----------------
(defun copy-symbol (symbol &optional copy-properties)
(let ((new (make-symbol (cl:string symbol))))
(when copy-properties
(setf (symbol-plist new) (COPY-SEQ (symbol-plist symbol))))
(setf (symbol-value new) (symbol-value symbol)
(symbol-function new) (symbol-function symbol))
new))
(defun cl:symbol-package (sym)
"Return the name of the package of the symbol.
This is \"emacs-lisp\" if the symbol doesn't contain any colon,
\"keyword\" if it starts with a colon, or the substring before the
colon if it contains one."
(let* ((name (symbol-name sym))
(colon (position (character ":") name)))
(case colon
((nil) "emacs-lisp")
((0) "keyword")
(otherwise (subseq name 0 colon)))))
(defun cl:symbol-name (sym)
"Return the name of the symbol. This is what's after the colon or
double colon if any, or the symbol name."
(let* ((name (symbol-name sym))
(colon (position (character ":") name)))
(cond
((and colon (char= (character ":") (char name (1+ colon))))
(subseq name (+ 2 colon)))
(colon
(subseq name (+ 1 colon)))
(t name))))
;; -----------------
;; - 11 - Packages -
;; -----------------
(defun intern* (string &optional package)
(cond ((and (stringp package) (string= "KEYWORD" package))
(intern (format ":%s" string)))
(t
(intern string))))
;; ----------------
;; - 12 - Numbers -
;; ----------------
(defun compute-most-positive-fixnum ()
(loop
for p from 0
for i = 1 then (* 2 i)
while (plusp i)
finally (return (+ (expt 2 (1- p)) (1- (expt 2 (1- p)))))))
(unless (boundp 'most-positive-fixnum)
(defconstant most-positive-fixnum (compute-most-positive-fixnum)
"Common-Lisp: most-positive-fixnum is that fixnum closest in value to positive infinity provided by the implementation, and greater than or equal to both 2^15 - 1 and array-dimension-limit.
URL: http://www.informatimago.com/local/lisp/HyperSpec/Body/v_most_p.htm#most-positive-fixnum"))
(unless (boundp 'most-negative-fixnum)
(defconstant most-negative-fixnum (- -1 (compute-most-positive-fixnum))
"Common-Lisp: most-negative-fixnum is that fixnum closest in value to negative infinity provided by the implementation, and less than or equal to both -2^15.
URL: http://www.informatimago.com/local/lisp/HyperSpec/Body/v_most_p.htm#most-negative-fixnum"))
(defun integer-length (x)
"Returns the number of bits needed to represent integer in binary two's-complement format."
(if (minusp x)
(integer-length (- x))
(loop
until (zerop x)
do (setf x (ash x -1))
sum 1 into result
finally (return result))))
(defun* cl:digit-char-p (char &optional (radix 10))
(let ((value (position (upcase char) "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")))
(and value (< value radix) value)))
(defun* cl:parse-integer (string &key (start 0) end (radix 10) junk-allowed)
(let ((end (or end (length string)))
(n 0)
(sign 1)
(plus (character "+"))
(minus (character "-"))
(space (character " ")))
(labels ((parse-integer-error ()
(error "Not an integer string %S (:start %d :end %d :radix %d)"
string start end radix))
(check-range ()
(unless (< start end)
(parse-integer-error)))
(eat-spaces (i)
(loop
while (and (< i end) (char= space (aref string i)))
do (incf i)
finally (return i))))
(setf start (eat-spaces start))
(check-range)
(cond
((char= plus (aref string start)) (setf sign +1) (incf start) (check-range))
((char= minus (aref string start)) (setf sign -1) (incf start) (check-range)))
(loop
for i from start below end
for digit = (cl:digit-char-p (aref string i) radix)
while digit
do (setf n (+ (* n radix) digit))
finally (when (< i end)
(setf i (eat-spaces i)))
(when (and (not junk-allowed) (< i end))
(parse-integer-error))
(return (values (* sign n) i))))))
;; -------------------
;; - 13 - Characters -
;; -------------------
(unless (fboundp 'characterp)
(defun characterp (x)
"Common-Lisp: Returns true if object is of type character; otherwise, returns false.
URL: http://www.informatimago.com/local/lisp/HyperSpec/Body/f_chp.htm#characterp
RETURN: Whether the argument is a character.
NOTE: With emacs, characters are integers."
(integerp x)))
(defun character (character)
"Common-Lisp: Returns the denoted character
URL: http://www.informatimago.com/local/lisp/HyperSpec/Body/f_ch.htm
RETURN: An emacs character.
EXAMPLES: (CHARACTER #\\a) => ?a INVALID: # is invalid read syntax for emacs!
(CHARACTER \"a\") => ?a
(CHARACTER 'a) => ?A WARNING: emacs symbols are multicase!
(CHARACTER '\\a) => ?a
(CHARACTER 65.) is an error.
(CHARACTER 'apple) is an error.
"
(cond
((characterp character) character)
((symbolp character)
(if (= 1 (length (cl:symbol-name character)))
(string-to-char (cl:symbol-name character))
(error "character does not accept symbols of more than one character.")))
((stringp character)
(string-to-char character))
(t (error "Unexpected type fo character argument."))))
(defun upper-case-p (character)
"common-lisp:
"
(and (equal (upcase character) character)
(not (equal (downcase character) character))))
(defun lower-case-p (character)
"common-lisp:
"
(and (equal (downcase character) character)
(not (equal (upcase character) character))))
(defun both-case-p (character)
"common-lisp:
"
(or (upper-case-p character) (lower-case-p character)))
(defun char-upcase (character) (upcase character))
(defun char-downcase (character) (downcase character))
(defun code-char* (code)
"Common-Lisp:
"
(if (numberp code)
(coerce code 'character)
(error "%s is not a character code." code)))
(defun char-code* (character)
"Common-Lisp: Return the code attribute of character.
RETURN: character (emacs characters are numbers).
"
character)
(defun char= (&rest characters)
"common-lisp: compare character arguments.
url: http://www.informatimago.com/local/lisp/hyperspec/body/f_chareq.htm
"
(unless characters
(error "too few arguments given to char=."))
(let ((a (car characters)))
(every (lambda (b) (= a b)) (cdr characters))))
(defun char/= (&rest characters)
"common-lisp: compare character arguments.
Returns true if all characters are different (no two equal).
url: http://www.informatimago.com/local/lisp/hyperspec/body/f_chareq.htm
"
(unless characters
(error "too few arguments given to char/=."))
(if (= 1 (length characters))
t
(loop with already-seen = nil
with result = t
for c in characters
while result
do (if (memq c already-seen)
(setq result nil)
(push c already-seen))
finally (return result))))
(defmacro pjb-cl%%compare-chars (name order characters)
`(progn
(unless ,characters
(error "Too few arguments given to %s." ',name))
(let ((prev (car ,characters))
(result t))
(loop for next in (cdr ,characters)
while result
do (unless (,order (char-code* prev) (char-code* next))
(setq result nil))
(setq prev next))
result)))
(defun char< (&rest characters)
"Common-Lisp: returns true if the characters are monotonically increasing; \
otherwise, it returns false.
If two characters have identical implementation-defined attributes,
then their ordering by char< is consistent with the numerical ordering
by the predicate < on their codes.
URL: http://www.informatimago.com/local/lisp/HyperSpec/Body/f_chareq.htm
"
(pjb-cl%%compare-chars char< < characters))
(defun char> (&rest characters)
"Common-Lisp: returns true if the characters are monotonically decreasing; \
otherwise, it returns false.
If two characters have identical implementation-defined attributes,
then their ordering by char> is consistent with the numerical ordering
by the predicate > on their codes.
URL: http://www.informatimago.com/local/lisp/HyperSpec/Body/f_chareq.htm
"
(pjb-cl%%compare-chars char> > characters))
(defun char<= (&rest characters)
"Common-Lisp: returns true if the characters are monotonically nondecreasing;\
otherwise, it returns false.
If two characters have identical implementation-defined attributes,
then their ordering by char<= is consistent with the numerical
ordering by the predicate <= on their codes.
URL: http://www.informatimago.com/local/lisp/HyperSpec/Body/f_chareq.htm
"
(pjb-cl%%compare-chars char<= <= characters))
(defun char>= (&rest characters)
"Common-Lisp: returns true if the characters are monotonically nonincreasing;\
otherwise, it returns false.
If two characters have identical implementation-defined attributes,
then their ordering by char>= is consistent with the numerical
ordering by the predicate >= on their codes.
URL: http://www.informatimago.com/local/lisp/HyperSpec/Body/f_chareq.htm
"
(pjb-cl%%compare-chars char>= >= characters))
(defun char-equal (&rest characters)
"Common-Lisp: similar to char= except it ignores differences in case and \
might have an implementation-defined behavior for non-simple characters.
URL: http://www.informatimago.com/local/lisp/HyperSpec/Body/f_chareq.htm
"
(apply (function char=) (mapcar 'upcase characters)))
(defun char-not-equal (&rest characters)
"Common-Lisp: similar to char/= except it ignores differences in case and \
might have an implementation-defined behavior for non-simple characters.
URL: http://www.informatimago.com/local/lisp/HyperSpec/Body/f_chareq.htm
"
(apply (function char/=) (mapcar 'upcase characters)))
(defun char-lessp (&rest characters)
"Common-Lisp: similar to char< except it ignores differences in case and \
might have an implementation-defined behavior for non-simple characters.
URL: http://www.informatimago.com/local/lisp/HyperSpec/Body/f_chareq.htm
"
(apply (function char<) (mapcar 'upcase characters)))
(defun char-greaterp (&rest characters)
"Common-Lisp: similar to char> except it ignores differences in case and \
might have an implementation-defined behavior for non-simple characters.
URL: http://www.informatimago.com/local/lisp/HyperSpec/Body/f_chareq.htm
"
(apply (function char>) (mapcar 'upcase characters)))
(defun char-not-greaterp (&rest characters)
"Common-Lisp: similar to char<= except it ignores differences in case and \
might have an implementation-defined behavior for non-simple characters.
URL: http://www.informatimago.com/local/lisp/HyperSpec/Body/f_chareq.htm
"
(apply (function char<=) (mapcar 'upcase characters)))
(defun char-not-lessp (&rest characters)
"Common-Lisp: similar to char>= except it ignores differences in case and \
might have an implementation-defined behavior for non-simple characters.
URL: http://www.informatimago.com/local/lisp/HyperSpec/Body/f_chareq.htm
"
(apply (function char>=) (mapcar 'upcase characters)))
(defun ensure-character (ch)
(cond
((characterp ch) ch)
((stringp ch) (char ch 0))
((symbolp ch) (char (cl:symbol-name ch) 0))
((numberp ch) (code-char* ch))
(t (error "Expected a character, not: %S" ch))))
(defun alpha-char-p (ch)
"
COMMON-LISP
IMPLEMENTATION: Assumes ISO-8859-1!
"
;;; (string-match
;;; "\\(\\c0\\|\\c1\\|\\c2\\|\\c3\\|\\c4\\|\\c6\\|\\c7\\|\\c8\\|\\c9\\)"
;;; (format "%c" ch))
;; 0: consonant
;; 1: base (independent) vowel
;; 2: upper diacritical mark (including upper vowel)
;; 3: lower diacritical mark (including lower vowel)
;; 4: tone mark
;; 5: symbol
;; 6: digit
;; 7: vowel-modifying diacritical mark
;; 8: vowel-signs
;; 9: semivowel lower
(let ((ch (ensure-character ch)))
(or (and (char<= (character "A") ch) (char<= ch (character "Z")))
(and (char<= (character "a") ch) (char<= ch (character "z")))
(and (char<= (character "à") ch) (char<= ch (character "ö")))
(and (char<= (character "ø") ch) (char<= ch (character "ö")))
(and (char<= (character "ø") ch) (char<= ch (character "ÿ"))))))
(defun alphanumericp (ch)
"
COMMON-LISP
IMPLEMENTATION: Assumes ISO-8859-1!
"
(let ((ch (ensure-character ch)))
(or (digit-char-p ch)
(alpha-char-p ch))))
(defun* cl:digit-char-p (char &optional (radix 10))
(let ((value (position (upcase char) "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")))
(and value (< value radix) value)))
(defun* cl:parse-integer (string &key (start 0) end (radix 10) junk-allowed)
(let ((end (or end (length string)))
(n 0)
(sign 1)
(plus (character "+"))
(minus (character "-"))
(space (character " ")))
(labels ((parse-integer-error ()
(error "Not an integer string %S (:start %d :end %d :radix %d)"
string start end radix))
(check-range ()
(unless (< start end)
(parse-integer-error)))
(eat-spaces (i)
(loop
while (and (< i end) (char= space (aref string i)))
do (incf i)
finally (return i))))
(setf start (eat-spaces start))
(check-range)
(cond
((char= plus (aref string start)) (setf sign +1) (incf start) (check-range))
((char= minus (aref string start)) (setf sign -1) (incf start) (check-range)))
(loop
for i from start below end
for digit = (cl:digit-char-p (aref string i) radix)
while digit
do (setf n (+ (* n radix) digit))
finally (when (< i end)
(setf i (eat-spaces i)))
(when (and (not junk-allowed) (< i end))
(parse-integer-error))
(return (values (* sign n) i))))))
;; ---------------
;; - 14 - Conses -
;; ---------------
;; c[ad]{2,4}r are defined in cl-macs.el
;; butlast, nbutlast are primitives in emacs 21.2.
;; ---------------
;; - 15 - Arrays -
;; ---------------
;; TODO: We should store the dimensions of the array somewhere!
;; TODO: We should add the fill-pointer!
(defconstant array-dimension-limit 16777216
"Common-Lisp: A positive fixnum, the exact magnitude of which is
implementation-dependent, but which is not less than 1024.
IMPLEMENTATION: Actually, it seems the limit depends on the available memory
somewhat around 2^27 in 32-bit emacs, or less depending on the current
memory usage.")
(defconstant array-rank-limit 16777216
"Common-Lisp: A positive fixnum, the exact magnitude of which is
implementation-dependent, but which is not less than 8.
IMPLEMENTATION: Since we make vector of vectors, there's no other limit than
available memory.")
(defun* make-array (dimensions &key element-type initial-element
initial-contents adjustable
fill-pointer displaced-to
displaced-index-offset)
"Common-Lisp: Creates and returns an array constructed of the most \
specialized type that can accommodate elements of type given by element-type. \
If dimensions is nil then a zero-dimensional array is created.
URL: http://www.informatimago.com/local/lisp/HyperSpec/Body/f_mk_ar.htm#MAKE-ARRAY
RETURN: A vector {of vectors}.
NOTE: :element-type is ignored.
:adjustable is ignored.
:fill-pointer is not supported.
:displaced-to is not supported.
:displaced-index-offset is not supported.
"
(when (numberp dimensions) (setq dimensions (list dimensions)))
(when (and displaced-to (or initial-element initial-contents))
(error (concatenate 'string
":displaced-to must not be specified along with "
":initial-element or :initial-contents.")))
(when (and initial-element (or displaced-to initial-contents))
(error (concatenate 'string
":initial-element must not be specified along with "
" or :displaced-to :initial-contents.")))
(when (and displaced-index-offset (null displaced-to))
(error (concatenate 'string
":displaced-index-offset must not be specified "
"without :displaced-to.")))
(when (and fill-pointer (/= 1 (length dimensions)))
(error (concatenate 'string
":fill-pointer can only be specified for vectors "
" (uni-dimensional arrays).")))
(when fill-pointer
(error "fill-pointers not implemented. Sorry."))
(when displaced-to
(error "displaced-to arrays are not implemented. Sorry."))
(cond
((null dimensions)
(error "a-dimensional arrays are not implemented. Sorry."))
(initial-contents
(if (= 1 (length dimensions))
(loop with result = (make-vector (car dimensions) initial-element)
for i from 0 below (length initial-contents)
do (setf (aref result i) (elt initial-contents i))
finally return result)
(loop with result = (make-vector (car dimensions) initial-element)
for i from 0 below (length initial-contents)
do (setf (aref result i)
(make-array (cdr dimensions)
:initial-contents (elt initial-contents i)
:adjustable adjustable
;; the rest is not implemented.
))
finally return result)))
(t ;; let's use initial-element
(if (= 1 (length dimensions))
(make-vector (car dimensions) initial-element)
(loop with result = (make-vector (car dimensions) initial-element)
for i from 0 below (car dimensions)
do (setf (aref result i)
(make-array (cdr dimensions)
:initial-element initial-element
:adjustable adjustable
;; the rest is not implemented.
))
finally return result)))))
(defun array-dimension (array axis-number)
"Common-Lisp: array-dimension returns the axis-number dimension[1] of array.
Any fill pointer is ignored.
"
(if (= 0 axis-number)
(length array)
(array-dimension (aref array 0) (1- axis-number))))
(defadvice aref
(around pjb-cl-aref first (array &rest subscripts) activate)
"Common-Lisp: Accesses the array element specified by the subscripts. \
If no subscripts are supplied and array is zero rank, aref accesses the \
sole element of array.
NOTE: zero rank arrays are not supported.
"
(setq ad-return-value
(if (= 1 (length subscripts))
ad-do-it
(loop for vector = array then (aref vector index)
for index in subscripts
;;do (printf "vector = %S index = %S\n" vector index)
finally return vector))))
(ad-activate 'aref)
(defun pjb-cl%%aset (array &rest subscripts-and-value)
"PRIVATE
DO: sets the value of the element in array at subcripts.
NOTE: Used as (defsetf aref pjb-cl%%aset).
"
(let ((value (car (last subscripts-and-value)))
(subscripts (butlast subscripts-and-value)))
(aset (apply 'aref array (butlast subscripts))
(car (last subscripts)) value)))
(defsetf aref pjb-cl%%aset)
(deftype vector* () '(or string vector))
(defun vectorp* (item) (or (vectorp item) (stringp item)))
;; -----------------
;; - 16 - Strings - http://.../cltl/clm/node165.html
;; -----------------
(defun cl:string (x)
"Common-Lisp: If X is a string, then X, else if it's a symbol, then (cl:symbol-name X).
X---a string, a symbol, or a character.
Returns a string described by x; specifically:
* If X is a string, it is returned.
* If X is a symbol, its name is returned.
* If X is a character, then a string containing that one character is returned.
* string might perform additional, implementation-defined conversions.
If there are more than one arguments, then emacs string is called.
"
(cond
((stringp x) x)
((symbolp x) (cl:symbol-name x))
((characterp x) (make-string* 1 :initial-element x))
(t (signal 'type-error "Expected a string, a symbol or a character."))))
(defun char (str index)
"Common-Lisp: The character at position index of the string is returned \
as a character object.
RETURN: [cltl2] The given index must be a non-negative integer less
than the length of string, which must be a string. The
character at position index of the string is returned as a
character object.
"
(setq str (cl:string str))
(aref (the string str) index))
(defsetf char aset)
(defun schar (simple-string index)
"Common-Lisp: The character at position index of the string is returned \
as a character object.
RETURN: [cltl2] The given index must be a non-negative integer less
than the length of string, which must be a string. The
character at position index of the string is returned as a
character object. (This character will necessarily satisfy the
predicate string-char-p.)
"
(setq simple-string (cl:string simple-string))
(aref simple-string index))
(defsetf schar aset)
(defun cl:string-equal (string1 string2 &rest cl-keys)
(setq string1 (cl:string string1)
string2 (cl:string string2))
(let ((start1 (or (cadr (memq :start1 cl-keys)) 0))
(end1 (or (cadr (memq :end1 cl-keys)) (length string1)))
(start2 (or (cadr (memq :start2 cl-keys)) 0))
(end2 (or (cadr (memq :end2 cl-keys)) (length string2))))
(if (/= (- end1 start1) (- end2 start2))
nil
(string-equal
(string-upcase (substring string1 start1 end1))
(string-upcase (substring string2 start2 end2))))))
(defun cl:string-lessp (string1 string2 &rest cl-keys)
(setq string1 (cl:string string1)
string2 (cl:string string2))
(let ((start1 (or (cadr (memq :start1 cl-keys)) 0))
(end1 (or (cadr (memq :end1 cl-keys)) (length string1)))
(start2 (or (cadr (memq :start2 cl-keys)) 0))
(end2 (or (cadr (memq :end2 cl-keys)) (length string2)))
)
(if (/= (- end1 start1) (- end2 start2))
nil
(string-lessp
(string-upcase (substring string1 start1 end1))
(string-upcase (substring string2 start2 end2))))))
(defun string-greaterp* (string1 string2 &rest cl-keys)
(not (string-not-lessp
string2 string1
:start1 (or (cadr (memq :start2 cl-keys)) nil)
:end1 (or (cadr (memq :end2 cl-keys)) (length string2))
:start2 (or (cadr (memq :start1 cl-keys)) nil)
:end2 (or (cadr (memq :end1 cl-keys)) (length string1)))))
(defun string-not-lessp (string1 string2 &rest cl-keys)
(not (apply (function string-lessp) string1 string2 cl-keys)))
(defun string-not-greaterp (string1 string2 &rest cl-keys)
(not (apply (function string-greaterp) string1 string2 cl-keys)))
(defun string-not-equal (string1 string2 &rest cl-keys)
(not (apply (function string-equal) string1 string2 cl-keys)))
(defun cl:string= (string1 string2 &rest cl-keys)
;; &key :start1 :end1 :start2 :end2)
"Common-Lisp: compares two strings and is true if they are the \
same (corresponding characters are identical) but is false if they are not.
DO: [cltl2] string= compares two strings and is true if they are
the same (corresponding characters are identical) but is false
if they are not. The function equal calls string= if applied
to two strings.
The keyword arguments :start1 and :start2 are the places in
the strings to start the comparison. The arguments :end1 and
:end2 are the places in the strings to stop comparing;
comparison stops just before the position specified by a
limit. The ``start'' arguments default to zero (beginning of
string), and the ``end'' arguments (if either omitted or nil)
default to the lengths of the strings (end of string), so that
by default the entirety of each string is examined. These
arguments are provided so that substrings can be compared
efficiently.
string= is necessarily false if the (sub)strings being
compared are of unequal length; that is, if (not (= (- end1
start1) (- end2 start2))) is true, then string= is false.
"
;; TODO: should use compare-string
(setq string1 (cl:string string1)
string2 (cl:string string2))
(let ((start1 (or (cadr (memq :start1 cl-keys)) 0))
(end1 (or (cadr (memq :end1 cl-keys)) (length string1)))
(start2 (or (cadr (memq :start2 cl-keys)) 0))
(end2 (or (cadr (memq :end2 cl-keys)) (length string2))))
(if (/= (- end1 start1) (- end2 start2))
nil
(string-equal (substring string1 start1 end1)
(substring string2 start2 end2)))))