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