More CL cleanups and reduction of use of cl.el.
[emacs.git] / lisp / emacs-lisp / cl-extra.el
blobea5e1cf9beb3b07a64367a3f47dad50a530450c8
1 ;;; cl-extra.el --- Common Lisp features, part 2 -*- lexical-binding: t -*-
3 ;; Copyright (C) 1993, 2000-2012 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)) (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))))
60 ;;; Predicates.
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))))
88 ;;; Control structures.
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))))
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))))
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 `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))))
152 (defun cl-mapc (cl-func cl-seq &rest cl-rest)
153 "Like `mapcar', but does not accumulate values returned by the function.
154 \n(fn FUNCTION SEQUENCE...)"
155 (if cl-rest
156 (progn (apply 'cl-map nil cl-func cl-seq cl-rest)
157 cl-seq)
158 (mapc cl-func cl-seq)))
160 ;;;###autoload
161 (defun cl-mapl (cl-func cl-list &rest cl-rest)
162 "Like `cl-maplist', but does not accumulate values returned by the function.
163 \n(fn FUNCTION LIST...)"
164 (if cl-rest
165 (apply 'cl-maplist cl-func cl-list cl-rest)
166 (let ((cl-p cl-list))
167 (while cl-p (funcall cl-func cl-p) (setq cl-p (cdr cl-p)))))
168 cl-list)
170 ;;;###autoload
171 (defun cl-mapcan (cl-func cl-seq &rest cl-rest)
172 "Like `mapcar', but nconc's together the values returned by the function.
173 \n(fn FUNCTION SEQUENCE...)"
174 (apply 'nconc (apply 'cl-mapcar cl-func cl-seq cl-rest)))
176 ;;;###autoload
177 (defun cl-mapcon (cl-func cl-list &rest cl-rest)
178 "Like `cl-maplist', but nconc's together the values returned by the function.
179 \n(fn FUNCTION LIST...)"
180 (apply 'nconc (apply 'cl-maplist cl-func cl-list cl-rest)))
182 ;;;###autoload
183 (defun cl-some (cl-pred cl-seq &rest cl-rest)
184 "Return true if PREDICATE is true of any element of SEQ or SEQs.
185 If so, return the true (non-nil) value returned by PREDICATE.
186 \n(fn PREDICATE SEQ...)"
187 (if (or cl-rest (nlistp cl-seq))
188 (catch 'cl-some
189 (apply 'cl-map nil
190 (function (lambda (&rest cl-x)
191 (let ((cl-res (apply cl-pred cl-x)))
192 (if cl-res (throw 'cl-some cl-res)))))
193 cl-seq cl-rest) nil)
194 (let ((cl-x nil))
195 (while (and cl-seq (not (setq cl-x (funcall cl-pred (pop cl-seq))))))
196 cl-x)))
198 ;;;###autoload
199 (defun cl-every (cl-pred cl-seq &rest cl-rest)
200 "Return true if PREDICATE is true of every element of SEQ or SEQs.
201 \n(fn PREDICATE SEQ...)"
202 (if (or cl-rest (nlistp cl-seq))
203 (catch 'cl-every
204 (apply 'cl-map nil
205 (function (lambda (&rest cl-x)
206 (or (apply cl-pred cl-x) (throw 'cl-every nil))))
207 cl-seq cl-rest) t)
208 (while (and cl-seq (funcall cl-pred (car cl-seq)))
209 (setq cl-seq (cdr cl-seq)))
210 (null cl-seq)))
212 ;;;###autoload
213 (defun cl-notany (cl-pred cl-seq &rest cl-rest)
214 "Return true if PREDICATE is false of every element of SEQ or SEQs.
215 \n(fn PREDICATE SEQ...)"
216 (not (apply 'cl-some cl-pred cl-seq cl-rest)))
218 ;;;###autoload
219 (defun cl-notevery (cl-pred cl-seq &rest cl-rest)
220 "Return true if PREDICATE is false of some element of SEQ or SEQs.
221 \n(fn PREDICATE SEQ...)"
222 (not (apply 'cl-every cl-pred cl-seq cl-rest)))
224 ;;;###autoload
225 (defun cl--map-keymap-recursively (cl-func-rec cl-map &optional cl-base)
226 (or cl-base
227 (setq cl-base (copy-sequence [0])))
228 (map-keymap
229 (function
230 (lambda (cl-key cl-bind)
231 (aset cl-base (1- (length cl-base)) cl-key)
232 (if (keymapp cl-bind)
233 (cl--map-keymap-recursively
234 cl-func-rec cl-bind
235 (vconcat cl-base (list 0)))
236 (funcall cl-func-rec cl-base cl-bind))))
237 cl-map))
239 ;;;###autoload
240 (defun cl--map-intervals (cl-func &optional cl-what cl-prop cl-start cl-end)
241 (or cl-what (setq cl-what (current-buffer)))
242 (if (bufferp cl-what)
243 (let (cl-mark cl-mark2 (cl-next t) cl-next2)
244 (with-current-buffer cl-what
245 (setq cl-mark (copy-marker (or cl-start (point-min))))
246 (setq cl-mark2 (and cl-end (copy-marker cl-end))))
247 (while (and cl-next (or (not cl-mark2) (< cl-mark cl-mark2)))
248 (setq cl-next (if cl-prop (next-single-property-change
249 cl-mark cl-prop cl-what)
250 (next-property-change cl-mark cl-what))
251 cl-next2 (or cl-next (with-current-buffer cl-what
252 (point-max))))
253 (funcall cl-func (prog1 (marker-position cl-mark)
254 (set-marker cl-mark cl-next2))
255 (if cl-mark2 (min cl-next2 cl-mark2) cl-next2)))
256 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil)))
257 (or cl-start (setq cl-start 0))
258 (or cl-end (setq cl-end (length cl-what)))
259 (while (< cl-start cl-end)
260 (let ((cl-next (or (if cl-prop (next-single-property-change
261 cl-start cl-prop cl-what)
262 (next-property-change cl-start cl-what))
263 cl-end)))
264 (funcall cl-func cl-start (min cl-next cl-end))
265 (setq cl-start cl-next)))))
267 ;;;###autoload
268 (defun cl--map-overlays (cl-func &optional cl-buffer cl-start cl-end cl-arg)
269 (or cl-buffer (setq cl-buffer (current-buffer)))
270 (if (fboundp 'overlay-lists)
272 ;; This is the preferred algorithm, though overlay-lists is undocumented.
273 (let (cl-ovl)
274 (with-current-buffer cl-buffer
275 (setq cl-ovl (overlay-lists))
276 (if cl-start (setq cl-start (copy-marker cl-start)))
277 (if cl-end (setq cl-end (copy-marker cl-end))))
278 (setq cl-ovl (nconc (car cl-ovl) (cdr cl-ovl)))
279 (while (and cl-ovl
280 (or (not (overlay-start (car cl-ovl)))
281 (and cl-end (>= (overlay-start (car cl-ovl)) cl-end))
282 (and cl-start (<= (overlay-end (car cl-ovl)) cl-start))
283 (not (funcall cl-func (car cl-ovl) cl-arg))))
284 (setq cl-ovl (cdr cl-ovl)))
285 (if cl-start (set-marker cl-start nil))
286 (if cl-end (set-marker cl-end nil)))
288 ;; This alternate algorithm fails to find zero-length overlays.
289 (let ((cl-mark (with-current-buffer cl-buffer
290 (copy-marker (or cl-start (point-min)))))
291 (cl-mark2 (and cl-end (with-current-buffer cl-buffer
292 (copy-marker cl-end))))
293 cl-pos cl-ovl)
294 (while (save-excursion
295 (and (setq cl-pos (marker-position cl-mark))
296 (< cl-pos (or cl-mark2 (point-max)))
297 (progn
298 (set-buffer cl-buffer)
299 (setq cl-ovl (overlays-at cl-pos))
300 (set-marker cl-mark (next-overlay-change cl-pos)))))
301 (while (and cl-ovl
302 (or (/= (overlay-start (car cl-ovl)) cl-pos)
303 (not (and (funcall cl-func (car cl-ovl) cl-arg)
304 (set-marker cl-mark nil)))))
305 (setq cl-ovl (cdr cl-ovl))))
306 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil)))))
308 ;;; Support for `setf'.
309 ;;;###autoload
310 (defun cl--set-frame-visible-p (frame val)
311 (cond ((null val) (make-frame-invisible frame))
312 ((eq val 'icon) (iconify-frame frame))
313 (t (make-frame-visible frame)))
314 val)
317 ;;; Numbers.
319 ;;;###autoload
320 (defun cl-gcd (&rest args)
321 "Return the greatest common divisor of the arguments."
322 (let ((a (abs (or (pop args) 0))))
323 (while args
324 (let ((b (abs (pop args))))
325 (while (> b 0) (setq b (% a (setq a b))))))
328 ;;;###autoload
329 (defun cl-lcm (&rest args)
330 "Return the least common multiple of the arguments."
331 (if (memq 0 args)
333 (let ((a (abs (or (pop args) 1))))
334 (while args
335 (let ((b (abs (pop args))))
336 (setq a (* (/ a (cl-gcd a b)) b))))
337 a)))
339 ;;;###autoload
340 (defun cl-isqrt (x)
341 "Return the integer square root of the argument."
342 (if (and (integerp x) (> x 0))
343 (let ((g (cond ((<= x 100) 10) ((<= x 10000) 100)
344 ((<= x 1000000) 1000) (t x)))
346 (while (< (setq g2 (/ (+ g (/ x g)) 2)) g)
347 (setq g g2))
349 (if (eq x 0) 0 (signal 'arith-error nil))))
351 ;;;###autoload
352 (defun cl-floor (x &optional y)
353 "Return a list of the floor of X and the fractional part of X.
354 With two arguments, return floor and remainder of their quotient."
355 (let ((q (floor x y)))
356 (list q (- x (if y (* y q) q)))))
358 ;;;###autoload
359 (defun cl-ceiling (x &optional y)
360 "Return a list of the ceiling of X and the fractional part of X.
361 With two arguments, return ceiling and remainder of their quotient."
362 (let ((res (cl-floor x y)))
363 (if (= (car (cdr res)) 0) res
364 (list (1+ (car res)) (- (car (cdr res)) (or y 1))))))
366 ;;;###autoload
367 (defun cl-truncate (x &optional y)
368 "Return a list of the integer part of X and the fractional part of X.
369 With two arguments, return truncation and remainder of their quotient."
370 (if (eq (>= x 0) (or (null y) (>= y 0)))
371 (cl-floor x y) (cl-ceiling x y)))
373 ;;;###autoload
374 (defun cl-round (x &optional y)
375 "Return a list of X rounded to the nearest integer and the remainder.
376 With two arguments, return rounding and remainder of their quotient."
377 (if y
378 (if (and (integerp x) (integerp y))
379 (let* ((hy (/ y 2))
380 (res (cl-floor (+ x hy) y)))
381 (if (and (= (car (cdr res)) 0)
382 (= (+ hy hy) y)
383 (/= (% (car res) 2) 0))
384 (list (1- (car res)) hy)
385 (list (car res) (- (car (cdr res)) hy))))
386 (let ((q (round (/ x y))))
387 (list q (- x (* q y)))))
388 (if (integerp x) (list x 0)
389 (let ((q (round x)))
390 (list q (- x q))))))
392 ;;;###autoload
393 (defun cl-mod (x y)
394 "The remainder of X divided by Y, with the same sign as Y."
395 (nth 1 (cl-floor x y)))
397 ;;;###autoload
398 (defun cl-rem (x y)
399 "The remainder of X divided by Y, with the same sign as X."
400 (nth 1 (cl-truncate x y)))
402 ;;;###autoload
403 (defun cl-signum (x)
404 "Return 1 if X is positive, -1 if negative, 0 if zero."
405 (cond ((> x 0) 1) ((< x 0) -1) (t 0)))
408 ;; Random numbers.
410 ;;;###autoload
411 (defun cl-random (lim &optional state)
412 "Return a random nonnegative number less than LIM, an integer or float.
413 Optional second arg STATE is a random-state object."
414 (or state (setq state cl--random-state))
415 ;; Inspired by "ran3" from Numerical Recipes. Additive congruential method.
416 (let ((vec (aref state 3)))
417 (if (integerp vec)
418 (let ((i 0) (j (- 1357335 (% (abs vec) 1357333))) (k 1))
419 (aset state 3 (setq vec (make-vector 55 nil)))
420 (aset vec 0 j)
421 (while (> (setq i (% (+ i 21) 55)) 0)
422 (aset vec i (setq j (prog1 k (setq k (- j k))))))
423 (while (< (setq i (1+ i)) 200) (cl-random 2 state))))
424 (let* ((i (aset state 1 (% (1+ (aref state 1)) 55)))
425 (j (aset state 2 (% (1+ (aref state 2)) 55)))
426 (n (logand 8388607 (aset vec i (- (aref vec i) (aref vec j))))))
427 (if (integerp lim)
428 (if (<= lim 512) (% n lim)
429 (if (> lim 8388607) (setq n (+ (lsh n 9) (cl-random 512 state))))
430 (let ((mask 1023))
431 (while (< mask (1- lim)) (setq mask (1+ (+ mask mask))))
432 (if (< (setq n (logand n mask)) lim) n (cl-random lim state))))
433 (* (/ n '8388608e0) lim)))))
435 ;;;###autoload
436 (defun cl-make-random-state (&optional state)
437 "Return a copy of random-state STATE, or of the internal state if omitted.
438 If STATE is t, return a new state object seeded from the time of day."
439 (cond ((null state) (cl-make-random-state cl--random-state))
440 ((vectorp state) (copy-tree state t))
441 ((integerp state) (vector 'cl-random-state-tag -1 30 state))
442 (t (cl-make-random-state (cl-random-time)))))
444 ;;;###autoload
445 (defun cl-random-state-p (object)
446 "Return t if OBJECT is a random-state object."
447 (and (vectorp object) (= (length object) 4)
448 (eq (aref object 0) 'cl-random-state-tag)))
451 ;; Implementation limits.
453 (defun cl--finite-do (func a b)
454 (condition-case _
455 (let ((res (funcall func a b))) ; check for IEEE infinity
456 (and (numberp res) (/= res (/ res 2)) res))
457 (arith-error nil)))
459 ;;;###autoload
460 (defun cl-float-limits ()
461 "Initialize the Common Lisp floating-point parameters.
462 This sets the values of: `cl-most-positive-float', `cl-most-negative-float',
463 `cl-least-positive-float', `cl-least-negative-float', `cl-float-epsilon',
464 `cl-float-negative-epsilon', `cl-least-positive-normalized-float', and
465 `cl-least-negative-normalized-float'."
466 (or cl-most-positive-float (not (numberp '2e1))
467 (let ((x '2e0) y z)
468 ;; Find maximum exponent (first two loops are optimizations)
469 (while (cl--finite-do '* x x) (setq x (* x x)))
470 (while (cl--finite-do '* x (/ x 2)) (setq x (* x (/ x 2))))
471 (while (cl--finite-do '+ x x) (setq x (+ x x)))
472 (setq z x y (/ x 2))
473 ;; Now cl-fill in 1's in the mantissa.
474 (while (and (cl--finite-do '+ x y) (/= (+ x y) x))
475 (setq x (+ x y) y (/ y 2)))
476 (setq cl-most-positive-float x
477 cl-most-negative-float (- x))
478 ;; Divide down until mantissa starts rounding.
479 (setq x (/ x z) y (/ 16 z) x (* x y))
480 (while (condition-case _ (and (= x (* (/ x 2) 2)) (> (/ y 2) 0))
481 (arith-error nil))
482 (setq x (/ x 2) y (/ y 2)))
483 (setq cl-least-positive-normalized-float y
484 cl-least-negative-normalized-float (- y))
485 ;; Divide down until value underflows to zero.
486 (setq x (/ 1 z) y x)
487 (while (condition-case _ (> (/ x 2) 0) (arith-error nil))
488 (setq x (/ x 2)))
489 (setq cl-least-positive-float x
490 cl-least-negative-float (- x))
491 (setq x '1e0)
492 (while (/= (+ '1e0 x) '1e0) (setq x (/ x 2)))
493 (setq cl-float-epsilon (* x 2))
494 (setq x '1e0)
495 (while (/= (- '1e0 x) '1e0) (setq x (/ x 2)))
496 (setq cl-float-negative-epsilon (* x 2))))
497 nil)
500 ;;; Sequence functions.
502 ;;;###autoload
503 (defun cl-subseq (seq start &optional end)
504 "Return the subsequence of SEQ from START to END.
505 If END is omitted, it defaults to the length of the sequence.
506 If START or END is negative, it counts from the end."
507 (declare (gv-setter
508 (lambda (new)
509 `(progn (cl-replace ,seq ,new :start1 ,start :end1 ,end)
510 ,new))))
511 (if (stringp seq) (substring seq start end)
512 (let (len)
513 (and end (< end 0) (setq end (+ end (setq len (length seq)))))
514 (if (< start 0) (setq start (+ start (or len (setq len (length seq))))))
515 (cond ((listp seq)
516 (if (> start 0) (setq seq (nthcdr start seq)))
517 (if end
518 (let ((res nil))
519 (while (>= (setq end (1- end)) start)
520 (push (pop seq) res))
521 (nreverse res))
522 (copy-sequence seq)))
524 (or end (setq end (or len (length seq))))
525 (let ((res (make-vector (max (- end start) 0) nil))
526 (i 0))
527 (while (< start end)
528 (aset res i (aref seq start))
529 (setq i (1+ i) start (1+ start)))
530 res))))))
532 ;;;###autoload
533 (defun cl-concatenate (type &rest seqs)
534 "Concatenate, into a sequence of type TYPE, the argument SEQUENCEs.
535 \n(fn TYPE SEQUENCE...)"
536 (cond ((eq type 'vector) (apply 'vconcat seqs))
537 ((eq type 'string) (apply 'concat seqs))
538 ((eq type 'list) (apply 'append (append seqs '(nil))))
539 (t (error "Not a sequence type name: %s" type))))
542 ;;; List functions.
544 ;;;###autoload
545 (defun cl-revappend (x y)
546 "Equivalent to (append (reverse X) Y)."
547 (nconc (reverse x) y))
549 ;;;###autoload
550 (defun cl-nreconc (x y)
551 "Equivalent to (nconc (nreverse X) Y)."
552 (nconc (nreverse x) y))
554 ;;;###autoload
555 (defun cl-list-length (x)
556 "Return the length of list X. Return nil if list is circular."
557 (let ((n 0) (fast x) (slow x))
558 (while (and (cdr fast) (not (and (eq fast slow) (> n 0))))
559 (setq n (+ n 2) fast (cdr (cdr fast)) slow (cdr slow)))
560 (if fast (if (cdr fast) nil (1+ n)) n)))
562 ;;;###autoload
563 (defun cl-tailp (sublist list)
564 "Return true if SUBLIST is a tail of LIST."
565 (while (and (consp list) (not (eq sublist list)))
566 (setq list (cdr list)))
567 (if (numberp sublist) (equal sublist list) (eq sublist list)))
569 ;;; Property lists.
571 ;;;###autoload
572 (defun cl-get (sym tag &optional def)
573 "Return the value of SYMBOL's PROPNAME property, or DEFAULT if none.
574 \n(fn SYMBOL PROPNAME &optional DEFAULT)"
575 (declare (compiler-macro cl--compiler-macro-get)
576 (gv-setter (lambda (store) `(put ,sym ,tag ,store))))
577 (or (get sym tag)
578 (and def
579 ;; Make sure `def' is really absent as opposed to set to nil.
580 (let ((plist (symbol-plist sym)))
581 (while (and plist (not (eq (car plist) tag)))
582 (setq plist (cdr (cdr plist))))
583 (if plist (car (cdr plist)) def)))))
584 (autoload 'cl--compiler-macro-get "cl-macs")
586 ;;;###autoload
587 (defun cl-getf (plist tag &optional def)
588 "Search PROPLIST for property PROPNAME; return its value or DEFAULT.
589 PROPLIST is a list of the sort returned by `symbol-plist'.
590 \n(fn PROPLIST PROPNAME &optional DEFAULT)"
591 (declare (gv-expander
592 (lambda (do)
593 (gv-letplace (getter setter) plist
594 (macroexp-let2 nil k tag
595 (macroexp-let2 nil d def
596 (funcall do `(cl-getf ,getter ,k ,d)
597 (lambda (v)
598 (funcall setter
599 `(cl--set-getf ,getter ,k ,v))))))))))
600 (setplist '--cl-getf-symbol-- plist)
601 (or (get '--cl-getf-symbol-- tag)
602 ;; Originally we called cl-get here,
603 ;; but that fails, because cl-get has a compiler macro
604 ;; definition that uses getf!
605 (when def
606 ;; Make sure `def' is really absent as opposed to set to nil.
607 (while (and plist (not (eq (car plist) tag)))
608 (setq plist (cdr (cdr plist))))
609 (if plist (car (cdr plist)) def))))
611 ;;;###autoload
612 (defun cl--set-getf (plist tag val)
613 (let ((p plist))
614 (while (and p (not (eq (car p) tag))) (setq p (cdr (cdr p))))
615 (if p (progn (setcar (cdr p) val) plist) (cl-list* tag val plist))))
617 ;;;###autoload
618 (defun cl--do-remf (plist tag)
619 (let ((p (cdr plist)))
620 (while (and (cdr p) (not (eq (car (cdr p)) tag))) (setq p (cdr (cdr p))))
621 (and (cdr p) (progn (setcdr p (cdr (cdr (cdr p)))) t))))
623 ;;;###autoload
624 (defun cl-remprop (sym tag)
625 "Remove from SYMBOL's plist the property PROPNAME and its value.
626 \n(fn SYMBOL PROPNAME)"
627 (let ((plist (symbol-plist sym)))
628 (if (and plist (eq tag (car plist)))
629 (progn (setplist sym (cdr (cdr plist))) t)
630 (cl--do-remf plist tag))))
632 ;;; Some debugging aids.
634 (defun cl-prettyprint (form)
635 "Insert a pretty-printed rendition of a Lisp FORM in current buffer."
636 (let ((pt (point)) last)
637 (insert "\n" (prin1-to-string form) "\n")
638 (setq last (point))
639 (goto-char (1+ pt))
640 (while (search-forward "(quote " last t)
641 (delete-char -7)
642 (insert "'")
643 (forward-sexp)
644 (delete-char 1))
645 (goto-char (1+ pt))
646 (cl--do-prettyprint)))
648 (defun cl--do-prettyprint ()
649 (skip-chars-forward " ")
650 (if (looking-at "(")
651 (let ((skip (or (looking-at "((") (looking-at "(prog")
652 (looking-at "(unwind-protect ")
653 (looking-at "(function (")
654 (looking-at "(cl--block-wrapper ")))
655 (two (or (looking-at "(defun ") (looking-at "(defmacro ")))
656 (let (or (looking-at "(let\\*? ") (looking-at "(while ")))
657 (set (looking-at "(p?set[qf] ")))
658 (if (or skip let
659 (progn
660 (forward-sexp)
661 (and (>= (current-column) 78) (progn (backward-sexp) t))))
662 (let ((nl t))
663 (forward-char 1)
664 (cl--do-prettyprint)
665 (or skip (looking-at ")") (cl--do-prettyprint))
666 (or (not two) (looking-at ")") (cl--do-prettyprint))
667 (while (not (looking-at ")"))
668 (if set (setq nl (not nl)))
669 (if nl (insert "\n"))
670 (lisp-indent-line)
671 (cl--do-prettyprint))
672 (forward-char 1))))
673 (forward-sexp)))
675 ;;;###autoload
676 (defun cl-prettyexpand (form &optional full)
677 (message "Expanding...")
678 (let ((cl--compiling-file full)
679 (byte-compile-macro-environment nil))
680 (setq form (macroexpand-all form
681 (and (not full) '((cl-block) (cl-eval-when)))))
682 (message "Formatting...")
683 (prog1 (cl-prettyprint form)
684 (message ""))))
688 (run-hooks 'cl-extra-load-hook)
690 ;; Local variables:
691 ;; byte-compile-dynamic: t
692 ;; byte-compile-warnings: (not cl-functions)
693 ;; generated-autoload-file: "cl-loaddefs.el"
694 ;; End:
696 ;;; cl-extra.el ends here