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