Provide generalized variables in core Elisp.
[bpt/emacs.git] / lisp / emacs-lisp / cl-extra.el
1 ;;; cl-extra.el --- Common Lisp features, part 2 -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 1993, 2000-2012 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 `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 (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)))
159
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)
169
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)))
175
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)))
181
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)))
197
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)))
211
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)))
217
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)))
223
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))
238
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)))))
266
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)
271
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)))
287
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)))))
307
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)
315
316 ;;; Support for `cl-progv'.
317 (defvar cl--progv-save)
318 ;;;###autoload
319 (defun cl--progv-before (syms values)
320 (while syms
321 (push (if (boundp (car syms))
322 (cons (car syms) (symbol-value (car syms)))
323 (car syms)) cl--progv-save)
324 (if values
325 (set (pop syms) (pop values))
326 (makunbound (pop syms)))))
327
328 (defun cl--progv-after ()
329 (while cl--progv-save
330 (if (consp (car cl--progv-save))
331 (set (car (car cl--progv-save)) (cdr (car cl--progv-save)))
332 (makunbound (car cl--progv-save)))
333 (pop cl--progv-save)))
334
335
336 ;;; Numbers.
337
338 ;;;###autoload
339 (defun cl-gcd (&rest args)
340 "Return the greatest common divisor of the arguments."
341 (let ((a (abs (or (pop args) 0))))
342 (while args
343 (let ((b (abs (pop args))))
344 (while (> b 0) (setq b (% a (setq a b))))))
345 a))
346
347 ;;;###autoload
348 (defun cl-lcm (&rest args)
349 "Return the least common multiple of the arguments."
350 (if (memq 0 args)
351 0
352 (let ((a (abs (or (pop args) 1))))
353 (while args
354 (let ((b (abs (pop args))))
355 (setq a (* (/ a (cl-gcd a b)) b))))
356 a)))
357
358 ;;;###autoload
359 (defun cl-isqrt (x)
360 "Return the integer square root of the argument."
361 (if (and (integerp x) (> x 0))
362 (let ((g (cond ((<= x 100) 10) ((<= x 10000) 100)
363 ((<= x 1000000) 1000) (t x)))
364 g2)
365 (while (< (setq g2 (/ (+ g (/ x g)) 2)) g)
366 (setq g g2))
367 g)
368 (if (eq x 0) 0 (signal 'arith-error nil))))
369
370 ;;;###autoload
371 (defun cl-floor (x &optional y)
372 "Return a list of the floor of X and the fractional part of X.
373 With two arguments, return floor and remainder of their quotient."
374 (let ((q (floor x y)))
375 (list q (- x (if y (* y q) q)))))
376
377 ;;;###autoload
378 (defun cl-ceiling (x &optional y)
379 "Return a list of the ceiling of X and the fractional part of X.
380 With two arguments, return ceiling and remainder of their quotient."
381 (let ((res (cl-floor x y)))
382 (if (= (car (cdr res)) 0) res
383 (list (1+ (car res)) (- (car (cdr res)) (or y 1))))))
384
385 ;;;###autoload
386 (defun cl-truncate (x &optional y)
387 "Return a list of the integer part of X and the fractional part of X.
388 With two arguments, return truncation and remainder of their quotient."
389 (if (eq (>= x 0) (or (null y) (>= y 0)))
390 (cl-floor x y) (cl-ceiling x y)))
391
392 ;;;###autoload
393 (defun cl-round (x &optional y)
394 "Return a list of X rounded to the nearest integer and the remainder.
395 With two arguments, return rounding and remainder of their quotient."
396 (if y
397 (if (and (integerp x) (integerp y))
398 (let* ((hy (/ y 2))
399 (res (cl-floor (+ x hy) y)))
400 (if (and (= (car (cdr res)) 0)
401 (= (+ hy hy) y)
402 (/= (% (car res) 2) 0))
403 (list (1- (car res)) hy)
404 (list (car res) (- (car (cdr res)) hy))))
405 (let ((q (round (/ x y))))
406 (list q (- x (* q y)))))
407 (if (integerp x) (list x 0)
408 (let ((q (round x)))
409 (list q (- x q))))))
410
411 ;;;###autoload
412 (defun cl-mod (x y)
413 "The remainder of X divided by Y, with the same sign as Y."
414 (nth 1 (cl-floor x y)))
415
416 ;;;###autoload
417 (defun cl-rem (x y)
418 "The remainder of X divided by Y, with the same sign as X."
419 (nth 1 (cl-truncate x y)))
420
421 ;;;###autoload
422 (defun cl-signum (x)
423 "Return 1 if X is positive, -1 if negative, 0 if zero."
424 (cond ((> x 0) 1) ((< x 0) -1) (t 0)))
425
426
427 ;; Random numbers.
428
429 ;;;###autoload
430 (defun cl-random (lim &optional state)
431 "Return a random nonnegative number less than LIM, an integer or float.
432 Optional second arg STATE is a random-state object."
433 (or state (setq state cl--random-state))
434 ;; Inspired by "ran3" from Numerical Recipes. Additive congruential method.
435 (let ((vec (aref state 3)))
436 (if (integerp vec)
437 (let ((i 0) (j (- 1357335 (% (abs vec) 1357333))) (k 1))
438 (aset state 3 (setq vec (make-vector 55 nil)))
439 (aset vec 0 j)
440 (while (> (setq i (% (+ i 21) 55)) 0)
441 (aset vec i (setq j (prog1 k (setq k (- j k))))))
442 (while (< (setq i (1+ i)) 200) (cl-random 2 state))))
443 (let* ((i (aset state 1 (% (1+ (aref state 1)) 55)))
444 (j (aset state 2 (% (1+ (aref state 2)) 55)))
445 (n (logand 8388607 (aset vec i (- (aref vec i) (aref vec j))))))
446 (if (integerp lim)
447 (if (<= lim 512) (% n lim)
448 (if (> lim 8388607) (setq n (+ (lsh n 9) (cl-random 512 state))))
449 (let ((mask 1023))
450 (while (< mask (1- lim)) (setq mask (1+ (+ mask mask))))
451 (if (< (setq n (logand n mask)) lim) n (cl-random lim state))))
452 (* (/ n '8388608e0) lim)))))
453
454 ;;;###autoload
455 (defun cl-make-random-state (&optional state)
456 "Return a copy of random-state STATE, or of the internal state if omitted.
457 If STATE is t, return a new state object seeded from the time of day."
458 (cond ((null state) (cl-make-random-state cl--random-state))
459 ((vectorp state) (copy-tree state t))
460 ((integerp state) (vector 'cl-random-state-tag -1 30 state))
461 (t (cl-make-random-state (cl-random-time)))))
462
463 ;;;###autoload
464 (defun cl-random-state-p (object)
465 "Return t if OBJECT is a random-state object."
466 (and (vectorp object) (= (length object) 4)
467 (eq (aref object 0) 'cl-random-state-tag)))
468
469
470 ;; Implementation limits.
471
472 (defun cl--finite-do (func a b)
473 (condition-case _
474 (let ((res (funcall func a b))) ; check for IEEE infinity
475 (and (numberp res) (/= res (/ res 2)) res))
476 (arith-error nil)))
477
478 ;;;###autoload
479 (defun cl-float-limits ()
480 "Initialize the Common Lisp floating-point parameters.
481 This sets the values of: `cl-most-positive-float', `cl-most-negative-float',
482 `cl-least-positive-float', `cl-least-negative-float', `cl-float-epsilon',
483 `cl-float-negative-epsilon', `cl-least-positive-normalized-float', and
484 `cl-least-negative-normalized-float'."
485 (or cl-most-positive-float (not (numberp '2e1))
486 (let ((x '2e0) y z)
487 ;; Find maximum exponent (first two loops are optimizations)
488 (while (cl--finite-do '* x x) (setq x (* x x)))
489 (while (cl--finite-do '* x (/ x 2)) (setq x (* x (/ x 2))))
490 (while (cl--finite-do '+ x x) (setq x (+ x x)))
491 (setq z x y (/ x 2))
492 ;; Now cl-fill in 1's in the mantissa.
493 (while (and (cl--finite-do '+ x y) (/= (+ x y) x))
494 (setq x (+ x y) y (/ y 2)))
495 (setq cl-most-positive-float x
496 cl-most-negative-float (- x))
497 ;; Divide down until mantissa starts rounding.
498 (setq x (/ x z) y (/ 16 z) x (* x y))
499 (while (condition-case _ (and (= x (* (/ x 2) 2)) (> (/ y 2) 0))
500 (arith-error nil))
501 (setq x (/ x 2) y (/ y 2)))
502 (setq cl-least-positive-normalized-float y
503 cl-least-negative-normalized-float (- y))
504 ;; Divide down until value underflows to zero.
505 (setq x (/ 1 z) y x)
506 (while (condition-case _ (> (/ x 2) 0) (arith-error nil))
507 (setq x (/ x 2)))
508 (setq cl-least-positive-float x
509 cl-least-negative-float (- x))
510 (setq x '1e0)
511 (while (/= (+ '1e0 x) '1e0) (setq x (/ x 2)))
512 (setq cl-float-epsilon (* x 2))
513 (setq x '1e0)
514 (while (/= (- '1e0 x) '1e0) (setq x (/ x 2)))
515 (setq cl-float-negative-epsilon (* x 2))))
516 nil)
517
518
519 ;;; Sequence functions.
520
521 ;;;###autoload
522 (defun cl-subseq (seq start &optional end)
523 "Return the subsequence of SEQ from START to END.
524 If END is omitted, it defaults to the length of the sequence.
525 If START or END is negative, it counts from the end."
526 (if (stringp seq) (substring seq start end)
527 (let (len)
528 (and end (< end 0) (setq end (+ end (setq len (length seq)))))
529 (if (< start 0) (setq start (+ start (or len (setq len (length seq))))))
530 (cond ((listp seq)
531 (if (> start 0) (setq seq (nthcdr start seq)))
532 (if end
533 (let ((res nil))
534 (while (>= (setq end (1- end)) start)
535 (push (pop seq) res))
536 (nreverse res))
537 (copy-sequence seq)))
538 (t
539 (or end (setq end (or len (length seq))))
540 (let ((res (make-vector (max (- end start) 0) nil))
541 (i 0))
542 (while (< start end)
543 (aset res i (aref seq start))
544 (setq i (1+ i) start (1+ start)))
545 res))))))
546
547 ;;;###autoload
548 (defun cl-concatenate (type &rest seqs)
549 "Concatenate, into a sequence of type TYPE, the argument SEQUENCEs.
550 \n(fn TYPE SEQUENCE...)"
551 (cond ((eq type 'vector) (apply 'vconcat seqs))
552 ((eq type 'string) (apply 'concat seqs))
553 ((eq type 'list) (apply 'append (append seqs '(nil))))
554 (t (error "Not a sequence type name: %s" type))))
555
556
557 ;;; List functions.
558
559 ;;;###autoload
560 (defun cl-revappend (x y)
561 "Equivalent to (append (reverse X) Y)."
562 (nconc (reverse x) y))
563
564 ;;;###autoload
565 (defun cl-nreconc (x y)
566 "Equivalent to (nconc (nreverse X) Y)."
567 (nconc (nreverse x) y))
568
569 ;;;###autoload
570 (defun cl-list-length (x)
571 "Return the length of list X. Return nil if list is circular."
572 (let ((n 0) (fast x) (slow x))
573 (while (and (cdr fast) (not (and (eq fast slow) (> n 0))))
574 (setq n (+ n 2) fast (cdr (cdr fast)) slow (cdr slow)))
575 (if fast (if (cdr fast) nil (1+ n)) n)))
576
577 ;;;###autoload
578 (defun cl-tailp (sublist list)
579 "Return true if SUBLIST is a tail of LIST."
580 (while (and (consp list) (not (eq sublist list)))
581 (setq list (cdr list)))
582 (if (numberp sublist) (equal sublist list) (eq sublist list)))
583
584 ;;; Property lists.
585
586 ;;;###autoload
587 (defun cl-get (sym tag &optional def)
588 "Return the value of SYMBOL's PROPNAME property, or DEFAULT if none.
589 \n(fn SYMBOL PROPNAME &optional DEFAULT)"
590 (declare (compiler-macro cl--compiler-macro-get))
591 (or (get sym tag)
592 (and def
593 ;; Make sure `def' is really absent as opposed to set to nil.
594 (let ((plist (symbol-plist sym)))
595 (while (and plist (not (eq (car plist) tag)))
596 (setq plist (cdr (cdr plist))))
597 (if plist (car (cdr plist)) def)))))
598 (autoload 'cl--compiler-macro-get "cl-macs")
599
600 ;;;###autoload
601 (defun cl-getf (plist tag &optional def)
602 "Search PROPLIST for property PROPNAME; return its value or DEFAULT.
603 PROPLIST is a list of the sort returned by `symbol-plist'.
604 \n(fn PROPLIST PROPNAME &optional DEFAULT)"
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))))
615
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))))
621
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))))
627
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))))
636
637 ;;; Some debugging aids.
638
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)))
652
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)))
679
680 ;;;###autoload
681 (defun cl-prettyexpand (form &optional full)
682 (message "Expanding...")
683 (let ((cl--compiling-file full)
684 (byte-compile-macro-environment nil))
685 (setq form (macroexpand-all form
686 (and (not full) '((cl-block) (cl-eval-when)))))
687 (message "Formatting...")
688 (prog1 (cl-prettyprint form)
689 (message ""))))
690
691
692
693 (run-hooks 'cl-extra-load-hook)
694
695 ;; Local variables:
696 ;; byte-compile-dynamic: t
697 ;; byte-compile-warnings: (not cl-functions)
698 ;; generated-autoload-file: "cl-loaddefs.el"
699 ;; End:
700
701 ;;; cl-extra.el ends here