comparison lisp/cl/cl-extra.el @ 0:376386a54a3c r19-14

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