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