Mercurial > hg > xemacs-beta
annotate lisp/cl-extra.el @ 5242:f3eca926258e
Bit vectors are also sequences; enforce this in some CL functions.
lisp/ChangeLog addition:
2010-07-24 Aidan Kehoe <kehoea@parhasard.net>
* cl-extra.el (concatenate):
* cl-seq.el (remove*, cl-delete-duplicates):
Bit vectors are also sequences; enforce this in these functions.
* cl-macs.el (concatenate):
If TYPE is constant, don't inline #'concatenate, replace it by a
call to the appropriate C functions.
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Sat, 24 Jul 2010 17:38:35 +0100 |
parents | 7789ae555c45 |
children | 30bf66dd3ca0 |
rev | line source |
---|---|
613 | 1 ;;; cl-extra.el --- Common Lisp extensions for XEmacs Lisp (part two) |
428 | 2 |
2153 | 3 ;; Copyright (C) 1993,2000,2003 Free Software Foundation, Inc. |
801 | 4 ;; Copyright (C) 2002 Ben Wing. |
428 | 5 |
6 ;; Author: Dave Gillespie <daveg@synaptics.com> | |
7 ;; Maintainer: XEmacs Development Team | |
8 ;; Version: 2.02 | |
9 ;; Keywords: extensions, dumped | |
10 | |
11 ;; This file is part of XEmacs. | |
12 | |
13 ;; XEmacs is free software; you can redistribute it and/or modify it | |
14 ;; under the terms of the GNU General Public License as published by | |
15 ;; the Free Software Foundation; either version 2, or (at your option) | |
16 ;; any later version. | |
17 | |
18 ;; XEmacs is distributed in the hope that it will be useful, but | |
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
21 ;; General Public License for more details. | |
22 | |
23 ;; You should have received a copy of the GNU General Public License | |
24 ;; along with XEmacs; see the file COPYING. If not, write to the Free | |
25 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
26 ;; 02111-1307, USA. | |
27 | |
2153 | 28 ;;; Synched up with: FSF 21.3. |
428 | 29 |
30 ;;; Commentary: | |
31 | |
32 ;; This file is dumped with XEmacs. | |
33 | |
34 ;; These are extensions to Emacs Lisp that provide a degree of | |
35 ;; Common Lisp compatibility, beyond what is already built-in | |
36 ;; in Emacs Lisp. | |
37 ;; | |
38 ;; This package was written by Dave Gillespie; it is a complete | |
39 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986. | |
40 ;; | |
41 ;; Bug reports, comments, and suggestions are welcome! | |
42 | |
43 ;; This file contains portions of the Common Lisp extensions | |
44 ;; package which are autoloaded since they are relatively obscure. | |
45 | |
46 ;; See cl.el for Change Log. | |
47 | |
48 | |
49 ;;; Code: | |
2153 | 50 ;; XEmacs addition |
428 | 51 (eval-when-compile |
52 (require 'obsolete)) | |
53 | |
54 ;;; Type coercion. | |
55 | |
56 (defun coerce (x type) | |
57 "Coerce OBJECT to type TYPE. | |
58 TYPE is a Common Lisp type specifier." | |
59 (cond ((eq type 'list) (if (listp x) x (append x nil))) | |
60 ((eq type 'vector) (if (vectorp x) x (vconcat x))) | |
61 ((eq type 'string) (if (stringp x) x (concat x))) | |
62 ((eq type 'array) (if (arrayp x) x (vconcat x))) | |
63 ((and (eq type 'character) (stringp x) (= (length x) 1)) (aref x 0)) | |
64 ((and (eq type 'character) (symbolp x)) (coerce (symbol-name x) type)) | |
2153 | 65 ;; XEmacs addition character <-> integer coercions |
446 | 66 ((and (eq type 'character) (char-int-p x)) (int-char x)) |
67 ((and (eq type 'integer) (characterp x)) (char-int x)) | |
428 | 68 ((eq type 'float) (float x)) |
2153 | 69 ;; XEmacs addition: enhanced numeric type coercions |
2367 | 70 ((and-fboundp 'coerce-number |
71 (memq type '(integer ratio bigfloat)) | |
72 (coerce-number x type))) | |
2153 | 73 ;; XEmacs addition: bit-vector coercion |
4995
8431b52e43b1
Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4800
diff
changeset
|
74 ((or (eq type 'bit-vector) |
8431b52e43b1
Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4800
diff
changeset
|
75 (eq type 'simple-bit-vector)) |
8431b52e43b1
Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4800
diff
changeset
|
76 (if (bit-vector-p x) x (apply 'bit-vector (append x nil)))) |
2153 | 77 ;; XEmacs addition: weak-list coercion |
428 | 78 ((eq type 'weak-list) |
79 (if (weak-list-p x) x | |
80 (let ((wl (make-weak-list))) | |
81 (set-weak-list-list wl (if (listp x) x (append x nil))) | |
82 wl))) | |
4995
8431b52e43b1
Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4800
diff
changeset
|
83 ((and |
8431b52e43b1
Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4800
diff
changeset
|
84 (consp type) |
8431b52e43b1
Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4800
diff
changeset
|
85 (or (eq (car type) 'vector) |
8431b52e43b1
Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4800
diff
changeset
|
86 (eq (car type) 'simple-array) |
8431b52e43b1
Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4800
diff
changeset
|
87 (eq (car type) 'simple-vector)) |
8431b52e43b1
Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4800
diff
changeset
|
88 (cond |
8431b52e43b1
Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4800
diff
changeset
|
89 ((equal (cdr-safe type) '(*)) |
8431b52e43b1
Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4800
diff
changeset
|
90 (coerce x 'vector)) |
8431b52e43b1
Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4800
diff
changeset
|
91 ((equal (cdr-safe type) '(bit)) |
8431b52e43b1
Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4800
diff
changeset
|
92 (coerce x 'bit-vector)) |
8431b52e43b1
Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4800
diff
changeset
|
93 ((equal (cdr-safe type) '(character)) |
8431b52e43b1
Move the various map* functions to C; add #'map-into.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4800
diff
changeset
|
94 (coerce x 'string))))) |
428 | 95 ((typep x type) x) |
96 (t (error "Can't coerce %s to type %s" x type)))) | |
97 | |
5219
2d0937dc83cf
Tidying of CL files; make docstrings read better, remove commented-out code
Aidan Kehoe <kehoea@parhasard.net>
parents:
5162
diff
changeset
|
98 ;; XEmacs; #'equalp is in C. |
428 | 99 |
4997
8800b5350a13
Move #'some, #'every to C, implementing them with mapcarX.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4996
diff
changeset
|
100 ;; XEmacs; #'map, #'mapc, #'mapl, #'maplist, #'mapcon, #'some and #'every |
8800b5350a13
Move #'some, #'every to C, implementing them with mapcarX.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4996
diff
changeset
|
101 ;; are now in C, together with #'map-into, which was never in this file. |
428 | 102 |
5226
7789ae555c45
Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
103 ;; The compiler macro for this in cl-macs.el means if #'complement is handed |
7789ae555c45
Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
104 ;; a constant expression, byte-compiled code will see a byte-compiled |
7789ae555c45
Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
105 ;; function. |
7789ae555c45
Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
106 (defun complement (function &optional documentation) |
7789ae555c45
Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
107 "Return a function which gives the logical inverse of what FUNCTION would." |
7789ae555c45
Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
108 `(lambda (&rest arguments) ,@(if documentation (list documentation)) |
7789ae555c45
Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
109 (not (apply ',function arguments)))) |
7789ae555c45
Add Common Lisp's #'complement to cl-extra.el.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5219
diff
changeset
|
110 |
428 | 111 (defun notany (cl-pred cl-seq &rest cl-rest) |
112 "Return true if PREDICATE is false of every element of SEQ or SEQs." | |
113 (not (apply 'some cl-pred cl-seq cl-rest))) | |
114 | |
115 (defun notevery (cl-pred cl-seq &rest cl-rest) | |
116 "Return true if PREDICATE is false of some element of SEQ or SEQs." | |
117 (not (apply 'every cl-pred cl-seq cl-rest))) | |
118 | |
119 ;;; Support for `loop'. | |
2153 | 120 (defalias 'cl-map-keymap 'map-keymap) |
428 | 121 |
122 (defun cl-map-keymap-recursively (cl-func-rec cl-map &optional cl-base) | |
123 (or cl-base | |
2153 | 124 (setq cl-base (copy-sequence [0]))) |
125 (map-keymap | |
428 | 126 (function |
127 (lambda (cl-key cl-bind) | |
128 (aset cl-base (1- (length cl-base)) cl-key) | |
129 (if (keymapp cl-bind) | |
130 (cl-map-keymap-recursively | |
131 cl-func-rec cl-bind | |
2153 | 132 (vconcat cl-base (list 0))) |
428 | 133 (funcall cl-func-rec cl-base cl-bind)))) |
134 cl-map)) | |
135 | |
136 (defun cl-map-intervals (cl-func &optional cl-what cl-prop cl-start cl-end) | |
137 (or cl-what (setq cl-what (current-buffer))) | |
138 (if (bufferp cl-what) | |
139 (let (cl-mark cl-mark2 (cl-next t) cl-next2) | |
2153 | 140 (with-current-buffer cl-what |
428 | 141 (setq cl-mark (copy-marker (or cl-start (point-min)))) |
142 (setq cl-mark2 (and cl-end (copy-marker cl-end)))) | |
143 (while (and cl-next (or (not cl-mark2) (< cl-mark cl-mark2))) | |
2153 | 144 (setq cl-next (if cl-prop (next-single-property-change |
145 cl-mark cl-prop cl-what) | |
146 (next-property-change cl-mark cl-what)) | |
147 cl-next2 (or cl-next (with-current-buffer cl-what | |
148 (point-max)))) | |
428 | 149 (funcall cl-func (prog1 (marker-position cl-mark) |
150 (set-marker cl-mark cl-next2)) | |
151 (if cl-mark2 (min cl-next2 cl-mark2) cl-next2))) | |
152 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil))) | |
153 (or cl-start (setq cl-start 0)) | |
154 (or cl-end (setq cl-end (length cl-what))) | |
155 (while (< cl-start cl-end) | |
2153 | 156 (let ((cl-next (or (if cl-prop (next-single-property-change |
157 cl-start cl-prop cl-what) | |
158 (next-property-change cl-start cl-what)) | |
428 | 159 cl-end))) |
160 (funcall cl-func cl-start (min cl-next cl-end)) | |
161 (setq cl-start cl-next))))) | |
162 | |
163 (defun cl-map-overlays (cl-func &optional cl-buffer cl-start cl-end cl-arg) | |
164 (or cl-buffer (setq cl-buffer (current-buffer))) | |
502 | 165 (with-fboundp '(overlay-start overlay-end overlays-at next-overlay-change) |
166 (if-fboundp 'overlay-lists | |
428 | 167 |
502 | 168 ;; This is the preferred algorithm, though overlay-lists is |
169 ;; undocumented. | |
170 (let (cl-ovl) | |
2153 | 171 (with-current-buffer cl-buffer |
502 | 172 (setq cl-ovl (overlay-lists)) |
173 (if cl-start (setq cl-start (copy-marker cl-start))) | |
174 (if cl-end (setq cl-end (copy-marker cl-end)))) | |
175 (setq cl-ovl (nconc (car cl-ovl) (cdr cl-ovl))) | |
176 (while (and cl-ovl | |
177 (or (not (overlay-start (car cl-ovl))) | |
178 (and cl-end (>= (overlay-start (car cl-ovl)) cl-end)) | |
179 (and cl-start (<= (overlay-end (car cl-ovl)) | |
180 cl-start)) | |
181 (not (funcall cl-func (car cl-ovl) cl-arg)))) | |
182 (setq cl-ovl (cdr cl-ovl))) | |
183 (if cl-start (set-marker cl-start nil)) | |
184 (if cl-end (set-marker cl-end nil))) | |
185 | |
186 ;; This alternate algorithm fails to find zero-length overlays. | |
2153 | 187 (let ((cl-mark (with-current-buffer cl-buffer |
188 (copy-marker (or cl-start (point-min))))) | |
189 (cl-mark2 (and cl-end (with-current-buffer cl-buffer | |
190 (copy-marker cl-end)))) | |
502 | 191 cl-pos cl-ovl) |
192 (while (save-excursion | |
193 (and (setq cl-pos (marker-position cl-mark)) | |
194 (< cl-pos (or cl-mark2 (point-max))) | |
195 (progn | |
196 (set-buffer cl-buffer) | |
197 (setq cl-ovl (overlays-at cl-pos)) | |
198 (set-marker cl-mark (next-overlay-change cl-pos))))) | |
199 (while (and cl-ovl | |
200 (or (/= (overlay-start (car cl-ovl)) cl-pos) | |
201 (not (and (funcall cl-func (car cl-ovl) cl-arg) | |
202 (set-marker cl-mark nil))))) | |
203 (setq cl-ovl (cdr cl-ovl)))) | |
204 (set-marker cl-mark nil) (if cl-mark2 (set-marker cl-mark2 nil)))))) | |
428 | 205 |
206 ;;; Support for `setf'. | |
207 (defun cl-set-frame-visible-p (frame val) | |
208 (cond ((null val) (make-frame-invisible frame)) | |
209 ((eq val 'icon) (iconify-frame frame)) | |
210 (t (make-frame-visible frame))) | |
211 val) | |
212 | |
213 ;;; Support for `progv'. | |
214 (defvar cl-progv-save) | |
215 (defun cl-progv-before (syms values) | |
216 (while syms | |
2153 | 217 (push (if (boundp (car syms)) |
428 | 218 (cons (car syms) (symbol-value (car syms))) |
219 (car syms)) cl-progv-save) | |
220 (if values | |
2153 | 221 (set (pop syms) (pop values)) |
222 (makunbound (pop syms))))) | |
428 | 223 |
224 (defun cl-progv-after () | |
225 (while cl-progv-save | |
226 (if (consp (car cl-progv-save)) | |
227 (set (car (car cl-progv-save)) (cdr (car cl-progv-save))) | |
228 (makunbound (car cl-progv-save))) | |
2153 | 229 (pop cl-progv-save))) |
428 | 230 |
231 ;;; Numbers. | |
232 | |
233 (defun gcd (&rest args) | |
234 "Return the greatest common divisor of the arguments." | |
2153 | 235 (let ((a (abs (or (pop args) 0)))) |
428 | 236 (while args |
2153 | 237 (let ((b (abs (pop args)))) |
428 | 238 (while (> b 0) (setq b (% a (setq a b)))))) |
239 a)) | |
240 | |
241 (defun lcm (&rest args) | |
242 "Return the least common multiple of the arguments." | |
243 (if (memq 0 args) | |
244 0 | |
2153 | 245 (let ((a (abs (or (pop args) 1)))) |
428 | 246 (while args |
2153 | 247 (let ((b (abs (pop args)))) |
428 | 248 (setq a (* (/ a (gcd a b)) b)))) |
249 a))) | |
250 | |
251 (defun isqrt (a) | |
252 "Return the integer square root of the argument." | |
253 (if (and (integerp a) (> a 0)) | |
254 ;; XEmacs change | |
255 (let ((g (cond ((>= a 1000000) 10000) ((>= a 10000) 1000) | |
256 ((>= a 100) 100) (t 10))) | |
257 g2) | |
258 (while (< (setq g2 (/ (+ g (/ a g)) 2)) g) | |
259 (setq g g2)) | |
260 g) | |
261 (if (eq a 0) 0 (signal 'arith-error nil)))) | |
262 | |
4678
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
263 ;; We can't use macrolet in this file; whence the literal macro |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
264 ;; definition-and-call: |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
265 ((macro . (lambda (&rest symbols) |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
266 "Make some old CL package truncate and round functions available. |
428 | 267 |
4678
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
268 These functions are now implemented in C; their Lisp implementations in this |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
269 XEmacs are trivial, so we provide them and mark them obsolete." |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
270 (let (symbol result) |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
271 (while symbols |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
272 (setq symbol (car symbols) |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
273 symbols (cdr symbols)) |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
274 (push `(make-obsolete ',(intern (format "%s*" symbol)) |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
275 ',symbol "21.5.29") |
4800
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
276 result) |
4678
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
277 (push |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
278 `(defun ,(intern (format "%s*" symbol)) (number &optional divisor) |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
279 ,(format "See `%s'. This returns a list, not multiple values." |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
280 symbol) |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
281 (multiple-value-list (,symbol number divisor))) |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
282 result)) |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
283 (cons 'progn result)))) |
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
284 ceiling floor round truncate) |
428 | 285 |
286 (defun mod* (x y) | |
287 "The remainder of X divided by Y, with the same sign as Y." | |
4678
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
288 (nth-value 1 (floor x y))) |
428 | 289 |
290 (defun rem* (x y) | |
291 "The remainder of X divided by Y, with the same sign as X." | |
4678
b5e1d4f6b66f
Make #'floor, #'ceiling, #'round, #'truncate conform to Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
292 (nth-value 1 (truncate x y))) |
428 | 293 |
294 (defun signum (a) | |
295 "Return 1 if A is positive, -1 if negative, 0 if zero." | |
296 (cond ((> a 0) 1) ((< a 0) -1) (t 0))) | |
297 | |
298 ;; Random numbers. | |
299 | |
300 (defvar *random-state*) | |
301 (defun random* (lim &optional state) | |
302 "Return a random nonnegative number less than LIM, an integer or float. | |
303 Optional second arg STATE is a random-state object." | |
304 (or state (setq state *random-state*)) | |
305 ;; Inspired by "ran3" from Numerical Recipes. Additive congruential method. | |
306 (let ((vec (aref state 3))) | |
307 (if (integerp vec) | |
308 (let ((i 0) (j (- 1357335 (% (abs vec) 1357333))) (k 1)) | |
309 (aset state 3 (setq vec (make-vector 55 nil))) | |
310 (aset vec 0 j) | |
311 (while (> (setq i (% (+ i 21) 55)) 0) | |
312 (aset vec i (setq j (prog1 k (setq k (- j k)))))) | |
313 (while (< (setq i (1+ i)) 200) (random* 2 state)))) | |
314 (let* ((i (aset state 1 (% (1+ (aref state 1)) 55))) | |
315 (j (aset state 2 (% (1+ (aref state 2)) 55))) | |
316 (n (logand 8388607 (aset vec i (- (aref vec i) (aref vec j)))))) | |
317 (if (integerp lim) | |
318 (if (<= lim 512) (% n lim) | |
319 (if (> lim 8388607) (setq n (+ (lsh n 9) (random* 512 state)))) | |
320 (let ((mask 1023)) | |
321 (while (< mask (1- lim)) (setq mask (1+ (+ mask mask)))) | |
322 (if (< (setq n (logand n mask)) lim) n (random* lim state)))) | |
323 (* (/ n '8388608e0) lim))))) | |
324 | |
325 (defun make-random-state (&optional state) | |
326 "Return a copy of random-state STATE, or of `*random-state*' if omitted. | |
327 If STATE is t, return a new state object seeded from the time of day." | |
328 (cond ((null state) (make-random-state *random-state*)) | |
329 ((vectorp state) (cl-copy-tree state t)) | |
330 ((integerp state) (vector 'cl-random-state-tag -1 30 state)) | |
331 (t (make-random-state (cl-random-time))))) | |
332 | |
333 (defun random-state-p (object) | |
334 "Return t if OBJECT is a random-state object." | |
335 (and (vectorp object) (= (length object) 4) | |
336 (eq (aref object 0) 'cl-random-state-tag))) | |
337 | |
338 | |
339 ;; Implementation limits. | |
340 | |
341 (defun cl-finite-do (func a b) | |
342 (condition-case nil | |
343 (let ((res (funcall func a b))) ; check for IEEE infinity | |
344 (and (numberp res) (/= res (/ res 2)) res)) | |
345 (arith-error nil))) | |
346 | |
347 (defun cl-float-limits () | |
348 (or most-positive-float (not (numberp '2e1)) | |
349 (let ((x '2e0) y z) | |
350 ;; Find maximum exponent (first two loops are optimizations) | |
351 (while (cl-finite-do '* x x) (setq x (* x x))) | |
352 (while (cl-finite-do '* x (/ x 2)) (setq x (* x (/ x 2)))) | |
353 (while (cl-finite-do '+ x x) (setq x (+ x x))) | |
354 (setq z x y (/ x 2)) | |
355 ;; Now fill in 1's in the mantissa. | |
356 (while (and (cl-finite-do '+ x y) (/= (+ x y) x)) | |
357 (setq x (+ x y) y (/ y 2))) | |
358 (setq most-positive-float x | |
359 most-negative-float (- x)) | |
360 ;; Divide down until mantissa starts rounding. | |
361 (setq x (/ x z) y (/ 16 z) x (* x y)) | |
362 (while (condition-case nil (and (= x (* (/ x 2) 2)) (> (/ y 2) 0)) | |
363 (arith-error nil)) | |
364 (setq x (/ x 2) y (/ y 2))) | |
365 (setq least-positive-normalized-float y | |
366 least-negative-normalized-float (- y)) | |
367 ;; Divide down until value underflows to zero. | |
368 (setq x (/ 1 z) y x) | |
369 (while (condition-case nil (> (/ x 2) 0) (arith-error nil)) | |
370 (setq x (/ x 2))) | |
371 (setq least-positive-float x | |
372 least-negative-float (- x)) | |
373 (setq x '1e0) | |
374 (while (/= (+ '1e0 x) '1e0) (setq x (/ x 2))) | |
375 (setq float-epsilon (* x 2)) | |
376 (setq x '1e0) | |
377 (while (/= (- '1e0 x) '1e0) (setq x (/ x 2))) | |
378 (setq float-negative-epsilon (* x 2)))) | |
379 nil) | |
380 | |
5219
2d0937dc83cf
Tidying of CL files; make docstrings read better, remove commented-out code
Aidan Kehoe <kehoea@parhasard.net>
parents:
5162
diff
changeset
|
381 ;; XEmacs; call cl-float-limits at dump time. |
2d0937dc83cf
Tidying of CL files; make docstrings read better, remove commented-out code
Aidan Kehoe <kehoea@parhasard.net>
parents:
5162
diff
changeset
|
382 (cl-float-limits) |
428 | 383 |
384 ;;; Sequence functions. | |
385 | |
5219
2d0937dc83cf
Tidying of CL files; make docstrings read better, remove commented-out code
Aidan Kehoe <kehoea@parhasard.net>
parents:
5162
diff
changeset
|
386 ;; XEmacs; #'subseq is in C. |
428 | 387 |
388 (defun concatenate (type &rest seqs) | |
389 "Concatenate, into a sequence of type TYPE, the argument SEQUENCES." | |
2153 | 390 ;; XEmacs change: use case instead of cond for clarity |
428 | 391 (case type |
392 (vector (apply 'vconcat seqs)) | |
393 (string (apply 'concat seqs)) | |
394 (list (apply 'append (append seqs '(nil)))) | |
5242
f3eca926258e
Bit vectors are also sequences; enforce this in some CL functions.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5226
diff
changeset
|
395 (bit-vector (apply 'bvconcat seqs)) |
5219
2d0937dc83cf
Tidying of CL files; make docstrings read better, remove commented-out code
Aidan Kehoe <kehoea@parhasard.net>
parents:
5162
diff
changeset
|
396 (t (error 'invalid-argument "Not a sequence type name" type)))) |
428 | 397 |
398 ;;; List functions. | |
399 | |
400 (defun revappend (x y) | |
401 "Equivalent to (append (reverse X) Y)." | |
402 (nconc (reverse x) y)) | |
403 | |
404 (defun nreconc (x y) | |
405 "Equivalent to (nconc (nreverse X) Y)." | |
406 (nconc (nreverse x) y)) | |
407 | |
5219
2d0937dc83cf
Tidying of CL files; make docstrings read better, remove commented-out code
Aidan Kehoe <kehoea@parhasard.net>
parents:
5162
diff
changeset
|
408 (defun list-length (list) |
2d0937dc83cf
Tidying of CL files; make docstrings read better, remove commented-out code
Aidan Kehoe <kehoea@parhasard.net>
parents:
5162
diff
changeset
|
409 "Return the length of LIST. Return nil if LIST is circular." |
2d0937dc83cf
Tidying of CL files; make docstrings read better, remove commented-out code
Aidan Kehoe <kehoea@parhasard.net>
parents:
5162
diff
changeset
|
410 (if (listp list) |
2d0937dc83cf
Tidying of CL files; make docstrings read better, remove commented-out code
Aidan Kehoe <kehoea@parhasard.net>
parents:
5162
diff
changeset
|
411 (condition-case nil (length list) (circular-list)) |
2d0937dc83cf
Tidying of CL files; make docstrings read better, remove commented-out code
Aidan Kehoe <kehoea@parhasard.net>
parents:
5162
diff
changeset
|
412 ;; Error on not-a-list: |
2d0937dc83cf
Tidying of CL files; make docstrings read better, remove commented-out code
Aidan Kehoe <kehoea@parhasard.net>
parents:
5162
diff
changeset
|
413 (car list))) |
428 | 414 |
415 (defun tailp (sublist list) | |
416 "Return true if SUBLIST is a tail of LIST." | |
417 (while (and (consp list) (not (eq sublist list))) | |
418 (setq list (cdr list))) | |
419 (if (numberp sublist) (equal sublist list) (eq sublist list))) | |
420 | |
2153 | 421 (defalias 'cl-copy-tree 'copy-tree) |
428 | 422 |
423 ;;; Property lists. | |
424 | |
425 ;; XEmacs: our `get' groks DEFAULT. | |
426 (defalias 'get* 'get) | |
442 | 427 (defalias 'getf 'plist-get) |
428 | 428 |
429 (defun cl-set-getf (plist tag val) | |
430 (let ((p plist)) | |
431 (while (and p (not (eq (car p) tag))) (setq p (cdr (cdr p)))) | |
432 (if p (progn (setcar (cdr p) val) plist) (list* tag val plist)))) | |
433 | |
434 (defun cl-do-remf (plist tag) | |
435 (let ((p (cdr plist))) | |
436 (while (and (cdr p) (not (eq (car (cdr p)) tag))) (setq p (cdr (cdr p)))) | |
437 (and (cdr p) (progn (setcdr p (cdr (cdr (cdr p)))) t)))) | |
438 | |
2153 | 439 ;; XEmacs change: we have a builtin remprop |
440 (defalias 'cl-remprop 'remprop) | |
441 | |
4800
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
442 (defun get-properties (plist indicator-list) |
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
443 "Find a property from INDICATOR-LIST in PLIST. |
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
444 Return 3 values: |
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
445 - the first property found, |
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
446 - its value, |
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
447 - the tail of PLIST beginning with the found entry." |
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
448 (do ((plst plist (cddr plst))) |
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
449 ((null plst) (values nil nil nil)) |
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
450 (cond ((atom (cdr plst)) |
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
451 (error "Malformed property list: %S." plist)) |
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
452 ((memq (car plst) indicator-list) |
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
453 (return (values (car plst) (cadr plst) plst)))))) |
2153 | 454 |
5075
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
455 ;; See also the compiler macro in cl-macs.el. |
5056
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
456 (defun constantly (value &rest more-values) |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
457 "Construct a function always returning VALUE, and possibly MORE-VALUES. |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
458 |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
459 The constructed function accepts any number of arguments, and ignores them. |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
460 |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
461 Members of MORE-VALUES, if provided, will be passed as multiple values; see |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
462 `multiple-value-bind' and `multiple-value-setq'." |
5075
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
463 (symbol-macrolet |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
464 ((arglist '(&rest ignore))) |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
465 (if (or more-values (eval-when-compile (not (cl-compiling-file)))) |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
466 `(lambda ,arglist (values-list ',(cons value more-values))) |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
467 (make-byte-code |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
468 arglist |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
469 (eval-when-compile |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
470 (let ((compiled (byte-compile-sexp #'(lambda (&rest ignore) |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
471 (declare (ignore ignore)) |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
472 'placeholder)))) |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
473 (assert (and |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
474 (equal [placeholder] |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
475 (compiled-function-constants compiled)) |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
476 (= 1 (compiled-function-stack-depth compiled))) |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
477 t |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
478 "Our assumptions about compiled code appear not to hold.") |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
479 (compiled-function-instructions compiled))) |
868a9ffcc37b
Normally return a compiled function if one argument, #'constantly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5056
diff
changeset
|
480 (vector value) 1)))) |
2153 | 481 |
428 | 482 ;;; Hash tables. |
483 | |
484 ;; The `regular' Common Lisp hash-table stuff has been moved into C. | |
485 ;; Only backward compatibility stuff remains here. | |
486 (defun make-hashtable (size &optional test) | |
487 (make-hash-table :test test :size size)) | |
488 (defun make-weak-hashtable (size &optional test) | |
489 (make-hash-table :test test :size size :weakness t)) | |
490 (defun make-key-weak-hashtable (size &optional test) | |
491 (make-hash-table :test test :size size :weakness 'key)) | |
492 (defun make-value-weak-hashtable (size &optional test) | |
493 (make-hash-table :test test :size size :weakness 'value)) | |
494 | |
495 (define-obsolete-function-alias 'hashtablep 'hash-table-p) | |
496 (define-obsolete-function-alias 'hashtable-fullness 'hash-table-count) | |
497 (define-obsolete-function-alias 'hashtable-test-function 'hash-table-test) | |
498 (define-obsolete-function-alias 'hashtable-type 'hash-table-type) | |
499 (define-obsolete-function-alias 'hashtable-size 'hash-table-size) | |
500 (define-obsolete-function-alias 'copy-hashtable 'copy-hash-table) | |
501 | |
502 (make-obsolete 'make-hashtable 'make-hash-table) | |
503 (make-obsolete 'make-weak-hashtable 'make-hash-table) | |
504 (make-obsolete 'make-key-weak-hashtable 'make-hash-table) | |
505 (make-obsolete 'make-value-weak-hashtable 'make-hash-table) | |
506 (make-obsolete 'hash-table-type 'hash-table-weakness) | |
507 | |
508 (when (fboundp 'x-keysym-hash-table) | |
509 (make-obsolete 'x-keysym-hashtable 'x-keysym-hash-table)) | |
510 | |
511 ;; Compatibility stuff for old kludgy cl.el hash table implementation | |
512 (defvar cl-builtin-gethash (symbol-function 'gethash)) | |
513 (defvar cl-builtin-remhash (symbol-function 'remhash)) | |
514 (defvar cl-builtin-clrhash (symbol-function 'clrhash)) | |
515 (defvar cl-builtin-maphash (symbol-function 'maphash)) | |
516 | |
517 (defalias 'cl-gethash 'gethash) | |
518 (defalias 'cl-puthash 'puthash) | |
519 (defalias 'cl-remhash 'remhash) | |
520 (defalias 'cl-clrhash 'clrhash) | |
521 (defalias 'cl-maphash 'maphash) | |
2153 | 522 ;; These three actually didn't exist in Emacs-20. |
523 (defalias 'cl-make-hash-table 'make-hash-table) | |
524 (defalias 'cl-hash-table-p 'hash-table-p) | |
525 (defalias 'cl-hash-table-count 'hash-table-count) | |
428 | 526 |
527 ;;; Some debugging aids. | |
528 | |
529 (defun cl-prettyprint (form) | |
530 "Insert a pretty-printed rendition of a Lisp FORM in current buffer." | |
5162
41262f87eb39
Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5075
diff
changeset
|
531 (let ((pt (point)) last just) |
428 | 532 (insert "\n" (prin1-to-string form) "\n") |
533 (setq last (point)) | |
534 (goto-char (1+ pt)) | |
5162
41262f87eb39
Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5075
diff
changeset
|
535 (while (re-search-forward "(\\(?:\\(?:function\\|quote\\) \\)" last t) |
41262f87eb39
Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5075
diff
changeset
|
536 (delete-region (match-beginning 0) (match-end 0)) |
41262f87eb39
Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5075
diff
changeset
|
537 (if (= (length "(function ") (- (match-end 0) (match-beginning 0))) |
41262f87eb39
Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5075
diff
changeset
|
538 (insert "#'") |
41262f87eb39
Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5075
diff
changeset
|
539 (insert "'")) |
41262f87eb39
Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5075
diff
changeset
|
540 (setq just (point)) |
428 | 541 (forward-sexp) |
5162
41262f87eb39
Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5075
diff
changeset
|
542 (delete-char 1) |
41262f87eb39
Handle (function ...) specially, cl-prettyprint.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5075
diff
changeset
|
543 (goto-char just)) |
428 | 544 (goto-char (1+ pt)) |
545 (cl-do-prettyprint))) | |
546 | |
547 (defun cl-do-prettyprint () | |
548 (skip-chars-forward " ") | |
549 (if (looking-at "(") | |
4800
b828e06dbe38
New (Common Lisp) function get-propertie
Didier Verna <didier@xemacs.org>
parents:
4792
diff
changeset
|
550 (let ((skip (or (looking-at "((") |
2153 | 551 ;; XEmacs: be selective about trailing stuff after prog |
1729 | 552 (looking-at "(prog[nv12\\(ress-feedback\\|n-with-message\\)]") |
428 | 553 (looking-at "(unwind-protect ") |
554 (looking-at "(function (") | |
555 (looking-at "(cl-block-wrapper "))) | |
556 (two (or (looking-at "(defun ") (looking-at "(defmacro "))) | |
557 (let (or (looking-at "(let\\*? ") (looking-at "(while "))) | |
558 (set (looking-at "(p?set[qf] "))) | |
559 (if (or skip let | |
560 (progn | |
561 (forward-sexp) | |
562 (and (>= (current-column) 78) (progn (backward-sexp) t)))) | |
563 (let ((nl t)) | |
564 (forward-char 1) | |
565 (cl-do-prettyprint) | |
566 (or skip (looking-at ")") (cl-do-prettyprint)) | |
567 (or (not two) (looking-at ")") (cl-do-prettyprint)) | |
568 (while (not (looking-at ")")) | |
569 (if set (setq nl (not nl))) | |
570 (if nl (insert "\n")) | |
571 (lisp-indent-line) | |
572 (cl-do-prettyprint)) | |
573 (forward-char 1)))) | |
574 (forward-sexp))) | |
575 | |
576 (defvar cl-macroexpand-cmacs nil) | |
577 (defvar cl-closure-vars nil) | |
578 | |
579 (defun cl-macroexpand-all (form &optional env) | |
580 "Expand all macro calls through a Lisp FORM. | |
581 This also does some trivial optimizations to make the form prettier." | |
582 (while (or (not (eq form (setq form (macroexpand form env)))) | |
583 (and cl-macroexpand-cmacs | |
584 (not (eq form (setq form (compiler-macroexpand form))))))) | |
585 (cond ((not (consp form)) form) | |
586 ((memq (car form) '(let let*)) | |
587 (if (null (nth 1 form)) | |
588 (cl-macroexpand-all (cons 'progn (cddr form)) env) | |
589 (let ((letf nil) (res nil) (lets (cadr form))) | |
590 (while lets | |
2153 | 591 (push (if (consp (car lets)) |
428 | 592 (let ((exp (cl-macroexpand-all (caar lets) env))) |
593 (or (symbolp exp) (setq letf t)) | |
594 (cons exp (cl-macroexpand-body (cdar lets) env))) | |
595 (let ((exp (cl-macroexpand-all (car lets) env))) | |
596 (if (symbolp exp) exp | |
597 (setq letf t) (list exp nil)))) res) | |
598 (setq lets (cdr lets))) | |
599 (list* (if letf (if (eq (car form) 'let) 'letf 'letf*) (car form)) | |
600 (nreverse res) (cl-macroexpand-body (cddr form) env))))) | |
601 ((eq (car form) 'cond) | |
602 (cons (car form) | |
603 (mapcar (function (lambda (x) (cl-macroexpand-body x env))) | |
604 (cdr form)))) | |
605 ((eq (car form) 'condition-case) | |
606 (list* (car form) (nth 1 form) (cl-macroexpand-all (nth 2 form) env) | |
607 (mapcar (function | |
608 (lambda (x) | |
609 (cons (car x) (cl-macroexpand-body (cdr x) env)))) | |
610 (cdddr form)))) | |
611 ((memq (car form) '(quote function)) | |
612 (if (eq (car-safe (nth 1 form)) 'lambda) | |
613 (let ((body (cl-macroexpand-body (cddadr form) env))) | |
614 (if (and cl-closure-vars (eq (car form) 'function) | |
615 (cl-expr-contains-any body cl-closure-vars)) | |
616 (let* ((new (mapcar 'gensym cl-closure-vars)) | |
617 (sub (pairlis cl-closure-vars new)) (decls nil)) | |
618 (while (or (stringp (car body)) | |
619 (eq (car-safe (car body)) 'interactive)) | |
2153 | 620 (push (list 'quote (pop body)) decls)) |
428 | 621 (put (car (last cl-closure-vars)) 'used t) |
622 (append | |
623 (list 'list '(quote lambda) '(quote (&rest --cl-rest--))) | |
624 (sublis sub (nreverse decls)) | |
625 (list | |
626 (list* 'list '(quote apply) | |
2153 | 627 ;; XEmacs: put a quote before the function |
428 | 628 (list 'list '(quote quote) |
629 (list 'function | |
630 (list* 'lambda | |
631 (append new (cadadr form)) | |
632 (sublis sub body)))) | |
633 (nconc (mapcar (function | |
634 (lambda (x) | |
635 (list 'list '(quote quote) x))) | |
636 cl-closure-vars) | |
637 '((quote --cl-rest--))))))) | |
638 (list (car form) (list* 'lambda (cadadr form) body)))) | |
639 (let ((found (assq (cadr form) env))) | |
2153 | 640 ;; XEmacs: cadr/caddr operate on nil without errors |
428 | 641 (if (eq (cadr (caddr found)) 'cl-labels-args) |
642 (cl-macroexpand-all (cadr (caddr (cadddr found))) env) | |
643 form)))) | |
644 ((memq (car form) '(defun defmacro)) | |
645 (list* (car form) (nth 1 form) (cl-macroexpand-body (cddr form) env))) | |
646 ((and (eq (car form) 'progn) (not (cddr form))) | |
647 (cl-macroexpand-all (nth 1 form) env)) | |
648 ((eq (car form) 'setq) | |
649 (let* ((args (cl-macroexpand-body (cdr form) env)) (p args)) | |
650 (while (and p (symbolp (car p))) (setq p (cddr p))) | |
651 (if p (cl-macroexpand-all (cons 'setf args)) (cons 'setq args)))) | |
652 (t (cons (car form) (cl-macroexpand-body (cdr form) env))))) | |
653 | |
654 (defun cl-macroexpand-body (body &optional env) | |
655 (mapcar (function (lambda (x) (cl-macroexpand-all x env))) body)) | |
656 | |
657 (defun cl-prettyexpand (form &optional full) | |
658 (message "Expanding...") | |
659 (let ((cl-macroexpand-cmacs full) (cl-compiling-file full) | |
660 (byte-compile-macro-environment nil)) | |
661 (setq form (cl-macroexpand-all form | |
662 (and (not full) '((block) (eval-when))))) | |
663 (message "Formatting...") | |
664 (prog1 (cl-prettyprint form) | |
665 (message "")))) | |
666 | |
667 (run-hooks 'cl-extra-load-hook) | |
668 | |
2153 | 669 ;; XEmacs addition |
428 | 670 (provide 'cl-extra) |
671 | |
2153 | 672 ;;; arch-tag: bcd03437-0871-43fb-a8f1-ad0e0b5427ed |
428 | 673 ;;; cl-extra.el ends here |