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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
| | ;;; cl-extra.el --- Common Lisp features, part 2 -*- lexical-binding: t -*-
;; Copyright (C) 1993, 2000-2021 Free Software Foundation, Inc.
;; Author: Dave Gillespie <daveg@synaptics.com>
;; Keywords: extensions
;; Package: emacs
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs 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 General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; These are extensions to Emacs Lisp that provide a degree of
;; Common Lisp compatibility, beyond what is already built-in
;; in Emacs Lisp.
;;
;; This package was written by Dave Gillespie; it is a complete
;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
;;
;; Bug reports, comments, and suggestions are welcome!
;; This file contains portions of the Common Lisp extensions
;; package which are autoloaded since they are relatively obscure.
;;; Code:
(require 'cl-lib)
(require 'seq)
;;; Type coercion.
;;;###autoload
(defun cl-coerce (x type)
"Coerce OBJECT to type TYPE.
TYPE is a Common Lisp type specifier.
\n(fn OBJECT TYPE)"
(cond ((eq type 'list) (if (listp x) x (append x nil)))
((eq type 'vector) (if (vectorp x) x (vconcat x)))
((eq type 'bool-vector)
(if (bool-vector-p x) x (apply #'bool-vector (cl-coerce x 'list))))
((eq type 'string) (if (stringp x) x (concat x)))
((eq type 'array) (if (arrayp x) x (vconcat x)))
((and (eq type 'character) (stringp x) (= (length x) 1)) (aref x 0))
((and (eq type 'character) (symbolp x))
(cl-coerce (symbol-name x) type))
((eq type 'float) (float x))
((cl-typep x type) x)
(t (error "Can't coerce %s to type %s" x type))))
;;; Predicates.
;;;###autoload
(defun cl-equalp (x y)
"Return t if two Lisp objects have similar structures and contents.
This is like `equal', except that it accepts numerically equal
numbers of different types (float vs. integer), and also compares
strings case-insensitively."
(cond ((eq x y) t)
((stringp x)
(and (stringp y) (= (length x) (length y))
(eq (compare-strings x nil nil y nil nil t) t)))
((numberp x)
(and (numberp y) (= x y)))
((consp x)
(while (and (consp x) (consp y) (cl-equalp (car x) (car y)))
(setq x (cdr x) y (cdr y)))
(and (not (consp x)) (cl-equalp x y)))
((vectorp x)
(and (vectorp y) (= (length x) (length y))
(let ((i (length x)))
(while (and (>= (setq i (1- i)) 0)
(cl-equalp (aref x i) (aref y i))))
(< i 0))))
(t (equal x y))))
;;; map-into
;; We implement a simple dispatcher for sequence types.
;;
;; cl-extra has cl--mapcar-many for similar purpose.
;; The core issue with it, it goes through args pre-emptively
;; to compute min length when there are more than 2 arguments
;; which makes it and its reverse dependencies fail on circular lists
;; unless there are <3 args.
;; Other issues are
;; - it performs type checks for sequences of known types at runtime
;; - it may cons whole arglist thrice per invocation
;; - looks like it's hard to extend.
;; Our approach doesn't have these issues.
(defconst cl--map-into-basic-call-arguments-limit 7
"Maximal reasonably expected number of arguments to `cl-map-into'.
`cl-map-into' caches its code corresponding to various signature
types of arglists supplied to `cl-map-into'. Arglists may vary
in length.
Code corresponding to arglists of length less than
`cl--map-into-basic-call-arguments-limit' is accessed via array.
Code corresponding to arglists of length greater than or equal to
`cl--map-into-basic-call-arguments-limit' is accessed via alist.
")
(defconst cl--map-into-max-small-signature
(expt 2 cl--map-into-basic-call-arguments-limit)
"Length of array to allocate for caching `cl-map-into' mappers
corresponding to small arglists.
Such mappers are accessed by their position in an array; position
equals the signature.
Consider `cl-map-into' arglist
(target f seq-1 seq-2)
call-arguments-limit corresponding to arglists of this length or
shorter, is 4 (as there are 4 arguments). This leaves at most 3
sequences to contribute to type signature.
Hovewer, we have to store one additional bit for fixnum-based
encoding to be unambiguous and simple. So overall array length
ends up being exactly (expt 2 call-arguments-limit).")
(defvar cl--map-into-mappers-array
(make-vector cl--map-into-max-small-signature nil)
"Array holding mappers corresponding to small arglists of `cl-map-into'.
Element type is (or function null).")
(defvar cl--map-into-mappers-alist nil
"Alist holding mappers corresponding to large arglists of `cl-map-into'.")
(defun cl--compute-map-into-signature (&rest all-sequences)
"Compute lookup key for `cl-map-into''s almost-arglist ALL-SEQUENCES.
Namely: ALL-SEQUENCES would be (TARGET &rest SEQUENCES)
for (cl-map-into TARGET f &rest SEQUENCES)
As a side effect, it checks that ALL-SEQUENCES are of sequence
types.
Example:
ELISP> (mapcar (lambda (arglist)
(apply #'cl--compute-map-into-signature arglist))
'(( () () () ) ; signature #b1000
( () () [] ) ; signature #b1001
( () [] () ) ; signature #b1010
( () [] [] ) ; signature #b1011
( [] () () ) ; signature #b1100
))
(8 9 10 11 12)"
;; This is not `cl-map-into'-specific and could be used for other caches
;; which is why we don't specify arglist as (target &rest sequences).
;; For the time being (while this dispatch is not used widely),
;; neither docstring nor name reflect this.
(let ((signature 1))
(dolist (s all-sequences signature)
(setq signature (ash signature 1))
(cl-etypecase s
(list)
(vector (cl-incf signature))))))
(cl-defmacro cl--do-seq-type-signature ((type-var signature &optional result)
&body body)
"With TYPE-VAR bound to sequence type, evaluate BODY forms. Return RESULT.
TYPE-VAR goes across sequence types in an arglist corresponding
to SIGNATURE that encodes sequence types in that arglist.
Iteration goes from arglist's end to arglist's start.
If :first is present at toplevel in BODY, all forms following
it (and those forms only) are evaluated in order when TYPE-VAR is
bound to the first sequence type in the arglist --- which would
be the last sequence type derived from SIGNATURE: see the
previous paragraph. At other iteration steps, only forms
preceding the first :first are evaluated.
Subsequent instances of toplevel :first in BODY don't affect anything."
(declare (indent 1))
(let* ((main (cl-copy-list body))
(first (if (eq :first (car main)) (progn (setf main nil)
(cdr main))
(cl-loop with sublist = main
while sublist do
(when (eq :first (cadr sublist))
(setf first (cddr sublist) (cdr sublist) nil)
(cl-return first))
(pop sublist)))))
(let ((sig (gensym "sig-")))
`(let ((,sig ,signature) ,type-var)
;; (declare (type (integer (1)) ,sig)
;; ;; Let's keep nil for now.
;; (type (member nil list vector) ,type-var))
(cl-check-type ,sig (integer (1)))
(cl-loop (cond
((or (when (= 2 ,sig) (setq ,type-var 'list))
(when (= 3 ,sig) (setq ,type-var 'vector)))
;; TODO: This duplicates main code sometimes,
;; think of elegant enough way to eliminate duplication.
,@(or first main) (cl-return ,result))
(t (setq ,type-var (if (zerop (mod ,sig 2))
'list
'vector))
,@main))
(setf ,sig (floor ,sig 2)))))))
(defun cl--make-map-into-mapper (signature &optional do-not-compile)
"Return mapper for `cl-map-into' specialized on arglists of type
encoded by SIGNATURE.
If DO-NOT-COMPILE is nil (default), return byte-compiled function.
Otherwise, return lambda form.
Example:
ELISP> (cl--make-map-into-mapper #b1011 t)
(lambda (f target-list vector-2 vector-1)
(cl-symbol-macrolet ((place (car target-cons)))
(cl-loop for target-cons on target-list
for elt-2 across vector-2
for elt-1 across vector-1
do (setf place (funcall f elt-2 elt-1))
finally return target-list)))"
(let ((gensym-counter 1) f xs ss loop
target-type target-index target-place target-var)
(cl-macrolet ((nconcf (var &rest seqs) `(setf ,var (nconc ,@seqs ,var))))
;; The only good thing about this name is, it's short and ends with f
(cl--do-seq-type-signature (type signature)
(nconcf loop (list 'for (let ((it (gensym "elt-")))
(push it xs)
(cl-decf gensym-counter)
it)
(cl-case type
(list 'in)
(vector 'across))
(let ((it (gensym (concat (symbol-name type) "-"))))
(push it ss)
it)))
:first (setq target-type type
target-var (make-symbol
(concat "target-" (symbol-name target-type))))
(nconcf loop (list 'for)
(cl-case type
(list (list (setq target-index (make-symbol "target-cons"))
'on target-var))
(vector (list (setq target-index (gensym "target-i"))
'to `(1- (length ,target-var))))))))
(funcall
(if do-not-compile #'identity #'byte-compile)
`(lambda ,(cons (setq f (make-symbol "f")) (cons target-var ss))
(cl-symbol-macrolet ((,(setq target-place (make-symbol "place"))
,(cl-case target-type
(list `(car ,target-index))
(vector `(aref ,target-var ,target-index)))))
(cl-loop ,@(nconc loop `(do (setf ,target-place (funcall ,f ,@xs))
;; Bytecode looks better
;; with finally return ..
;; than with finally (cl-return ..).
finally return ,target-var))))))))
(defun cl-map-into (target function &rest sequences)
"Common Lisp's map-into.
Destructively modify TARGET to contain the results of applying
FUNCTION to each element in the argument SEQUENCES in turn.
TARGET and each element of SEQUENCES can each be either a list
or a vector. If TARGET and each element of SEQUENCES are not
all the same length, the iteration terminates when the shortest sequence
(of any of the SEQUENCES or the TARGET) is exhausted. If TARGET
is longer than the shortest element of SEQUENCES, extra elements
at the end of TARGET are left unchanged."
(cl-check-type function function)
(apply
(let* ((sig (apply #'cl--compute-map-into-signature target sequences))
(small (< sig cl--map-into-max-small-signature)))
(with-memoization (if small (aref cl--map-into-mappers-array sig)
;; TODO: Order alist entries for faster lookup
;; (note that we'll have to abandon alist-get then).
(alist-get sig cl--map-into-mappers-alist
nil nil #'=))
(cl--make-map-into-mapper sig)))
function target sequences))
;;; Control structures.
;;;###autoload
(defun cl--mapcar-many (cl-func cl-seqs &optional acc)
(if (cdr (cdr cl-seqs))
(let* ((cl-res nil)
(cl-n (apply #'min (mapcar #'length cl-seqs)))
(cl-i 0)
(cl-args (copy-sequence cl-seqs))
cl-p1 cl-p2)
(setq cl-seqs (copy-sequence cl-seqs))
(while (< cl-i cl-n)
(setq cl-p1 cl-seqs cl-p2 cl-args)
(while cl-p1
(setcar cl-p2
(if (consp (car cl-p1))
(prog1 (car (car cl-p1))
(setcar cl-p1 (cdr (car cl-p1))))
(aref (car cl-p1) cl-i)))
(setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2)))
(if acc
(push (apply cl-func cl-args) cl-res)
(apply cl-func cl-args))
(setq cl-i (1+ cl-i)))
(and acc (nreverse cl-res)))
(let ((cl-res nil)
(cl-x (car cl-seqs))
(cl-y (nth 1 cl-seqs)))
(let ((cl-n (min (length cl-x) (length cl-y)))
(cl-i -1))
(while (< (setq cl-i (1+ cl-i)) cl-n)
(let ((val (funcall cl-func
(if (consp cl-x) (pop cl-x) (aref cl-x cl-i))
(if (consp cl-y) (pop cl-y) (aref cl-y cl-i)))))
(when acc
(push val cl-res)))))
(and acc (nreverse cl-res)))))
;;;###autoload
(defun cl-map (cl-type cl-func cl-seq &rest cl-rest)
"Map a FUNCTION across one or more SEQUENCEs, returning a sequence.
TYPE is the sequence type to return.
\n(fn TYPE FUNCTION SEQUENCE...)"
(let ((cl-res (apply #'cl-mapcar cl-func cl-seq cl-rest)))
(and cl-type (cl-coerce cl-res cl-type))))
;;;###autoload
(defun cl-maplist (cl-func cl-list &rest cl-rest)
"Map FUNCTION to each sublist of LIST or LISTs.
Like `cl-mapcar', except applies to lists and their cdr's rather than to
the elements themselves.
\n(fn FUNCTION LIST...)"
(if cl-rest
(let ((cl-res nil)
(cl-args (cons cl-list (copy-sequence cl-rest)))
cl-p)
(while (not (memq nil cl-args))
(push (apply cl-func cl-args) cl-res)
(setq cl-p cl-args)
(while cl-p (setcar cl-p (cdr (pop cl-p)))))
(nreverse cl-res))
(let ((cl-res nil))
(while cl-list
(push (funcall cl-func cl-list) cl-res)
(setq cl-list (cdr cl-list)))
(nreverse cl-res))))
;;;###autoload
(defun cl-mapc (cl-func cl-seq &rest cl-rest)
"Like `cl-mapcar', but does not accumulate values returned by the function.
\n(fn FUNCTION SEQUENCE...)"
(if cl-rest
(if (or (cdr cl-rest) (nlistp cl-seq) (nlistp (car cl-rest)))
(progn
(cl--mapcar-many cl-func (cons cl-seq cl-rest))
cl-seq)
(let ((cl-x cl-seq) (cl-y (car cl-rest)))
(while (and cl-x cl-y)
(funcall cl-func (pop cl-x) (pop cl-y)))
cl-seq))
(mapc cl-func cl-seq)))
;;;###autoload
(defun cl-mapl (cl-func cl-list &rest cl-rest)
"Like `cl-maplist', but does not accumulate values returned by the function.
\n(fn FUNCTION LIST...)"
(if cl-rest
(let ((cl-args (cons cl-list (copy-sequence cl-rest)))
cl-p)
(while (not (memq nil cl-args))
(apply cl-func cl-args)
(setq cl-p cl-args)
(while cl-p (setcar cl-p (cdr (pop cl-p))))))
(let ((cl-p cl-list))
(while cl-p (funcall cl-func cl-p) (setq cl-p (cdr cl-p)))))
cl-list)
;;;###autoload
(defun cl-mapcan (cl-func cl-seq &rest cl-rest)
"Like `cl-mapcar', but nconc's together the values returned by the function.
\n(fn FUNCTION SEQUENCE...)"
(if cl-rest
(apply #'nconc (apply #'cl-mapcar cl-func cl-seq cl-rest))
(mapcan cl-func cl-seq)))
;;;###autoload
(defun cl-mapcon (cl-func cl-list &rest cl-rest)
"Like `cl-maplist', but nconc's together the values returned by the function.
\n(fn FUNCTION LIST...)"
(apply #'nconc (apply #'cl-maplist cl-func cl-list cl-rest)))
;;;###autoload
(defun cl-some (cl-pred cl-seq &rest cl-rest)
"Say whether PREDICATE is true for any element in the SEQ sequences.
More specifically, the return value of this function will be the
same as the first return value of PREDICATE where PREDICATE has a
non-nil value.
\n(fn PREDICATE SEQ...)"
(if (or cl-rest (nlistp cl-seq))
(catch 'cl-some
(apply #'cl-map nil
(lambda (&rest cl-x)
(let ((cl-res (apply cl-pred cl-x)))
(if cl-res (throw 'cl-some cl-res))))
cl-seq cl-rest) nil)
(let ((cl-x nil))
(while (and cl-seq (not (setq cl-x (funcall cl-pred (pop cl-seq))))))
cl-x)))
;;;###autoload
(defun cl-every (cl-pred cl-seq &rest cl-rest)
"Return true if PREDICATE is true of every element of SEQ or SEQs.
\n(fn PREDICATE SEQ...)"
(if (or cl-rest (nlistp cl-seq))
(catch 'cl-every
(apply #'cl-map nil
(lambda (&rest cl-x)
(or (apply cl-pred cl-x) (throw 'cl-every nil)))
cl-seq cl-rest) t)
(while (and cl-seq (funcall cl-pred (car cl-seq)))
(setq cl-seq (cdr cl-seq)))
(null cl-seq)))
;;;###autoload
(defun cl-notany (cl-pred cl-seq &rest cl-rest)
"Return true if PREDICATE is false of every element of SEQ or SEQs.
\n(fn PREDICATE SEQ...)"
(not (apply #'cl-some cl-pred cl-seq cl-rest)))
;;;###autoload
(defun cl-notevery (cl-pred cl-seq &rest cl-rest)
"Return true if PREDICATE is false of some element of SEQ or SEQs.
\n(fn PREDICATE SEQ...)"
(not (apply #'cl-every cl-pred cl-seq cl-rest)))
;;;###autoload
(defun cl--map-keymap-recursively (cl-func-rec cl-map &optional cl-base)
(or cl-base
(setq cl-base (copy-sequence [0])))
(map-keymap
(lambda (cl-key cl-bind)
(aset cl-base (1- (length cl-base)) cl-key)
(if (keymapp cl-bind)
(cl--map-keymap-recursively
cl-func-rec cl-bind
(vconcat cl-base (list 0)))
(funcall cl-func-rec cl-base cl-bind)))
cl-map))
;;;###autoload
(defun cl--map-intervals (cl-func &optional cl-what cl-prop cl-start cl-end)
(or cl-what (setq cl-what (current-buffer)))
(if (bufferp cl-what)
(let (cl-mark cl-mark2 (cl-next t) cl-next2)
(with-current-buffer cl-what
(setq cl-mark (copy-marker (or cl-start (point-min))))
(setq cl-mark2 (and cl-end (copy-marker cl-end))))
(while (and cl-next (or (not cl-mark2) (< cl-mark cl-mark2)))
(setq cl-next (if cl-prop (next-single-property-change
cl-mark cl-prop cl-what)
(next-property-change cl-mark cl-what))
cl-next2 (or cl-next (with-current-buffer cl-what
(point-max))))
(funcall cl-func (prog1 (marker-position cl-mark)
(set-marker cl-mark cl-next2))
(if cl-mark2 (min cl-next2 cl-mark2) cl-next2)))
(set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil)))
(or cl-start (setq cl-start 0))
(or cl-end (setq cl-end (length cl-what)))
(while (< cl-start cl-end)
(let ((cl-next (or (if cl-prop (next-single-property-change
cl-start cl-prop cl-what)
(next-property-change cl-start cl-what))
cl-end)))
(funcall cl-func cl-start (min cl-next cl-end))
(setq cl-start cl-next)))))
;;;###autoload
(defun cl--map-overlays (cl-func &optional cl-buffer cl-start cl-end cl-arg)
(or cl-buffer (setq cl-buffer (current-buffer)))
(let (cl-ovl)
(with-current-buffer cl-buffer
(setq cl-ovl (overlay-lists))
(if cl-start (setq cl-start (copy-marker cl-start)))
(if cl-end (setq cl-end (copy-marker cl-end))))
(setq cl-ovl (nconc (car cl-ovl) (cdr cl-ovl)))
(while (and cl-ovl
(or (not (overlay-start (car cl-ovl)))
(and cl-end (>= (overlay-start (car cl-ovl)) cl-end))
(and cl-start (<= (overlay-end (car cl-ovl)) cl-start))
(not (funcall cl-func (car cl-ovl) cl-arg))))
(setq cl-ovl (cdr cl-ovl)))
(if cl-start (set-marker cl-start nil))
(if cl-end (set-marker cl-end nil))))
;;; Support for `setf'.
;;;###autoload
(defun cl--set-frame-visible-p (frame val)
(cond ((null val) (make-frame-invisible frame))
((eq val 'icon) (iconify-frame frame))
(t (make-frame-visible frame)))
val)
;;; Numbers.
;;;###autoload
(defun cl-gcd (&rest args)
"Return the greatest common divisor of the arguments."
(let ((a (or (pop args) 0)))
(dolist (b args)
(while (/= b 0)
(setq b (% a (setq a b)))))
(abs a)))
;;;###autoload
(defun cl-lcm (&rest args)
"Return the least common multiple of the arguments."
(if (memq 0 args)
0
(let ((a (or (pop args) 1)))
(dolist (b args)
(setq a (* (/ a (cl-gcd a b)) b)))
(abs a))))
;;;###autoload
(defun cl-isqrt (x)
"Return the integer square root of the (integer) argument X."
(if (and (integerp x) (> x 0))
(let ((g (ash 2 (/ (logb x) 2)))
g2)
(while (< (setq g2 (/ (+ g (/ x g)) 2)) g)
(setq g g2))
g)
(if (eq x 0) 0 (signal 'arith-error nil))))
;;;###autoload
(defun cl-floor (x &optional y)
"Return a list of the floor of X and the fractional part of X.
With two arguments, return floor and remainder of their quotient."
(let ((q (floor x y)))
(list q (- x (if y (* y q) q)))))
;;;###autoload
(defun cl-ceiling (x &optional y)
"Return a list of the ceiling of X and the fractional part of X.
With two arguments, return ceiling and remainder of their quotient."
(let ((res (cl-floor x y)))
(if (= (car (cdr res)) 0) res
(list (1+ (car res)) (- (car (cdr res)) (or y 1))))))
;;;###autoload
(defun cl-truncate (x &optional y)
"Return a list of the integer part of X and the fractional part of X.
With two arguments, return truncation and remainder of their quotient."
(if (eq (>= x 0) (or (null y) (>= y 0)))
(cl-floor x y) (cl-ceiling x y)))
;;;###autoload
(defun cl-round (x &optional y)
"Return a list of X rounded to the nearest integer and the remainder.
With two arguments, return rounding and remainder of their quotient."
(if y
(if (and (integerp x) (integerp y))
(let* ((hy (/ y 2))
(res (cl-floor (+ x hy) y)))
(if (and (= (car (cdr res)) 0)
(= (+ hy hy) y)
(/= (% (car res) 2) 0))
(list (1- (car res)) hy)
(list (car res) (- (car (cdr res)) hy))))
(let ((q (round (/ x y))))
(list q (- x (* q y)))))
(if (integerp x) (list x 0)
(let ((q (round x)))
(list q (- x q))))))
;;;###autoload
(defun cl-mod (x y)
"The remainder of X divided by Y, with the same sign as Y."
(nth 1 (cl-floor x y)))
;;;###autoload
(defun cl-rem (x y)
"The remainder of X divided by Y, with the same sign as X."
(nth 1 (cl-truncate x y)))
;;;###autoload
(defun cl-signum (x)
"Return 1 if X is positive, -1 if negative, 0 if zero."
(cond ((> x 0) 1) ((< x 0) -1) (t 0)))
;;;###autoload
(cl-defun cl-parse-integer (string &key start end radix junk-allowed)
"Parse integer from the substring of STRING from START to END.
STRING may be surrounded by whitespace chars (chars with syntax ` ').
Other non-digit chars are considered junk.
RADIX is an integer between 2 and 36, the default is 10. Signal
an error if the substring between START and END cannot be parsed
as an integer unless JUNK-ALLOWED is non-nil."
(cl-check-type string string)
(let* ((start (or start 0))
(len (length string))
(end (or end len))
(radix (or radix 10)))
(or (<= start end len)
(error "Bad interval: [%d, %d)" start end))
(cl-flet ((skip-whitespace ()
(while (and (< start end)
(= 32 (char-syntax (aref string start))))
(setq start (1+ start)))))
(skip-whitespace)
(let ((sign (cl-case (and (< start end) (aref string start))
(?+ (cl-incf start) +1)
(?- (cl-incf start) -1)
(t +1)))
digit sum)
(while (and (< start end)
(setq digit (cl-digit-char-p (aref string start) radix)))
(setq sum (+ (* (or sum 0) radix) digit)
start (1+ start)))
(skip-whitespace)
(cond ((and junk-allowed (null sum)) sum)
(junk-allowed (* sign sum))
((or (/= start end) (null sum))
(error "Not an integer string: `%s'" string))
(t (* sign sum)))))))
;; Random numbers.
(defun cl--random-time ()
(car (time-convert nil t)))
;;;###autoload (autoload 'cl-random-state-p "cl-extra")
(cl-defstruct (cl--random-state
(:copier nil)
(:predicate cl-random-state-p)
(:constructor nil)
(:constructor cl--make-random-state (vec)))
(i -1) (j 30) vec)
(defvar cl--random-state (cl--make-random-state (cl--random-time)))
;;;###autoload
(defun cl-random (lim &optional state)
"Return a pseudo-random nonnegative number less than LIM, an integer or float.
Optional second arg STATE is a random-state object."
(or state (setq state cl--random-state))
;; Inspired by "ran3" from Numerical Recipes. Additive congruential method.
(let ((vec (cl--random-state-vec state)))
(if (integerp vec)
(let ((i 0) (j (- 1357335 (abs (% vec 1357333)))) (k 1))
(setf (cl--random-state-vec state)
(setq vec (make-vector 55 nil)))
(aset vec 0 j)
(while (> (setq i (% (+ i 21) 55)) 0)
(aset vec i (setq j (prog1 k (setq k (- j k))))))
(while (< (setq i (1+ i)) 200) (cl-random 2 state))))
(let* ((i (cl-callf (lambda (x) (% (1+ x) 55)) (cl--random-state-i state)))
(j (cl-callf (lambda (x) (% (1+ x) 55)) (cl--random-state-j state)))
(n (aset vec i (logand 8388607 (- (aref vec i) (aref vec j))))))
(if (integerp lim)
(if (<= lim 512) (% n lim)
(if (> lim 8388607) (setq n (+ (ash n 9) (cl-random 512 state))))
(let ((mask 1023))
(while (< mask (1- lim)) (setq mask (1+ (+ mask mask))))
(if (< (setq n (logand n mask)) lim) n (cl-random lim state))))
(* (/ n '8388608e0) lim)))))
;;;###autoload
(defun cl-make-random-state (&optional state)
"Return a copy of random-state STATE, or of the internal state if omitted.
If STATE is t, return a new state object seeded from the time of day."
(unless state (setq state cl--random-state))
(if (cl-random-state-p state)
(copy-sequence state)
(cl--make-random-state (if (integerp state) state (cl--random-time)))))
;; Implementation limits.
(defun cl--finite-do (func a b)
(condition-case _
(let ((res (funcall func a b))) ; check for IEEE infinity
(and (numberp res) (/= res (/ res 2)) res))
(arith-error nil)))
;;;###autoload
(defun cl-float-limits ()
"Initialize the Common Lisp floating-point parameters.
This sets the values of: `cl-most-positive-float', `cl-most-negative-float',
`cl-least-positive-float', `cl-least-negative-float', `cl-float-epsilon',
`cl-float-negative-epsilon', `cl-least-positive-normalized-float', and
`cl-least-negative-normalized-float'."
(or cl-most-positive-float (not (numberp '2e1))
(let ((x '2e0) y z)
;; Find maximum exponent (first two loops are optimizations)
(while (cl--finite-do '* x x) (setq x (* x x)))
(while (cl--finite-do '* x (/ x 2)) (setq x (* x (/ x 2))))
(while (cl--finite-do '+ x x) (setq x (+ x x)))
(setq z x y (/ x 2))
;; Now cl-fill in 1's in the mantissa.
(while (and (cl--finite-do '+ x y) (/= (+ x y) x))
(setq x (+ x y) y (/ y 2)))
(setq cl-most-positive-float x
cl-most-negative-float (- x))
;; Divide down until mantissa starts rounding.
(setq x (/ x z) y (/ 16 z) x (* x y))
(while (condition-case _ (and (= x (* (/ x 2) 2)) (> (/ y 2) 0))
(arith-error nil))
(setq x (/ x 2) y (/ y 2)))
(setq cl-least-positive-normalized-float y
cl-least-negative-normalized-float (- y))
;; Divide down until value underflows to zero.
(setq x (/ z) y x)
(while (condition-case _ (> (/ x 2) 0) (arith-error nil))
(setq x (/ x 2)))
(setq cl-least-positive-float x
cl-least-negative-float (- x))
(setq x '1e0)
(while (/= (+ '1e0 x) '1e0) (setq x (/ x 2)))
(setq cl-float-epsilon (* x 2))
(setq x '1e0)
(while (/= (- '1e0 x) '1e0) (setq x (/ x 2)))
(setq cl-float-negative-epsilon (* x 2))))
nil)
;;; Sequence functions.
;;;###autoload
(defun cl-subseq (seq start &optional end)
"Return the subsequence of SEQ from START to END.
If END is omitted, it defaults to the length of the sequence.
If START or END is negative, it counts from the end.
Signal an error if START or END are outside of the sequence (i.e
too large if positive or too small if negative)."
(declare (gv-setter
(lambda (new)
(macroexp-let2 nil new new
`(progn (cl-replace ,seq ,new :start1 ,start :end1 ,end)
,new)))))
(seq-subseq seq start end))
;;;###autoload
(defalias 'cl-concatenate #'seq-concatenate
"Concatenate, into a sequence of type TYPE, the argument SEQUENCEs.
\n(fn TYPE SEQUENCE...)")
;;; List functions.
;;;###autoload
(defun cl-revappend (x y)
"Equivalent to (append (reverse X) Y)."
(nconc (reverse x) y))
;;;###autoload
(defun cl-nreconc (x y)
"Equivalent to (nconc (nreverse X) Y)."
(nconc (nreverse x) y))
;;;###autoload
(defun cl-list-length (x)
"Return the length of list X. Return nil if list is circular."
(cl-check-type x list)
(condition-case nil
(length x)
(circular-list)))
;;;###autoload
(defun cl-tailp (sublist list)
"Return true if SUBLIST is a tail of LIST."
(while (and (consp list) (not (eq sublist list)))
(setq list (cdr list)))
(if (numberp sublist) (equal sublist list) (eq sublist list)))
;;; Property lists.
;;;###autoload
(defun cl-get (sym tag &optional def)
"Return the value of SYMBOL's PROPNAME property, or DEFAULT if none.
\n(fn SYMBOL PROPNAME &optional DEFAULT)"
(declare (compiler-macro cl--compiler-macro-get)
(gv-setter (lambda (store) (ignore def) `(put ,sym ,tag ,store))))
(cl-getf (symbol-plist sym) tag def))
(autoload 'cl--compiler-macro-get "cl-macs")
;;;###autoload
(defun cl-getf (plist tag &optional def)
"Search PROPLIST for property PROPNAME; return its value or DEFAULT.
PROPLIST is a list of the sort returned by `symbol-plist'.
\n(fn PROPLIST PROPNAME &optional DEFAULT)"
(declare (gv-expander
(lambda (do)
(gv-letplace (getter setter) plist
(macroexp-let2* nil ((k tag) (d def))
(funcall do `(cl-getf ,getter ,k ,d)
(lambda (v)
(macroexp-let2 nil val v
`(progn
,(funcall setter
`(cl--set-getf ,getter ,k ,val))
,val)))))))))
(let ((val-tail (cdr-safe (plist-member plist tag))))
(if val-tail (car val-tail) def)))
;;;###autoload
(defun cl--set-getf (plist tag val)
(let ((val-tail (cdr-safe (plist-member plist tag))))
(if val-tail (progn (setcar val-tail val) plist)
(cl-list* tag val plist))))
;;;###autoload
(defun cl--do-remf (plist tag)
(let ((p (cdr plist)))
;; Can't use `plist-member' here because it goes to the cons-cell
;; of TAG and we need the one before.
(while (and (cdr p) (not (eq (car (cdr p)) tag))) (setq p (cdr (cdr p))))
(and (cdr p) (progn (setcdr p (cdr (cdr (cdr p)))) t))))
;;;###autoload
(defun cl-remprop (sym tag)
"Remove from SYMBOL's plist the property PROPNAME and its value.
\n(fn SYMBOL PROPNAME)"
(let ((plist (symbol-plist sym)))
(if (and plist (eq tag (car plist)))
(progn (setplist sym (cdr (cdr plist))) t)
(cl--do-remf plist tag))))
;;; Streams.
;;;###autoload
(defun cl-fresh-line (&optional stream)
"Output a newline unless already at the beginning of a line."
(terpri stream 'ensure))
;;; Some debugging aids.
(defun cl-prettyprint (form)
"Insert a pretty-printed rendition of a Lisp FORM in current buffer."
(let ((pt (point)) last)
(insert "\n" (prin1-to-string form) "\n")
(setq last (point))
(goto-char (1+ pt))
(while (search-forward "(quote " last t)
(delete-char -7)
(insert "'")
(forward-sexp)
(delete-char 1))
(goto-char (1+ pt))
(cl--do-prettyprint)))
(defun cl--do-prettyprint ()
(skip-chars-forward " ")
(if (looking-at "(")
(let ((skip (or (looking-at "((") (looking-at "(prog")
(looking-at "(unwind-protect ")
(looking-at "(function (")
(looking-at "(cl--block-wrapper ")))
(two (or (looking-at "(defun ") (looking-at "(defmacro ")))
(let (or (looking-at "(let\\*? ") (looking-at "(while ")))
(set (looking-at "(p?set[qf] ")))
(if (or skip let
(progn
(forward-sexp)
(and (>= (current-column) 78) (progn (backward-sexp) t))))
(let ((nl t))
(forward-char 1)
(cl--do-prettyprint)
(or skip (looking-at ")") (cl--do-prettyprint))
(or (not two) (looking-at ")") (cl--do-prettyprint))
(while (not (looking-at ")"))
(if set (setq nl (not nl)))
(if nl (insert "\n"))
(lisp-indent-line)
(cl--do-prettyprint))
(forward-char 1))))
(forward-sexp)))
;;;###autoload
(defun cl-prettyexpand (form &optional _full)
"Expand macros in FORM and insert the pretty-printed result."
(declare (advertised-calling-convention (form) "27.1"))
(message "Expanding...")
(setq form (macroexpand-all form))
(message "Formatting...")
(prog1
(cl-prettyprint form)
(message "")))
;;; Integration into the online help system.
(eval-when-compile (require 'cl-macs)) ;Explicitly, for cl--find-class.
(require 'help-mode)
;; FIXME: We could go crazy and add another entry so describe-symbol can be
;; used with the slot names of CL structs (and/or EIEIO objects).
(add-to-list 'describe-symbol-backends
`(nil ,#'cl-find-class ,(lambda (s _b _f) (cl-describe-type s))))
(defconst cl--typedef-regexp
(concat "(" (regexp-opt '("defclass" "defstruct" "cl-defstruct"
"cl-deftype" "deftype"))
"[ \t\r\n]+%s[ \t\r\n]+"))
(with-eval-after-load 'find-func
(defvar find-function-regexp-alist)
(add-to-list 'find-function-regexp-alist
'(define-type . cl--typedef-regexp)))
(define-button-type 'cl-help-type
:supertype 'help-function-def
'help-function #'cl-describe-type
'help-echo (purecopy "mouse-2, RET: describe this type"))
(define-button-type 'cl-type-definition
:supertype 'help-function-def
'help-echo (purecopy "mouse-2, RET: find type definition"))
(declare-function help-fns-short-filename "help-fns" (filename))
;;;###autoload
(defun cl-find-class (type) (cl--find-class type))
;;;###autoload
(defun cl-describe-type (type)
"Display the documentation for type TYPE (a symbol)."
(interactive
(let ((str (completing-read "Describe type: " obarray #'cl-find-class t)))
(if (<= (length str) 0)
(user-error "Abort!")
(list (intern str)))))
(help-setup-xref (list #'cl-describe-type type)
(called-interactively-p 'interactive))
(save-excursion
(with-help-window (help-buffer)
(with-current-buffer standard-output
(let ((class (cl-find-class type)))
(if class
(cl--describe-class type class)
;; FIXME: Describe other types (the built-in ones, or those from
;; cl-deftype).
(user-error "Unknown type %S" type))))
(with-current-buffer standard-output
;; Return the text we displayed.
(buffer-string)))))
(defun cl--describe-class (type &optional class)
(unless class (setq class (cl--find-class type)))
(let ((location (find-lisp-object-file-name type 'define-type))
(metatype (type-of class)))
(insert (symbol-name type)
(substitute-command-keys " is a type (of kind `"))
(help-insert-xref-button (symbol-name metatype)
'cl-help-type metatype)
(insert (substitute-command-keys "')"))
(when location
(insert (substitute-command-keys " in `"))
(help-insert-xref-button
(help-fns-short-filename location)
'cl-type-definition type location 'define-type)
(insert (substitute-command-keys "'")))
(insert ".\n")
;; Parents.
(let ((pl (cl--class-parents class))
cur)
(when pl
(insert " Inherits from ")
(while (setq cur (pop pl))
(setq cur (cl--class-name cur))
(insert (substitute-command-keys "`"))
(help-insert-xref-button (symbol-name cur)
'cl-help-type cur)
(insert (substitute-command-keys (if pl "', " "'"))))
(insert ".\n")))
;; Children, if available. ¡For EIEIO!
(let ((ch (condition-case nil
(cl-struct-slot-value metatype 'children class)
(cl-struct-unknown-slot nil)))
cur)
(when ch
(insert " Children ")
(while (setq cur (pop ch))
(insert (substitute-command-keys "`"))
(help-insert-xref-button (symbol-name cur)
'cl-help-type cur)
(insert (substitute-command-keys (if ch "', " "'"))))
(insert ".\n")))
;; Type's documentation.
(let ((doc (cl--class-docstring class)))
(when doc
(insert "\n" doc "\n\n")))
;; Describe all the slots in this class.
(cl--describe-class-slots class)
;; Describe all the methods specific to this class.
(let ((generics (cl-generic-all-functions type)))
(when generics
(insert (propertize "Specialized Methods:\n\n" 'face 'bold))
(dolist (generic generics)
(insert (substitute-command-keys "`"))
(help-insert-xref-button (symbol-name generic)
'help-function generic)
(insert (substitute-command-keys "'"))
(pcase-dolist (`(,qualifiers ,args ,doc)
(cl--generic-method-documentation generic type))
(insert (format " %s%S\n" qualifiers args)
(or doc "")))
(insert "\n\n"))))))
(defun cl--describe-class-slot (slot)
(insert
(concat
(propertize "Slot: " 'face 'bold)
(prin1-to-string (cl--slot-descriptor-name slot))
(unless (eq (cl--slot-descriptor-type slot) t)
(concat " type = "
(prin1-to-string (cl--slot-descriptor-type slot))))
;; FIXME: The default init form is treated differently for structs and for
;; eieio objects: for structs, the default is nil, for eieio-objects
;; it's a special "unbound" value.
(unless nil ;; (eq (cl--slot-descriptor-initform slot) eieio-unbound)
(concat " default = "
(prin1-to-string (cl--slot-descriptor-initform slot))))
(when (alist-get :printer (cl--slot-descriptor-props slot))
(concat " printer = "
(prin1-to-string
(alist-get :printer (cl--slot-descriptor-props slot)))))
(when (alist-get :documentation (cl--slot-descriptor-props slot))
(concat "\n "
(substitute-command-keys
(alist-get :documentation (cl--slot-descriptor-props slot)))
"\n")))
"\n"))
(defun cl--print-table (header rows &optional last-slot-on-next-line)
;; FIXME: Isn't this functionality already implemented elsewhere?
(let ((cols (apply #'vector (mapcar #'string-width header)))
(col-space 2))
(dolist (row rows)
(dotimes (i (length cols))
(let* ((x (pop row))
(curwidth (aref cols i))
(newwidth (if x (string-width x) 0)))
(if (> newwidth curwidth)
(setf (aref cols i) newwidth)))))
(let ((formats '())
(col 0))
(dotimes (i (length cols))
(push (concat (propertize " "
'display
`(space :align-to ,(+ col col-space)))
"%s")
formats)
(cl-incf col (+ col-space (aref cols i))))
(let ((format (mapconcat #'identity (nreverse formats) "")))
(insert (apply #'format format
(mapcar (lambda (str) (propertize str 'face 'italic))
header))
"\n")
(insert (apply #'format format
(mapcar (lambda (str) (make-string (string-width str) ?—))
header))
"\n")
(dolist (row rows)
(insert (apply #'format format row) "\n")
(when last-slot-on-next-line
(dolist (line (string-lines (car (last row))))
(insert " " line "\n"))
(insert "\n")))))))
(defun cl--describe-class-slots (class)
"Print help description for the slots in CLASS.
Outputs to the current buffer."
(let* ((slots (cl--class-slots class))
(metatype (type-of class))
;; ¡For EIEIO!
(cslots (condition-case nil
(cl-struct-slot-value metatype 'class-slots class)
(cl-struct-unknown-slot nil))))
(insert (propertize "Instance Allocated Slots:\n\n"
'face 'bold))
(let* ((has-doc nil)
(slots-strings
(mapcar
(lambda (slot)
(list (cl-prin1-to-string (cl--slot-descriptor-name slot))
(cl-prin1-to-string (cl--slot-descriptor-type slot))
(cl-prin1-to-string (cl--slot-descriptor-initform slot))
(let ((doc (alist-get :documentation
(cl--slot-descriptor-props slot))))
(if (not doc) ""
(setq has-doc t)
(substitute-command-keys doc)))))
slots)))
(cl--print-table `("Name" "Type" "Default") slots-strings has-doc))
(insert "\n")
(when (> (length cslots) 0)
(insert (propertize "\nClass Allocated Slots:\n\n" 'face 'bold))
(mapc #'cl--describe-class-slot cslots))))
(make-obsolete-variable 'cl-extra-load-hook
"use `with-eval-after-load' instead." "28.1")
(run-hooks 'cl-extra-load-hook)
;; Local variables:
;; generated-autoload-file: "cl-loaddefs.el"
;; End:
(provide 'cl-extra)
;;; cl-extra.el ends here
|