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