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