Mercurial > hg > xemacs-beta
annotate lisp/cl-macs.el @ 5090:0ca81354c4c7
Further frame-geometry cleanups
-------------------- ChangeLog entries follow: --------------------
man/ChangeLog addition:
2010-03-03 Ben Wing <ben@xemacs.org>
* internals/internals.texi (Intro to Window and Frame Geometry):
* internals/internals.texi (The Paned Area):
* internals/internals.texi (The Displayable Area):
Update to make note of e.g. the fact that the bottom gutter is
actually above the minibuffer.
src/ChangeLog addition:
2010-03-03 Ben Wing <ben@xemacs.org>
* emacs.c:
* emacs.c (assert_equal_failed):
* lisp.h:
* lisp.h (assert_equal):
New fun assert_equal, asserting that two values == each other, and
printing out both values upon failure.
* frame-gtk.c (gtk_initialize_frame_size):
* frame-impl.h:
* frame-impl.h (FRAME_TOP_INTERNAL_BORDER_START):
* frame-impl.h (FRAME_BOTTOM_INTERNAL_BORDER_START):
* frame-impl.h (FRAME_LEFT_INTERNAL_BORDER_START):
* frame-impl.h (FRAME_PANED_TOP_EDGE):
* frame-impl.h (FRAME_NONPANED_SIZE):
* frame-x.c (x_initialize_frame_size):
* frame.c:
* gutter.c (get_gutter_coords):
* gutter.c (calculate_gutter_size):
* gutter.h:
* gutter.h (WINDOW_REAL_TOP_GUTTER_BOUNDS):
* gutter.h (FRAME_TOP_GUTTER_BOUNDS):
* input-method-xlib.c:
* input-method-xlib.c (XIM_SetGeometry):
* redisplay-output.c (clear_left_border):
* redisplay-output.c (clear_right_border):
* redisplay-output.c (redisplay_output_pixmap):
* redisplay-output.c (redisplay_clear_region):
* redisplay-output.c (redisplay_clear_top_of_window):
* redisplay-output.c (redisplay_clear_to_window_end):
* redisplay-xlike-inc.c (XLIKE_clear_frame):
* redisplay.c:
* redisplay.c (UPDATE_CACHE_RETURN):
* redisplay.c (pixel_to_glyph_translation):
* toolbar.c (update_frame_toolbars_geometry):
* window.c (Fwindow_pixel_edges):
Get rid of some redundant macros. Consistently use the
FRAME_TOP_*_START, FRAME_RIGHT_*_END, etc. format. Rename
FRAME_*_BORDER_* to FRAME_*_INTERNAL_BORDER_*. Comment out
FRAME_BOTTOM_* for gutters and the paned area due to the
uncertainty over where the paned area actually begins. (Eventually
we should probably move the gutters outside the minibuffer so that
the paned area is contiguous.) Use FRAME_PANED_* more often in the
code to make things clearer.
Update the diagram to show that the bottom gutter is inside the
minibuffer (!) and that there are "junk boxes" when you have left
and/or right gutters (dead boxes that are mistakenly left uncleared,
unlike the corresponding scrollbar dead boxes). Update the text
appropriately to cover the bottom gutter position, etc.
Rewrite gutter-geometry code to use the FRAME_*_GUTTER_* in place of
equivalent expressions referencing other frame elements, to make the
code more portable in case we move around the gutter location.
Cleanup FRAME_*_GUTTER_BOUNDS() in gutter.h.
Add some #### GEOM! comments where I think code is incorrect --
typically, it wasn't fixed up properly when the gutter was added.
Some cosmetic changes.
author | Ben Wing <ben@xemacs.org> |
---|---|
date | Wed, 03 Mar 2010 05:07:47 -0600 |
parents | 5502045ec510 |
children | 6afe991b8135 |
rev | line source |
---|---|
613 | 1 ;;; cl-macs.el --- Common Lisp extensions for XEmacs Lisp (part four) |
428 | 2 |
2153 | 3 ;; Copyright (C) 1993, 2003, 2004 Free Software Foundation, Inc. |
4906
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
4 ;; Copyright (C) 2002, 2010 Ben Wing. |
428 | 5 |
6 ;; Author: Dave Gillespie <daveg@synaptics.com> | |
7 ;; Version: 2.02 | |
8 ;; Keywords: extensions | |
9 | |
10 ;; This file is part of XEmacs. | |
11 | |
12 ;; XEmacs is free software; you can redistribute it and/or modify it | |
13 ;; under the terms of the GNU General Public License as published by | |
14 ;; the Free Software Foundation; either version 2, or (at your option) | |
15 ;; any later version. | |
16 | |
17 ;; XEmacs is distributed in the hope that it will be useful, but | |
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 ;; General Public License for more details. | |
21 | |
22 ;; You should have received a copy of the GNU General Public License | |
23 ;; along with XEmacs; see the file COPYING. If not, write to the Free | |
24 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
25 ;; 02111-1307, USA. | |
26 | |
2153 | 27 ;;; Synched up with: FSF 21.3. |
428 | 28 |
29 ;;; Commentary: | |
30 | |
31 ;; These are extensions to Emacs Lisp that provide a degree of | |
32 ;; Common Lisp compatibility, beyond what is already built-in | |
33 ;; in Emacs Lisp. | |
34 ;; | |
35 ;; This package was written by Dave Gillespie; it is a complete | |
36 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986. | |
37 ;; | |
38 ;; Bug reports, comments, and suggestions are welcome! | |
39 | |
40 ;; This file contains the portions of the Common Lisp extensions | |
41 ;; package which should be autoloaded, but need only be present | |
42 ;; if the compiler or interpreter is used---this file is not | |
43 ;; necessary for executing compiled code. | |
44 | |
45 ;; See cl.el for Change Log. | |
46 | |
47 | |
48 ;;; Code: | |
49 | |
50 (or (memq 'cl-19 features) | |
51 (error "Tried to load `cl-macs' before `cl'!")) | |
52 | |
53 | |
54 (defmacro cl-pop2 (place) | |
55 (list 'prog1 (list 'car (list 'cdr place)) | |
56 (list 'setq place (list 'cdr (list 'cdr place))))) | |
57 (put 'cl-pop2 'edebug-form-spec 'edebug-sexps) | |
58 | |
59 (defvar cl-optimize-safety) | |
60 (defvar cl-optimize-speed) | |
61 | |
62 | |
63 ;;; This kludge allows macros which use cl-transform-function-property | |
64 ;;; to be called at compile-time. | |
65 | |
66 (require | |
67 (progn | |
68 (or (fboundp 'cl-transform-function-property) | |
69 (defalias 'cl-transform-function-property | |
70 #'(lambda (n p f) | |
71 (list 'put (list 'quote n) (list 'quote p) | |
72 (list 'function (cons 'lambda f)))))) | |
442 | 73 'xemacs)) |
428 | 74 |
75 | |
76 ;;; Initialization. | |
77 | |
78 (defvar cl-old-bc-file-form nil) | |
79 | |
80 ;;;###autoload | |
81 (defun cl-compile-time-init () | |
82 (run-hooks 'cl-hack-bytecomp-hook)) | |
83 | |
84 | |
2153 | 85 ;;; Some predicates for analyzing Lisp forms. These are used by various |
86 ;;; macro expanders to optimize the results in certain common cases. | |
87 | |
88 (defconst cl-simple-funcs '(car cdr nth aref elt if and or + - 1+ 1- min max | |
89 car-safe cdr-safe progn prog1 prog2)) | |
90 (defconst cl-safe-funcs '(* / % length memq list vector vectorp | |
91 < > <= >= = error)) | |
92 | |
93 ;;; Check if no side effects, and executes quickly. | |
94 (defun cl-simple-expr-p (x &optional size) | |
95 (or size (setq size 10)) | |
96 (if (and (consp x) (not (memq (car x) '(quote function function*)))) | |
97 (and (symbolp (car x)) | |
98 (or (memq (car x) cl-simple-funcs) | |
99 (get (car x) 'side-effect-free)) | |
100 (progn | |
101 (setq size (1- size)) | |
102 (while (and (setq x (cdr x)) | |
103 (setq size (cl-simple-expr-p (car x) size)))) | |
104 (and (null x) (>= size 0) size))) | |
105 (and (> size 0) (1- size)))) | |
106 | |
107 (defun cl-simple-exprs-p (xs) | |
108 (while (and xs (cl-simple-expr-p (car xs))) | |
109 (setq xs (cdr xs))) | |
110 (not xs)) | |
111 | |
112 ;;; Check if no side effects. | |
113 (defun cl-safe-expr-p (x) | |
114 (or (not (and (consp x) (not (memq (car x) '(quote function function*))))) | |
115 (and (symbolp (car x)) | |
116 (or (memq (car x) cl-simple-funcs) | |
117 (memq (car x) cl-safe-funcs) | |
118 (get (car x) 'side-effect-free)) | |
119 (progn | |
120 (while (and (setq x (cdr x)) (cl-safe-expr-p (car x)))) | |
121 (null x))))) | |
122 | |
123 ;;; Check if constant (i.e., no side effects or dependencies). | |
124 (defun cl-const-expr-p (x) | |
125 (cond ((consp x) | |
126 (or (eq (car x) 'quote) | |
127 (and (memq (car x) '(function function*)) | |
128 (or (symbolp (nth 1 x)) | |
129 (and (eq (car-safe (nth 1 x)) 'lambda) 'func))))) | |
130 ((symbolp x) (and (memq x '(nil t)) t)) | |
131 (t t))) | |
132 | |
133 (defun cl-const-exprs-p (xs) | |
134 (while (and xs (cl-const-expr-p (car xs))) | |
135 (setq xs (cdr xs))) | |
136 (not xs)) | |
137 | |
138 (defun cl-const-expr-val (x) | |
139 (and (eq (cl-const-expr-p x) t) (if (consp x) (nth 1 x) x))) | |
140 | |
141 (defun cl-expr-access-order (x v) | |
142 (if (cl-const-expr-p x) v | |
143 (if (consp x) | |
144 (progn | |
145 (while (setq x (cdr x)) (setq v (cl-expr-access-order (car x) v))) | |
146 v) | |
147 (if (eq x (car v)) (cdr v) '(t))))) | |
148 | |
149 ;;; Count number of times X refers to Y. Return nil for 0 times. | |
150 (defun cl-expr-contains (x y) | |
151 (cond ((equal y x) 1) | |
152 ((and (consp x) (not (memq (car-safe x) '(quote function function*)))) | |
153 (let ((sum 0)) | |
154 (while x | |
155 (setq sum (+ sum (or (cl-expr-contains (pop x) y) 0)))) | |
156 (and (> sum 0) sum))) | |
157 (t nil))) | |
158 | |
159 (defun cl-expr-contains-any (x y) | |
160 (while (and y (not (cl-expr-contains x (car y)))) (pop y)) | |
161 y) | |
162 | |
163 ;;; Check whether X may depend on any of the symbols in Y. | |
164 (defun cl-expr-depends-p (x y) | |
165 (and (not (cl-const-expr-p x)) | |
166 (or (not (cl-safe-expr-p x)) (cl-expr-contains-any x y)))) | |
167 | |
168 ;;; Symbols. | |
169 | |
170 (defvar *gensym-counter*) | |
171 | |
172 ;; XEmacs change: gensym and gentemp have been moved to cl.el. | |
173 | |
174 | |
428 | 175 ;;; Program structure. |
176 | |
177 ;;;###autoload | |
178 (defmacro defun* (name args &rest body) | |
179 "(defun* NAME ARGLIST [DOCSTRING] BODY...): define NAME as a function. | |
180 Like normal `defun', except ARGLIST allows full Common Lisp conventions, | |
872 | 181 and BODY is implicitly surrounded by (block NAME ...). |
182 | |
183 \"Full Common Lisp conventions\" means that: | |
184 | |
185 -- In addition to &optional and &rest, the lambda-list keywords &key, | |
186 &allow-other-keys, and &aux are allowed. | |
187 | |
188 -- The format of the arguments to &optional is expanded: As well as simple | |
189 variables, they can be lists of the form (VAR [INITFORM [SVAR]]); when | |
190 no argument is available for VAR, INITFORM is evaluated (or nil, if | |
191 INITFORM is omitted) and stored as VAR's value, and SVAR is bound to t. | |
192 If an arguent is available for VAR, and INITFORM is unused, SVAR is | |
193 bound to nil. | |
194 | |
195 -- &key specifies keyword arguments. The format of each argument is | |
196 VAR || ( { VAR || (KEYWORD VAR) } [INITFORM [SVAR]]) -- #### document me. | |
197 | |
198 -- &allow-other-keys means that if other keyword arguments are given that are | |
199 not specifically list in the arg list, they are allowed, rather than an | |
200 error being signalled. They can be retrieved with an &rest form. | |
201 | |
202 -- &aux specifies extra bindings, exactly like a `let*' enclosing the body. | |
203 The format of each binding is VAR || (VAR [INITFORM]) -- exactly like the | |
204 format of `let'/`let*' bindings. | |
205 " | |
428 | 206 (let* ((res (cl-transform-lambda (cons args body) name)) |
207 (form (list* 'defun name (cdr res)))) | |
208 (if (car res) (list 'progn (car res) form) form))) | |
209 | |
210 ;;;###autoload | |
211 (defmacro defmacro* (name args &rest body) | |
212 "(defmacro* NAME ARGLIST [DOCSTRING] BODY...): define NAME as a macro. | |
213 Like normal `defmacro', except ARGLIST allows full Common Lisp conventions, | |
872 | 214 and BODY is implicitly surrounded by (block NAME ...). |
215 | |
216 \"Full Common Lisp conventions\" means that: | |
217 | |
218 -- The lambda-list keywords &optional, &rest, &key, &allow-other-keys, and | |
219 &aux are allowed, as in `defun*'. | |
220 | |
221 -- Three additional lambda-list keywords are allowed: &body, &whole, and | |
222 &environment. #### Document me. | |
223 | |
224 -- The macro arg list syntax allows for \"destructuring\" -- see also | |
225 `destructuring-bind', which destructures exactly like `defmacro*', and | |
226 `loop', which does a rather different way of destructuring. Anywhere | |
227 that a simple argument may appear, and (if following a lambda-list | |
228 keyword) a list may not normally appear, an embedded lambda list can be | |
229 substituted. (The format of the embedded lambda list is exactly the | |
230 same as for a top-level list except that &environment is not allowed.) | |
231 When matching this lambda list against a caller-specified argument, that | |
232 argument is treated as a list and normal lambda-list processing occurs, | |
233 just as if the entire operation were happening at top level. | |
234 Furthermore, any lambda list, embedded or top-level, can be dotted at its | |
235 end, and this will cause matching with an appropriate dotted list given | |
236 as an argument. | |
237 | |
238 See `loop' for practical examples of destructuring, but | |
239 keep in mind that `loop' destructuring is somewhat different from macro | |
240 destructuring in that | |
241 | |
242 (a) Macro destructuring has extra features in the various lambda-list | |
243 keywords, allowing for special processing of a list other than just | |
244 simple matching. | |
245 (b) Macro destructuring is strict, in that an error is signalled if the | |
246 actual structure does not match the expected structure. On the | |
247 other hand, loop destructuring is lax -- extra arguments in a list | |
248 are ignored, not enough arguments cause the remaining parameters to | |
249 receive a value of nil, etc. | |
250 " | |
428 | 251 (let* ((res (cl-transform-lambda (cons args body) name)) |
252 (form (list* 'defmacro name (cdr res)))) | |
253 (if (car res) (list 'progn (car res) form) form))) | |
254 | |
255 ;;;###autoload | |
256 (defmacro function* (func) | |
257 "(function* SYMBOL-OR-LAMBDA): introduce a function. | |
258 Like normal `function', except that if argument is a lambda form, its | |
259 ARGLIST allows full Common Lisp conventions." | |
260 (if (eq (car-safe func) 'lambda) | |
261 (let* ((res (cl-transform-lambda (cdr func) 'cl-none)) | |
262 (form (list 'function (cons 'lambda (cdr res))))) | |
263 (if (car res) (list 'progn (car res) form) form)) | |
264 (list 'function func))) | |
265 | |
266 (defun cl-transform-function-property (func prop form) | |
267 (let ((res (cl-transform-lambda form func))) | |
268 (append '(progn) (cdr (cdr (car res))) | |
269 (list (list 'put (list 'quote func) (list 'quote prop) | |
270 (list 'function (cons 'lambda (cdr res)))))))) | |
271 | |
272 (defconst lambda-list-keywords | |
273 '(&optional &rest &key &allow-other-keys &aux &whole &body &environment)) | |
274 | |
275 (defvar cl-macro-environment nil) | |
276 (defvar bind-block) (defvar bind-defs) (defvar bind-enquote) | |
277 (defvar bind-inits) (defvar bind-lets) (defvar bind-forms) | |
278 | |
452 | 279 ;; npak@ispras.ru |
280 (defun cl-upcase-arg (arg) | |
1580 | 281 ;; Changes all non-keyword symbols in `ARG' to symbols |
452 | 282 ;; with name in upper case. |
1580 | 283 ;; ARG is either symbol or list of symbols or lists |
452 | 284 (cond ((symbolp arg) |
1580 | 285 ;; Do not upcase &optional, &key etc. |
4694
2ac296807b88
Don't needlessly intern symbols, #'function-arglist, #'cl-function-arglist
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
286 (if (memq arg lambda-list-keywords) |
2ac296807b88
Don't needlessly intern symbols, #'function-arglist, #'cl-function-arglist
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
287 arg |
2ac296807b88
Don't needlessly intern symbols, #'function-arglist, #'cl-function-arglist
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
288 (make-symbol (upcase (symbol-name arg))))) |
1580 | 289 ((listp arg) |
290 (let ((arg (copy-list arg)) junk) | |
291 ;; Clean the list | |
292 (let ((p (last arg))) (if (cdr p) (setcdr p (list '&rest (cdr p))))) | |
293 (if (setq junk (cadr (memq '&cl-defs arg))) | |
294 (setq arg (delq '&cl-defs (delq junk arg)))) | |
295 (if (memq '&cl-quote arg) | |
296 (setq arg (delq '&cl-quote arg))) | |
297 (mapcar 'cl-upcase-arg arg))) | |
298 (t arg))) ; Maybe we are in initializer | |
452 | 299 |
5076
d555581e3cba
fix issues with display of argument docstrings
Ben Wing <ben@xemacs.org>
parents:
5072
diff
changeset
|
300 ;; npak@ispras.ru, modified by ben@666.com |
4702
eb1a409c317b
Unbreak autoload.el
Mike Sperber <sperber@deinprogramm.de>
parents:
4695
diff
changeset
|
301 ;;;###autoload |
5076
d555581e3cba
fix issues with display of argument docstrings
Ben Wing <ben@xemacs.org>
parents:
5072
diff
changeset
|
302 (defun cl-function-arglist (arglist) |
452 | 303 "Returns string with printed representation of arguments list. |
304 Supports Common Lisp lambda lists." | |
1580 | 305 (if (not (or (listp arglist) (symbolp arglist))) |
306 "Not available" | |
4694
2ac296807b88
Don't needlessly intern symbols, #'function-arglist, #'cl-function-arglist
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
307 (check-argument-type #'true-list-p arglist) |
2ac296807b88
Don't needlessly intern symbols, #'function-arglist, #'cl-function-arglist
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
308 (let ((print-gensym nil)) |
2ac296807b88
Don't needlessly intern symbols, #'function-arglist, #'cl-function-arglist
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
309 (condition-case nil |
5076
d555581e3cba
fix issues with display of argument docstrings
Ben Wing <ben@xemacs.org>
parents:
5072
diff
changeset
|
310 (let ((args (cond ((null arglist) nil) |
d555581e3cba
fix issues with display of argument docstrings
Ben Wing <ben@xemacs.org>
parents:
5072
diff
changeset
|
311 ((listp arglist) (cl-upcase-arg arglist)) |
d555581e3cba
fix issues with display of argument docstrings
Ben Wing <ben@xemacs.org>
parents:
5072
diff
changeset
|
312 ((symbolp arglist) |
d555581e3cba
fix issues with display of argument docstrings
Ben Wing <ben@xemacs.org>
parents:
5072
diff
changeset
|
313 (cl-upcase-arg (list '&rest arglist))) |
d555581e3cba
fix issues with display of argument docstrings
Ben Wing <ben@xemacs.org>
parents:
5072
diff
changeset
|
314 (t (wrong-type-argument 'listp arglist))))) |
d555581e3cba
fix issues with display of argument docstrings
Ben Wing <ben@xemacs.org>
parents:
5072
diff
changeset
|
315 (if args (prin1-to-string args) "()")) |
5070
b0f4adffca7d
fix so that CL docstrings (with &key, etc.) handled properly
Ben Wing <ben@xemacs.org>
parents:
4997
diff
changeset
|
316 (t "Not available"))))) |
452 | 317 |
428 | 318 (defun cl-transform-lambda (form bind-block) |
319 (let* ((args (car form)) (body (cdr form)) | |
320 (bind-defs nil) (bind-enquote nil) | |
321 (bind-inits nil) (bind-lets nil) (bind-forms nil) | |
452 | 322 (header nil) (simple-args nil) |
5076
d555581e3cba
fix issues with display of argument docstrings
Ben Wing <ben@xemacs.org>
parents:
5072
diff
changeset
|
323 (complex-arglist (cl-function-arglist args)) |
452 | 324 (doc "")) |
428 | 325 (while (or (stringp (car body)) (eq (car-safe (car body)) 'interactive)) |
2153 | 326 (push (pop body) header)) |
428 | 327 (setq args (if (listp args) (copy-list args) (list '&rest args))) |
328 (let ((p (last args))) (if (cdr p) (setcdr p (list '&rest (cdr p))))) | |
329 (if (setq bind-defs (cadr (memq '&cl-defs args))) | |
330 (setq args (delq '&cl-defs (delq bind-defs args)) | |
331 bind-defs (cadr bind-defs))) | |
332 (if (setq bind-enquote (memq '&cl-quote args)) | |
333 (setq args (delq '&cl-quote args))) | |
334 (if (memq '&whole args) (error "&whole not currently implemented")) | |
335 (let* ((p (memq '&environment args)) (v (cadr p))) | |
336 (if p (setq args (nconc (delq (car p) (delq v args)) | |
337 (list '&aux (list v 'cl-macro-environment)))))) | |
338 (while (and args (symbolp (car args)) | |
339 (not (memq (car args) '(nil &rest &body &key &aux))) | |
340 (not (and (eq (car args) '&optional) | |
341 (or bind-defs (consp (cadr args)))))) | |
2153 | 342 (push (pop args) simple-args)) |
428 | 343 (or (eq bind-block 'cl-none) |
344 (setq body (list (list* 'block bind-block body)))) | |
4694
2ac296807b88
Don't needlessly intern symbols, #'function-arglist, #'cl-function-arglist
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
345 (setq simple-args (nreverse simple-args) |
2ac296807b88
Don't needlessly intern symbols, #'function-arglist, #'cl-function-arglist
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
346 header (nreverse header)) |
2ac296807b88
Don't needlessly intern symbols, #'function-arglist, #'cl-function-arglist
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
347 ;; Add CL lambda list to documentation, if the CL lambda list differs |
2ac296807b88
Don't needlessly intern symbols, #'function-arglist, #'cl-function-arglist
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
348 ;; from the non-CL lambda list. npak@ispras.ru |
2ac296807b88
Don't needlessly intern symbols, #'function-arglist, #'cl-function-arglist
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
349 (unless (equal complex-arglist |
5076
d555581e3cba
fix issues with display of argument docstrings
Ben Wing <ben@xemacs.org>
parents:
5072
diff
changeset
|
350 (cl-function-arglist simple-args)) |
4694
2ac296807b88
Don't needlessly intern symbols, #'function-arglist, #'cl-function-arglist
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
351 (and (stringp (car header)) (setq doc (pop header))) |
5070
b0f4adffca7d
fix so that CL docstrings (with &key, etc.) handled properly
Ben Wing <ben@xemacs.org>
parents:
4997
diff
changeset
|
352 ;; Stick the arguments onto the end of the doc string in a way that |
b0f4adffca7d
fix so that CL docstrings (with &key, etc.) handled properly
Ben Wing <ben@xemacs.org>
parents:
4997
diff
changeset
|
353 ;; will be recognized specially by `function-arglist'. |
b0f4adffca7d
fix so that CL docstrings (with &key, etc.) handled properly
Ben Wing <ben@xemacs.org>
parents:
4997
diff
changeset
|
354 (push (concat doc "\n\narguments: " complex-arglist "\n") |
b0f4adffca7d
fix so that CL docstrings (with &key, etc.) handled properly
Ben Wing <ben@xemacs.org>
parents:
4997
diff
changeset
|
355 header)) |
428 | 356 (if (null args) |
4694
2ac296807b88
Don't needlessly intern symbols, #'function-arglist, #'cl-function-arglist
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
357 (list* nil simple-args (nconc header body)) |
2153 | 358 (if (memq '&optional simple-args) (push '&optional args)) |
428 | 359 (cl-do-arglist args nil (- (length simple-args) |
360 (if (memq '&optional simple-args) 1 0))) | |
361 (setq bind-lets (nreverse bind-lets)) | |
362 (list* (and bind-inits (list* 'eval-when '(compile load eval) | |
363 (nreverse bind-inits))) | |
4694
2ac296807b88
Don't needlessly intern symbols, #'function-arglist, #'cl-function-arglist
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
364 (nconc simple-args |
2153 | 365 (list '&rest (car (pop bind-lets)))) |
366 ;; XEmacs change: we add usage information using Nickolay's | |
367 ;; approach above | |
4694
2ac296807b88
Don't needlessly intern symbols, #'function-arglist, #'cl-function-arglist
Aidan Kehoe <kehoea@parhasard.net>
parents:
4677
diff
changeset
|
368 (nconc header |
428 | 369 (list (nconc (list 'let* bind-lets) |
370 (nreverse bind-forms) body))))))) | |
371 | |
372 (defun cl-do-arglist (args expr &optional num) ; uses bind-* | |
373 (if (nlistp args) | |
374 (if (or (memq args lambda-list-keywords) (not (symbolp args))) | |
375 (error "Invalid argument name: %s" args) | |
2153 | 376 (push (list args expr) bind-lets)) |
428 | 377 (setq args (copy-list args)) |
378 (let ((p (last args))) (if (cdr p) (setcdr p (list '&rest (cdr p))))) | |
379 (let ((p (memq '&body args))) (if p (setcar p '&rest))) | |
380 (if (memq '&environment args) (error "&environment used incorrectly")) | |
381 (let ((save-args args) | |
382 (restarg (memq '&rest args)) | |
383 (safety (if (cl-compiling-file) cl-optimize-safety 3)) | |
384 (keys nil) | |
385 (laterarg nil) (exactarg nil) minarg) | |
386 (or num (setq num 0)) | |
387 (if (listp (cadr restarg)) | |
388 (setq restarg (gensym "--rest--")) | |
389 (setq restarg (cadr restarg))) | |
2153 | 390 (push (list restarg expr) bind-lets) |
428 | 391 (if (eq (car args) '&whole) |
2153 | 392 (push (list (cl-pop2 args) restarg) bind-lets)) |
428 | 393 (let ((p args)) |
394 (setq minarg restarg) | |
395 (while (and p (not (memq (car p) lambda-list-keywords))) | |
396 (or (eq p args) (setq minarg (list 'cdr minarg))) | |
397 (setq p (cdr p))) | |
398 (if (memq (car p) '(nil &aux)) | |
399 (setq minarg (list '= (list 'length restarg) | |
400 (length (ldiff args p))) | |
401 exactarg (not (eq args p))))) | |
402 (while (and args (not (memq (car args) lambda-list-keywords))) | |
403 (let ((poparg (list (if (or (cdr args) (not exactarg)) 'pop 'car) | |
404 restarg))) | |
405 (cl-do-arglist | |
2153 | 406 (pop args) |
428 | 407 (if (or laterarg (= safety 0)) poparg |
408 (list 'if minarg poparg | |
409 (list 'signal '(quote wrong-number-of-arguments) | |
410 (list 'list (and (not (eq bind-block 'cl-none)) | |
411 (list 'quote bind-block)) | |
412 (list 'length restarg))))))) | |
413 (setq num (1+ num) laterarg t)) | |
2153 | 414 (while (and (eq (car args) '&optional) (pop args)) |
428 | 415 (while (and args (not (memq (car args) lambda-list-keywords))) |
2153 | 416 (let ((arg (pop args))) |
428 | 417 (or (consp arg) (setq arg (list arg))) |
418 (if (cddr arg) (cl-do-arglist (nth 2 arg) (list 'and restarg t))) | |
419 (let ((def (if (cdr arg) (nth 1 arg) | |
420 (or (car bind-defs) | |
421 (nth 1 (assq (car arg) bind-defs))))) | |
422 (poparg (list 'pop restarg))) | |
423 (and def bind-enquote (setq def (list 'quote def))) | |
424 (cl-do-arglist (car arg) | |
425 (if def (list 'if restarg poparg def) poparg)) | |
426 (setq num (1+ num)))))) | |
427 (if (eq (car args) '&rest) | |
428 (let ((arg (cl-pop2 args))) | |
429 (if (consp arg) (cl-do-arglist arg restarg))) | |
430 (or (eq (car args) '&key) (= safety 0) exactarg | |
2153 | 431 (push (list 'if restarg |
428 | 432 (list 'signal '(quote wrong-number-of-arguments) |
433 (list 'list | |
434 (and (not (eq bind-block 'cl-none)) | |
435 (list 'quote bind-block)) | |
436 (list '+ num (list 'length restarg))))) | |
437 bind-forms))) | |
2153 | 438 (while (and (eq (car args) '&key) (pop args)) |
428 | 439 (while (and args (not (memq (car args) lambda-list-keywords))) |
2153 | 440 (let ((arg (pop args))) |
428 | 441 (or (consp arg) (setq arg (list arg))) |
4793
8b50bee3c88c
Remove attempted support for 1996-era emacs without self-quoting keywords.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
442 (let* ((karg (if (consp (car arg)) |
8b50bee3c88c
Remove attempted support for 1996-era emacs without self-quoting keywords.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
443 ;; It's possible to use non-keywords here, as |
8b50bee3c88c
Remove attempted support for 1996-era emacs without self-quoting keywords.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
444 ;; in the KEYWORD-ARGUMENT-NAME-PACKAGE Common |
8b50bee3c88c
Remove attempted support for 1996-era emacs without self-quoting keywords.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
445 ;; Lisp issue: |
8b50bee3c88c
Remove attempted support for 1996-era emacs without self-quoting keywords.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
446 (caar arg) |
8b50bee3c88c
Remove attempted support for 1996-era emacs without self-quoting keywords.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
447 ;; Use read instead of intern in case we ever |
8b50bee3c88c
Remove attempted support for 1996-era emacs without self-quoting keywords.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
448 ;; actually get packages and keywords are no |
8b50bee3c88c
Remove attempted support for 1996-era emacs without self-quoting keywords.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
449 ;; longer in obarray: |
8b50bee3c88c
Remove attempted support for 1996-era emacs without self-quoting keywords.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
450 (read (concat ":" (symbol-name (car arg)))))) |
428 | 451 (varg (if (consp (car arg)) (cadar arg) (car arg))) |
452 (def (if (cdr arg) (cadr arg) | |
453 (or (car bind-defs) (cadr (assq varg bind-defs))))) | |
4793
8b50bee3c88c
Remove attempted support for 1996-era emacs without self-quoting keywords.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
454 (look (list 'memq (quote-maybe karg) restarg))) |
428 | 455 (and def bind-enquote (setq def (list 'quote def))) |
456 (if (cddr arg) | |
457 (let* ((temp (or (nth 2 arg) (gensym))) | |
458 (val (list 'car (list 'cdr temp)))) | |
459 (cl-do-arglist temp look) | |
460 (cl-do-arglist varg | |
461 (list 'if temp | |
462 (list 'prog1 val (list 'setq temp t)) | |
463 def))) | |
464 (cl-do-arglist | |
465 varg | |
466 (list 'car | |
467 (list 'cdr | |
468 (if (null def) | |
469 look | |
470 (list 'or look | |
471 (if (eq (cl-const-expr-p def) t) | |
472 (list | |
473 'quote | |
474 (list nil (cl-const-expr-val def))) | |
475 (list 'list nil def)))))))) | |
4793
8b50bee3c88c
Remove attempted support for 1996-era emacs without self-quoting keywords.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
476 (push karg keys))))) |
428 | 477 (setq keys (nreverse keys)) |
2153 | 478 (or (and (eq (car args) '&allow-other-keys) (pop args)) |
428 | 479 (null keys) (= safety 0) |
480 (let* ((var (gensym "--keys--")) | |
481 (allow '(:allow-other-keys)) | |
482 (check (list | |
483 'while var | |
484 (list | |
485 'cond | |
486 (list (list 'memq (list 'car var) | |
487 (list 'quote (append keys allow))) | |
488 (list 'setq var (list 'cdr (list 'cdr var)))) | |
489 (list (list 'car | |
490 (list 'cdr | |
4793
8b50bee3c88c
Remove attempted support for 1996-era emacs without self-quoting keywords.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
491 (list 'memq (car allow) |
428 | 492 restarg))) |
493 (list 'setq var nil)) | |
494 (list t | |
495 (list | |
496 'error | |
497 (format "Keyword argument %%s not one of %s" | |
498 keys) | |
499 (list 'car var))))))) | |
2153 | 500 (push (list 'let (list (list var restarg)) check) bind-forms))) |
501 (while (and (eq (car args) '&aux) (pop args)) | |
428 | 502 (while (and args (not (memq (car args) lambda-list-keywords))) |
503 (if (consp (car args)) | |
504 (if (and bind-enquote (cadar args)) | |
505 (cl-do-arglist (caar args) | |
2153 | 506 (list 'quote (cadr (pop args)))) |
507 (cl-do-arglist (caar args) (cadr (pop args)))) | |
508 (cl-do-arglist (pop args) nil)))) | |
428 | 509 (if args (error "Malformed argument list %s" save-args))))) |
510 | |
511 (defun cl-arglist-args (args) | |
512 (if (nlistp args) (list args) | |
513 (let ((res nil) (kind nil) arg) | |
514 (while (consp args) | |
2153 | 515 (setq arg (pop args)) |
428 | 516 (if (memq arg lambda-list-keywords) (setq kind arg) |
2153 | 517 (if (eq arg '&cl-defs) (pop args) |
428 | 518 (and (consp arg) kind (setq arg (car arg))) |
519 (and (consp arg) (cdr arg) (eq kind '&key) (setq arg (cadr arg))) | |
520 (setq res (nconc res (cl-arglist-args arg)))))) | |
521 (nconc res (and args (list args)))))) | |
522 | |
523 ;;;###autoload | |
524 (defmacro destructuring-bind (args expr &rest body) | |
872 | 525 "Bind the arguments in ARGS to EXPR then eval BODY. |
526 This is similar to `let' but it does \"destructuring\", in that it matches | |
527 the structure of ARGS to the structure of EXPR and binds corresponding | |
528 arguments in ARGS to their values in EXPR. The format of ARGS, and the | |
529 way the destructuring works, is exactly like the destructuring that occurs | |
530 in `defmacro*'; see that for more information. | |
531 | |
532 An alternative means of destructuring is using the `loop' macro. `loop' | |
533 gives practical examples of destructuring. `defmacro*' describes the | |
534 differences between loop and macro-style destructuring. | |
535 | |
536 You can rewrite a call to (destructuring-bind ARGS EXPR &rest BODY) using | |
537 `loop', approximately like this: | |
538 | |
539 (loop for ARGS = EXPR | |
540 return (progn BODY)) | |
541 | |
542 I say \"approximately\" because the destructuring works in a somewhat | |
543 different fashion, although for most reasonably simple constructs the | |
544 results will be the same." | |
428 | 545 (let* ((bind-lets nil) (bind-forms nil) (bind-inits nil) |
546 (bind-defs nil) (bind-block 'cl-none)) | |
547 (cl-do-arglist (or args '(&aux)) expr) | |
548 (append '(progn) bind-inits | |
549 (list (nconc (list 'let* (nreverse bind-lets)) | |
550 (nreverse bind-forms) body))))) | |
551 | |
552 | |
553 ;;; The `eval-when' form. | |
554 | |
555 (defvar cl-not-toplevel nil) | |
556 | |
557 ;;;###autoload | |
558 (defmacro eval-when (when &rest body) | |
559 "(eval-when (WHEN...) BODY...): control when BODY is evaluated. | |
560 If `compile' is in WHEN, BODY is evaluated when compiled at top-level. | |
561 If `load' is in WHEN, BODY is evaluated when loaded after top-level compile. | |
562 If `eval' is in WHEN, BODY is evaluated when interpreted or at non-top-level." | |
563 (if (and (fboundp 'cl-compiling-file) (cl-compiling-file) | |
564 (not cl-not-toplevel) (not (boundp 'for-effect))) ; horrible kludge | |
2153 | 565 (let ((comp (or (memq 'compile when) (memq :compile-toplevel when))) |
428 | 566 (cl-not-toplevel t)) |
2153 | 567 (if (or (memq 'load when) (memq :load-toplevel when)) |
428 | 568 (if comp (cons 'progn (mapcar 'cl-compile-time-too body)) |
569 (list* 'if nil nil body)) | |
570 (progn (if comp (eval (cons 'progn body))) nil))) | |
2153 | 571 (and (or (memq 'eval when) (memq :execute when)) |
428 | 572 (cons 'progn body)))) |
573 | |
574 (defun cl-compile-time-too (form) | |
575 (or (and (symbolp (car-safe form)) (get (car-safe form) 'byte-hunk-handler)) | |
576 (setq form (macroexpand | |
577 form (cons '(eval-when) byte-compile-macro-environment)))) | |
578 (cond ((eq (car-safe form) 'progn) | |
579 (cons 'progn (mapcar 'cl-compile-time-too (cdr form)))) | |
580 ((eq (car-safe form) 'eval-when) | |
581 (let ((when (nth 1 form))) | |
2153 | 582 (if (or (memq 'eval when) (memq :execute when)) |
428 | 583 (list* 'eval-when (cons 'compile when) (cddr form)) |
584 form))) | |
585 (t (eval form) form))) | |
586 | |
587 ;;;###autoload | |
588 (defmacro load-time-value (form &optional read-only) | |
589 "Like `progn', but evaluates the body at load time. | |
590 The result of the body appears to the compiler as a quoted constant." | |
591 (if (cl-compiling-file) | |
592 (let* ((temp (gentemp "--cl-load-time--")) | |
593 (set (list 'set (list 'quote temp) form))) | |
594 (if (and (fboundp 'byte-compile-file-form-defmumble) | |
595 (boundp 'this-kind) (boundp 'that-one)) | |
596 (fset 'byte-compile-file-form | |
597 (list 'lambda '(form) | |
598 (list 'fset '(quote byte-compile-file-form) | |
599 (list 'quote | |
600 (symbol-function 'byte-compile-file-form))) | |
601 (list 'byte-compile-file-form (list 'quote set)) | |
602 '(byte-compile-file-form form))) | |
603 ;; XEmacs change | |
604 (print set (symbol-value ;;'outbuffer | |
605 'byte-compile-output-buffer | |
606 ))) | |
607 (list 'symbol-value (list 'quote temp))) | |
608 (list 'quote (eval form)))) | |
609 | |
610 | |
611 ;;; Conditional control structures. | |
612 | |
613 ;;;###autoload | |
614 (defmacro case (expr &rest clauses) | |
4695
fee33ab25966
Add arglist info for autoloaded functions and macros.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4694
diff
changeset
|
615 "Evals EXPR, chooses from CLAUSES on that value. |
428 | 616 Each clause looks like (KEYLIST BODY...). EXPR is evaluated and compared |
617 against each key in each KEYLIST; the corresponding BODY is evaluated. | |
618 If no clause succeeds, case returns nil. A single atom may be used in | |
619 place of a KEYLIST of one atom. A KEYLIST of `t' or `otherwise' is | |
620 allowed only in the final clause, and matches if no other keys match. | |
621 Key values are compared by `eql'." | |
622 (let* ((temp (if (cl-simple-expr-p expr 3) expr (gensym))) | |
623 (head-list nil) | |
624 (last-clause (car (last clauses))) | |
625 (body (cons | |
626 'cond | |
627 (mapcar | |
628 #'(lambda (c) | |
629 (cons (cond ((memq (car c) '(t otherwise)) | |
2153 | 630 ;; XEmacs addition: check for last clause |
428 | 631 (or (eq c last-clause) |
632 (error | |
633 "`%s' is allowed only as the last case clause" | |
634 (car c))) | |
635 t) | |
636 ((eq (car c) 'ecase-error-flag) | |
637 (list 'error "ecase failed: %s, %s" | |
638 temp (list 'quote (reverse head-list)))) | |
639 ((listp (car c)) | |
640 (setq head-list (append (car c) head-list)) | |
641 (list 'member* temp (list 'quote (car c)))) | |
642 (t | |
643 (if (memq (car c) head-list) | |
644 (error "Duplicate key in case: %s" | |
645 (car c))) | |
2153 | 646 (push (car c) head-list) |
428 | 647 (list 'eql temp (list 'quote (car c))))) |
648 (or (cdr c) '(nil)))) | |
649 clauses)))) | |
650 (if (eq temp expr) body | |
651 (list 'let (list (list temp expr)) body)))) | |
652 | |
653 ;; #### CL standard also requires `ccase', which signals a continuable | |
654 ;; error (`cerror' in XEmacs). However, I don't think it buys us | |
655 ;; anything to introduce it, as there is probably much more CL stuff | |
656 ;; missing, and the feature is not essential. --hniksic | |
657 | |
658 ;;;###autoload | |
659 (defmacro ecase (expr &rest clauses) | |
4695
fee33ab25966
Add arglist info for autoloaded functions and macros.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4694
diff
changeset
|
660 "Like `case', but error if no case fits. |
428 | 661 `otherwise'-clauses are not allowed." |
2153 | 662 ;; XEmacs addition: disallow t and otherwise |
428 | 663 (let ((disallowed (or (assq t clauses) |
664 (assq 'otherwise clauses)))) | |
665 (if disallowed | |
666 (error "`%s' is not allowed in ecase" (car disallowed)))) | |
667 (list* 'case expr (append clauses '((ecase-error-flag))))) | |
668 | |
669 ;;;###autoload | |
670 (defmacro typecase (expr &rest clauses) | |
4695
fee33ab25966
Add arglist info for autoloaded functions and macros.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4694
diff
changeset
|
671 "Evals EXPR, chooses from CLAUSES on that value. |
428 | 672 Each clause looks like (TYPE BODY...). EXPR is evaluated and, if it |
673 satisfies TYPE, the corresponding BODY is evaluated. If no clause succeeds, | |
674 typecase returns nil. A TYPE of `t' or `otherwise' is allowed only in the | |
675 final clause, and matches if no other keys match." | |
676 (let* ((temp (if (cl-simple-expr-p expr 3) expr (gensym))) | |
677 (type-list nil) | |
678 (body (cons | |
679 'cond | |
680 (mapcar | |
681 #'(lambda (c) | |
682 (cons (cond ((eq (car c) 'otherwise) t) | |
683 ((eq (car c) 'ecase-error-flag) | |
684 (list 'error "etypecase failed: %s, %s" | |
685 temp (list 'quote (reverse type-list)))) | |
686 (t | |
2153 | 687 (push (car c) type-list) |
428 | 688 (cl-make-type-test temp (car c)))) |
689 (or (cdr c) '(nil)))) | |
690 clauses)))) | |
691 (if (eq temp expr) body | |
692 (list 'let (list (list temp expr)) body)))) | |
693 | |
694 ;;;###autoload | |
695 (defmacro etypecase (expr &rest clauses) | |
4695
fee33ab25966
Add arglist info for autoloaded functions and macros.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4694
diff
changeset
|
696 "Like `typecase', but error if no case fits. |
428 | 697 `otherwise'-clauses are not allowed." |
698 (list* 'typecase expr (append clauses '((ecase-error-flag))))) | |
699 | |
700 | |
701 ;;; Blocks and exits. | |
702 | |
703 ;;;###autoload | |
704 (defmacro block (name &rest body) | |
4695
fee33ab25966
Add arglist info for autoloaded functions and macros.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4694
diff
changeset
|
705 "Define a lexically-scoped block named NAME. |
428 | 706 NAME may be any symbol. Code inside the BODY forms can call `return-from' |
707 to jump prematurely out of the block. This differs from `catch' and `throw' | |
708 in two respects: First, the NAME is an unevaluated symbol rather than a | |
709 quoted symbol or other form; and second, NAME is lexically rather than | |
710 dynamically scoped: Only references to it within BODY will work. These | |
711 references may appear inside macro expansions, but not inside functions | |
712 called from BODY." | |
713 (if (cl-safe-expr-p (cons 'progn body)) (cons 'progn body) | |
714 (list 'cl-block-wrapper | |
715 (list* 'catch (list 'quote (intern (format "--cl-block-%s--" name))) | |
716 body)))) | |
717 | |
718 (defvar cl-active-block-names nil) | |
719 | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
720 (put 'cl-block-wrapper 'byte-compile |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
721 #'(lambda (cl-form) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
722 (if (/= (length cl-form) 2) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
723 (byte-compile-warn-wrong-args cl-form 1)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
724 |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
725 (if (fboundp 'byte-compile-form-do-effect) ; Check for optimizing |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
726 ; compiler |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
727 (progn |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
728 (let* ((cl-entry (cons (nth 1 (nth 1 (nth 1 cl-form))) nil)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
729 (cl-active-block-names (cons cl-entry |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
730 cl-active-block-names)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
731 (cl-body (byte-compile-top-level |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
732 (cons 'progn (cddr (nth 1 cl-form)))))) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
733 (if (cdr cl-entry) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
734 (byte-compile-form (list 'catch (nth 1 (nth 1 cl-form)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
735 cl-body)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
736 (byte-compile-form cl-body)))) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
737 (byte-compile-form (nth 1 cl-form))))) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
738 |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
739 (put 'cl-block-throw 'byte-compile |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
740 #'(lambda (cl-form) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
741 (let ((cl-found (assq (nth 1 (nth 1 cl-form)) cl-active-block-names))) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
742 (if cl-found (setcdr cl-found t))) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
743 (byte-compile-throw (cons 'throw (cdr cl-form))))) |
428 | 744 |
745 ;;;###autoload | |
2153 | 746 (defmacro return (&optional result) |
4695
fee33ab25966
Add arglist info for autoloaded functions and macros.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4694
diff
changeset
|
747 "Return from the block named nil. |
428 | 748 This is equivalent to `(return-from nil RESULT)'." |
2153 | 749 (list 'return-from nil result)) |
428 | 750 |
751 ;;;###autoload | |
2153 | 752 (defmacro return-from (name &optional result) |
4695
fee33ab25966
Add arglist info for autoloaded functions and macros.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4694
diff
changeset
|
753 "Return from the block named NAME. |
428 | 754 This jumps out to the innermost enclosing `(block NAME ...)' form, |
755 returning RESULT from that form (or nil if RESULT is omitted). | |
756 This is compatible with Common Lisp, but note that `defun' and | |
757 `defmacro' do not create implicit blocks as they do in Common Lisp." | |
758 (let ((name2 (intern (format "--cl-block-%s--" name)))) | |
2153 | 759 (list 'cl-block-throw (list 'quote name2) result))) |
428 | 760 |
761 | |
762 ;;; The "loop" macro. | |
763 | |
764 (defvar args) (defvar loop-accum-var) (defvar loop-accum-vars) | |
765 (defvar loop-bindings) (defvar loop-body) (defvar loop-destr-temps) | |
766 (defvar loop-finally) (defvar loop-finish-flag) (defvar loop-first-flag) | |
767 (defvar loop-initially) (defvar loop-map-form) (defvar loop-name) | |
768 (defvar loop-result) (defvar loop-result-explicit) | |
769 (defvar loop-result-var) (defvar loop-steps) (defvar loop-symbol-macs) | |
770 | |
771 ;;;###autoload | |
772 (defmacro loop (&rest args) | |
773 "(loop CLAUSE...): The Common Lisp `loop' macro. | |
800 | 774 |
775 The loop macro consists of a series of clauses, which do things like | |
776 iterate variables, set conditions for exiting the loop, accumulating values | |
777 to be returned as the return value of the loop, and executing arbitrary | |
2297 | 778 blocks of code. Each clause is processed in turn, and the loop executes its |
800 | 779 body repeatedly until an exit condition is hit. |
780 | |
781 It's important to understand that loop clauses such as `for' and `while', | |
782 which look like loop-establishing constructs, don't actually *establish* a | |
783 loop\; the looping is established by the `loop' clause itself, which will | |
784 repeatedly process its body until told to stop. `while' merely establishes | |
785 a condition which, when true, causes the loop to finish, and `for' sets a | |
786 variable to different values on each iteration (e.g. successive elements of | |
787 a list) and sets an exit condition when there are no more values. This | |
788 means, for example, that if two `for' clauses appear, you don't get two | |
789 nested loops, but instead two variables that are stepped in parallel, and | |
790 two exit conditions, either of which, if triggered, will cause the loop to | |
791 end. Similarly for a loop with a `for' and a `while' clause. For example: | |
792 | |
793 \(loop | |
794 for x in list | |
795 while x | |
796 do ...) | |
797 | |
798 In each successive iteration, X is set to the next element of the list. If | |
799 there are no more elements, or if any element is nil (the `while' clause), | |
800 the loop exits. Otherwise, the block of code following `do' is executed.) | |
801 | |
802 This example also shows that some clauses establish variable bindings -- | |
803 essentially like a `let' binding -- and that following clauses can | |
804 reference these variables. Furthermore, the entire loop is surrounded by a | |
805 block named nil (unless the `named' clause is given), so you can return | |
806 from the loop using the macro `return'. (The other way to exit the loop is | |
807 through the macro `loop-finish'. The difference is that some loop clauses | |
808 establish or accumulate a value to be returned, and `loop-finish' returns | |
809 this. `return', however, can only return an explicitly-specified value. | |
810 NOTE CAREFULLY: There is a loop clause called `return' as well as a | |
811 standard Lisp macro called `return'. Normally they work similarly\; but if | |
812 you give the loop a name with `named', you will need to use the macro | |
813 `return-from'.) | |
814 | |
815 Another extremely useful feature of loops is called \"destructuring\". If, | |
816 in place of VAR, a list (possibly dotted, possibly a tree of arbitary | |
817 complexity) is given, the value to be assigned is assumed to have a similar | |
818 structure to the list given, and variables in the list will be matched up | |
819 with corresponding elements in the structure. For example: | |
820 | |
821 \(loop | |
822 for (x y) in '((foo 1) (bar 2) (baz 3)) | |
823 do (puthash x y some-hash-table)) | |
824 | |
825 will add three elements to a hash table, mapping foo -> 1, bar -> 2, and | |
826 baz -> 3. As other examples, you can conveniently process alists using | |
827 | |
828 \(loop for (x . y) in alist do ...) | |
829 | |
830 and plists using | |
831 | |
832 \(loop for (x y) on plist by #'cddr do ...) | |
833 | |
834 Destructuring is forgiving in that mismatches in the number of elements on | |
835 either size will be handled gracefully, either by ignoring or initializing | |
1123 | 836 to nil. Destructuring is extremely powerful, and is probably the single |
837 most useful feature of `loop'. | |
838 | |
839 Other useful features of loops are iterating over hash-tables, collecting values into lists, and being able to modify lists in-place as you iterate over them. As an example of the first two, | |
840 | |
841 \(loop for x being the hash-key in table using (hash-value y) | |
842 collect (cons x y)) | |
843 | |
844 converts hash-table TABLE to an alist. (What `collect' actually does is | |
845 push its value onto the end of an internal list and establish this list as | |
846 the default return value of the loop. See below for more information.) | |
847 | |
848 An example of in-place modification is | |
849 | |
850 \(setq foo '(1 3 5)) | |
851 \(loop for x in-ref foo do | |
852 (setf x (* x x))) | |
853 | |
854 after which foo will contain '(1 9 25). | |
800 | 855 |
856 If you don't understand how a particular loop clause works, create an | |
857 example and use `macroexpand-sexp' to expand the macro. | |
858 | |
428 | 859 Valid clauses are: |
800 | 860 |
861 \(NOTE: Keywords in lowercase\; slashes separate different possibilities | |
862 for keywords, some of which are synonymous\; brackets indicate optional | |
863 parts of the clause. In all of the clauses with `being', the word `being', | |
864 the words `each' or `the', and the difference between singular and plural | |
865 keywords are all just syntactic sugar. Stylistically, you should write | |
866 either `being each foo' or `being the foos'.) | |
867 | |
868 for VAR from/upfrom/downfrom NUM1 to/upto/downto/above/below NUM2 [by NUMSTEP] | |
869 Step VAR across numbers. `upfrom', `upto', and `below' explicitly | |
870 indicate upward stepping\; `downfrom', `downto', and `above' explicitly | |
871 indicate downward stepping. (If none of these is given, the default is | |
872 upward.) `to', `upto', and `downto' cause stepping to include NUM2 as | |
873 the last iteration, while `above' and `below' stop just before reaching | |
874 NUM2. `by' can be given to indicate a stepping increment other than 1. | |
875 | |
876 for VAR in LIST [by FUNC] | |
877 Step VAR over elements of a LIST. FUNC specifies how to get successive | |
878 sublists and defaults to `cdr'. | |
879 | |
880 for VAR on LIST [by FUNC] | |
881 Step VAR over tails of a LIST. FUNC specifies how to get successive | |
882 sublists and defaults to `cdr'. | |
883 | |
884 for VAR in-ref LIST [by FUNC] | |
885 Step VAR over elements of a LIST, like `for ... in', except the VAR is | |
886 bound using `symbol-macrolet' instead of `let'. In essence, VAR is set | |
887 to a \"reference\" to the list element instead of the element itself\; | |
888 this us, you can destructively modify the list using `setf' on VAR, and | |
889 any changes to the list will \"magically\" reflect themselves in | |
890 subsequent uses of VAR. | |
891 | |
892 for VAR = INIT [then EXPR] | |
893 Set VAR on each iteration of the loop. If only INIT is given, use it | |
894 on each iteration. Otherwise, use INIT on the first iteration and EXPR | |
895 on subsequent ones. | |
896 | |
897 for VAR across/across-ref ARRAY | |
898 Step VAR across a sequence other than a list (string, vector, bit | |
899 vector). If `across-ref' is given, VAR is bound using | |
900 `symbol-macrolet' instead of `let' -- see above. | |
901 | |
902 for VAR being each/the element/elements in/of/in-ref/of-ref SEQUENCE [using (index INDEX-VAR)] | |
903 Step VAR across any sequence. A variable can be specified with a | |
904 `using' phrase to receive the index, starting at 0. If `in-ref' or | |
905 `of-ref' is given, VAR is bound using `symbol-macrolet' instead of | |
906 `let' -- see above. | |
907 | |
908 for VAR being each/the hash-key/hash-keys/hash-value/hash-values in/of HASH-TABLE [using (hash-value/hash-key OTHER-VAR)] | |
909 | |
910 for VAR being each/the hash-key/hash-keys/hash-value/hash-values in/of HASH-TABLE [using (hash-value/hash-key OTHER-VAR)] | |
911 Map VAR over a hash table. The various keywords are synonymous except | |
912 those that distinguish between keys and values. The `using' phrase is | |
913 optional and allows both key and value to be bound. | |
914 | |
915 for VAR being each/the symbol/present-symbol/external-symbol/symbols/present-symbols/external-symbols in/of OBARRAY | |
916 Map VAR over the symbols in an obarray. All symbol keywords are | |
917 currently synonymous. | |
918 | |
919 for VAR being each/the extent/extents [in/of BUFFER-OR-STRING] [from POS] [to POS] | |
920 Map VAR over the extents in a buffer or string, defaulting to the | |
921 current buffer, the beginning and the end, respectively. | |
922 | |
923 for VAR being each/the interval/intervals [in/of BUFFER-OR-STRING] [property PROPERTY] [from POS] [to POS] | |
924 Map VAR over the intervals without property change in a buffer or | |
925 string, defaulting to the current buffer, the beginning and the end, | |
926 respectively. If PROPERTY is given, iteration occurs using | |
927 `next-single-property-change'\; otherwise, using | |
928 `next-property-change'. | |
929 | |
930 for VAR being each/the window/windows [in/of FRAME] | |
931 Step VAR over the windows in FRAME, defaulting to the selected frame. | |
932 | |
933 for VAR being each/the frame/frames | |
934 Step VAR over all frames. | |
935 | |
936 for VAR being each/the buffer/buffers [by FUNC] | |
937 Step VAR over all buffers. This is actually equivalent to | |
938 `for VAR in (buffer-list) [by FUNC]'. | |
939 | |
940 for VAR being each/the key-code/key-codes/key-seq/key-seqs/key-binding/key-bindings in KEYMAP [using (key-code/key-codes/key-seq/key-seqs/key-binding/key-bindings OTHER-VAR)] | |
941 Map VAR over the entries in a keymap. Keyword `key-seq' causes | |
942 recursive mapping over prefix keymaps occurring in the keymap, with VAR | |
943 getting the built-up sequence (a vector). Otherwise, mapping does not | |
944 occur recursively. `key-code' and `key-seq' refer to what is bound | |
945 (second argument of `define-key'), and `key-binding' what it's bound to | |
946 (third argument of `define-key'). | |
947 | |
948 as VAR ... | |
949 `as' is a synonym for `for'. | |
950 | |
951 and VAR ... | |
952 `and' clauses have the same syntax as `for' clauses except that the | |
953 variables in the clause are bound in parallel with a preceding | |
954 `and'/`for' clause instead of in series. | |
955 | |
956 with VAR = INIT | |
957 Set VAR to INIT once, before doing any iterations. | |
958 | |
959 repeat NUM | |
960 Exit the loop if more than NUM iterations have occurred. | |
961 | |
962 while COND | |
963 Exit the loop if COND isn't true. | |
964 | |
965 until COND | |
966 Exit the loop if COND is true. | |
967 | |
968 collect EXPR [into VAR] | |
969 Push EXPR onto the end of a list of values -- stored either in VAR or a | |
970 temporary variable that will be returned as the return value of the | |
971 loop if it terminates through an exit condition or a call to | |
972 `loop-finish'. | |
973 | |
974 append EXPR [into VAR] | |
975 Append EXPR (a list) onto the end of a list of values, like `collect'. | |
976 | |
977 nconc EXPR [into VAR] | |
978 Nconc EXPR (a list) onto the end of a list of values, like `collect'. | |
979 | |
980 concat EXPR [into VAR] | |
981 Concatenate EXPR (a string) onto the end of a string of values, like | |
982 `collect'. | |
983 | |
984 vconcat EXPR [into VAR] | |
985 Concatenate EXPR (a vector) onto the end of a vector of values, like | |
986 `collect'. | |
987 | |
988 bvconcat EXPR [into VAR] | |
989 Concatenate EXPR (a bit vector) onto the end of a bit vector of values, | |
990 like `collect'. | |
991 | |
992 sum EXPR [into VAR] | |
993 Add EXPR to a value, like `collect'. | |
994 | |
995 count EXPR [into VAR] | |
996 If EXPR is true, increment a value by 1, like `collect'. | |
997 | |
998 maximize EXPR [into VAR] | |
999 IF EXPR is greater than a value, replace the value with EXPR, like | |
1000 `collect'. | |
1001 | |
1002 minimize EXPR [into VAR] | |
1003 IF EXPR is less than a value, replace the value with EXPR, like | |
1004 `collect'. | |
1005 | |
1006 always COND | |
1007 If COND is true, continue the loop and set the loop return value (the | |
1008 same value that's manipulated by `collect' and friends and is returned | |
1009 by a normal loop exit or an exit using `loop-finish') to t\; otherwise, | |
1010 exit the loop and return nil. The effect is to determine and return | |
1011 whether a condition is true \"always\" (all iterations of the loop). | |
1012 | |
1013 never COND | |
1014 If COND is false, continue the loop and set the loop return value (like | |
1015 `always') to t\; otherwise, exit the loop and return nil. The effect | |
1016 is to determine and return whether a condition is \"never\" true (all | |
1017 iterations of the loop). | |
1018 | |
1019 thereis COND | |
1020 If COND is true, exit the loop and return COND. | |
1021 | |
1022 if/when COND CLAUSE [and CLAUSE]... else CLAUSE [and CLAUSE...] | |
1023 If COND is true, execute the directly following clause(s)\; otherwise, | |
1024 execute the clauses following `else'. | |
1025 | |
1026 unless COND CLAUSE [and CLAUSE]... else CLAUSE [and CLAUSE...] | |
1027 If COND is false, execute the directly following clause(s)\; otherwise, execute the clauses following `else'. | |
1028 | |
1029 do EXPRS... | |
1030 Execute the expressions (any Lisp forms). | |
1031 | |
1032 initially EXPRS... | |
1033 Execute EXPR once, before doing any iterations, and after values have | |
1034 been set using `with'. | |
1035 | |
1036 finally EXPRS... | |
1037 Execute EXPR once, directly before the loop terminates. This will not | |
1038 be executed if the loop terminates prematurely as a result of `always', | |
1039 `never', `thereis', or `return'. | |
1040 | |
1041 return EXPR | |
1042 Exit from the loop and return EXPR. | |
1043 | |
1044 finally return EXPR | |
1045 Specify the value to be returned when the loop exits. (Unlike `return', | |
1046 this doesn't cause the loop to immediately exit\; it will exit whenever | |
1047 it normally would have.) This takes precedence over a return value | |
1048 specified with `collect' and friends or `always' and friends. | |
1049 | |
1050 named NAME | |
1051 Specify the name for block surrounding the loop, in place of nil. | |
1052 (See `block'.) | |
1053 " | |
428 | 1054 (if (not (memq t (mapcar 'symbolp (delq nil (delq t (copy-list args)))))) |
1055 (list 'block nil (list* 'while t args)) | |
1056 (let ((loop-name nil) (loop-bindings nil) | |
1057 (loop-body nil) (loop-steps nil) | |
1058 (loop-result nil) (loop-result-explicit nil) | |
1059 (loop-result-var nil) (loop-finish-flag nil) | |
1060 (loop-accum-var nil) (loop-accum-vars nil) | |
1061 (loop-initially nil) (loop-finally nil) | |
1062 (loop-map-form nil) (loop-first-flag nil) | |
1063 (loop-destr-temps nil) (loop-symbol-macs nil)) | |
1064 (setq args (append args '(cl-end-loop))) | |
1065 (while (not (eq (car args) 'cl-end-loop)) (cl-parse-loop-clause)) | |
1066 (if loop-finish-flag | |
2153 | 1067 (push (list (list loop-finish-flag t)) loop-bindings)) |
428 | 1068 (if loop-first-flag |
2153 | 1069 (progn (push (list (list loop-first-flag t)) loop-bindings) |
1070 (push (list 'setq loop-first-flag nil) loop-steps))) | |
428 | 1071 (let* ((epilogue (nconc (nreverse loop-finally) |
1072 (list (or loop-result-explicit loop-result)))) | |
1073 (ands (cl-loop-build-ands (nreverse loop-body))) | |
1074 (while-body (nconc (cadr ands) (nreverse loop-steps))) | |
1075 (body (append | |
1076 (nreverse loop-initially) | |
1077 (list (if loop-map-form | |
1078 (list 'block '--cl-finish-- | |
1079 (subst | |
1080 (if (eq (car ands) t) while-body | |
1081 (cons (list 'or (car ands) | |
1082 '(return-from --cl-finish-- | |
1083 nil)) | |
1084 while-body)) | |
1085 '--cl-map loop-map-form)) | |
1086 (list* 'while (car ands) while-body))) | |
1087 (if loop-finish-flag | |
1346 | 1088 (if (equal epilogue '(nil)) |
1089 ;; XEmacs change: When epilogue is nil and | |
1090 ;; loop-finish-flag exists, you get a byte-compiler | |
1091 ;; warning using the original (commented-out) | |
1092 ;; code below. So instead we create a form that | |
1093 ;; gives the same result but uses loop-finish-flag. | |
1094 ;; --ben | |
1095 ;(list loop-result-var) | |
1096 (list (list 'if loop-finish-flag | |
1097 loop-result-var loop-result-var)) | |
428 | 1098 (list (list 'if loop-finish-flag |
1099 (cons 'progn epilogue) loop-result-var))) | |
1100 epilogue)))) | |
2153 | 1101 (if loop-result-var (push (list loop-result-var) loop-bindings)) |
428 | 1102 (while loop-bindings |
1103 (if (cdar loop-bindings) | |
2153 | 1104 (setq body (list (cl-loop-let (pop loop-bindings) body t))) |
428 | 1105 (let ((lets nil)) |
1106 (while (and loop-bindings | |
1107 (not (cdar loop-bindings))) | |
2153 | 1108 (push (car (pop loop-bindings)) lets)) |
428 | 1109 (setq body (list (cl-loop-let lets body nil)))))) |
1110 (if loop-symbol-macs | |
1111 (setq body (list (list* 'symbol-macrolet loop-symbol-macs body)))) | |
1112 (list* 'block loop-name body))))) | |
1113 | |
1114 (defun cl-parse-loop-clause () ; uses args, loop-* | |
2153 | 1115 (let ((word (pop args)) |
428 | 1116 (hash-types '(hash-key hash-keys hash-value hash-values)) |
1117 (key-types '(key-code key-codes key-seq key-seqs | |
1118 key-binding key-bindings))) | |
1119 (cond | |
1120 | |
1121 ((null args) | |
1122 (error "Malformed `loop' macro")) | |
1123 | |
1124 ((eq word 'named) | |
2153 | 1125 (setq loop-name (pop args))) |
428 | 1126 |
1127 ((eq word 'initially) | |
2153 | 1128 (if (memq (car args) '(do doing)) (pop args)) |
428 | 1129 (or (consp (car args)) (error "Syntax error on `initially' clause")) |
1130 (while (consp (car args)) | |
2153 | 1131 (push (pop args) loop-initially))) |
428 | 1132 |
1133 ((eq word 'finally) | |
1134 (if (eq (car args) 'return) | |
1135 (setq loop-result-explicit (or (cl-pop2 args) '(quote nil))) | |
2153 | 1136 (if (memq (car args) '(do doing)) (pop args)) |
428 | 1137 (or (consp (car args)) (error "Syntax error on `finally' clause")) |
1138 (if (and (eq (caar args) 'return) (null loop-name)) | |
2153 | 1139 (setq loop-result-explicit (or (nth 1 (pop args)) '(quote nil))) |
428 | 1140 (while (consp (car args)) |
2153 | 1141 (push (pop args) loop-finally))))) |
428 | 1142 |
1143 ((memq word '(for as)) | |
1144 (let ((loop-for-bindings nil) (loop-for-sets nil) (loop-for-steps nil) | |
1145 (ands nil)) | |
1146 (while | |
2153 | 1147 (let ((var (or (pop args) (gensym)))) |
1148 (setq word (pop args)) | |
1149 (if (eq word 'being) (setq word (pop args))) | |
1150 (if (memq word '(the each)) (setq word (pop args))) | |
428 | 1151 (if (memq word '(buffer buffers)) |
1152 (setq word 'in args (cons '(buffer-list) args))) | |
1153 (cond | |
1154 | |
1155 ((memq word '(from downfrom upfrom to downto upto | |
1156 above below by)) | |
2153 | 1157 (push word args) |
428 | 1158 (if (memq (car args) '(downto above)) |
1159 (error "Must specify `from' value for downward loop")) | |
1160 (let* ((down (or (eq (car args) 'downfrom) | |
1161 (memq (caddr args) '(downto above)))) | |
1162 (excl (or (memq (car args) '(above below)) | |
1163 (memq (caddr args) '(above below)))) | |
1164 (start (and (memq (car args) '(from upfrom downfrom)) | |
1165 (cl-pop2 args))) | |
1166 (end (and (memq (car args) | |
1167 '(to upto downto above below)) | |
1168 (cl-pop2 args))) | |
1169 (step (and (eq (car args) 'by) (cl-pop2 args))) | |
1170 (end-var (and (not (cl-const-expr-p end)) (gensym))) | |
1171 (step-var (and (not (cl-const-expr-p step)) | |
1172 (gensym)))) | |
1173 (and step (numberp step) (<= step 0) | |
1174 (error "Loop `by' value is not positive: %s" step)) | |
2153 | 1175 (push (list var (or start 0)) loop-for-bindings) |
1176 (if end-var (push (list end-var end) loop-for-bindings)) | |
1177 (if step-var (push (list step-var step) | |
428 | 1178 loop-for-bindings)) |
1179 (if end | |
2153 | 1180 (push (list |
428 | 1181 (if down (if excl '> '>=) (if excl '< '<=)) |
1182 var (or end-var end)) loop-body)) | |
2153 | 1183 (push (list var (list (if down '- '+) var |
428 | 1184 (or step-var step 1))) |
1185 loop-for-steps))) | |
1186 | |
1187 ((memq word '(in in-ref on)) | |
1188 (let* ((on (eq word 'on)) | |
1189 (temp (if (and on (symbolp var)) var (gensym)))) | |
2153 | 1190 (push (list temp (pop args)) loop-for-bindings) |
1191 (push (list 'consp temp) loop-body) | |
428 | 1192 (if (eq word 'in-ref) |
2153 | 1193 (push (list var (list 'car temp)) loop-symbol-macs) |
428 | 1194 (or (eq temp var) |
1195 (progn | |
2153 | 1196 (push (list var nil) loop-for-bindings) |
1197 (push (list var (if on temp (list 'car temp))) | |
428 | 1198 loop-for-sets)))) |
2153 | 1199 (push (list temp |
428 | 1200 (if (eq (car args) 'by) |
1201 (let ((step (cl-pop2 args))) | |
1202 (if (and (memq (car-safe step) | |
1203 '(quote function | |
1204 function*)) | |
1205 (symbolp (nth 1 step))) | |
1206 (list (nth 1 step) temp) | |
1207 (list 'funcall step temp))) | |
1208 (list 'cdr temp))) | |
1209 loop-for-steps))) | |
1210 | |
1211 ((eq word '=) | |
2153 | 1212 (let* ((start (pop args)) |
428 | 1213 (then (if (eq (car args) 'then) (cl-pop2 args) start))) |
2153 | 1214 (push (list var nil) loop-for-bindings) |
428 | 1215 (if (or ands (eq (car args) 'and)) |
1216 (progn | |
2153 | 1217 (push (list var |
428 | 1218 (list 'if |
1219 (or loop-first-flag | |
1220 (setq loop-first-flag | |
1221 (gensym))) | |
1222 start var)) | |
1223 loop-for-sets) | |
2153 | 1224 (push (list var then) loop-for-steps)) |
1225 (push (list var | |
428 | 1226 (if (eq start then) start |
1227 (list 'if | |
1228 (or loop-first-flag | |
1229 (setq loop-first-flag (gensym))) | |
1230 start then))) | |
1231 loop-for-sets)))) | |
1232 | |
1233 ((memq word '(across across-ref)) | |
1234 (let ((temp-vec (gensym)) (temp-idx (gensym))) | |
2153 | 1235 (push (list temp-vec (pop args)) loop-for-bindings) |
1236 (push (list temp-idx -1) loop-for-bindings) | |
1237 (push (list '< (list 'setq temp-idx (list '1+ temp-idx)) | |
428 | 1238 (list 'length temp-vec)) loop-body) |
1239 (if (eq word 'across-ref) | |
2153 | 1240 (push (list var (list 'aref temp-vec temp-idx)) |
428 | 1241 loop-symbol-macs) |
2153 | 1242 (push (list var nil) loop-for-bindings) |
1243 (push (list var (list 'aref temp-vec temp-idx)) | |
428 | 1244 loop-for-sets)))) |
1245 | |
1246 ((memq word '(element elements)) | |
1247 (let ((ref (or (memq (car args) '(in-ref of-ref)) | |
1248 (and (not (memq (car args) '(in of))) | |
1249 (error "Expected `of'")))) | |
1250 (seq (cl-pop2 args)) | |
1251 (temp-seq (gensym)) | |
1252 (temp-idx (if (eq (car args) 'using) | |
1253 (if (and (= (length (cadr args)) 2) | |
1254 (eq (caadr args) 'index)) | |
1255 (cadr (cl-pop2 args)) | |
1256 (error "Bad `using' clause")) | |
1257 (gensym)))) | |
2153 | 1258 (push (list temp-seq seq) loop-for-bindings) |
1259 (push (list temp-idx 0) loop-for-bindings) | |
428 | 1260 (if ref |
1261 (let ((temp-len (gensym))) | |
2153 | 1262 (push (list temp-len (list 'length temp-seq)) |
428 | 1263 loop-for-bindings) |
2153 | 1264 (push (list var (list 'elt temp-seq temp-idx)) |
428 | 1265 loop-symbol-macs) |
2153 | 1266 (push (list '< temp-idx temp-len) loop-body)) |
1267 (push (list var nil) loop-for-bindings) | |
1268 (push (list 'and temp-seq | |
428 | 1269 (list 'or (list 'consp temp-seq) |
1270 (list '< temp-idx | |
1271 (list 'length temp-seq)))) | |
1272 loop-body) | |
2153 | 1273 (push (list var (list 'if (list 'consp temp-seq) |
428 | 1274 (list 'pop temp-seq) |
1275 (list 'aref temp-seq temp-idx))) | |
1276 loop-for-sets)) | |
2153 | 1277 (push (list temp-idx (list '1+ temp-idx)) |
428 | 1278 loop-for-steps))) |
1279 | |
1280 ((memq word hash-types) | |
1281 (or (memq (car args) '(in of)) (error "Expected `of'")) | |
1282 (let* ((table (cl-pop2 args)) | |
1283 (other (if (eq (car args) 'using) | |
1284 (if (and (= (length (cadr args)) 2) | |
1285 (memq (caadr args) hash-types) | |
1286 (not (eq (caadr args) word))) | |
1287 (cadr (cl-pop2 args)) | |
1288 (error "Bad `using' clause")) | |
1289 (gensym)))) | |
1290 (if (memq word '(hash-value hash-values)) | |
1291 (setq var (prog1 other (setq other var)))) | |
1292 (setq loop-map-form | |
1293 (list 'maphash (list 'function | |
1294 (list* 'lambda (list var other) | |
1295 '--cl-map)) table)))) | |
1296 | |
1297 ((memq word '(symbol present-symbol external-symbol | |
1298 symbols present-symbols external-symbols)) | |
1299 (let ((ob (and (memq (car args) '(in of)) (cl-pop2 args)))) | |
1300 (setq loop-map-form | |
1301 (list 'mapatoms (list 'function | |
1302 (list* 'lambda (list var) | |
1303 '--cl-map)) ob)))) | |
1304 | |
1305 ((memq word '(overlay overlays extent extents)) | |
1306 (let ((buf nil) (from nil) (to nil)) | |
1307 (while (memq (car args) '(in of from to)) | |
1308 (cond ((eq (car args) 'from) (setq from (cl-pop2 args))) | |
1309 ((eq (car args) 'to) (setq to (cl-pop2 args))) | |
1310 (t (setq buf (cl-pop2 args))))) | |
1311 (setq loop-map-form | |
1312 (list 'cl-map-extents | |
1313 (list 'function (list 'lambda (list var (gensym)) | |
1314 '(progn . --cl-map) nil)) | |
1315 buf from to)))) | |
1316 | |
1317 ((memq word '(interval intervals)) | |
1318 (let ((buf nil) (prop nil) (from nil) (to nil) | |
1319 (var1 (gensym)) (var2 (gensym))) | |
1320 (while (memq (car args) '(in of property from to)) | |
1321 (cond ((eq (car args) 'from) (setq from (cl-pop2 args))) | |
1322 ((eq (car args) 'to) (setq to (cl-pop2 args))) | |
1323 ((eq (car args) 'property) | |
1324 (setq prop (cl-pop2 args))) | |
1325 (t (setq buf (cl-pop2 args))))) | |
1326 (if (and (consp var) (symbolp (car var)) (symbolp (cdr var))) | |
1327 (setq var1 (car var) var2 (cdr var)) | |
2153 | 1328 (push (list var (list 'cons var1 var2)) loop-for-sets)) |
428 | 1329 (setq loop-map-form |
1330 (list 'cl-map-intervals | |
1331 (list 'function (list 'lambda (list var1 var2) | |
1332 '(progn . --cl-map))) | |
1333 buf prop from to)))) | |
1334 | |
1335 ((memq word key-types) | |
1336 (or (memq (car args) '(in of)) (error "Expected `of'")) | |
800 | 1337 (let* ((map (cl-pop2 args)) |
1338 other-word | |
1339 (other (if (eq (car args) 'using) | |
1340 (if (and (= (length (cadr args)) 2) | |
1341 (memq (setq other-word (caadr args)) | |
1342 key-types) | |
1343 (not (eq (caadr args) word))) | |
1344 (cadr (cl-pop2 args)) | |
1345 (error "Bad `using' clause")) | |
428 | 1346 (gensym)))) |
2153 | 1347 ;; XEmacs addition: track other-word |
800 | 1348 (when (memq word '(key-binding key-bindings)) |
1349 (setq var (prog1 other (setq other var))) | |
1350 (and other-word (setq word other-word))) | |
428 | 1351 (setq loop-map-form |
1352 (list (if (memq word '(key-seq key-seqs)) | |
2153 | 1353 'cl-map-keymap-recursively 'map-keymap) |
428 | 1354 (list 'function (list* 'lambda (list var other) |
1355 '--cl-map)) map)))) | |
1356 | |
1357 ((memq word '(frame frames screen screens)) | |
1358 (let ((temp (gensym))) | |
2153 | 1359 (push (list var '(selected-frame)) |
428 | 1360 loop-for-bindings) |
2153 | 1361 (push (list temp nil) loop-for-bindings) |
1362 (push (list 'prog1 (list 'not (list 'eq var temp)) | |
428 | 1363 (list 'or temp (list 'setq temp var))) |
1364 loop-body) | |
2153 | 1365 (push (list var (list 'next-frame var)) |
428 | 1366 loop-for-steps))) |
1367 | |
1368 ((memq word '(window windows)) | |
1369 (let ((scr (and (memq (car args) '(in of)) (cl-pop2 args))) | |
1370 (temp (gensym))) | |
2153 | 1371 (push (list var (if scr |
428 | 1372 (list 'frame-selected-window scr) |
1373 '(selected-window))) | |
1374 loop-for-bindings) | |
2153 | 1375 (push (list temp nil) loop-for-bindings) |
1376 (push (list 'prog1 (list 'not (list 'eq var temp)) | |
428 | 1377 (list 'or temp (list 'setq temp var))) |
1378 loop-body) | |
2153 | 1379 (push (list var (list 'next-window var)) loop-for-steps))) |
428 | 1380 |
1381 (t | |
1382 (let ((handler (and (symbolp word) | |
1383 (get word 'cl-loop-for-handler)))) | |
1384 (if handler | |
1385 (funcall handler var) | |
1386 (error "Expected a `for' preposition, found %s" word))))) | |
1387 (eq (car args) 'and)) | |
1388 (setq ands t) | |
2153 | 1389 (pop args)) |
428 | 1390 (if (and ands loop-for-bindings) |
2153 | 1391 (push (nreverse loop-for-bindings) loop-bindings) |
428 | 1392 (setq loop-bindings (nconc (mapcar 'list loop-for-bindings) |
1393 loop-bindings))) | |
1394 (if loop-for-sets | |
2153 | 1395 (push (list 'progn |
428 | 1396 (cl-loop-let (nreverse loop-for-sets) 'setq ands) |
1397 t) loop-body)) | |
1398 (if loop-for-steps | |
2153 | 1399 (push (cons (if ands 'psetq 'setq) |
428 | 1400 (apply 'append (nreverse loop-for-steps))) |
1401 loop-steps)))) | |
1402 | |
1403 ((eq word 'repeat) | |
1404 (let ((temp (gensym))) | |
2153 | 1405 (push (list (list temp (pop args))) loop-bindings) |
1406 (push (list '>= (list 'setq temp (list '1- temp)) 0) loop-body))) | |
1407 | |
1408 ((memq word '(collect collecting)) | |
1409 (let ((what (pop args)) | |
428 | 1410 (var (cl-loop-handle-accum nil 'nreverse))) |
1411 (if (eq var loop-accum-var) | |
2153 | 1412 (push (list 'progn (list 'push what var) t) loop-body) |
1413 (push (list 'progn | |
428 | 1414 (list 'setq var (list 'nconc var (list 'list what))) |
1415 t) loop-body)))) | |
1416 | |
1417 ((memq word '(nconc nconcing append appending)) | |
2153 | 1418 (let ((what (pop args)) |
428 | 1419 (var (cl-loop-handle-accum nil 'nreverse))) |
2153 | 1420 (push (list 'progn |
428 | 1421 (list 'setq var |
1422 (if (eq var loop-accum-var) | |
1423 (list 'nconc | |
1424 (list (if (memq word '(nconc nconcing)) | |
1425 'nreverse 'reverse) | |
1426 what) | |
1427 var) | |
1428 (list (if (memq word '(nconc nconcing)) | |
1429 'nconc 'append) | |
1430 var what))) t) loop-body))) | |
1431 | |
1432 ((memq word '(concat concating)) | |
2153 | 1433 (let ((what (pop args)) |
428 | 1434 (var (cl-loop-handle-accum ""))) |
2153 | 1435 (push (list 'progn (list 'callf 'concat var what) t) loop-body))) |
428 | 1436 |
1437 ((memq word '(vconcat vconcating)) | |
2153 | 1438 (let ((what (pop args)) |
428 | 1439 (var (cl-loop-handle-accum []))) |
2153 | 1440 (push (list 'progn (list 'callf 'vconcat var what) t) loop-body))) |
1441 | |
1442 ;; XEmacs addition: handle bit-vectors | |
800 | 1443 ((memq word '(bvconcat bvconcating)) |
2153 | 1444 (let ((what (pop args)) |
800 | 1445 (var (cl-loop-handle-accum #*))) |
2153 | 1446 (push (list 'progn (list 'callf 'bvconcat var what) t) loop-body))) |
800 | 1447 |
428 | 1448 ((memq word '(sum summing)) |
2153 | 1449 (let ((what (pop args)) |
428 | 1450 (var (cl-loop-handle-accum 0))) |
2153 | 1451 (push (list 'progn (list 'incf var what) t) loop-body))) |
428 | 1452 |
1453 ((memq word '(count counting)) | |
2153 | 1454 (let ((what (pop args)) |
428 | 1455 (var (cl-loop-handle-accum 0))) |
2153 | 1456 (push (list 'progn (list 'if what (list 'incf var)) t) loop-body))) |
428 | 1457 |
1458 ((memq word '(minimize minimizing maximize maximizing)) | |
2153 | 1459 (let* ((what (pop args)) |
428 | 1460 (temp (if (cl-simple-expr-p what) what (gensym))) |
1461 (var (cl-loop-handle-accum nil)) | |
1462 (func (intern (substring (symbol-name word) 0 3))) | |
1463 (set (list 'setq var (list 'if var (list func var temp) temp)))) | |
2153 | 1464 (push (list 'progn (if (eq temp what) set |
428 | 1465 (list 'let (list (list temp what)) set)) |
1466 t) loop-body))) | |
1467 | |
1468 ((eq word 'with) | |
1469 (let ((bindings nil)) | |
2153 | 1470 (while (progn (push (list (pop args) |
428 | 1471 (and (eq (car args) '=) (cl-pop2 args))) |
1472 bindings) | |
1473 (eq (car args) 'and)) | |
2153 | 1474 (pop args)) |
1475 (push (nreverse bindings) loop-bindings))) | |
428 | 1476 |
1477 ((eq word 'while) | |
2153 | 1478 (push (pop args) loop-body)) |
428 | 1479 |
1480 ((eq word 'until) | |
2153 | 1481 (push (list 'not (pop args)) loop-body)) |
428 | 1482 |
1483 ((eq word 'always) | |
1484 (or loop-finish-flag (setq loop-finish-flag (gensym))) | |
2153 | 1485 (push (list 'setq loop-finish-flag (pop args)) loop-body) |
428 | 1486 (setq loop-result t)) |
1487 | |
1488 ((eq word 'never) | |
1489 (or loop-finish-flag (setq loop-finish-flag (gensym))) | |
2153 | 1490 (push (list 'setq loop-finish-flag (list 'not (pop args))) |
428 | 1491 loop-body) |
1492 (setq loop-result t)) | |
1493 | |
1494 ((eq word 'thereis) | |
1495 (or loop-finish-flag (setq loop-finish-flag (gensym))) | |
1496 (or loop-result-var (setq loop-result-var (gensym))) | |
2153 | 1497 (push (list 'setq loop-finish-flag |
1498 (list 'not (list 'setq loop-result-var (pop args)))) | |
428 | 1499 loop-body)) |
1500 | |
1501 ((memq word '(if when unless)) | |
2153 | 1502 (let* ((cond (pop args)) |
428 | 1503 (then (let ((loop-body nil)) |
1504 (cl-parse-loop-clause) | |
1505 (cl-loop-build-ands (nreverse loop-body)))) | |
1506 (else (let ((loop-body nil)) | |
1507 (if (eq (car args) 'else) | |
2153 | 1508 (progn (pop args) (cl-parse-loop-clause))) |
428 | 1509 (cl-loop-build-ands (nreverse loop-body)))) |
1510 (simple (and (eq (car then) t) (eq (car else) t)))) | |
2153 | 1511 (if (eq (car args) 'end) (pop args)) |
428 | 1512 (if (eq word 'unless) (setq then (prog1 else (setq else then)))) |
1513 (let ((form (cons (if simple (cons 'progn (nth 1 then)) (nth 2 then)) | |
1514 (if simple (nth 1 else) (list (nth 2 else)))))) | |
1515 (if (cl-expr-contains form 'it) | |
1516 (let ((temp (gensym))) | |
2153 | 1517 (push (list temp) loop-bindings) |
428 | 1518 (setq form (list* 'if (list 'setq temp cond) |
1519 (subst temp 'it form)))) | |
1520 (setq form (list* 'if cond form))) | |
2153 | 1521 (push (if simple (list 'progn form t) form) loop-body)))) |
428 | 1522 |
1523 ((memq word '(do doing)) | |
1524 (let ((body nil)) | |
1525 (or (consp (car args)) (error "Syntax error on `do' clause")) | |
2153 | 1526 (while (consp (car args)) (push (pop args) body)) |
1527 (push (cons 'progn (nreverse (cons t body))) loop-body))) | |
428 | 1528 |
1529 ((eq word 'return) | |
1530 (or loop-finish-flag (setq loop-finish-flag (gensym))) | |
1531 (or loop-result-var (setq loop-result-var (gensym))) | |
2153 | 1532 (push (list 'setq loop-result-var (pop args) |
428 | 1533 loop-finish-flag nil) loop-body)) |
1534 | |
1535 (t | |
1536 (let ((handler (and (symbolp word) (get word 'cl-loop-handler)))) | |
1537 (or handler (error "Expected a loop keyword, found %s" word)) | |
1538 (funcall handler)))) | |
1539 (if (eq (car args) 'and) | |
2153 | 1540 (progn (pop args) (cl-parse-loop-clause))))) |
428 | 1541 |
1542 (defun cl-loop-let (specs body par) ; uses loop-* | |
1543 (let ((p specs) (temps nil) (new nil)) | |
1544 (while (and p (or (symbolp (car-safe (car p))) (null (cadar p)))) | |
1545 (setq p (cdr p))) | |
1546 (and par p | |
1547 (progn | |
1548 (setq par nil p specs) | |
1549 (while p | |
1550 (or (cl-const-expr-p (cadar p)) | |
1551 (let ((temp (gensym))) | |
2153 | 1552 (push (list temp (cadar p)) temps) |
428 | 1553 (setcar (cdar p) temp))) |
1554 (setq p (cdr p))))) | |
1555 (while specs | |
1556 (if (and (consp (car specs)) (listp (caar specs))) | |
1557 (let* ((spec (caar specs)) (nspecs nil) | |
2153 | 1558 (expr (cadr (pop specs))) |
428 | 1559 (temp (cdr (or (assq spec loop-destr-temps) |
2153 | 1560 (car (push (cons spec (or (last spec 0) |
428 | 1561 (gensym))) |
1562 loop-destr-temps)))))) | |
2153 | 1563 (push (list temp expr) new) |
428 | 1564 (while (consp spec) |
2153 | 1565 (push (list (pop spec) |
428 | 1566 (and expr (list (if spec 'pop 'car) temp))) |
1567 nspecs)) | |
1568 (setq specs (nconc (nreverse nspecs) specs))) | |
2153 | 1569 (push (pop specs) new))) |
428 | 1570 (if (eq body 'setq) |
1571 (let ((set (cons (if par 'psetq 'setq) (apply 'nconc (nreverse new))))) | |
1572 (if temps (list 'let* (nreverse temps) set) set)) | |
1573 (list* (if par 'let 'let*) | |
1574 (nconc (nreverse temps) (nreverse new)) body)))) | |
1575 | |
1576 (defun cl-loop-handle-accum (def &optional func) ; uses args, loop-* | |
1577 (if (eq (car args) 'into) | |
1578 (let ((var (cl-pop2 args))) | |
1579 (or (memq var loop-accum-vars) | |
2153 | 1580 (progn (push (list (list var def)) loop-bindings) |
1581 (push var loop-accum-vars))) | |
428 | 1582 var) |
1583 (or loop-accum-var | |
1584 (progn | |
2153 | 1585 (push (list (list (setq loop-accum-var (gensym)) def)) |
428 | 1586 loop-bindings) |
1587 (setq loop-result (if func (list func loop-accum-var) | |
1588 loop-accum-var)) | |
1589 loop-accum-var)))) | |
1590 | |
1591 (defun cl-loop-build-ands (clauses) | |
1592 (let ((ands nil) | |
1593 (body nil)) | |
1594 (while clauses | |
1595 (if (and (eq (car-safe (car clauses)) 'progn) | |
1596 (eq (car (last (car clauses))) t)) | |
1597 (if (cdr clauses) | |
1598 (setq clauses (cons (nconc (butlast (car clauses)) | |
1599 (if (eq (car-safe (cadr clauses)) | |
1600 'progn) | |
1601 (cdadr clauses) | |
1602 (list (cadr clauses)))) | |
1603 (cddr clauses))) | |
2153 | 1604 (setq body (cdr (butlast (pop clauses))))) |
1605 (push (pop clauses) ands))) | |
428 | 1606 (setq ands (or (nreverse ands) (list t))) |
1607 (list (if (cdr ands) (cons 'and ands) (car ands)) | |
1608 body | |
1609 (let ((full (if body | |
1610 (append ands (list (cons 'progn (append body '(t))))) | |
1611 ands))) | |
1612 (if (cdr full) (cons 'and full) (car full)))))) | |
1613 | |
1614 | |
1615 ;;; Other iteration control structures. | |
1616 | |
1617 ;;;###autoload | |
1618 (defmacro do (steps endtest &rest body) | |
1619 "The Common Lisp `do' loop. | |
1620 Format is: (do ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)" | |
1621 (cl-expand-do-loop steps endtest body nil)) | |
1622 | |
1623 ;;;###autoload | |
1624 (defmacro do* (steps endtest &rest body) | |
1625 "The Common Lisp `do*' loop. | |
1626 Format is: (do* ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)" | |
1627 (cl-expand-do-loop steps endtest body t)) | |
1628 | |
1629 (defun cl-expand-do-loop (steps endtest body star) | |
1630 (list 'block nil | |
1631 (list* (if star 'let* 'let) | |
1632 (mapcar #'(lambda (c) (if (consp c) (list (car c) (nth 1 c)) c)) | |
1633 steps) | |
1634 (list* 'while (list 'not (car endtest)) | |
1635 (append body | |
1636 (let ((sets (mapcar | |
1637 #'(lambda (c) | |
1638 (and (consp c) (cdr (cdr c)) | |
1639 (list (car c) (nth 2 c)))) | |
1640 steps))) | |
1641 (setq sets (delq nil sets)) | |
1642 (and sets | |
1643 (list (cons (if (or star (not (cdr sets))) | |
1644 'setq 'psetq) | |
1645 (apply 'append sets))))))) | |
1646 (or (cdr endtest) '(nil))))) | |
1647 | |
1648 ;;;###autoload | |
1649 (defmacro dolist (spec &rest body) | |
1650 "(dolist (VAR LIST [RESULT]) BODY...): loop over a list. | |
1651 Evaluate BODY with VAR bound to each `car' from LIST, in turn. | |
1652 Then evaluate RESULT to get return value, default nil." | |
1653 (let ((temp (gensym "--dolist-temp--"))) | |
1654 (list 'block nil | |
1655 (list* 'let (list (list temp (nth 1 spec)) (car spec)) | |
1656 (list* 'while temp (list 'setq (car spec) (list 'car temp)) | |
1657 (append body (list (list 'setq temp | |
1658 (list 'cdr temp))))) | |
1659 (if (cdr (cdr spec)) | |
1660 (cons (list 'setq (car spec) nil) (cdr (cdr spec))) | |
1661 '(nil)))))) | |
1662 | |
1663 ;;;###autoload | |
1664 (defmacro dotimes (spec &rest body) | |
1665 "(dotimes (VAR COUNT [RESULT]) BODY...): loop a certain number of times. | |
1666 Evaluate BODY with VAR bound to successive integers from 0, inclusive, | |
1667 to COUNT, exclusive. Then evaluate RESULT to get return value, default | |
1668 nil." | |
1669 (let ((temp (gensym "--dotimes-temp--"))) | |
1670 (list 'block nil | |
1671 (list* 'let (list (list temp (nth 1 spec)) (list (car spec) 0)) | |
1672 (list* 'while (list '< (car spec) temp) | |
1673 (append body (list (list 'incf (car spec))))) | |
1674 (or (cdr (cdr spec)) '(nil)))))) | |
1675 | |
1676 ;;;###autoload | |
1677 (defmacro do-symbols (spec &rest body) | |
1678 "(dosymbols (VAR [OBARRAY [RESULT]]) BODY...): loop over all symbols. | |
1679 Evaluate BODY with VAR bound to each interned symbol, or to each symbol | |
1680 from OBARRAY." | |
1681 ;; Apparently this doesn't have an implicit block. | |
1682 (list 'block nil | |
1683 (list 'let (list (car spec)) | |
1684 (list* 'mapatoms | |
1685 (list 'function (list* 'lambda (list (car spec)) body)) | |
1686 (and (cadr spec) (list (cadr spec)))) | |
1687 (caddr spec)))) | |
1688 | |
1689 ;;;###autoload | |
1690 (defmacro do-all-symbols (spec &rest body) | |
1691 (list* 'do-symbols (list (car spec) nil (cadr spec)) body)) | |
1692 | |
1693 | |
1694 ;;; Assignments. | |
1695 | |
1696 ;;;###autoload | |
1697 (defmacro psetq (&rest args) | |
1698 "(psetq SYM VAL SYM VAL ...): set SYMs to the values VALs in parallel. | |
1699 This is like `setq', except that all VAL forms are evaluated (in order) | |
1700 before assigning any symbols SYM to the corresponding values." | |
1701 (cons 'psetf args)) | |
1702 | |
1703 | |
1704 ;;; Binding control structures. | |
1705 | |
1706 ;;;###autoload | |
1707 (defmacro progv (symbols values &rest body) | |
4695
fee33ab25966
Add arglist info for autoloaded functions and macros.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4694
diff
changeset
|
1708 "Bind SYMBOLS to VALUES dynamically in BODY. |
428 | 1709 The forms SYMBOLS and VALUES are evaluated, and must evaluate to lists. |
1710 Each SYMBOL in the first list is bound to the corresponding VALUE in the | |
1711 second list (or made unbound if VALUES is shorter than SYMBOLS); then the | |
1712 BODY forms are executed and their result is returned. This is much like | |
1713 a `let' form, except that the list of symbols can be computed at run-time." | |
1714 (list 'let '((cl-progv-save nil)) | |
1715 (list 'unwind-protect | |
1716 (list* 'progn (list 'cl-progv-before symbols values) body) | |
1717 '(cl-progv-after)))) | |
1718 | |
1719 ;;; This should really have some way to shadow 'byte-compile properties, etc. | |
1720 ;;;###autoload | |
1721 (defmacro flet (bindings &rest body) | |
1722 "(flet ((FUNC ARGLIST BODY...) ...) FORM...): make temporary function defns. | |
1723 This is an analogue of `let' that operates on the function cell of FUNC | |
1724 rather than its value cell. The FORMs are evaluated with the specified | |
1725 function definitions in place, then the definitions are undone (the FUNCs | |
1726 go back to their previous definitions, or lack thereof)." | |
1727 (list* 'letf* | |
1728 (mapcar | |
1729 #'(lambda (x) | |
1730 (if (or (and (fboundp (car x)) | |
1731 (eq (car-safe (symbol-function (car x))) 'macro)) | |
1732 (cdr (assq (car x) cl-macro-environment))) | |
1733 (error "Use `labels', not `flet', to rebind macro names")) | |
1734 (let ((func (list 'function* | |
1735 (list 'lambda (cadr x) | |
1736 (list* 'block (car x) (cddr x)))))) | |
1737 (if (and (cl-compiling-file) | |
1738 (boundp 'byte-compile-function-environment)) | |
2153 | 1739 (push (cons (car x) (eval func)) |
428 | 1740 byte-compile-function-environment)) |
1741 (list (list 'symbol-function (list 'quote (car x))) func))) | |
1742 bindings) | |
1743 body)) | |
1744 | |
1745 ;;;###autoload | |
1746 (defmacro labels (bindings &rest body) | |
1747 "(labels ((FUNC ARGLIST BODY...) ...) FORM...): make temporary func bindings. | |
1748 This is like `flet', except the bindings are lexical instead of dynamic. | |
1749 Unlike `flet', this macro is fully compliant with the Common Lisp standard." | |
1750 (let ((vars nil) (sets nil) (cl-macro-environment cl-macro-environment)) | |
1751 (while bindings | |
1752 (let ((var (gensym))) | |
2153 | 1753 (push var vars) |
1754 (push (list 'function* (cons 'lambda (cdar bindings))) sets) | |
1755 (push var sets) | |
1756 (push (list (car (pop bindings)) 'lambda '(&rest cl-labels-args) | |
428 | 1757 (list 'list* '(quote funcall) (list 'quote var) |
1758 'cl-labels-args)) | |
1759 cl-macro-environment))) | |
1760 (cl-macroexpand-all (list* 'lexical-let vars (cons (cons 'setq sets) body)) | |
1761 cl-macro-environment))) | |
1762 | |
1763 ;; The following ought to have a better definition for use with newer | |
1764 ;; byte compilers. | |
1765 ;;;###autoload | |
1766 (defmacro macrolet (bindings &rest body) | |
1767 "(macrolet ((NAME ARGLIST BODY...) ...) FORM...): make temporary macro defns. | |
1768 This is like `flet', but for macros instead of functions." | |
1769 (if (cdr bindings) | |
1770 (list 'macrolet | |
1771 (list (car bindings)) (list* 'macrolet (cdr bindings) body)) | |
1772 (if (null bindings) (cons 'progn body) | |
1773 (let* ((name (caar bindings)) | |
1774 (res (cl-transform-lambda (cdar bindings) name))) | |
1775 (eval (car res)) | |
1776 (cl-macroexpand-all (cons 'progn body) | |
1777 (cons (list* name 'lambda (cdr res)) | |
1778 cl-macro-environment)))))) | |
1779 | |
1780 ;;;###autoload | |
1781 (defmacro symbol-macrolet (bindings &rest body) | |
1782 "(symbol-macrolet ((NAME EXPANSION) ...) FORM...): make symbol macro defns. | |
1783 Within the body FORMs, references to the variable NAME will be replaced | |
1784 by EXPANSION, and (setq NAME ...) will act like (setf EXPANSION ...)." | |
1785 (if (cdr bindings) | |
1786 (list 'symbol-macrolet | |
1787 (list (car bindings)) (list* 'symbol-macrolet (cdr bindings) body)) | |
1788 (if (null bindings) (cons 'progn body) | |
1789 (cl-macroexpand-all (cons 'progn body) | |
1790 (cons (list (symbol-name (caar bindings)) | |
1791 (cadar bindings)) | |
1792 cl-macro-environment))))) | |
1793 | |
1794 (defvar cl-closure-vars nil) | |
1795 ;;;###autoload | |
1796 (defmacro lexical-let (bindings &rest body) | |
4695
fee33ab25966
Add arglist info for autoloaded functions and macros.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4694
diff
changeset
|
1797 "Like `let', but lexically scoped. |
428 | 1798 The main visible difference is that lambdas inside BODY will create |
1799 lexical closures as in Common Lisp." | |
1800 (let* ((cl-closure-vars cl-closure-vars) | |
1801 (vars (mapcar #'(lambda (x) | |
1802 (or (consp x) (setq x (list x))) | |
2153 | 1803 (push (gensym (format "--%s--" (car x))) |
428 | 1804 cl-closure-vars) |
2153 | 1805 (set (car cl-closure-vars) [bad-lexical-ref]) |
428 | 1806 (list (car x) (cadr x) (car cl-closure-vars))) |
1807 bindings)) | |
1808 (ebody | |
1809 (cl-macroexpand-all | |
1810 (cons 'progn body) | |
1811 (nconc (mapcar #'(lambda (x) | |
1812 (list (symbol-name (car x)) | |
1813 (list 'symbol-value (caddr x)) | |
1814 t)) | |
1815 vars) | |
1816 (list '(defun . cl-defun-expander)) | |
1817 cl-macro-environment)))) | |
1818 (if (not (get (car (last cl-closure-vars)) 'used)) | |
1819 (list 'let (mapcar #'(lambda (x) (list (caddr x) (cadr x))) vars) | |
1820 (sublis (mapcar #'(lambda (x) | |
1821 (cons (caddr x) (list 'quote (caddr x)))) | |
1822 vars) | |
1823 ebody)) | |
1824 (list 'let (mapcar #'(lambda (x) | |
1825 (list (caddr x) | |
1826 (list 'make-symbol | |
1827 (format "--%s--" (car x))))) | |
1828 vars) | |
1829 (apply 'append '(setf) | |
1830 (mapcar #'(lambda (x) | |
1831 (list (list 'symbol-value (caddr x)) (cadr x))) | |
1832 vars)) | |
1833 ebody)))) | |
1834 | |
1835 ;;;###autoload | |
1836 (defmacro lexical-let* (bindings &rest body) | |
4695
fee33ab25966
Add arglist info for autoloaded functions and macros.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4694
diff
changeset
|
1837 "Like `let*', but lexically scoped. |
428 | 1838 The main visible difference is that lambdas inside BODY will create |
1839 lexical closures as in Common Lisp." | |
1840 (if (null bindings) (cons 'progn body) | |
1841 (setq bindings (reverse bindings)) | |
1842 (while bindings | |
2153 | 1843 (setq body (list (list* 'lexical-let (list (pop bindings)) body)))) |
428 | 1844 (car body))) |
1845 | |
1846 (defun cl-defun-expander (func &rest rest) | |
1847 (list 'progn | |
1848 (list 'defalias (list 'quote func) | |
1849 (list 'function (cons 'lambda rest))) | |
1850 (list 'quote func))) | |
1851 | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1852 ;;; Multiple values. We support full Common Lisp conventions here. |
428 | 1853 |
1854 ;;;###autoload | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1855 (defmacro multiple-value-bind (syms form &rest body) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1856 "Collect and bind multiple return values. |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1857 |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1858 If FORM returns multiple values, each symbol in SYMS is bound to one of |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1859 them, in order, and BODY is executed. If FORM returns fewer multiple values |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1860 than there are SYMS, remaining SYMS are bound to nil. If FORM does |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1861 not return multiple values, it is treated as returning one multiple value. |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1862 |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1863 Returns the value given by the last element of BODY." |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1864 (if (null syms) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1865 `(progn ,form ,@body) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1866 (if (= 1 (length syms)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1867 ;; Code written to deal with other "implementations" of multiple |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1868 ;; values may have a one-element SYMS. |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1869 `(let ((,(car syms) ,form)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1870 ,@body) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1871 (let ((temp (gensym))) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1872 `(let* ((,temp (multiple-value-list-internal 0 ,(length syms) ,form)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1873 ,@(loop |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1874 for var in syms |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1875 collect `(,var (prog1 (car ,temp) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1876 (setq ,temp (cdr ,temp)))))) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1877 ,@body))))) |
428 | 1878 |
1879 ;;;###autoload | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1880 (defmacro multiple-value-setq (syms form) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1881 "Collect and set multiple values. |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1882 |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1883 FORM should normally return multiple values; the first N values are stored |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1884 in the symbols in SYMS in turn. If FORM returns fewer than N values, the |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1885 remaining symbols have their values set to nil. FORM not returning multiple |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1886 values is treated as FORM returning one multiple value, with other elements |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1887 of SYMS initialized to nil. |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1888 |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1889 Returns the first of the multiple values given by FORM." |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1890 (if (null syms) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1891 ;; Never return multiple values from multiple-value-setq: |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1892 (and form `(values ,form)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1893 (if (= 1 (length syms)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1894 `(setq ,(car syms) ,form) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1895 (let ((temp (gensym))) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1896 `(let* ((,temp (multiple-value-list-internal 0 ,(length syms) ,form))) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1897 (setq ,@(loop |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1898 for sym in syms |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1899 nconc `(,sym (car-safe ,temp) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1900 ,temp (cdr-safe ,temp)))) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1901 ,(car syms)))))) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1902 |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1903 ;;;###autoload |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1904 (defmacro multiple-value-list (form) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1905 "Evaluate FORM and return a list of the multiple values it returned." |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1906 `(multiple-value-list-internal 0 multiple-values-limit ,form)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1907 |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1908 ;;;###autoload |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1909 (defmacro nth-value (n form) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1910 "Evaluate FORM and return the Nth multiple value it returned." |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1911 (if (integerp n) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1912 `(car (multiple-value-list-internal ,n ,(1+ n) ,form)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1913 (let ((temp (gensym))) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1914 `(let ((,temp ,n)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
1915 (car (multiple-value-list-internal ,temp (1+ ,temp) ,form)))))) |
428 | 1916 |
1917 ;;; Declarations. | |
1918 | |
1919 ;;;###autoload | |
1920 (defmacro locally (&rest body) (cons 'progn body)) | |
1921 ;;;###autoload | |
1922 (defmacro the (type form) form) | |
1923 | |
1924 (defvar cl-proclaim-history t) ; for future compilers | |
1925 (defvar cl-declare-stack t) ; for future compilers | |
1926 | |
1927 (defun cl-do-proclaim (spec hist) | |
2153 | 1928 (and hist (listp cl-proclaim-history) (push spec cl-proclaim-history)) |
428 | 1929 (cond ((eq (car-safe spec) 'special) |
1930 (if (boundp 'byte-compile-bound-variables) | |
1931 (setq byte-compile-bound-variables | |
442 | 1932 (append |
2153 | 1933 ;; XEmacs change |
442 | 1934 (mapcar #'(lambda (v) (cons v byte-compile-global-bit)) |
1935 (cdr spec)) | |
1936 byte-compile-bound-variables)))) | |
428 | 1937 |
1938 ((eq (car-safe spec) 'inline) | |
1939 (while (setq spec (cdr spec)) | |
1940 (or (memq (get (car spec) 'byte-optimizer) | |
1941 '(nil byte-compile-inline-expand)) | |
1942 (error "%s already has a byte-optimizer, can't make it inline" | |
1943 (car spec))) | |
1944 (put (car spec) 'byte-optimizer 'byte-compile-inline-expand))) | |
1945 | |
1946 ((eq (car-safe spec) 'notinline) | |
1947 (while (setq spec (cdr spec)) | |
1948 (if (eq (get (car spec) 'byte-optimizer) | |
1949 'byte-compile-inline-expand) | |
1950 (put (car spec) 'byte-optimizer nil)))) | |
1951 | |
1952 ((eq (car-safe spec) 'optimize) | |
1953 (let ((speed (assq (nth 1 (assq 'speed (cdr spec))) | |
446 | 1954 '((0 . nil) (1 . t) (2 . t) (3 . t)))) |
428 | 1955 (safety (assq (nth 1 (assq 'safety (cdr spec))) |
446 | 1956 '((0 . t) (1 . t) (2 . t) (3 . nil))))) |
1957 (when speed | |
1958 (setq cl-optimize-speed (car speed) | |
1959 byte-optimize (cdr speed))) | |
1960 (when safety | |
1961 (setq cl-optimize-safety (car safety) | |
1962 byte-compile-delete-errors (cdr safety))))) | |
428 | 1963 |
1964 ((and (eq (car-safe spec) 'warn) (boundp 'byte-compile-warnings)) | |
1965 (if (eq byte-compile-warnings t) | |
1966 ;; XEmacs change | |
1967 (setq byte-compile-warnings byte-compile-default-warnings)) | |
1968 (while (setq spec (cdr spec)) | |
1969 (if (consp (car spec)) | |
1970 (if (eq (cadar spec) 0) | |
1971 (setq byte-compile-warnings | |
1972 (delq (caar spec) byte-compile-warnings)) | |
1973 (setq byte-compile-warnings | |
1974 (adjoin (caar spec) byte-compile-warnings))))))) | |
1975 nil) | |
1976 | |
1977 ;;; Process any proclamations made before cl-macs was loaded. | |
1978 (defvar cl-proclaims-deferred) | |
1979 (let ((p (reverse cl-proclaims-deferred))) | |
2153 | 1980 (while p (cl-do-proclaim (pop p) t)) |
428 | 1981 (setq cl-proclaims-deferred nil)) |
1982 | |
1983 ;;;###autoload | |
1984 (defmacro declare (&rest specs) | |
1985 (if (cl-compiling-file) | |
1986 (while specs | |
2153 | 1987 (if (listp cl-declare-stack) (push (car specs) cl-declare-stack)) |
1988 (cl-do-proclaim (pop specs) nil))) | |
428 | 1989 nil) |
1990 | |
1991 | |
1992 | |
1993 ;;; Generalized variables. | |
1994 | |
1995 ;;;###autoload | |
1996 (defmacro define-setf-method (func args &rest body) | |
1997 "(define-setf-method NAME ARGLIST BODY...): define a `setf' method. | |
1998 This method shows how to handle `setf's to places of the form (NAME ARGS...). | |
1999 The argument forms ARGS are bound according to ARGLIST, as if NAME were | |
2000 going to be expanded as a macro, then the BODY forms are executed and must | |
2001 return a list of five elements: a temporary-variables list, a value-forms | |
2002 list, a store-variables list (of length one), a store-form, and an access- | |
2003 form. See `defsetf' for a simpler way to define most setf-methods." | |
2004 (append '(eval-when (compile load eval)) | |
2005 (if (stringp (car body)) | |
2006 (list (list 'put (list 'quote func) '(quote setf-documentation) | |
2153 | 2007 (pop body)))) |
428 | 2008 (list (cl-transform-function-property |
2009 func 'setf-method (cons args body))))) | |
2153 | 2010 (defalias 'define-setf-expander 'define-setf-method) |
428 | 2011 |
2012 ;;;###autoload | |
2013 (defmacro defsetf (func arg1 &rest args) | |
2014 "(defsetf NAME FUNC): define a `setf' method. | |
2015 This macro is an easy-to-use substitute for `define-setf-method' that works | |
2016 well for simple place forms. In the simple `defsetf' form, `setf's of | |
2017 the form (setf (NAME ARGS...) VAL) are transformed to function or macro | |
2018 calls of the form (FUNC ARGS... VAL). Example: (defsetf aref aset). | |
2019 Alternate form: (defsetf NAME ARGLIST (STORE) BODY...). | |
2020 Here, the above `setf' call is expanded by binding the argument forms ARGS | |
2021 according to ARGLIST, binding the value form VAL to STORE, then executing | |
2022 BODY, which must return a Lisp form that does the necessary `setf' operation. | |
2023 Actually, ARGLIST and STORE may be bound to temporary variables which are | |
2024 introduced automatically to preserve proper execution order of the arguments. | |
2025 Example: (defsetf nth (n x) (v) (list 'setcar (list 'nthcdr n x) v))." | |
2026 (if (listp arg1) | |
2027 (let* ((largs nil) (largsr nil) | |
2028 (temps nil) (tempsr nil) | |
2029 (restarg nil) (rest-temps nil) | |
2030 (store-var (car (prog1 (car args) (setq args (cdr args))))) | |
2031 (store-temp (intern (format "--%s--temp--" store-var))) | |
2032 (lets1 nil) (lets2 nil) | |
2033 (docstr nil) (p arg1)) | |
2034 (if (stringp (car args)) | |
2035 (setq docstr (prog1 (car args) (setq args (cdr args))))) | |
2036 (while (and p (not (eq (car p) '&aux))) | |
2037 (if (eq (car p) '&rest) | |
2038 (setq p (cdr p) restarg (car p)) | |
2039 (or (memq (car p) '(&optional &key &allow-other-keys)) | |
2040 (setq largs (cons (if (consp (car p)) (car (car p)) (car p)) | |
2041 largs) | |
2042 temps (cons (intern (format "--%s--temp--" (car largs))) | |
2043 temps)))) | |
2044 (setq p (cdr p))) | |
2045 (setq largs (nreverse largs) temps (nreverse temps)) | |
2046 (if restarg | |
2047 (setq largsr (append largs (list restarg)) | |
2048 rest-temps (intern (format "--%s--temp--" restarg)) | |
2049 tempsr (append temps (list rest-temps))) | |
2050 (setq largsr largs tempsr temps)) | |
2051 (let ((p1 largs) (p2 temps)) | |
2052 (while p1 | |
2053 (setq lets1 (cons (list (car p2) | |
2054 (list 'gensym (format "--%s--" (car p1)))) | |
2055 lets1) | |
2056 lets2 (cons (list (car p1) (car p2)) lets2) | |
2057 p1 (cdr p1) p2 (cdr p2)))) | |
2058 (if restarg (setq lets2 (cons (list restarg rest-temps) lets2))) | |
2059 (append (list 'define-setf-method func arg1) | |
2060 (and docstr (list docstr)) | |
2061 (list | |
2062 (list 'let* | |
2063 (nreverse | |
2064 (cons (list store-temp | |
2065 (list 'gensym (format "--%s--" store-var))) | |
2066 (if restarg | |
2067 (append | |
2068 (list | |
2069 (list rest-temps | |
2070 (list 'mapcar '(quote gensym) | |
2071 restarg))) | |
2072 lets1) | |
2073 lets1))) | |
2074 (list 'list ; 'values | |
2075 (cons (if restarg 'list* 'list) tempsr) | |
2076 (cons (if restarg 'list* 'list) largsr) | |
2077 (list 'list store-temp) | |
2078 (cons 'let* | |
2079 (cons (nreverse | |
2080 (cons (list store-var store-temp) | |
2081 lets2)) | |
2082 args)) | |
2083 (cons (if restarg 'list* 'list) | |
2084 (cons (list 'quote func) tempsr))))))) | |
2085 (list 'defsetf func '(&rest args) '(store) | |
2086 (let ((call (list 'cons (list 'quote arg1) | |
2087 '(append args (list store))))) | |
2088 (if (car args) | |
2089 (list 'list '(quote progn) call 'store) | |
2090 call))))) | |
2091 | |
2092 ;;; Some standard place types from Common Lisp. | |
2153 | 2093 (eval-when-compile (defvar ignored-arg)) ; XEmacs: warning suppression |
428 | 2094 (defsetf aref aset) |
2095 (defsetf car setcar) | |
2096 (defsetf cdr setcdr) | |
2153 | 2097 (defsetf caar (x) (val) (list 'setcar (list 'car x) val)) |
2098 (defsetf cadr (x) (val) (list 'setcar (list 'cdr x) val)) | |
2099 (defsetf cdar (x) (val) (list 'setcdr (list 'car x) val)) | |
2100 (defsetf cddr (x) (val) (list 'setcdr (list 'cdr x) val)) | |
428 | 2101 (defsetf elt (seq n) (store) |
2102 (list 'if (list 'listp seq) (list 'setcar (list 'nthcdr n seq) store) | |
2103 (list 'aset seq n store))) | |
2153 | 2104 ;; XEmacs change: ignore the optional DEFAULT arguments |
428 | 2105 (defsetf get (x y &optional ignored-arg) (store) (list 'put x y store)) |
2106 (defsetf get* (x y &optional ignored-arg) (store) (list 'put x y store)) | |
2153 | 2107 (defsetf gethash (x h &optional ignored-arg) (store) (list 'puthash x store h)) |
428 | 2108 (defsetf nth (n x) (store) (list 'setcar (list 'nthcdr n x) store)) |
2109 (defsetf subseq (seq start &optional end) (new) | |
2153 | 2110 (list 'progn (list 'replace seq new :start1 start :end1 end) new)) |
428 | 2111 (defsetf symbol-function fset) |
2112 (defsetf symbol-plist setplist) | |
2113 (defsetf symbol-value set) | |
2114 | |
2115 ;;; Various car/cdr aliases. Note that `cadr' is handled specially. | |
2116 (defsetf first setcar) | |
2117 (defsetf second (x) (store) (list 'setcar (list 'cdr x) store)) | |
2118 (defsetf third (x) (store) (list 'setcar (list 'cddr x) store)) | |
2119 (defsetf fourth (x) (store) (list 'setcar (list 'cdddr x) store)) | |
2120 (defsetf fifth (x) (store) (list 'setcar (list 'nthcdr 4 x) store)) | |
2121 (defsetf sixth (x) (store) (list 'setcar (list 'nthcdr 5 x) store)) | |
2122 (defsetf seventh (x) (store) (list 'setcar (list 'nthcdr 6 x) store)) | |
2123 (defsetf eighth (x) (store) (list 'setcar (list 'nthcdr 7 x) store)) | |
2124 (defsetf ninth (x) (store) (list 'setcar (list 'nthcdr 8 x) store)) | |
2125 (defsetf tenth (x) (store) (list 'setcar (list 'nthcdr 9 x) store)) | |
2126 (defsetf rest setcdr) | |
2127 | |
2128 ;;; Some more Emacs-related place types. | |
2129 (defsetf buffer-file-name set-visited-file-name t) | |
2153 | 2130 ;; XEmacs change: we do not need to wrap this in with-current-buffer |
428 | 2131 (defsetf buffer-modified-p set-buffer-modified-p t) |
2132 (defsetf buffer-name rename-buffer t) | |
2133 (defsetf buffer-string () (store) | |
2134 (list 'progn '(erase-buffer) (list 'insert store))) | |
2135 (defsetf buffer-substring cl-set-buffer-substring) | |
2136 (defsetf current-buffer set-buffer) | |
2137 (defsetf current-case-table set-case-table) | |
2138 (defsetf current-column move-to-column t) | |
2139 (defsetf current-global-map use-global-map t) | |
2140 (defsetf current-input-mode () (store) | |
2141 (list 'progn (list 'apply 'set-input-mode store) store)) | |
2142 (defsetf current-local-map use-local-map t) | |
2143 (defsetf current-window-configuration set-window-configuration t) | |
2144 (defsetf default-file-modes set-default-file-modes t) | |
2145 (defsetf default-value set-default) | |
2146 (defsetf documentation-property put) | |
2153 | 2147 ;;(defsetf extent-data set-extent-data) |
428 | 2148 (defsetf extent-face set-extent-face) |
2149 (defsetf extent-priority set-extent-priority) | |
2153 | 2150 ;; XEmacs addition |
428 | 2151 (defsetf extent-property (x y &optional ignored-arg) (arg) |
2152 (list 'set-extent-property x y arg)) | |
2153 | 2153 (defsetf extent-end-position (ext) (store) |
2154 `(progn (set-extent-endpoints ,ext (extent-start-position ,ext) ,store) | |
2155 ,store)) | |
428 | 2156 (defsetf extent-start-position (ext) (store) |
2157 `(progn (set-extent-endpoints ,ext ,store (extent-end-position ,ext)) | |
2158 ,store)) | |
2159 (defsetf face-background (f &optional s) (x) (list 'set-face-background f x s)) | |
2160 (defsetf face-background-pixmap (f &optional s) (x) | |
2161 (list 'set-face-background-pixmap f x s)) | |
5080
5502045ec510
The background-placement face property.
Didier Verna <didier@lrde.epita.fr>
parents:
5076
diff
changeset
|
2162 (defsetf face-background-placement (f &optional s) (x) |
5502045ec510
The background-placement face property.
Didier Verna <didier@lrde.epita.fr>
parents:
5076
diff
changeset
|
2163 (list 'set-face-background-placement f x s)) |
428 | 2164 (defsetf face-font (f &optional s) (x) (list 'set-face-font f x s)) |
2165 (defsetf face-foreground (f &optional s) (x) (list 'set-face-foreground f x s)) | |
2166 (defsetf face-underline-p (f &optional s) (x) | |
2167 (list 'set-face-underline-p f x s)) | |
2168 (defsetf file-modes set-file-modes t) | |
2153 | 2169 (defsetf frame-height (&optional f) (v) |
2170 `(progn (set-frame-height ,f ,v) ,v)) | |
428 | 2171 (defsetf frame-parameters modify-frame-parameters t) |
2172 (defsetf frame-visible-p cl-set-frame-visible-p) | |
2153 | 2173 (defsetf frame-width (&optional f) (v) |
2174 `(progn (set-frame-width ,f ,v) ,v)) | |
2175 ;; XEmacs change: frame-properties instead of frame-parameters | |
428 | 2176 (defsetf frame-properties (&optional f) (p) |
2177 `(progn (set-frame-properties ,f ,p) ,p)) | |
2178 (defsetf frame-property (f p &optional ignored-arg) (v) | |
2179 `(progn (set-frame-property ,f ,v) ,p)) | |
2153 | 2180 ;; XEmacs addition |
428 | 2181 (defsetf current-frame-configuration set-frame-configuration) |
2182 | |
2183 ;; XEmacs: new stuff | |
2184 ;; Consoles | |
2185 (defsetf selected-console select-console t) | |
2186 (defsetf selected-device select-device t) | |
2187 (defsetf device-baud-rate (&optional d) (v) | |
2188 `(set-device-baud-rate ,d ,v)) | |
2189 ;; This setf method is a bad idea, because set-specifier *adds* a | |
2190 ;; specification, rather than just setting it. The net effect is that | |
2191 ;; it makes specifier-instance return VAL, but other things don't work | |
2192 ;; as expected -- letf, to name one. | |
2193 ;(defsetf specifier-instance (spec &optional dom def nof) (val) | |
2194 ; `(set-specifier ,spec ,val ,dom)) | |
2195 | |
2196 ;; Annotations | |
2197 (defsetf annotation-glyph set-annotation-glyph) | |
2198 (defsetf annotation-down-glyph set-annotation-down-glyph) | |
2199 (defsetf annotation-face set-annotation-face) | |
2200 (defsetf annotation-layout set-annotation-layout) | |
2201 (defsetf annotation-data set-annotation-data) | |
2202 (defsetf annotation-action set-annotation-action) | |
2203 (defsetf annotation-menu set-annotation-menu) | |
2204 ;; Widget | |
2205 (defsetf widget-get widget-put t) | |
2206 (defsetf widget-value widget-value-set t) | |
2207 | |
2208 ;; Misc | |
2209 (defsetf recent-keys-ring-size set-recent-keys-ring-size) | |
2210 (defsetf symbol-value-in-buffer (s b &optional ignored-arg) (store) | |
2211 `(with-current-buffer ,b (set ,s ,store))) | |
2212 (defsetf symbol-value-in-console (s c &optional ignored-arg) (store) | |
2213 `(letf (((selected-console) ,c)) | |
2214 (set ,s ,store))) | |
2215 | |
2216 (defsetf buffer-dedicated-frame (&optional b) (v) | |
2217 `(set-buffer-dedicated-frame ,b ,v)) | |
2218 (defsetf console-type-image-conversion-list | |
2219 set-console-type-image-conversion-list) | |
2220 (defsetf default-toolbar-position set-default-toolbar-position) | |
2221 (defsetf device-class (&optional d) (v) | |
2222 `(set-device-class ,d ,v)) | |
2223 (defsetf extent-begin-glyph set-extent-begin-glyph) | |
2224 (defsetf extent-begin-glyph-layout set-extent-begin-glyph-layout) | |
2225 (defsetf extent-end-glyph set-extent-end-glyph) | |
2226 (defsetf extent-end-glyph-layout set-extent-end-glyph-layout) | |
2227 (defsetf extent-keymap set-extent-keymap) | |
2228 (defsetf extent-parent set-extent-parent) | |
2229 (defsetf extent-properties set-extent-properties) | |
2230 ;; Avoid adding various face and glyph functions. | |
2231 (defsetf frame-selected-window (&optional f) (v) | |
2232 `(set-frame-selected-window ,f ,v)) | |
2233 (defsetf glyph-image (glyph &optional domain) (i) | |
2234 (list 'set-glyph-image glyph i domain)) | |
2235 (defsetf itimer-function set-itimer-function) | |
2236 (defsetf itimer-function-arguments set-itimer-function-arguments) | |
2237 (defsetf itimer-is-idle set-itimer-is-idle) | |
2238 (defsetf itimer-recorded-run-time set-itimer-recorded-run-time) | |
2239 (defsetf itimer-restart set-itimer-restart) | |
2240 (defsetf itimer-uses-arguments set-itimer-uses-arguments) | |
2241 (defsetf itimer-value set-itimer-value) | |
2242 (defsetf keymap-parents set-keymap-parents) | |
2243 (defsetf marker-insertion-type set-marker-insertion-type) | |
2244 (defsetf mouse-pixel-position (&optional d) (v) | |
2245 `(progn | |
2246 (set-mouse-pixel-position ,d ,(car v) ,(car (cdr v)) ,(cdr (cdr v))) | |
2247 ,v)) | |
2248 (defsetf trunc-stack-length set-trunc-stack-length) | |
2249 (defsetf trunc-stack-stack set-trunc-stack-stack) | |
2250 (defsetf undoable-stack-max set-undoable-stack-max) | |
2251 (defsetf weak-list-list set-weak-list-list) | |
2153 | 2252 ;; End of new XEmacs stuff |
428 | 2253 |
2254 (defsetf getenv setenv t) | |
2255 (defsetf get-register set-register) | |
2256 (defsetf global-key-binding global-set-key) | |
2257 (defsetf keymap-parent set-keymap-parent) | |
2153 | 2258 ;; XEmacs addition: more keymap-related setf forms |
428 | 2259 (defsetf keymap-name set-keymap-name) |
2260 (defsetf keymap-prompt set-keymap-prompt) | |
2261 (defsetf keymap-default-binding set-keymap-default-binding) | |
2262 (defsetf local-key-binding local-set-key) | |
2263 (defsetf mark set-mark t) | |
2264 (defsetf mark-marker set-mark t) | |
2265 (defsetf marker-position set-marker t) | |
2266 (defsetf match-data store-match-data t) | |
2267 (defsetf mouse-position (scr) (store) | |
2268 (list 'set-mouse-position scr (list 'car store) (list 'cadr store) | |
2269 (list 'cddr store))) | |
2270 (defsetf overlay-get overlay-put) | |
2271 (defsetf overlay-start (ov) (store) | |
2272 (list 'progn (list 'move-overlay ov store (list 'overlay-end ov)) store)) | |
2273 (defsetf overlay-end (ov) (store) | |
2274 (list 'progn (list 'move-overlay ov (list 'overlay-start ov) store) store)) | |
2275 (defsetf point goto-char) | |
2276 (defsetf point-marker goto-char t) | |
2277 (defsetf point-max () (store) | |
2278 (list 'progn (list 'narrow-to-region '(point-min) store) store)) | |
2279 (defsetf point-min () (store) | |
2280 (list 'progn (list 'narrow-to-region store '(point-max)) store)) | |
2281 (defsetf process-buffer set-process-buffer) | |
2282 (defsetf process-filter set-process-filter) | |
2283 (defsetf process-sentinel set-process-sentinel) | |
2153 | 2284 ;;(defsetf process-get process-put) |
428 | 2285 (defsetf read-mouse-position (scr) (store) |
2286 (list 'set-mouse-position scr (list 'car store) (list 'cdr store))) | |
2153 | 2287 ;;(defsetf screen-height set-screen-height t) |
2288 ;;(defsetf screen-width set-screen-width t) | |
428 | 2289 (defsetf selected-window select-window) |
2153 | 2290 ;;(defsetf selected-screen select-screen) |
428 | 2291 (defsetf selected-frame select-frame) |
2292 (defsetf standard-case-table set-standard-case-table) | |
2293 (defsetf syntax-table set-syntax-table) | |
2294 (defsetf visited-file-modtime set-visited-file-modtime t) | |
2295 (defsetf window-buffer set-window-buffer t) | |
2296 (defsetf window-display-table set-window-display-table t) | |
2297 (defsetf window-dedicated-p set-window-dedicated-p t) | |
2298 (defsetf window-height (&optional window) (store) | |
2299 `(progn (enlarge-window (- ,store (window-height)) nil ,window) ,store)) | |
2300 (defsetf window-hscroll set-window-hscroll) | |
2301 (defsetf window-point set-window-point) | |
2302 (defsetf window-start set-window-start) | |
2303 (defsetf window-width (&optional window) (store) | |
2304 `(progn (enlarge-window (- ,store (window-width)) t ,window) ,store)) | |
2305 (defsetf x-get-cutbuffer x-store-cutbuffer t) | |
2306 (defsetf x-get-cut-buffer x-store-cut-buffer t) ; groan. | |
2307 (defsetf x-get-secondary-selection x-own-secondary-selection t) | |
2308 (defsetf x-get-selection x-own-selection t) | |
442 | 2309 (defsetf get-selection own-selection t) |
428 | 2310 |
2311 ;;; More complex setf-methods. | |
2312 ;;; These should take &environment arguments, but since full arglists aren't | |
2313 ;;; available while compiling cl-macs, we fake it by referring to the global | |
2314 ;;; variable cl-macro-environment directly. | |
2315 | |
2316 (define-setf-method apply (func arg1 &rest rest) | |
2317 (or (and (memq (car-safe func) '(quote function function*)) | |
2318 (symbolp (car-safe (cdr-safe func)))) | |
2319 (error "First arg to apply in setf is not (function SYM): %s" func)) | |
2320 (let* ((form (cons (nth 1 func) (cons arg1 rest))) | |
2321 (method (get-setf-method form cl-macro-environment))) | |
2322 (list (car method) (nth 1 method) (nth 2 method) | |
2323 (cl-setf-make-apply (nth 3 method) (cadr func) (car method)) | |
2324 (cl-setf-make-apply (nth 4 method) (cadr func) (car method))))) | |
2325 | |
2326 (defun cl-setf-make-apply (form func temps) | |
2327 (if (eq (car form) 'progn) | |
2328 (list* 'progn (cl-setf-make-apply (cadr form) func temps) (cddr form)) | |
2329 (or (equal (last form) (last temps)) | |
2330 (error "%s is not suitable for use with setf-of-apply" func)) | |
2331 (list* 'apply (list 'quote (car form)) (cdr form)))) | |
2332 | |
2333 (define-setf-method nthcdr (n place) | |
2334 (let ((method (get-setf-method place cl-macro-environment)) | |
2335 (n-temp (gensym "--nthcdr-n--")) | |
2336 (store-temp (gensym "--nthcdr-store--"))) | |
2337 (list (cons n-temp (car method)) | |
2338 (cons n (nth 1 method)) | |
2339 (list store-temp) | |
2340 (list 'let (list (list (car (nth 2 method)) | |
2341 (list 'cl-set-nthcdr n-temp (nth 4 method) | |
2342 store-temp))) | |
2343 (nth 3 method) store-temp) | |
2344 (list 'nthcdr n-temp (nth 4 method))))) | |
2345 | |
2346 (define-setf-method getf (place tag &optional def) | |
2347 (let ((method (get-setf-method place cl-macro-environment)) | |
2348 (tag-temp (gensym "--getf-tag--")) | |
2349 (def-temp (gensym "--getf-def--")) | |
2350 (store-temp (gensym "--getf-store--"))) | |
2351 (list (append (car method) (list tag-temp def-temp)) | |
2352 (append (nth 1 method) (list tag def)) | |
2353 (list store-temp) | |
2354 (list 'let (list (list (car (nth 2 method)) | |
2355 (list 'cl-set-getf (nth 4 method) | |
2356 tag-temp store-temp))) | |
2357 (nth 3 method) store-temp) | |
2358 (list 'getf (nth 4 method) tag-temp def-temp)))) | |
2359 | |
2360 (define-setf-method substring (place from &optional to) | |
2361 (let ((method (get-setf-method place cl-macro-environment)) | |
2362 (from-temp (gensym "--substring-from--")) | |
2363 (to-temp (gensym "--substring-to--")) | |
2364 (store-temp (gensym "--substring-store--"))) | |
2365 (list (append (car method) (list from-temp to-temp)) | |
2366 (append (nth 1 method) (list from to)) | |
2367 (list store-temp) | |
2368 (list 'let (list (list (car (nth 2 method)) | |
2369 (list 'cl-set-substring (nth 4 method) | |
2370 from-temp to-temp store-temp))) | |
2371 (nth 3 method) store-temp) | |
2372 (list 'substring (nth 4 method) from-temp to-temp)))) | |
2373 | |
2153 | 2374 ;; XEmacs addition |
428 | 2375 (define-setf-method values (&rest args) |
2376 (let ((methods (mapcar #'(lambda (x) | |
2377 (get-setf-method x cl-macro-environment)) | |
2378 args)) | |
2379 (store-temp (gensym "--values-store--"))) | |
2380 (list (apply 'append (mapcar 'first methods)) | |
2381 (apply 'append (mapcar 'second methods)) | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
2382 `((,store-temp |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
2383 (multiple-value-list-internal 0 ,(if args (length args) 1)))) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
2384 (cons 'values |
428 | 2385 (mapcar #'(lambda (m) |
2386 (cl-setf-do-store (cons (car (third m)) (fourth m)) | |
2387 (list 'pop store-temp))) | |
2388 methods)) | |
2389 (cons 'list (mapcar 'fifth methods))))) | |
2390 | |
2391 ;;; Getting and optimizing setf-methods. | |
2392 ;;;###autoload | |
2393 (defun get-setf-method (place &optional env) | |
2394 "Return a list of five values describing the setf-method for PLACE. | |
2395 PLACE may be any Lisp form which can appear as the PLACE argument to | |
2396 a macro like `setf' or `incf'." | |
2397 (if (symbolp place) | |
2398 (let ((temp (gensym "--setf--"))) | |
2399 (list nil nil (list temp) (list 'setq place temp) place)) | |
2400 (or (and (symbolp (car place)) | |
2401 (let* ((func (car place)) | |
2402 (name (symbol-name func)) | |
2403 (method (get func 'setf-method)) | |
2404 (case-fold-search nil)) | |
2405 (or (and method | |
2406 (let ((cl-macro-environment env)) | |
2407 (setq method (apply method (cdr place)))) | |
2408 (if (and (consp method) (= (length method) 5)) | |
2409 method | |
2410 (error "Setf-method for %s returns malformed method" | |
2411 func))) | |
2412 (and (save-match-data | |
2413 (string-match "\\`c[ad][ad][ad]?[ad]?r\\'" name)) | |
2414 (get-setf-method (compiler-macroexpand place))) | |
2415 (and (eq func 'edebug-after) | |
2416 (get-setf-method (nth (1- (length place)) place) | |
2417 env))))) | |
2418 (if (eq place (setq place (macroexpand place env))) | |
2419 (if (and (symbolp (car place)) (fboundp (car place)) | |
2420 (symbolp (symbol-function (car place)))) | |
2421 (get-setf-method (cons (symbol-function (car place)) | |
2422 (cdr place)) env) | |
2423 (error "No setf-method known for %s" (car place))) | |
2424 (get-setf-method place env))))) | |
2425 | |
2426 (defun cl-setf-do-modify (place opt-expr) | |
2427 (let* ((method (get-setf-method place cl-macro-environment)) | |
2428 (temps (car method)) (values (nth 1 method)) | |
2429 (lets nil) (subs nil) | |
2430 (optimize (and (not (eq opt-expr 'no-opt)) | |
2431 (or (and (not (eq opt-expr 'unsafe)) | |
2432 (cl-safe-expr-p opt-expr)) | |
2433 (cl-setf-simple-store-p (car (nth 2 method)) | |
2434 (nth 3 method))))) | |
2435 (simple (and optimize (consp place) (cl-simple-exprs-p (cdr place))))) | |
2436 (while values | |
2437 (if (or simple (cl-const-expr-p (car values))) | |
2153 | 2438 (push (cons (pop temps) (pop values)) subs) |
2439 (push (list (pop temps) (pop values)) lets))) | |
428 | 2440 (list (nreverse lets) |
2441 (cons (car (nth 2 method)) (sublis subs (nth 3 method))) | |
2442 (sublis subs (nth 4 method))))) | |
2443 | |
2444 (defun cl-setf-do-store (spec val) | |
2445 (let ((sym (car spec)) | |
2446 (form (cdr spec))) | |
4677
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
2447 (if (consp sym) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
2448 ;; XEmacs change, only used for implementing #'values at the moment. |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
2449 (let* ((orig (copy-list sym)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
2450 (intermediate (last orig)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
2451 (circular-limit 32)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
2452 (while (consp (car intermediate)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
2453 (when (zerop circular-limit) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
2454 (error 'circular-list "Form seems to contain loops")) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
2455 (setq intermediate (last (car intermediate)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
2456 circular-limit (1- circular-limit))) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
2457 (setcdr intermediate (list val)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
2458 `(let (,orig) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
2459 ,form)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
2460 (if (or (cl-const-expr-p val) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
2461 (and (cl-simple-expr-p val) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
2462 (eq (cl-expr-contains form sym) 1)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
2463 (cl-setf-simple-store-p sym form)) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
2464 (subst val sym form) |
8f1ee2d15784
Support full Common Lisp multiple values in C.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
2465 (list 'let (list (list sym val)) form))))) |
428 | 2466 |
2467 (defun cl-setf-simple-store-p (sym form) | |
2468 (and (consp form) (eq (cl-expr-contains form sym) 1) | |
2469 (eq (nth (1- (length form)) form) sym) | |
2470 (symbolp (car form)) (fboundp (car form)) | |
2471 (not (eq (car-safe (symbol-function (car form))) 'macro)))) | |
2472 | |
2473 ;;; The standard modify macros. | |
2474 ;;;###autoload | |
2475 (defmacro setf (&rest args) | |
2476 "(setf PLACE VAL PLACE VAL ...): set each PLACE to the value of its VAL. | |
2477 This is a generalized version of `setq'; the PLACEs may be symbolic | |
2478 references such as (car x) or (aref x i), as well as plain symbols. | |
2479 For example, (setf (cadar x) y) is equivalent to (setcar (cdar x) y). | |
2480 The return value is the last VAL in the list." | |
2481 (if (cdr (cdr args)) | |
2482 (let ((sets nil)) | |
2153 | 2483 (while args (push (list 'setf (pop args) (pop args)) sets)) |
428 | 2484 (cons 'progn (nreverse sets))) |
2485 (if (symbolp (car args)) | |
2486 (and args (cons 'setq args)) | |
2487 (let* ((method (cl-setf-do-modify (car args) (nth 1 args))) | |
2488 (store (cl-setf-do-store (nth 1 method) (nth 1 args)))) | |
2489 (if (car method) (list 'let* (car method) store) store))))) | |
2490 | |
2491 ;;;###autoload | |
2492 (defmacro psetf (&rest args) | |
2493 "(psetf PLACE VAL PLACE VAL ...): set PLACEs to the values VALs in parallel. | |
2494 This is like `setf', except that all VAL forms are evaluated (in order) | |
2495 before assigning any PLACEs to the corresponding values." | |
2496 (let ((p args) (simple t) (vars nil)) | |
2497 (while p | |
2498 (if (or (not (symbolp (car p))) (cl-expr-depends-p (nth 1 p) vars)) | |
2499 (setq simple nil)) | |
2500 (if (memq (car p) vars) | |
2501 (error "Destination duplicated in psetf: %s" (car p))) | |
2153 | 2502 (push (pop p) vars) |
428 | 2503 (or p (error "Odd number of arguments to psetf")) |
2153 | 2504 (pop p)) |
428 | 2505 (if simple |
2506 (list 'progn (cons 'setf args) nil) | |
2507 (setq args (reverse args)) | |
2508 (let ((expr (list 'setf (cadr args) (car args)))) | |
2509 (while (setq args (cddr args)) | |
2510 (setq expr (list 'setf (cadr args) (list 'prog1 (car args) expr)))) | |
2511 (list 'progn expr nil))))) | |
2512 | |
2513 ;;;###autoload | |
2514 (defun cl-do-pop (place) | |
2515 (if (cl-simple-expr-p place) | |
2516 (list 'prog1 (list 'car place) (list 'setf place (list 'cdr place))) | |
2517 (let* ((method (cl-setf-do-modify place t)) | |
2518 (temp (gensym "--pop--"))) | |
2519 (list 'let* | |
2520 (append (car method) | |
2521 (list (list temp (nth 2 method)))) | |
2522 (list 'prog1 | |
2523 (list 'car temp) | |
2524 (cl-setf-do-store (nth 1 method) (list 'cdr temp))))))) | |
2525 | |
2526 ;;;###autoload | |
2527 (defmacro remf (place tag) | |
4695
fee33ab25966
Add arglist info for autoloaded functions and macros.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4694
diff
changeset
|
2528 "Remove TAG from property list PLACE. |
428 | 2529 PLACE may be a symbol, or any generalized variable allowed by `setf'. |
2530 The form returns true if TAG was found and removed, nil otherwise." | |
2531 (let* ((method (cl-setf-do-modify place t)) | |
2532 (tag-temp (and (not (cl-const-expr-p tag)) (gensym "--remf-tag--"))) | |
2533 (val-temp (and (not (cl-simple-expr-p place)) | |
2534 (gensym "--remf-place--"))) | |
2535 (ttag (or tag-temp tag)) | |
2536 (tval (or val-temp (nth 2 method)))) | |
2537 (list 'let* | |
2538 (append (car method) | |
2539 (and val-temp (list (list val-temp (nth 2 method)))) | |
2540 (and tag-temp (list (list tag-temp tag)))) | |
2541 (list 'if (list 'eq ttag (list 'car tval)) | |
2542 (list 'progn | |
2543 (cl-setf-do-store (nth 1 method) (list 'cddr tval)) | |
2544 t) | |
2545 (list 'cl-do-remf tval ttag))))) | |
2546 | |
2547 ;;;###autoload | |
2548 (defmacro shiftf (place &rest args) | |
2549 "(shiftf PLACE PLACE... VAL): shift left among PLACEs. | |
2550 Example: (shiftf A B C) sets A to B, B to C, and returns the old A. | |
2551 Each PLACE may be a symbol, or any generalized variable allowed by `setf'." | |
2153 | 2552 ;; XEmacs change: use iteration instead of recursion |
428 | 2553 (if (not (memq nil (mapcar 'symbolp (butlast (cons place args))))) |
2554 (list* 'prog1 place | |
2555 (let ((sets nil)) | |
2556 (while args | |
2153 | 2557 (push (list 'setq place (car args)) sets) |
2558 (setq place (pop args))) | |
428 | 2559 (nreverse sets))) |
2560 (let* ((places (reverse (cons place args))) | |
2153 | 2561 (form (pop places))) |
428 | 2562 (while places |
2153 | 2563 (let ((method (cl-setf-do-modify (pop places) 'unsafe))) |
428 | 2564 (setq form (list 'let* (car method) |
2565 (list 'prog1 (nth 2 method) | |
2566 (cl-setf-do-store (nth 1 method) form)))))) | |
2567 form))) | |
2568 | |
2569 ;;;###autoload | |
2570 (defmacro rotatef (&rest args) | |
2571 "(rotatef PLACE...): rotate left among PLACEs. | |
2572 Example: (rotatef A B C) sets A to B, B to C, and C to A. It returns nil. | |
2573 Each PLACE may be a symbol, or any generalized variable allowed by `setf'." | |
2574 (if (not (memq nil (mapcar 'symbolp args))) | |
2575 (and (cdr args) | |
2576 (let ((sets nil) | |
2577 (first (car args))) | |
2578 (while (cdr args) | |
2153 | 2579 (setq sets (nconc sets (list (pop args) (car args))))) |
428 | 2580 (nconc (list 'psetf) sets (list (car args) first)))) |
2581 (let* ((places (reverse args)) | |
2582 (temp (gensym "--rotatef--")) | |
2583 (form temp)) | |
2584 (while (cdr places) | |
2153 | 2585 (let ((method (cl-setf-do-modify (pop places) 'unsafe))) |
428 | 2586 (setq form (list 'let* (car method) |
2587 (list 'prog1 (nth 2 method) | |
2588 (cl-setf-do-store (nth 1 method) form)))))) | |
2589 (let ((method (cl-setf-do-modify (car places) 'unsafe))) | |
2590 (list 'let* (append (car method) (list (list temp (nth 2 method)))) | |
2591 (cl-setf-do-store (nth 1 method) form) nil))))) | |
2592 | |
4742
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2593 ;; This function is not in Common Lisp, and there are gaps in its structure and |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2594 ;; implementation that reflect that it was never well-specified. E.g. with |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2595 ;; setf, the question of whether a PLACE is bound or not and how to make it |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2596 ;; unbound doesn't arise, but we need some way of specifying that for letf to |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2597 ;; be sensible for gethash, symbol-value and so on; currently we just hard-code |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2598 ;; symbol-value, symbol-function and values (the latter is XEmacs work that |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2599 ;; I've just done) in the body of this function, and the following gives the |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2600 ;; wrong behaviour for gethash: |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2601 ;; |
4820
e6dec75ded0e
Use keywords, not ordinary symbols, in the structure syntax for hash tables.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4810
diff
changeset
|
2602 ;; (setq my-hash-table #s(hash-table :test equal :data ()) |
4742
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2603 ;; print-gensym t) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2604 ;; => t |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2605 ;; (gethash "my-key" my-hash-table (gensym)) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2606 ;; => #:G68010 |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2607 ;; (letf (((gethash "my-key" my-hash-table) 4000)) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2608 ;; (message "key value is %S" (gethash "my-key" my-hash-table))) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2609 ;; => "key value is 4000" |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2610 ;; (gethash "my-key" my-hash-table (gensym)) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2611 ;; => nil ;; should be an uninterned symbol. |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2612 ;; |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2613 ;; Aidan Kehoe, Fr Nov 13 16:12:21 GMT 2009 |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2614 |
428 | 2615 ;;;###autoload |
2616 (defmacro letf (bindings &rest body) | |
2617 "(letf ((PLACE VALUE) ...) BODY...): temporarily bind to PLACEs. | |
2618 This is the analogue of `let', but with generalized variables (in the | |
2619 sense of `setf') for the PLACEs. Each PLACE is set to the corresponding | |
2620 VALUE, then the BODY forms are executed. On exit, either normally or | |
2621 because of a `throw' or error, the PLACEs are set back to their original | |
2622 values. Note that this macro is *not* available in Common Lisp. | |
2623 As a special case, if `(PLACE)' is used instead of `(PLACE VALUE)', | |
2624 the PLACE is not modified before executing BODY." | |
2625 (if (and (not (cdr bindings)) (cdar bindings) (symbolp (caar bindings))) | |
2626 (list* 'let bindings body) | |
2627 (let ((lets nil) | |
2628 (rev (reverse bindings))) | |
2629 (while rev | |
2630 (let* ((place (if (symbolp (caar rev)) | |
2631 (list 'symbol-value (list 'quote (caar rev))) | |
2632 (caar rev))) | |
2633 (value (cadar rev)) | |
2634 (method (cl-setf-do-modify place 'no-opt)) | |
2635 (save (gensym "--letf-save--")) | |
4742
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2636 (bound (and (memq (car place) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2637 '(symbol-value symbol-function values)) |
428 | 2638 (gensym "--letf-bound--"))) |
2639 (temp (and (not (cl-const-expr-p value)) (cdr bindings) | |
4742
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2640 (gensym "--letf-val--"))) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2641 (syms (and (eq 'values (car place)) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2642 (gensym "--letf-syms--"))) |
4783
e29fcfd8df5f
Eliminate most core code byte-compile warnings.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4742
diff
changeset
|
2643 (cursor (and syms (gensym "--letf-cursor--")))) |
428 | 2644 (setq lets (nconc (car method) |
4742
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2645 (cond |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2646 (syms |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2647 `((,syms ',(loop |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2648 for sym in (cdr place) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2649 nconc (if (symbolp sym) (list sym)))) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2650 (,cursor ,syms) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2651 (,bound nil) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2652 (,save |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2653 (prog2 |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2654 (while (consp ,cursor) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2655 (setq ,bound |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2656 (cons (and (boundp (car ,cursor)) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2657 (symbol-value |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2658 (car ,cursor))) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2659 ,bound) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2660 ,cursor (cdr ,cursor))) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2661 ;; Just using ,bound as a temporary |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2662 ;; variable here, to initialise ,save: |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2663 (nreverse ,bound) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2664 ;; Now, really initialise ,bound: |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2665 (setq ,cursor ,syms |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2666 ,bound nil |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2667 ,bound |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2668 (progn (while (consp ,cursor) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2669 (setq ,bound |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2670 (cons |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2671 (boundp (car ,cursor)) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2672 ,bound) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2673 ,cursor (cdr ,cursor))) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2674 (nreverse ,bound))))))) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2675 (bound |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2676 (list (list bound |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2677 (list (if (eq (car place) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2678 'symbol-value) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2679 'boundp 'fboundp) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2680 (nth 1 (nth 2 method)))) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2681 (list save (list 'and bound |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2682 (nth 2 method))))) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2683 (t |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2684 (list (list save (nth 2 method))))) |
428 | 2685 (and temp (list (list temp value))) |
2686 lets) | |
2687 body (list | |
2688 (list 'unwind-protect | |
2689 (cons 'progn | |
2690 (if (cdr (car rev)) | |
2691 (cons (cl-setf-do-store (nth 1 method) | |
2692 (or temp value)) | |
2693 body) | |
2694 body)) | |
4742
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2695 (cond |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2696 (syms |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2697 `(while (consp ,syms) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2698 (if (car ,bound) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2699 (set (car ,syms) (car ,save)) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2700 (makunbound (car ,syms))) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2701 (setq ,syms (cdr ,syms) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2702 ,bound (cdr ,bound) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2703 ,save (cdr ,save)))) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2704 (bound |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2705 (list 'if bound |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2706 (cl-setf-do-store (nth 1 method) save) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2707 (list (if (eq (car place) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2708 'symbol-function) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2709 'fmakunbound |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2710 'makunbound) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2711 (nth 1 (nth 2 method))))) |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2712 (t |
4cf435fcebbc
Make #'letf not error if handed a #'values form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4723
diff
changeset
|
2713 (cl-setf-do-store (nth 1 method) save))))) |
428 | 2714 rev (cdr rev)))) |
2715 (list* 'let* lets body)))) | |
2716 | |
2717 ;;;###autoload | |
2718 (defmacro letf* (bindings &rest body) | |
2719 "(letf* ((PLACE VALUE) ...) BODY...): temporarily bind to PLACEs. | |
2720 This is the analogue of `let*', but with generalized variables (in the | |
2721 sense of `setf') for the PLACEs. Each PLACE is set to the corresponding | |
2722 VALUE, then the BODY forms are executed. On exit, either normally or | |
2723 because of a `throw' or error, the PLACEs are set back to their original | |
2724 values. Note that this macro is *not* available in Common Lisp. | |
2725 As a special case, if `(PLACE)' is used instead of `(PLACE VALUE)', | |
2726 the PLACE is not modified before executing BODY." | |
2727 (if (null bindings) | |
2728 (cons 'progn body) | |
2729 (setq bindings (reverse bindings)) | |
2730 (while bindings | |
2153 | 2731 (setq body (list (list* 'letf (list (pop bindings)) body)))) |
428 | 2732 (car body))) |
2733 | |
2734 ;;;###autoload | |
2735 (defmacro callf (func place &rest args) | |
4695
fee33ab25966
Add arglist info for autoloaded functions and macros.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4694
diff
changeset
|
2736 "Set PLACE to (FUNC PLACE ARGS...). |
428 | 2737 FUNC should be an unquoted function name. PLACE may be a symbol, |
2738 or any generalized variable allowed by `setf'." | |
2739 (let* ((method (cl-setf-do-modify place (cons 'list args))) | |
2740 (rargs (cons (nth 2 method) args))) | |
2741 (list 'let* (car method) | |
2742 (cl-setf-do-store (nth 1 method) | |
2743 (if (symbolp func) (cons func rargs) | |
2744 (list* 'funcall (list 'function func) | |
2745 rargs)))))) | |
2746 | |
2747 ;;;###autoload | |
2748 (defmacro callf2 (func arg1 place &rest args) | |
4695
fee33ab25966
Add arglist info for autoloaded functions and macros.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4694
diff
changeset
|
2749 "Set PLACE to (FUNC ARG1 PLACE ARGS...). |
428 | 2750 Like `callf', but PLACE is the second argument of FUNC, not the first." |
2751 (if (and (cl-safe-expr-p arg1) (cl-simple-expr-p place) (symbolp func)) | |
2752 (list 'setf place (list* func arg1 place args)) | |
2753 (let* ((method (cl-setf-do-modify place (cons 'list args))) | |
2754 (temp (and (not (cl-const-expr-p arg1)) (gensym "--arg1--"))) | |
2755 (rargs (list* (or temp arg1) (nth 2 method) args))) | |
2756 (list 'let* (append (and temp (list (list temp arg1))) (car method)) | |
2757 (cl-setf-do-store (nth 1 method) | |
2758 (if (symbolp func) (cons func rargs) | |
2759 (list* 'funcall (list 'function func) | |
2760 rargs))))))) | |
2761 | |
2762 ;;;###autoload | |
2763 (defmacro define-modify-macro (name arglist func &optional doc) | |
4695
fee33ab25966
Add arglist info for autoloaded functions and macros.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4694
diff
changeset
|
2764 "Define a `setf'-like modify macro. |
428 | 2765 If NAME is called, it combines its PLACE argument with the other arguments |
2766 from ARGLIST using FUNC: (define-modify-macro incf (&optional (n 1)) +)" | |
2767 (if (memq '&key arglist) (error "&key not allowed in define-modify-macro")) | |
2768 (let ((place (gensym "--place--"))) | |
2769 (list 'defmacro* name (cons place arglist) doc | |
2770 (list* (if (memq '&rest arglist) 'list* 'list) | |
2771 '(quote callf) (list 'quote func) place | |
2772 (cl-arglist-args arglist))))) | |
2773 | |
2774 | |
2775 ;;; Structures. | |
2776 | |
2777 ;;;###autoload | |
2778 (defmacro defstruct (struct &rest descs) | |
2779 "(defstruct (NAME OPTIONS...) (SLOT SLOT-OPTS...)...): define a struct type. | |
2780 This macro defines a new Lisp data type called NAME, which contains data | |
2781 stored in SLOTs. This defines a `make-NAME' constructor, a `copy-NAME' | |
2782 copier, a `NAME-p' predicate, and setf-able `NAME-SLOT' accessors." | |
2783 (let* ((name (if (consp struct) (car struct) struct)) | |
2784 (opts (cdr-safe struct)) | |
2785 (slots nil) | |
2786 (defaults nil) | |
2787 (conc-name (concat (symbol-name name) "-")) | |
2788 (constructor (intern (format "make-%s" name))) | |
2789 (constrs nil) | |
2790 (copier (intern (format "copy-%s" name))) | |
2791 (predicate (intern (format "%s-p" name))) | |
2792 (print-func nil) (print-auto nil) | |
2793 (safety (if (cl-compiling-file) cl-optimize-safety 3)) | |
2794 (include nil) | |
2795 (tag (intern (format "cl-struct-%s" name))) | |
2796 (tag-symbol (intern (format "cl-struct-%s-tags" name))) | |
2797 (include-descs nil) | |
2798 (side-eff nil) | |
2799 (type nil) | |
2800 (named nil) | |
2801 (forms nil) | |
2802 pred-form pred-check) | |
2803 (if (stringp (car descs)) | |
2153 | 2804 (push (list 'put (list 'quote name) '(quote structure-documentation) |
2805 (pop descs)) forms)) | |
428 | 2806 (setq descs (cons '(cl-tag-slot) |
2807 (mapcar #'(lambda (x) (if (consp x) x (list x))) | |
2808 descs))) | |
2809 (while opts | |
2810 (let ((opt (if (consp (car opts)) (caar opts) (car opts))) | |
2153 | 2811 (args (cdr-safe (pop opts)))) |
2812 (cond ((eq opt :conc-name) | |
428 | 2813 (if args |
2814 (setq conc-name (if (car args) | |
2815 (symbol-name (car args)) "")))) | |
2153 | 2816 ((eq opt :constructor) |
428 | 2817 (if (cdr args) |
2153 | 2818 (push args constrs) |
428 | 2819 (if args (setq constructor (car args))))) |
2153 | 2820 ((eq opt :copier) |
428 | 2821 (if args (setq copier (car args)))) |
2153 | 2822 ((eq opt :predicate) |
428 | 2823 (if args (setq predicate (car args)))) |
2153 | 2824 ((eq opt :include) |
428 | 2825 (setq include (car args) |
2826 include-descs (mapcar #'(lambda (x) | |
2827 (if (consp x) x (list x))) | |
2828 (cdr args)))) | |
2153 | 2829 ((eq opt :print-function) |
428 | 2830 (setq print-func (car args))) |
2153 | 2831 ((eq opt :type) |
428 | 2832 (setq type (car args))) |
2153 | 2833 ((eq opt :named) |
428 | 2834 (setq named t)) |
2153 | 2835 ((eq opt :initial-offset) |
428 | 2836 (setq descs (nconc (make-list (car args) '(cl-skip-slot)) |
2837 descs))) | |
2838 (t | |
2839 (error "Slot option %s unrecognized" opt))))) | |
2840 (if print-func | |
2841 (setq print-func (list 'progn | |
2842 (list 'funcall (list 'function print-func) | |
2843 'cl-x 'cl-s 'cl-n) t)) | |
2844 (or type (and include (not (get include 'cl-struct-print))) | |
2845 (setq print-auto t | |
2846 print-func (and (or (not (or include type)) (null print-func)) | |
2847 (list 'progn | |
2848 (list 'princ (format "#S(%s" name) | |
2849 'cl-s)))))) | |
2850 (if include | |
2851 (let ((inc-type (get include 'cl-struct-type)) | |
2852 (old-descs (get include 'cl-struct-slots))) | |
2853 (or inc-type (error "%s is not a struct name" include)) | |
2854 (and type (not (eq (car inc-type) type)) | |
2855 (error ":type disagrees with :include for %s" name)) | |
2856 (while include-descs | |
2857 (setcar (memq (or (assq (caar include-descs) old-descs) | |
2858 (error "No slot %s in included struct %s" | |
2859 (caar include-descs) include)) | |
2860 old-descs) | |
2153 | 2861 (pop include-descs))) |
428 | 2862 (setq descs (append old-descs (delq (assq 'cl-tag-slot descs) descs)) |
2863 type (car inc-type) | |
2864 named (assq 'cl-tag-slot descs)) | |
2865 (if (cadr inc-type) (setq tag name named t)) | |
2866 (let ((incl include)) | |
2867 (while incl | |
2153 | 2868 (push (list 'pushnew (list 'quote tag) |
428 | 2869 (intern (format "cl-struct-%s-tags" incl))) |
2870 forms) | |
2871 (setq incl (get incl 'cl-struct-include))))) | |
2872 (if type | |
2873 (progn | |
2874 (or (memq type '(vector list)) | |
2875 (error "Illegal :type specifier: %s" type)) | |
2876 (if named (setq tag name))) | |
2877 (setq type 'vector named 'true))) | |
2878 (or named (setq descs (delq (assq 'cl-tag-slot descs) descs))) | |
2153 | 2879 (push (list 'defvar tag-symbol) forms) |
428 | 2880 (setq pred-form (and named |
2881 (let ((pos (- (length descs) | |
2882 (length (memq (assq 'cl-tag-slot descs) | |
2883 descs))))) | |
2884 (if (eq type 'vector) | |
2885 (list 'and '(vectorp cl-x) | |
2886 (list '>= '(length cl-x) (length descs)) | |
2887 (list 'memq (list 'aref 'cl-x pos) | |
2888 tag-symbol)) | |
2889 (if (= pos 0) | |
2890 (list 'memq '(car-safe cl-x) tag-symbol) | |
2891 (list 'and '(consp cl-x) | |
2892 (list 'memq (list 'nth pos 'cl-x) | |
2893 tag-symbol)))))) | |
2894 pred-check (and pred-form (> safety 0) | |
2895 (if (and (eq (caadr pred-form) 'vectorp) | |
2896 (= safety 1)) | |
2897 (cons 'and (cdddr pred-form)) pred-form))) | |
2898 (let ((pos 0) (descp descs)) | |
2899 (while descp | |
2153 | 2900 (let* ((desc (pop descp)) |
428 | 2901 (slot (car desc))) |
2902 (if (memq slot '(cl-tag-slot cl-skip-slot)) | |
2903 (progn | |
2153 | 2904 (push nil slots) |
2905 (push (and (eq slot 'cl-tag-slot) (list 'quote tag)) | |
428 | 2906 defaults)) |
2907 (if (assq slot descp) | |
2908 (error "Duplicate slots named %s in %s" slot name)) | |
2909 (let ((accessor (intern (format "%s%s" conc-name slot)))) | |
2153 | 2910 (push slot slots) |
2911 (push (nth 1 desc) defaults) | |
2912 (push (list* | |
428 | 2913 'defsubst* accessor '(cl-x) |
2914 (append | |
2915 (and pred-check | |
2916 (list (list 'or pred-check | |
2917 (list 'error | |
2918 (format "%s accessing a non-%s" | |
2919 accessor name) | |
2920 'cl-x)))) | |
2921 (list (if (eq type 'vector) (list 'aref 'cl-x pos) | |
2922 (if (= pos 0) '(car cl-x) | |
2923 (list 'nth pos 'cl-x)))))) forms) | |
2153 | 2924 (push (cons accessor t) side-eff) |
2925 (push (list 'define-setf-method accessor '(cl-x) | |
2926 (if (cadr (memq :read-only (cddr desc))) | |
428 | 2927 (list 'error (format "%s is a read-only slot" |
2928 accessor)) | |
2929 (list 'cl-struct-setf-expander 'cl-x | |
2930 (list 'quote name) (list 'quote accessor) | |
2931 (and pred-check (list 'quote pred-check)) | |
2932 pos))) | |
2933 forms) | |
2934 (if print-auto | |
2935 (nconc print-func | |
2936 (list (list 'princ (format " %s" slot) 'cl-s) | |
2937 (list 'prin1 (list accessor 'cl-x) 'cl-s))))))) | |
2938 (setq pos (1+ pos)))) | |
2939 (setq slots (nreverse slots) | |
2940 defaults (nreverse defaults)) | |
2941 (and predicate pred-form | |
2153 | 2942 (progn (push (list 'defsubst* predicate '(cl-x) |
428 | 2943 (if (eq (car pred-form) 'and) |
2944 (append pred-form '(t)) | |
2945 (list 'and pred-form t))) forms) | |
2153 | 2946 (push (cons predicate 'error-free) side-eff))) |
428 | 2947 (and copier |
2153 | 2948 (progn (push (list 'defun copier '(x) '(copy-sequence x)) forms) |
2949 (push (cons copier t) side-eff))) | |
428 | 2950 (if constructor |
2153 | 2951 (push (list constructor |
428 | 2952 (cons '&key (delq nil (copy-sequence slots)))) |
2953 constrs)) | |
2954 (while constrs | |
2955 (let* ((name (caar constrs)) | |
2153 | 2956 (args (cadr (pop constrs))) |
428 | 2957 (anames (cl-arglist-args args)) |
2958 (make (mapcar* #'(lambda (s d) (if (memq s anames) s d)) | |
2959 slots defaults))) | |
2153 | 2960 (push (list 'defsubst* name |
428 | 2961 (list* '&cl-defs (list 'quote (cons nil descs)) args) |
2962 (cons type make)) forms) | |
2963 (if (cl-safe-expr-p (cons 'progn (mapcar 'second descs))) | |
2153 | 2964 (push (cons name t) side-eff)))) |
428 | 2965 (if print-auto (nconc print-func (list '(princ ")" cl-s) t))) |
2966 (if print-func | |
2153 | 2967 (push (list 'push |
428 | 2968 (list 'function |
2969 (list 'lambda '(cl-x cl-s cl-n) | |
2970 (list 'and pred-form print-func))) | |
2971 'custom-print-functions) forms)) | |
2153 | 2972 (push (list 'setq tag-symbol (list 'list (list 'quote tag))) forms) |
2973 (push (list* 'eval-when '(compile load eval) | |
428 | 2974 (list 'put (list 'quote name) '(quote cl-struct-slots) |
2975 (list 'quote descs)) | |
2976 (list 'put (list 'quote name) '(quote cl-struct-type) | |
2977 (list 'quote (list type (eq named t)))) | |
2978 (list 'put (list 'quote name) '(quote cl-struct-include) | |
2979 (list 'quote include)) | |
2980 (list 'put (list 'quote name) '(quote cl-struct-print) | |
2981 print-auto) | |
2982 (mapcar #'(lambda (x) | |
2983 (list 'put (list 'quote (car x)) | |
2984 '(quote side-effect-free) | |
2985 (list 'quote (cdr x)))) | |
2986 side-eff)) | |
2987 forms) | |
2988 (cons 'progn (nreverse (cons (list 'quote name) forms))))) | |
2989 | |
2990 ;;;###autoload | |
2991 (defun cl-struct-setf-expander (x name accessor pred-form pos) | |
2992 (let* ((temp (gensym "--x--")) (store (gensym "--store--"))) | |
2993 (list (list temp) (list x) (list store) | |
2994 (append '(progn) | |
2995 (and pred-form | |
2996 (list (list 'or (subst temp 'cl-x pred-form) | |
2997 (list 'error | |
2998 (format | |
2999 "%s storing a non-%s" accessor name) | |
3000 temp)))) | |
3001 (list (if (eq (car (get name 'cl-struct-type)) 'vector) | |
3002 (list 'aset temp pos store) | |
3003 (list 'setcar | |
3004 (if (<= pos 5) | |
3005 (let ((xx temp)) | |
3006 (while (>= (setq pos (1- pos)) 0) | |
3007 (setq xx (list 'cdr xx))) | |
3008 xx) | |
3009 (list 'nthcdr pos temp)) | |
3010 store)))) | |
3011 (list accessor temp)))) | |
3012 | |
3013 | |
3014 ;;; Types and assertions. | |
3015 | |
3016 ;;;###autoload | |
2153 | 3017 (defmacro deftype (name arglist &rest body) |
4695
fee33ab25966
Add arglist info for autoloaded functions and macros.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4694
diff
changeset
|
3018 "Define NAME as a new data type. |
428 | 3019 The type name can then be used in `typecase', `check-type', etc." |
3020 (list 'eval-when '(compile load eval) | |
3021 (cl-transform-function-property | |
2153 | 3022 name 'cl-deftype-handler (cons (list* '&cl-defs ''('*) arglist) body)))) |
428 | 3023 |
3024 (defun cl-make-type-test (val type) | |
3025 (if (symbolp type) | |
3026 (cond ((get type 'cl-deftype-handler) | |
3027 (cl-make-type-test val (funcall (get type 'cl-deftype-handler)))) | |
3028 ((memq type '(nil t)) type) | |
2153 | 3029 ((eq type 'null) `(null ,val)) |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4820
diff
changeset
|
3030 ((eq type 'float) `(floatp ,val)) |
2153 | 3031 ((eq type 'real) `(numberp ,val)) |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4820
diff
changeset
|
3032 ((eq type 'fixnum) `(fixnump ,val)) |
2153 | 3033 ;; XEmacs change: we do not have char-valid-p |
3034 ((memq type '(character string-char)) `(characterp ,val)) | |
428 | 3035 (t |
3036 (let* ((name (symbol-name type)) | |
3037 (namep (intern (concat name "p")))) | |
3038 (if (fboundp namep) (list namep val) | |
3039 (list (intern (concat name "-p")) val))))) | |
3040 (cond ((get (car type) 'cl-deftype-handler) | |
3041 (cl-make-type-test val (apply (get (car type) 'cl-deftype-handler) | |
3042 (cdr type)))) | |
3043 ((memq (car-safe type) '(integer float real number)) | |
3044 (delq t (list 'and (cl-make-type-test val (car type)) | |
3045 (if (memq (cadr type) '(* nil)) t | |
3046 (if (consp (cadr type)) (list '> val (caadr type)) | |
3047 (list '>= val (cadr type)))) | |
3048 (if (memq (caddr type) '(* nil)) t | |
3049 (if (consp (caddr type)) (list '< val (caaddr type)) | |
3050 (list '<= val (caddr type))))))) | |
3051 ((memq (car-safe type) '(and or not)) | |
3052 (cons (car type) | |
3053 (mapcar #'(lambda (x) (cl-make-type-test val x)) | |
3054 (cdr type)))) | |
3055 ((memq (car-safe type) '(member member*)) | |
3056 (list 'and (list 'member* val (list 'quote (cdr type))) t)) | |
3057 ((eq (car-safe type) 'satisfies) (list (cadr type) val)) | |
3058 (t (error "Bad type spec: %s" type))))) | |
3059 | |
3060 ;;;###autoload | |
444 | 3061 (defun typep (object type) ; See compiler macro below. |
428 | 3062 "Check that OBJECT is of type TYPE. |
3063 TYPE is a Common Lisp-style type specifier." | |
444 | 3064 (eval (cl-make-type-test 'object type))) |
428 | 3065 |
3066 ;;;###autoload | |
446 | 3067 (defmacro check-type (place type &optional string) |
3068 "Verify that PLACE is of type TYPE; signal a continuable error if not. | |
428 | 3069 STRING is an optional description of the desired type." |
2153 | 3070 (and (or (not (cl-compiling-file)) |
3071 (< cl-optimize-speed 3) (= cl-optimize-safety 3)) | |
3072 (let* ((temp (if (cl-simple-expr-p place 3) place (gensym))) | |
3073 (test (cl-make-type-test temp type)) | |
3074 (signal-error `(signal 'wrong-type-argument | |
3075 ,(list 'list (or string (list 'quote type)) | |
3076 temp (list 'quote place)))) | |
3077 (body | |
3078 (condition-case nil | |
3079 `(while (not ,test) | |
3080 ,(macroexpand `(setf ,place ,signal-error))) | |
3081 (error | |
3082 `(if ,test (progn ,signal-error nil)))))) | |
3083 (if (eq temp place) `(progn ,body nil) | |
3084 `(let ((,temp ,place)) ,body nil))))) | |
428 | 3085 |
3086 ;;;###autoload | |
3087 (defmacro assert (form &optional show-args string &rest args) | |
3088 "Verify that FORM returns non-nil; signal an error if not. | |
3089 Second arg SHOW-ARGS means to include arguments of FORM in message. | |
3090 Other args STRING and ARGS... are arguments to be passed to `error'. | |
3091 They are not evaluated unless the assertion fails. If STRING is | |
3092 omitted, a default message listing FORM itself is used." | |
3093 (and (or (not (cl-compiling-file)) | |
3094 (< cl-optimize-speed 3) (= cl-optimize-safety 3)) | |
3095 (let ((sargs (and show-args (delq nil (mapcar | |
3096 #'(lambda (x) | |
3097 (and (not (cl-const-expr-p x)) | |
3098 x)) | |
3099 (cdr form)))))) | |
3100 (list 'progn | |
3101 (list 'or form | |
3102 (if string | |
3103 (list* 'error string (append sargs args)) | |
3104 (list 'signal '(quote cl-assertion-failed) | |
3105 (list* 'list (list 'quote form) sargs)))) | |
3106 nil)))) | |
3107 | |
3108 ;;;###autoload | |
3109 (defmacro ignore-errors (&rest body) | |
2153 | 3110 "Execute BODY; if an error occurs, return nil. |
3111 Otherwise, return result of last form in BODY." | |
428 | 3112 `(condition-case nil (progn ,@body) (error nil))) |
3113 | |
2153 | 3114 ;; XEmacs addition |
428 | 3115 ;;;###autoload |
3116 (defmacro ignore-file-errors (&rest body) | |
3117 "Execute FORMS; if an error of type `file-error' occurs, return nil. | |
3118 Otherwise, return result of last FORM." | |
3119 `(condition-case nil (progn ,@body) (file-error nil))) | |
3120 | |
3121 | |
3122 ;;; Compiler macros. | |
3123 | |
3124 ;;;###autoload | |
3125 (defmacro define-compiler-macro (func args &rest body) | |
4695
fee33ab25966
Add arglist info for autoloaded functions and macros.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4694
diff
changeset
|
3126 "Define a compiler-only macro. |
428 | 3127 This is like `defmacro', but macro expansion occurs only if the call to |
3128 FUNC is compiled (i.e., not interpreted). Compiler macros should be used | |
3129 for optimizing the way calls to FUNC are compiled; the form returned by | |
3130 BODY should do the same thing as a call to the normal function called | |
3131 FUNC, though possibly more efficiently. Note that, like regular macros, | |
3132 compiler macros are expanded repeatedly until no further expansions are | |
3133 possible. Unlike regular macros, BODY can decide to \"punt\" and leave the | |
3134 original function call alone by declaring an initial `&whole foo' parameter | |
3135 and then returning foo." | |
3136 (let ((p (if (listp args) args (list '&rest args))) (res nil)) | |
2153 | 3137 (while (consp p) (push (pop p) res)) |
3138 (setq args (nconc (nreverse res) (and p (list '&rest p))))) | |
428 | 3139 (list 'eval-when '(compile load eval) |
3140 (cl-transform-function-property | |
3141 func 'cl-compiler-macro | |
3142 (cons (if (memq '&whole args) (delq '&whole args) | |
3143 (cons '--cl-whole-arg-- args)) body)) | |
3144 (list 'or (list 'get (list 'quote func) '(quote byte-compile)) | |
3145 (list 'put (list 'quote func) '(quote byte-compile) | |
3146 '(quote cl-byte-compile-compiler-macro))))) | |
3147 | |
3148 ;;;###autoload | |
3149 (defun compiler-macroexpand (form) | |
3150 (while | |
3151 (let ((func (car-safe form)) (handler nil)) | |
3152 (while (and (symbolp func) | |
3153 (not (setq handler (get func 'cl-compiler-macro))) | |
3154 (fboundp func) | |
3155 (or (not (eq (car-safe (symbol-function func)) 'autoload)) | |
3156 (load (nth 1 (symbol-function func))))) | |
3157 (setq func (symbol-function func))) | |
3158 (and handler | |
3159 (not (eq form (setq form (apply handler form (cdr form)))))))) | |
3160 form) | |
3161 | |
3162 (defun cl-byte-compile-compiler-macro (form) | |
3163 (if (eq form (setq form (compiler-macroexpand form))) | |
3164 (byte-compile-normal-call form) | |
3165 (byte-compile-form form))) | |
3166 | |
3167 (defmacro defsubst* (name args &rest body) | |
3168 "(defsubst* NAME ARGLIST [DOCSTRING] BODY...): define NAME as a function. | |
3169 Like `defun', except the function is automatically declared `inline', | |
3170 ARGLIST allows full Common Lisp conventions, and BODY is implicitly | |
3171 surrounded by (block NAME ...)." | |
3172 (let* ((argns (cl-arglist-args args)) (p argns) | |
3173 (pbody (cons 'progn body)) | |
3174 (unsafe (not (cl-safe-expr-p pbody)))) | |
2153 | 3175 (while (and p (eq (cl-expr-contains args (car p)) 1)) (pop p)) |
428 | 3176 (list 'progn |
3177 (if p nil ; give up if defaults refer to earlier args | |
3178 (list 'define-compiler-macro name | |
2153 | 3179 (if (memq '&key args) |
3180 (list* '&whole 'cl-whole '&cl-quote args) | |
3181 (cons '&cl-quote args)) | |
428 | 3182 (list* 'cl-defsubst-expand (list 'quote argns) |
3183 (list 'quote (list* 'block name body)) | |
3184 (not (or unsafe (cl-expr-access-order pbody argns))) | |
3185 (and (memq '&key args) 'cl-whole) unsafe argns))) | |
3186 (list* 'defun* name args body)))) | |
3187 | |
3188 (defun cl-defsubst-expand (argns body simple whole unsafe &rest argvs) | |
3189 (if (and whole (not (cl-safe-expr-p (cons 'progn argvs)))) whole | |
3190 (if (cl-simple-exprs-p argvs) (setq simple t)) | |
3191 (let ((lets (delq nil | |
3192 (mapcar* #'(lambda (argn argv) | |
3193 (if (or simple (cl-const-expr-p argv)) | |
3194 (progn (setq body (subst argv argn body)) | |
3195 (and unsafe (list argn argv))) | |
3196 (list argn argv))) | |
3197 argns argvs)))) | |
3198 (if lets (list 'let lets body) body)))) | |
3199 | |
3200 | |
3201 ;;; Compile-time optimizations for some functions defined in this package. | |
3202 ;;; Note that cl.el arranges to force cl-macs to be loaded at compile-time, | |
3203 ;;; mainly to make sure these macros will be present. | |
3204 | |
3205 (put 'eql 'byte-compile nil) | |
3206 (define-compiler-macro eql (&whole form a b) | |
3207 (cond ((eq (cl-const-expr-p a) t) | |
3208 (let ((val (cl-const-expr-val a))) | |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4820
diff
changeset
|
3209 (if (and (numberp val) (not (fixnump val))) |
428 | 3210 (list 'equal a b) |
3211 (list 'eq a b)))) | |
3212 ((eq (cl-const-expr-p b) t) | |
3213 (let ((val (cl-const-expr-val b))) | |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4820
diff
changeset
|
3214 (if (and (numberp val) (not (fixnump val))) |
428 | 3215 (list 'equal a b) |
3216 (list 'eq a b)))) | |
3217 ((cl-simple-expr-p a 5) | |
3218 (list 'if (list 'numberp a) | |
3219 (list 'equal a b) | |
3220 (list 'eq a b))) | |
3221 ((and (cl-safe-expr-p a) | |
3222 (cl-simple-expr-p b 5)) | |
3223 (list 'if (list 'numberp b) | |
3224 (list 'equal a b) | |
3225 (list 'eq a b))) | |
3226 (t form))) | |
3227 | |
3228 (define-compiler-macro member* (&whole form a list &rest keys) | |
2153 | 3229 (let ((test (and (= (length keys) 2) (eq (car keys) :test) |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4820
diff
changeset
|
3230 (cl-const-expr-val (nth 1 keys)))) |
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4820
diff
changeset
|
3231 a-val) |
428 | 3232 (cond ((eq test 'eq) (list 'memq a list)) |
3233 ((eq test 'equal) (list 'member a list)) | |
3234 ((or (null keys) (eq test 'eql)) | |
3235 (if (eq (cl-const-expr-p a) t) | |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4820
diff
changeset
|
3236 (list (if (and (numberp (setq a-val (cl-const-expr-val a))) |
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4820
diff
changeset
|
3237 (not (fixnump a-val))) |
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4820
diff
changeset
|
3238 'member |
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4820
diff
changeset
|
3239 'memq) |
428 | 3240 a list) |
3241 (if (eq (cl-const-expr-p list) t) | |
3242 (let ((p (cl-const-expr-val list)) (mb nil) (mq nil)) | |
3243 (if (not (cdr p)) | |
3244 (and p (list 'eql a (list 'quote (car p)))) | |
3245 (while p | |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4820
diff
changeset
|
3246 (if (and (numberp (car p)) (not (fixnump (car p)))) |
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4820
diff
changeset
|
3247 (setq mb t) |
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4820
diff
changeset
|
3248 (or (fixnump (car p)) (symbolp (car p)) (setq mq t))) |
428 | 3249 (setq p (cdr p))) |
3250 (if (not mb) (list 'memq a list) | |
3251 (if (not mq) (list 'member a list) form)))) | |
3252 form))) | |
3253 (t form)))) | |
3254 | |
3255 (define-compiler-macro assoc* (&whole form a list &rest keys) | |
2153 | 3256 (let ((test (and (= (length keys) 2) (eq (car keys) :test) |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4820
diff
changeset
|
3257 (cl-const-expr-val (nth 1 keys)))) |
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4820
diff
changeset
|
3258 a-val) |
428 | 3259 (cond ((eq test 'eq) (list 'assq a list)) |
3260 ((eq test 'equal) (list 'assoc a list)) | |
3261 ((and (eq (cl-const-expr-p a) t) (or (null keys) (eq test 'eql))) | |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4820
diff
changeset
|
3262 (if (and (numberp (setq a-val (cl-const-expr-val a))) |
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4820
diff
changeset
|
3263 (not (fixnump a-val))) |
428 | 3264 (list 'assoc a list) (list 'assq a list))) |
3265 (t form)))) | |
3266 | |
3267 (define-compiler-macro adjoin (&whole form a list &rest keys) | |
3268 (if (and (cl-simple-expr-p a) (cl-simple-expr-p list) | |
2153 | 3269 (not (memq :key keys))) |
428 | 3270 (list 'if (list* 'member* a list keys) list (list 'cons a list)) |
3271 form)) | |
3272 | |
3273 (define-compiler-macro list* (arg &rest others) | |
3274 (let* ((args (reverse (cons arg others))) | |
3275 (form (car args))) | |
3276 (while (setq args (cdr args)) | |
3277 (setq form (list 'cons (car args) form))) | |
3278 form)) | |
3279 | |
2153 | 3280 ;; XEmacs change: our builtin get takes the default argument |
440 | 3281 (define-compiler-macro get* (sym prop &optional default) |
3282 (list 'get sym prop default)) | |
428 | 3283 |
442 | 3284 (define-compiler-macro getf (sym prop &optional default) |
3285 (list 'plist-get sym prop default)) | |
3286 | |
428 | 3287 (define-compiler-macro typep (&whole form val type) |
3288 (if (cl-const-expr-p type) | |
3289 (let ((res (cl-make-type-test val (cl-const-expr-val type)))) | |
3290 (if (or (memq (cl-expr-contains res val) '(nil 1)) | |
3291 (cl-simple-expr-p val)) res | |
3292 (let ((temp (gensym))) | |
3293 (list 'let (list (list temp val)) (subst temp val res))))) | |
3294 form)) | |
3295 | |
4607
517f6887fbc0
Remove duplicate functions, chiefly #'delete-duplicates reimplementations.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2297
diff
changeset
|
3296 ;; XEmacs; inline delete-duplicates if it's called with a literal |
517f6887fbc0
Remove duplicate functions, chiefly #'delete-duplicates reimplementations.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2297
diff
changeset
|
3297 ;; #'equal or #'eq and no other keywords, we want the speed in |
517f6887fbc0
Remove duplicate functions, chiefly #'delete-duplicates reimplementations.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2297
diff
changeset
|
3298 ;; font-lock.el. |
517f6887fbc0
Remove duplicate functions, chiefly #'delete-duplicates reimplementations.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2297
diff
changeset
|
3299 (define-compiler-macro delete-duplicates (&whole form cl-seq &rest cl-keys) |
4707
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3300 (let ((listp-check |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3301 (if (memq (car-safe cl-seq) |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3302 ;; No need to check for a list at runtime with these. We |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3303 ;; could expand the list, but these are all the functions |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3304 ;; in the relevant context at the moment. |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3305 '(nreverse append nconc mapcan mapcar)) |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3306 t |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3307 '(listp begin)))) |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3308 (cond ((and (= 4 (length form)) |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3309 (eq :test (third form)) |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3310 (or (equal '(quote eq) (fourth form)) |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3311 (equal '(function eq) (fourth form)))) |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3312 `(let* ((begin ,cl-seq) |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3313 (cl-seq begin)) |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3314 (if ,listp-check |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3315 (progn |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3316 (while cl-seq |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3317 (setq cl-seq (setcdr cl-seq (delq (car cl-seq) |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3318 (cdr cl-seq))))) |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3319 begin) |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3320 ;; Call cl-delete-duplicates explicitly, to avoid the form |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3321 ;; getting compiler-macroexpanded again: |
4714
84f870bbd17b
Fix another bug in the delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4707
diff
changeset
|
3322 (cl-delete-duplicates begin ',cl-keys nil)))) |
4707
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3323 ((and (= 4 (length form)) |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3324 (eq :test (third form)) |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3325 (or (equal '(quote equal) (fourth form)) |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3326 (equal '(function equal) (fourth form)))) |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3327 `(let* ((begin ,cl-seq) |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3328 (cl-seq begin)) |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3329 (if ,listp-check |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3330 (progn |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3331 (while cl-seq |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3332 (setq cl-seq (setcdr cl-seq (delete (car cl-seq) |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3333 (cdr cl-seq))))) |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3334 begin) |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3335 ;; Call cl-delete-duplicates explicitly, to avoid the form |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3336 ;; getting compiler-macroexpanded again: |
4714
84f870bbd17b
Fix another bug in the delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4707
diff
changeset
|
3337 (cl-delete-duplicates begin ',cl-keys nil)))) |
4707
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3338 (t |
5bb0735f56e0
Handle non-list sequences better, delete-duplicates compiler macro.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4702
diff
changeset
|
3339 form)))) |
428 | 3340 |
4723
ebca981a0012
If STRING is constant, call regexp-quote at compile time.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4719
diff
changeset
|
3341 ;; XEmacs; it's perfectly reasonable, and often much clearer to those |
ebca981a0012
If STRING is constant, call regexp-quote at compile time.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4719
diff
changeset
|
3342 ;; reading the code, to call regexp-quote on a constant string, which is |
ebca981a0012
If STRING is constant, call regexp-quote at compile time.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4719
diff
changeset
|
3343 ;; something we can optimise here easily. |
ebca981a0012
If STRING is constant, call regexp-quote at compile time.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4719
diff
changeset
|
3344 (define-compiler-macro regexp-quote (&whole form string) |
ebca981a0012
If STRING is constant, call regexp-quote at compile time.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4719
diff
changeset
|
3345 (if (stringp string) |
ebca981a0012
If STRING is constant, call regexp-quote at compile time.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4719
diff
changeset
|
3346 (regexp-quote string) |
ebca981a0012
If STRING is constant, call regexp-quote at compile time.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4719
diff
changeset
|
3347 form)) |
ebca981a0012
If STRING is constant, call regexp-quote at compile time.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4719
diff
changeset
|
3348 |
4906
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3349 ;; NOTE: `equalp' is now a primitive, although as of yet it still doesn't |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3350 ;; have a byte-compiler opcode for it. The compiler-macro for `equalp' used |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3351 ;; to try and remove as much as possible of the logic of the Lisp `equalp' as |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3352 ;; possible whenever one of the arguments is a constant, boiling things down |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3353 ;; to a few if-statements and some calls to various no-longer-defined |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3354 ;; helper functions. Besides the fact that the helper functions aren't |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3355 ;; defined, there's little point in doing any of that expansion, since it will |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3356 ;; end up executing in Lisp what would otherwise be done in C by a direct |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3357 ;; call to `equalp'. The only exception is when the reduction is quite |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3358 ;; simple and is to functions that do have op-codes; that may gain something. |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3359 ;; However, if `equalp' becomes an opcode itself, consider removing everything |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3360 ;; here except maybe when the call can directly be reduced to `equal' or `eq'. |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3361 ;; |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3362 ;; --ben |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3363 |
4792
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3364 (define-compiler-macro equalp (&whole form x y) |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3365 "Expand calls to `equalp' where X or Y is a constant expression. |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3366 |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3367 Much of the processing that `equalp' does is dependent on the types of both |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3368 of its arguments, and with type information for one of them, we can |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3369 eliminate much of the body of the function at compile time. |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3370 |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3371 Where both X and Y are constant expressions, `equalp' is evaluated at |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3372 compile time by byte-optimize.el--this compiler macro passes FORM through to |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3373 the byte optimizer in those cases." |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3374 ;; Cases where both arguments are constant are handled in |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3375 ;; byte-optimize.el, we only need to handle those cases where one is |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3376 ;; constant here. |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3377 (let* ((equalp-sym (eval-when-compile (gensym))) |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3378 (let-form '(progn)) |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3379 (original-y y) |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3380 equalp-temp checked) |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3381 (macrolet |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3382 ((unordered-check (check) |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3383 `(prog1 |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3384 (setq checked |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3385 (or ,check |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3386 (prog1 ,(sublis '((x . y) (y . x)) check :test #'eq) |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3387 (setq equalp-temp x x y y equalp-temp)))) |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3388 (when checked |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3389 (unless (symbolp y) |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3390 (setq let-form `(let ((,equalp-sym ,y))) y equalp-sym)))))) |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3391 ;; In the bodies of the below clauses, x is always a constant expression |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3392 ;; of the type we're interested in, and y is always a symbol that refers |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3393 ;; to the result non-constant side of the comparison. |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3394 (cond ((unordered-check (and (arrayp x) (not (cl-const-expr-p y)))) |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3395 ;; Strings and other arrays. A vector containing the same |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3396 ;; character elements as a given string is equalp to that string; |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3397 ;; a bit-vector can only be equalp to a string if both are |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3398 ;; zero-length. |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3399 (cond |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3400 ((member x '("" #* [])) |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3401 ;; No need to protect against multiple evaluation here: |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3402 `(and (member ,original-y '("" #* [])) t)) |
4906
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3403 (t form))) |
4792
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3404 ((unordered-check (and (numberp x) (not (cl-const-expr-p y)))) |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3405 `(,@let-form |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3406 (and (numberp ,y) |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3407 (= ,x ,y)))) |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3408 ((unordered-check (and (hash-table-p x) (not (cl-const-expr-p y)))) |
4906
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3409 form) |
4792
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3410 ((unordered-check |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3411 ;; Symbols; eq. |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3412 (and (not (cl-const-expr-p y)) |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3413 (or (memq x '(nil t)) |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3414 (and (eq (car-safe x) 'quote) (symbolp (second x)))))) |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3415 (cons 'eq (cdr form))) |
4906
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3416 |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3417 ;; This clause is wrong -- e.g. when comparing a constant char-table |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3418 ;; against a non-constant expression that evaluates to a char-table, |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3419 ;; or some for range tables or certain other types, `equalp' is |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3420 ;; not the same as `equal'. We could insert the known list of |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3421 ;; types with special `equalp' property, but it's fragile and may |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3422 ;; not be much of an optimization, esp. since these types don't |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3423 ;; occur that often are often big. |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3424 ;;((unordered-check |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3425 ;; ;; Compare conses at runtime, there's no real upside to |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3426 ;; ;; unrolling the function -> they fall through to the next |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3427 ;; ;; clause in this function. |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3428 ;; (and (cl-const-expr-p x) (not (consp x)) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3429 ;; (not (cl-const-expr-p y)))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3430 ;; ;; All other types; use equal. |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3431 ;; (cons 'equal (cdr form))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3432 |
4792
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3433 ;; Neither side is a constant expression, do all our evaluation at |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3434 ;; runtime (or both are, and equalp will be called from |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3435 ;; byte-optimize.el). |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3436 (t form))))) |
95b04754ea8c
Make #'equalp more compatible with CL; add a compiler macro, test & doc it.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4783
diff
changeset
|
3437 |
4906
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3438 ;;(define-compiler-macro equalp (&whole form x y) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3439 ;; "Expand calls to `equalp' where X or Y is a constant expression. |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3440 ;; |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3441 ;;Much of the processing that `equalp' does is dependent on the types of both |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3442 ;;of its arguments, and with type information for one of them, we can |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3443 ;;eliminate much of the body of the function at compile time. |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3444 ;; |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3445 ;;Where both X and Y are constant expressions, `equalp' is evaluated at |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3446 ;;compile time by byte-optimize.el--this compiler macro passes FORM through to |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3447 ;;the byte optimizer in those cases." |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3448 ;; ;; Cases where both arguments are constant are handled in |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3449 ;; ;; byte-optimize.el, we only need to handle those cases where one is |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3450 ;; ;; constant here. |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3451 ;; (let* ((equalp-sym (eval-when-compile (gensym))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3452 ;; (let-form '(progn)) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3453 ;; (check-bit-vector t) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3454 ;; (check-string t) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3455 ;; (original-y y) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3456 ;; equalp-temp checked) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3457 ;; (macrolet |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3458 ;; ((unordered-check (check) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3459 ;; `(prog1 |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3460 ;; (setq checked |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3461 ;; (or ,check |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3462 ;; (prog1 ,(sublis '((x . y) (y . x)) check :test #'eq) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3463 ;; (setq equalp-temp x x y y equalp-temp)))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3464 ;; (when checked |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3465 ;; (unless (symbolp y) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3466 ;; (setq let-form `(let ((,equalp-sym ,y))) y equalp-sym)))))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3467 ;; ;; In the bodies of the below clauses, x is always a constant expression |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3468 ;; ;; of the type we're interested in, and y is always a symbol that refers |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3469 ;; ;; to the result non-constant side of the comparison. |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3470 ;; (cond ((unordered-check (and (arrayp x) (not (cl-const-expr-p y)))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3471 ;; ;; Strings and other arrays. A vector containing the same |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3472 ;; ;; character elements as a given string is equalp to that string; |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3473 ;; ;; a bit-vector can only be equalp to a string if both are |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3474 ;; ;; zero-length. |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3475 ;; (cond |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3476 ;; ((member x '("" #* [])) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3477 ;; ;; No need to protect against multiple evaluation here: |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3478 ;; `(and (member ,original-y '("" #* [])) t)) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3479 ;; ((stringp x) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3480 ;; `(,@let-form |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3481 ;; (if (stringp ,y) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3482 ;; (eq t (compare-strings ,x nil nil |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3483 ;; ,y nil nil t)) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3484 ;; (if (vectorp ,y) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3485 ;; (cl-string-vector-equalp ,x ,y))))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3486 ;; ((bit-vector-p x) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3487 ;; `(,@let-form |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3488 ;; (if (bit-vector-p ,y) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3489 ;; ;; No need to call equalp on each element here: |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3490 ;; (equal ,x ,y) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3491 ;; (if (vectorp ,y) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3492 ;; (cl-bit-vector-vector-equalp ,x ,y))))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3493 ;; (t |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3494 ;; (loop |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3495 ;; for elt across x |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3496 ;; ;; We may not need to check the other argument if it's a |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3497 ;; ;; string or bit vector, depending on the contents of x: |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3498 ;; always (progn |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3499 ;; (unless (characterp elt) (setq check-string nil)) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3500 ;; (unless (and (numberp elt) (or (= elt 0) (= elt 1))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3501 ;; (setq check-bit-vector nil)) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3502 ;; (or check-string check-bit-vector))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3503 ;; `(,@let-form |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3504 ;; (cond |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3505 ;; ,@(if check-string |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3506 ;; `(((stringp ,y) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3507 ;; (cl-string-vector-equalp ,y ,x)))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3508 ;; ,@(if check-bit-vector |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3509 ;; `(((bit-vector-p ,y) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3510 ;; (cl-bit-vector-vector-equalp ,y ,x)))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3511 ;; ((vectorp ,y) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3512 ;; (cl-vector-array-equalp ,x ,y))))))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3513 ;; ((unordered-check (and (characterp x) (not (cl-const-expr-p y)))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3514 ;; `(,@let-form |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3515 ;; (or (eq ,x ,y) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3516 ;; ;; eq has a bytecode, char-equal doesn't. |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3517 ;; (and (characterp ,y) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3518 ;; (eq (downcase ,x) (downcase ,y)))))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3519 ;; ((unordered-check (and (numberp x) (not (cl-const-expr-p y)))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3520 ;; `(,@let-form |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3521 ;; (and (numberp ,y) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3522 ;; (= ,x ,y)))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3523 ;; ((unordered-check (and (hash-table-p x) (not (cl-const-expr-p y)))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3524 ;; ;; Hash tables; follow the CL spec. |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3525 ;; `(,@let-form |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3526 ;; (and (hash-table-p ,y) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3527 ;; (eq ',(hash-table-test x) (hash-table-test ,y)) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3528 ;; (= ,(hash-table-count x) (hash-table-count ,y)) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3529 ;; (cl-hash-table-contents-equalp ,x ,y)))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3530 ;; ((unordered-check |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3531 ;; ;; Symbols; eq. |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3532 ;; (and (not (cl-const-expr-p y)) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3533 ;; (or (memq x '(nil t)) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3534 ;; (and (eq (car-safe x) 'quote) (symbolp (second x)))))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3535 ;; (cons 'eq (cdr form))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3536 ;; ((unordered-check |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3537 ;; ;; Compare conses at runtime, there's no real upside to |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3538 ;; ;; unrolling the function -> they fall through to the next |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3539 ;; ;; clause in this function. |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3540 ;; (and (cl-const-expr-p x) (not (consp x)) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3541 ;; (not (cl-const-expr-p y)))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3542 ;; ;; All other types; use equal. |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3543 ;; (cons 'equal (cdr form))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3544 ;; ;; Neither side is a constant expression, do all our evaluation at |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3545 ;; ;; runtime (or both are, and equalp will be called from |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3546 ;; ;; byte-optimize.el). |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3547 ;; (t form))))) |
6ef8256a020a
implement equalp in C, fix case-folding, add equal() method for keymaps
Ben Wing <ben@xemacs.org>
parents:
4885
diff
changeset
|
3548 |
4997
8800b5350a13
Move #'some, #'every to C, implementing them with mapcarX.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4996
diff
changeset
|
3549 (define-compiler-macro notany (&whole form &rest cl-rest) |
8800b5350a13
Move #'some, #'every to C, implementing them with mapcarX.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4996
diff
changeset
|
3550 (cons 'not (cons 'some (cdr cl-rest)))) |
8800b5350a13
Move #'some, #'every to C, implementing them with mapcarX.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4996
diff
changeset
|
3551 |
8800b5350a13
Move #'some, #'every to C, implementing them with mapcarX.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4996
diff
changeset
|
3552 (define-compiler-macro notevery (&whole form &rest cl-rest) |
8800b5350a13
Move #'some, #'every to C, implementing them with mapcarX.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4996
diff
changeset
|
3553 (cons 'not (cons 'every (cdr cl-rest)))) |
8800b5350a13
Move #'some, #'every to C, implementing them with mapcarX.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4996
diff
changeset
|
3554 |
5056
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3555 (define-compiler-macro constantly (&whole form value &rest more-values) |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3556 (cond |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3557 ((< (length form) 2) |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3558 ;; Error at runtime: |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3559 form) |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3560 ((cl-const-exprs-p (cdr form)) |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3561 `#'(lambda (&rest ignore) (values ,@(cdr form)))) |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3562 (t |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3563 (let* ((num-values (length (cdr form))) |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3564 (placeholders-counts (make-vector num-values -1)) |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3565 (placeholders (loop |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3566 for i from 0 below num-values |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3567 collect (make-symbol (format "%d" i)))) |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3568 (compiled |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3569 (byte-compile-sexp |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3570 `#'(lambda (&rest ignore) |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3571 ;; Compiles to a references into the compiled function |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3572 ;; constants vector: |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3573 (values ,@(mapcar #'quote-maybe placeholders))))) |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3574 position) |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3575 `(make-byte-code '(&rest ignore) |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3576 ,(compiled-function-instructions compiled) |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3577 (vector ,@(loop |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3578 for constant across (compiled-function-constants compiled) |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3579 collect (if (setq position |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3580 (position constant placeholders)) |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3581 (prog2 |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3582 (incf (aref placeholders-counts position)) |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3583 (nth position (cdr form))) |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3584 (quote-maybe constant)) |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3585 finally |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3586 (assert (every #'zerop placeholders-counts) |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3587 t "Placeholders should each have been used once"))) |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3588 ,(compiled-function-stack-depth compiled)))))) |
6aba0daedb7c
Add #'constantly, as specified by ANSI Common Lisp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4997
diff
changeset
|
3589 |
428 | 3590 (mapc |
3591 #'(lambda (y) | |
3592 (put (car y) 'side-effect-free t) | |
3593 (put (car y) 'byte-compile 'cl-byte-compile-compiler-macro) | |
3594 (put (car y) 'cl-compiler-macro | |
3595 (list 'lambda '(w x) | |
3596 (if (symbolp (cadr y)) | |
3597 (list 'list (list 'quote (cadr y)) | |
3598 (list 'list (list 'quote (caddr y)) 'x)) | |
3599 (cons 'list (cdr y)))))) | |
3600 '((first 'car x) (second 'cadr x) (third 'caddr x) (fourth 'cadddr x) | |
3601 (fifth 'nth 4 x) (sixth 'nth 5 x) (seventh 'nth 6 x) | |
3602 (eighth 'nth 7 x) (ninth 'nth 8 x) (tenth 'nth 9 x) | |
3603 (rest 'cdr x) (endp 'null x) (plusp '> x 0) (minusp '< x 0) | |
446 | 3604 (oddp 'eq (list 'logand x 1) 1) |
3605 (evenp 'eq (list 'logand x 1) 0) | |
428 | 3606 (caar car car) (cadr car cdr) (cdar cdr car) (cddr cdr cdr) |
3607 (caaar car caar) (caadr car cadr) (cadar car cdar) | |
3608 (caddr car cddr) (cdaar cdr caar) (cdadr cdr cadr) | |
3609 (cddar cdr cdar) (cdddr cdr cddr) (caaaar car caaar) | |
3610 (caaadr car caadr) (caadar car cadar) (caaddr car caddr) | |
3611 (cadaar car cdaar) (cadadr car cdadr) (caddar car cddar) | |
3612 (cadddr car cdddr) (cdaaar cdr caaar) (cdaadr cdr caadr) | |
3613 (cdadar cdr cadar) (cdaddr cdr caddr) (cddaar cdr cdaar) | |
3614 (cddadr cdr cdadr) (cdddar cdr cddar) (cddddr cdr cdddr))) | |
3615 | |
3616 ;;; Things that are inline. | |
4997
8800b5350a13
Move #'some, #'every to C, implementing them with mapcarX.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4996
diff
changeset
|
3617 (proclaim '(inline acons map concatenate |
2153 | 3618 ;; XEmacs omission: gethash is builtin |
3619 cl-set-elt revappend nreconc)) | |
428 | 3620 |
3621 ;;; Things that are side-effect-free. Moved to byte-optimize.el | |
2153 | 3622 ;(mapcar (function (lambda (x) (put x 'side-effect-free t))) |
3623 ; '(oddp evenp signum last butlast ldiff pairlis gcd lcm | |
3624 ; isqrt floor* ceiling* truncate* round* mod* rem* subseq | |
3625 ; list-length get* getf)) | |
428 | 3626 |
3627 ;;; Things that are side-effect-and-error-free. Moved to byte-optimize.el | |
2153 | 3628 ;(mapcar (function (lambda (x) (put x 'side-effect-free 'error-free))) |
4885
6772ce4d982b
Fix hash tables, #'member*, #'assoc*, #'eql compiler macros if bignums
Aidan Kehoe <kehoea@parhasard.net>
parents:
4820
diff
changeset
|
3629 ; '(eql list* subst acons equalp random-state-p |
2153 | 3630 ; copy-tree sublis)) |
428 | 3631 |
3632 | |
3633 (run-hooks 'cl-macs-load-hook) | |
3634 | |
2153 | 3635 ;;; arch-tag: afd947a6-b553-4df1-bba5-000be6388f46 |
428 | 3636 ;;; cl-macs.el ends here |