.
[emacs.git] / lisp / emacs-lisp / cl-extra.el
blobaa6b4f9ae48aee1891462d495b23edd5d5cd9d75
1 ;;; cl-extra.el --- Common Lisp features, part 2 -*-byte-compile-dynamic: t;-*-
3 ;; Copyright (C) 1993,2000 Free Software Foundation, Inc.
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
6 ;; Keywords: extensions
8 ;; This file is part of GNU Emacs.
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.
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.
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.
25 ;;; Commentary:
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.
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.
34 ;; Bug reports, comments, and suggestions are welcome!
36 ;; This file contains portions of the Common Lisp extensions
37 ;; package which are autoloaded since they are relatively obscure.
39 ;;; Code:
41 (or (memq 'cl-19 features)
42 (error "Tried to load `cl-extra' before `cl'!"))
45 ;;; Type coercion.
47 (defun coerce (x type)
48 "Coerce OBJECT to type TYPE.
49 TYPE is a Common Lisp type specifier."
50 (cond ((eq type 'list) (if (listp x) x (append x nil)))
51 ((eq type 'vector) (if (vectorp x) x (vconcat x)))
52 ((eq type 'string) (if (stringp x) x (concat x)))
53 ((eq type 'array) (if (arrayp x) x (vconcat x)))
54 ((and (eq type 'character) (stringp x) (= (length x) 1)) (aref x 0))
55 ((and (eq type 'character) (symbolp x)) (coerce (symbol-name x) type))
56 ((eq type 'float) (float x))
57 ((typep x type) x)
58 (t (error "Can't coerce %s to type %s" x type))))
61 ;;; Predicates.
63 (defun equalp (x y)
64 "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) (equalp (car x) (car y)))
77 (setq x (cdr x) y (cdr y)))
78 (and (not (consp x)) (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 (equalp (aref x i) (aref y i))))
84 (< i 0))))
85 (t (equal x y))))
88 ;;; Control structures.
90 (defun cl-mapcar-many (cl-func cl-seqs)
91 (if (cdr (cdr cl-seqs))
92 (let* ((cl-res nil)
93 (cl-n (apply 'min (mapcar 'length cl-seqs)))
94 (cl-i 0)
95 (cl-args (copy-sequence cl-seqs))
96 cl-p1 cl-p2)
97 (setq cl-seqs (copy-sequence cl-seqs))
98 (while (< cl-i cl-n)
99 (setq cl-p1 cl-seqs cl-p2 cl-args)
100 (while cl-p1
101 (setcar cl-p2
102 (if (consp (car cl-p1))
103 (prog1 (car (car cl-p1))
104 (setcar cl-p1 (cdr (car cl-p1))))
105 (aref (car cl-p1) cl-i)))
106 (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2)))
107 (push (apply cl-func cl-args) cl-res)
108 (setq cl-i (1+ cl-i)))
109 (nreverse cl-res))
110 (let ((cl-res nil)
111 (cl-x (car cl-seqs))
112 (cl-y (nth 1 cl-seqs)))
113 (let ((cl-n (min (length cl-x) (length cl-y)))
114 (cl-i -1))
115 (while (< (setq cl-i (1+ cl-i)) cl-n)
116 (push (funcall cl-func
117 (if (consp cl-x) (pop cl-x) (aref cl-x cl-i))
118 (if (consp cl-y) (pop cl-y) (aref cl-y cl-i)))
119 cl-res)))
120 (nreverse cl-res))))
122 (defun map (cl-type cl-func cl-seq &rest cl-rest)
123 "Map a function across one or more sequences, returning a sequence.
124 TYPE is the sequence type to return, FUNC is the function, and SEQS
125 are the argument sequences."
126 (let ((cl-res (apply 'mapcar* cl-func cl-seq cl-rest)))
127 (and cl-type (coerce cl-res cl-type))))
129 (defun maplist (cl-func cl-list &rest cl-rest)
130 "Map FUNC to each sublist of LIST or LISTS.
131 Like `mapcar', except applies to lists and their cdr's rather than to
132 the elements themselves."
133 (if cl-rest
134 (let ((cl-res nil)
135 (cl-args (cons cl-list (copy-sequence cl-rest)))
136 cl-p)
137 (while (not (memq nil cl-args))
138 (push (apply cl-func cl-args) cl-res)
139 (setq cl-p cl-args)
140 (while cl-p (setcar cl-p (cdr (pop cl-p)) )))
141 (nreverse cl-res))
142 (let ((cl-res nil))
143 (while cl-list
144 (push (funcall cl-func cl-list) cl-res)
145 (setq cl-list (cdr cl-list)))
146 (nreverse cl-res))))
148 (defun cl-mapc (cl-func cl-seq &rest cl-rest)
149 "Like `mapcar', but does not accumulate values returned by the function."
150 (if cl-rest
151 (progn (apply 'map nil cl-func cl-seq cl-rest)
152 cl-seq)
153 (mapc cl-func cl-seq)))
155 (defun mapl (cl-func cl-list &rest cl-rest)
156 "Like `maplist', but does not accumulate values returned by the function."
157 (if cl-rest
158 (apply 'maplist cl-func cl-list cl-rest)
159 (let ((cl-p cl-list))
160 (while cl-p (funcall cl-func cl-p) (setq cl-p (cdr cl-p)))))
161 cl-list)
163 (defun mapcan (cl-func cl-seq &rest cl-rest)
164 "Like `mapcar', but nconc's together the values returned by the function."
165 (apply 'nconc (apply 'mapcar* cl-func cl-seq cl-rest)))
167 (defun mapcon (cl-func cl-list &rest cl-rest)
168 "Like `maplist', but nconc's together the values returned by the function."
169 (apply 'nconc (apply 'maplist cl-func cl-list cl-rest)))
171 (defun some (cl-pred cl-seq &rest cl-rest)
172 "Return true if PREDICATE is true of any element of SEQ or SEQs.
173 If so, return the true (non-nil) value returned by PREDICATE."
174 (if (or cl-rest (nlistp cl-seq))
175 (catch 'cl-some
176 (apply 'map nil
177 (function (lambda (&rest cl-x)
178 (let ((cl-res (apply cl-pred cl-x)))
179 (if cl-res (throw 'cl-some cl-res)))))
180 cl-seq cl-rest) nil)
181 (let ((cl-x nil))
182 (while (and cl-seq (not (setq cl-x (funcall cl-pred (pop cl-seq))))))
183 cl-x)))
185 (defun every (cl-pred cl-seq &rest cl-rest)
186 "Return true if PREDICATE is true of every element of SEQ or SEQs."
187 (if (or cl-rest (nlistp cl-seq))
188 (catch 'cl-every
189 (apply 'map nil
190 (function (lambda (&rest cl-x)
191 (or (apply cl-pred cl-x) (throw 'cl-every nil))))
192 cl-seq cl-rest) t)
193 (while (and cl-seq (funcall cl-pred (car cl-seq)))
194 (setq cl-seq (cdr cl-seq)))
195 (null cl-seq)))
197 (defun notany (cl-pred cl-seq &rest cl-rest)
198 "Return true if PREDICATE is false of every element of SEQ or SEQs."
199 (not (apply 'some cl-pred cl-seq cl-rest)))
201 (defun notevery (cl-pred cl-seq &rest cl-rest)
202 "Return true if PREDICATE is false of some element of SEQ or SEQs."
203 (not (apply 'every cl-pred cl-seq cl-rest)))
205 ;;; Support for `loop'.
206 (defun cl-map-keymap (cl-func cl-map)
207 (while (symbolp cl-map) (setq cl-map (symbol-function cl-map)))
208 (if (listp cl-map)
209 (let ((cl-p cl-map))
210 (while (consp (setq cl-p (cdr cl-p)))
211 (cond ((consp (car cl-p))
212 (funcall cl-func (car (car cl-p)) (cdr (car cl-p))))
213 ((or (vectorp (car cl-p)) (char-table-p (car cl-p)))
214 (cl-map-keymap cl-func (car cl-p)))
215 ((eq (car cl-p) 'keymap)
216 (setq cl-p nil)))))
217 (let ((cl-i -1))
218 (while (< (setq cl-i (1+ cl-i)) (length cl-map))
219 (if (aref cl-map cl-i)
220 (funcall cl-func cl-i (aref cl-map cl-i)))))))
222 (defun cl-map-keymap-recursively (cl-func-rec cl-map &optional cl-base)
223 (or cl-base
224 (setq cl-base (copy-sequence [0])))
225 (cl-map-keymap
226 (function
227 (lambda (cl-key cl-bind)
228 (aset cl-base (1- (length cl-base)) cl-key)
229 (if (keymapp cl-bind)
230 (cl-map-keymap-recursively
231 cl-func-rec cl-bind
232 (vconcat cl-base (list 0)))
233 (funcall cl-func-rec cl-base cl-bind))))
234 cl-map))
236 (defun cl-map-intervals (cl-func &optional cl-what cl-prop cl-start cl-end)
237 (or cl-what (setq cl-what (current-buffer)))
238 (if (bufferp cl-what)
239 (let (cl-mark cl-mark2 (cl-next t) cl-next2)
240 (with-current-buffer cl-what
241 (setq cl-mark (copy-marker (or cl-start (point-min))))
242 (setq cl-mark2 (and cl-end (copy-marker cl-end))))
243 (while (and cl-next (or (not cl-mark2) (< cl-mark cl-mark2)))
244 (setq cl-next (if cl-prop (next-single-property-change
245 cl-mark cl-prop cl-what)
246 (next-property-change cl-mark cl-what))
247 cl-next2 (or cl-next (with-current-buffer cl-what
248 (point-max))))
249 (funcall cl-func (prog1 (marker-position cl-mark)
250 (set-marker cl-mark cl-next2))
251 (if cl-mark2 (min cl-next2 cl-mark2) cl-next2)))
252 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil)))
253 (or cl-start (setq cl-start 0))
254 (or cl-end (setq cl-end (length cl-what)))
255 (while (< cl-start cl-end)
256 (let ((cl-next (or (if cl-prop (next-single-property-change
257 cl-start cl-prop cl-what)
258 (next-property-change cl-start cl-what))
259 cl-end)))
260 (funcall cl-func cl-start (min cl-next cl-end))
261 (setq cl-start cl-next)))))
263 (defun cl-map-overlays (cl-func &optional cl-buffer cl-start cl-end cl-arg)
264 (or cl-buffer (setq cl-buffer (current-buffer)))
265 (if (fboundp 'overlay-lists)
267 ;; This is the preferred algorithm, though overlay-lists is undocumented.
268 (let (cl-ovl)
269 (with-current-buffer cl-buffer
270 (setq cl-ovl (overlay-lists))
271 (if cl-start (setq cl-start (copy-marker cl-start)))
272 (if cl-end (setq cl-end (copy-marker cl-end))))
273 (setq cl-ovl (nconc (car cl-ovl) (cdr cl-ovl)))
274 (while (and cl-ovl
275 (or (not (overlay-start (car cl-ovl)))
276 (and cl-end (>= (overlay-start (car cl-ovl)) cl-end))
277 (and cl-start (<= (overlay-end (car cl-ovl)) cl-start))
278 (not (funcall cl-func (car cl-ovl) cl-arg))))
279 (setq cl-ovl (cdr cl-ovl)))
280 (if cl-start (set-marker cl-start nil))
281 (if cl-end (set-marker cl-end nil)))
283 ;; This alternate algorithm fails to find zero-length overlays.
284 (let ((cl-mark (with-current-buffer cl-buffer
285 (copy-marker (or cl-start (point-min)))))
286 (cl-mark2 (and cl-end (with-current-buffer cl-buffer
287 (copy-marker cl-end))))
288 cl-pos cl-ovl)
289 (while (save-excursion
290 (and (setq cl-pos (marker-position cl-mark))
291 (< cl-pos (or cl-mark2 (point-max)))
292 (progn
293 (set-buffer cl-buffer)
294 (setq cl-ovl (overlays-at cl-pos))
295 (set-marker cl-mark (next-overlay-change cl-pos)))))
296 (while (and cl-ovl
297 (or (/= (overlay-start (car cl-ovl)) cl-pos)
298 (not (and (funcall cl-func (car cl-ovl) cl-arg)
299 (set-marker cl-mark nil)))))
300 (setq cl-ovl (cdr cl-ovl))))
301 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil)))))
303 ;;; Support for `setf'.
304 (defun cl-set-frame-visible-p (frame val)
305 (cond ((null val) (make-frame-invisible frame))
306 ((eq val 'icon) (iconify-frame frame))
307 (t (make-frame-visible frame)))
308 val)
310 ;;; Support for `progv'.
311 (defvar cl-progv-save)
312 (defun cl-progv-before (syms values)
313 (while syms
314 (push (if (boundp (car syms))
315 (cons (car syms) (symbol-value (car syms)))
316 (car syms)) cl-progv-save)
317 (if values
318 (set (pop syms) (pop values))
319 (makunbound (pop syms)))))
321 (defun cl-progv-after ()
322 (while cl-progv-save
323 (if (consp (car cl-progv-save))
324 (set (car (car cl-progv-save)) (cdr (car cl-progv-save)))
325 (makunbound (car cl-progv-save)))
326 (pop cl-progv-save)))
329 ;;; Numbers.
331 (defun gcd (&rest args)
332 "Return the greatest common divisor of the arguments."
333 (let ((a (abs (or (pop args) 0))))
334 (while args
335 (let ((b (abs (pop args))))
336 (while (> b 0) (setq b (% a (setq a b))))))
339 (defun lcm (&rest args)
340 "Return the least common multiple of the arguments."
341 (if (memq 0 args)
343 (let ((a (abs (or (pop args) 1))))
344 (while args
345 (let ((b (abs (pop args))))
346 (setq a (* (/ a (gcd a b)) b))))
347 a)))
349 (defun isqrt (a)
350 "Return the integer square root of the argument."
351 (if (and (integerp a) (> a 0))
352 (let ((g (cond ((<= a 100) 10) ((<= a 10000) 100)
353 ((<= a 1000000) 1000) (t a)))
355 (while (< (setq g2 (/ (+ g (/ a g)) 2)) g)
356 (setq g g2))
358 (if (eq a 0) 0 (signal 'arith-error nil))))
360 (defun floor* (x &optional y)
361 "Return a list of the floor of X and the fractional part of X.
362 With two arguments, return floor and remainder of their quotient."
363 (let ((q (floor x y)))
364 (list q (- x (if y (* y q) q)))))
366 (defun ceiling* (x &optional y)
367 "Return a list of the ceiling of X and the fractional part of X.
368 With two arguments, return ceiling and remainder of their quotient."
369 (let ((res (floor* x y)))
370 (if (= (car (cdr res)) 0) res
371 (list (1+ (car res)) (- (car (cdr res)) (or y 1))))))
373 (defun truncate* (x &optional y)
374 "Return a list of the integer part of X and the fractional part of X.
375 With two arguments, return truncation and remainder of their quotient."
376 (if (eq (>= x 0) (or (null y) (>= y 0)))
377 (floor* x y) (ceiling* x y)))
379 (defun round* (x &optional y)
380 "Return a list of X rounded to the nearest integer and the remainder.
381 With two arguments, return rounding and remainder of their quotient."
382 (if y
383 (if (and (integerp x) (integerp y))
384 (let* ((hy (/ y 2))
385 (res (floor* (+ x hy) y)))
386 (if (and (= (car (cdr res)) 0)
387 (= (+ hy hy) y)
388 (/= (% (car res) 2) 0))
389 (list (1- (car res)) hy)
390 (list (car res) (- (car (cdr res)) hy))))
391 (let ((q (round (/ x y))))
392 (list q (- x (* q y)))))
393 (if (integerp x) (list x 0)
394 (let ((q (round x)))
395 (list q (- x q))))))
397 (defun mod* (x y)
398 "The remainder of X divided by Y, with the same sign as Y."
399 (nth 1 (floor* x y)))
401 (defun rem* (x y)
402 "The remainder of X divided by Y, with the same sign as X."
403 (nth 1 (truncate* x y)))
405 (defun signum (a)
406 "Return 1 if A is positive, -1 if negative, 0 if zero."
407 (cond ((> a 0) 1) ((< a 0) -1) (t 0)))
410 ;; Random numbers.
412 (defvar *random-state*)
413 (defun random* (lim &optional state)
414 "Return a random nonnegative number less than LIM, an integer or float.
415 Optional second arg STATE is a random-state object."
416 (or state (setq state *random-state*))
417 ;; Inspired by "ran3" from Numerical Recipes. Additive congruential method.
418 (let ((vec (aref state 3)))
419 (if (integerp vec)
420 (let ((i 0) (j (- 1357335 (% (abs vec) 1357333))) (k 1) ii)
421 (aset state 3 (setq vec (make-vector 55 nil)))
422 (aset vec 0 j)
423 (while (> (setq i (% (+ i 21) 55)) 0)
424 (aset vec i (setq j (prog1 k (setq k (- j k))))))
425 (while (< (setq i (1+ i)) 200) (random* 2 state))))
426 (let* ((i (aset state 1 (% (1+ (aref state 1)) 55)))
427 (j (aset state 2 (% (1+ (aref state 2)) 55)))
428 (n (logand 8388607 (aset vec i (- (aref vec i) (aref vec j))))))
429 (if (integerp lim)
430 (if (<= lim 512) (% n lim)
431 (if (> lim 8388607) (setq n (+ (lsh n 9) (random* 512 state))))
432 (let ((mask 1023))
433 (while (< mask (1- lim)) (setq mask (1+ (+ mask mask))))
434 (if (< (setq n (logand n mask)) lim) n (random* lim state))))
435 (* (/ n '8388608e0) lim)))))
437 (defun make-random-state (&optional state)
438 "Return a copy of random-state STATE, or of `*random-state*' if omitted.
439 If STATE is t, return a new state object seeded from the time of day."
440 (cond ((null state) (make-random-state *random-state*))
441 ((vectorp state) (cl-copy-tree state t))
442 ((integerp state) (vector 'cl-random-state-tag -1 30 state))
443 (t (make-random-state (cl-random-time)))))
445 (defun 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 err
455 (let ((res (funcall func a b))) ; check for IEEE infinity
456 (and (numberp res) (/= res (/ res 2)) res))
457 (arith-error nil)))
459 (defvar most-positive-float)
460 (defvar most-negative-float)
461 (defvar least-positive-float)
462 (defvar least-negative-float)
463 (defvar least-positive-normalized-float)
464 (defvar least-negative-normalized-float)
465 (defvar float-epsilon)
466 (defvar float-negative-epsilon)
468 (defun cl-float-limits ()
469 (or most-positive-float (not (numberp '2e1))
470 (let ((x '2e0) y z)
471 ;; Find maximum exponent (first two loops are optimizations)
472 (while (cl-finite-do '* x x) (setq x (* x x)))
473 (while (cl-finite-do '* x (/ x 2)) (setq x (* x (/ x 2))))
474 (while (cl-finite-do '+ x x) (setq x (+ x x)))
475 (setq z x y (/ x 2))
476 ;; Now fill in 1's in the mantissa.
477 (while (and (cl-finite-do '+ x y) (/= (+ x y) x))
478 (setq x (+ x y) y (/ y 2)))
479 (setq most-positive-float x
480 most-negative-float (- x))
481 ;; Divide down until mantissa starts rounding.
482 (setq x (/ x z) y (/ 16 z) x (* x y))
483 (while (condition-case err (and (= x (* (/ x 2) 2)) (> (/ y 2) 0))
484 (arith-error nil))
485 (setq x (/ x 2) y (/ y 2)))
486 (setq least-positive-normalized-float y
487 least-negative-normalized-float (- y))
488 ;; Divide down until value underflows to zero.
489 (setq x (/ 1 z) y x)
490 (while (condition-case err (> (/ x 2) 0) (arith-error nil))
491 (setq x (/ x 2)))
492 (setq least-positive-float x
493 least-negative-float (- x))
494 (setq x '1e0)
495 (while (/= (+ '1e0 x) '1e0) (setq x (/ x 2)))
496 (setq float-epsilon (* x 2))
497 (setq x '1e0)
498 (while (/= (- '1e0 x) '1e0) (setq x (/ x 2)))
499 (setq float-negative-epsilon (* x 2))))
500 nil)
503 ;;; Sequence functions.
505 (defun subseq (seq start &optional end)
506 "Return the subsequence of SEQ from START to END.
507 If END is omitted, it defaults to the length of the sequence.
508 If START or END is negative, it counts from the end."
509 (if (stringp seq) (substring seq start end)
510 (let (len)
511 (and end (< end 0) (setq end (+ end (setq len (length seq)))))
512 (if (< start 0) (setq start (+ start (or len (setq len (length seq))))))
513 (cond ((listp seq)
514 (if (> start 0) (setq seq (nthcdr start seq)))
515 (if end
516 (let ((res nil))
517 (while (>= (setq end (1- end)) start)
518 (push (pop seq) res))
519 (nreverse res))
520 (copy-sequence seq)))
522 (or end (setq end (or len (length seq))))
523 (let ((res (make-vector (max (- end start) 0) nil))
524 (i 0))
525 (while (< start end)
526 (aset res i (aref seq start))
527 (setq i (1+ i) start (1+ start)))
528 res))))))
530 (defun concatenate (type &rest seqs)
531 "Concatenate, into a sequence of type TYPE, the argument SEQUENCES."
532 (cond ((eq type 'vector) (apply 'vconcat seqs))
533 ((eq type 'string) (apply 'concat seqs))
534 ((eq type 'list) (apply 'append (append seqs '(nil))))
535 (t (error "Not a sequence type name: %s" type))))
538 ;;; List functions.
540 (defun revappend (x y)
541 "Equivalent to (append (reverse X) Y)."
542 (nconc (reverse x) y))
544 (defun nreconc (x y)
545 "Equivalent to (nconc (nreverse X) Y)."
546 (nconc (nreverse x) y))
548 (defun list-length (x)
549 "Return the length of a list. Return nil if list is circular."
550 (let ((n 0) (fast x) (slow x))
551 (while (and (cdr fast) (not (and (eq fast slow) (> n 0))))
552 (setq n (+ n 2) fast (cdr (cdr fast)) slow (cdr slow)))
553 (if fast (if (cdr fast) nil (1+ n)) n)))
555 (defun tailp (sublist list)
556 "Return true if SUBLIST is a tail of LIST."
557 (while (and (consp list) (not (eq sublist list)))
558 (setq list (cdr list)))
559 (if (numberp sublist) (equal sublist list) (eq sublist list)))
561 (defalias 'cl-copy-tree 'copy-tree)
564 ;;; Property lists.
566 (defun get* (sym tag &optional def) ; See compiler macro in cl-macs.el
567 "Return the value of SYMBOL's PROPNAME property, or DEFAULT if none."
568 (or (get sym tag)
569 (and def
570 (let ((plist (symbol-plist sym)))
571 (while (and plist (not (eq (car plist) tag)))
572 (setq plist (cdr (cdr plist))))
573 (if plist (car (cdr plist)) def)))))
575 (defun getf (plist tag &optional def)
576 "Search PROPLIST for property PROPNAME; return its value or DEFAULT.
577 PROPLIST is a list of the sort returned by `symbol-plist'."
578 (setplist '--cl-getf-symbol-- plist)
579 (or (get '--cl-getf-symbol-- tag)
580 ;; Originally we called get* here,
581 ;; but that fails, because get* has a compiler macro
582 ;; definition that uses getf!
583 (when def
584 (while (and plist (not (eq (car plist) tag)))
585 (setq plist (cdr (cdr plist))))
586 (if plist (car (cdr plist)) def))))
588 (defun cl-set-getf (plist tag val)
589 (let ((p plist))
590 (while (and p (not (eq (car p) tag))) (setq p (cdr (cdr p))))
591 (if p (progn (setcar (cdr p) val) plist) (list* tag val plist))))
593 (defun cl-do-remf (plist tag)
594 (let ((p (cdr plist)))
595 (while (and (cdr p) (not (eq (car (cdr p)) tag))) (setq p (cdr (cdr p))))
596 (and (cdr p) (progn (setcdr p (cdr (cdr (cdr p)))) t))))
598 (defun cl-remprop (sym tag)
599 "Remove from SYMBOL's plist the property PROP and its value."
600 (let ((plist (symbol-plist sym)))
601 (if (and plist (eq tag (car plist)))
602 (progn (setplist sym (cdr (cdr plist))) t)
603 (cl-do-remf plist tag))))
604 (defalias 'remprop 'cl-remprop)
608 ;;; Hash tables.
609 ;; This is just kept for compatibility with code byte-compiled by Emacs-20.
611 ;; No idea if this might still be needed.
612 (defun cl-not-hash-table (x &optional y &rest z)
613 (signal 'wrong-type-argument (list 'cl-hash-table-p (or y x))))
615 (defvar cl-builtin-gethash (symbol-function 'gethash))
616 (defvar cl-builtin-remhash (symbol-function 'remhash))
617 (defvar cl-builtin-clrhash (symbol-function 'clrhash))
618 (defvar cl-builtin-maphash (symbol-function 'maphash))
620 (defalias 'cl-gethash 'gethash)
621 (defalias 'cl-puthash 'puthash)
622 (defalias 'cl-remhash 'remhash)
623 (defalias 'cl-clrhash 'clrhash)
624 (defalias 'cl-maphash 'maphash)
625 ;; These three actually didn't exist in Emacs-20.
626 (defalias 'cl-make-hash-table 'make-hash-table)
627 (defalias 'cl-hash-table-p 'hash-table-p)
628 (defalias 'cl-hash-table-count 'hash-table-count)
630 ;;; Some debugging aids.
632 (defun cl-prettyprint (form)
633 "Insert a pretty-printed rendition of a Lisp FORM in current buffer."
634 (let ((pt (point)) last)
635 (insert "\n" (prin1-to-string form) "\n")
636 (setq last (point))
637 (goto-char (1+ pt))
638 (while (search-forward "(quote " last t)
639 (delete-backward-char 7)
640 (insert "'")
641 (forward-sexp)
642 (delete-char 1))
643 (goto-char (1+ pt))
644 (cl-do-prettyprint)))
646 (defun cl-do-prettyprint ()
647 (skip-chars-forward " ")
648 (if (looking-at "(")
649 (let ((skip (or (looking-at "((") (looking-at "(prog")
650 (looking-at "(unwind-protect ")
651 (looking-at "(function (")
652 (looking-at "(cl-block-wrapper ")))
653 (two (or (looking-at "(defun ") (looking-at "(defmacro ")))
654 (let (or (looking-at "(let\\*? ") (looking-at "(while ")))
655 (set (looking-at "(p?set[qf] ")))
656 (if (or skip let
657 (progn
658 (forward-sexp)
659 (and (>= (current-column) 78) (progn (backward-sexp) t))))
660 (let ((nl t))
661 (forward-char 1)
662 (cl-do-prettyprint)
663 (or skip (looking-at ")") (cl-do-prettyprint))
664 (or (not two) (looking-at ")") (cl-do-prettyprint))
665 (while (not (looking-at ")"))
666 (if set (setq nl (not nl)))
667 (if nl (insert "\n"))
668 (lisp-indent-line)
669 (cl-do-prettyprint))
670 (forward-char 1))))
671 (forward-sexp)))
673 (defvar cl-macroexpand-cmacs nil)
674 (defvar cl-closure-vars nil)
676 (defun cl-macroexpand-all (form &optional env)
677 "Expand all macro calls through a Lisp FORM.
678 This also does some trivial optimizations to make the form prettier."
679 (while (or (not (eq form (setq form (macroexpand form env))))
680 (and cl-macroexpand-cmacs
681 (not (eq form (setq form (compiler-macroexpand form)))))))
682 (cond ((not (consp form)) form)
683 ((memq (car form) '(let let*))
684 (if (null (nth 1 form))
685 (cl-macroexpand-all (cons 'progn (cddr form)) env)
686 (let ((letf nil) (res nil) (lets (cadr form)))
687 (while lets
688 (push (if (consp (car lets))
689 (let ((exp (cl-macroexpand-all (caar lets) env)))
690 (or (symbolp exp) (setq letf t))
691 (cons exp (cl-macroexpand-body (cdar lets) env)))
692 (let ((exp (cl-macroexpand-all (car lets) env)))
693 (if (symbolp exp) exp
694 (setq letf t) (list exp nil)))) res)
695 (setq lets (cdr lets)))
696 (list* (if letf (if (eq (car form) 'let) 'letf 'letf*) (car form))
697 (nreverse res) (cl-macroexpand-body (cddr form) env)))))
698 ((eq (car form) 'cond)
699 (cons (car form)
700 (mapcar (function (lambda (x) (cl-macroexpand-body x env)))
701 (cdr form))))
702 ((eq (car form) 'condition-case)
703 (list* (car form) (nth 1 form) (cl-macroexpand-all (nth 2 form) env)
704 (mapcar (function
705 (lambda (x)
706 (cons (car x) (cl-macroexpand-body (cdr x) env))))
707 (cdddr form))))
708 ((memq (car form) '(quote function))
709 (if (eq (car-safe (nth 1 form)) 'lambda)
710 (let ((body (cl-macroexpand-body (cddadr form) env)))
711 (if (and cl-closure-vars (eq (car form) 'function)
712 (cl-expr-contains-any body cl-closure-vars))
713 (let* ((new (mapcar 'gensym cl-closure-vars))
714 (sub (pairlis cl-closure-vars new)) (decls nil))
715 (while (or (stringp (car body))
716 (eq (car-safe (car body)) 'interactive))
717 (push (list 'quote (pop body)) decls))
718 (put (car (last cl-closure-vars)) 'used t)
719 (append
720 (list 'list '(quote lambda) '(quote (&rest --cl-rest--)))
721 (sublis sub (nreverse decls))
722 (list
723 (list* 'list '(quote apply)
724 (list 'list '(quote quote)
725 (list 'function
726 (list* 'lambda
727 (append new (cadadr form))
728 (sublis sub body))))
729 (nconc (mapcar (function
730 (lambda (x)
731 (list 'list '(quote quote) x)))
732 cl-closure-vars)
733 '((quote --cl-rest--)))))))
734 (list (car form) (list* 'lambda (cadadr form) body))))
735 (let ((found (assq (cadr form) env)))
736 (if (eq (cadr (caddr found)) 'cl-labels-args)
737 (cl-macroexpand-all (cadr (caddr (cadddr found))) env)
738 form))))
739 ((memq (car form) '(defun defmacro))
740 (list* (car form) (nth 1 form) (cl-macroexpand-body (cddr form) env)))
741 ((and (eq (car form) 'progn) (not (cddr form)))
742 (cl-macroexpand-all (nth 1 form) env))
743 ((eq (car form) 'setq)
744 (let* ((args (cl-macroexpand-body (cdr form) env)) (p args))
745 (while (and p (symbolp (car p))) (setq p (cddr p)))
746 (if p (cl-macroexpand-all (cons 'setf args)) (cons 'setq args))))
747 (t (cons (car form) (cl-macroexpand-body (cdr form) env)))))
749 (defun cl-macroexpand-body (body &optional env)
750 (mapcar (function (lambda (x) (cl-macroexpand-all x env))) body))
752 (defun cl-prettyexpand (form &optional full)
753 (message "Expanding...")
754 (let ((cl-macroexpand-cmacs full) (cl-compiling-file full)
755 (byte-compile-macro-environment nil))
756 (setq form (cl-macroexpand-all form
757 (and (not full) '((block) (eval-when)))))
758 (message "Formatting...")
759 (prog1 (cl-prettyprint form)
760 (message ""))))
764 (run-hooks 'cl-extra-load-hook)
766 ;;; cl-extra.el ends here