Mercurial > emacs
annotate lisp/emacs-lisp/cl-extra.el @ 27154:b32c5e16dbe9
(auto-revert-mode): Return value of auto-revert-mode.
author | Gerd Moellmann <gerd@gnu.org> |
---|---|
date | Tue, 04 Jan 2000 21:29:59 +0000 |
parents | b153112d5bd0 |
children | 3e34f4e0b1c2 |
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))) | |
27123 | 224 (if (listp cl-map) |
225 (let ((cl-p cl-map)) | |
226 (while (consp (setq cl-p (cdr cl-p))) | |
227 (cond ((consp (car cl-p)) | |
228 (funcall cl-func (car (car cl-p)) (cdr (car cl-p)))) | |
229 ((vectorp (car cl-p)) | |
230 (cl-map-keymap cl-func (car cl-p))) | |
231 ((eq (car cl-p) 'keymap) | |
232 (setq cl-p nil))))) | |
233 (let ((cl-i -1)) | |
234 (while (< (setq cl-i (1+ cl-i)) (length cl-map)) | |
235 (if (aref cl-map cl-i) | |
236 (funcall cl-func cl-i (aref cl-map cl-i))))))) | |
4355 | 237 |
238 (defun cl-map-keymap-recursively (cl-func-rec cl-map &optional cl-base) | |
239 (or cl-base | |
27123 | 240 (setq cl-base (copy-sequence [0]))) |
4355 | 241 (cl-map-keymap |
242 (function | |
243 (lambda (cl-key cl-bind) | |
244 (aset cl-base (1- (length cl-base)) cl-key) | |
245 (if (keymapp cl-bind) | |
246 (cl-map-keymap-recursively | |
247 cl-func-rec cl-bind | |
27123 | 248 (vconcat cl-base (list 0))) |
4355 | 249 (funcall cl-func-rec cl-base cl-bind)))) |
250 cl-map)) | |
251 | |
252 (defun cl-map-intervals (cl-func &optional cl-what cl-prop cl-start cl-end) | |
253 (or cl-what (setq cl-what (current-buffer))) | |
254 (if (bufferp cl-what) | |
255 (let (cl-mark cl-mark2 (cl-next t) cl-next2) | |
256 (save-excursion | |
257 (set-buffer cl-what) | |
258 (setq cl-mark (copy-marker (or cl-start (point-min)))) | |
259 (setq cl-mark2 (and cl-end (copy-marker cl-end)))) | |
260 (while (and cl-next (or (not cl-mark2) (< cl-mark cl-mark2))) | |
261 (setq cl-next (and (fboundp 'next-property-change) | |
262 (if cl-prop (next-single-property-change | |
263 cl-mark cl-prop cl-what) | |
264 (next-property-change cl-mark cl-what))) | |
265 cl-next2 (or cl-next (save-excursion | |
266 (set-buffer cl-what) (point-max)))) | |
267 (funcall cl-func (prog1 (marker-position cl-mark) | |
268 (set-marker cl-mark cl-next2)) | |
269 (if cl-mark2 (min cl-next2 cl-mark2) cl-next2))) | |
270 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil))) | |
271 (or cl-start (setq cl-start 0)) | |
272 (or cl-end (setq cl-end (length cl-what))) | |
273 (while (< cl-start cl-end) | |
274 (let ((cl-next (or (and (fboundp 'next-property-change) | |
275 (if cl-prop (next-single-property-change | |
276 cl-start cl-prop cl-what) | |
277 (next-property-change cl-start cl-what))) | |
278 cl-end))) | |
279 (funcall cl-func cl-start (min cl-next cl-end)) | |
280 (setq cl-start cl-next))))) | |
281 | |
282 (defun cl-map-overlays (cl-func &optional cl-buffer cl-start cl-end cl-arg) | |
283 (or cl-buffer (setq cl-buffer (current-buffer))) | |
284 (if (fboundp 'overlay-lists) | |
285 | |
286 ;; This is the preferred algorithm, though overlay-lists is undocumented. | |
287 (let (cl-ovl) | |
288 (save-excursion | |
289 (set-buffer cl-buffer) | |
290 (setq cl-ovl (overlay-lists)) | |
291 (if cl-start (setq cl-start (copy-marker cl-start))) | |
292 (if cl-end (setq cl-end (copy-marker cl-end)))) | |
293 (setq cl-ovl (nconc (car cl-ovl) (cdr cl-ovl))) | |
294 (while (and cl-ovl | |
295 (or (not (overlay-start (car cl-ovl))) | |
296 (and cl-end (>= (overlay-start (car cl-ovl)) cl-end)) | |
297 (and cl-start (<= (overlay-end (car cl-ovl)) cl-start)) | |
298 (not (funcall cl-func (car cl-ovl) cl-arg)))) | |
299 (setq cl-ovl (cdr cl-ovl))) | |
300 (if cl-start (set-marker cl-start nil)) | |
301 (if cl-end (set-marker cl-end nil))) | |
302 | |
303 ;; This alternate algorithm fails to find zero-length overlays. | |
304 (let ((cl-mark (save-excursion (set-buffer cl-buffer) | |
305 (copy-marker (or cl-start (point-min))))) | |
306 (cl-mark2 (and cl-end (save-excursion (set-buffer cl-buffer) | |
307 (copy-marker cl-end)))) | |
308 cl-pos cl-ovl) | |
309 (while (save-excursion | |
310 (and (setq cl-pos (marker-position cl-mark)) | |
311 (< cl-pos (or cl-mark2 (point-max))) | |
312 (progn | |
313 (set-buffer cl-buffer) | |
314 (setq cl-ovl (overlays-at cl-pos)) | |
315 (set-marker cl-mark (next-overlay-change cl-pos))))) | |
316 (while (and cl-ovl | |
317 (or (/= (overlay-start (car cl-ovl)) cl-pos) | |
318 (not (and (funcall cl-func (car cl-ovl) cl-arg) | |
319 (set-marker cl-mark nil))))) | |
320 (setq cl-ovl (cdr cl-ovl)))) | |
321 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil))))) | |
322 | |
323 ;;; Support for `setf'. | |
324 (defun cl-set-frame-visible-p (frame val) | |
325 (cond ((null val) (make-frame-invisible frame)) | |
326 ((eq val 'icon) (iconify-frame frame)) | |
327 (t (make-frame-visible frame))) | |
328 val) | |
329 | |
330 ;;; Support for `progv'. | |
331 (defvar cl-progv-save) | |
332 (defun cl-progv-before (syms values) | |
333 (while syms | |
334 (cl-push (if (boundp (car syms)) | |
335 (cons (car syms) (symbol-value (car syms))) | |
336 (car syms)) cl-progv-save) | |
337 (if values | |
338 (set (cl-pop syms) (cl-pop values)) | |
339 (makunbound (cl-pop syms))))) | |
340 | |
341 (defun cl-progv-after () | |
342 (while cl-progv-save | |
343 (if (consp (car cl-progv-save)) | |
344 (set (car (car cl-progv-save)) (cdr (car cl-progv-save))) | |
345 (makunbound (car cl-progv-save))) | |
346 (cl-pop cl-progv-save))) | |
347 | |
348 | |
349 ;;; Numbers. | |
350 | |
351 (defun gcd (&rest args) | |
352 "Return the greatest common divisor of the arguments." | |
353 (let ((a (abs (or (cl-pop args) 0)))) | |
354 (while args | |
355 (let ((b (abs (cl-pop args)))) | |
356 (while (> b 0) (setq b (% a (setq a b)))))) | |
357 a)) | |
358 | |
359 (defun lcm (&rest args) | |
360 "Return the least common multiple of the arguments." | |
361 (if (memq 0 args) | |
362 0 | |
363 (let ((a (abs (or (cl-pop args) 1)))) | |
364 (while args | |
365 (let ((b (abs (cl-pop args)))) | |
366 (setq a (* (/ a (gcd a b)) b)))) | |
367 a))) | |
368 | |
369 (defun isqrt (a) | |
370 "Return the integer square root of the argument." | |
371 (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
|
372 (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
|
373 ((<= a 1000000) 1000) (t a))) |
4355 | 374 g2) |
375 (while (< (setq g2 (/ (+ g (/ a g)) 2)) g) | |
376 (setq g g2)) | |
377 g) | |
378 (if (eq a 0) 0 (signal 'arith-error nil)))) | |
379 | |
380 (defun cl-expt (x y) | |
381 "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
|
382 (if (<= y 0) (if (= y 0) 1 (if (memq x '(-1 1)) (cl-expt x (- y)) 0)) |
4355 | 383 (* (if (= (% y 2) 0) 1 x) (cl-expt (* x x) (/ y 2))))) |
384 (or (and (fboundp 'expt) (subrp (symbol-function 'expt))) | |
385 (defalias 'expt 'cl-expt)) | |
386 | |
387 (defun floor* (x &optional y) | |
388 "Return a list of the floor of X and the fractional part of X. | |
389 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
|
390 (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
|
391 (list q (- x (if y (* y q) q))))) |
4355 | 392 |
393 (defun ceiling* (x &optional y) | |
394 "Return a list of the ceiling of X and the fractional part of X. | |
395 With two arguments, return ceiling and remainder of their quotient." | |
396 (let ((res (floor* x y))) | |
397 (if (= (car (cdr res)) 0) res | |
398 (list (1+ (car res)) (- (car (cdr res)) (or y 1)))))) | |
399 | |
400 (defun truncate* (x &optional y) | |
401 "Return a list of the integer part of X and the fractional part of X. | |
402 With two arguments, return truncation and remainder of their quotient." | |
403 (if (eq (>= x 0) (or (null y) (>= y 0))) | |
404 (floor* x y) (ceiling* x y))) | |
405 | |
406 (defun round* (x &optional y) | |
407 "Return a list of X rounded to the nearest integer and the remainder. | |
408 With two arguments, return rounding and remainder of their quotient." | |
409 (if y | |
410 (if (and (integerp x) (integerp y)) | |
411 (let* ((hy (/ y 2)) | |
412 (res (floor* (+ x hy) y))) | |
413 (if (and (= (car (cdr res)) 0) | |
414 (= (+ hy hy) y) | |
415 (/= (% (car res) 2) 0)) | |
416 (list (1- (car res)) hy) | |
417 (list (car res) (- (car (cdr res)) hy)))) | |
418 (let ((q (round (/ x y)))) | |
419 (list q (- x (* q y))))) | |
420 (if (integerp x) (list x 0) | |
421 (let ((q (round x))) | |
422 (list q (- x q)))))) | |
423 | |
424 (defun mod* (x y) | |
425 "The remainder of X divided by Y, with the same sign as Y." | |
426 (nth 1 (floor* x y))) | |
427 | |
428 (defun rem* (x y) | |
429 "The remainder of X divided by Y, with the same sign as X." | |
430 (nth 1 (truncate* x y))) | |
431 | |
432 (defun signum (a) | |
433 "Return 1 if A is positive, -1 if negative, 0 if zero." | |
434 (cond ((> a 0) 1) ((< a 0) -1) (t 0))) | |
435 | |
436 | |
437 ;; Random numbers. | |
438 | |
439 (defvar *random-state*) | |
440 (defun random* (lim &optional state) | |
441 "Return a random nonnegative number less than LIM, an integer or float. | |
442 Optional second arg STATE is a random-state object." | |
443 (or state (setq state *random-state*)) | |
444 ;; Inspired by "ran3" from Numerical Recipes. Additive congruential method. | |
445 (let ((vec (aref state 3))) | |
446 (if (integerp vec) | |
447 (let ((i 0) (j (- 1357335 (% (abs vec) 1357333))) (k 1) ii) | |
448 (aset state 3 (setq vec (make-vector 55 nil))) | |
449 (aset vec 0 j) | |
450 (while (> (setq i (% (+ i 21) 55)) 0) | |
451 (aset vec i (setq j (prog1 k (setq k (- j k)))))) | |
452 (while (< (setq i (1+ i)) 200) (random* 2 state)))) | |
453 (let* ((i (aset state 1 (% (1+ (aref state 1)) 55))) | |
454 (j (aset state 2 (% (1+ (aref state 2)) 55))) | |
455 (n (logand 8388607 (aset vec i (- (aref vec i) (aref vec j)))))) | |
456 (if (integerp lim) | |
457 (if (<= lim 512) (% n lim) | |
458 (if (> lim 8388607) (setq n (+ (lsh n 9) (random* 512 state)))) | |
459 (let ((mask 1023)) | |
460 (while (< mask (1- lim)) (setq mask (1+ (+ mask mask)))) | |
461 (if (< (setq n (logand n mask)) lim) n (random* lim state)))) | |
462 (* (/ n '8388608e0) lim))))) | |
463 | |
464 (defun make-random-state (&optional state) | |
465 "Return a copy of random-state STATE, or of `*random-state*' if omitted. | |
466 If STATE is t, return a new state object seeded from the time of day." | |
467 (cond ((null state) (make-random-state *random-state*)) | |
468 ((vectorp state) (cl-copy-tree state t)) | |
469 ((integerp state) (vector 'cl-random-state-tag -1 30 state)) | |
470 (t (make-random-state (cl-random-time))))) | |
471 | |
472 (defun random-state-p (object) | |
473 "Return t if OBJECT is a random-state object." | |
474 (and (vectorp object) (= (length object) 4) | |
475 (eq (aref object 0) 'cl-random-state-tag))) | |
476 | |
477 | |
478 ;; Implementation limits. | |
479 | |
480 (defun cl-finite-do (func a b) | |
481 (condition-case err | |
482 (let ((res (funcall func a b))) ; check for IEEE infinity | |
483 (and (numberp res) (/= res (/ res 2)) res)) | |
484 (arith-error nil))) | |
485 | |
486 (defvar most-positive-float) | |
487 (defvar most-negative-float) | |
488 (defvar least-positive-float) | |
489 (defvar least-negative-float) | |
490 (defvar least-positive-normalized-float) | |
491 (defvar least-negative-normalized-float) | |
492 (defvar float-epsilon) | |
493 (defvar float-negative-epsilon) | |
494 | |
495 (defun cl-float-limits () | |
496 (or most-positive-float (not (numberp '2e1)) | |
497 (let ((x '2e0) y z) | |
498 ;; Find maximum exponent (first two loops are optimizations) | |
499 (while (cl-finite-do '* x x) (setq x (* x x))) | |
500 (while (cl-finite-do '* x (/ x 2)) (setq x (* x (/ x 2)))) | |
501 (while (cl-finite-do '+ x x) (setq x (+ x x))) | |
502 (setq z x y (/ x 2)) | |
503 ;; Now fill in 1's in the mantissa. | |
504 (while (and (cl-finite-do '+ x y) (/= (+ x y) x)) | |
505 (setq x (+ x y) y (/ y 2))) | |
506 (setq most-positive-float x | |
507 most-negative-float (- x)) | |
508 ;; Divide down until mantissa starts rounding. | |
509 (setq x (/ x z) y (/ 16 z) x (* x y)) | |
510 (while (condition-case err (and (= x (* (/ x 2) 2)) (> (/ y 2) 0)) | |
511 (arith-error nil)) | |
512 (setq x (/ x 2) y (/ y 2))) | |
513 (setq least-positive-normalized-float y | |
514 least-negative-normalized-float (- y)) | |
515 ;; Divide down until value underflows to zero. | |
516 (setq x (/ 1 z) y x) | |
517 (while (condition-case err (> (/ x 2) 0) (arith-error nil)) | |
518 (setq x (/ x 2))) | |
519 (setq least-positive-float x | |
520 least-negative-float (- x)) | |
521 (setq x '1e0) | |
522 (while (/= (+ '1e0 x) '1e0) (setq x (/ x 2))) | |
523 (setq float-epsilon (* x 2)) | |
524 (setq x '1e0) | |
525 (while (/= (- '1e0 x) '1e0) (setq x (/ x 2))) | |
526 (setq float-negative-epsilon (* x 2)))) | |
527 nil) | |
528 | |
529 | |
530 ;;; Sequence functions. | |
531 | |
532 (defun subseq (seq start &optional end) | |
533 "Return the subsequence of SEQ from START to END. | |
534 If END is omitted, it defaults to the length of the sequence. | |
535 If START or END is negative, it counts from the end." | |
536 (if (stringp seq) (substring seq start end) | |
537 (let (len) | |
538 (and end (< end 0) (setq end (+ end (setq len (length seq))))) | |
539 (if (< start 0) (setq start (+ start (or len (setq len (length seq)))))) | |
540 (cond ((listp seq) | |
541 (if (> start 0) (setq seq (nthcdr start seq))) | |
542 (if end | |
543 (let ((res nil)) | |
544 (while (>= (setq end (1- end)) start) | |
545 (cl-push (cl-pop seq) res)) | |
546 (nreverse res)) | |
547 (copy-sequence seq))) | |
548 (t | |
549 (or end (setq end (or len (length seq)))) | |
550 (let ((res (make-vector (max (- end start) 0) nil)) | |
551 (i 0)) | |
552 (while (< start end) | |
553 (aset res i (aref seq start)) | |
554 (setq i (1+ i) start (1+ start))) | |
555 res)))))) | |
556 | |
557 (defun concatenate (type &rest seqs) | |
558 "Concatenate, into a sequence of type TYPE, the argument SEQUENCES." | |
559 (cond ((eq type 'vector) (apply 'vconcat seqs)) | |
560 ((eq type 'string) (apply 'concat seqs)) | |
561 ((eq type 'list) (apply 'append (append seqs '(nil)))) | |
562 (t (error "Not a sequence type name: %s" type)))) | |
563 | |
564 | |
565 ;;; List functions. | |
566 | |
567 (defun revappend (x y) | |
568 "Equivalent to (append (reverse X) Y)." | |
569 (nconc (reverse x) y)) | |
570 | |
571 (defun nreconc (x y) | |
572 "Equivalent to (nconc (nreverse X) Y)." | |
573 (nconc (nreverse x) y)) | |
574 | |
575 (defun list-length (x) | |
576 "Return the length of a list. Return nil if list is circular." | |
577 (let ((n 0) (fast x) (slow x)) | |
578 (while (and (cdr fast) (not (and (eq fast slow) (> n 0)))) | |
579 (setq n (+ n 2) fast (cdr (cdr fast)) slow (cdr slow))) | |
580 (if fast (if (cdr fast) nil (1+ n)) n))) | |
581 | |
582 (defun tailp (sublist list) | |
583 "Return true if SUBLIST is a tail of LIST." | |
584 (while (and (consp list) (not (eq sublist list))) | |
585 (setq list (cdr list))) | |
586 (if (numberp sublist) (equal sublist list) (eq sublist list))) | |
587 | |
588 (defun cl-copy-tree (tree &optional vecp) | |
589 "Make a copy of TREE. | |
590 If TREE is a cons cell, this recursively copies both its car and its cdr. | |
13970 | 591 Contrast to copy-sequence, which copies only along the cdrs. With second |
4355 | 592 argument VECP, this copies vectors as well as conses." |
593 (if (consp tree) | |
594 (let ((p (setq tree (copy-list tree)))) | |
595 (while (consp p) | |
596 (if (or (consp (car p)) (and vecp (vectorp (car p)))) | |
597 (setcar p (cl-copy-tree (car p) vecp))) | |
598 (or (listp (cdr p)) (setcdr p (cl-copy-tree (cdr p) vecp))) | |
599 (cl-pop p))) | |
600 (if (and vecp (vectorp tree)) | |
601 (let ((i (length (setq tree (copy-sequence tree))))) | |
602 (while (>= (setq i (1- i)) 0) | |
603 (aset tree i (cl-copy-tree (aref tree i) vecp)))))) | |
604 tree) | |
605 (or (and (fboundp 'copy-tree) (subrp (symbol-function 'copy-tree))) | |
606 (defalias 'copy-tree 'cl-copy-tree)) | |
607 | |
608 | |
609 ;;; Property lists. | |
610 | |
611 (defun get* (sym tag &optional def) ; See compiler macro in cl-macs.el | |
612 "Return the value of SYMBOL's PROPNAME property, or DEFAULT if none." | |
613 (or (get sym tag) | |
614 (and def | |
615 (let ((plist (symbol-plist sym))) | |
616 (while (and plist (not (eq (car plist) tag))) | |
617 (setq plist (cdr (cdr plist)))) | |
618 (if plist (car (cdr plist)) def))))) | |
619 | |
620 (defun getf (plist tag &optional def) | |
621 "Search PROPLIST for property PROPNAME; return its value or DEFAULT. | |
622 PROPLIST is a list of the sort returned by `symbol-plist'." | |
623 (setplist '--cl-getf-symbol-- plist) | |
624 (or (get '--cl-getf-symbol-- tag) | |
24826 | 625 ;; Originally we called get* here, |
626 ;; but that fails, because get* has a compiler macro | |
627 ;; definition that uses getf! | |
628 (when def | |
629 (while (and plist (not (eq (car plist) tag))) | |
630 (setq plist (cdr (cdr plist)))) | |
631 (if plist (car (cdr plist)) def)))) | |
4355 | 632 |
633 (defun cl-set-getf (plist tag val) | |
634 (let ((p plist)) | |
635 (while (and p (not (eq (car p) tag))) (setq p (cdr (cdr p)))) | |
636 (if p (progn (setcar (cdr p) val) plist) (list* tag val plist)))) | |
637 | |
638 (defun cl-do-remf (plist tag) | |
639 (let ((p (cdr plist))) | |
640 (while (and (cdr p) (not (eq (car (cdr p)) tag))) (setq p (cdr (cdr p)))) | |
641 (and (cdr p) (progn (setcdr p (cdr (cdr (cdr p)))) t)))) | |
642 | |
643 (defun cl-remprop (sym tag) | |
644 "Remove from SYMBOL's plist the property PROP and its value." | |
645 (let ((plist (symbol-plist sym))) | |
646 (if (and plist (eq tag (car plist))) | |
647 (progn (setplist sym (cdr (cdr plist))) t) | |
648 (cl-do-remf plist tag)))) | |
649 (or (and (fboundp 'remprop) (subrp (symbol-function 'remprop))) | |
650 (defalias 'remprop 'cl-remprop)) | |
651 | |
652 | |
653 | |
654 ;;; Hash tables. | |
655 | |
24988
3bfd67af61d0
(cl-make-hash-table): Renamed from make-hash-table.
Gerd Moellmann <gerd@gnu.org>
parents:
24826
diff
changeset
|
656 (defun cl-make-hash-table (&rest cl-keys) |
4355 | 657 "Make an empty Common Lisp-style hash-table. |
658 If :test is `eq', this can use Lucid Emacs built-in hash-tables. | |
659 In non-Lucid Emacs, or with non-`eq' test, this internally uses a-lists. | |
660 Keywords supported: :test :size | |
661 The Common Lisp keywords :rehash-size and :rehash-threshold are ignored." | |
662 (let ((cl-test (or (car (cdr (memq ':test cl-keys))) 'eql)) | |
663 (cl-size (or (car (cdr (memq ':size cl-keys))) 20))) | |
664 (if (and (eq cl-test 'eq) (fboundp 'make-hashtable)) | |
665 (funcall 'make-hashtable cl-size) | |
666 (list 'cl-hash-table-tag cl-test | |
667 (if (> cl-size 1) (make-vector cl-size 0) | |
668 (let ((sym (make-symbol "--hashsym--"))) (set sym nil) sym)) | |
669 0)))) | |
670 | |
671 (defvar cl-lucid-hash-tag | |
672 (if (and (fboundp 'make-hashtable) (vectorp (make-hashtable 1))) | |
673 (aref (make-hashtable 1) 0) (make-symbol "--cl-hash-tag--"))) | |
674 | |
24988
3bfd67af61d0
(cl-make-hash-table): Renamed from make-hash-table.
Gerd Moellmann <gerd@gnu.org>
parents:
24826
diff
changeset
|
675 (defun cl-hash-table-p (x) |
4355 | 676 "Return t if OBJECT is a hash table." |
677 (or (eq (car-safe x) 'cl-hash-table-tag) | |
678 (and (vectorp x) (= (length x) 4) (eq (aref x 0) cl-lucid-hash-tag)) | |
679 (and (fboundp 'hashtablep) (funcall 'hashtablep x)))) | |
680 | |
681 (defun cl-not-hash-table (x &optional y &rest z) | |
27123 | 682 (signal 'wrong-type-argument (list 'cl-hash-table-p (or y x)))) |
4355 | 683 |
684 (defun cl-hash-lookup (key table) | |
685 (or (eq (car-safe table) 'cl-hash-table-tag) (cl-not-hash-table table)) | |
686 (let* ((array (nth 2 table)) (test (car (cdr table))) (str key) sym) | |
687 (if (symbolp array) (setq str nil sym (symbol-value array)) | |
688 (while (or (consp str) (and (vectorp str) (> (length str) 0))) | |
689 (setq str (elt str 0))) | |
690 (cond ((stringp str) (if (eq test 'equalp) (setq str (downcase str)))) | |
691 ((symbolp str) (setq str (symbol-name str))) | |
692 ((and (numberp str) (> str -8000000) (< str 8000000)) | |
693 (or (integerp str) (setq str (truncate str))) | |
694 (setq str (aref ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" | |
695 "11" "12" "13" "14" "15"] (logand str 15)))) | |
696 (t (setq str "*"))) | |
697 (setq sym (symbol-value (intern-soft str array)))) | |
698 (list (and sym (cond ((or (eq test 'eq) | |
699 (and (eq test 'eql) (not (numberp key)))) | |
700 (assq key sym)) | |
701 ((memq test '(eql equal)) (assoc key sym)) | |
702 (t (assoc* key sym ':test test)))) | |
703 sym str))) | |
704 | |
705 (defvar cl-builtin-gethash | |
706 (if (and (fboundp 'gethash) (subrp (symbol-function 'gethash))) | |
707 (symbol-function 'gethash) 'cl-not-hash-table)) | |
708 (defvar cl-builtin-remhash | |
709 (if (and (fboundp 'remhash) (subrp (symbol-function 'remhash))) | |
710 (symbol-function 'remhash) 'cl-not-hash-table)) | |
711 (defvar cl-builtin-clrhash | |
712 (if (and (fboundp 'clrhash) (subrp (symbol-function 'clrhash))) | |
713 (symbol-function 'clrhash) 'cl-not-hash-table)) | |
714 (defvar cl-builtin-maphash | |
715 (if (and (fboundp 'maphash) (subrp (symbol-function 'maphash))) | |
716 (symbol-function 'maphash) 'cl-not-hash-table)) | |
717 | |
718 (defun cl-gethash (key table &optional def) | |
719 "Look up KEY in HASH-TABLE; return corresponding value, or DEFAULT." | |
720 (if (consp table) | |
721 (let ((found (cl-hash-lookup key table))) | |
722 (if (car found) (cdr (car found)) def)) | |
723 (funcall cl-builtin-gethash key table def))) | |
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 | |
757 (defun cl-clrhash (table) | |
758 "Clear HASH-TABLE." | |
759 (if (consp table) | |
760 (progn | |
27123 | 761 (or (cl-hash-table-p table) (cl-not-hash-table table)) |
4355 | 762 (if (symbolp (nth 2 table)) (set (nth 2 table) nil) |
763 (setcar (cdr (cdr table)) (make-vector (length (nth 2 table)) 0))) | |
764 (setcar (cdr (cdr (cdr table))) 0)) | |
765 (funcall cl-builtin-clrhash table)) | |
766 nil) | |
767 | |
768 (defun cl-maphash (cl-func cl-table) | |
769 "Call FUNCTION on keys and values from HASH-TABLE." | |
27123 | 770 (or (cl-hash-table-p cl-table) (cl-not-hash-table cl-table)) |
4355 | 771 (if (consp cl-table) |
772 (mapatoms (function (lambda (cl-x) | |
773 (setq cl-x (symbol-value cl-x)) | |
774 (while cl-x | |
775 (funcall cl-func (car (car cl-x)) | |
776 (cdr (car cl-x))) | |
777 (setq cl-x (cdr cl-x))))) | |
778 (if (symbolp (nth 2 cl-table)) | |
779 (vector (nth 2 cl-table)) (nth 2 cl-table))) | |
780 (funcall cl-builtin-maphash cl-func cl-table))) | |
781 | |
24988
3bfd67af61d0
(cl-make-hash-table): Renamed from make-hash-table.
Gerd Moellmann <gerd@gnu.org>
parents:
24826
diff
changeset
|
782 (defun cl-hash-table-count (table) |
4355 | 783 "Return the number of entries in HASH-TABLE." |
27123 | 784 (or (cl-hash-table-p table) (cl-not-hash-table table)) |
4355 | 785 (if (consp table) (nth 3 table) (funcall 'hashtable-fullness table))) |
786 | |
787 | |
788 ;;; Some debugging aids. | |
789 | |
790 (defun cl-prettyprint (form) | |
791 "Insert a pretty-printed rendition of a Lisp FORM in current buffer." | |
792 (let ((pt (point)) last) | |
793 (insert "\n" (prin1-to-string form) "\n") | |
794 (setq last (point)) | |
795 (goto-char (1+ pt)) | |
796 (while (search-forward "(quote " last t) | |
797 (delete-backward-char 7) | |
798 (insert "'") | |
799 (forward-sexp) | |
800 (delete-char 1)) | |
801 (goto-char (1+ pt)) | |
802 (cl-do-prettyprint))) | |
803 | |
804 (defun cl-do-prettyprint () | |
805 (skip-chars-forward " ") | |
806 (if (looking-at "(") | |
807 (let ((skip (or (looking-at "((") (looking-at "(prog") | |
808 (looking-at "(unwind-protect ") | |
809 (looking-at "(function (") | |
810 (looking-at "(cl-block-wrapper "))) | |
811 (two (or (looking-at "(defun ") (looking-at "(defmacro "))) | |
812 (let (or (looking-at "(let\\*? ") (looking-at "(while "))) | |
813 (set (looking-at "(p?set[qf] "))) | |
814 (if (or skip let | |
815 (progn | |
816 (forward-sexp) | |
817 (and (>= (current-column) 78) (progn (backward-sexp) t)))) | |
818 (let ((nl t)) | |
819 (forward-char 1) | |
820 (cl-do-prettyprint) | |
821 (or skip (looking-at ")") (cl-do-prettyprint)) | |
822 (or (not two) (looking-at ")") (cl-do-prettyprint)) | |
823 (while (not (looking-at ")")) | |
824 (if set (setq nl (not nl))) | |
825 (if nl (insert "\n")) | |
826 (lisp-indent-line) | |
827 (cl-do-prettyprint)) | |
828 (forward-char 1)))) | |
829 (forward-sexp))) | |
830 | |
831 (defvar cl-macroexpand-cmacs nil) | |
832 (defvar cl-closure-vars nil) | |
833 | |
834 (defun cl-macroexpand-all (form &optional env) | |
835 "Expand all macro calls through a Lisp FORM. | |
836 This also does some trivial optimizations to make the form prettier." | |
837 (while (or (not (eq form (setq form (macroexpand form env)))) | |
838 (and cl-macroexpand-cmacs | |
839 (not (eq form (setq form (compiler-macroexpand form))))))) | |
840 (cond ((not (consp form)) form) | |
841 ((memq (car form) '(let let*)) | |
842 (if (null (nth 1 form)) | |
843 (cl-macroexpand-all (cons 'progn (cddr form)) env) | |
844 (let ((letf nil) (res nil) (lets (cadr form))) | |
845 (while lets | |
846 (cl-push (if (consp (car lets)) | |
847 (let ((exp (cl-macroexpand-all (caar lets) env))) | |
848 (or (symbolp exp) (setq letf t)) | |
849 (cons exp (cl-macroexpand-body (cdar lets) env))) | |
850 (let ((exp (cl-macroexpand-all (car lets) env))) | |
851 (if (symbolp exp) exp | |
852 (setq letf t) (list exp nil)))) res) | |
853 (setq lets (cdr lets))) | |
854 (list* (if letf (if (eq (car form) 'let) 'letf 'letf*) (car form)) | |
855 (nreverse res) (cl-macroexpand-body (cddr form) env))))) | |
856 ((eq (car form) 'cond) | |
857 (cons (car form) | |
858 (mapcar (function (lambda (x) (cl-macroexpand-body x env))) | |
859 (cdr form)))) | |
860 ((eq (car form) 'condition-case) | |
861 (list* (car form) (nth 1 form) (cl-macroexpand-all (nth 2 form) env) | |
862 (mapcar (function | |
863 (lambda (x) | |
864 (cons (car x) (cl-macroexpand-body (cdr x) env)))) | |
865 (cdddr form)))) | |
866 ((memq (car form) '(quote function)) | |
867 (if (eq (car-safe (nth 1 form)) 'lambda) | |
868 (let ((body (cl-macroexpand-body (cddadr form) env))) | |
869 (if (and cl-closure-vars (eq (car form) 'function) | |
870 (cl-expr-contains-any body cl-closure-vars)) | |
871 (let* ((new (mapcar 'gensym cl-closure-vars)) | |
872 (sub (pairlis cl-closure-vars new)) (decls nil)) | |
873 (while (or (stringp (car body)) | |
874 (eq (car-safe (car body)) 'interactive)) | |
875 (cl-push (list 'quote (cl-pop body)) decls)) | |
876 (put (car (last cl-closure-vars)) 'used t) | |
877 (append | |
878 (list 'list '(quote lambda) '(quote (&rest --cl-rest--))) | |
879 (sublis sub (nreverse decls)) | |
880 (list | |
881 (list* 'list '(quote apply) | |
882 (list 'list '(quote quote) | |
883 (list 'function | |
884 (list* 'lambda | |
885 (append new (cadadr form)) | |
886 (sublis sub body)))) | |
887 (nconc (mapcar (function | |
888 (lambda (x) | |
889 (list 'list '(quote quote) x))) | |
890 cl-closure-vars) | |
891 '((quote --cl-rest--))))))) | |
892 (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
|
893 (let ((found (assq (cadr form) env))) |
ba44a899c055
(isqrt): Support expanded range of Lisp integers.
Richard M. Stallman <rms@gnu.org>
parents:
14794
diff
changeset
|
894 (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
|
895 (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
|
896 form)))) |
4355 | 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 |