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