]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/cl-extra.el
9c6e17e9fecba8a61462109e20f8f4badc058622
[gnu-emacs] / lisp / emacs-lisp / cl-extra.el
1 ;;; cl-extra.el --- Common Lisp features, part 2 -*-byte-compile-dynamic: t;-*-
2
3 ;; Copyright (C) 1993 Free Software Foundation, Inc.
4
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
6 ;; Keywords: extensions
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; These are extensions to Emacs Lisp that provide a degree of
28 ;; Common Lisp compatibility, beyond what is already built-in
29 ;; in Emacs Lisp.
30 ;;
31 ;; This package was written by Dave Gillespie; it is a complete
32 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
33 ;;
34 ;; Bug reports, comments, and suggestions are welcome!
35
36 ;; This file contains portions of the Common Lisp extensions
37 ;; package which are autoloaded since they are relatively obscure.
38
39 ;;; Code:
40
41 (or (memq 'cl-19 features)
42 (error "Tried to load `cl-extra' before `cl'!"))
43
44
45 ;;; We define these here so that this file can compile without having
46 ;;; loaded the cl.el file already.
47
48 (defmacro cl-push (x place) (list 'setq place (list 'cons x place)))
49 (defmacro cl-pop (place)
50 (list 'car (list 'prog1 place (list 'setq place (list 'cdr place)))))
51
52 ;;; Type coercion.
53
54 (defun coerce (x type)
55 "Coerce OBJECT to type TYPE.
56 TYPE is a Common Lisp type specifier."
57 (cond ((eq type 'list) (if (listp x) x (append x nil)))
58 ((eq type 'vector) (if (vectorp x) x (vconcat x)))
59 ((eq type 'string) (if (stringp x) x (concat x)))
60 ((eq type 'array) (if (arrayp x) x (vconcat x)))
61 ((and (eq type 'character) (stringp x) (= (length x) 1)) (aref x 0))
62 ((and (eq type 'character) (symbolp x)) (coerce (symbol-name x) type))
63 ((eq type 'float) (float x))
64 ((typep x type) x)
65 (t (error "Can't coerce %s to type %s" x type))))
66
67
68 ;;; Predicates.
69
70 (defun equalp (x y)
71 "T if two Lisp objects have similar structures and contents.
72 This is like `equal', except that it accepts numerically equal
73 numbers of different types (float vs. integer), and also compares
74 strings case-insensitively."
75 (cond ((eq x y) t)
76 ((stringp x)
77 (and (stringp y) (= (length x) (length y))
78 (or (string-equal x y)
79 (string-equal (downcase x) (downcase y))))) ; lazy but simple!
80 ((numberp x)
81 (and (numberp y) (= x y)))
82 ((consp x)
83 (while (and (consp x) (consp y) (equalp (car x) (car y)))
84 (setq x (cdr x) y (cdr y)))
85 (and (not (consp x)) (equalp x y)))
86 ((vectorp x)
87 (and (vectorp y) (= (length x) (length y))
88 (let ((i (length x)))
89 (while (and (>= (setq i (1- i)) 0)
90 (equalp (aref x i) (aref y i))))
91 (< i 0))))
92 (t (equal x y))))
93
94
95 ;;; Control structures.
96
97 (defun cl-mapcar-many (cl-func cl-seqs)
98 (if (cdr (cdr cl-seqs))
99 (let* ((cl-res nil)
100 (cl-n (apply 'min (mapcar 'length cl-seqs)))
101 (cl-i 0)
102 (cl-args (copy-sequence cl-seqs))
103 cl-p1 cl-p2)
104 (setq cl-seqs (copy-sequence cl-seqs))
105 (while (< cl-i cl-n)
106 (setq cl-p1 cl-seqs cl-p2 cl-args)
107 (while cl-p1
108 (setcar cl-p2
109 (if (consp (car cl-p1))
110 (prog1 (car (car cl-p1))
111 (setcar cl-p1 (cdr (car cl-p1))))
112 (aref (car cl-p1) cl-i)))
113 (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2)))
114 (cl-push (apply cl-func cl-args) cl-res)
115 (setq cl-i (1+ cl-i)))
116 (nreverse cl-res))
117 (let ((cl-res nil)
118 (cl-x (car cl-seqs))
119 (cl-y (nth 1 cl-seqs)))
120 (let ((cl-n (min (length cl-x) (length cl-y)))
121 (cl-i -1))
122 (while (< (setq cl-i (1+ cl-i)) cl-n)
123 (cl-push (funcall cl-func
124 (if (consp cl-x) (cl-pop cl-x) (aref cl-x cl-i))
125 (if (consp cl-y) (cl-pop cl-y) (aref cl-y cl-i)))
126 cl-res)))
127 (nreverse cl-res))))
128
129 (defun map (cl-type cl-func cl-seq &rest cl-rest)
130 "Map a function across one or more sequences, returning a sequence.
131 TYPE is the sequence type to return, FUNC is the function, and SEQS
132 are the argument sequences."
133 (let ((cl-res (apply 'mapcar* cl-func cl-seq cl-rest)))
134 (and cl-type (coerce cl-res cl-type))))
135
136 (defun maplist (cl-func cl-list &rest cl-rest)
137 "Map FUNC to each sublist of LIST or LISTS.
138 Like `mapcar', except applies to lists and their cdr's rather than to
139 the elements themselves."
140 (if cl-rest
141 (let ((cl-res nil)
142 (cl-args (cons cl-list (copy-sequence cl-rest)))
143 cl-p)
144 (while (not (memq nil cl-args))
145 (cl-push (apply cl-func cl-args) cl-res)
146 (setq cl-p cl-args)
147 (while cl-p (setcar cl-p (cdr (cl-pop cl-p)) )))
148 (nreverse cl-res))
149 (let ((cl-res nil))
150 (while cl-list
151 (cl-push (funcall cl-func cl-list) cl-res)
152 (setq cl-list (cdr cl-list)))
153 (nreverse cl-res))))
154
155 (defun mapc (cl-func cl-seq &rest cl-rest)
156 "Like `mapcar', but does not accumulate values returned by the function."
157 (if cl-rest
158 (apply 'map nil cl-func cl-seq cl-rest)
159 (mapcar cl-func cl-seq))
160 cl-seq)
161
162 (defun mapl (cl-func cl-list &rest cl-rest)
163 "Like `maplist', but does not accumulate values returned by the function."
164 (if cl-rest
165 (apply '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 (defun mapcan (cl-func cl-seq &rest cl-rest)
171 "Like `mapcar', but nconc's together the values returned by the function."
172 (apply 'nconc (apply 'mapcar* cl-func cl-seq cl-rest)))
173
174 (defun mapcon (cl-func cl-list &rest cl-rest)
175 "Like `maplist', but nconc's together the values returned by the function."
176 (apply 'nconc (apply 'maplist cl-func cl-list cl-rest)))
177
178 (defun some (cl-pred cl-seq &rest cl-rest)
179 "Return true if PREDICATE is true of any element of SEQ or SEQs.
180 If so, return the true (non-nil) value returned by PREDICATE."
181 (if (or cl-rest (nlistp cl-seq))
182 (catch 'cl-some
183 (apply 'map nil
184 (function (lambda (&rest cl-x)
185 (let ((cl-res (apply cl-pred cl-x)))
186 (if cl-res (throw 'cl-some cl-res)))))
187 cl-seq cl-rest) nil)
188 (let ((cl-x nil))
189 (while (and cl-seq (not (setq cl-x (funcall cl-pred (cl-pop cl-seq))))))
190 cl-x)))
191
192 (defun every (cl-pred cl-seq &rest cl-rest)
193 "Return true if PREDICATE is true of every element of SEQ or SEQs."
194 (if (or cl-rest (nlistp cl-seq))
195 (catch 'cl-every
196 (apply 'map nil
197 (function (lambda (&rest cl-x)
198 (or (apply cl-pred cl-x) (throw 'cl-every nil))))
199 cl-seq cl-rest) t)
200 (while (and cl-seq (funcall cl-pred (car cl-seq)))
201 (setq cl-seq (cdr cl-seq)))
202 (null cl-seq)))
203
204 (defun notany (cl-pred cl-seq &rest cl-rest)
205 "Return true if PREDICATE is false of every element of SEQ or SEQs."
206 (not (apply 'some cl-pred cl-seq cl-rest)))
207
208 (defun notevery (cl-pred cl-seq &rest cl-rest)
209 "Return true if PREDICATE is false of some element of SEQ or SEQs."
210 (not (apply 'every cl-pred cl-seq cl-rest)))
211
212 ;;; Support for `loop'.
213 (defun cl-map-keymap (cl-func cl-map)
214 (while (symbolp cl-map) (setq cl-map (symbol-function cl-map)))
215 (if (listp cl-map)
216 (let ((cl-p cl-map))
217 (while (consp (setq cl-p (cdr cl-p)))
218 (cond ((consp (car cl-p))
219 (funcall cl-func (car (car cl-p)) (cdr (car cl-p))))
220 ((vectorp (car cl-p))
221 (cl-map-keymap cl-func (car cl-p)))
222 ((eq (car cl-p) 'keymap)
223 (setq cl-p nil)))))
224 (let ((cl-i -1))
225 (while (< (setq cl-i (1+ cl-i)) (length cl-map))
226 (if (aref cl-map cl-i)
227 (funcall cl-func cl-i (aref cl-map cl-i)))))))
228
229 (defun cl-map-keymap-recursively (cl-func-rec cl-map &optional cl-base)
230 (or cl-base
231 (setq cl-base (copy-sequence [0])))
232 (cl-map-keymap
233 (function
234 (lambda (cl-key cl-bind)
235 (aset cl-base (1- (length cl-base)) cl-key)
236 (if (keymapp cl-bind)
237 (cl-map-keymap-recursively
238 cl-func-rec cl-bind
239 (vconcat cl-base (list 0)))
240 (funcall cl-func-rec cl-base cl-bind))))
241 cl-map))
242
243 (defun cl-map-intervals (cl-func &optional cl-what cl-prop cl-start cl-end)
244 (or cl-what (setq cl-what (current-buffer)))
245 (if (bufferp cl-what)
246 (let (cl-mark cl-mark2 (cl-next t) cl-next2)
247 (save-excursion
248 (set-buffer cl-what)
249 (setq cl-mark (copy-marker (or cl-start (point-min))))
250 (setq cl-mark2 (and cl-end (copy-marker cl-end))))
251 (while (and cl-next (or (not cl-mark2) (< cl-mark cl-mark2)))
252 (setq cl-next (and (fboundp 'next-property-change)
253 (if cl-prop (next-single-property-change
254 cl-mark cl-prop cl-what)
255 (next-property-change cl-mark cl-what)))
256 cl-next2 (or cl-next (save-excursion
257 (set-buffer cl-what) (point-max))))
258 (funcall cl-func (prog1 (marker-position cl-mark)
259 (set-marker cl-mark cl-next2))
260 (if cl-mark2 (min cl-next2 cl-mark2) cl-next2)))
261 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil)))
262 (or cl-start (setq cl-start 0))
263 (or cl-end (setq cl-end (length cl-what)))
264 (while (< cl-start cl-end)
265 (let ((cl-next (or (and (fboundp 'next-property-change)
266 (if cl-prop (next-single-property-change
267 cl-start cl-prop cl-what)
268 (next-property-change cl-start cl-what)))
269 cl-end)))
270 (funcall cl-func cl-start (min cl-next cl-end))
271 (setq cl-start cl-next)))))
272
273 (defun cl-map-overlays (cl-func &optional cl-buffer cl-start cl-end cl-arg)
274 (or cl-buffer (setq cl-buffer (current-buffer)))
275 (if (fboundp 'overlay-lists)
276
277 ;; This is the preferred algorithm, though overlay-lists is undocumented.
278 (let (cl-ovl)
279 (save-excursion
280 (set-buffer cl-buffer)
281 (setq cl-ovl (overlay-lists))
282 (if cl-start (setq cl-start (copy-marker cl-start)))
283 (if cl-end (setq cl-end (copy-marker cl-end))))
284 (setq cl-ovl (nconc (car cl-ovl) (cdr cl-ovl)))
285 (while (and cl-ovl
286 (or (not (overlay-start (car cl-ovl)))
287 (and cl-end (>= (overlay-start (car cl-ovl)) cl-end))
288 (and cl-start (<= (overlay-end (car cl-ovl)) cl-start))
289 (not (funcall cl-func (car cl-ovl) cl-arg))))
290 (setq cl-ovl (cdr cl-ovl)))
291 (if cl-start (set-marker cl-start nil))
292 (if cl-end (set-marker cl-end nil)))
293
294 ;; This alternate algorithm fails to find zero-length overlays.
295 (let ((cl-mark (save-excursion (set-buffer cl-buffer)
296 (copy-marker (or cl-start (point-min)))))
297 (cl-mark2 (and cl-end (save-excursion (set-buffer cl-buffer)
298 (copy-marker cl-end))))
299 cl-pos cl-ovl)
300 (while (save-excursion
301 (and (setq cl-pos (marker-position cl-mark))
302 (< cl-pos (or cl-mark2 (point-max)))
303 (progn
304 (set-buffer cl-buffer)
305 (setq cl-ovl (overlays-at cl-pos))
306 (set-marker cl-mark (next-overlay-change cl-pos)))))
307 (while (and cl-ovl
308 (or (/= (overlay-start (car cl-ovl)) cl-pos)
309 (not (and (funcall cl-func (car cl-ovl) cl-arg)
310 (set-marker cl-mark nil)))))
311 (setq cl-ovl (cdr cl-ovl))))
312 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil)))))
313
314 ;;; Support for `setf'.
315 (defun cl-set-frame-visible-p (frame val)
316 (cond ((null val) (make-frame-invisible frame))
317 ((eq val 'icon) (iconify-frame frame))
318 (t (make-frame-visible frame)))
319 val)
320
321 ;;; Support for `progv'.
322 (defvar cl-progv-save)
323 (defun cl-progv-before (syms values)
324 (while syms
325 (cl-push (if (boundp (car syms))
326 (cons (car syms) (symbol-value (car syms)))
327 (car syms)) cl-progv-save)
328 (if values
329 (set (cl-pop syms) (cl-pop values))
330 (makunbound (cl-pop syms)))))
331
332 (defun cl-progv-after ()
333 (while cl-progv-save
334 (if (consp (car cl-progv-save))
335 (set (car (car cl-progv-save)) (cdr (car cl-progv-save)))
336 (makunbound (car cl-progv-save)))
337 (cl-pop cl-progv-save)))
338
339
340 ;;; Numbers.
341
342 (defun gcd (&rest args)
343 "Return the greatest common divisor of the arguments."
344 (let ((a (abs (or (cl-pop args) 0))))
345 (while args
346 (let ((b (abs (cl-pop args))))
347 (while (> b 0) (setq b (% a (setq a b))))))
348 a))
349
350 (defun lcm (&rest args)
351 "Return the least common multiple of the arguments."
352 (if (memq 0 args)
353 0
354 (let ((a (abs (or (cl-pop args) 1))))
355 (while args
356 (let ((b (abs (cl-pop args))))
357 (setq a (* (/ a (gcd a b)) b))))
358 a)))
359
360 (defun isqrt (a)
361 "Return the integer square root of the argument."
362 (if (and (integerp a) (> a 0))
363 (let ((g (cond ((<= a 100) 10) ((<= a 10000) 100)
364 ((<= a 1000000) 1000) (t a)))
365 g2)
366 (while (< (setq g2 (/ (+ g (/ a g)) 2)) g)
367 (setq g g2))
368 g)
369 (if (eq a 0) 0 (signal 'arith-error nil))))
370
371 (defun cl-expt (x y)
372 "Return X raised to the power of Y. Works only for integer arguments."
373 (if (<= y 0) (if (= y 0) 1 (if (memq x '(-1 1)) (cl-expt x (- y)) 0))
374 (* (if (= (% y 2) 0) 1 x) (cl-expt (* x x) (/ y 2)))))
375 (or (and (fboundp 'expt) (subrp (symbol-function 'expt)))
376 (defalias 'expt 'cl-expt))
377
378 (defun floor* (x &optional y)
379 "Return a list of the floor of X and the fractional part of X.
380 With two arguments, return floor and remainder of their quotient."
381 (let ((q (floor x y)))
382 (list q (- x (if y (* y q) q)))))
383
384 (defun ceiling* (x &optional y)
385 "Return a list of the ceiling of X and the fractional part of X.
386 With two arguments, return ceiling and remainder of their quotient."
387 (let ((res (floor* x y)))
388 (if (= (car (cdr res)) 0) res
389 (list (1+ (car res)) (- (car (cdr res)) (or y 1))))))
390
391 (defun truncate* (x &optional y)
392 "Return a list of the integer part of X and the fractional part of X.
393 With two arguments, return truncation and remainder of their quotient."
394 (if (eq (>= x 0) (or (null y) (>= y 0)))
395 (floor* x y) (ceiling* x y)))
396
397 (defun round* (x &optional y)
398 "Return a list of X rounded to the nearest integer and the remainder.
399 With two arguments, return rounding and remainder of their quotient."
400 (if y
401 (if (and (integerp x) (integerp y))
402 (let* ((hy (/ y 2))
403 (res (floor* (+ x hy) y)))
404 (if (and (= (car (cdr res)) 0)
405 (= (+ hy hy) y)
406 (/= (% (car res) 2) 0))
407 (list (1- (car res)) hy)
408 (list (car res) (- (car (cdr res)) hy))))
409 (let ((q (round (/ x y))))
410 (list q (- x (* q y)))))
411 (if (integerp x) (list x 0)
412 (let ((q (round x)))
413 (list q (- x q))))))
414
415 (defun mod* (x y)
416 "The remainder of X divided by Y, with the same sign as Y."
417 (nth 1 (floor* x y)))
418
419 (defun rem* (x y)
420 "The remainder of X divided by Y, with the same sign as X."
421 (nth 1 (truncate* x y)))
422
423 (defun signum (a)
424 "Return 1 if A is positive, -1 if negative, 0 if zero."
425 (cond ((> a 0) 1) ((< a 0) -1) (t 0)))
426
427
428 ;; Random numbers.
429
430 (defvar *random-state*)
431 (defun random* (lim &optional state)
432 "Return a random nonnegative number less than LIM, an integer or float.
433 Optional second arg STATE is a random-state object."
434 (or state (setq state *random-state*))
435 ;; Inspired by "ran3" from Numerical Recipes. Additive congruential method.
436 (let ((vec (aref state 3)))
437 (if (integerp vec)
438 (let ((i 0) (j (- 1357335 (% (abs vec) 1357333))) (k 1) ii)
439 (aset state 3 (setq vec (make-vector 55 nil)))
440 (aset vec 0 j)
441 (while (> (setq i (% (+ i 21) 55)) 0)
442 (aset vec i (setq j (prog1 k (setq k (- j k))))))
443 (while (< (setq i (1+ i)) 200) (random* 2 state))))
444 (let* ((i (aset state 1 (% (1+ (aref state 1)) 55)))
445 (j (aset state 2 (% (1+ (aref state 2)) 55)))
446 (n (logand 8388607 (aset vec i (- (aref vec i) (aref vec j))))))
447 (if (integerp lim)
448 (if (<= lim 512) (% n lim)
449 (if (> lim 8388607) (setq n (+ (lsh n 9) (random* 512 state))))
450 (let ((mask 1023))
451 (while (< mask (1- lim)) (setq mask (1+ (+ mask mask))))
452 (if (< (setq n (logand n mask)) lim) n (random* lim state))))
453 (* (/ n '8388608e0) lim)))))
454
455 (defun make-random-state (&optional state)
456 "Return a copy of random-state STATE, or of `*random-state*' if omitted.
457 If STATE is t, return a new state object seeded from the time of day."
458 (cond ((null state) (make-random-state *random-state*))
459 ((vectorp state) (cl-copy-tree state t))
460 ((integerp state) (vector 'cl-random-state-tag -1 30 state))
461 (t (make-random-state (cl-random-time)))))
462
463 (defun random-state-p (object)
464 "Return t if OBJECT is a random-state object."
465 (and (vectorp object) (= (length object) 4)
466 (eq (aref object 0) 'cl-random-state-tag)))
467
468
469 ;; Implementation limits.
470
471 (defun cl-finite-do (func a b)
472 (condition-case err
473 (let ((res (funcall func a b))) ; check for IEEE infinity
474 (and (numberp res) (/= res (/ res 2)) res))
475 (arith-error nil)))
476
477 (defvar most-positive-float)
478 (defvar most-negative-float)
479 (defvar least-positive-float)
480 (defvar least-negative-float)
481 (defvar least-positive-normalized-float)
482 (defvar least-negative-normalized-float)
483 (defvar float-epsilon)
484 (defvar float-negative-epsilon)
485
486 (defun cl-float-limits ()
487 (or most-positive-float (not (numberp '2e1))
488 (let ((x '2e0) y z)
489 ;; Find maximum exponent (first two loops are optimizations)
490 (while (cl-finite-do '* x x) (setq x (* x x)))
491 (while (cl-finite-do '* x (/ x 2)) (setq x (* x (/ x 2))))
492 (while (cl-finite-do '+ x x) (setq x (+ x x)))
493 (setq z x y (/ x 2))
494 ;; Now fill in 1's in the mantissa.
495 (while (and (cl-finite-do '+ x y) (/= (+ x y) x))
496 (setq x (+ x y) y (/ y 2)))
497 (setq most-positive-float x
498 most-negative-float (- x))
499 ;; Divide down until mantissa starts rounding.
500 (setq x (/ x z) y (/ 16 z) x (* x y))
501 (while (condition-case err (and (= x (* (/ x 2) 2)) (> (/ y 2) 0))
502 (arith-error nil))
503 (setq x (/ x 2) y (/ y 2)))
504 (setq least-positive-normalized-float y
505 least-negative-normalized-float (- y))
506 ;; Divide down until value underflows to zero.
507 (setq x (/ 1 z) y x)
508 (while (condition-case err (> (/ x 2) 0) (arith-error nil))
509 (setq x (/ x 2)))
510 (setq least-positive-float x
511 least-negative-float (- x))
512 (setq x '1e0)
513 (while (/= (+ '1e0 x) '1e0) (setq x (/ x 2)))
514 (setq float-epsilon (* x 2))
515 (setq x '1e0)
516 (while (/= (- '1e0 x) '1e0) (setq x (/ x 2)))
517 (setq float-negative-epsilon (* x 2))))
518 nil)
519
520
521 ;;; Sequence functions.
522
523 (defun subseq (seq start &optional end)
524 "Return the subsequence of SEQ from START to END.
525 If END is omitted, it defaults to the length of the sequence.
526 If START or END is negative, it counts from the end."
527 (if (stringp seq) (substring seq start end)
528 (let (len)
529 (and end (< end 0) (setq end (+ end (setq len (length seq)))))
530 (if (< start 0) (setq start (+ start (or len (setq len (length seq))))))
531 (cond ((listp seq)
532 (if (> start 0) (setq seq (nthcdr start seq)))
533 (if end
534 (let ((res nil))
535 (while (>= (setq end (1- end)) start)
536 (cl-push (cl-pop seq) res))
537 (nreverse res))
538 (copy-sequence seq)))
539 (t
540 (or end (setq end (or len (length seq))))
541 (let ((res (make-vector (max (- end start) 0) nil))
542 (i 0))
543 (while (< start end)
544 (aset res i (aref seq start))
545 (setq i (1+ i) start (1+ start)))
546 res))))))
547
548 (defun concatenate (type &rest seqs)
549 "Concatenate, into a sequence of type TYPE, the argument SEQUENCES."
550 (cond ((eq type 'vector) (apply 'vconcat seqs))
551 ((eq type 'string) (apply 'concat seqs))
552 ((eq type 'list) (apply 'append (append seqs '(nil))))
553 (t (error "Not a sequence type name: %s" type))))
554
555
556 ;;; List functions.
557
558 (defun revappend (x y)
559 "Equivalent to (append (reverse X) Y)."
560 (nconc (reverse x) y))
561
562 (defun nreconc (x y)
563 "Equivalent to (nconc (nreverse X) Y)."
564 (nconc (nreverse x) y))
565
566 (defun list-length (x)
567 "Return the length of a list. Return nil if list is circular."
568 (let ((n 0) (fast x) (slow x))
569 (while (and (cdr fast) (not (and (eq fast slow) (> n 0))))
570 (setq n (+ n 2) fast (cdr (cdr fast)) slow (cdr slow)))
571 (if fast (if (cdr fast) nil (1+ n)) n)))
572
573 (defun tailp (sublist list)
574 "Return true if SUBLIST is a tail of LIST."
575 (while (and (consp list) (not (eq sublist list)))
576 (setq list (cdr list)))
577 (if (numberp sublist) (equal sublist list) (eq sublist list)))
578
579 (defun cl-copy-tree (tree &optional vecp)
580 "Make a copy of TREE.
581 If TREE is a cons cell, this recursively copies both its car and its cdr.
582 Contrast to copy-sequence, which copies only along the cdrs. With second
583 argument VECP, this copies vectors as well as conses."
584 (if (consp tree)
585 (let ((p (setq tree (copy-list tree))))
586 (while (consp p)
587 (if (or (consp (car p)) (and vecp (vectorp (car p))))
588 (setcar p (cl-copy-tree (car p) vecp)))
589 (or (listp (cdr p)) (setcdr p (cl-copy-tree (cdr p) vecp)))
590 (cl-pop p)))
591 (if (and vecp (vectorp tree))
592 (let ((i (length (setq tree (copy-sequence tree)))))
593 (while (>= (setq i (1- i)) 0)
594 (aset tree i (cl-copy-tree (aref tree i) vecp))))))
595 tree)
596 (or (and (fboundp 'copy-tree) (subrp (symbol-function 'copy-tree)))
597 (defalias 'copy-tree 'cl-copy-tree))
598
599
600 ;;; Property lists.
601
602 (defun get* (sym tag &optional def) ; See compiler macro in cl-macs.el
603 "Return the value of SYMBOL's PROPNAME property, or DEFAULT if none."
604 (or (get sym tag)
605 (and def
606 (let ((plist (symbol-plist sym)))
607 (while (and plist (not (eq (car plist) tag)))
608 (setq plist (cdr (cdr plist))))
609 (if plist (car (cdr plist)) def)))))
610
611 (defun getf (plist tag &optional def)
612 "Search PROPLIST for property PROPNAME; return its value or DEFAULT.
613 PROPLIST is a list of the sort returned by `symbol-plist'."
614 (setplist '--cl-getf-symbol-- plist)
615 (or (get '--cl-getf-symbol-- tag)
616 ;; Originally we called get* here,
617 ;; but that fails, because get* has a compiler macro
618 ;; definition that uses getf!
619 (when def
620 (while (and plist (not (eq (car plist) tag)))
621 (setq plist (cdr (cdr plist))))
622 (if plist (car (cdr plist)) def))))
623
624 (defun cl-set-getf (plist tag val)
625 (let ((p plist))
626 (while (and p (not (eq (car p) tag))) (setq p (cdr (cdr p))))
627 (if p (progn (setcar (cdr p) val) plist) (list* tag val plist))))
628
629 (defun cl-do-remf (plist tag)
630 (let ((p (cdr plist)))
631 (while (and (cdr p) (not (eq (car (cdr p)) tag))) (setq p (cdr (cdr p))))
632 (and (cdr p) (progn (setcdr p (cdr (cdr (cdr p)))) t))))
633
634 (defun cl-remprop (sym tag)
635 "Remove from SYMBOL's plist the property PROP and its value."
636 (let ((plist (symbol-plist sym)))
637 (if (and plist (eq tag (car plist)))
638 (progn (setplist sym (cdr (cdr plist))) t)
639 (cl-do-remf plist tag))))
640 (or (and (fboundp 'remprop) (subrp (symbol-function 'remprop)))
641 (defalias 'remprop 'cl-remprop))
642
643
644
645 ;;; Hash tables.
646
647 (defun cl-make-hash-table (&rest cl-keys)
648 "Make an empty Common Lisp-style hash-table.
649 Keywords supported: :test :size
650 The Common Lisp keywords :rehash-size and :rehash-threshold are ignored."
651 (let ((cl-test (or (car (cdr (memq ':test cl-keys))) 'eql))
652 (cl-size (or (car (cdr (memq ':size cl-keys))) 20)))
653 (make-hash-table :size cl-size :test cl-size)))
654
655 (defun cl-hash-table-p (x)
656 "Return t if OBJECT is a hash table."
657 (or (hash-table-p x)
658 (eq (car-safe x) 'cl-hash-table-tag)))
659
660 (defun cl-not-hash-table (x &optional y &rest z)
661 (signal 'wrong-type-argument (list 'cl-hash-table-p (or y x))))
662
663 (defun cl-hash-lookup (key table)
664 (or (eq (car-safe table) 'cl-hash-table-tag) (cl-not-hash-table table))
665 (let* ((array (nth 2 table)) (test (car (cdr table))) (str key) sym)
666 (if (symbolp array) (setq str nil sym (symbol-value array))
667 (while (or (consp str) (and (vectorp str) (> (length str) 0)))
668 (setq str (elt str 0)))
669 (cond ((stringp str) (if (eq test 'equalp) (setq str (downcase str))))
670 ((symbolp str) (setq str (symbol-name str)))
671 ((and (numberp str) (> str -8000000) (< str 8000000))
672 (or (integerp str) (setq str (truncate str)))
673 (setq str (aref ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"
674 "11" "12" "13" "14" "15"] (logand str 15))))
675 (t (setq str "*")))
676 (setq sym (symbol-value (intern-soft str array))))
677 (list (and sym (cond ((or (eq test 'eq)
678 (and (eq test 'eql) (not (numberp key))))
679 (assq key sym))
680 ((memq test '(eql equal)) (assoc key sym))
681 (t (assoc* key sym ':test test))))
682 sym str)))
683
684 (defun cl-gethash (key table &optional def)
685 "Look up KEY in HASH-TABLE; return corresponding value, or DEFAULT."
686 (if (consp table)
687 (let ((found (cl-hash-lookup key table)))
688 (if (car found) (cdr (car found)) def))
689 (gethash key table def)))
690
691 (defun cl-puthash (key val table)
692 (if (consp table)
693 (let ((found (cl-hash-lookup key table)))
694 (if (car found) (setcdr (car found) val)
695 (if (nth 2 found)
696 (progn
697 (if (> (nth 3 table) (* (length (nth 2 table)) 3))
698 (let ((new-table (make-vector (nth 3 table) 0)))
699 (mapatoms (function
700 (lambda (sym)
701 (set (intern (symbol-name sym) new-table)
702 (symbol-value sym))))
703 (nth 2 table))
704 (setcar (cdr (cdr table)) new-table)))
705 (set (intern (nth 2 found) (nth 2 table))
706 (cons (cons key val) (nth 1 found))))
707 (set (nth 2 table) (cons (cons key val) (nth 1 found))))
708 (setcar (cdr (cdr (cdr table))) (1+ (nth 3 table)))))
709 (funcall 'puthash key val table)) val)
710
711 (defun cl-remhash (key table)
712 "Remove KEY from HASH-TABLE."
713 (if (consp table)
714 (let ((found (cl-hash-lookup key table)))
715 (and (car found)
716 (let ((del (delq (car found) (nth 1 found))))
717 (setcar (cdr (cdr (cdr table))) (1- (nth 3 table)))
718 (if (nth 2 found) (set (intern (nth 2 found) (nth 2 table)) del)
719 (set (nth 2 table) del)) t)))
720 (prog1 (not (eq (gethash key table '--cl--) '--cl--))
721 (remhash key table))))
722
723 (defun cl-clrhash (table)
724 "Clear HASH-TABLE."
725 (if (consp table)
726 (progn
727 (or (cl-hash-table-p table) (cl-not-hash-table table))
728 (if (symbolp (nth 2 table)) (set (nth 2 table) nil)
729 (setcar (cdr (cdr table)) (make-vector (length (nth 2 table)) 0)))
730 (setcar (cdr (cdr (cdr table))) 0))
731 (clrhash table))
732 nil)
733
734 (defun cl-maphash (cl-func cl-table)
735 "Call FUNCTION on keys and values from HASH-TABLE."
736 (or (cl-hash-table-p cl-table) (cl-not-hash-table cl-table))
737 (if (consp cl-table)
738 (mapatoms (function (lambda (cl-x)
739 (setq cl-x (symbol-value cl-x))
740 (while cl-x
741 (funcall cl-func (car (car cl-x))
742 (cdr (car cl-x)))
743 (setq cl-x (cdr cl-x)))))
744 (if (symbolp (nth 2 cl-table))
745 (vector (nth 2 cl-table)) (nth 2 cl-table)))
746 (maphash cl-func cl-table)))
747
748 (defun cl-hash-table-count (table)
749 "Return the number of entries in HASH-TABLE."
750 (or (cl-hash-table-p table) (cl-not-hash-table table))
751 (if (consp table)
752 (nth 3 table)
753 (hash-table-count table)))
754
755
756 ;;; Some debugging aids.
757
758 (defun cl-prettyprint (form)
759 "Insert a pretty-printed rendition of a Lisp FORM in current buffer."
760 (let ((pt (point)) last)
761 (insert "\n" (prin1-to-string form) "\n")
762 (setq last (point))
763 (goto-char (1+ pt))
764 (while (search-forward "(quote " last t)
765 (delete-backward-char 7)
766 (insert "'")
767 (forward-sexp)
768 (delete-char 1))
769 (goto-char (1+ pt))
770 (cl-do-prettyprint)))
771
772 (defun cl-do-prettyprint ()
773 (skip-chars-forward " ")
774 (if (looking-at "(")
775 (let ((skip (or (looking-at "((") (looking-at "(prog")
776 (looking-at "(unwind-protect ")
777 (looking-at "(function (")
778 (looking-at "(cl-block-wrapper ")))
779 (two (or (looking-at "(defun ") (looking-at "(defmacro ")))
780 (let (or (looking-at "(let\\*? ") (looking-at "(while ")))
781 (set (looking-at "(p?set[qf] ")))
782 (if (or skip let
783 (progn
784 (forward-sexp)
785 (and (>= (current-column) 78) (progn (backward-sexp) t))))
786 (let ((nl t))
787 (forward-char 1)
788 (cl-do-prettyprint)
789 (or skip (looking-at ")") (cl-do-prettyprint))
790 (or (not two) (looking-at ")") (cl-do-prettyprint))
791 (while (not (looking-at ")"))
792 (if set (setq nl (not nl)))
793 (if nl (insert "\n"))
794 (lisp-indent-line)
795 (cl-do-prettyprint))
796 (forward-char 1))))
797 (forward-sexp)))
798
799 (defvar cl-macroexpand-cmacs nil)
800 (defvar cl-closure-vars nil)
801
802 (defun cl-macroexpand-all (form &optional env)
803 "Expand all macro calls through a Lisp FORM.
804 This also does some trivial optimizations to make the form prettier."
805 (while (or (not (eq form (setq form (macroexpand form env))))
806 (and cl-macroexpand-cmacs
807 (not (eq form (setq form (compiler-macroexpand form)))))))
808 (cond ((not (consp form)) form)
809 ((memq (car form) '(let let*))
810 (if (null (nth 1 form))
811 (cl-macroexpand-all (cons 'progn (cddr form)) env)
812 (let ((letf nil) (res nil) (lets (cadr form)))
813 (while lets
814 (cl-push (if (consp (car lets))
815 (let ((exp (cl-macroexpand-all (caar lets) env)))
816 (or (symbolp exp) (setq letf t))
817 (cons exp (cl-macroexpand-body (cdar lets) env)))
818 (let ((exp (cl-macroexpand-all (car lets) env)))
819 (if (symbolp exp) exp
820 (setq letf t) (list exp nil)))) res)
821 (setq lets (cdr lets)))
822 (list* (if letf (if (eq (car form) 'let) 'letf 'letf*) (car form))
823 (nreverse res) (cl-macroexpand-body (cddr form) env)))))
824 ((eq (car form) 'cond)
825 (cons (car form)
826 (mapcar (function (lambda (x) (cl-macroexpand-body x env)))
827 (cdr form))))
828 ((eq (car form) 'condition-case)
829 (list* (car form) (nth 1 form) (cl-macroexpand-all (nth 2 form) env)
830 (mapcar (function
831 (lambda (x)
832 (cons (car x) (cl-macroexpand-body (cdr x) env))))
833 (cdddr form))))
834 ((memq (car form) '(quote function))
835 (if (eq (car-safe (nth 1 form)) 'lambda)
836 (let ((body (cl-macroexpand-body (cddadr form) env)))
837 (if (and cl-closure-vars (eq (car form) 'function)
838 (cl-expr-contains-any body cl-closure-vars))
839 (let* ((new (mapcar 'gensym cl-closure-vars))
840 (sub (pairlis cl-closure-vars new)) (decls nil))
841 (while (or (stringp (car body))
842 (eq (car-safe (car body)) 'interactive))
843 (cl-push (list 'quote (cl-pop body)) decls))
844 (put (car (last cl-closure-vars)) 'used t)
845 (append
846 (list 'list '(quote lambda) '(quote (&rest --cl-rest--)))
847 (sublis sub (nreverse decls))
848 (list
849 (list* 'list '(quote apply)
850 (list 'list '(quote quote)
851 (list 'function
852 (list* 'lambda
853 (append new (cadadr form))
854 (sublis sub body))))
855 (nconc (mapcar (function
856 (lambda (x)
857 (list 'list '(quote quote) x)))
858 cl-closure-vars)
859 '((quote --cl-rest--)))))))
860 (list (car form) (list* 'lambda (cadadr form) body))))
861 (let ((found (assq (cadr form) env)))
862 (if (eq (cadr (caddr found)) 'cl-labels-args)
863 (cl-macroexpand-all (cadr (caddr (cadddr found))) env)
864 form))))
865 ((memq (car form) '(defun defmacro))
866 (list* (car form) (nth 1 form) (cl-macroexpand-body (cddr form) env)))
867 ((and (eq (car form) 'progn) (not (cddr form)))
868 (cl-macroexpand-all (nth 1 form) env))
869 ((eq (car form) 'setq)
870 (let* ((args (cl-macroexpand-body (cdr form) env)) (p args))
871 (while (and p (symbolp (car p))) (setq p (cddr p)))
872 (if p (cl-macroexpand-all (cons 'setf args)) (cons 'setq args))))
873 (t (cons (car form) (cl-macroexpand-body (cdr form) env)))))
874
875 (defun cl-macroexpand-body (body &optional env)
876 (mapcar (function (lambda (x) (cl-macroexpand-all x env))) body))
877
878 (defun cl-prettyexpand (form &optional full)
879 (message "Expanding...")
880 (let ((cl-macroexpand-cmacs full) (cl-compiling-file full)
881 (byte-compile-macro-environment nil))
882 (setq form (cl-macroexpand-all form
883 (and (not full) '((block) (eval-when)))))
884 (message "Formatting...")
885 (prog1 (cl-prettyprint form)
886 (message ""))))
887
888
889
890 (run-hooks 'cl-extra-load-hook)
891
892 ;;; cl-extra.el ends here