Mercurial > hg > xemacs-beta
annotate lisp/cl-extra.el @ 5285:99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
lisp/ChangeLog addition:
2010-10-14 Aidan Kehoe <kehoea@parhasard.net>
* byte-optimize.el (side-effect-free-fns):
* cl-macs.el (remf, getf):
* cl-extra.el (tailp, cl-set-getf, cl-do-remf):
* cl.el (ldiff, endp):
Tighten up Common Lisp compatibility for #'ldiff, #'endp, #'tailp;
add circularity checking for the first two.
#'cl-set-getf and #'cl-do-remf were Lisp implementations of
#'plist-put and #'plist-remprop; change the names to aliases,
changes the macros that use them to using #'plist-put and
#'plist-remprop directly.
src/ChangeLog addition:
2010-10-14 Aidan Kehoe <kehoea@parhasard.net>
* fns.c (Fnbutlast, Fbutlast):
Tighten up Common Lisp compatibility for these two functions; they
need to operate on dotted lists without erroring.
tests/ChangeLog addition:
2010-10-14 Aidan Kehoe <kehoea@parhasard.net>
* automated/lisp-tests.el (x):
Test #'nbutlast, #'butlast with dotted lists.
Check that #'ldiff and #'tailp don't hang on circular lists; check
that #'tailp returns t with circular lists when that is
appropriate. Test them both with dotted lists.
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Thu, 14 Oct 2010 18:50:38 +0100 |
parents | d27c1ee1943b |
children | 09fed7053634 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 | |
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)) |
5257
30bf66dd3ca0
Add fixnum as an accepted destination type, #'coerce
Aidan Kehoe <kehoea@parhasard.net>
parents:
5242
diff
changeset
|
67 ((and (memq type '(integer fixnum)) (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 |
5257
30bf66dd3ca0
Add fixnum as an accepted destination type, #'coerce
Aidan Kehoe <kehoea@parhasard.net>
parents:
5242
diff
changeset
|
71 (memq type '(integer ratio bigfloat fixnum)) |
2367 | 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 | |
5285
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
408 ;; XEmacs; check LIST for type and circularity. |
428 | 409 (defun tailp (sublist list) |
410 "Return true if SUBLIST is a tail of LIST." | |
5285
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
411 (check-argument-type #'listp list) |
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
412 (let ((before list) (evenp t)) |
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
413 (while (and (consp list) (not (eq sublist list))) |
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
414 (setq list (cdr list) |
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
415 evenp (not evenp)) |
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
416 (if evenp (setq before (cdr before))) |
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
417 (if (eq before list) (error 'circular-list list))) |
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
418 (eql sublist list))) |
428 | 419 |
2153 | 420 (defalias 'cl-copy-tree 'copy-tree) |
428 | 421 |
422 ;;; Property lists. | |
423 | |
424 ;; XEmacs: our `get' groks DEFAULT. | |
425 (defalias 'get* 'get) | |
442 | 426 (defalias 'getf 'plist-get) |
428 | 427 |
5285
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
428 ;; XEmacs; these are built-in. |
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
429 (defalias 'cl-set-getf 'plist-put) |
99de5fd48e87
Tighten up Common Lisp compatibility, #'butlast, #'nbutlast, #'tailp, #'ldiff
Aidan Kehoe <kehoea@parhasard.net>
parents:
5284
diff
changeset
|
430 (defalias 'cl-do-remf 'plist-remprop) |
2153 | 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 | |
5284
d27c1ee1943b
Make the order of preloaded-file-list more sane.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5273
diff
changeset
|
658 ;; XEmacs addition; force cl-macs to be available from here on when |
d27c1ee1943b
Make the order of preloaded-file-list more sane.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5273
diff
changeset
|
659 ;; compiling files to be dumped. This is more reasonable than forcing other |
d27c1ee1943b
Make the order of preloaded-file-list more sane.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5273
diff
changeset
|
660 ;; files to do the same, multiple times. |
d27c1ee1943b
Make the order of preloaded-file-list more sane.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5273
diff
changeset
|
661 (eval-when-compile (or (cl-compiling-file) (load "cl-macs"))) |
d27c1ee1943b
Make the order of preloaded-file-list more sane.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5273
diff
changeset
|
662 |
428 | 663 (run-hooks 'cl-extra-load-hook) |
664 | |
2153 | 665 ;; XEmacs addition |
428 | 666 (provide 'cl-extra) |
667 | |
2153 | 668 ;;; arch-tag: bcd03437-0871-43fb-a8f1-ad0e0b5427ed |
428 | 669 ;;; cl-extra.el ends here |