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