Mercurial > emacs
annotate lisp/emacs-lisp/cl-extra.el @ 26340:f1ae5b57d9db
Changed the mail addess of the maintainer.
author | Francesco Potortì <pot@gnu.org> |
---|---|
date | Wed, 03 Nov 1999 23:13:10 +0000 |
parents | 3bfd67af61d0 |
children | b153112d5bd0 |
rev | line source |
---|---|
16057
a74507d555ba
Turn on byte-compile-dynamic.
Richard M. Stallman <rms@gnu.org>
parents:
15029
diff
changeset
|
1 ;;; cl-extra.el --- Common Lisp features, part 2 -*-byte-compile-dynamic: t;-*- |
4355 | 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 | |
12244 | 13 ;; the Free Software Foundation; either version 2, or (at your option) |
4355 | 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 | |
14169 | 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. | |
4355 | 25 |
7942 | 26 ;;; Commentary: |
4355 | 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 | |
7942 | 45 ;;; Code: |
4355 | 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. | |
65 TYPE 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. | |
81 This is like `equal', except that it accepts numerically equal | |
82 numbers of different types (float vs. integer), and also compares | |
83 strings case-insensitively." | |
84 (cond ((eq x y) t) | |
85 ((stringp x) | |
86 (and (stringp y) (= (length x) (length y)) | |
14794
c3a2cabb73ef
(equalp): Use string-equal on strings.
Erik Naggum <erik@naggum.no>
parents:
14762
diff
changeset
|
87 (or (string-equal x y) |
c3a2cabb73ef
(equalp): Use string-equal on strings.
Erik Naggum <erik@naggum.no>
parents:
14762
diff
changeset
|
88 (string-equal (downcase x) (downcase y))))) ; lazy but simple! |
4355 | 89 ((numberp x) |
90 (and (numberp y) (= x y))) | |
91 ((consp x) | |
14762
624d5547a6d6
(equalp): Correctly compare last elt of two lists.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
92 (while (and (consp x) (consp y) (equalp (car x) (car y))) |
624d5547a6d6
(equalp): Correctly compare last elt of two lists.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
93 (setq x (cdr x) y (cdr y))) |
4355 | 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 (mapcar cl-func cl-seq)) | |
169 cl-seq) | |
170 | |
171 (defun mapl (cl-func cl-list &rest cl-rest) | |
172 "Like `maplist', but does not accumulate values returned by the function." | |
173 (if cl-rest | |
174 (apply 'maplist cl-func cl-list cl-rest) | |
175 (let ((cl-p cl-list)) | |
176 (while cl-p (funcall cl-func cl-p) (setq cl-p (cdr cl-p))))) | |
177 cl-list) | |
178 | |
179 (defun mapcan (cl-func cl-seq &rest cl-rest) | |
180 "Like `mapcar', but nconc's together the values returned by the function." | |
181 (apply 'nconc (apply 'mapcar* cl-func cl-seq cl-rest))) | |
182 | |
183 (defun mapcon (cl-func cl-list &rest cl-rest) | |
184 "Like `maplist', but nconc's together the values returned by the function." | |
185 (apply 'nconc (apply 'maplist cl-func cl-list cl-rest))) | |
186 | |
187 (defun some (cl-pred cl-seq &rest cl-rest) | |
188 "Return true if PREDICATE is true of any element of SEQ or SEQs. | |
189 If so, return the true (non-nil) value returned by PREDICATE." | |
190 (if (or cl-rest (nlistp cl-seq)) | |
191 (catch 'cl-some | |
192 (apply 'map nil | |
193 (function (lambda (&rest cl-x) | |
194 (let ((cl-res (apply cl-pred cl-x))) | |
195 (if cl-res (throw 'cl-some cl-res))))) | |
196 cl-seq cl-rest) nil) | |
197 (let ((cl-x nil)) | |
198 (while (and cl-seq (not (setq cl-x (funcall cl-pred (cl-pop cl-seq)))))) | |
199 cl-x))) | |
200 | |
201 (defun every (cl-pred cl-seq &rest cl-rest) | |
202 "Return true if PREDICATE is true of every element of SEQ or SEQs." | |
203 (if (or cl-rest (nlistp cl-seq)) | |
204 (catch 'cl-every | |
205 (apply 'map nil | |
206 (function (lambda (&rest cl-x) | |
207 (or (apply cl-pred cl-x) (throw 'cl-every nil)))) | |
208 cl-seq cl-rest) t) | |
209 (while (and cl-seq (funcall cl-pred (car cl-seq))) | |
210 (setq cl-seq (cdr cl-seq))) | |
211 (null cl-seq))) | |
212 | |
213 (defun notany (cl-pred cl-seq &rest cl-rest) | |
214 "Return true if PREDICATE is false of every element of SEQ or SEQs." | |
215 (not (apply 'some cl-pred cl-seq cl-rest))) | |
216 | |
217 (defun notevery (cl-pred cl-seq &rest cl-rest) | |
218 "Return true if PREDICATE is false of some element of SEQ or SEQs." | |
219 (not (apply 'every cl-pred cl-seq cl-rest))) | |
220 | |
221 ;;; Support for `loop'. | |
222 (defun cl-map-keymap (cl-func cl-map) | |
223 (while (symbolp cl-map) (setq cl-map (symbol-function cl-map))) | |
224 (if (eq cl-emacs-type 'lucid) (funcall 'map-keymap cl-func cl-map) | |
225 (if (listp cl-map) | |
226 (let ((cl-p cl-map)) | |
227 (while (consp (setq cl-p (cdr cl-p))) | |
228 (cond ((consp (car cl-p)) | |
229 (funcall cl-func (car (car cl-p)) (cdr (car cl-p)))) | |
230 ((vectorp (car cl-p)) | |
231 (cl-map-keymap cl-func (car cl-p))) | |
232 ((eq (car cl-p) 'keymap) | |
233 (setq cl-p nil))))) | |
234 (let ((cl-i -1)) | |
235 (while (< (setq cl-i (1+ cl-i)) (length cl-map)) | |
236 (if (aref cl-map cl-i) | |
237 (funcall cl-func cl-i (aref cl-map cl-i)))))))) | |
238 | |
239 (defun cl-map-keymap-recursively (cl-func-rec cl-map &optional cl-base) | |
240 (or cl-base | |
241 (setq cl-base (copy-sequence (if (eq cl-emacs-type 18) "0" [0])))) | |
242 (cl-map-keymap | |
243 (function | |
244 (lambda (cl-key cl-bind) | |
245 (aset cl-base (1- (length cl-base)) cl-key) | |
246 (if (keymapp cl-bind) | |
247 (cl-map-keymap-recursively | |
248 cl-func-rec cl-bind | |
249 (funcall (if (eq cl-emacs-type 18) 'concat 'vconcat) | |
250 cl-base (list 0))) | |
251 (funcall cl-func-rec cl-base cl-bind)))) | |
252 cl-map)) | |
253 | |
254 (defun cl-map-intervals (cl-func &optional cl-what cl-prop cl-start cl-end) | |
255 (or cl-what (setq cl-what (current-buffer))) | |
256 (if (bufferp cl-what) | |
257 (let (cl-mark cl-mark2 (cl-next t) cl-next2) | |
258 (save-excursion | |
259 (set-buffer cl-what) | |
260 (setq cl-mark (copy-marker (or cl-start (point-min)))) | |
261 (setq cl-mark2 (and cl-end (copy-marker cl-end)))) | |
262 (while (and cl-next (or (not cl-mark2) (< cl-mark cl-mark2))) | |
263 (setq cl-next (and (fboundp 'next-property-change) | |
264 (if cl-prop (next-single-property-change | |
265 cl-mark cl-prop cl-what) | |
266 (next-property-change cl-mark cl-what))) | |
267 cl-next2 (or cl-next (save-excursion | |
268 (set-buffer cl-what) (point-max)))) | |
269 (funcall cl-func (prog1 (marker-position cl-mark) | |
270 (set-marker cl-mark cl-next2)) | |
271 (if cl-mark2 (min cl-next2 cl-mark2) cl-next2))) | |
272 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil))) | |
273 (or cl-start (setq cl-start 0)) | |
274 (or cl-end (setq cl-end (length cl-what))) | |
275 (while (< cl-start cl-end) | |
276 (let ((cl-next (or (and (fboundp 'next-property-change) | |
277 (if cl-prop (next-single-property-change | |
278 cl-start cl-prop cl-what) | |
279 (next-property-change cl-start cl-what))) | |
280 cl-end))) | |
281 (funcall cl-func cl-start (min cl-next cl-end)) | |
282 (setq cl-start cl-next))))) | |
283 | |
284 (defun cl-map-overlays (cl-func &optional cl-buffer cl-start cl-end cl-arg) | |
285 (or cl-buffer (setq cl-buffer (current-buffer))) | |
286 (if (fboundp 'overlay-lists) | |
287 | |
288 ;; This is the preferred algorithm, though overlay-lists is undocumented. | |
289 (let (cl-ovl) | |
290 (save-excursion | |
291 (set-buffer cl-buffer) | |
292 (setq cl-ovl (overlay-lists)) | |
293 (if cl-start (setq cl-start (copy-marker cl-start))) | |
294 (if cl-end (setq cl-end (copy-marker cl-end)))) | |
295 (setq cl-ovl (nconc (car cl-ovl) (cdr cl-ovl))) | |
296 (while (and cl-ovl | |
297 (or (not (overlay-start (car cl-ovl))) | |
298 (and cl-end (>= (overlay-start (car cl-ovl)) cl-end)) | |
299 (and cl-start (<= (overlay-end (car cl-ovl)) cl-start)) | |
300 (not (funcall cl-func (car cl-ovl) cl-arg)))) | |
301 (setq cl-ovl (cdr cl-ovl))) | |
302 (if cl-start (set-marker cl-start nil)) | |
303 (if cl-end (set-marker cl-end nil))) | |
304 | |
305 ;; This alternate algorithm fails to find zero-length overlays. | |
306 (let ((cl-mark (save-excursion (set-buffer cl-buffer) | |
307 (copy-marker (or cl-start (point-min))))) | |
308 (cl-mark2 (and cl-end (save-excursion (set-buffer cl-buffer) | |
309 (copy-marker cl-end)))) | |
310 cl-pos cl-ovl) | |
311 (while (save-excursion | |
312 (and (setq cl-pos (marker-position cl-mark)) | |
313 (< cl-pos (or cl-mark2 (point-max))) | |
314 (progn | |
315 (set-buffer cl-buffer) | |
316 (setq cl-ovl (overlays-at cl-pos)) | |
317 (set-marker cl-mark (next-overlay-change cl-pos))))) | |
318 (while (and cl-ovl | |
319 (or (/= (overlay-start (car cl-ovl)) cl-pos) | |
320 (not (and (funcall cl-func (car cl-ovl) cl-arg) | |
321 (set-marker cl-mark nil))))) | |
322 (setq cl-ovl (cdr cl-ovl)))) | |
323 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil))))) | |
324 | |
325 ;;; Support for `setf'. | |
326 (defun cl-set-frame-visible-p (frame val) | |
327 (cond ((null val) (make-frame-invisible frame)) | |
328 ((eq val 'icon) (iconify-frame frame)) | |
329 (t (make-frame-visible frame))) | |
330 val) | |
331 | |
332 ;;; Support for `progv'. | |
333 (defvar cl-progv-save) | |
334 (defun cl-progv-before (syms values) | |
335 (while syms | |
336 (cl-push (if (boundp (car syms)) | |
337 (cons (car syms) (symbol-value (car syms))) | |
338 (car syms)) cl-progv-save) | |
339 (if values | |
340 (set (cl-pop syms) (cl-pop values)) | |
341 (makunbound (cl-pop syms))))) | |
342 | |
343 (defun cl-progv-after () | |
344 (while cl-progv-save | |
345 (if (consp (car cl-progv-save)) | |
346 (set (car (car cl-progv-save)) (cdr (car cl-progv-save))) | |
347 (makunbound (car cl-progv-save))) | |
348 (cl-pop cl-progv-save))) | |
349 | |
350 | |
351 ;;; Numbers. | |
352 | |
353 (defun gcd (&rest args) | |
354 "Return the greatest common divisor of the arguments." | |
355 (let ((a (abs (or (cl-pop args) 0)))) | |
356 (while args | |
357 (let ((b (abs (cl-pop args)))) | |
358 (while (> b 0) (setq b (% a (setq a b)))))) | |
359 a)) | |
360 | |
361 (defun lcm (&rest args) | |
362 "Return the least common multiple of the arguments." | |
363 (if (memq 0 args) | |
364 0 | |
365 (let ((a (abs (or (cl-pop args) 1)))) | |
366 (while args | |
367 (let ((b (abs (cl-pop args)))) | |
368 (setq a (* (/ a (gcd a b)) b)))) | |
369 a))) | |
370 | |
371 (defun isqrt (a) | |
372 "Return the integer square root of the argument." | |
373 (if (and (integerp a) (> a 0)) | |
15029
ba44a899c055
(isqrt): Support expanded range of Lisp integers.
Richard M. Stallman <rms@gnu.org>
parents:
14794
diff
changeset
|
374 (let ((g (cond ((<= a 100) 10) ((<= a 10000) 100) |
ba44a899c055
(isqrt): Support expanded range of Lisp integers.
Richard M. Stallman <rms@gnu.org>
parents:
14794
diff
changeset
|
375 ((<= a 1000000) 1000) (t a))) |
4355 | 376 g2) |
377 (while (< (setq g2 (/ (+ g (/ a g)) 2)) g) | |
378 (setq g g2)) | |
379 g) | |
380 (if (eq a 0) 0 (signal 'arith-error nil)))) | |
381 | |
382 (defun cl-expt (x y) | |
383 "Return X raised to the power of Y. Works only for integer arguments." | |
15029
ba44a899c055
(isqrt): Support expanded range of Lisp integers.
Richard M. Stallman <rms@gnu.org>
parents:
14794
diff
changeset
|
384 (if (<= y 0) (if (= y 0) 1 (if (memq x '(-1 1)) (cl-expt x (- y)) 0)) |
4355 | 385 (* (if (= (% y 2) 0) 1 x) (cl-expt (* x x) (/ y 2))))) |
386 (or (and (fboundp 'expt) (subrp (symbol-function 'expt))) | |
387 (defalias 'expt 'cl-expt)) | |
388 | |
389 (defun floor* (x &optional y) | |
390 "Return a list of the floor of X and the fractional part of X. | |
391 With two arguments, return floor and remainder of their quotient." | |
4517
45e3868140f1
(floor*): Use `floor' instead of doing most the work ourselves.
Paul Eggert <eggert@twinsun.com>
parents:
4355
diff
changeset
|
392 (let ((q (floor x y))) |
45e3868140f1
(floor*): Use `floor' instead of doing most the work ourselves.
Paul Eggert <eggert@twinsun.com>
parents:
4355
diff
changeset
|
393 (list q (- x (if y (* y q) q))))) |
4355 | 394 |
395 (defun ceiling* (x &optional y) | |
396 "Return a list of the ceiling of X and the fractional part of X. | |
397 With two arguments, return ceiling and remainder of their quotient." | |
398 (let ((res (floor* x y))) | |
399 (if (= (car (cdr res)) 0) res | |
400 (list (1+ (car res)) (- (car (cdr res)) (or y 1)))))) | |
401 | |
402 (defun truncate* (x &optional y) | |
403 "Return a list of the integer part of X and the fractional part of X. | |
404 With two arguments, return truncation and remainder of their quotient." | |
405 (if (eq (>= x 0) (or (null y) (>= y 0))) | |
406 (floor* x y) (ceiling* x y))) | |
407 | |
408 (defun round* (x &optional y) | |
409 "Return a list of X rounded to the nearest integer and the remainder. | |
410 With two arguments, return rounding and remainder of their quotient." | |
411 (if y | |
412 (if (and (integerp x) (integerp y)) | |
413 (let* ((hy (/ y 2)) | |
414 (res (floor* (+ x hy) y))) | |
415 (if (and (= (car (cdr res)) 0) | |
416 (= (+ hy hy) y) | |
417 (/= (% (car res) 2) 0)) | |
418 (list (1- (car res)) hy) | |
419 (list (car res) (- (car (cdr res)) hy)))) | |
420 (let ((q (round (/ x y)))) | |
421 (list q (- x (* q y))))) | |
422 (if (integerp x) (list x 0) | |
423 (let ((q (round x))) | |
424 (list q (- x q)))))) | |
425 | |
426 (defun mod* (x y) | |
427 "The remainder of X divided by Y, with the same sign as Y." | |
428 (nth 1 (floor* x y))) | |
429 | |
430 (defun rem* (x y) | |
431 "The remainder of X divided by Y, with the same sign as X." | |
432 (nth 1 (truncate* x y))) | |
433 | |
434 (defun signum (a) | |
435 "Return 1 if A is positive, -1 if negative, 0 if zero." | |
436 (cond ((> a 0) 1) ((< a 0) -1) (t 0))) | |
437 | |
438 | |
439 ;; Random numbers. | |
440 | |
441 (defvar *random-state*) | |
442 (defun random* (lim &optional state) | |
443 "Return a random nonnegative number less than LIM, an integer or float. | |
444 Optional second arg STATE is a random-state object." | |
445 (or state (setq state *random-state*)) | |
446 ;; Inspired by "ran3" from Numerical Recipes. Additive congruential method. | |
447 (let ((vec (aref state 3))) | |
448 (if (integerp vec) | |
449 (let ((i 0) (j (- 1357335 (% (abs vec) 1357333))) (k 1) ii) | |
450 (aset state 3 (setq vec (make-vector 55 nil))) | |
451 (aset vec 0 j) | |
452 (while (> (setq i (% (+ i 21) 55)) 0) | |
453 (aset vec i (setq j (prog1 k (setq k (- j k)))))) | |
454 (while (< (setq i (1+ i)) 200) (random* 2 state)))) | |
455 (let* ((i (aset state 1 (% (1+ (aref state 1)) 55))) | |
456 (j (aset state 2 (% (1+ (aref state 2)) 55))) | |
457 (n (logand 8388607 (aset vec i (- (aref vec i) (aref vec j)))))) | |
458 (if (integerp lim) | |
459 (if (<= lim 512) (% n lim) | |
460 (if (> lim 8388607) (setq n (+ (lsh n 9) (random* 512 state)))) | |
461 (let ((mask 1023)) | |
462 (while (< mask (1- lim)) (setq mask (1+ (+ mask mask)))) | |
463 (if (< (setq n (logand n mask)) lim) n (random* lim state)))) | |
464 (* (/ n '8388608e0) lim))))) | |
465 | |
466 (defun make-random-state (&optional state) | |
467 "Return a copy of random-state STATE, or of `*random-state*' if omitted. | |
468 If STATE is t, return a new state object seeded from the time of day." | |
469 (cond ((null state) (make-random-state *random-state*)) | |
470 ((vectorp state) (cl-copy-tree state t)) | |
471 ((integerp state) (vector 'cl-random-state-tag -1 30 state)) | |
472 (t (make-random-state (cl-random-time))))) | |
473 | |
474 (defun random-state-p (object) | |
475 "Return t if OBJECT is a random-state object." | |
476 (and (vectorp object) (= (length object) 4) | |
477 (eq (aref object 0) 'cl-random-state-tag))) | |
478 | |
479 | |
480 ;; Implementation limits. | |
481 | |
482 (defun cl-finite-do (func a b) | |
483 (condition-case err | |
484 (let ((res (funcall func a b))) ; check for IEEE infinity | |
485 (and (numberp res) (/= res (/ res 2)) res)) | |
486 (arith-error nil))) | |
487 | |
488 (defvar most-positive-float) | |
489 (defvar most-negative-float) | |
490 (defvar least-positive-float) | |
491 (defvar least-negative-float) | |
492 (defvar least-positive-normalized-float) | |
493 (defvar least-negative-normalized-float) | |
494 (defvar float-epsilon) | |
495 (defvar float-negative-epsilon) | |
496 | |
497 (defun cl-float-limits () | |
498 (or most-positive-float (not (numberp '2e1)) | |
499 (let ((x '2e0) y z) | |
500 ;; Find maximum exponent (first two loops are optimizations) | |
501 (while (cl-finite-do '* x x) (setq x (* x x))) | |
502 (while (cl-finite-do '* x (/ x 2)) (setq x (* x (/ x 2)))) | |
503 (while (cl-finite-do '+ x x) (setq x (+ x x))) | |
504 (setq z x y (/ x 2)) | |
505 ;; Now fill in 1's in the mantissa. | |
506 (while (and (cl-finite-do '+ x y) (/= (+ x y) x)) | |
507 (setq x (+ x y) y (/ y 2))) | |
508 (setq most-positive-float x | |
509 most-negative-float (- x)) | |
510 ;; Divide down until mantissa starts rounding. | |
511 (setq x (/ x z) y (/ 16 z) x (* x y)) | |
512 (while (condition-case err (and (= x (* (/ x 2) 2)) (> (/ y 2) 0)) | |
513 (arith-error nil)) | |
514 (setq x (/ x 2) y (/ y 2))) | |
515 (setq least-positive-normalized-float y | |
516 least-negative-normalized-float (- y)) | |
517 ;; Divide down until value underflows to zero. | |
518 (setq x (/ 1 z) y x) | |
519 (while (condition-case err (> (/ x 2) 0) (arith-error nil)) | |
520 (setq x (/ x 2))) | |
521 (setq least-positive-float x | |
522 least-negative-float (- x)) | |
523 (setq x '1e0) | |
524 (while (/= (+ '1e0 x) '1e0) (setq x (/ x 2))) | |
525 (setq float-epsilon (* x 2)) | |
526 (setq x '1e0) | |
527 (while (/= (- '1e0 x) '1e0) (setq x (/ x 2))) | |
528 (setq float-negative-epsilon (* x 2)))) | |
529 nil) | |
530 | |
531 | |
532 ;;; Sequence functions. | |
533 | |
534 (defun subseq (seq start &optional end) | |
535 "Return the subsequence of SEQ from START to END. | |
536 If END is omitted, it defaults to the length of the sequence. | |
537 If START or END is negative, it counts from the end." | |
538 (if (stringp seq) (substring seq start end) | |
539 (let (len) | |
540 (and end (< end 0) (setq end (+ end (setq len (length seq))))) | |
541 (if (< start 0) (setq start (+ start (or len (setq len (length seq)))))) | |
542 (cond ((listp seq) | |
543 (if (> start 0) (setq seq (nthcdr start seq))) | |
544 (if end | |
545 (let ((res nil)) | |
546 (while (>= (setq end (1- end)) start) | |
547 (cl-push (cl-pop seq) res)) | |
548 (nreverse res)) | |
549 (copy-sequence seq))) | |
550 (t | |
551 (or end (setq end (or len (length seq)))) | |
552 (let ((res (make-vector (max (- end start) 0) nil)) | |
553 (i 0)) | |
554 (while (< start end) | |
555 (aset res i (aref seq start)) | |
556 (setq i (1+ i) start (1+ start))) | |
557 res)))))) | |
558 | |
559 (defun concatenate (type &rest seqs) | |
560 "Concatenate, into a sequence of type TYPE, the argument SEQUENCES." | |
561 (cond ((eq type 'vector) (apply 'vconcat seqs)) | |
562 ((eq type 'string) (apply 'concat seqs)) | |
563 ((eq type 'list) (apply 'append (append seqs '(nil)))) | |
564 (t (error "Not a sequence type name: %s" type)))) | |
565 | |
566 | |
567 ;;; List functions. | |
568 | |
569 (defun revappend (x y) | |
570 "Equivalent to (append (reverse X) Y)." | |
571 (nconc (reverse x) y)) | |
572 | |
573 (defun nreconc (x y) | |
574 "Equivalent to (nconc (nreverse X) Y)." | |
575 (nconc (nreverse x) y)) | |
576 | |
577 (defun list-length (x) | |
578 "Return the length of a list. Return nil if list is circular." | |
579 (let ((n 0) (fast x) (slow x)) | |
580 (while (and (cdr fast) (not (and (eq fast slow) (> n 0)))) | |
581 (setq n (+ n 2) fast (cdr (cdr fast)) slow (cdr slow))) | |
582 (if fast (if (cdr fast) nil (1+ n)) n))) | |
583 | |
584 (defun tailp (sublist list) | |
585 "Return true if SUBLIST is a tail of LIST." | |
586 (while (and (consp list) (not (eq sublist list))) | |
587 (setq list (cdr list))) | |
588 (if (numberp sublist) (equal sublist list) (eq sublist list))) | |
589 | |
590 (defun cl-copy-tree (tree &optional vecp) | |
591 "Make a copy of TREE. | |
592 If TREE is a cons cell, this recursively copies both its car and its cdr. | |
13970 | 593 Contrast to copy-sequence, which copies only along the cdrs. With second |
4355 | 594 argument VECP, this copies vectors as well as conses." |
595 (if (consp tree) | |
596 (let ((p (setq tree (copy-list tree)))) | |
597 (while (consp p) | |
598 (if (or (consp (car p)) (and vecp (vectorp (car p)))) | |
599 (setcar p (cl-copy-tree (car p) vecp))) | |
600 (or (listp (cdr p)) (setcdr p (cl-copy-tree (cdr p) vecp))) | |
601 (cl-pop p))) | |
602 (if (and vecp (vectorp tree)) | |
603 (let ((i (length (setq tree (copy-sequence tree))))) | |
604 (while (>= (setq i (1- i)) 0) | |
605 (aset tree i (cl-copy-tree (aref tree i) vecp)))))) | |
606 tree) | |
607 (or (and (fboundp 'copy-tree) (subrp (symbol-function 'copy-tree))) | |
608 (defalias 'copy-tree 'cl-copy-tree)) | |
609 | |
610 | |
611 ;;; Property lists. | |
612 | |
613 (defun get* (sym tag &optional def) ; See compiler macro in cl-macs.el | |
614 "Return the value of SYMBOL's PROPNAME property, or DEFAULT if none." | |
615 (or (get sym tag) | |
616 (and def | |
617 (let ((plist (symbol-plist sym))) | |
618 (while (and plist (not (eq (car plist) tag))) | |
619 (setq plist (cdr (cdr plist)))) | |
620 (if plist (car (cdr plist)) def))))) | |
621 | |
622 (defun getf (plist tag &optional def) | |
623 "Search PROPLIST for property PROPNAME; return its value or DEFAULT. | |
624 PROPLIST is a list of the sort returned by `symbol-plist'." | |
625 (setplist '--cl-getf-symbol-- plist) | |
626 (or (get '--cl-getf-symbol-- tag) | |
24826 | 627 ;; Originally we called get* here, |
628 ;; but that fails, because get* has a compiler macro | |
629 ;; definition that uses getf! | |
630 (when def | |
631 (while (and plist (not (eq (car plist) tag))) | |
632 (setq plist (cdr (cdr plist)))) | |
633 (if plist (car (cdr plist)) def)))) | |
4355 | 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 | |
24988
3bfd67af61d0
(cl-make-hash-table): Renamed from make-hash-table.
Gerd Moellmann <gerd@gnu.org>
parents:
24826
diff
changeset
|
658 (defun cl-make-hash-table (&rest cl-keys) |
4355 | 659 "Make an empty Common Lisp-style hash-table. |
660 If :test is `eq', this can use Lucid Emacs built-in hash-tables. | |
661 In non-Lucid Emacs, or with non-`eq' test, this internally uses a-lists. | |
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))) | |
666 (if (and (eq cl-test 'eq) (fboundp 'make-hashtable)) | |
667 (funcall 'make-hashtable cl-size) | |
668 (list 'cl-hash-table-tag cl-test | |
669 (if (> cl-size 1) (make-vector cl-size 0) | |
670 (let ((sym (make-symbol "--hashsym--"))) (set sym nil) sym)) | |
671 0)))) | |
672 | |
673 (defvar cl-lucid-hash-tag | |
674 (if (and (fboundp 'make-hashtable) (vectorp (make-hashtable 1))) | |
675 (aref (make-hashtable 1) 0) (make-symbol "--cl-hash-tag--"))) | |
676 | |
24988
3bfd67af61d0
(cl-make-hash-table): Renamed from make-hash-table.
Gerd Moellmann <gerd@gnu.org>
parents:
24826
diff
changeset
|
677 (defun cl-hash-table-p (x) |
4355 | 678 "Return t if OBJECT is a hash table." |
679 (or (eq (car-safe x) 'cl-hash-table-tag) | |
680 (and (vectorp x) (= (length x) 4) (eq (aref x 0) cl-lucid-hash-tag)) | |
681 (and (fboundp 'hashtablep) (funcall 'hashtablep x)))) | |
682 | |
683 (defun cl-not-hash-table (x &optional y &rest z) | |
684 (signal 'wrong-type-argument (list 'hash-table-p (or y x)))) | |
685 | |
686 (defun cl-hash-lookup (key table) | |
687 (or (eq (car-safe table) 'cl-hash-table-tag) (cl-not-hash-table table)) | |
688 (let* ((array (nth 2 table)) (test (car (cdr table))) (str key) sym) | |
689 (if (symbolp array) (setq str nil sym (symbol-value array)) | |
690 (while (or (consp str) (and (vectorp str) (> (length str) 0))) | |
691 (setq str (elt str 0))) | |
692 (cond ((stringp str) (if (eq test 'equalp) (setq str (downcase str)))) | |
693 ((symbolp str) (setq str (symbol-name str))) | |
694 ((and (numberp str) (> str -8000000) (< str 8000000)) | |
695 (or (integerp str) (setq str (truncate str))) | |
696 (setq str (aref ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" | |
697 "11" "12" "13" "14" "15"] (logand str 15)))) | |
698 (t (setq str "*"))) | |
699 (setq sym (symbol-value (intern-soft str array)))) | |
700 (list (and sym (cond ((or (eq test 'eq) | |
701 (and (eq test 'eql) (not (numberp key)))) | |
702 (assq key sym)) | |
703 ((memq test '(eql equal)) (assoc key sym)) | |
704 (t (assoc* key sym ':test test)))) | |
705 sym str))) | |
706 | |
707 (defvar cl-builtin-gethash | |
708 (if (and (fboundp 'gethash) (subrp (symbol-function 'gethash))) | |
709 (symbol-function 'gethash) 'cl-not-hash-table)) | |
710 (defvar cl-builtin-remhash | |
711 (if (and (fboundp 'remhash) (subrp (symbol-function 'remhash))) | |
712 (symbol-function 'remhash) 'cl-not-hash-table)) | |
713 (defvar cl-builtin-clrhash | |
714 (if (and (fboundp 'clrhash) (subrp (symbol-function 'clrhash))) | |
715 (symbol-function 'clrhash) 'cl-not-hash-table)) | |
716 (defvar cl-builtin-maphash | |
717 (if (and (fboundp 'maphash) (subrp (symbol-function 'maphash))) | |
718 (symbol-function 'maphash) 'cl-not-hash-table)) | |
719 | |
720 (defun cl-gethash (key table &optional def) | |
721 "Look up KEY in HASH-TABLE; return corresponding value, or DEFAULT." | |
722 (if (consp table) | |
723 (let ((found (cl-hash-lookup key table))) | |
724 (if (car found) (cdr (car found)) def)) | |
725 (funcall cl-builtin-gethash key table def))) | |
726 | |
727 (defun cl-puthash (key val table) | |
728 (if (consp table) | |
729 (let ((found (cl-hash-lookup key table))) | |
730 (if (car found) (setcdr (car found) val) | |
731 (if (nth 2 found) | |
732 (progn | |
733 (if (> (nth 3 table) (* (length (nth 2 table)) 3)) | |
734 (let ((new-table (make-vector (nth 3 table) 0))) | |
735 (mapatoms (function | |
736 (lambda (sym) | |
737 (set (intern (symbol-name sym) new-table) | |
738 (symbol-value sym)))) | |
739 (nth 2 table)) | |
740 (setcar (cdr (cdr table)) new-table))) | |
741 (set (intern (nth 2 found) (nth 2 table)) | |
742 (cons (cons key val) (nth 1 found)))) | |
743 (set (nth 2 table) (cons (cons key val) (nth 1 found)))) | |
744 (setcar (cdr (cdr (cdr table))) (1+ (nth 3 table))))) | |
745 (funcall 'puthash key val table)) val) | |
746 | |
747 (defun cl-remhash (key table) | |
748 "Remove KEY from HASH-TABLE." | |
749 (if (consp table) | |
750 (let ((found (cl-hash-lookup key table))) | |
751 (and (car found) | |
752 (let ((del (delq (car found) (nth 1 found)))) | |
753 (setcar (cdr (cdr (cdr table))) (1- (nth 3 table))) | |
754 (if (nth 2 found) (set (intern (nth 2 found) (nth 2 table)) del) | |
755 (set (nth 2 table) del)) t))) | |
756 (prog1 (not (eq (funcall cl-builtin-gethash key table '--cl--) '--cl--)) | |
757 (funcall cl-builtin-remhash key table)))) | |
758 | |
759 (defun cl-clrhash (table) | |
760 "Clear HASH-TABLE." | |
761 (if (consp table) | |
762 (progn | |
763 (or (hash-table-p table) (cl-not-hash-table table)) | |
764 (if (symbolp (nth 2 table)) (set (nth 2 table) nil) | |
765 (setcar (cdr (cdr table)) (make-vector (length (nth 2 table)) 0))) | |
766 (setcar (cdr (cdr (cdr table))) 0)) | |
767 (funcall cl-builtin-clrhash table)) | |
768 nil) | |
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 | |
24988
3bfd67af61d0
(cl-make-hash-table): Renamed from make-hash-table.
Gerd Moellmann <gerd@gnu.org>
parents:
24826
diff
changeset
|
784 (defun cl-hash-table-count (table) |
4355 | 785 "Return the number of entries in HASH-TABLE." |
786 (or (hash-table-p table) (cl-not-hash-table table)) | |
787 (if (consp table) (nth 3 table) (funcall 'hashtable-fullness table))) | |
788 | |
789 | |
790 ;;; Some debugging aids. | |
791 | |
792 (defun cl-prettyprint (form) | |
793 "Insert a pretty-printed rendition of a Lisp FORM in current buffer." | |
794 (let ((pt (point)) last) | |
795 (insert "\n" (prin1-to-string form) "\n") | |
796 (setq last (point)) | |
797 (goto-char (1+ pt)) | |
798 (while (search-forward "(quote " last t) | |
799 (delete-backward-char 7) | |
800 (insert "'") | |
801 (forward-sexp) | |
802 (delete-char 1)) | |
803 (goto-char (1+ pt)) | |
804 (cl-do-prettyprint))) | |
805 | |
806 (defun cl-do-prettyprint () | |
807 (skip-chars-forward " ") | |
808 (if (looking-at "(") | |
809 (let ((skip (or (looking-at "((") (looking-at "(prog") | |
810 (looking-at "(unwind-protect ") | |
811 (looking-at "(function (") | |
812 (looking-at "(cl-block-wrapper "))) | |
813 (two (or (looking-at "(defun ") (looking-at "(defmacro "))) | |
814 (let (or (looking-at "(let\\*? ") (looking-at "(while "))) | |
815 (set (looking-at "(p?set[qf] "))) | |
816 (if (or skip let | |
817 (progn | |
818 (forward-sexp) | |
819 (and (>= (current-column) 78) (progn (backward-sexp) t)))) | |
820 (let ((nl t)) | |
821 (forward-char 1) | |
822 (cl-do-prettyprint) | |
823 (or skip (looking-at ")") (cl-do-prettyprint)) | |
824 (or (not two) (looking-at ")") (cl-do-prettyprint)) | |
825 (while (not (looking-at ")")) | |
826 (if set (setq nl (not nl))) | |
827 (if nl (insert "\n")) | |
828 (lisp-indent-line) | |
829 (cl-do-prettyprint)) | |
830 (forward-char 1)))) | |
831 (forward-sexp))) | |
832 | |
833 (defvar cl-macroexpand-cmacs nil) | |
834 (defvar cl-closure-vars nil) | |
835 | |
836 (defun cl-macroexpand-all (form &optional env) | |
837 "Expand all macro calls through a Lisp FORM. | |
838 This also does some trivial optimizations to make the form prettier." | |
839 (while (or (not (eq form (setq form (macroexpand form env)))) | |
840 (and cl-macroexpand-cmacs | |
841 (not (eq form (setq form (compiler-macroexpand form))))))) | |
842 (cond ((not (consp form)) form) | |
843 ((memq (car form) '(let let*)) | |
844 (if (null (nth 1 form)) | |
845 (cl-macroexpand-all (cons 'progn (cddr form)) env) | |
846 (let ((letf nil) (res nil) (lets (cadr form))) | |
847 (while lets | |
848 (cl-push (if (consp (car lets)) | |
849 (let ((exp (cl-macroexpand-all (caar lets) env))) | |
850 (or (symbolp exp) (setq letf t)) | |
851 (cons exp (cl-macroexpand-body (cdar lets) env))) | |
852 (let ((exp (cl-macroexpand-all (car lets) env))) | |
853 (if (symbolp exp) exp | |
854 (setq letf t) (list exp nil)))) res) | |
855 (setq lets (cdr lets))) | |
856 (list* (if letf (if (eq (car form) 'let) 'letf 'letf*) (car form)) | |
857 (nreverse res) (cl-macroexpand-body (cddr form) env))))) | |
858 ((eq (car form) 'cond) | |
859 (cons (car form) | |
860 (mapcar (function (lambda (x) (cl-macroexpand-body x env))) | |
861 (cdr form)))) | |
862 ((eq (car form) 'condition-case) | |
863 (list* (car form) (nth 1 form) (cl-macroexpand-all (nth 2 form) env) | |
864 (mapcar (function | |
865 (lambda (x) | |
866 (cons (car x) (cl-macroexpand-body (cdr x) env)))) | |
867 (cdddr form)))) | |
868 ((memq (car form) '(quote function)) | |
869 (if (eq (car-safe (nth 1 form)) 'lambda) | |
870 (let ((body (cl-macroexpand-body (cddadr form) env))) | |
871 (if (and cl-closure-vars (eq (car form) 'function) | |
872 (cl-expr-contains-any body cl-closure-vars)) | |
873 (let* ((new (mapcar 'gensym cl-closure-vars)) | |
874 (sub (pairlis cl-closure-vars new)) (decls nil)) | |
875 (while (or (stringp (car body)) | |
876 (eq (car-safe (car body)) 'interactive)) | |
877 (cl-push (list 'quote (cl-pop body)) decls)) | |
878 (put (car (last cl-closure-vars)) 'used t) | |
879 (append | |
880 (list 'list '(quote lambda) '(quote (&rest --cl-rest--))) | |
881 (sublis sub (nreverse decls)) | |
882 (list | |
883 (list* 'list '(quote apply) | |
884 (list 'list '(quote quote) | |
885 (list 'function | |
886 (list* 'lambda | |
887 (append new (cadadr form)) | |
888 (sublis sub body)))) | |
889 (nconc (mapcar (function | |
890 (lambda (x) | |
891 (list 'list '(quote quote) x))) | |
892 cl-closure-vars) | |
893 '((quote --cl-rest--))))))) | |
894 (list (car form) (list* 'lambda (cadadr form) body)))) | |
15029
ba44a899c055
(isqrt): Support expanded range of Lisp integers.
Richard M. Stallman <rms@gnu.org>
parents:
14794
diff
changeset
|
895 (let ((found (assq (cadr form) env))) |
ba44a899c055
(isqrt): Support expanded range of Lisp integers.
Richard M. Stallman <rms@gnu.org>
parents:
14794
diff
changeset
|
896 (if (eq (cadr (caddr found)) 'cl-labels-args) |
ba44a899c055
(isqrt): Support expanded range of Lisp integers.
Richard M. Stallman <rms@gnu.org>
parents:
14794
diff
changeset
|
897 (cl-macroexpand-all (cadr (caddr (cadddr found))) env) |
ba44a899c055
(isqrt): Support expanded range of Lisp integers.
Richard M. Stallman <rms@gnu.org>
parents:
14794
diff
changeset
|
898 form)))) |
4355 | 899 ((memq (car form) '(defun defmacro)) |
900 (list* (car form) (nth 1 form) (cl-macroexpand-body (cddr form) env))) | |
901 ((and (eq (car form) 'progn) (not (cddr form))) | |
902 (cl-macroexpand-all (nth 1 form) env)) | |
903 ((eq (car form) 'setq) | |
904 (let* ((args (cl-macroexpand-body (cdr form) env)) (p args)) | |
905 (while (and p (symbolp (car p))) (setq p (cddr p))) | |
906 (if p (cl-macroexpand-all (cons 'setf args)) (cons 'setq args)))) | |
907 (t (cons (car form) (cl-macroexpand-body (cdr form) env))))) | |
908 | |
909 (defun cl-macroexpand-body (body &optional env) | |
910 (mapcar (function (lambda (x) (cl-macroexpand-all x env))) body)) | |
911 | |
912 (defun cl-prettyexpand (form &optional full) | |
913 (message "Expanding...") | |
914 (let ((cl-macroexpand-cmacs full) (cl-compiling-file full) | |
915 (byte-compile-macro-environment nil)) | |
916 (setq form (cl-macroexpand-all form | |
917 (and (not full) '((block) (eval-when))))) | |
918 (message "Formatting...") | |
919 (prog1 (cl-prettyprint form) | |
920 (message "")))) | |
921 | |
922 | |
923 | |
924 (run-hooks 'cl-extra-load-hook) | |
925 | |
926 ;;; cl-extra.el ends here |