]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/cl-extra.el
(cl-old-mapc): Fix definition.
[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 (defvar cl-old-mapc (prog1 (symbol-function 'mapc)
156 (defalias 'mapc 'cl-mapc)))
157
158 (defun cl-mapc (cl-func cl-seq &rest cl-rest)
159 "Like `mapcar', but does not accumulate values returned by the function."
160 (if cl-rest
161 (progn (apply 'map nil cl-func cl-seq cl-rest)
162 cl-seq)
163 (funcall cl-old-mapc cl-func cl-seq)))
164
165 (defun mapl (cl-func cl-list &rest cl-rest)
166 "Like `maplist', but does not accumulate values returned by the function."
167 (if cl-rest
168 (apply 'maplist cl-func cl-list cl-rest)
169 (let ((cl-p cl-list))
170 (while cl-p (funcall cl-func cl-p) (setq cl-p (cdr cl-p)))))
171 cl-list)
172
173 (defun mapcan (cl-func cl-seq &rest cl-rest)
174 "Like `mapcar', but nconc's together the values returned by the function."
175 (apply 'nconc (apply 'mapcar* cl-func cl-seq cl-rest)))
176
177 (defun mapcon (cl-func cl-list &rest cl-rest)
178 "Like `maplist', but nconc's together the values returned by the function."
179 (apply 'nconc (apply 'maplist cl-func cl-list cl-rest)))
180
181 (defun some (cl-pred cl-seq &rest cl-rest)
182 "Return true if PREDICATE is true of any element of SEQ or SEQs.
183 If so, return the true (non-nil) value returned by PREDICATE."
184 (if (or cl-rest (nlistp cl-seq))
185 (catch 'cl-some
186 (apply 'map nil
187 (function (lambda (&rest cl-x)
188 (let ((cl-res (apply cl-pred cl-x)))
189 (if cl-res (throw 'cl-some cl-res)))))
190 cl-seq cl-rest) nil)
191 (let ((cl-x nil))
192 (while (and cl-seq (not (setq cl-x (funcall cl-pred (cl-pop cl-seq))))))
193 cl-x)))
194
195 (defun every (cl-pred cl-seq &rest cl-rest)
196 "Return true if PREDICATE is true of every element of SEQ or SEQs."
197 (if (or cl-rest (nlistp cl-seq))
198 (catch 'cl-every
199 (apply 'map nil
200 (function (lambda (&rest cl-x)
201 (or (apply cl-pred cl-x) (throw 'cl-every nil))))
202 cl-seq cl-rest) t)
203 (while (and cl-seq (funcall cl-pred (car cl-seq)))
204 (setq cl-seq (cdr cl-seq)))
205 (null cl-seq)))
206
207 (defun notany (cl-pred cl-seq &rest cl-rest)
208 "Return true if PREDICATE is false of every element of SEQ or SEQs."
209 (not (apply 'some cl-pred cl-seq cl-rest)))
210
211 (defun notevery (cl-pred cl-seq &rest cl-rest)
212 "Return true if PREDICATE is false of some element of SEQ or SEQs."
213 (not (apply 'every cl-pred cl-seq cl-rest)))
214
215 ;;; Support for `loop'.
216 (defun cl-map-keymap (cl-func cl-map)
217 (while (symbolp cl-map) (setq cl-map (symbol-function cl-map)))
218 (if (listp cl-map)
219 (let ((cl-p cl-map))
220 (while (consp (setq cl-p (cdr cl-p)))
221 (cond ((consp (car cl-p))
222 (funcall cl-func (car (car cl-p)) (cdr (car cl-p))))
223 ((vectorp (car cl-p))
224 (cl-map-keymap cl-func (car cl-p)))
225 ((eq (car cl-p) 'keymap)
226 (setq cl-p nil)))))
227 (let ((cl-i -1))
228 (while (< (setq cl-i (1+ cl-i)) (length cl-map))
229 (if (aref cl-map cl-i)
230 (funcall cl-func cl-i (aref cl-map cl-i)))))))
231
232 (defun cl-map-keymap-recursively (cl-func-rec cl-map &optional cl-base)
233 (or cl-base
234 (setq cl-base (copy-sequence [0])))
235 (cl-map-keymap
236 (function
237 (lambda (cl-key cl-bind)
238 (aset cl-base (1- (length cl-base)) cl-key)
239 (if (keymapp cl-bind)
240 (cl-map-keymap-recursively
241 cl-func-rec cl-bind
242 (vconcat cl-base (list 0)))
243 (funcall cl-func-rec cl-base cl-bind))))
244 cl-map))
245
246 (defun cl-map-intervals (cl-func &optional cl-what cl-prop cl-start cl-end)
247 (or cl-what (setq cl-what (current-buffer)))
248 (if (bufferp cl-what)
249 (let (cl-mark cl-mark2 (cl-next t) cl-next2)
250 (with-current-buffer cl-what
251 (setq cl-mark (copy-marker (or cl-start (point-min))))
252 (setq cl-mark2 (and cl-end (copy-marker cl-end))))
253 (while (and cl-next (or (not cl-mark2) (< cl-mark cl-mark2)))
254 (setq cl-next (if cl-prop (next-single-property-change
255 cl-mark cl-prop cl-what)
256 (next-property-change cl-mark cl-what))
257 cl-next2 (or cl-next (with-current-buffer cl-what
258 (point-max))))
259 (funcall cl-func (prog1 (marker-position cl-mark)
260 (set-marker cl-mark cl-next2))
261 (if cl-mark2 (min cl-next2 cl-mark2) cl-next2)))
262 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil)))
263 (or cl-start (setq cl-start 0))
264 (or cl-end (setq cl-end (length cl-what)))
265 (while (< cl-start cl-end)
266 (let ((cl-next (or (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 (with-current-buffer cl-buffer
280 (setq cl-ovl (overlay-lists))
281 (if cl-start (setq cl-start (copy-marker cl-start)))
282 (if cl-end (setq cl-end (copy-marker cl-end))))
283 (setq cl-ovl (nconc (car cl-ovl) (cdr cl-ovl)))
284 (while (and cl-ovl
285 (or (not (overlay-start (car cl-ovl)))
286 (and cl-end (>= (overlay-start (car cl-ovl)) cl-end))
287 (and cl-start (<= (overlay-end (car cl-ovl)) cl-start))
288 (not (funcall cl-func (car cl-ovl) cl-arg))))
289 (setq cl-ovl (cdr cl-ovl)))
290 (if cl-start (set-marker cl-start nil))
291 (if cl-end (set-marker cl-end nil)))
292
293 ;; This alternate algorithm fails to find zero-length overlays.
294 (let ((cl-mark (with-current-buffer cl-buffer
295 (copy-marker (or cl-start (point-min)))))
296 (cl-mark2 (and cl-end (with-current-buffer cl-buffer
297 (copy-marker cl-end))))
298 cl-pos cl-ovl)
299 (while (save-excursion
300 (and (setq cl-pos (marker-position cl-mark))
301 (< cl-pos (or cl-mark2 (point-max)))
302 (progn
303 (set-buffer cl-buffer)
304 (setq cl-ovl (overlays-at cl-pos))
305 (set-marker cl-mark (next-overlay-change cl-pos)))))
306 (while (and cl-ovl
307 (or (/= (overlay-start (car cl-ovl)) cl-pos)
308 (not (and (funcall cl-func (car cl-ovl) cl-arg)
309 (set-marker cl-mark nil)))))
310 (setq cl-ovl (cdr cl-ovl))))
311 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil)))))
312
313 ;;; Support for `setf'.
314 (defun cl-set-frame-visible-p (frame val)
315 (cond ((null val) (make-frame-invisible frame))
316 ((eq val 'icon) (iconify-frame frame))
317 (t (make-frame-visible frame)))
318 val)
319
320 ;;; Support for `progv'.
321 (defvar cl-progv-save)
322 (defun cl-progv-before (syms values)
323 (while syms
324 (cl-push (if (boundp (car syms))
325 (cons (car syms) (symbol-value (car syms)))
326 (car syms)) cl-progv-save)
327 (if values
328 (set (cl-pop syms) (cl-pop values))
329 (makunbound (cl-pop syms)))))
330
331 (defun cl-progv-after ()
332 (while cl-progv-save
333 (if (consp (car cl-progv-save))
334 (set (car (car cl-progv-save)) (cdr (car cl-progv-save)))
335 (makunbound (car cl-progv-save)))
336 (cl-pop cl-progv-save)))
337
338
339 ;;; Numbers.
340
341 (defun gcd (&rest args)
342 "Return the greatest common divisor of the arguments."
343 (let ((a (abs (or (cl-pop args) 0))))
344 (while args
345 (let ((b (abs (cl-pop args))))
346 (while (> b 0) (setq b (% a (setq a b))))))
347 a))
348
349 (defun lcm (&rest args)
350 "Return the least common multiple of the arguments."
351 (if (memq 0 args)
352 0
353 (let ((a (abs (or (cl-pop args) 1))))
354 (while args
355 (let ((b (abs (cl-pop args))))
356 (setq a (* (/ a (gcd a b)) b))))
357 a)))
358
359 (defun isqrt (a)
360 "Return the integer square root of the argument."
361 (if (and (integerp a) (> a 0))
362 (let ((g (cond ((<= a 100) 10) ((<= a 10000) 100)
363 ((<= a 1000000) 1000) (t a)))
364 g2)
365 (while (< (setq g2 (/ (+ g (/ a g)) 2)) g)
366 (setq g g2))
367 g)
368 (if (eq a 0) 0 (signal 'arith-error nil))))
369
370 (defun floor* (x &optional y)
371 "Return a list of the floor of X and the fractional part of X.
372 With two arguments, return floor and remainder of their quotient."
373 (let ((q (floor x y)))
374 (list q (- x (if y (* y q) q)))))
375
376 (defun ceiling* (x &optional y)
377 "Return a list of the ceiling of X and the fractional part of X.
378 With two arguments, return ceiling and remainder of their quotient."
379 (let ((res (floor* x y)))
380 (if (= (car (cdr res)) 0) res
381 (list (1+ (car res)) (- (car (cdr res)) (or y 1))))))
382
383 (defun truncate* (x &optional y)
384 "Return a list of the integer part of X and the fractional part of X.
385 With two arguments, return truncation and remainder of their quotient."
386 (if (eq (>= x 0) (or (null y) (>= y 0)))
387 (floor* x y) (ceiling* x y)))
388
389 (defun round* (x &optional y)
390 "Return a list of X rounded to the nearest integer and the remainder.
391 With two arguments, return rounding and remainder of their quotient."
392 (if y
393 (if (and (integerp x) (integerp y))
394 (let* ((hy (/ y 2))
395 (res (floor* (+ x hy) y)))
396 (if (and (= (car (cdr res)) 0)
397 (= (+ hy hy) y)
398 (/= (% (car res) 2) 0))
399 (list (1- (car res)) hy)
400 (list (car res) (- (car (cdr res)) hy))))
401 (let ((q (round (/ x y))))
402 (list q (- x (* q y)))))
403 (if (integerp x) (list x 0)
404 (let ((q (round x)))
405 (list q (- x q))))))
406
407 (defun mod* (x y)
408 "The remainder of X divided by Y, with the same sign as Y."
409 (nth 1 (floor* x y)))
410
411 (defun rem* (x y)
412 "The remainder of X divided by Y, with the same sign as X."
413 (nth 1 (truncate* x y)))
414
415 (defun signum (a)
416 "Return 1 if A is positive, -1 if negative, 0 if zero."
417 (cond ((> a 0) 1) ((< a 0) -1) (t 0)))
418
419
420 ;; Random numbers.
421
422 (defvar *random-state*)
423 (defun random* (lim &optional state)
424 "Return a random nonnegative number less than LIM, an integer or float.
425 Optional second arg STATE is a random-state object."
426 (or state (setq state *random-state*))
427 ;; Inspired by "ran3" from Numerical Recipes. Additive congruential method.
428 (let ((vec (aref state 3)))
429 (if (integerp vec)
430 (let ((i 0) (j (- 1357335 (% (abs vec) 1357333))) (k 1) ii)
431 (aset state 3 (setq vec (make-vector 55 nil)))
432 (aset vec 0 j)
433 (while (> (setq i (% (+ i 21) 55)) 0)
434 (aset vec i (setq j (prog1 k (setq k (- j k))))))
435 (while (< (setq i (1+ i)) 200) (random* 2 state))))
436 (let* ((i (aset state 1 (% (1+ (aref state 1)) 55)))
437 (j (aset state 2 (% (1+ (aref state 2)) 55)))
438 (n (logand 8388607 (aset vec i (- (aref vec i) (aref vec j))))))
439 (if (integerp lim)
440 (if (<= lim 512) (% n lim)
441 (if (> lim 8388607) (setq n (+ (lsh n 9) (random* 512 state))))
442 (let ((mask 1023))
443 (while (< mask (1- lim)) (setq mask (1+ (+ mask mask))))
444 (if (< (setq n (logand n mask)) lim) n (random* lim state))))
445 (* (/ n '8388608e0) lim)))))
446
447 (defun make-random-state (&optional state)
448 "Return a copy of random-state STATE, or of `*random-state*' if omitted.
449 If STATE is t, return a new state object seeded from the time of day."
450 (cond ((null state) (make-random-state *random-state*))
451 ((vectorp state) (cl-copy-tree state t))
452 ((integerp state) (vector 'cl-random-state-tag -1 30 state))
453 (t (make-random-state (cl-random-time)))))
454
455 (defun random-state-p (object)
456 "Return t if OBJECT is a random-state object."
457 (and (vectorp object) (= (length object) 4)
458 (eq (aref object 0) 'cl-random-state-tag)))
459
460
461 ;; Implementation limits.
462
463 (defun cl-finite-do (func a b)
464 (condition-case err
465 (let ((res (funcall func a b))) ; check for IEEE infinity
466 (and (numberp res) (/= res (/ res 2)) res))
467 (arith-error nil)))
468
469 (defvar most-positive-float)
470 (defvar most-negative-float)
471 (defvar least-positive-float)
472 (defvar least-negative-float)
473 (defvar least-positive-normalized-float)
474 (defvar least-negative-normalized-float)
475 (defvar float-epsilon)
476 (defvar float-negative-epsilon)
477
478 (defun cl-float-limits ()
479 (or most-positive-float (not (numberp '2e1))
480 (let ((x '2e0) y z)
481 ;; Find maximum exponent (first two loops are optimizations)
482 (while (cl-finite-do '* x x) (setq x (* x x)))
483 (while (cl-finite-do '* x (/ x 2)) (setq x (* x (/ x 2))))
484 (while (cl-finite-do '+ x x) (setq x (+ x x)))
485 (setq z x y (/ x 2))
486 ;; Now fill in 1's in the mantissa.
487 (while (and (cl-finite-do '+ x y) (/= (+ x y) x))
488 (setq x (+ x y) y (/ y 2)))
489 (setq most-positive-float x
490 most-negative-float (- x))
491 ;; Divide down until mantissa starts rounding.
492 (setq x (/ x z) y (/ 16 z) x (* x y))
493 (while (condition-case err (and (= x (* (/ x 2) 2)) (> (/ y 2) 0))
494 (arith-error nil))
495 (setq x (/ x 2) y (/ y 2)))
496 (setq least-positive-normalized-float y
497 least-negative-normalized-float (- y))
498 ;; Divide down until value underflows to zero.
499 (setq x (/ 1 z) y x)
500 (while (condition-case err (> (/ x 2) 0) (arith-error nil))
501 (setq x (/ x 2)))
502 (setq least-positive-float x
503 least-negative-float (- x))
504 (setq x '1e0)
505 (while (/= (+ '1e0 x) '1e0) (setq x (/ x 2)))
506 (setq float-epsilon (* x 2))
507 (setq x '1e0)
508 (while (/= (- '1e0 x) '1e0) (setq x (/ x 2)))
509 (setq float-negative-epsilon (* x 2))))
510 nil)
511
512
513 ;;; Sequence functions.
514
515 (defun subseq (seq start &optional end)
516 "Return the subsequence of SEQ from START to END.
517 If END is omitted, it defaults to the length of the sequence.
518 If START or END is negative, it counts from the end."
519 (if (stringp seq) (substring seq start end)
520 (let (len)
521 (and end (< end 0) (setq end (+ end (setq len (length seq)))))
522 (if (< start 0) (setq start (+ start (or len (setq len (length seq))))))
523 (cond ((listp seq)
524 (if (> start 0) (setq seq (nthcdr start seq)))
525 (if end
526 (let ((res nil))
527 (while (>= (setq end (1- end)) start)
528 (cl-push (cl-pop seq) res))
529 (nreverse res))
530 (copy-sequence seq)))
531 (t
532 (or end (setq end (or len (length seq))))
533 (let ((res (make-vector (max (- end start) 0) nil))
534 (i 0))
535 (while (< start end)
536 (aset res i (aref seq start))
537 (setq i (1+ i) start (1+ start)))
538 res))))))
539
540 (defun concatenate (type &rest seqs)
541 "Concatenate, into a sequence of type TYPE, the argument SEQUENCES."
542 (cond ((eq type 'vector) (apply 'vconcat seqs))
543 ((eq type 'string) (apply 'concat seqs))
544 ((eq type 'list) (apply 'append (append seqs '(nil))))
545 (t (error "Not a sequence type name: %s" type))))
546
547
548 ;;; List functions.
549
550 (defun revappend (x y)
551 "Equivalent to (append (reverse X) Y)."
552 (nconc (reverse x) y))
553
554 (defun nreconc (x y)
555 "Equivalent to (nconc (nreverse X) Y)."
556 (nconc (nreverse x) y))
557
558 (defun list-length (x)
559 "Return the length of a list. Return nil if list is circular."
560 (let ((n 0) (fast x) (slow x))
561 (while (and (cdr fast) (not (and (eq fast slow) (> n 0))))
562 (setq n (+ n 2) fast (cdr (cdr fast)) slow (cdr slow)))
563 (if fast (if (cdr fast) nil (1+ n)) n)))
564
565 (defun 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)))
570
571 (defun cl-copy-tree (tree &optional vecp)
572 "Make a copy of TREE.
573 If TREE is a cons cell, this recursively copies both its car and its cdr.
574 Contrast to copy-sequence, which copies only along the cdrs. With second
575 argument VECP, this copies vectors as well as conses."
576 (if (consp tree)
577 (let ((p (setq tree (copy-list tree))))
578 (while (consp p)
579 (if (or (consp (car p)) (and vecp (vectorp (car p))))
580 (setcar p (cl-copy-tree (car p) vecp)))
581 (or (listp (cdr p)) (setcdr p (cl-copy-tree (cdr p) vecp)))
582 (cl-pop p)))
583 (if (and vecp (vectorp tree))
584 (let ((i (length (setq tree (copy-sequence tree)))))
585 (while (>= (setq i (1- i)) 0)
586 (aset tree i (cl-copy-tree (aref tree i) vecp))))))
587 tree)
588 (defalias 'copy-tree 'cl-copy-tree)
589
590
591 ;;; Property lists.
592
593 (defun get* (sym tag &optional def) ; See compiler macro in cl-macs.el
594 "Return the value of SYMBOL's PROPNAME property, or DEFAULT if none."
595 (or (get sym tag)
596 (and def
597 (let ((plist (symbol-plist sym)))
598 (while (and plist (not (eq (car plist) tag)))
599 (setq plist (cdr (cdr plist))))
600 (if plist (car (cdr plist)) def)))))
601
602 (defun getf (plist tag &optional def)
603 "Search PROPLIST for property PROPNAME; return its value or DEFAULT.
604 PROPLIST is a list of the sort returned by `symbol-plist'."
605 (setplist '--cl-getf-symbol-- plist)
606 (or (get '--cl-getf-symbol-- tag)
607 ;; Originally we called get* here,
608 ;; but that fails, because get* has a compiler macro
609 ;; definition that uses getf!
610 (when def
611 (while (and plist (not (eq (car plist) tag)))
612 (setq plist (cdr (cdr plist))))
613 (if plist (car (cdr plist)) def))))
614
615 (defun cl-set-getf (plist tag val)
616 (let ((p plist))
617 (while (and p (not (eq (car p) tag))) (setq p (cdr (cdr p))))
618 (if p (progn (setcar (cdr p) val) plist) (list* tag val plist))))
619
620 (defun cl-do-remf (plist tag)
621 (let ((p (cdr plist)))
622 (while (and (cdr p) (not (eq (car (cdr p)) tag))) (setq p (cdr (cdr p))))
623 (and (cdr p) (progn (setcdr p (cdr (cdr (cdr p)))) t))))
624
625 (defun cl-remprop (sym tag)
626 "Remove from SYMBOL's plist the property PROP and its value."
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))))
631 (defalias 'remprop 'cl-remprop)
632
633
634
635 ;;; Hash tables.
636
637 (defun cl-make-hash-table (&rest cl-keys)
638 "Make an empty Common Lisp-style hash-table.
639 Keywords supported: :test :size
640 The Common Lisp keywords :rehash-size and :rehash-threshold are ignored."
641 (let ((cl-test (or (car (cdr (memq :test cl-keys))) 'eql))
642 (cl-size (or (car (cdr (memq :size cl-keys))) 20)))
643 (make-hash-table :size cl-size :test cl-size)))
644
645 (defun cl-hash-table-p (x)
646 "Return t if OBJECT is a hash table."
647 (or (hash-table-p x)
648 (eq (car-safe x) 'cl-hash-table-tag)))
649
650 (defun cl-not-hash-table (x &optional y &rest z)
651 (signal 'wrong-type-argument (list 'cl-hash-table-p (or y x))))
652
653 (defun cl-hash-lookup (key table)
654 (or (eq (car-safe table) 'cl-hash-table-tag) (cl-not-hash-table table))
655 (let* ((array (nth 2 table)) (test (car (cdr table))) (str key) sym)
656 (if (symbolp array) (setq str nil sym (symbol-value array))
657 (while (or (consp str) (and (vectorp str) (> (length str) 0)))
658 (setq str (elt str 0)))
659 (cond ((stringp str) (if (eq test 'equalp) (setq str (downcase str))))
660 ((symbolp str) (setq str (symbol-name str)))
661 ((and (numberp str) (> str -8000000) (< str 8000000))
662 (or (integerp str) (setq str (truncate str)))
663 (setq str (aref ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"
664 "11" "12" "13" "14" "15"] (logand str 15))))
665 (t (setq str "*")))
666 (setq sym (symbol-value (intern-soft str array))))
667 (list (and sym (cond ((or (eq test 'eq)
668 (and (eq test 'eql) (not (numberp key))))
669 (assq key sym))
670 ((memq test '(eql equal)) (assoc key sym))
671 (t (assoc* key sym :test test))))
672 sym str)))
673
674 (defun cl-gethash (key table &optional def)
675 "Look up KEY in HASH-TABLE; return corresponding value, or DEFAULT."
676 (if (consp table)
677 (let ((found (cl-hash-lookup key table)))
678 (if (car found) (cdr (car found)) def))
679 (gethash key table def)))
680
681 (defun cl-puthash (key val table)
682 (if (consp table)
683 (let ((found (cl-hash-lookup key table)))
684 (if (car found) (setcdr (car found) val)
685 (if (nth 2 found)
686 (progn
687 (if (> (nth 3 table) (* (length (nth 2 table)) 3))
688 (let ((new-table (make-vector (nth 3 table) 0)))
689 (mapatoms (function
690 (lambda (sym)
691 (set (intern (symbol-name sym) new-table)
692 (symbol-value sym))))
693 (nth 2 table))
694 (setcar (cdr (cdr table)) new-table)))
695 (set (intern (nth 2 found) (nth 2 table))
696 (cons (cons key val) (nth 1 found))))
697 (set (nth 2 table) (cons (cons key val) (nth 1 found))))
698 (setcar (cdr (cdr (cdr table))) (1+ (nth 3 table)))))
699 (funcall 'puthash key val table)) val)
700
701 (defun cl-remhash (key table)
702 "Remove KEY from HASH-TABLE."
703 (if (consp table)
704 (let ((found (cl-hash-lookup key table)))
705 (and (car found)
706 (let ((del (delq (car found) (nth 1 found))))
707 (setcar (cdr (cdr (cdr table))) (1- (nth 3 table)))
708 (if (nth 2 found) (set (intern (nth 2 found) (nth 2 table)) del)
709 (set (nth 2 table) del)) t)))
710 (prog1 (not (eq (gethash key table '--cl--) '--cl--))
711 (remhash key table))))
712
713 (defun cl-clrhash (table)
714 "Clear HASH-TABLE."
715 (if (consp table)
716 (progn
717 (or (cl-hash-table-p table) (cl-not-hash-table table))
718 (if (symbolp (nth 2 table)) (set (nth 2 table) nil)
719 (setcar (cdr (cdr table)) (make-vector (length (nth 2 table)) 0)))
720 (setcar (cdr (cdr (cdr table))) 0))
721 (clrhash table))
722 nil)
723
724 (defun cl-maphash (cl-func cl-table)
725 "Call FUNCTION on keys and values from HASH-TABLE."
726 (or (cl-hash-table-p cl-table) (cl-not-hash-table cl-table))
727 (if (consp cl-table)
728 (mapatoms (function (lambda (cl-x)
729 (setq cl-x (symbol-value cl-x))
730 (while cl-x
731 (funcall cl-func (car (car cl-x))
732 (cdr (car cl-x)))
733 (setq cl-x (cdr cl-x)))))
734 (if (symbolp (nth 2 cl-table))
735 (vector (nth 2 cl-table)) (nth 2 cl-table)))
736 (maphash cl-func cl-table)))
737
738 (defun cl-hash-table-count (table)
739 "Return the number of entries in HASH-TABLE."
740 (or (cl-hash-table-p table) (cl-not-hash-table table))
741 (if (consp table)
742 (nth 3 table)
743 (hash-table-count table)))
744
745
746 ;;; Some debugging aids.
747
748 (defun cl-prettyprint (form)
749 "Insert a pretty-printed rendition of a Lisp FORM in current buffer."
750 (let ((pt (point)) last)
751 (insert "\n" (prin1-to-string form) "\n")
752 (setq last (point))
753 (goto-char (1+ pt))
754 (while (search-forward "(quote " last t)
755 (delete-backward-char 7)
756 (insert "'")
757 (forward-sexp)
758 (delete-char 1))
759 (goto-char (1+ pt))
760 (cl-do-prettyprint)))
761
762 (defun cl-do-prettyprint ()
763 (skip-chars-forward " ")
764 (if (looking-at "(")
765 (let ((skip (or (looking-at "((") (looking-at "(prog")
766 (looking-at "(unwind-protect ")
767 (looking-at "(function (")
768 (looking-at "(cl-block-wrapper ")))
769 (two (or (looking-at "(defun ") (looking-at "(defmacro ")))
770 (let (or (looking-at "(let\\*? ") (looking-at "(while ")))
771 (set (looking-at "(p?set[qf] ")))
772 (if (or skip let
773 (progn
774 (forward-sexp)
775 (and (>= (current-column) 78) (progn (backward-sexp) t))))
776 (let ((nl t))
777 (forward-char 1)
778 (cl-do-prettyprint)
779 (or skip (looking-at ")") (cl-do-prettyprint))
780 (or (not two) (looking-at ")") (cl-do-prettyprint))
781 (while (not (looking-at ")"))
782 (if set (setq nl (not nl)))
783 (if nl (insert "\n"))
784 (lisp-indent-line)
785 (cl-do-prettyprint))
786 (forward-char 1))))
787 (forward-sexp)))
788
789 (defvar cl-macroexpand-cmacs nil)
790 (defvar cl-closure-vars nil)
791
792 (defun cl-macroexpand-all (form &optional env)
793 "Expand all macro calls through a Lisp FORM.
794 This also does some trivial optimizations to make the form prettier."
795 (while (or (not (eq form (setq form (macroexpand form env))))
796 (and cl-macroexpand-cmacs
797 (not (eq form (setq form (compiler-macroexpand form)))))))
798 (cond ((not (consp form)) form)
799 ((memq (car form) '(let let*))
800 (if (null (nth 1 form))
801 (cl-macroexpand-all (cons 'progn (cddr form)) env)
802 (let ((letf nil) (res nil) (lets (cadr form)))
803 (while lets
804 (cl-push (if (consp (car lets))
805 (let ((exp (cl-macroexpand-all (caar lets) env)))
806 (or (symbolp exp) (setq letf t))
807 (cons exp (cl-macroexpand-body (cdar lets) env)))
808 (let ((exp (cl-macroexpand-all (car lets) env)))
809 (if (symbolp exp) exp
810 (setq letf t) (list exp nil)))) res)
811 (setq lets (cdr lets)))
812 (list* (if letf (if (eq (car form) 'let) 'letf 'letf*) (car form))
813 (nreverse res) (cl-macroexpand-body (cddr form) env)))))
814 ((eq (car form) 'cond)
815 (cons (car form)
816 (mapcar (function (lambda (x) (cl-macroexpand-body x env)))
817 (cdr form))))
818 ((eq (car form) 'condition-case)
819 (list* (car form) (nth 1 form) (cl-macroexpand-all (nth 2 form) env)
820 (mapcar (function
821 (lambda (x)
822 (cons (car x) (cl-macroexpand-body (cdr x) env))))
823 (cdddr form))))
824 ((memq (car form) '(quote function))
825 (if (eq (car-safe (nth 1 form)) 'lambda)
826 (let ((body (cl-macroexpand-body (cddadr form) env)))
827 (if (and cl-closure-vars (eq (car form) 'function)
828 (cl-expr-contains-any body cl-closure-vars))
829 (let* ((new (mapcar 'gensym cl-closure-vars))
830 (sub (pairlis cl-closure-vars new)) (decls nil))
831 (while (or (stringp (car body))
832 (eq (car-safe (car body)) 'interactive))
833 (cl-push (list 'quote (cl-pop body)) decls))
834 (put (car (last cl-closure-vars)) 'used t)
835 (append
836 (list 'list '(quote lambda) '(quote (&rest --cl-rest--)))
837 (sublis sub (nreverse decls))
838 (list
839 (list* 'list '(quote apply)
840 (list 'list '(quote quote)
841 (list 'function
842 (list* 'lambda
843 (append new (cadadr form))
844 (sublis sub body))))
845 (nconc (mapcar (function
846 (lambda (x)
847 (list 'list '(quote quote) x)))
848 cl-closure-vars)
849 '((quote --cl-rest--)))))))
850 (list (car form) (list* 'lambda (cadadr form) body))))
851 (let ((found (assq (cadr form) env)))
852 (if (eq (cadr (caddr found)) 'cl-labels-args)
853 (cl-macroexpand-all (cadr (caddr (cadddr found))) env)
854 form))))
855 ((memq (car form) '(defun defmacro))
856 (list* (car form) (nth 1 form) (cl-macroexpand-body (cddr form) env)))
857 ((and (eq (car form) 'progn) (not (cddr form)))
858 (cl-macroexpand-all (nth 1 form) env))
859 ((eq (car form) 'setq)
860 (let* ((args (cl-macroexpand-body (cdr form) env)) (p args))
861 (while (and p (symbolp (car p))) (setq p (cddr p)))
862 (if p (cl-macroexpand-all (cons 'setf args)) (cons 'setq args))))
863 (t (cons (car form) (cl-macroexpand-body (cdr form) env)))))
864
865 (defun cl-macroexpand-body (body &optional env)
866 (mapcar (function (lambda (x) (cl-macroexpand-all x env))) body))
867
868 (defun cl-prettyexpand (form &optional full)
869 (message "Expanding...")
870 (let ((cl-macroexpand-cmacs full) (cl-compiling-file full)
871 (byte-compile-macro-environment nil))
872 (setq form (cl-macroexpand-all form
873 (and (not full) '((block) (eval-when)))))
874 (message "Formatting...")
875 (prog1 (cl-prettyprint form)
876 (message ""))))
877
878
879
880 (run-hooks 'cl-extra-load-hook)
881
882 ;;; cl-extra.el ends here