]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/cl-extra.el
Update copyright notices for 2013.
[gnu-emacs] / lisp / emacs-lisp / cl-extra.el
1 ;;; cl-extra.el --- Common Lisp features, part 2 -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 1993, 2000-2013 Free Software Foundation, Inc.
4
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
6 ;; Keywords: extensions
7 ;; Package: emacs
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; These are extensions to Emacs Lisp that provide a degree of
27 ;; Common Lisp compatibility, beyond what is already built-in
28 ;; in Emacs Lisp.
29 ;;
30 ;; This package was written by Dave Gillespie; it is a complete
31 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
32 ;;
33 ;; Bug reports, comments, and suggestions are welcome!
34
35 ;; This file contains portions of the Common Lisp extensions
36 ;; package which are autoloaded since they are relatively obscure.
37
38 ;;; Code:
39
40 (require 'cl-lib)
41
42 ;;; Type coercion.
43
44 ;;;###autoload
45 (defun cl-coerce (x type)
46 "Coerce OBJECT to type TYPE.
47 TYPE is a Common Lisp type specifier.
48 \n(fn OBJECT TYPE)"
49 (cond ((eq type 'list) (if (listp x) x (append x nil)))
50 ((eq type 'vector) (if (vectorp x) x (vconcat x)))
51 ((eq type 'string) (if (stringp x) x (concat x)))
52 ((eq type 'array) (if (arrayp x) x (vconcat x)))
53 ((and (eq type 'character) (stringp x) (= (length x) 1)) (aref x 0))
54 ((and (eq type 'character) (symbolp x)) (cl-coerce (symbol-name x) type))
55 ((eq type 'float) (float x))
56 ((cl-typep x type) x)
57 (t (error "Can't coerce %s to type %s" x type))))
58
59
60 ;;; Predicates.
61
62 ;;;###autoload
63 (defun cl-equalp (x y)
64 "Return t if two Lisp objects have similar structures and contents.
65 This is like `equal', except that it accepts numerically equal
66 numbers of different types (float vs. integer), and also compares
67 strings case-insensitively."
68 (cond ((eq x y) t)
69 ((stringp x)
70 (and (stringp y) (= (length x) (length y))
71 (or (string-equal x y)
72 (string-equal (downcase x) (downcase y))))) ; lazy but simple!
73 ((numberp x)
74 (and (numberp y) (= x y)))
75 ((consp x)
76 (while (and (consp x) (consp y) (cl-equalp (car x) (car y)))
77 (setq x (cdr x) y (cdr y)))
78 (and (not (consp x)) (cl-equalp x y)))
79 ((vectorp x)
80 (and (vectorp y) (= (length x) (length y))
81 (let ((i (length x)))
82 (while (and (>= (setq i (1- i)) 0)
83 (cl-equalp (aref x i) (aref y i))))
84 (< i 0))))
85 (t (equal x y))))
86
87
88 ;;; Control structures.
89
90 ;;;###autoload
91 (defun cl--mapcar-many (cl-func cl-seqs)
92 (if (cdr (cdr cl-seqs))
93 (let* ((cl-res nil)
94 (cl-n (apply 'min (mapcar 'length cl-seqs)))
95 (cl-i 0)
96 (cl-args (copy-sequence cl-seqs))
97 cl-p1 cl-p2)
98 (setq cl-seqs (copy-sequence cl-seqs))
99 (while (< cl-i cl-n)
100 (setq cl-p1 cl-seqs cl-p2 cl-args)
101 (while cl-p1
102 (setcar cl-p2
103 (if (consp (car cl-p1))
104 (prog1 (car (car cl-p1))
105 (setcar cl-p1 (cdr (car cl-p1))))
106 (aref (car cl-p1) cl-i)))
107 (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2)))
108 (push (apply cl-func cl-args) cl-res)
109 (setq cl-i (1+ cl-i)))
110 (nreverse cl-res))
111 (let ((cl-res nil)
112 (cl-x (car cl-seqs))
113 (cl-y (nth 1 cl-seqs)))
114 (let ((cl-n (min (length cl-x) (length cl-y)))
115 (cl-i -1))
116 (while (< (setq cl-i (1+ cl-i)) cl-n)
117 (push (funcall cl-func
118 (if (consp cl-x) (pop cl-x) (aref cl-x cl-i))
119 (if (consp cl-y) (pop cl-y) (aref cl-y cl-i)))
120 cl-res)))
121 (nreverse cl-res))))
122
123 ;;;###autoload
124 (defun cl-map (cl-type cl-func cl-seq &rest cl-rest)
125 "Map a FUNCTION across one or more SEQUENCEs, returning a sequence.
126 TYPE is the sequence type to return.
127 \n(fn TYPE FUNCTION SEQUENCE...)"
128 (let ((cl-res (apply 'cl-mapcar cl-func cl-seq cl-rest)))
129 (and cl-type (cl-coerce cl-res cl-type))))
130
131 ;;;###autoload
132 (defun cl-maplist (cl-func cl-list &rest cl-rest)
133 "Map FUNCTION to each sublist of LIST or LISTs.
134 Like `cl-mapcar', except applies to lists and their cdr's rather than to
135 the elements themselves.
136 \n(fn FUNCTION LIST...)"
137 (if cl-rest
138 (let ((cl-res nil)
139 (cl-args (cons cl-list (copy-sequence cl-rest)))
140 cl-p)
141 (while (not (memq nil cl-args))
142 (push (apply cl-func cl-args) cl-res)
143 (setq cl-p cl-args)
144 (while cl-p (setcar cl-p (cdr (pop cl-p)) )))
145 (nreverse cl-res))
146 (let ((cl-res nil))
147 (while cl-list
148 (push (funcall cl-func cl-list) cl-res)
149 (setq cl-list (cdr cl-list)))
150 (nreverse cl-res))))
151
152 ;;;###autoload
153 (defun cl-mapc (cl-func cl-seq &rest cl-rest)
154 "Like `cl-mapcar', but does not accumulate values returned by the function.
155 \n(fn FUNCTION SEQUENCE...)"
156 (if cl-rest
157 (progn (apply 'cl-map nil cl-func cl-seq cl-rest)
158 cl-seq)
159 (mapc cl-func cl-seq)))
160
161 ;;;###autoload
162 (defun cl-mapl (cl-func cl-list &rest cl-rest)
163 "Like `cl-maplist', but does not accumulate values returned by the function.
164 \n(fn FUNCTION LIST...)"
165 (if cl-rest
166 (apply 'cl-maplist cl-func cl-list cl-rest)
167 (let ((cl-p cl-list))
168 (while cl-p (funcall cl-func cl-p) (setq cl-p (cdr cl-p)))))
169 cl-list)
170
171 ;;;###autoload
172 (defun cl-mapcan (cl-func cl-seq &rest cl-rest)
173 "Like `cl-mapcar', but nconc's together the values returned by the function.
174 \n(fn FUNCTION SEQUENCE...)"
175 (apply 'nconc (apply 'cl-mapcar cl-func cl-seq cl-rest)))
176
177 ;;;###autoload
178 (defun cl-mapcon (cl-func cl-list &rest cl-rest)
179 "Like `cl-maplist', but nconc's together the values returned by the function.
180 \n(fn FUNCTION LIST...)"
181 (apply 'nconc (apply 'cl-maplist cl-func cl-list cl-rest)))
182
183 ;;;###autoload
184 (defun cl-some (cl-pred cl-seq &rest cl-rest)
185 "Return true if PREDICATE is true of any element of SEQ or SEQs.
186 If so, return the true (non-nil) value returned by PREDICATE.
187 \n(fn PREDICATE SEQ...)"
188 (if (or cl-rest (nlistp cl-seq))
189 (catch 'cl-some
190 (apply 'cl-map nil
191 (function (lambda (&rest cl-x)
192 (let ((cl-res (apply cl-pred cl-x)))
193 (if cl-res (throw 'cl-some cl-res)))))
194 cl-seq cl-rest) nil)
195 (let ((cl-x nil))
196 (while (and cl-seq (not (setq cl-x (funcall cl-pred (pop cl-seq))))))
197 cl-x)))
198
199 ;;;###autoload
200 (defun cl-every (cl-pred cl-seq &rest cl-rest)
201 "Return true if PREDICATE is true of every element of SEQ or SEQs.
202 \n(fn PREDICATE SEQ...)"
203 (if (or cl-rest (nlistp cl-seq))
204 (catch 'cl-every
205 (apply 'cl-map nil
206 (function (lambda (&rest cl-x)
207 (or (apply cl-pred cl-x) (throw 'cl-every nil))))
208 cl-seq cl-rest) t)
209 (while (and cl-seq (funcall cl-pred (car cl-seq)))
210 (setq cl-seq (cdr cl-seq)))
211 (null cl-seq)))
212
213 ;;;###autoload
214 (defun cl-notany (cl-pred cl-seq &rest cl-rest)
215 "Return true if PREDICATE is false of every element of SEQ or SEQs.
216 \n(fn PREDICATE SEQ...)"
217 (not (apply 'cl-some cl-pred cl-seq cl-rest)))
218
219 ;;;###autoload
220 (defun cl-notevery (cl-pred cl-seq &rest cl-rest)
221 "Return true if PREDICATE is false of some element of SEQ or SEQs.
222 \n(fn PREDICATE SEQ...)"
223 (not (apply 'cl-every cl-pred cl-seq cl-rest)))
224
225 ;;;###autoload
226 (defun cl--map-keymap-recursively (cl-func-rec cl-map &optional cl-base)
227 (or cl-base
228 (setq cl-base (copy-sequence [0])))
229 (map-keymap
230 (function
231 (lambda (cl-key cl-bind)
232 (aset cl-base (1- (length cl-base)) cl-key)
233 (if (keymapp cl-bind)
234 (cl--map-keymap-recursively
235 cl-func-rec cl-bind
236 (vconcat cl-base (list 0)))
237 (funcall cl-func-rec cl-base cl-bind))))
238 cl-map))
239
240 ;;;###autoload
241 (defun cl--map-intervals (cl-func &optional cl-what cl-prop cl-start cl-end)
242 (or cl-what (setq cl-what (current-buffer)))
243 (if (bufferp cl-what)
244 (let (cl-mark cl-mark2 (cl-next t) cl-next2)
245 (with-current-buffer cl-what
246 (setq cl-mark (copy-marker (or cl-start (point-min))))
247 (setq cl-mark2 (and cl-end (copy-marker cl-end))))
248 (while (and cl-next (or (not cl-mark2) (< cl-mark cl-mark2)))
249 (setq cl-next (if cl-prop (next-single-property-change
250 cl-mark cl-prop cl-what)
251 (next-property-change cl-mark cl-what))
252 cl-next2 (or cl-next (with-current-buffer cl-what
253 (point-max))))
254 (funcall cl-func (prog1 (marker-position cl-mark)
255 (set-marker cl-mark cl-next2))
256 (if cl-mark2 (min cl-next2 cl-mark2) cl-next2)))
257 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil)))
258 (or cl-start (setq cl-start 0))
259 (or cl-end (setq cl-end (length cl-what)))
260 (while (< cl-start cl-end)
261 (let ((cl-next (or (if cl-prop (next-single-property-change
262 cl-start cl-prop cl-what)
263 (next-property-change cl-start cl-what))
264 cl-end)))
265 (funcall cl-func cl-start (min cl-next cl-end))
266 (setq cl-start cl-next)))))
267
268 ;;;###autoload
269 (defun cl--map-overlays (cl-func &optional cl-buffer cl-start cl-end cl-arg)
270 (or cl-buffer (setq cl-buffer (current-buffer)))
271 (if (fboundp 'overlay-lists)
272
273 ;; This is the preferred algorithm, though overlay-lists is undocumented.
274 (let (cl-ovl)
275 (with-current-buffer cl-buffer
276 (setq cl-ovl (overlay-lists))
277 (if cl-start (setq cl-start (copy-marker cl-start)))
278 (if cl-end (setq cl-end (copy-marker cl-end))))
279 (setq cl-ovl (nconc (car cl-ovl) (cdr cl-ovl)))
280 (while (and cl-ovl
281 (or (not (overlay-start (car cl-ovl)))
282 (and cl-end (>= (overlay-start (car cl-ovl)) cl-end))
283 (and cl-start (<= (overlay-end (car cl-ovl)) cl-start))
284 (not (funcall cl-func (car cl-ovl) cl-arg))))
285 (setq cl-ovl (cdr cl-ovl)))
286 (if cl-start (set-marker cl-start nil))
287 (if cl-end (set-marker cl-end nil)))
288
289 ;; This alternate algorithm fails to find zero-length overlays.
290 (let ((cl-mark (with-current-buffer cl-buffer
291 (copy-marker (or cl-start (point-min)))))
292 (cl-mark2 (and cl-end (with-current-buffer cl-buffer
293 (copy-marker cl-end))))
294 cl-pos cl-ovl)
295 (while (save-excursion
296 (and (setq cl-pos (marker-position cl-mark))
297 (< cl-pos (or cl-mark2 (point-max)))
298 (progn
299 (set-buffer cl-buffer)
300 (setq cl-ovl (overlays-at cl-pos))
301 (set-marker cl-mark (next-overlay-change cl-pos)))))
302 (while (and cl-ovl
303 (or (/= (overlay-start (car cl-ovl)) cl-pos)
304 (not (and (funcall cl-func (car cl-ovl) cl-arg)
305 (set-marker cl-mark nil)))))
306 (setq cl-ovl (cdr cl-ovl))))
307 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil)))))
308
309 ;;; Support for `setf'.
310 ;;;###autoload
311 (defun cl--set-frame-visible-p (frame val)
312 (cond ((null val) (make-frame-invisible frame))
313 ((eq val 'icon) (iconify-frame frame))
314 (t (make-frame-visible frame)))
315 val)
316
317
318 ;;; Numbers.
319
320 ;;;###autoload
321 (defun cl-gcd (&rest args)
322 "Return the greatest common divisor of the arguments."
323 (let ((a (abs (or (pop args) 0))))
324 (while args
325 (let ((b (abs (pop args))))
326 (while (> b 0) (setq b (% a (setq a b))))))
327 a))
328
329 ;;;###autoload
330 (defun cl-lcm (&rest args)
331 "Return the least common multiple of the arguments."
332 (if (memq 0 args)
333 0
334 (let ((a (abs (or (pop args) 1))))
335 (while args
336 (let ((b (abs (pop args))))
337 (setq a (* (/ a (cl-gcd a b)) b))))
338 a)))
339
340 ;;;###autoload
341 (defun cl-isqrt (x)
342 "Return the integer square root of the argument."
343 (if (and (integerp x) (> x 0))
344 (let ((g (cond ((<= x 100) 10) ((<= x 10000) 100)
345 ((<= x 1000000) 1000) (t x)))
346 g2)
347 (while (< (setq g2 (/ (+ g (/ x g)) 2)) g)
348 (setq g g2))
349 g)
350 (if (eq x 0) 0 (signal 'arith-error nil))))
351
352 ;;;###autoload
353 (defun cl-floor (x &optional y)
354 "Return a list of the floor of X and the fractional part of X.
355 With two arguments, return floor and remainder of their quotient."
356 (let ((q (floor x y)))
357 (list q (- x (if y (* y q) q)))))
358
359 ;;;###autoload
360 (defun cl-ceiling (x &optional y)
361 "Return a list of the ceiling of X and the fractional part of X.
362 With two arguments, return ceiling and remainder of their quotient."
363 (let ((res (cl-floor x y)))
364 (if (= (car (cdr res)) 0) res
365 (list (1+ (car res)) (- (car (cdr res)) (or y 1))))))
366
367 ;;;###autoload
368 (defun cl-truncate (x &optional y)
369 "Return a list of the integer part of X and the fractional part of X.
370 With two arguments, return truncation and remainder of their quotient."
371 (if (eq (>= x 0) (or (null y) (>= y 0)))
372 (cl-floor x y) (cl-ceiling x y)))
373
374 ;;;###autoload
375 (defun cl-round (x &optional y)
376 "Return a list of X rounded to the nearest integer and the remainder.
377 With two arguments, return rounding and remainder of their quotient."
378 (if y
379 (if (and (integerp x) (integerp y))
380 (let* ((hy (/ y 2))
381 (res (cl-floor (+ x hy) y)))
382 (if (and (= (car (cdr res)) 0)
383 (= (+ hy hy) y)
384 (/= (% (car res) 2) 0))
385 (list (1- (car res)) hy)
386 (list (car res) (- (car (cdr res)) hy))))
387 (let ((q (round (/ x y))))
388 (list q (- x (* q y)))))
389 (if (integerp x) (list x 0)
390 (let ((q (round x)))
391 (list q (- x q))))))
392
393 ;;;###autoload
394 (defun cl-mod (x y)
395 "The remainder of X divided by Y, with the same sign as Y."
396 (nth 1 (cl-floor x y)))
397
398 ;;;###autoload
399 (defun cl-rem (x y)
400 "The remainder of X divided by Y, with the same sign as X."
401 (nth 1 (cl-truncate x y)))
402
403 ;;;###autoload
404 (defun cl-signum (x)
405 "Return 1 if X is positive, -1 if negative, 0 if zero."
406 (cond ((> x 0) 1) ((< x 0) -1) (t 0)))
407
408
409 ;; Random numbers.
410
411 ;;;###autoload
412 (defun cl-random (lim &optional state)
413 "Return a random nonnegative number less than LIM, an integer or float.
414 Optional second arg STATE is a random-state object."
415 (or state (setq state cl--random-state))
416 ;; Inspired by "ran3" from Numerical Recipes. Additive congruential method.
417 (let ((vec (aref state 3)))
418 (if (integerp vec)
419 (let ((i 0) (j (- 1357335 (% (abs vec) 1357333))) (k 1))
420 (aset state 3 (setq vec (make-vector 55 nil)))
421 (aset vec 0 j)
422 (while (> (setq i (% (+ i 21) 55)) 0)
423 (aset vec i (setq j (prog1 k (setq k (- j k))))))
424 (while (< (setq i (1+ i)) 200) (cl-random 2 state))))
425 (let* ((i (aset state 1 (% (1+ (aref state 1)) 55)))
426 (j (aset state 2 (% (1+ (aref state 2)) 55)))
427 (n (logand 8388607 (aset vec i (- (aref vec i) (aref vec j))))))
428 (if (integerp lim)
429 (if (<= lim 512) (% n lim)
430 (if (> lim 8388607) (setq n (+ (lsh n 9) (cl-random 512 state))))
431 (let ((mask 1023))
432 (while (< mask (1- lim)) (setq mask (1+ (+ mask mask))))
433 (if (< (setq n (logand n mask)) lim) n (cl-random lim state))))
434 (* (/ n '8388608e0) lim)))))
435
436 ;;;###autoload
437 (defun cl-make-random-state (&optional state)
438 "Return a copy of random-state STATE, or of the internal state if omitted.
439 If STATE is t, return a new state object seeded from the time of day."
440 (cond ((null state) (cl-make-random-state cl--random-state))
441 ((vectorp state) (copy-tree state t))
442 ((integerp state) (vector 'cl-random-state-tag -1 30 state))
443 (t (cl-make-random-state (cl-random-time)))))
444
445 ;;;###autoload
446 (defun cl-random-state-p (object)
447 "Return t if OBJECT is a random-state object."
448 (and (vectorp object) (= (length object) 4)
449 (eq (aref object 0) 'cl-random-state-tag)))
450
451
452 ;; Implementation limits.
453
454 (defun cl--finite-do (func a b)
455 (condition-case _
456 (let ((res (funcall func a b))) ; check for IEEE infinity
457 (and (numberp res) (/= res (/ res 2)) res))
458 (arith-error nil)))
459
460 ;;;###autoload
461 (defun cl-float-limits ()
462 "Initialize the Common Lisp floating-point parameters.
463 This sets the values of: `cl-most-positive-float', `cl-most-negative-float',
464 `cl-least-positive-float', `cl-least-negative-float', `cl-float-epsilon',
465 `cl-float-negative-epsilon', `cl-least-positive-normalized-float', and
466 `cl-least-negative-normalized-float'."
467 (or cl-most-positive-float (not (numberp '2e1))
468 (let ((x '2e0) y z)
469 ;; Find maximum exponent (first two loops are optimizations)
470 (while (cl--finite-do '* x x) (setq x (* x x)))
471 (while (cl--finite-do '* x (/ x 2)) (setq x (* x (/ x 2))))
472 (while (cl--finite-do '+ x x) (setq x (+ x x)))
473 (setq z x y (/ x 2))
474 ;; Now cl-fill in 1's in the mantissa.
475 (while (and (cl--finite-do '+ x y) (/= (+ x y) x))
476 (setq x (+ x y) y (/ y 2)))
477 (setq cl-most-positive-float x
478 cl-most-negative-float (- x))
479 ;; Divide down until mantissa starts rounding.
480 (setq x (/ x z) y (/ 16 z) x (* x y))
481 (while (condition-case _ (and (= x (* (/ x 2) 2)) (> (/ y 2) 0))
482 (arith-error nil))
483 (setq x (/ x 2) y (/ y 2)))
484 (setq cl-least-positive-normalized-float y
485 cl-least-negative-normalized-float (- y))
486 ;; Divide down until value underflows to zero.
487 (setq x (/ 1 z) y x)
488 (while (condition-case _ (> (/ x 2) 0) (arith-error nil))
489 (setq x (/ x 2)))
490 (setq cl-least-positive-float x
491 cl-least-negative-float (- x))
492 (setq x '1e0)
493 (while (/= (+ '1e0 x) '1e0) (setq x (/ x 2)))
494 (setq cl-float-epsilon (* x 2))
495 (setq x '1e0)
496 (while (/= (- '1e0 x) '1e0) (setq x (/ x 2)))
497 (setq cl-float-negative-epsilon (* x 2))))
498 nil)
499
500
501 ;;; Sequence functions.
502
503 ;;;###autoload
504 (defun cl-subseq (seq start &optional end)
505 "Return the subsequence of SEQ from START to END.
506 If END is omitted, it defaults to the length of the sequence.
507 If START or END is negative, it counts from the end."
508 (declare (gv-setter
509 (lambda (new)
510 `(progn (cl-replace ,seq ,new :start1 ,start :end1 ,end)
511 ,new))))
512 (if (stringp seq) (substring seq start end)
513 (let (len)
514 (and end (< end 0) (setq end (+ end (setq len (length seq)))))
515 (if (< start 0) (setq start (+ start (or len (setq len (length seq))))))
516 (cond ((listp seq)
517 (if (> start 0) (setq seq (nthcdr start seq)))
518 (if end
519 (let ((res nil))
520 (while (>= (setq end (1- end)) start)
521 (push (pop seq) res))
522 (nreverse res))
523 (copy-sequence seq)))
524 (t
525 (or end (setq end (or len (length seq))))
526 (let ((res (make-vector (max (- end start) 0) nil))
527 (i 0))
528 (while (< start end)
529 (aset res i (aref seq start))
530 (setq i (1+ i) start (1+ start)))
531 res))))))
532
533 ;;;###autoload
534 (defun cl-concatenate (type &rest seqs)
535 "Concatenate, into a sequence of type TYPE, the argument SEQUENCEs.
536 \n(fn TYPE SEQUENCE...)"
537 (cond ((eq type 'vector) (apply 'vconcat seqs))
538 ((eq type 'string) (apply 'concat seqs))
539 ((eq type 'list) (apply 'append (append seqs '(nil))))
540 (t (error "Not a sequence type name: %s" type))))
541
542
543 ;;; List functions.
544
545 ;;;###autoload
546 (defun cl-revappend (x y)
547 "Equivalent to (append (reverse X) Y)."
548 (nconc (reverse x) y))
549
550 ;;;###autoload
551 (defun cl-nreconc (x y)
552 "Equivalent to (nconc (nreverse X) Y)."
553 (nconc (nreverse x) y))
554
555 ;;;###autoload
556 (defun cl-list-length (x)
557 "Return the length of list X. Return nil if list is circular."
558 (let ((n 0) (fast x) (slow x))
559 (while (and (cdr fast) (not (and (eq fast slow) (> n 0))))
560 (setq n (+ n 2) fast (cdr (cdr fast)) slow (cdr slow)))
561 (if fast (if (cdr fast) nil (1+ n)) n)))
562
563 ;;;###autoload
564 (defun cl-tailp (sublist list)
565 "Return true if SUBLIST is a tail of LIST."
566 (while (and (consp list) (not (eq sublist list)))
567 (setq list (cdr list)))
568 (if (numberp sublist) (equal sublist list) (eq sublist list)))
569
570 ;;; Property lists.
571
572 ;;;###autoload
573 (defun cl-get (sym tag &optional def)
574 "Return the value of SYMBOL's PROPNAME property, or DEFAULT if none.
575 \n(fn SYMBOL PROPNAME &optional DEFAULT)"
576 (declare (compiler-macro cl--compiler-macro-get)
577 (gv-setter (lambda (store) `(put ,sym ,tag ,store))))
578 (or (get sym tag)
579 (and def
580 ;; Make sure `def' is really absent as opposed to set to nil.
581 (let ((plist (symbol-plist sym)))
582 (while (and plist (not (eq (car plist) tag)))
583 (setq plist (cdr (cdr plist))))
584 (if plist (car (cdr plist)) def)))))
585 (autoload 'cl--compiler-macro-get "cl-macs")
586
587 ;;;###autoload
588 (defun cl-getf (plist tag &optional def)
589 "Search PROPLIST for property PROPNAME; return its value or DEFAULT.
590 PROPLIST is a list of the sort returned by `symbol-plist'.
591 \n(fn PROPLIST PROPNAME &optional DEFAULT)"
592 (declare (gv-expander
593 (lambda (do)
594 (gv-letplace (getter setter) plist
595 (macroexp-let2 nil k tag
596 (macroexp-let2 nil d def
597 (funcall do `(cl-getf ,getter ,k ,d)
598 (lambda (v)
599 (funcall setter
600 `(cl--set-getf ,getter ,k ,v))))))))))
601 (setplist '--cl-getf-symbol-- plist)
602 (or (get '--cl-getf-symbol-- tag)
603 ;; Originally we called cl-get here,
604 ;; but that fails, because cl-get has a compiler macro
605 ;; definition that uses getf!
606 (when def
607 ;; Make sure `def' is really absent as opposed to set to nil.
608 (while (and plist (not (eq (car plist) tag)))
609 (setq plist (cdr (cdr plist))))
610 (if plist (car (cdr plist)) def))))
611
612 ;;;###autoload
613 (defun cl--set-getf (plist tag val)
614 (let ((p plist))
615 (while (and p (not (eq (car p) tag))) (setq p (cdr (cdr p))))
616 (if p (progn (setcar (cdr p) val) plist) (cl-list* tag val plist))))
617
618 ;;;###autoload
619 (defun cl--do-remf (plist tag)
620 (let ((p (cdr plist)))
621 (while (and (cdr p) (not (eq (car (cdr p)) tag))) (setq p (cdr (cdr p))))
622 (and (cdr p) (progn (setcdr p (cdr (cdr (cdr p)))) t))))
623
624 ;;;###autoload
625 (defun cl-remprop (sym tag)
626 "Remove from SYMBOL's plist the property PROPNAME and its value.
627 \n(fn SYMBOL PROPNAME)"
628 (let ((plist (symbol-plist sym)))
629 (if (and plist (eq tag (car plist)))
630 (progn (setplist sym (cdr (cdr plist))) t)
631 (cl--do-remf plist tag))))
632
633 ;;; Some debugging aids.
634
635 (defun cl-prettyprint (form)
636 "Insert a pretty-printed rendition of a Lisp FORM in current buffer."
637 (let ((pt (point)) last)
638 (insert "\n" (prin1-to-string form) "\n")
639 (setq last (point))
640 (goto-char (1+ pt))
641 (while (search-forward "(quote " last t)
642 (delete-char -7)
643 (insert "'")
644 (forward-sexp)
645 (delete-char 1))
646 (goto-char (1+ pt))
647 (cl--do-prettyprint)))
648
649 (defun cl--do-prettyprint ()
650 (skip-chars-forward " ")
651 (if (looking-at "(")
652 (let ((skip (or (looking-at "((") (looking-at "(prog")
653 (looking-at "(unwind-protect ")
654 (looking-at "(function (")
655 (looking-at "(cl--block-wrapper ")))
656 (two (or (looking-at "(defun ") (looking-at "(defmacro ")))
657 (let (or (looking-at "(let\\*? ") (looking-at "(while ")))
658 (set (looking-at "(p?set[qf] ")))
659 (if (or skip let
660 (progn
661 (forward-sexp)
662 (and (>= (current-column) 78) (progn (backward-sexp) t))))
663 (let ((nl t))
664 (forward-char 1)
665 (cl--do-prettyprint)
666 (or skip (looking-at ")") (cl--do-prettyprint))
667 (or (not two) (looking-at ")") (cl--do-prettyprint))
668 (while (not (looking-at ")"))
669 (if set (setq nl (not nl)))
670 (if nl (insert "\n"))
671 (lisp-indent-line)
672 (cl--do-prettyprint))
673 (forward-char 1))))
674 (forward-sexp)))
675
676 ;;;###autoload
677 (defun cl-prettyexpand (form &optional full)
678 "Expand macros in FORM and insert the pretty-printed result.
679 Optional argument FULL non-nil means to expand all macros,
680 including `cl-block' and `cl-eval-when'."
681 (message "Expanding...")
682 (let ((cl--compiling-file full)
683 (byte-compile-macro-environment nil))
684 (setq form (macroexpand-all form
685 (and (not full) '((cl-block) (cl-eval-when)))))
686 (message "Formatting...")
687 (prog1 (cl-prettyprint form)
688 (message ""))))
689
690
691
692 (run-hooks 'cl-extra-load-hook)
693
694 ;; Local variables:
695 ;; byte-compile-dynamic: t
696 ;; generated-autoload-file: "cl-loaddefs.el"
697 ;; End:
698
699 ;;; cl-extra.el ends here