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