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
|
|
743 to nil.
|
|
744
|
|
745 If you don't understand how a particular loop clause works, create an
|
|
746 example and use `macroexpand-sexp' to expand the macro.
|
|
747
|
428
|
748 Valid clauses are:
|
800
|
749
|
|
750 \(NOTE: Keywords in lowercase\; slashes separate different possibilities
|
|
751 for keywords, some of which are synonymous\; brackets indicate optional
|
|
752 parts of the clause. In all of the clauses with `being', the word `being',
|
|
753 the words `each' or `the', and the difference between singular and plural
|
|
754 keywords are all just syntactic sugar. Stylistically, you should write
|
|
755 either `being each foo' or `being the foos'.)
|
|
756
|
|
757 for VAR from/upfrom/downfrom NUM1 to/upto/downto/above/below NUM2 [by NUMSTEP]
|
|
758 Step VAR across numbers. `upfrom', `upto', and `below' explicitly
|
|
759 indicate upward stepping\; `downfrom', `downto', and `above' explicitly
|
|
760 indicate downward stepping. (If none of these is given, the default is
|
|
761 upward.) `to', `upto', and `downto' cause stepping to include NUM2 as
|
|
762 the last iteration, while `above' and `below' stop just before reaching
|
|
763 NUM2. `by' can be given to indicate a stepping increment other than 1.
|
|
764
|
|
765 for VAR in LIST [by FUNC]
|
|
766 Step VAR over elements of a LIST. FUNC specifies how to get successive
|
|
767 sublists and defaults to `cdr'.
|
|
768
|
|
769 for VAR on LIST [by FUNC]
|
|
770 Step VAR over tails of a LIST. FUNC specifies how to get successive
|
|
771 sublists and defaults to `cdr'.
|
|
772
|
|
773 for VAR in-ref LIST [by FUNC]
|
|
774 Step VAR over elements of a LIST, like `for ... in', except the VAR is
|
|
775 bound using `symbol-macrolet' instead of `let'. In essence, VAR is set
|
|
776 to a \"reference\" to the list element instead of the element itself\;
|
|
777 this us, you can destructively modify the list using `setf' on VAR, and
|
|
778 any changes to the list will \"magically\" reflect themselves in
|
|
779 subsequent uses of VAR.
|
|
780
|
|
781 for VAR = INIT [then EXPR]
|
|
782 Set VAR on each iteration of the loop. If only INIT is given, use it
|
|
783 on each iteration. Otherwise, use INIT on the first iteration and EXPR
|
|
784 on subsequent ones.
|
|
785
|
|
786 for VAR across/across-ref ARRAY
|
|
787 Step VAR across a sequence other than a list (string, vector, bit
|
|
788 vector). If `across-ref' is given, VAR is bound using
|
|
789 `symbol-macrolet' instead of `let' -- see above.
|
|
790
|
|
791 for VAR being each/the element/elements in/of/in-ref/of-ref SEQUENCE [using (index INDEX-VAR)]
|
|
792 Step VAR across any sequence. A variable can be specified with a
|
|
793 `using' phrase to receive the index, starting at 0. If `in-ref' or
|
|
794 `of-ref' is given, VAR is bound using `symbol-macrolet' instead of
|
|
795 `let' -- see above.
|
|
796
|
|
797 for VAR being each/the hash-key/hash-keys/hash-value/hash-values in/of HASH-TABLE [using (hash-value/hash-key OTHER-VAR)]
|
|
798
|
|
799 for VAR being each/the hash-key/hash-keys/hash-value/hash-values in/of HASH-TABLE [using (hash-value/hash-key OTHER-VAR)]
|
|
800 Map VAR over a hash table. The various keywords are synonymous except
|
|
801 those that distinguish between keys and values. The `using' phrase is
|
|
802 optional and allows both key and value to be bound.
|
|
803
|
|
804 for VAR being each/the symbol/present-symbol/external-symbol/symbols/present-symbols/external-symbols in/of OBARRAY
|
|
805 Map VAR over the symbols in an obarray. All symbol keywords are
|
|
806 currently synonymous.
|
|
807
|
|
808 for VAR being each/the extent/extents [in/of BUFFER-OR-STRING] [from POS] [to POS]
|
|
809 Map VAR over the extents in a buffer or string, defaulting to the
|
|
810 current buffer, the beginning and the end, respectively.
|
|
811
|
|
812 for VAR being each/the interval/intervals [in/of BUFFER-OR-STRING] [property PROPERTY] [from POS] [to POS]
|
|
813 Map VAR over the intervals without property change in a buffer or
|
|
814 string, defaulting to the current buffer, the beginning and the end,
|
|
815 respectively. If PROPERTY is given, iteration occurs using
|
|
816 `next-single-property-change'\; otherwise, using
|
|
817 `next-property-change'.
|
|
818
|
|
819 for VAR being each/the window/windows [in/of FRAME]
|
|
820 Step VAR over the windows in FRAME, defaulting to the selected frame.
|
|
821
|
|
822 for VAR being each/the frame/frames
|
|
823 Step VAR over all frames.
|
|
824
|
|
825 for VAR being each/the buffer/buffers [by FUNC]
|
|
826 Step VAR over all buffers. This is actually equivalent to
|
|
827 `for VAR in (buffer-list) [by FUNC]'.
|
|
828
|
|
829 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)]
|
|
830 Map VAR over the entries in a keymap. Keyword `key-seq' causes
|
|
831 recursive mapping over prefix keymaps occurring in the keymap, with VAR
|
|
832 getting the built-up sequence (a vector). Otherwise, mapping does not
|
|
833 occur recursively. `key-code' and `key-seq' refer to what is bound
|
|
834 (second argument of `define-key'), and `key-binding' what it's bound to
|
|
835 (third argument of `define-key').
|
|
836
|
|
837 as VAR ...
|
|
838 `as' is a synonym for `for'.
|
|
839
|
|
840 and VAR ...
|
|
841 `and' clauses have the same syntax as `for' clauses except that the
|
|
842 variables in the clause are bound in parallel with a preceding
|
|
843 `and'/`for' clause instead of in series.
|
|
844
|
|
845 with VAR = INIT
|
|
846 Set VAR to INIT once, before doing any iterations.
|
|
847
|
|
848 repeat NUM
|
|
849 Exit the loop if more than NUM iterations have occurred.
|
|
850
|
|
851 while COND
|
|
852 Exit the loop if COND isn't true.
|
|
853
|
|
854 until COND
|
|
855 Exit the loop if COND is true.
|
|
856
|
|
857 collect EXPR [into VAR]
|
|
858 Push EXPR onto the end of a list of values -- stored either in VAR or a
|
|
859 temporary variable that will be returned as the return value of the
|
|
860 loop if it terminates through an exit condition or a call to
|
|
861 `loop-finish'.
|
|
862
|
|
863 append EXPR [into VAR]
|
|
864 Append EXPR (a list) onto the end of a list of values, like `collect'.
|
|
865
|
|
866 nconc EXPR [into VAR]
|
|
867 Nconc EXPR (a list) onto the end of a list of values, like `collect'.
|
|
868
|
|
869 concat EXPR [into VAR]
|
|
870 Concatenate EXPR (a string) onto the end of a string of values, like
|
|
871 `collect'.
|
|
872
|
|
873 vconcat EXPR [into VAR]
|
|
874 Concatenate EXPR (a vector) onto the end of a vector of values, like
|
|
875 `collect'.
|
|
876
|
|
877 bvconcat EXPR [into VAR]
|
|
878 Concatenate EXPR (a bit vector) onto the end of a bit vector of values,
|
|
879 like `collect'.
|
|
880
|
|
881 sum EXPR [into VAR]
|
|
882 Add EXPR to a value, like `collect'.
|
|
883
|
|
884 count EXPR [into VAR]
|
|
885 If EXPR is true, increment a value by 1, like `collect'.
|
|
886
|
|
887 maximize EXPR [into VAR]
|
|
888 IF EXPR is greater than a value, replace the value with EXPR, like
|
|
889 `collect'.
|
|
890
|
|
891 minimize EXPR [into VAR]
|
|
892 IF EXPR is less than a value, replace the value with EXPR, like
|
|
893 `collect'.
|
|
894
|
|
895 always COND
|
|
896 If COND is true, continue the loop and set the loop return value (the
|
|
897 same value that's manipulated by `collect' and friends and is returned
|
|
898 by a normal loop exit or an exit using `loop-finish') to t\; otherwise,
|
|
899 exit the loop and return nil. The effect is to determine and return
|
|
900 whether a condition is true \"always\" (all iterations of the loop).
|
|
901
|
|
902 never COND
|
|
903 If COND is false, continue the loop and set the loop return value (like
|
|
904 `always') to t\; otherwise, exit the loop and return nil. The effect
|
|
905 is to determine and return whether a condition is \"never\" true (all
|
|
906 iterations of the loop).
|
|
907
|
|
908 thereis COND
|
|
909 If COND is true, exit the loop and return COND.
|
|
910
|
|
911 if/when COND CLAUSE [and CLAUSE]... else CLAUSE [and CLAUSE...]
|
|
912 If COND is true, execute the directly following clause(s)\; otherwise,
|
|
913 execute the clauses following `else'.
|
|
914
|
|
915 unless COND CLAUSE [and CLAUSE]... else CLAUSE [and CLAUSE...]
|
|
916 If COND is false, execute the directly following clause(s)\; otherwise, execute the clauses following `else'.
|
|
917
|
|
918 do EXPRS...
|
|
919 Execute the expressions (any Lisp forms).
|
|
920
|
|
921 initially EXPRS...
|
|
922 Execute EXPR once, before doing any iterations, and after values have
|
|
923 been set using `with'.
|
|
924
|
|
925 finally EXPRS...
|
|
926 Execute EXPR once, directly before the loop terminates. This will not
|
|
927 be executed if the loop terminates prematurely as a result of `always',
|
|
928 `never', `thereis', or `return'.
|
|
929
|
|
930 return EXPR
|
|
931 Exit from the loop and return EXPR.
|
|
932
|
|
933 finally return EXPR
|
|
934 Specify the value to be returned when the loop exits. (Unlike `return',
|
|
935 this doesn't cause the loop to immediately exit\; it will exit whenever
|
|
936 it normally would have.) This takes precedence over a return value
|
|
937 specified with `collect' and friends or `always' and friends.
|
|
938
|
|
939 named NAME
|
|
940 Specify the name for block surrounding the loop, in place of nil.
|
|
941 (See `block'.)
|
|
942 "
|
428
|
943 (if (not (memq t (mapcar 'symbolp (delq nil (delq t (copy-list args))))))
|
|
944 (list 'block nil (list* 'while t args))
|
|
945 (let ((loop-name nil) (loop-bindings nil)
|
|
946 (loop-body nil) (loop-steps nil)
|
|
947 (loop-result nil) (loop-result-explicit nil)
|
|
948 (loop-result-var nil) (loop-finish-flag nil)
|
|
949 (loop-accum-var nil) (loop-accum-vars nil)
|
|
950 (loop-initially nil) (loop-finally nil)
|
|
951 (loop-map-form nil) (loop-first-flag nil)
|
|
952 (loop-destr-temps nil) (loop-symbol-macs nil))
|
|
953 (setq args (append args '(cl-end-loop)))
|
|
954 (while (not (eq (car args) 'cl-end-loop)) (cl-parse-loop-clause))
|
|
955 (if loop-finish-flag
|
|
956 (cl-push (list (list loop-finish-flag t)) loop-bindings))
|
|
957 (if loop-first-flag
|
|
958 (progn (cl-push (list (list loop-first-flag t)) loop-bindings)
|
|
959 (cl-push (list 'setq loop-first-flag nil) loop-steps)))
|
|
960 (let* ((epilogue (nconc (nreverse loop-finally)
|
|
961 (list (or loop-result-explicit loop-result))))
|
|
962 (ands (cl-loop-build-ands (nreverse loop-body)))
|
|
963 (while-body (nconc (cadr ands) (nreverse loop-steps)))
|
|
964 (body (append
|
|
965 (nreverse loop-initially)
|
|
966 (list (if loop-map-form
|
|
967 (list 'block '--cl-finish--
|
|
968 (subst
|
|
969 (if (eq (car ands) t) while-body
|
|
970 (cons (list 'or (car ands)
|
|
971 '(return-from --cl-finish--
|
|
972 nil))
|
|
973 while-body))
|
|
974 '--cl-map loop-map-form))
|
|
975 (list* 'while (car ands) while-body)))
|
|
976 (if loop-finish-flag
|
|
977 (if (equal epilogue '(nil)) (list loop-result-var)
|
|
978 (list (list 'if loop-finish-flag
|
|
979 (cons 'progn epilogue) loop-result-var)))
|
|
980 epilogue))))
|
|
981 (if loop-result-var (cl-push (list loop-result-var) loop-bindings))
|
|
982 (while loop-bindings
|
|
983 (if (cdar loop-bindings)
|
|
984 (setq body (list (cl-loop-let (cl-pop loop-bindings) body t)))
|
|
985 (let ((lets nil))
|
|
986 (while (and loop-bindings
|
|
987 (not (cdar loop-bindings)))
|
|
988 (cl-push (car (cl-pop loop-bindings)) lets))
|
|
989 (setq body (list (cl-loop-let lets body nil))))))
|
|
990 (if loop-symbol-macs
|
|
991 (setq body (list (list* 'symbol-macrolet loop-symbol-macs body))))
|
|
992 (list* 'block loop-name body)))))
|
|
993
|
|
994 (defun cl-parse-loop-clause () ; uses args, loop-*
|
|
995 (let ((word (cl-pop args))
|
|
996 (hash-types '(hash-key hash-keys hash-value hash-values))
|
|
997 (key-types '(key-code key-codes key-seq key-seqs
|
|
998 key-binding key-bindings)))
|
|
999 (cond
|
|
1000
|
|
1001 ((null args)
|
|
1002 (error "Malformed `loop' macro"))
|
|
1003
|
|
1004 ((eq word 'named)
|
|
1005 (setq loop-name (cl-pop args)))
|
|
1006
|
|
1007 ((eq word 'initially)
|
|
1008 (if (memq (car args) '(do doing)) (cl-pop args))
|
|
1009 (or (consp (car args)) (error "Syntax error on `initially' clause"))
|
|
1010 (while (consp (car args))
|
|
1011 (cl-push (cl-pop args) loop-initially)))
|
|
1012
|
|
1013 ((eq word 'finally)
|
|
1014 (if (eq (car args) 'return)
|
|
1015 (setq loop-result-explicit (or (cl-pop2 args) '(quote nil)))
|
|
1016 (if (memq (car args) '(do doing)) (cl-pop args))
|
|
1017 (or (consp (car args)) (error "Syntax error on `finally' clause"))
|
|
1018 (if (and (eq (caar args) 'return) (null loop-name))
|
|
1019 (setq loop-result-explicit (or (nth 1 (cl-pop args)) '(quote nil)))
|
|
1020 (while (consp (car args))
|
|
1021 (cl-push (cl-pop args) loop-finally)))))
|
|
1022
|
|
1023 ((memq word '(for as))
|
|
1024 (let ((loop-for-bindings nil) (loop-for-sets nil) (loop-for-steps nil)
|
|
1025 (ands nil))
|
|
1026 (while
|
|
1027 (let ((var (or (cl-pop args) (gensym))))
|
|
1028 (setq word (cl-pop args))
|
|
1029 (if (eq word 'being) (setq word (cl-pop args)))
|
|
1030 (if (memq word '(the each)) (setq word (cl-pop args)))
|
|
1031 (if (memq word '(buffer buffers))
|
|
1032 (setq word 'in args (cons '(buffer-list) args)))
|
|
1033 (cond
|
|
1034
|
|
1035 ((memq word '(from downfrom upfrom to downto upto
|
|
1036 above below by))
|
|
1037 (cl-push word args)
|
|
1038 (if (memq (car args) '(downto above))
|
|
1039 (error "Must specify `from' value for downward loop"))
|
|
1040 (let* ((down (or (eq (car args) 'downfrom)
|
|
1041 (memq (caddr args) '(downto above))))
|
|
1042 (excl (or (memq (car args) '(above below))
|
|
1043 (memq (caddr args) '(above below))))
|
|
1044 (start (and (memq (car args) '(from upfrom downfrom))
|
|
1045 (cl-pop2 args)))
|
|
1046 (end (and (memq (car args)
|
|
1047 '(to upto downto above below))
|
|
1048 (cl-pop2 args)))
|
|
1049 (step (and (eq (car args) 'by) (cl-pop2 args)))
|
|
1050 (end-var (and (not (cl-const-expr-p end)) (gensym)))
|
|
1051 (step-var (and (not (cl-const-expr-p step))
|
|
1052 (gensym))))
|
|
1053 (and step (numberp step) (<= step 0)
|
|
1054 (error "Loop `by' value is not positive: %s" step))
|
|
1055 (cl-push (list var (or start 0)) loop-for-bindings)
|
|
1056 (if end-var (cl-push (list end-var end) loop-for-bindings))
|
|
1057 (if step-var (cl-push (list step-var step)
|
|
1058 loop-for-bindings))
|
|
1059 (if end
|
|
1060 (cl-push (list
|
|
1061 (if down (if excl '> '>=) (if excl '< '<=))
|
|
1062 var (or end-var end)) loop-body))
|
|
1063 (cl-push (list var (list (if down '- '+) var
|
|
1064 (or step-var step 1)))
|
|
1065 loop-for-steps)))
|
|
1066
|
|
1067 ((memq word '(in in-ref on))
|
|
1068 (let* ((on (eq word 'on))
|
|
1069 (temp (if (and on (symbolp var)) var (gensym))))
|
|
1070 (cl-push (list temp (cl-pop args)) loop-for-bindings)
|
|
1071 (cl-push (list 'consp temp) loop-body)
|
|
1072 (if (eq word 'in-ref)
|
|
1073 (cl-push (list var (list 'car temp)) loop-symbol-macs)
|
|
1074 (or (eq temp var)
|
|
1075 (progn
|
|
1076 (cl-push (list var nil) loop-for-bindings)
|
|
1077 (cl-push (list var (if on temp (list 'car temp)))
|
|
1078 loop-for-sets))))
|
|
1079 (cl-push (list temp
|
|
1080 (if (eq (car args) 'by)
|
|
1081 (let ((step (cl-pop2 args)))
|
|
1082 (if (and (memq (car-safe step)
|
|
1083 '(quote function
|
|
1084 function*))
|
|
1085 (symbolp (nth 1 step)))
|
|
1086 (list (nth 1 step) temp)
|
|
1087 (list 'funcall step temp)))
|
|
1088 (list 'cdr temp)))
|
|
1089 loop-for-steps)))
|
|
1090
|
|
1091 ((eq word '=)
|
|
1092 (let* ((start (cl-pop args))
|
|
1093 (then (if (eq (car args) 'then) (cl-pop2 args) start)))
|
|
1094 (cl-push (list var nil) loop-for-bindings)
|
|
1095 (if (or ands (eq (car args) 'and))
|
|
1096 (progn
|
|
1097 (cl-push (list var
|
|
1098 (list 'if
|
|
1099 (or loop-first-flag
|
|
1100 (setq loop-first-flag
|
|
1101 (gensym)))
|
|
1102 start var))
|
|
1103 loop-for-sets)
|
|
1104 (cl-push (list var then) loop-for-steps))
|
|
1105 (cl-push (list var
|
|
1106 (if (eq start then) start
|
|
1107 (list 'if
|
|
1108 (or loop-first-flag
|
|
1109 (setq loop-first-flag (gensym)))
|
|
1110 start then)))
|
|
1111 loop-for-sets))))
|
|
1112
|
|
1113 ((memq word '(across across-ref))
|
|
1114 (let ((temp-vec (gensym)) (temp-idx (gensym)))
|
|
1115 (cl-push (list temp-vec (cl-pop args)) loop-for-bindings)
|
|
1116 (cl-push (list temp-idx -1) loop-for-bindings)
|
|
1117 (cl-push (list '< (list 'setq temp-idx (list '1+ temp-idx))
|
|
1118 (list 'length temp-vec)) loop-body)
|
|
1119 (if (eq word 'across-ref)
|
|
1120 (cl-push (list var (list 'aref temp-vec temp-idx))
|
|
1121 loop-symbol-macs)
|
|
1122 (cl-push (list var nil) loop-for-bindings)
|
|
1123 (cl-push (list var (list 'aref temp-vec temp-idx))
|
|
1124 loop-for-sets))))
|
|
1125
|
|
1126 ((memq word '(element elements))
|
|
1127 (let ((ref (or (memq (car args) '(in-ref of-ref))
|
|
1128 (and (not (memq (car args) '(in of)))
|
|
1129 (error "Expected `of'"))))
|
|
1130 (seq (cl-pop2 args))
|
|
1131 (temp-seq (gensym))
|
|
1132 (temp-idx (if (eq (car args) 'using)
|
|
1133 (if (and (= (length (cadr args)) 2)
|
|
1134 (eq (caadr args) 'index))
|
|
1135 (cadr (cl-pop2 args))
|
|
1136 (error "Bad `using' clause"))
|
|
1137 (gensym))))
|
|
1138 (cl-push (list temp-seq seq) loop-for-bindings)
|
|
1139 (cl-push (list temp-idx 0) loop-for-bindings)
|
|
1140 (if ref
|
|
1141 (let ((temp-len (gensym)))
|
|
1142 (cl-push (list temp-len (list 'length temp-seq))
|
|
1143 loop-for-bindings)
|
|
1144 (cl-push (list var (list 'elt temp-seq temp-idx))
|
|
1145 loop-symbol-macs)
|
|
1146 (cl-push (list '< temp-idx temp-len) loop-body))
|
|
1147 (cl-push (list var nil) loop-for-bindings)
|
|
1148 (cl-push (list 'and temp-seq
|
|
1149 (list 'or (list 'consp temp-seq)
|
|
1150 (list '< temp-idx
|
|
1151 (list 'length temp-seq))))
|
|
1152 loop-body)
|
|
1153 (cl-push (list var (list 'if (list 'consp temp-seq)
|
|
1154 (list 'pop temp-seq)
|
|
1155 (list 'aref temp-seq temp-idx)))
|
|
1156 loop-for-sets))
|
|
1157 (cl-push (list temp-idx (list '1+ temp-idx))
|
|
1158 loop-for-steps)))
|
|
1159
|
|
1160 ((memq word hash-types)
|
|
1161 (or (memq (car args) '(in of)) (error "Expected `of'"))
|
|
1162 (let* ((table (cl-pop2 args))
|
|
1163 (other (if (eq (car args) 'using)
|
|
1164 (if (and (= (length (cadr args)) 2)
|
|
1165 (memq (caadr args) hash-types)
|
|
1166 (not (eq (caadr args) word)))
|
|
1167 (cadr (cl-pop2 args))
|
|
1168 (error "Bad `using' clause"))
|
|
1169 (gensym))))
|
|
1170 (if (memq word '(hash-value hash-values))
|
|
1171 (setq var (prog1 other (setq other var))))
|
|
1172 (setq loop-map-form
|
|
1173 (list 'maphash (list 'function
|
|
1174 (list* 'lambda (list var other)
|
|
1175 '--cl-map)) table))))
|
|
1176
|
|
1177 ((memq word '(symbol present-symbol external-symbol
|
|
1178 symbols present-symbols external-symbols))
|
|
1179 (let ((ob (and (memq (car args) '(in of)) (cl-pop2 args))))
|
|
1180 (setq loop-map-form
|
|
1181 (list 'mapatoms (list 'function
|
|
1182 (list* 'lambda (list var)
|
|
1183 '--cl-map)) ob))))
|
|
1184
|
|
1185 ((memq word '(overlay overlays extent extents))
|
|
1186 (let ((buf nil) (from nil) (to nil))
|
|
1187 (while (memq (car args) '(in of from to))
|
|
1188 (cond ((eq (car args) 'from) (setq from (cl-pop2 args)))
|
|
1189 ((eq (car args) 'to) (setq to (cl-pop2 args)))
|
|
1190 (t (setq buf (cl-pop2 args)))))
|
|
1191 (setq loop-map-form
|
|
1192 (list 'cl-map-extents
|
|
1193 (list 'function (list 'lambda (list var (gensym))
|
|
1194 '(progn . --cl-map) nil))
|
|
1195 buf from to))))
|
|
1196
|
|
1197 ((memq word '(interval intervals))
|
|
1198 (let ((buf nil) (prop nil) (from nil) (to nil)
|
|
1199 (var1 (gensym)) (var2 (gensym)))
|
|
1200 (while (memq (car args) '(in of property from to))
|
|
1201 (cond ((eq (car args) 'from) (setq from (cl-pop2 args)))
|
|
1202 ((eq (car args) 'to) (setq to (cl-pop2 args)))
|
|
1203 ((eq (car args) 'property)
|
|
1204 (setq prop (cl-pop2 args)))
|
|
1205 (t (setq buf (cl-pop2 args)))))
|
|
1206 (if (and (consp var) (symbolp (car var)) (symbolp (cdr var)))
|
|
1207 (setq var1 (car var) var2 (cdr var))
|
|
1208 (cl-push (list var (list 'cons var1 var2)) loop-for-sets))
|
|
1209 (setq loop-map-form
|
|
1210 (list 'cl-map-intervals
|
|
1211 (list 'function (list 'lambda (list var1 var2)
|
|
1212 '(progn . --cl-map)))
|
|
1213 buf prop from to))))
|
|
1214
|
|
1215 ((memq word key-types)
|
|
1216 (or (memq (car args) '(in of)) (error "Expected `of'"))
|
800
|
1217 (let* ((map (cl-pop2 args))
|
|
1218 other-word
|
|
1219 (other (if (eq (car args) 'using)
|
|
1220 (if (and (= (length (cadr args)) 2)
|
|
1221 (memq (setq other-word (caadr args))
|
|
1222 key-types)
|
|
1223 (not (eq (caadr args) word)))
|
|
1224 (cadr (cl-pop2 args))
|
|
1225 (error "Bad `using' clause"))
|
428
|
1226 (gensym))))
|
800
|
1227 (when (memq word '(key-binding key-bindings))
|
|
1228 (setq var (prog1 other (setq other var)))
|
|
1229 (and other-word (setq word other-word)))
|
428
|
1230 (setq loop-map-form
|
|
1231 (list (if (memq word '(key-seq key-seqs))
|
|
1232 'cl-map-keymap-recursively 'cl-map-keymap)
|
|
1233 (list 'function (list* 'lambda (list var other)
|
|
1234 '--cl-map)) map))))
|
|
1235
|
|
1236 ((memq word '(frame frames screen screens))
|
|
1237 (let ((temp (gensym)))
|
|
1238 (cl-push (list var '(selected-frame))
|
|
1239 loop-for-bindings)
|
|
1240 (cl-push (list temp nil) loop-for-bindings)
|
|
1241 (cl-push (list 'prog1 (list 'not (list 'eq var temp))
|
|
1242 (list 'or temp (list 'setq temp var)))
|
|
1243 loop-body)
|
|
1244 (cl-push (list var (list 'next-frame var))
|
|
1245 loop-for-steps)))
|
|
1246
|
|
1247 ((memq word '(window windows))
|
|
1248 (let ((scr (and (memq (car args) '(in of)) (cl-pop2 args)))
|
|
1249 (temp (gensym)))
|
|
1250 (cl-push (list var (if scr
|
|
1251 (list 'frame-selected-window scr)
|
|
1252 '(selected-window)))
|
|
1253 loop-for-bindings)
|
|
1254 (cl-push (list temp nil) loop-for-bindings)
|
|
1255 (cl-push (list 'prog1 (list 'not (list 'eq var temp))
|
|
1256 (list 'or temp (list 'setq temp var)))
|
|
1257 loop-body)
|
|
1258 (cl-push (list var (list 'next-window var)) loop-for-steps)))
|
|
1259
|
|
1260 (t
|
|
1261 (let ((handler (and (symbolp word)
|
|
1262 (get word 'cl-loop-for-handler))))
|
|
1263 (if handler
|
|
1264 (funcall handler var)
|
|
1265 (error "Expected a `for' preposition, found %s" word)))))
|
|
1266 (eq (car args) 'and))
|
|
1267 (setq ands t)
|
|
1268 (cl-pop args))
|
|
1269 (if (and ands loop-for-bindings)
|
|
1270 (cl-push (nreverse loop-for-bindings) loop-bindings)
|
|
1271 (setq loop-bindings (nconc (mapcar 'list loop-for-bindings)
|
|
1272 loop-bindings)))
|
|
1273 (if loop-for-sets
|
|
1274 (cl-push (list 'progn
|
|
1275 (cl-loop-let (nreverse loop-for-sets) 'setq ands)
|
|
1276 t) loop-body))
|
|
1277 (if loop-for-steps
|
|
1278 (cl-push (cons (if ands 'psetq 'setq)
|
|
1279 (apply 'append (nreverse loop-for-steps)))
|
|
1280 loop-steps))))
|
|
1281
|
|
1282 ((eq word 'repeat)
|
|
1283 (let ((temp (gensym)))
|
|
1284 (cl-push (list (list temp (cl-pop args))) loop-bindings)
|
|
1285 (cl-push (list '>= (list 'setq temp (list '1- temp)) 0) loop-body)))
|
|
1286
|
|
1287 ((eq word 'collect)
|
|
1288 (let ((what (cl-pop args))
|
|
1289 (var (cl-loop-handle-accum nil 'nreverse)))
|
|
1290 (if (eq var loop-accum-var)
|
|
1291 (cl-push (list 'progn (list 'push what var) t) loop-body)
|
|
1292 (cl-push (list 'progn
|
|
1293 (list 'setq var (list 'nconc var (list 'list what)))
|
|
1294 t) loop-body))))
|
|
1295
|
|
1296 ((memq word '(nconc nconcing append appending))
|
|
1297 (let ((what (cl-pop args))
|
|
1298 (var (cl-loop-handle-accum nil 'nreverse)))
|
|
1299 (cl-push (list 'progn
|
|
1300 (list 'setq var
|
|
1301 (if (eq var loop-accum-var)
|
|
1302 (list 'nconc
|
|
1303 (list (if (memq word '(nconc nconcing))
|
|
1304 'nreverse 'reverse)
|
|
1305 what)
|
|
1306 var)
|
|
1307 (list (if (memq word '(nconc nconcing))
|
|
1308 'nconc 'append)
|
|
1309 var what))) t) loop-body)))
|
|
1310
|
|
1311 ((memq word '(concat concating))
|
|
1312 (let ((what (cl-pop args))
|
|
1313 (var (cl-loop-handle-accum "")))
|
|
1314 (cl-push (list 'progn (list 'callf 'concat var what) t) loop-body)))
|
|
1315
|
|
1316 ((memq word '(vconcat vconcating))
|
|
1317 (let ((what (cl-pop args))
|
|
1318 (var (cl-loop-handle-accum [])))
|
|
1319 (cl-push (list 'progn (list 'callf 'vconcat var what) t) loop-body)))
|
|
1320
|
800
|
1321 ((memq word '(bvconcat bvconcating))
|
|
1322 (let ((what (cl-pop args))
|
|
1323 (var (cl-loop-handle-accum #*)))
|
|
1324 (cl-push (list 'progn (list 'callf 'bvconcat var what) t) loop-body)))
|
|
1325
|
428
|
1326 ((memq word '(sum summing))
|
|
1327 (let ((what (cl-pop args))
|
|
1328 (var (cl-loop-handle-accum 0)))
|
|
1329 (cl-push (list 'progn (list 'incf var what) t) loop-body)))
|
|
1330
|
|
1331 ((memq word '(count counting))
|
|
1332 (let ((what (cl-pop args))
|
|
1333 (var (cl-loop-handle-accum 0)))
|
|
1334 (cl-push (list 'progn (list 'if what (list 'incf var)) t) loop-body)))
|
|
1335
|
|
1336 ((memq word '(minimize minimizing maximize maximizing))
|
|
1337 (let* ((what (cl-pop args))
|
|
1338 (temp (if (cl-simple-expr-p what) what (gensym)))
|
|
1339 (var (cl-loop-handle-accum nil))
|
|
1340 (func (intern (substring (symbol-name word) 0 3)))
|
|
1341 (set (list 'setq var (list 'if var (list func var temp) temp))))
|
|
1342 (cl-push (list 'progn (if (eq temp what) set
|
|
1343 (list 'let (list (list temp what)) set))
|
|
1344 t) loop-body)))
|
|
1345
|
|
1346 ((eq word 'with)
|
|
1347 (let ((bindings nil))
|
|
1348 (while (progn (cl-push (list (cl-pop args)
|
|
1349 (and (eq (car args) '=) (cl-pop2 args)))
|
|
1350 bindings)
|
|
1351 (eq (car args) 'and))
|
|
1352 (cl-pop args))
|
|
1353 (cl-push (nreverse bindings) loop-bindings)))
|
|
1354
|
|
1355 ((eq word 'while)
|
|
1356 (cl-push (cl-pop args) loop-body))
|
|
1357
|
|
1358 ((eq word 'until)
|
|
1359 (cl-push (list 'not (cl-pop args)) loop-body))
|
|
1360
|
|
1361 ((eq word 'always)
|
|
1362 (or loop-finish-flag (setq loop-finish-flag (gensym)))
|
|
1363 (cl-push (list 'setq loop-finish-flag (cl-pop args)) loop-body)
|
|
1364 (setq loop-result t))
|
|
1365
|
|
1366 ((eq word 'never)
|
|
1367 (or loop-finish-flag (setq loop-finish-flag (gensym)))
|
|
1368 (cl-push (list 'setq loop-finish-flag (list 'not (cl-pop args)))
|
|
1369 loop-body)
|
|
1370 (setq loop-result t))
|
|
1371
|
|
1372 ((eq word 'thereis)
|
|
1373 (or loop-finish-flag (setq loop-finish-flag (gensym)))
|
|
1374 (or loop-result-var (setq loop-result-var (gensym)))
|
|
1375 (cl-push (list 'setq loop-finish-flag
|
|
1376 (list 'not (list 'setq loop-result-var (cl-pop args))))
|
|
1377 loop-body))
|
|
1378
|
|
1379 ((memq word '(if when unless))
|
|
1380 (let* ((cond (cl-pop args))
|
|
1381 (then (let ((loop-body nil))
|
|
1382 (cl-parse-loop-clause)
|
|
1383 (cl-loop-build-ands (nreverse loop-body))))
|
|
1384 (else (let ((loop-body nil))
|
|
1385 (if (eq (car args) 'else)
|
|
1386 (progn (cl-pop args) (cl-parse-loop-clause)))
|
|
1387 (cl-loop-build-ands (nreverse loop-body))))
|
|
1388 (simple (and (eq (car then) t) (eq (car else) t))))
|
|
1389 (if (eq (car args) 'end) (cl-pop args))
|
|
1390 (if (eq word 'unless) (setq then (prog1 else (setq else then))))
|
|
1391 (let ((form (cons (if simple (cons 'progn (nth 1 then)) (nth 2 then))
|
|
1392 (if simple (nth 1 else) (list (nth 2 else))))))
|
|
1393 (if (cl-expr-contains form 'it)
|
|
1394 (let ((temp (gensym)))
|
|
1395 (cl-push (list temp) loop-bindings)
|
|
1396 (setq form (list* 'if (list 'setq temp cond)
|
|
1397 (subst temp 'it form))))
|
|
1398 (setq form (list* 'if cond form)))
|
|
1399 (cl-push (if simple (list 'progn form t) form) loop-body))))
|
|
1400
|
|
1401 ((memq word '(do doing))
|
|
1402 (let ((body nil))
|
|
1403 (or (consp (car args)) (error "Syntax error on `do' clause"))
|
|
1404 (while (consp (car args)) (cl-push (cl-pop args) body))
|
|
1405 (cl-push (cons 'progn (nreverse (cons t body))) loop-body)))
|
|
1406
|
|
1407 ((eq word 'return)
|
|
1408 (or loop-finish-flag (setq loop-finish-flag (gensym)))
|
|
1409 (or loop-result-var (setq loop-result-var (gensym)))
|
|
1410 (cl-push (list 'setq loop-result-var (cl-pop args)
|
|
1411 loop-finish-flag nil) loop-body))
|
|
1412
|
|
1413 (t
|
|
1414 (let ((handler (and (symbolp word) (get word 'cl-loop-handler))))
|
|
1415 (or handler (error "Expected a loop keyword, found %s" word))
|
|
1416 (funcall handler))))
|
|
1417 (if (eq (car args) 'and)
|
|
1418 (progn (cl-pop args) (cl-parse-loop-clause)))))
|
|
1419
|
|
1420 (defun cl-loop-let (specs body par) ; uses loop-*
|
|
1421 (let ((p specs) (temps nil) (new nil))
|
|
1422 (while (and p (or (symbolp (car-safe (car p))) (null (cadar p))))
|
|
1423 (setq p (cdr p)))
|
|
1424 (and par p
|
|
1425 (progn
|
|
1426 (setq par nil p specs)
|
|
1427 (while p
|
|
1428 (or (cl-const-expr-p (cadar p))
|
|
1429 (let ((temp (gensym)))
|
|
1430 (cl-push (list temp (cadar p)) temps)
|
|
1431 (setcar (cdar p) temp)))
|
|
1432 (setq p (cdr p)))))
|
|
1433 (while specs
|
|
1434 (if (and (consp (car specs)) (listp (caar specs)))
|
|
1435 (let* ((spec (caar specs)) (nspecs nil)
|
|
1436 (expr (cadr (cl-pop specs)))
|
|
1437 (temp (cdr (or (assq spec loop-destr-temps)
|
|
1438 (car (cl-push (cons spec (or (last spec 0)
|
|
1439 (gensym)))
|
|
1440 loop-destr-temps))))))
|
|
1441 (cl-push (list temp expr) new)
|
|
1442 (while (consp spec)
|
|
1443 (cl-push (list (cl-pop spec)
|
|
1444 (and expr (list (if spec 'pop 'car) temp)))
|
|
1445 nspecs))
|
|
1446 (setq specs (nconc (nreverse nspecs) specs)))
|
|
1447 (cl-push (cl-pop specs) new)))
|
|
1448 (if (eq body 'setq)
|
|
1449 (let ((set (cons (if par 'psetq 'setq) (apply 'nconc (nreverse new)))))
|
|
1450 (if temps (list 'let* (nreverse temps) set) set))
|
|
1451 (list* (if par 'let 'let*)
|
|
1452 (nconc (nreverse temps) (nreverse new)) body))))
|
|
1453
|
|
1454 (defun cl-loop-handle-accum (def &optional func) ; uses args, loop-*
|
|
1455 (if (eq (car args) 'into)
|
|
1456 (let ((var (cl-pop2 args)))
|
|
1457 (or (memq var loop-accum-vars)
|
|
1458 (progn (cl-push (list (list var def)) loop-bindings)
|
|
1459 (cl-push var loop-accum-vars)))
|
|
1460 var)
|
|
1461 (or loop-accum-var
|
|
1462 (progn
|
|
1463 (cl-push (list (list (setq loop-accum-var (gensym)) def))
|
|
1464 loop-bindings)
|
|
1465 (setq loop-result (if func (list func loop-accum-var)
|
|
1466 loop-accum-var))
|
|
1467 loop-accum-var))))
|
|
1468
|
|
1469 (defun cl-loop-build-ands (clauses)
|
|
1470 (let ((ands nil)
|
|
1471 (body nil))
|
|
1472 (while clauses
|
|
1473 (if (and (eq (car-safe (car clauses)) 'progn)
|
|
1474 (eq (car (last (car clauses))) t))
|
|
1475 (if (cdr clauses)
|
|
1476 (setq clauses (cons (nconc (butlast (car clauses))
|
|
1477 (if (eq (car-safe (cadr clauses))
|
|
1478 'progn)
|
|
1479 (cdadr clauses)
|
|
1480 (list (cadr clauses))))
|
|
1481 (cddr clauses)))
|
|
1482 (setq body (cdr (butlast (cl-pop clauses)))))
|
|
1483 (cl-push (cl-pop clauses) ands)))
|
|
1484 (setq ands (or (nreverse ands) (list t)))
|
|
1485 (list (if (cdr ands) (cons 'and ands) (car ands))
|
|
1486 body
|
|
1487 (let ((full (if body
|
|
1488 (append ands (list (cons 'progn (append body '(t)))))
|
|
1489 ands)))
|
|
1490 (if (cdr full) (cons 'and full) (car full))))))
|
|
1491
|
|
1492
|
|
1493 ;;; Other iteration control structures.
|
|
1494
|
|
1495 ;;;###autoload
|
|
1496 (defmacro do (steps endtest &rest body)
|
|
1497 "The Common Lisp `do' loop.
|
|
1498 Format is: (do ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)"
|
|
1499 (cl-expand-do-loop steps endtest body nil))
|
|
1500
|
|
1501 ;;;###autoload
|
|
1502 (defmacro do* (steps endtest &rest body)
|
|
1503 "The Common Lisp `do*' loop.
|
|
1504 Format is: (do* ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)"
|
|
1505 (cl-expand-do-loop steps endtest body t))
|
|
1506
|
|
1507 (defun cl-expand-do-loop (steps endtest body star)
|
|
1508 (list 'block nil
|
|
1509 (list* (if star 'let* 'let)
|
|
1510 (mapcar #'(lambda (c) (if (consp c) (list (car c) (nth 1 c)) c))
|
|
1511 steps)
|
|
1512 (list* 'while (list 'not (car endtest))
|
|
1513 (append body
|
|
1514 (let ((sets (mapcar
|
|
1515 #'(lambda (c)
|
|
1516 (and (consp c) (cdr (cdr c))
|
|
1517 (list (car c) (nth 2 c))))
|
|
1518 steps)))
|
|
1519 (setq sets (delq nil sets))
|
|
1520 (and sets
|
|
1521 (list (cons (if (or star (not (cdr sets)))
|
|
1522 'setq 'psetq)
|
|
1523 (apply 'append sets)))))))
|
|
1524 (or (cdr endtest) '(nil)))))
|
|
1525
|
|
1526 ;;;###autoload
|
|
1527 (defmacro dolist (spec &rest body)
|
|
1528 "(dolist (VAR LIST [RESULT]) BODY...): loop over a list.
|
|
1529 Evaluate BODY with VAR bound to each `car' from LIST, in turn.
|
|
1530 Then evaluate RESULT to get return value, default nil."
|
|
1531 (let ((temp (gensym "--dolist-temp--")))
|
|
1532 (list 'block nil
|
|
1533 (list* 'let (list (list temp (nth 1 spec)) (car spec))
|
|
1534 (list* 'while temp (list 'setq (car spec) (list 'car temp))
|
|
1535 (append body (list (list 'setq temp
|
|
1536 (list 'cdr temp)))))
|
|
1537 (if (cdr (cdr spec))
|
|
1538 (cons (list 'setq (car spec) nil) (cdr (cdr spec)))
|
|
1539 '(nil))))))
|
|
1540
|
|
1541 ;;;###autoload
|
|
1542 (defmacro dotimes (spec &rest body)
|
|
1543 "(dotimes (VAR COUNT [RESULT]) BODY...): loop a certain number of times.
|
|
1544 Evaluate BODY with VAR bound to successive integers from 0, inclusive,
|
|
1545 to COUNT, exclusive. Then evaluate RESULT to get return value, default
|
|
1546 nil."
|
|
1547 (let ((temp (gensym "--dotimes-temp--")))
|
|
1548 (list 'block nil
|
|
1549 (list* 'let (list (list temp (nth 1 spec)) (list (car spec) 0))
|
|
1550 (list* 'while (list '< (car spec) temp)
|
|
1551 (append body (list (list 'incf (car spec)))))
|
|
1552 (or (cdr (cdr spec)) '(nil))))))
|
|
1553
|
|
1554 ;;;###autoload
|
|
1555 (defmacro do-symbols (spec &rest body)
|
|
1556 "(dosymbols (VAR [OBARRAY [RESULT]]) BODY...): loop over all symbols.
|
|
1557 Evaluate BODY with VAR bound to each interned symbol, or to each symbol
|
|
1558 from OBARRAY."
|
|
1559 ;; Apparently this doesn't have an implicit block.
|
|
1560 (list 'block nil
|
|
1561 (list 'let (list (car spec))
|
|
1562 (list* 'mapatoms
|
|
1563 (list 'function (list* 'lambda (list (car spec)) body))
|
|
1564 (and (cadr spec) (list (cadr spec))))
|
|
1565 (caddr spec))))
|
|
1566
|
|
1567 ;;;###autoload
|
|
1568 (defmacro do-all-symbols (spec &rest body)
|
|
1569 (list* 'do-symbols (list (car spec) nil (cadr spec)) body))
|
|
1570
|
|
1571
|
|
1572 ;;; Assignments.
|
|
1573
|
|
1574 ;;;###autoload
|
|
1575 (defmacro psetq (&rest args)
|
|
1576 "(psetq SYM VAL SYM VAL ...): set SYMs to the values VALs in parallel.
|
|
1577 This is like `setq', except that all VAL forms are evaluated (in order)
|
|
1578 before assigning any symbols SYM to the corresponding values."
|
|
1579 (cons 'psetf args))
|
|
1580
|
|
1581
|
|
1582 ;;; Binding control structures.
|
|
1583
|
|
1584 ;;;###autoload
|
|
1585 (defmacro progv (symbols values &rest body)
|
|
1586 "(progv SYMBOLS VALUES BODY...): bind SYMBOLS to VALUES dynamically in BODY.
|
|
1587 The forms SYMBOLS and VALUES are evaluated, and must evaluate to lists.
|
|
1588 Each SYMBOL in the first list is bound to the corresponding VALUE in the
|
|
1589 second list (or made unbound if VALUES is shorter than SYMBOLS); then the
|
|
1590 BODY forms are executed and their result is returned. This is much like
|
|
1591 a `let' form, except that the list of symbols can be computed at run-time."
|
|
1592 (list 'let '((cl-progv-save nil))
|
|
1593 (list 'unwind-protect
|
|
1594 (list* 'progn (list 'cl-progv-before symbols values) body)
|
|
1595 '(cl-progv-after))))
|
|
1596
|
|
1597 ;;; This should really have some way to shadow 'byte-compile properties, etc.
|
|
1598 ;;;###autoload
|
|
1599 (defmacro flet (bindings &rest body)
|
|
1600 "(flet ((FUNC ARGLIST BODY...) ...) FORM...): make temporary function defns.
|
|
1601 This is an analogue of `let' that operates on the function cell of FUNC
|
|
1602 rather than its value cell. The FORMs are evaluated with the specified
|
|
1603 function definitions in place, then the definitions are undone (the FUNCs
|
|
1604 go back to their previous definitions, or lack thereof)."
|
|
1605 (list* 'letf*
|
|
1606 (mapcar
|
|
1607 #'(lambda (x)
|
|
1608 (if (or (and (fboundp (car x))
|
|
1609 (eq (car-safe (symbol-function (car x))) 'macro))
|
|
1610 (cdr (assq (car x) cl-macro-environment)))
|
|
1611 (error "Use `labels', not `flet', to rebind macro names"))
|
|
1612 (let ((func (list 'function*
|
|
1613 (list 'lambda (cadr x)
|
|
1614 (list* 'block (car x) (cddr x))))))
|
|
1615 (if (and (cl-compiling-file)
|
|
1616 (boundp 'byte-compile-function-environment))
|
|
1617 (cl-push (cons (car x) (eval func))
|
|
1618 byte-compile-function-environment))
|
|
1619 (list (list 'symbol-function (list 'quote (car x))) func)))
|
|
1620 bindings)
|
|
1621 body))
|
|
1622
|
|
1623 ;;;###autoload
|
|
1624 (defmacro labels (bindings &rest body)
|
|
1625 "(labels ((FUNC ARGLIST BODY...) ...) FORM...): make temporary func bindings.
|
|
1626 This is like `flet', except the bindings are lexical instead of dynamic.
|
|
1627 Unlike `flet', this macro is fully compliant with the Common Lisp standard."
|
|
1628 (let ((vars nil) (sets nil) (cl-macro-environment cl-macro-environment))
|
|
1629 (while bindings
|
|
1630 (let ((var (gensym)))
|
|
1631 (cl-push var vars)
|
|
1632 (cl-push (list 'function* (cons 'lambda (cdar bindings))) sets)
|
|
1633 (cl-push var sets)
|
|
1634 (cl-push (list (car (cl-pop bindings)) 'lambda '(&rest cl-labels-args)
|
|
1635 (list 'list* '(quote funcall) (list 'quote var)
|
|
1636 'cl-labels-args))
|
|
1637 cl-macro-environment)))
|
|
1638 (cl-macroexpand-all (list* 'lexical-let vars (cons (cons 'setq sets) body))
|
|
1639 cl-macro-environment)))
|
|
1640
|
|
1641 ;; The following ought to have a better definition for use with newer
|
|
1642 ;; byte compilers.
|
|
1643 ;;;###autoload
|
|
1644 (defmacro macrolet (bindings &rest body)
|
|
1645 "(macrolet ((NAME ARGLIST BODY...) ...) FORM...): make temporary macro defns.
|
|
1646 This is like `flet', but for macros instead of functions."
|
|
1647 (if (cdr bindings)
|
|
1648 (list 'macrolet
|
|
1649 (list (car bindings)) (list* 'macrolet (cdr bindings) body))
|
|
1650 (if (null bindings) (cons 'progn body)
|
|
1651 (let* ((name (caar bindings))
|
|
1652 (res (cl-transform-lambda (cdar bindings) name)))
|
|
1653 (eval (car res))
|
|
1654 (cl-macroexpand-all (cons 'progn body)
|
|
1655 (cons (list* name 'lambda (cdr res))
|
|
1656 cl-macro-environment))))))
|
|
1657
|
|
1658 ;;;###autoload
|
|
1659 (defmacro symbol-macrolet (bindings &rest body)
|
|
1660 "(symbol-macrolet ((NAME EXPANSION) ...) FORM...): make symbol macro defns.
|
|
1661 Within the body FORMs, references to the variable NAME will be replaced
|
|
1662 by EXPANSION, and (setq NAME ...) will act like (setf EXPANSION ...)."
|
|
1663 (if (cdr bindings)
|
|
1664 (list 'symbol-macrolet
|
|
1665 (list (car bindings)) (list* 'symbol-macrolet (cdr bindings) body))
|
|
1666 (if (null bindings) (cons 'progn body)
|
|
1667 (cl-macroexpand-all (cons 'progn body)
|
|
1668 (cons (list (symbol-name (caar bindings))
|
|
1669 (cadar bindings))
|
|
1670 cl-macro-environment)))))
|
|
1671
|
|
1672 (defvar cl-closure-vars nil)
|
|
1673 ;;;###autoload
|
|
1674 (defmacro lexical-let (bindings &rest body)
|
|
1675 "(lexical-let BINDINGS BODY...): like `let', but lexically scoped.
|
|
1676 The main visible difference is that lambdas inside BODY will create
|
|
1677 lexical closures as in Common Lisp."
|
|
1678 (let* ((cl-closure-vars cl-closure-vars)
|
|
1679 (vars (mapcar #'(lambda (x)
|
|
1680 (or (consp x) (setq x (list x)))
|
|
1681 (cl-push (gensym (format "--%s--" (car x)))
|
|
1682 cl-closure-vars)
|
|
1683 (list (car x) (cadr x) (car cl-closure-vars)))
|
|
1684 bindings))
|
|
1685 (ebody
|
|
1686 (cl-macroexpand-all
|
|
1687 (cons 'progn body)
|
|
1688 (nconc (mapcar #'(lambda (x)
|
|
1689 (list (symbol-name (car x))
|
|
1690 (list 'symbol-value (caddr x))
|
|
1691 t))
|
|
1692 vars)
|
|
1693 (list '(defun . cl-defun-expander))
|
|
1694 cl-macro-environment))))
|
|
1695 (if (not (get (car (last cl-closure-vars)) 'used))
|
|
1696 (list 'let (mapcar #'(lambda (x) (list (caddr x) (cadr x))) vars)
|
|
1697 (sublis (mapcar #'(lambda (x)
|
|
1698 (cons (caddr x) (list 'quote (caddr x))))
|
|
1699 vars)
|
|
1700 ebody))
|
|
1701 (list 'let (mapcar #'(lambda (x)
|
|
1702 (list (caddr x)
|
|
1703 (list 'make-symbol
|
|
1704 (format "--%s--" (car x)))))
|
|
1705 vars)
|
|
1706 (apply 'append '(setf)
|
|
1707 (mapcar #'(lambda (x)
|
|
1708 (list (list 'symbol-value (caddr x)) (cadr x)))
|
|
1709 vars))
|
|
1710 ebody))))
|
|
1711
|
|
1712 ;;;###autoload
|
|
1713 (defmacro lexical-let* (bindings &rest body)
|
|
1714 "(lexical-let* BINDINGS BODY...): like `let*', but lexically scoped.
|
|
1715 The main visible difference is that lambdas inside BODY will create
|
|
1716 lexical closures as in Common Lisp."
|
|
1717 (if (null bindings) (cons 'progn body)
|
|
1718 (setq bindings (reverse bindings))
|
|
1719 (while bindings
|
|
1720 (setq body (list (list* 'lexical-let (list (cl-pop bindings)) body))))
|
|
1721 (car body)))
|
|
1722
|
|
1723 (defun cl-defun-expander (func &rest rest)
|
|
1724 (list 'progn
|
|
1725 (list 'defalias (list 'quote func)
|
|
1726 (list 'function (cons 'lambda rest)))
|
|
1727 (list 'quote func)))
|
|
1728
|
|
1729
|
|
1730 ;;; Multiple values.
|
|
1731
|
|
1732 ;;;###autoload
|
|
1733 (defmacro multiple-value-bind (vars form &rest body)
|
|
1734 "(multiple-value-bind (SYM SYM...) FORM BODY): collect multiple return values.
|
|
1735 FORM must return a list; the BODY is then executed with the first N elements
|
|
1736 of this list bound (`let'-style) to each of the symbols SYM in turn. This
|
|
1737 is analogous to the Common Lisp `multiple-value-bind' macro, using lists to
|
|
1738 simulate true multiple return values. For compatibility, (values A B C) is
|
|
1739 a synonym for (list A B C)."
|
|
1740 (let ((temp (gensym)) (n -1))
|
|
1741 (list* 'let* (cons (list temp form)
|
|
1742 (mapcar #'(lambda (v)
|
|
1743 (list v (list 'nth (setq n (1+ n)) temp)))
|
|
1744 vars))
|
|
1745 body)))
|
|
1746
|
|
1747 ;;;###autoload
|
|
1748 (defmacro multiple-value-setq (vars form)
|
|
1749 "(multiple-value-setq (SYM SYM...) FORM): collect multiple return values.
|
|
1750 FORM must return a list; the first N elements of this list are stored in
|
|
1751 each of the symbols SYM in turn. This is analogous to the Common Lisp
|
|
1752 `multiple-value-setq' macro, using lists to simulate true multiple return
|
|
1753 values. For compatibility, (values A B C) is a synonym for (list A B C)."
|
|
1754 (cond ((null vars) (list 'progn form nil))
|
|
1755 ((null (cdr vars)) (list 'setq (car vars) (list 'car form)))
|
|
1756 (t
|
|
1757 (let* ((temp (gensym)) (n 0))
|
|
1758 (list 'let (list (list temp form))
|
|
1759 (list 'prog1 (list 'setq (cl-pop vars) (list 'car temp))
|
|
1760 (cons 'setq
|
|
1761 (apply 'nconc
|
|
1762 (mapcar
|
|
1763 #'(lambda (v)
|
|
1764 (list v (list
|
|
1765 'nth
|
|
1766 (setq n (1+ n))
|
|
1767 temp)))
|
|
1768 vars)))))))))
|
|
1769
|
|
1770
|
|
1771 ;;; Declarations.
|
|
1772
|
|
1773 ;;;###autoload
|
|
1774 (defmacro locally (&rest body) (cons 'progn body))
|
|
1775 ;;;###autoload
|
|
1776 (defmacro the (type form) form)
|
|
1777
|
|
1778 (defvar cl-proclaim-history t) ; for future compilers
|
|
1779 (defvar cl-declare-stack t) ; for future compilers
|
|
1780
|
|
1781 (defun cl-do-proclaim (spec hist)
|
|
1782 (and hist (listp cl-proclaim-history) (cl-push spec cl-proclaim-history))
|
|
1783 (cond ((eq (car-safe spec) 'special)
|
|
1784 (if (boundp 'byte-compile-bound-variables)
|
|
1785 (setq byte-compile-bound-variables
|
442
|
1786 (append
|
|
1787 (mapcar #'(lambda (v) (cons v byte-compile-global-bit))
|
|
1788 (cdr spec))
|
|
1789 byte-compile-bound-variables))))
|
428
|
1790
|
|
1791 ((eq (car-safe spec) 'inline)
|
|
1792 (while (setq spec (cdr spec))
|
|
1793 (or (memq (get (car spec) 'byte-optimizer)
|
|
1794 '(nil byte-compile-inline-expand))
|
|
1795 (error "%s already has a byte-optimizer, can't make it inline"
|
|
1796 (car spec)))
|
|
1797 (put (car spec) 'byte-optimizer 'byte-compile-inline-expand)))
|
|
1798
|
|
1799 ((eq (car-safe spec) 'notinline)
|
|
1800 (while (setq spec (cdr spec))
|
|
1801 (if (eq (get (car spec) 'byte-optimizer)
|
|
1802 'byte-compile-inline-expand)
|
|
1803 (put (car spec) 'byte-optimizer nil))))
|
|
1804
|
|
1805 ((eq (car-safe spec) 'optimize)
|
|
1806 (let ((speed (assq (nth 1 (assq 'speed (cdr spec)))
|
446
|
1807 '((0 . nil) (1 . t) (2 . t) (3 . t))))
|
428
|
1808 (safety (assq (nth 1 (assq 'safety (cdr spec)))
|
446
|
1809 '((0 . t) (1 . t) (2 . t) (3 . nil)))))
|
|
1810 (when speed
|
|
1811 (setq cl-optimize-speed (car speed)
|
|
1812 byte-optimize (cdr speed)))
|
|
1813 (when safety
|
|
1814 (setq cl-optimize-safety (car safety)
|
|
1815 byte-compile-delete-errors (cdr safety)))))
|
428
|
1816
|
|
1817 ((and (eq (car-safe spec) 'warn) (boundp 'byte-compile-warnings))
|
|
1818 (if (eq byte-compile-warnings t)
|
|
1819 ;; XEmacs change
|
|
1820 (setq byte-compile-warnings byte-compile-default-warnings))
|
|
1821 (while (setq spec (cdr spec))
|
|
1822 (if (consp (car spec))
|
|
1823 (if (eq (cadar spec) 0)
|
|
1824 (setq byte-compile-warnings
|
|
1825 (delq (caar spec) byte-compile-warnings))
|
|
1826 (setq byte-compile-warnings
|
|
1827 (adjoin (caar spec) byte-compile-warnings)))))))
|
|
1828 nil)
|
|
1829
|
|
1830 ;;; Process any proclamations made before cl-macs was loaded.
|
|
1831 (defvar cl-proclaims-deferred)
|
|
1832 (let ((p (reverse cl-proclaims-deferred)))
|
|
1833 (while p (cl-do-proclaim (cl-pop p) t))
|
|
1834 (setq cl-proclaims-deferred nil))
|
|
1835
|
|
1836 ;;;###autoload
|
|
1837 (defmacro declare (&rest specs)
|
|
1838 (if (cl-compiling-file)
|
|
1839 (while specs
|
|
1840 (if (listp cl-declare-stack) (cl-push (car specs) cl-declare-stack))
|
|
1841 (cl-do-proclaim (cl-pop specs) nil)))
|
|
1842 nil)
|
|
1843
|
|
1844
|
|
1845
|
|
1846 ;;; Generalized variables.
|
|
1847
|
|
1848 ;;;###autoload
|
|
1849 (defmacro define-setf-method (func args &rest body)
|
|
1850 "(define-setf-method NAME ARGLIST BODY...): define a `setf' method.
|
|
1851 This method shows how to handle `setf's to places of the form (NAME ARGS...).
|
|
1852 The argument forms ARGS are bound according to ARGLIST, as if NAME were
|
|
1853 going to be expanded as a macro, then the BODY forms are executed and must
|
|
1854 return a list of five elements: a temporary-variables list, a value-forms
|
|
1855 list, a store-variables list (of length one), a store-form, and an access-
|
|
1856 form. See `defsetf' for a simpler way to define most setf-methods."
|
|
1857 (append '(eval-when (compile load eval))
|
|
1858 (if (stringp (car body))
|
|
1859 (list (list 'put (list 'quote func) '(quote setf-documentation)
|
|
1860 (cl-pop body))))
|
|
1861 (list (cl-transform-function-property
|
|
1862 func 'setf-method (cons args body)))))
|
|
1863
|
|
1864 ;;;###autoload
|
|
1865 (defmacro defsetf (func arg1 &rest args)
|
|
1866 "(defsetf NAME FUNC): define a `setf' method.
|
|
1867 This macro is an easy-to-use substitute for `define-setf-method' that works
|
|
1868 well for simple place forms. In the simple `defsetf' form, `setf's of
|
|
1869 the form (setf (NAME ARGS...) VAL) are transformed to function or macro
|
|
1870 calls of the form (FUNC ARGS... VAL). Example: (defsetf aref aset).
|
|
1871 Alternate form: (defsetf NAME ARGLIST (STORE) BODY...).
|
|
1872 Here, the above `setf' call is expanded by binding the argument forms ARGS
|
|
1873 according to ARGLIST, binding the value form VAL to STORE, then executing
|
|
1874 BODY, which must return a Lisp form that does the necessary `setf' operation.
|
|
1875 Actually, ARGLIST and STORE may be bound to temporary variables which are
|
|
1876 introduced automatically to preserve proper execution order of the arguments.
|
|
1877 Example: (defsetf nth (n x) (v) (list 'setcar (list 'nthcdr n x) v))."
|
|
1878 (if (listp arg1)
|
|
1879 (let* ((largs nil) (largsr nil)
|
|
1880 (temps nil) (tempsr nil)
|
|
1881 (restarg nil) (rest-temps nil)
|
|
1882 (store-var (car (prog1 (car args) (setq args (cdr args)))))
|
|
1883 (store-temp (intern (format "--%s--temp--" store-var)))
|
|
1884 (lets1 nil) (lets2 nil)
|
|
1885 (docstr nil) (p arg1))
|
|
1886 (if (stringp (car args))
|
|
1887 (setq docstr (prog1 (car args) (setq args (cdr args)))))
|
|
1888 (while (and p (not (eq (car p) '&aux)))
|
|
1889 (if (eq (car p) '&rest)
|
|
1890 (setq p (cdr p) restarg (car p))
|
|
1891 (or (memq (car p) '(&optional &key &allow-other-keys))
|
|
1892 (setq largs (cons (if (consp (car p)) (car (car p)) (car p))
|
|
1893 largs)
|
|
1894 temps (cons (intern (format "--%s--temp--" (car largs)))
|
|
1895 temps))))
|
|
1896 (setq p (cdr p)))
|
|
1897 (setq largs (nreverse largs) temps (nreverse temps))
|
|
1898 (if restarg
|
|
1899 (setq largsr (append largs (list restarg))
|
|
1900 rest-temps (intern (format "--%s--temp--" restarg))
|
|
1901 tempsr (append temps (list rest-temps)))
|
|
1902 (setq largsr largs tempsr temps))
|
|
1903 (let ((p1 largs) (p2 temps))
|
|
1904 (while p1
|
|
1905 (setq lets1 (cons (list (car p2)
|
|
1906 (list 'gensym (format "--%s--" (car p1))))
|
|
1907 lets1)
|
|
1908 lets2 (cons (list (car p1) (car p2)) lets2)
|
|
1909 p1 (cdr p1) p2 (cdr p2))))
|
|
1910 (if restarg (setq lets2 (cons (list restarg rest-temps) lets2)))
|
|
1911 (append (list 'define-setf-method func arg1)
|
|
1912 (and docstr (list docstr))
|
|
1913 (list
|
|
1914 (list 'let*
|
|
1915 (nreverse
|
|
1916 (cons (list store-temp
|
|
1917 (list 'gensym (format "--%s--" store-var)))
|
|
1918 (if restarg
|
|
1919 (append
|
|
1920 (list
|
|
1921 (list rest-temps
|
|
1922 (list 'mapcar '(quote gensym)
|
|
1923 restarg)))
|
|
1924 lets1)
|
|
1925 lets1)))
|
|
1926 (list 'list ; 'values
|
|
1927 (cons (if restarg 'list* 'list) tempsr)
|
|
1928 (cons (if restarg 'list* 'list) largsr)
|
|
1929 (list 'list store-temp)
|
|
1930 (cons 'let*
|
|
1931 (cons (nreverse
|
|
1932 (cons (list store-var store-temp)
|
|
1933 lets2))
|
|
1934 args))
|
|
1935 (cons (if restarg 'list* 'list)
|
|
1936 (cons (list 'quote func) tempsr)))))))
|
|
1937 (list 'defsetf func '(&rest args) '(store)
|
|
1938 (let ((call (list 'cons (list 'quote arg1)
|
|
1939 '(append args (list store)))))
|
|
1940 (if (car args)
|
|
1941 (list 'list '(quote progn) call 'store)
|
|
1942 call)))))
|
|
1943
|
|
1944 ;;; Some standard place types from Common Lisp.
|
|
1945 (eval-when-compile (defvar ignored-arg)) ; Warning suppression
|
|
1946 (defsetf aref aset)
|
|
1947 (defsetf car setcar)
|
|
1948 (defsetf cdr setcdr)
|
|
1949 (defsetf elt (seq n) (store)
|
|
1950 (list 'if (list 'listp seq) (list 'setcar (list 'nthcdr n seq) store)
|
|
1951 (list 'aset seq n store)))
|
|
1952 (defsetf get (x y &optional ignored-arg) (store) (list 'put x y store))
|
|
1953 (defsetf get* (x y &optional ignored-arg) (store) (list 'put x y store))
|
|
1954 (defsetf gethash (x h &optional ignored-arg) (store) (list 'cl-puthash x store h))
|
|
1955 (defsetf nth (n x) (store) (list 'setcar (list 'nthcdr n x) store))
|
|
1956 (defsetf subseq (seq start &optional end) (new)
|
|
1957 (list 'progn (list 'replace seq new ':start1 start ':end1 end) new))
|
|
1958 (defsetf symbol-function fset)
|
|
1959 (defsetf symbol-plist setplist)
|
|
1960 (defsetf symbol-value set)
|
|
1961
|
|
1962 ;;; Various car/cdr aliases. Note that `cadr' is handled specially.
|
|
1963 (defsetf first setcar)
|
|
1964 (defsetf second (x) (store) (list 'setcar (list 'cdr x) store))
|
|
1965 (defsetf third (x) (store) (list 'setcar (list 'cddr x) store))
|
|
1966 (defsetf fourth (x) (store) (list 'setcar (list 'cdddr x) store))
|
|
1967 (defsetf fifth (x) (store) (list 'setcar (list 'nthcdr 4 x) store))
|
|
1968 (defsetf sixth (x) (store) (list 'setcar (list 'nthcdr 5 x) store))
|
|
1969 (defsetf seventh (x) (store) (list 'setcar (list 'nthcdr 6 x) store))
|
|
1970 (defsetf eighth (x) (store) (list 'setcar (list 'nthcdr 7 x) store))
|
|
1971 (defsetf ninth (x) (store) (list 'setcar (list 'nthcdr 8 x) store))
|
|
1972 (defsetf tenth (x) (store) (list 'setcar (list 'nthcdr 9 x) store))
|
|
1973 (defsetf rest setcdr)
|
|
1974
|
|
1975 ;;; Some more Emacs-related place types.
|
|
1976 (defsetf buffer-file-name set-visited-file-name t)
|
|
1977 (defsetf buffer-modified-p set-buffer-modified-p t)
|
|
1978 (defsetf buffer-name rename-buffer t)
|
|
1979 (defsetf buffer-string () (store)
|
|
1980 (list 'progn '(erase-buffer) (list 'insert store)))
|
|
1981 (defsetf buffer-substring cl-set-buffer-substring)
|
|
1982 (defsetf current-buffer set-buffer)
|
|
1983 (defsetf current-case-table set-case-table)
|
|
1984 (defsetf current-column move-to-column t)
|
|
1985 (defsetf current-global-map use-global-map t)
|
|
1986 (defsetf current-input-mode () (store)
|
|
1987 (list 'progn (list 'apply 'set-input-mode store) store))
|
|
1988 (defsetf current-local-map use-local-map t)
|
|
1989 (defsetf current-window-configuration set-window-configuration t)
|
|
1990 (defsetf default-file-modes set-default-file-modes t)
|
|
1991 (defsetf default-value set-default)
|
|
1992 (defsetf documentation-property put)
|
|
1993 (defsetf extent-face set-extent-face)
|
|
1994 (defsetf extent-priority set-extent-priority)
|
|
1995 (defsetf extent-property (x y &optional ignored-arg) (arg)
|
|
1996 (list 'set-extent-property x y arg))
|
|
1997 (defsetf extent-start-position (ext) (store)
|
|
1998 `(progn (set-extent-endpoints ,ext ,store (extent-end-position ,ext))
|
|
1999 ,store))
|
|
2000 (defsetf extent-end-position (ext) (store)
|
|
2001 `(progn (set-extent-endpoints ,ext (extent-start-position ,ext) ,store)
|
|
2002 ,store))
|
|
2003 (defsetf face-background (f &optional s) (x) (list 'set-face-background f x s))
|
|
2004 (defsetf face-background-pixmap (f &optional s) (x)
|
|
2005 (list 'set-face-background-pixmap f x s))
|
|
2006 (defsetf face-font (f &optional s) (x) (list 'set-face-font f x s))
|
|
2007 (defsetf face-foreground (f &optional s) (x) (list 'set-face-foreground f x s))
|
|
2008 (defsetf face-underline-p (f &optional s) (x)
|
|
2009 (list 'set-face-underline-p f x s))
|
|
2010 (defsetf file-modes set-file-modes t)
|
|
2011 (defsetf frame-parameters modify-frame-parameters t)
|
|
2012 (defsetf frame-visible-p cl-set-frame-visible-p)
|
|
2013 (defsetf frame-properties (&optional f) (p)
|
|
2014 `(progn (set-frame-properties ,f ,p) ,p))
|
|
2015 (defsetf frame-property (f p &optional ignored-arg) (v)
|
|
2016 `(progn (set-frame-property ,f ,v) ,p))
|
|
2017 (defsetf frame-width (&optional f) (v)
|
|
2018 `(progn (set-frame-width ,f ,v) ,v))
|
|
2019 (defsetf frame-height (&optional f) (v)
|
|
2020 `(progn (set-frame-height ,f ,v) ,v))
|
|
2021 (defsetf current-frame-configuration set-frame-configuration)
|
|
2022
|
|
2023 ;; XEmacs: new stuff
|
|
2024 ;; Consoles
|
|
2025 (defsetf selected-console select-console t)
|
|
2026 (defsetf selected-device select-device t)
|
|
2027 (defsetf device-baud-rate (&optional d) (v)
|
|
2028 `(set-device-baud-rate ,d ,v))
|
|
2029 ;; This setf method is a bad idea, because set-specifier *adds* a
|
|
2030 ;; specification, rather than just setting it. The net effect is that
|
|
2031 ;; it makes specifier-instance return VAL, but other things don't work
|
|
2032 ;; as expected -- letf, to name one.
|
|
2033 ;(defsetf specifier-instance (spec &optional dom def nof) (val)
|
|
2034 ; `(set-specifier ,spec ,val ,dom))
|
|
2035
|
|
2036 ;; Annotations
|
|
2037 (defsetf annotation-glyph set-annotation-glyph)
|
|
2038 (defsetf annotation-down-glyph set-annotation-down-glyph)
|
|
2039 (defsetf annotation-face set-annotation-face)
|
|
2040 (defsetf annotation-layout set-annotation-layout)
|
|
2041 (defsetf annotation-data set-annotation-data)
|
|
2042 (defsetf annotation-action set-annotation-action)
|
|
2043 (defsetf annotation-menu set-annotation-menu)
|
|
2044 ;; Widget
|
|
2045 (defsetf widget-get widget-put t)
|
|
2046 (defsetf widget-value widget-value-set t)
|
|
2047
|
|
2048 ;; Misc
|
|
2049 (defsetf recent-keys-ring-size set-recent-keys-ring-size)
|
|
2050 (defsetf symbol-value-in-buffer (s b &optional ignored-arg) (store)
|
|
2051 `(with-current-buffer ,b (set ,s ,store)))
|
|
2052 (defsetf symbol-value-in-console (s c &optional ignored-arg) (store)
|
|
2053 `(letf (((selected-console) ,c))
|
|
2054 (set ,s ,store)))
|
|
2055
|
|
2056 (defsetf buffer-dedicated-frame (&optional b) (v)
|
|
2057 `(set-buffer-dedicated-frame ,b ,v))
|
|
2058 (defsetf console-type-image-conversion-list
|
|
2059 set-console-type-image-conversion-list)
|
|
2060 (defsetf default-toolbar-position set-default-toolbar-position)
|
|
2061 (defsetf device-class (&optional d) (v)
|
|
2062 `(set-device-class ,d ,v))
|
|
2063 (defsetf extent-begin-glyph set-extent-begin-glyph)
|
|
2064 (defsetf extent-begin-glyph-layout set-extent-begin-glyph-layout)
|
|
2065 (defsetf extent-end-glyph set-extent-end-glyph)
|
|
2066 (defsetf extent-end-glyph-layout set-extent-end-glyph-layout)
|
|
2067 (defsetf extent-keymap set-extent-keymap)
|
|
2068 (defsetf extent-parent set-extent-parent)
|
|
2069 (defsetf extent-properties set-extent-properties)
|
|
2070 ;; Avoid adding various face and glyph functions.
|
|
2071 (defsetf frame-selected-window (&optional f) (v)
|
|
2072 `(set-frame-selected-window ,f ,v))
|
|
2073 (defsetf glyph-image (glyph &optional domain) (i)
|
|
2074 (list 'set-glyph-image glyph i domain))
|
|
2075 (defsetf itimer-function set-itimer-function)
|
|
2076 (defsetf itimer-function-arguments set-itimer-function-arguments)
|
|
2077 (defsetf itimer-is-idle set-itimer-is-idle)
|
|
2078 (defsetf itimer-recorded-run-time set-itimer-recorded-run-time)
|
|
2079 (defsetf itimer-restart set-itimer-restart)
|
|
2080 (defsetf itimer-uses-arguments set-itimer-uses-arguments)
|
|
2081 (defsetf itimer-value set-itimer-value)
|
|
2082 (defsetf keymap-parents set-keymap-parents)
|
|
2083 (defsetf marker-insertion-type set-marker-insertion-type)
|
|
2084 (defsetf mouse-pixel-position (&optional d) (v)
|
|
2085 `(progn
|
|
2086 (set-mouse-pixel-position ,d ,(car v) ,(car (cdr v)) ,(cdr (cdr v)))
|
|
2087 ,v))
|
|
2088 (defsetf trunc-stack-length set-trunc-stack-length)
|
|
2089 (defsetf trunc-stack-stack set-trunc-stack-stack)
|
|
2090 (defsetf undoable-stack-max set-undoable-stack-max)
|
|
2091 (defsetf weak-list-list set-weak-list-list)
|
|
2092
|
|
2093
|
|
2094 (defsetf getenv setenv t)
|
|
2095 (defsetf get-register set-register)
|
|
2096 (defsetf global-key-binding global-set-key)
|
|
2097 (defsetf keymap-parent set-keymap-parent)
|
|
2098 (defsetf keymap-name set-keymap-name)
|
|
2099 (defsetf keymap-prompt set-keymap-prompt)
|
|
2100 (defsetf keymap-default-binding set-keymap-default-binding)
|
|
2101 (defsetf local-key-binding local-set-key)
|
|
2102 (defsetf mark set-mark t)
|
|
2103 (defsetf mark-marker set-mark t)
|
|
2104 (defsetf marker-position set-marker t)
|
|
2105 (defsetf match-data store-match-data t)
|
|
2106 (defsetf mouse-position (scr) (store)
|
|
2107 (list 'set-mouse-position scr (list 'car store) (list 'cadr store)
|
|
2108 (list 'cddr store)))
|
|
2109 (defsetf overlay-get overlay-put)
|
|
2110 (defsetf overlay-start (ov) (store)
|
|
2111 (list 'progn (list 'move-overlay ov store (list 'overlay-end ov)) store))
|
|
2112 (defsetf overlay-end (ov) (store)
|
|
2113 (list 'progn (list 'move-overlay ov (list 'overlay-start ov) store) store))
|
|
2114 (defsetf point goto-char)
|
|
2115 (defsetf point-marker goto-char t)
|
|
2116 (defsetf point-max () (store)
|
|
2117 (list 'progn (list 'narrow-to-region '(point-min) store) store))
|
|
2118 (defsetf point-min () (store)
|
|
2119 (list 'progn (list 'narrow-to-region store '(point-max)) store))
|
|
2120 (defsetf process-buffer set-process-buffer)
|
|
2121 (defsetf process-filter set-process-filter)
|
|
2122 (defsetf process-sentinel set-process-sentinel)
|
|
2123 (defsetf read-mouse-position (scr) (store)
|
|
2124 (list 'set-mouse-position scr (list 'car store) (list 'cdr store)))
|
|
2125 (defsetf selected-window select-window)
|
|
2126 (defsetf selected-frame select-frame)
|
|
2127 (defsetf standard-case-table set-standard-case-table)
|
|
2128 (defsetf syntax-table set-syntax-table)
|
|
2129 (defsetf visited-file-modtime set-visited-file-modtime t)
|
|
2130 (defsetf window-buffer set-window-buffer t)
|
|
2131 (defsetf window-display-table set-window-display-table t)
|
|
2132 (defsetf window-dedicated-p set-window-dedicated-p t)
|
|
2133 (defsetf window-height (&optional window) (store)
|
|
2134 `(progn (enlarge-window (- ,store (window-height)) nil ,window) ,store))
|
|
2135 (defsetf window-hscroll set-window-hscroll)
|
|
2136 (defsetf window-point set-window-point)
|
|
2137 (defsetf window-start set-window-start)
|
|
2138 (defsetf window-width (&optional window) (store)
|
|
2139 `(progn (enlarge-window (- ,store (window-width)) t ,window) ,store))
|
|
2140 (defsetf x-get-cutbuffer x-store-cutbuffer t)
|
|
2141 (defsetf x-get-cut-buffer x-store-cut-buffer t) ; groan.
|
|
2142 (defsetf x-get-secondary-selection x-own-secondary-selection t)
|
|
2143 (defsetf x-get-selection x-own-selection t)
|
442
|
2144 (defsetf get-selection own-selection t)
|
428
|
2145
|
|
2146 ;;; More complex setf-methods.
|
|
2147 ;;; These should take &environment arguments, but since full arglists aren't
|
|
2148 ;;; available while compiling cl-macs, we fake it by referring to the global
|
|
2149 ;;; variable cl-macro-environment directly.
|
|
2150
|
|
2151 (define-setf-method apply (func arg1 &rest rest)
|
|
2152 (or (and (memq (car-safe func) '(quote function function*))
|
|
2153 (symbolp (car-safe (cdr-safe func))))
|
|
2154 (error "First arg to apply in setf is not (function SYM): %s" func))
|
|
2155 (let* ((form (cons (nth 1 func) (cons arg1 rest)))
|
|
2156 (method (get-setf-method form cl-macro-environment)))
|
|
2157 (list (car method) (nth 1 method) (nth 2 method)
|
|
2158 (cl-setf-make-apply (nth 3 method) (cadr func) (car method))
|
|
2159 (cl-setf-make-apply (nth 4 method) (cadr func) (car method)))))
|
|
2160
|
|
2161 (defun cl-setf-make-apply (form func temps)
|
|
2162 (if (eq (car form) 'progn)
|
|
2163 (list* 'progn (cl-setf-make-apply (cadr form) func temps) (cddr form))
|
|
2164 (or (equal (last form) (last temps))
|
|
2165 (error "%s is not suitable for use with setf-of-apply" func))
|
|
2166 (list* 'apply (list 'quote (car form)) (cdr form))))
|
|
2167
|
|
2168 (define-setf-method nthcdr (n place)
|
|
2169 (let ((method (get-setf-method place cl-macro-environment))
|
|
2170 (n-temp (gensym "--nthcdr-n--"))
|
|
2171 (store-temp (gensym "--nthcdr-store--")))
|
|
2172 (list (cons n-temp (car method))
|
|
2173 (cons n (nth 1 method))
|
|
2174 (list store-temp)
|
|
2175 (list 'let (list (list (car (nth 2 method))
|
|
2176 (list 'cl-set-nthcdr n-temp (nth 4 method)
|
|
2177 store-temp)))
|
|
2178 (nth 3 method) store-temp)
|
|
2179 (list 'nthcdr n-temp (nth 4 method)))))
|
|
2180
|
|
2181 (define-setf-method getf (place tag &optional def)
|
|
2182 (let ((method (get-setf-method place cl-macro-environment))
|
|
2183 (tag-temp (gensym "--getf-tag--"))
|
|
2184 (def-temp (gensym "--getf-def--"))
|
|
2185 (store-temp (gensym "--getf-store--")))
|
|
2186 (list (append (car method) (list tag-temp def-temp))
|
|
2187 (append (nth 1 method) (list tag def))
|
|
2188 (list store-temp)
|
|
2189 (list 'let (list (list (car (nth 2 method))
|
|
2190 (list 'cl-set-getf (nth 4 method)
|
|
2191 tag-temp store-temp)))
|
|
2192 (nth 3 method) store-temp)
|
|
2193 (list 'getf (nth 4 method) tag-temp def-temp))))
|
|
2194
|
|
2195 (define-setf-method substring (place from &optional to)
|
|
2196 (let ((method (get-setf-method place cl-macro-environment))
|
|
2197 (from-temp (gensym "--substring-from--"))
|
|
2198 (to-temp (gensym "--substring-to--"))
|
|
2199 (store-temp (gensym "--substring-store--")))
|
|
2200 (list (append (car method) (list from-temp to-temp))
|
|
2201 (append (nth 1 method) (list from to))
|
|
2202 (list store-temp)
|
|
2203 (list 'let (list (list (car (nth 2 method))
|
|
2204 (list 'cl-set-substring (nth 4 method)
|
|
2205 from-temp to-temp store-temp)))
|
|
2206 (nth 3 method) store-temp)
|
|
2207 (list 'substring (nth 4 method) from-temp to-temp))))
|
|
2208
|
|
2209 (define-setf-method values (&rest args)
|
|
2210 (let ((methods (mapcar #'(lambda (x)
|
|
2211 (get-setf-method x cl-macro-environment))
|
|
2212 args))
|
|
2213 (store-temp (gensym "--values-store--")))
|
|
2214 (list (apply 'append (mapcar 'first methods))
|
|
2215 (apply 'append (mapcar 'second methods))
|
|
2216 (list store-temp)
|
|
2217 (cons 'list
|
|
2218 (mapcar #'(lambda (m)
|
|
2219 (cl-setf-do-store (cons (car (third m)) (fourth m))
|
|
2220 (list 'pop store-temp)))
|
|
2221 methods))
|
|
2222 (cons 'list (mapcar 'fifth methods)))))
|
|
2223
|
|
2224 ;;; Getting and optimizing setf-methods.
|
|
2225 ;;;###autoload
|
|
2226 (defun get-setf-method (place &optional env)
|
|
2227 "Return a list of five values describing the setf-method for PLACE.
|
|
2228 PLACE may be any Lisp form which can appear as the PLACE argument to
|
|
2229 a macro like `setf' or `incf'."
|
|
2230 (if (symbolp place)
|
|
2231 (let ((temp (gensym "--setf--")))
|
|
2232 (list nil nil (list temp) (list 'setq place temp) place))
|
|
2233 (or (and (symbolp (car place))
|
|
2234 (let* ((func (car place))
|
|
2235 (name (symbol-name func))
|
|
2236 (method (get func 'setf-method))
|
|
2237 (case-fold-search nil))
|
|
2238 (or (and method
|
|
2239 (let ((cl-macro-environment env))
|
|
2240 (setq method (apply method (cdr place))))
|
|
2241 (if (and (consp method) (= (length method) 5))
|
|
2242 method
|
|
2243 (error "Setf-method for %s returns malformed method"
|
|
2244 func)))
|
|
2245 (and (save-match-data
|
|
2246 (string-match "\\`c[ad][ad][ad]?[ad]?r\\'" name))
|
|
2247 (get-setf-method (compiler-macroexpand place)))
|
|
2248 (and (eq func 'edebug-after)
|
|
2249 (get-setf-method (nth (1- (length place)) place)
|
|
2250 env)))))
|
|
2251 (if (eq place (setq place (macroexpand place env)))
|
|
2252 (if (and (symbolp (car place)) (fboundp (car place))
|
|
2253 (symbolp (symbol-function (car place))))
|
|
2254 (get-setf-method (cons (symbol-function (car place))
|
|
2255 (cdr place)) env)
|
|
2256 (error "No setf-method known for %s" (car place)))
|
|
2257 (get-setf-method place env)))))
|
|
2258
|
|
2259 (defun cl-setf-do-modify (place opt-expr)
|
|
2260 (let* ((method (get-setf-method place cl-macro-environment))
|
|
2261 (temps (car method)) (values (nth 1 method))
|
|
2262 (lets nil) (subs nil)
|
|
2263 (optimize (and (not (eq opt-expr 'no-opt))
|
|
2264 (or (and (not (eq opt-expr 'unsafe))
|
|
2265 (cl-safe-expr-p opt-expr))
|
|
2266 (cl-setf-simple-store-p (car (nth 2 method))
|
|
2267 (nth 3 method)))))
|
|
2268 (simple (and optimize (consp place) (cl-simple-exprs-p (cdr place)))))
|
|
2269 (while values
|
|
2270 (if (or simple (cl-const-expr-p (car values)))
|
|
2271 (cl-push (cons (cl-pop temps) (cl-pop values)) subs)
|
|
2272 (cl-push (list (cl-pop temps) (cl-pop values)) lets)))
|
|
2273 (list (nreverse lets)
|
|
2274 (cons (car (nth 2 method)) (sublis subs (nth 3 method)))
|
|
2275 (sublis subs (nth 4 method)))))
|
|
2276
|
|
2277 (defun cl-setf-do-store (spec val)
|
|
2278 (let ((sym (car spec))
|
|
2279 (form (cdr spec)))
|
|
2280 (if (or (cl-const-expr-p val)
|
|
2281 (and (cl-simple-expr-p val) (eq (cl-expr-contains form sym) 1))
|
|
2282 (cl-setf-simple-store-p sym form))
|
|
2283 (subst val sym form)
|
|
2284 (list 'let (list (list sym val)) form))))
|
|
2285
|
|
2286 (defun cl-setf-simple-store-p (sym form)
|
|
2287 (and (consp form) (eq (cl-expr-contains form sym) 1)
|
|
2288 (eq (nth (1- (length form)) form) sym)
|
|
2289 (symbolp (car form)) (fboundp (car form))
|
|
2290 (not (eq (car-safe (symbol-function (car form))) 'macro))))
|
|
2291
|
|
2292 ;;; The standard modify macros.
|
|
2293 ;;;###autoload
|
|
2294 (defmacro setf (&rest args)
|
|
2295 "(setf PLACE VAL PLACE VAL ...): set each PLACE to the value of its VAL.
|
|
2296 This is a generalized version of `setq'; the PLACEs may be symbolic
|
|
2297 references such as (car x) or (aref x i), as well as plain symbols.
|
|
2298 For example, (setf (cadar x) y) is equivalent to (setcar (cdar x) y).
|
|
2299 The return value is the last VAL in the list."
|
|
2300 (if (cdr (cdr args))
|
|
2301 (let ((sets nil))
|
|
2302 (while args (cl-push (list 'setf (cl-pop args) (cl-pop args)) sets))
|
|
2303 (cons 'progn (nreverse sets)))
|
|
2304 (if (symbolp (car args))
|
|
2305 (and args (cons 'setq args))
|
|
2306 (let* ((method (cl-setf-do-modify (car args) (nth 1 args)))
|
|
2307 (store (cl-setf-do-store (nth 1 method) (nth 1 args))))
|
|
2308 (if (car method) (list 'let* (car method) store) store)))))
|
|
2309
|
|
2310 ;;;###autoload
|
|
2311 (defmacro psetf (&rest args)
|
|
2312 "(psetf PLACE VAL PLACE VAL ...): set PLACEs to the values VALs in parallel.
|
|
2313 This is like `setf', except that all VAL forms are evaluated (in order)
|
|
2314 before assigning any PLACEs to the corresponding values."
|
|
2315 (let ((p args) (simple t) (vars nil))
|
|
2316 (while p
|
|
2317 (if (or (not (symbolp (car p))) (cl-expr-depends-p (nth 1 p) vars))
|
|
2318 (setq simple nil))
|
|
2319 (if (memq (car p) vars)
|
|
2320 (error "Destination duplicated in psetf: %s" (car p)))
|
|
2321 (cl-push (cl-pop p) vars)
|
|
2322 (or p (error "Odd number of arguments to psetf"))
|
|
2323 (cl-pop p))
|
|
2324 (if simple
|
|
2325 (list 'progn (cons 'setf args) nil)
|
|
2326 (setq args (reverse args))
|
|
2327 (let ((expr (list 'setf (cadr args) (car args))))
|
|
2328 (while (setq args (cddr args))
|
|
2329 (setq expr (list 'setf (cadr args) (list 'prog1 (car args) expr))))
|
|
2330 (list 'progn expr nil)))))
|
|
2331
|
|
2332 ;;;###autoload
|
|
2333 (defun cl-do-pop (place)
|
|
2334 (if (cl-simple-expr-p place)
|
|
2335 (list 'prog1 (list 'car place) (list 'setf place (list 'cdr place)))
|
|
2336 (let* ((method (cl-setf-do-modify place t))
|
|
2337 (temp (gensym "--pop--")))
|
|
2338 (list 'let*
|
|
2339 (append (car method)
|
|
2340 (list (list temp (nth 2 method))))
|
|
2341 (list 'prog1
|
|
2342 (list 'car temp)
|
|
2343 (cl-setf-do-store (nth 1 method) (list 'cdr temp)))))))
|
|
2344
|
|
2345 ;;;###autoload
|
|
2346 (defmacro remf (place tag)
|
|
2347 "(remf PLACE TAG): remove TAG from property list PLACE.
|
|
2348 PLACE may be a symbol, or any generalized variable allowed by `setf'.
|
|
2349 The form returns true if TAG was found and removed, nil otherwise."
|
|
2350 (let* ((method (cl-setf-do-modify place t))
|
|
2351 (tag-temp (and (not (cl-const-expr-p tag)) (gensym "--remf-tag--")))
|
|
2352 (val-temp (and (not (cl-simple-expr-p place))
|
|
2353 (gensym "--remf-place--")))
|
|
2354 (ttag (or tag-temp tag))
|
|
2355 (tval (or val-temp (nth 2 method))))
|
|
2356 (list 'let*
|
|
2357 (append (car method)
|
|
2358 (and val-temp (list (list val-temp (nth 2 method))))
|
|
2359 (and tag-temp (list (list tag-temp tag))))
|
|
2360 (list 'if (list 'eq ttag (list 'car tval))
|
|
2361 (list 'progn
|
|
2362 (cl-setf-do-store (nth 1 method) (list 'cddr tval))
|
|
2363 t)
|
|
2364 (list 'cl-do-remf tval ttag)))))
|
|
2365
|
|
2366 ;;;###autoload
|
|
2367 (defmacro shiftf (place &rest args)
|
|
2368 "(shiftf PLACE PLACE... VAL): shift left among PLACEs.
|
|
2369 Example: (shiftf A B C) sets A to B, B to C, and returns the old A.
|
|
2370 Each PLACE may be a symbol, or any generalized variable allowed by `setf'."
|
|
2371 (if (not (memq nil (mapcar 'symbolp (butlast (cons place args)))))
|
|
2372 (list* 'prog1 place
|
|
2373 (let ((sets nil))
|
|
2374 (while args
|
|
2375 (cl-push (list 'setq place (car args)) sets)
|
|
2376 (setq place (cl-pop args)))
|
|
2377 (nreverse sets)))
|
|
2378 (let* ((places (reverse (cons place args)))
|
|
2379 (form (cl-pop places)))
|
|
2380 (while places
|
|
2381 (let ((method (cl-setf-do-modify (cl-pop places) 'unsafe)))
|
|
2382 (setq form (list 'let* (car method)
|
|
2383 (list 'prog1 (nth 2 method)
|
|
2384 (cl-setf-do-store (nth 1 method) form))))))
|
|
2385 form)))
|
|
2386
|
|
2387 ;;;###autoload
|
|
2388 (defmacro rotatef (&rest args)
|
|
2389 "(rotatef PLACE...): rotate left among PLACEs.
|
|
2390 Example: (rotatef A B C) sets A to B, B to C, and C to A. It returns nil.
|
|
2391 Each PLACE may be a symbol, or any generalized variable allowed by `setf'."
|
|
2392 (if (not (memq nil (mapcar 'symbolp args)))
|
|
2393 (and (cdr args)
|
|
2394 (let ((sets nil)
|
|
2395 (first (car args)))
|
|
2396 (while (cdr args)
|
|
2397 (setq sets (nconc sets (list (cl-pop args) (car args)))))
|
|
2398 (nconc (list 'psetf) sets (list (car args) first))))
|
|
2399 (let* ((places (reverse args))
|
|
2400 (temp (gensym "--rotatef--"))
|
|
2401 (form temp))
|
|
2402 (while (cdr places)
|
|
2403 (let ((method (cl-setf-do-modify (cl-pop places) 'unsafe)))
|
|
2404 (setq form (list 'let* (car method)
|
|
2405 (list 'prog1 (nth 2 method)
|
|
2406 (cl-setf-do-store (nth 1 method) form))))))
|
|
2407 (let ((method (cl-setf-do-modify (car places) 'unsafe)))
|
|
2408 (list 'let* (append (car method) (list (list temp (nth 2 method))))
|
|
2409 (cl-setf-do-store (nth 1 method) form) nil)))))
|
|
2410
|
|
2411 ;;;###autoload
|
|
2412 (defmacro letf (bindings &rest body)
|
|
2413 "(letf ((PLACE VALUE) ...) BODY...): temporarily bind to PLACEs.
|
|
2414 This is the analogue of `let', but with generalized variables (in the
|
|
2415 sense of `setf') for the PLACEs. Each PLACE is set to the corresponding
|
|
2416 VALUE, then the BODY forms are executed. On exit, either normally or
|
|
2417 because of a `throw' or error, the PLACEs are set back to their original
|
|
2418 values. Note that this macro is *not* available in Common Lisp.
|
|
2419 As a special case, if `(PLACE)' is used instead of `(PLACE VALUE)',
|
|
2420 the PLACE is not modified before executing BODY."
|
|
2421 (if (and (not (cdr bindings)) (cdar bindings) (symbolp (caar bindings)))
|
|
2422 (list* 'let bindings body)
|
|
2423 (let ((lets nil)
|
|
2424 (rev (reverse bindings)))
|
|
2425 (while rev
|
|
2426 (let* ((place (if (symbolp (caar rev))
|
|
2427 (list 'symbol-value (list 'quote (caar rev)))
|
|
2428 (caar rev)))
|
|
2429 (value (cadar rev))
|
|
2430 (method (cl-setf-do-modify place 'no-opt))
|
|
2431 (save (gensym "--letf-save--"))
|
|
2432 (bound (and (memq (car place) '(symbol-value symbol-function))
|
|
2433 (gensym "--letf-bound--")))
|
|
2434 (temp (and (not (cl-const-expr-p value)) (cdr bindings)
|
|
2435 (gensym "--letf-val--"))))
|
|
2436 (setq lets (nconc (car method)
|
|
2437 (if bound
|
|
2438 (list (list bound
|
|
2439 (list (if (eq (car place)
|
|
2440 'symbol-value)
|
|
2441 'boundp 'fboundp)
|
|
2442 (nth 1 (nth 2 method))))
|
|
2443 (list save (list 'and bound
|
|
2444 (nth 2 method))))
|
|
2445 (list (list save (nth 2 method))))
|
|
2446 (and temp (list (list temp value)))
|
|
2447 lets)
|
|
2448 body (list
|
|
2449 (list 'unwind-protect
|
|
2450 (cons 'progn
|
|
2451 (if (cdr (car rev))
|
|
2452 (cons (cl-setf-do-store (nth 1 method)
|
|
2453 (or temp value))
|
|
2454 body)
|
|
2455 body))
|
|
2456 (if bound
|
|
2457 (list 'if bound
|
|
2458 (cl-setf-do-store (nth 1 method) save)
|
|
2459 (list (if (eq (car place) 'symbol-value)
|
|
2460 'makunbound 'fmakunbound)
|
|
2461 (nth 1 (nth 2 method))))
|
|
2462 (cl-setf-do-store (nth 1 method) save))))
|
|
2463 rev (cdr rev))))
|
|
2464 (list* 'let* lets body))))
|
|
2465
|
|
2466 ;;;###autoload
|
|
2467 (defmacro letf* (bindings &rest body)
|
|
2468 "(letf* ((PLACE VALUE) ...) BODY...): temporarily bind to PLACEs.
|
|
2469 This is the analogue of `let*', but with generalized variables (in the
|
|
2470 sense of `setf') for the PLACEs. Each PLACE is set to the corresponding
|
|
2471 VALUE, then the BODY forms are executed. On exit, either normally or
|
|
2472 because of a `throw' or error, the PLACEs are set back to their original
|
|
2473 values. Note that this macro is *not* available in Common Lisp.
|
|
2474 As a special case, if `(PLACE)' is used instead of `(PLACE VALUE)',
|
|
2475 the PLACE is not modified before executing BODY."
|
|
2476 (if (null bindings)
|
|
2477 (cons 'progn body)
|
|
2478 (setq bindings (reverse bindings))
|
|
2479 (while bindings
|
|
2480 (setq body (list (list* 'letf (list (cl-pop bindings)) body))))
|
|
2481 (car body)))
|
|
2482
|
|
2483 ;;;###autoload
|
|
2484 (defmacro callf (func place &rest args)
|
|
2485 "(callf FUNC PLACE ARGS...): set PLACE to (FUNC PLACE ARGS...).
|
|
2486 FUNC should be an unquoted function name. PLACE may be a symbol,
|
|
2487 or any generalized variable allowed by `setf'."
|
|
2488 (let* ((method (cl-setf-do-modify place (cons 'list args)))
|
|
2489 (rargs (cons (nth 2 method) args)))
|
|
2490 (list 'let* (car method)
|
|
2491 (cl-setf-do-store (nth 1 method)
|
|
2492 (if (symbolp func) (cons func rargs)
|
|
2493 (list* 'funcall (list 'function func)
|
|
2494 rargs))))))
|
|
2495
|
|
2496 ;;;###autoload
|
|
2497 (defmacro callf2 (func arg1 place &rest args)
|
|
2498 "(callf2 FUNC ARG1 PLACE ARGS...): set PLACE to (FUNC ARG1 PLACE ARGS...).
|
|
2499 Like `callf', but PLACE is the second argument of FUNC, not the first."
|
|
2500 (if (and (cl-safe-expr-p arg1) (cl-simple-expr-p place) (symbolp func))
|
|
2501 (list 'setf place (list* func arg1 place args))
|
|
2502 (let* ((method (cl-setf-do-modify place (cons 'list args)))
|
|
2503 (temp (and (not (cl-const-expr-p arg1)) (gensym "--arg1--")))
|
|
2504 (rargs (list* (or temp arg1) (nth 2 method) args)))
|
|
2505 (list 'let* (append (and temp (list (list temp arg1))) (car method))
|
|
2506 (cl-setf-do-store (nth 1 method)
|
|
2507 (if (symbolp func) (cons func rargs)
|
|
2508 (list* 'funcall (list 'function func)
|
|
2509 rargs)))))))
|
|
2510
|
|
2511 ;;;###autoload
|
|
2512 (defmacro define-modify-macro (name arglist func &optional doc)
|
|
2513 "(define-modify-macro NAME ARGLIST FUNC): define a `setf'-like modify macro.
|
|
2514 If NAME is called, it combines its PLACE argument with the other arguments
|
|
2515 from ARGLIST using FUNC: (define-modify-macro incf (&optional (n 1)) +)"
|
|
2516 (if (memq '&key arglist) (error "&key not allowed in define-modify-macro"))
|
|
2517 (let ((place (gensym "--place--")))
|
|
2518 (list 'defmacro* name (cons place arglist) doc
|
|
2519 (list* (if (memq '&rest arglist) 'list* 'list)
|
|
2520 '(quote callf) (list 'quote func) place
|
|
2521 (cl-arglist-args arglist)))))
|
|
2522
|
|
2523
|
|
2524 ;;; Structures.
|
|
2525
|
|
2526 ;;;###autoload
|
|
2527 (defmacro defstruct (struct &rest descs)
|
|
2528 "(defstruct (NAME OPTIONS...) (SLOT SLOT-OPTS...)...): define a struct type.
|
|
2529 This macro defines a new Lisp data type called NAME, which contains data
|
|
2530 stored in SLOTs. This defines a `make-NAME' constructor, a `copy-NAME'
|
|
2531 copier, a `NAME-p' predicate, and setf-able `NAME-SLOT' accessors."
|
|
2532 (let* ((name (if (consp struct) (car struct) struct))
|
|
2533 (opts (cdr-safe struct))
|
|
2534 (slots nil)
|
|
2535 (defaults nil)
|
|
2536 (conc-name (concat (symbol-name name) "-"))
|
|
2537 (constructor (intern (format "make-%s" name)))
|
|
2538 (constrs nil)
|
|
2539 (copier (intern (format "copy-%s" name)))
|
|
2540 (predicate (intern (format "%s-p" name)))
|
|
2541 (print-func nil) (print-auto nil)
|
|
2542 (safety (if (cl-compiling-file) cl-optimize-safety 3))
|
|
2543 (include nil)
|
|
2544 (tag (intern (format "cl-struct-%s" name)))
|
|
2545 (tag-symbol (intern (format "cl-struct-%s-tags" name)))
|
|
2546 (include-descs nil)
|
|
2547 (side-eff nil)
|
|
2548 (type nil)
|
|
2549 (named nil)
|
|
2550 (forms nil)
|
|
2551 pred-form pred-check)
|
|
2552 (if (stringp (car descs))
|
|
2553 (cl-push (list 'put (list 'quote name) '(quote structure-documentation)
|
|
2554 (cl-pop descs)) forms))
|
|
2555 (setq descs (cons '(cl-tag-slot)
|
|
2556 (mapcar #'(lambda (x) (if (consp x) x (list x)))
|
|
2557 descs)))
|
|
2558 (while opts
|
|
2559 (let ((opt (if (consp (car opts)) (caar opts) (car opts)))
|
|
2560 (args (cdr-safe (cl-pop opts))))
|
|
2561 (cond ((eq opt ':conc-name)
|
|
2562 (if args
|
|
2563 (setq conc-name (if (car args)
|
|
2564 (symbol-name (car args)) ""))))
|
|
2565 ((eq opt ':constructor)
|
|
2566 (if (cdr args)
|
|
2567 (cl-push args constrs)
|
|
2568 (if args (setq constructor (car args)))))
|
|
2569 ((eq opt ':copier)
|
|
2570 (if args (setq copier (car args))))
|
|
2571 ((eq opt ':predicate)
|
|
2572 (if args (setq predicate (car args))))
|
|
2573 ((eq opt ':include)
|
|
2574 (setq include (car args)
|
|
2575 include-descs (mapcar #'(lambda (x)
|
|
2576 (if (consp x) x (list x)))
|
|
2577 (cdr args))))
|
|
2578 ((eq opt ':print-function)
|
|
2579 (setq print-func (car args)))
|
|
2580 ((eq opt ':type)
|
|
2581 (setq type (car args)))
|
|
2582 ((eq opt ':named)
|
|
2583 (setq named t))
|
|
2584 ((eq opt ':initial-offset)
|
|
2585 (setq descs (nconc (make-list (car args) '(cl-skip-slot))
|
|
2586 descs)))
|
|
2587 (t
|
|
2588 (error "Slot option %s unrecognized" opt)))))
|
|
2589 (if print-func
|
|
2590 (setq print-func (list 'progn
|
|
2591 (list 'funcall (list 'function print-func)
|
|
2592 'cl-x 'cl-s 'cl-n) t))
|
|
2593 (or type (and include (not (get include 'cl-struct-print)))
|
|
2594 (setq print-auto t
|
|
2595 print-func (and (or (not (or include type)) (null print-func))
|
|
2596 (list 'progn
|
|
2597 (list 'princ (format "#S(%s" name)
|
|
2598 'cl-s))))))
|
|
2599 (if include
|
|
2600 (let ((inc-type (get include 'cl-struct-type))
|
|
2601 (old-descs (get include 'cl-struct-slots)))
|
|
2602 (or inc-type (error "%s is not a struct name" include))
|
|
2603 (and type (not (eq (car inc-type) type))
|
|
2604 (error ":type disagrees with :include for %s" name))
|
|
2605 (while include-descs
|
|
2606 (setcar (memq (or (assq (caar include-descs) old-descs)
|
|
2607 (error "No slot %s in included struct %s"
|
|
2608 (caar include-descs) include))
|
|
2609 old-descs)
|
|
2610 (cl-pop include-descs)))
|
|
2611 (setq descs (append old-descs (delq (assq 'cl-tag-slot descs) descs))
|
|
2612 type (car inc-type)
|
|
2613 named (assq 'cl-tag-slot descs))
|
|
2614 (if (cadr inc-type) (setq tag name named t))
|
|
2615 (let ((incl include))
|
|
2616 (while incl
|
|
2617 (cl-push (list 'pushnew (list 'quote tag)
|
|
2618 (intern (format "cl-struct-%s-tags" incl)))
|
|
2619 forms)
|
|
2620 (setq incl (get incl 'cl-struct-include)))))
|
|
2621 (if type
|
|
2622 (progn
|
|
2623 (or (memq type '(vector list))
|
|
2624 (error "Illegal :type specifier: %s" type))
|
|
2625 (if named (setq tag name)))
|
|
2626 (setq type 'vector named 'true)))
|
|
2627 (or named (setq descs (delq (assq 'cl-tag-slot descs) descs)))
|
|
2628 (cl-push (list 'defvar tag-symbol) forms)
|
|
2629 (setq pred-form (and named
|
|
2630 (let ((pos (- (length descs)
|
|
2631 (length (memq (assq 'cl-tag-slot descs)
|
|
2632 descs)))))
|
|
2633 (if (eq type 'vector)
|
|
2634 (list 'and '(vectorp cl-x)
|
|
2635 (list '>= '(length cl-x) (length descs))
|
|
2636 (list 'memq (list 'aref 'cl-x pos)
|
|
2637 tag-symbol))
|
|
2638 (if (= pos 0)
|
|
2639 (list 'memq '(car-safe cl-x) tag-symbol)
|
|
2640 (list 'and '(consp cl-x)
|
|
2641 (list 'memq (list 'nth pos 'cl-x)
|
|
2642 tag-symbol))))))
|
|
2643 pred-check (and pred-form (> safety 0)
|
|
2644 (if (and (eq (caadr pred-form) 'vectorp)
|
|
2645 (= safety 1))
|
|
2646 (cons 'and (cdddr pred-form)) pred-form)))
|
|
2647 (let ((pos 0) (descp descs))
|
|
2648 (while descp
|
|
2649 (let* ((desc (cl-pop descp))
|
|
2650 (slot (car desc)))
|
|
2651 (if (memq slot '(cl-tag-slot cl-skip-slot))
|
|
2652 (progn
|
|
2653 (cl-push nil slots)
|
|
2654 (cl-push (and (eq slot 'cl-tag-slot) (list 'quote tag))
|
|
2655 defaults))
|
|
2656 (if (assq slot descp)
|
|
2657 (error "Duplicate slots named %s in %s" slot name))
|
|
2658 (let ((accessor (intern (format "%s%s" conc-name slot))))
|
|
2659 (cl-push slot slots)
|
|
2660 (cl-push (nth 1 desc) defaults)
|
|
2661 (cl-push (list*
|
|
2662 'defsubst* accessor '(cl-x)
|
|
2663 (append
|
|
2664 (and pred-check
|
|
2665 (list (list 'or pred-check
|
|
2666 (list 'error
|
|
2667 (format "%s accessing a non-%s"
|
|
2668 accessor name)
|
|
2669 'cl-x))))
|
|
2670 (list (if (eq type 'vector) (list 'aref 'cl-x pos)
|
|
2671 (if (= pos 0) '(car cl-x)
|
|
2672 (list 'nth pos 'cl-x)))))) forms)
|
|
2673 (cl-push (cons accessor t) side-eff)
|
|
2674 (cl-push (list 'define-setf-method accessor '(cl-x)
|
|
2675 (if (cadr (memq ':read-only (cddr desc)))
|
|
2676 (list 'error (format "%s is a read-only slot"
|
|
2677 accessor))
|
|
2678 (list 'cl-struct-setf-expander 'cl-x
|
|
2679 (list 'quote name) (list 'quote accessor)
|
|
2680 (and pred-check (list 'quote pred-check))
|
|
2681 pos)))
|
|
2682 forms)
|
|
2683 (if print-auto
|
|
2684 (nconc print-func
|
|
2685 (list (list 'princ (format " %s" slot) 'cl-s)
|
|
2686 (list 'prin1 (list accessor 'cl-x) 'cl-s)))))))
|
|
2687 (setq pos (1+ pos))))
|
|
2688 (setq slots (nreverse slots)
|
|
2689 defaults (nreverse defaults))
|
|
2690 (and predicate pred-form
|
|
2691 (progn (cl-push (list 'defsubst* predicate '(cl-x)
|
|
2692 (if (eq (car pred-form) 'and)
|
|
2693 (append pred-form '(t))
|
|
2694 (list 'and pred-form t))) forms)
|
|
2695 (cl-push (cons predicate 'error-free) side-eff)))
|
|
2696 (and copier
|
|
2697 (progn (cl-push (list 'defun copier '(x) '(copy-sequence x)) forms)
|
|
2698 (cl-push (cons copier t) side-eff)))
|
|
2699 (if constructor
|
|
2700 (cl-push (list constructor
|
|
2701 (cons '&key (delq nil (copy-sequence slots))))
|
|
2702 constrs))
|
|
2703 (while constrs
|
|
2704 (let* ((name (caar constrs))
|
|
2705 (args (cadr (cl-pop constrs)))
|
|
2706 (anames (cl-arglist-args args))
|
|
2707 (make (mapcar* #'(lambda (s d) (if (memq s anames) s d))
|
|
2708 slots defaults)))
|
|
2709 (cl-push (list 'defsubst* name
|
|
2710 (list* '&cl-defs (list 'quote (cons nil descs)) args)
|
|
2711 (cons type make)) forms)
|
|
2712 (if (cl-safe-expr-p (cons 'progn (mapcar 'second descs)))
|
|
2713 (cl-push (cons name t) side-eff))))
|
|
2714 (if print-auto (nconc print-func (list '(princ ")" cl-s) t)))
|
|
2715 (if print-func
|
|
2716 (cl-push (list 'push
|
|
2717 (list 'function
|
|
2718 (list 'lambda '(cl-x cl-s cl-n)
|
|
2719 (list 'and pred-form print-func)))
|
|
2720 'custom-print-functions) forms))
|
|
2721 (cl-push (list 'setq tag-symbol (list 'list (list 'quote tag))) forms)
|
|
2722 (cl-push (list* 'eval-when '(compile load eval)
|
|
2723 (list 'put (list 'quote name) '(quote cl-struct-slots)
|
|
2724 (list 'quote descs))
|
|
2725 (list 'put (list 'quote name) '(quote cl-struct-type)
|
|
2726 (list 'quote (list type (eq named t))))
|
|
2727 (list 'put (list 'quote name) '(quote cl-struct-include)
|
|
2728 (list 'quote include))
|
|
2729 (list 'put (list 'quote name) '(quote cl-struct-print)
|
|
2730 print-auto)
|
|
2731 (mapcar #'(lambda (x)
|
|
2732 (list 'put (list 'quote (car x))
|
|
2733 '(quote side-effect-free)
|
|
2734 (list 'quote (cdr x))))
|
|
2735 side-eff))
|
|
2736 forms)
|
|
2737 (cons 'progn (nreverse (cons (list 'quote name) forms)))))
|
|
2738
|
|
2739 ;;;###autoload
|
|
2740 (defun cl-struct-setf-expander (x name accessor pred-form pos)
|
|
2741 (let* ((temp (gensym "--x--")) (store (gensym "--store--")))
|
|
2742 (list (list temp) (list x) (list store)
|
|
2743 (append '(progn)
|
|
2744 (and pred-form
|
|
2745 (list (list 'or (subst temp 'cl-x pred-form)
|
|
2746 (list 'error
|
|
2747 (format
|
|
2748 "%s storing a non-%s" accessor name)
|
|
2749 temp))))
|
|
2750 (list (if (eq (car (get name 'cl-struct-type)) 'vector)
|
|
2751 (list 'aset temp pos store)
|
|
2752 (list 'setcar
|
|
2753 (if (<= pos 5)
|
|
2754 (let ((xx temp))
|
|
2755 (while (>= (setq pos (1- pos)) 0)
|
|
2756 (setq xx (list 'cdr xx)))
|
|
2757 xx)
|
|
2758 (list 'nthcdr pos temp))
|
|
2759 store))))
|
|
2760 (list accessor temp))))
|
|
2761
|
|
2762
|
|
2763 ;;; Types and assertions.
|
|
2764
|
|
2765 ;;;###autoload
|
|
2766 (defmacro deftype (name args &rest body)
|
|
2767 "(deftype NAME ARGLIST BODY...): define NAME as a new data type.
|
|
2768 The type name can then be used in `typecase', `check-type', etc."
|
|
2769 (list 'eval-when '(compile load eval)
|
|
2770 (cl-transform-function-property
|
|
2771 name 'cl-deftype-handler (cons (list* '&cl-defs ''('*) args) body))))
|
|
2772
|
|
2773 (defun cl-make-type-test (val type)
|
|
2774 (if (symbolp type)
|
|
2775 (cond ((get type 'cl-deftype-handler)
|
|
2776 (cl-make-type-test val (funcall (get type 'cl-deftype-handler))))
|
|
2777 ((memq type '(nil t)) type)
|
|
2778 ((eq type 'string-char) (list 'characterp val))
|
|
2779 ((eq type 'null) (list 'null val))
|
|
2780 ((eq type 'float) (list 'floatp-safe val))
|
|
2781 ((eq type 'real) (list 'numberp val))
|
|
2782 ((eq type 'fixnum) (list 'integerp val))
|
|
2783 (t
|
|
2784 (let* ((name (symbol-name type))
|
|
2785 (namep (intern (concat name "p"))))
|
|
2786 (if (fboundp namep) (list namep val)
|
|
2787 (list (intern (concat name "-p")) val)))))
|
|
2788 (cond ((get (car type) 'cl-deftype-handler)
|
|
2789 (cl-make-type-test val (apply (get (car type) 'cl-deftype-handler)
|
|
2790 (cdr type))))
|
|
2791 ((memq (car-safe type) '(integer float real number))
|
|
2792 (delq t (list 'and (cl-make-type-test val (car type))
|
|
2793 (if (memq (cadr type) '(* nil)) t
|
|
2794 (if (consp (cadr type)) (list '> val (caadr type))
|
|
2795 (list '>= val (cadr type))))
|
|
2796 (if (memq (caddr type) '(* nil)) t
|
|
2797 (if (consp (caddr type)) (list '< val (caaddr type))
|
|
2798 (list '<= val (caddr type)))))))
|
|
2799 ((memq (car-safe type) '(and or not))
|
|
2800 (cons (car type)
|
|
2801 (mapcar #'(lambda (x) (cl-make-type-test val x))
|
|
2802 (cdr type))))
|
|
2803 ((memq (car-safe type) '(member member*))
|
|
2804 (list 'and (list 'member* val (list 'quote (cdr type))) t))
|
|
2805 ((eq (car-safe type) 'satisfies) (list (cadr type) val))
|
|
2806 (t (error "Bad type spec: %s" type)))))
|
|
2807
|
|
2808 ;;;###autoload
|
444
|
2809 (defun typep (object type) ; See compiler macro below.
|
428
|
2810 "Check that OBJECT is of type TYPE.
|
|
2811 TYPE is a Common Lisp-style type specifier."
|
444
|
2812 (eval (cl-make-type-test 'object type)))
|
428
|
2813
|
|
2814 ;;;###autoload
|
446
|
2815 (defmacro check-type (place type &optional string)
|
|
2816 "Verify that PLACE is of type TYPE; signal a continuable error if not.
|
428
|
2817 STRING is an optional description of the desired type."
|
446
|
2818 (when (or (not (cl-compiling-file))
|
|
2819 (< cl-optimize-speed 3)
|
|
2820 (= cl-optimize-safety 3))
|
|
2821 (let* ((temp (if (cl-simple-expr-p place 3) place (gensym)))
|
|
2822 (test (cl-make-type-test temp type))
|
|
2823 (signal-error `(signal 'wrong-type-argument
|
|
2824 ,(list 'list (or string (list 'quote type))
|
|
2825 temp (list 'quote place))))
|
|
2826 (body
|
|
2827 (condition-case nil
|
|
2828 `(while (not ,test)
|
|
2829 ,(macroexpand `(setf ,place ,signal-error)))
|
|
2830 (error
|
|
2831 `(if ,test (progn ,signal-error nil))))))
|
|
2832 (if (eq temp place)
|
|
2833 body
|
|
2834 `(let ((,temp ,place)) ,body)))))
|
428
|
2835
|
|
2836 ;;;###autoload
|
|
2837 (defmacro assert (form &optional show-args string &rest args)
|
|
2838 "Verify that FORM returns non-nil; signal an error if not.
|
|
2839 Second arg SHOW-ARGS means to include arguments of FORM in message.
|
|
2840 Other args STRING and ARGS... are arguments to be passed to `error'.
|
|
2841 They are not evaluated unless the assertion fails. If STRING is
|
|
2842 omitted, a default message listing FORM itself is used."
|
|
2843 (and (or (not (cl-compiling-file))
|
|
2844 (< cl-optimize-speed 3) (= cl-optimize-safety 3))
|
|
2845 (let ((sargs (and show-args (delq nil (mapcar
|
|
2846 #'(lambda (x)
|
|
2847 (and (not (cl-const-expr-p x))
|
|
2848 x))
|
|
2849 (cdr form))))))
|
|
2850 (list 'progn
|
|
2851 (list 'or form
|
|
2852 (if string
|
|
2853 (list* 'error string (append sargs args))
|
|
2854 (list 'signal '(quote cl-assertion-failed)
|
|
2855 (list* 'list (list 'quote form) sargs))))
|
|
2856 nil))))
|
|
2857
|
|
2858 ;;;###autoload
|
|
2859 (defmacro ignore-errors (&rest body)
|
|
2860 "Execute FORMS; if an error occurs, return nil.
|
|
2861 Otherwise, return result of last FORM."
|
|
2862 `(condition-case nil (progn ,@body) (error nil)))
|
|
2863
|
|
2864 ;;;###autoload
|
|
2865 (defmacro ignore-file-errors (&rest body)
|
|
2866 "Execute FORMS; if an error of type `file-error' occurs, return nil.
|
|
2867 Otherwise, return result of last FORM."
|
|
2868 `(condition-case nil (progn ,@body) (file-error nil)))
|
|
2869
|
|
2870 ;;; Some predicates for analyzing Lisp forms. These are used by various
|
|
2871 ;;; macro expanders to optimize the results in certain common cases.
|
|
2872
|
|
2873 (defconst cl-simple-funcs '(car cdr nth aref elt if and or + - 1+ 1- min max
|
|
2874 car-safe cdr-safe progn prog1 prog2))
|
|
2875 (defconst cl-safe-funcs '(* / % length memq list vector vectorp
|
|
2876 < > <= >= = error))
|
|
2877
|
|
2878 ;;; Check if no side effects, and executes quickly.
|
|
2879 (defun cl-simple-expr-p (x &optional size)
|
|
2880 (or size (setq size 10))
|
|
2881 (if (and (consp x) (not (memq (car x) '(quote function function*))))
|
|
2882 (and (symbolp (car x))
|
|
2883 (or (memq (car x) cl-simple-funcs)
|
|
2884 (get (car x) 'side-effect-free))
|
|
2885 (progn
|
|
2886 (setq size (1- size))
|
|
2887 (while (and (setq x (cdr x))
|
|
2888 (setq size (cl-simple-expr-p (car x) size))))
|
|
2889 (and (null x) (>= size 0) size)))
|
|
2890 (and (> size 0) (1- size))))
|
|
2891
|
|
2892 (defun cl-simple-exprs-p (xs)
|
|
2893 (while (and xs (cl-simple-expr-p (car xs)))
|
|
2894 (setq xs (cdr xs)))
|
|
2895 (not xs))
|
|
2896
|
|
2897 ;;; Check if no side effects.
|
|
2898 (defun cl-safe-expr-p (x)
|
|
2899 (or (not (and (consp x) (not (memq (car x) '(quote function function*)))))
|
|
2900 (and (symbolp (car x))
|
|
2901 (or (memq (car x) cl-simple-funcs)
|
|
2902 (memq (car x) cl-safe-funcs)
|
|
2903 (get (car x) 'side-effect-free))
|
|
2904 (progn
|
|
2905 (while (and (setq x (cdr x)) (cl-safe-expr-p (car x))))
|
|
2906 (null x)))))
|
|
2907
|
|
2908 ;;; Check if constant (i.e., no side effects or dependencies).
|
|
2909 (defun cl-const-expr-p (x)
|
|
2910 (cond ((consp x)
|
|
2911 (or (eq (car x) 'quote)
|
|
2912 (and (memq (car x) '(function function*))
|
|
2913 (or (symbolp (nth 1 x))
|
|
2914 (and (eq (car-safe (nth 1 x)) 'lambda) 'func)))))
|
|
2915 ((symbolp x) (and (memq x '(nil t)) t))
|
|
2916 (t t)))
|
|
2917
|
|
2918 (defun cl-const-exprs-p (xs)
|
|
2919 (while (and xs (cl-const-expr-p (car xs)))
|
|
2920 (setq xs (cdr xs)))
|
|
2921 (not xs))
|
|
2922
|
|
2923 (defun cl-const-expr-val (x)
|
|
2924 (and (eq (cl-const-expr-p x) t) (if (consp x) (nth 1 x) x)))
|
|
2925
|
|
2926 (defun cl-expr-access-order (x v)
|
|
2927 (if (cl-const-expr-p x) v
|
|
2928 (if (consp x)
|
|
2929 (progn
|
|
2930 (while (setq x (cdr x)) (setq v (cl-expr-access-order (car x) v)))
|
|
2931 v)
|
|
2932 (if (eq x (car v)) (cdr v) '(t)))))
|
|
2933
|
|
2934 ;;; Count number of times X refers to Y. Return NIL for 0 times.
|
|
2935 (defun cl-expr-contains (x y)
|
|
2936 (cond ((equal y x) 1)
|
|
2937 ((and (consp x) (not (memq (car-safe x) '(quote function function*))))
|
|
2938 (let ((sum 0))
|
|
2939 (while x
|
|
2940 (setq sum (+ sum (or (cl-expr-contains (cl-pop x) y) 0))))
|
|
2941 (and (> sum 0) sum)))
|
|
2942 (t nil)))
|
|
2943
|
|
2944 (defun cl-expr-contains-any (x y)
|
|
2945 (while (and y (not (cl-expr-contains x (car y)))) (cl-pop y))
|
|
2946 y)
|
|
2947
|
|
2948 ;;; Check whether X may depend on any of the symbols in Y.
|
|
2949 (defun cl-expr-depends-p (x y)
|
|
2950 (and (not (cl-const-expr-p x))
|
|
2951 (or (not (cl-safe-expr-p x)) (cl-expr-contains-any x y))))
|
|
2952
|
|
2953
|
|
2954 ;;; Compiler macros.
|
|
2955
|
|
2956 ;;;###autoload
|
|
2957 (defmacro define-compiler-macro (func args &rest body)
|
|
2958 "(define-compiler-macro FUNC ARGLIST BODY...): Define a compiler-only macro.
|
|
2959 This is like `defmacro', but macro expansion occurs only if the call to
|
|
2960 FUNC is compiled (i.e., not interpreted). Compiler macros should be used
|
|
2961 for optimizing the way calls to FUNC are compiled; the form returned by
|
|
2962 BODY should do the same thing as a call to the normal function called
|
|
2963 FUNC, though possibly more efficiently. Note that, like regular macros,
|
|
2964 compiler macros are expanded repeatedly until no further expansions are
|
|
2965 possible. Unlike regular macros, BODY can decide to \"punt\" and leave the
|
|
2966 original function call alone by declaring an initial `&whole foo' parameter
|
|
2967 and then returning foo."
|
|
2968 (let ((p (if (listp args) args (list '&rest args))) (res nil))
|
|
2969 (while (consp p) (cl-push (cl-pop p) res))
|
|
2970 (setq args (nreverse res)) (setcdr res (and p (list '&rest p))))
|
|
2971 (list 'eval-when '(compile load eval)
|
|
2972 (cl-transform-function-property
|
|
2973 func 'cl-compiler-macro
|
|
2974 (cons (if (memq '&whole args) (delq '&whole args)
|
|
2975 (cons '--cl-whole-arg-- args)) body))
|
|
2976 (list 'or (list 'get (list 'quote func) '(quote byte-compile))
|
|
2977 (list 'put (list 'quote func) '(quote byte-compile)
|
|
2978 '(quote cl-byte-compile-compiler-macro)))))
|
|
2979
|
|
2980 ;;;###autoload
|
|
2981 (defun compiler-macroexpand (form)
|
|
2982 (while
|
|
2983 (let ((func (car-safe form)) (handler nil))
|
|
2984 (while (and (symbolp func)
|
|
2985 (not (setq handler (get func 'cl-compiler-macro)))
|
|
2986 (fboundp func)
|
|
2987 (or (not (eq (car-safe (symbol-function func)) 'autoload))
|
|
2988 (load (nth 1 (symbol-function func)))))
|
|
2989 (setq func (symbol-function func)))
|
|
2990 (and handler
|
|
2991 (not (eq form (setq form (apply handler form (cdr form))))))))
|
|
2992 form)
|
|
2993
|
|
2994 (defun cl-byte-compile-compiler-macro (form)
|
|
2995 (if (eq form (setq form (compiler-macroexpand form)))
|
|
2996 (byte-compile-normal-call form)
|
|
2997 (byte-compile-form form)))
|
|
2998
|
|
2999 (defmacro defsubst* (name args &rest body)
|
|
3000 "(defsubst* NAME ARGLIST [DOCSTRING] BODY...): define NAME as a function.
|
|
3001 Like `defun', except the function is automatically declared `inline',
|
|
3002 ARGLIST allows full Common Lisp conventions, and BODY is implicitly
|
|
3003 surrounded by (block NAME ...)."
|
|
3004 (let* ((argns (cl-arglist-args args)) (p argns)
|
|
3005 (pbody (cons 'progn body))
|
|
3006 (unsafe (not (cl-safe-expr-p pbody))))
|
|
3007 (while (and p (eq (cl-expr-contains args (car p)) 1)) (cl-pop p))
|
|
3008 (list 'progn
|
|
3009 (if p nil ; give up if defaults refer to earlier args
|
|
3010 (list 'define-compiler-macro name
|
|
3011 (list* '&whole 'cl-whole '&cl-quote args)
|
|
3012 (list* 'cl-defsubst-expand (list 'quote argns)
|
|
3013 (list 'quote (list* 'block name body))
|
|
3014 (not (or unsafe (cl-expr-access-order pbody argns)))
|
|
3015 (and (memq '&key args) 'cl-whole) unsafe argns)))
|
|
3016 (list* 'defun* name args body))))
|
|
3017
|
|
3018 (defun cl-defsubst-expand (argns body simple whole unsafe &rest argvs)
|
|
3019 (if (and whole (not (cl-safe-expr-p (cons 'progn argvs)))) whole
|
|
3020 (if (cl-simple-exprs-p argvs) (setq simple t))
|
|
3021 (let ((lets (delq nil
|
|
3022 (mapcar* #'(lambda (argn argv)
|
|
3023 (if (or simple (cl-const-expr-p argv))
|
|
3024 (progn (setq body (subst argv argn body))
|
|
3025 (and unsafe (list argn argv)))
|
|
3026 (list argn argv)))
|
|
3027 argns argvs))))
|
|
3028 (if lets (list 'let lets body) body))))
|
|
3029
|
|
3030
|
|
3031 ;;; Compile-time optimizations for some functions defined in this package.
|
|
3032 ;;; Note that cl.el arranges to force cl-macs to be loaded at compile-time,
|
|
3033 ;;; mainly to make sure these macros will be present.
|
|
3034
|
|
3035 (put 'eql 'byte-compile nil)
|
|
3036 (define-compiler-macro eql (&whole form a b)
|
|
3037 (cond ((eq (cl-const-expr-p a) t)
|
|
3038 (let ((val (cl-const-expr-val a)))
|
|
3039 (if (and (numberp val) (not (integerp val)))
|
|
3040 (list 'equal a b)
|
|
3041 (list 'eq a b))))
|
|
3042 ((eq (cl-const-expr-p b) t)
|
|
3043 (let ((val (cl-const-expr-val b)))
|
|
3044 (if (and (numberp val) (not (integerp val)))
|
|
3045 (list 'equal a b)
|
|
3046 (list 'eq a b))))
|
|
3047 ((cl-simple-expr-p a 5)
|
|
3048 (list 'if (list 'numberp a)
|
|
3049 (list 'equal a b)
|
|
3050 (list 'eq a b)))
|
|
3051 ((and (cl-safe-expr-p a)
|
|
3052 (cl-simple-expr-p b 5))
|
|
3053 (list 'if (list 'numberp b)
|
|
3054 (list 'equal a b)
|
|
3055 (list 'eq a b)))
|
|
3056 (t form)))
|
|
3057
|
|
3058 (define-compiler-macro member* (&whole form a list &rest keys)
|
|
3059 (let ((test (and (= (length keys) 2) (eq (car keys) ':test)
|
|
3060 (cl-const-expr-val (nth 1 keys)))))
|
|
3061 (cond ((eq test 'eq) (list 'memq a list))
|
|
3062 ((eq test 'equal) (list 'member a list))
|
|
3063 ((or (null keys) (eq test 'eql))
|
|
3064 (if (eq (cl-const-expr-p a) t)
|
|
3065 (list (if (floatp-safe (cl-const-expr-val a)) 'member 'memq)
|
|
3066 a list)
|
|
3067 (if (eq (cl-const-expr-p list) t)
|
|
3068 (let ((p (cl-const-expr-val list)) (mb nil) (mq nil))
|
|
3069 (if (not (cdr p))
|
|
3070 (and p (list 'eql a (list 'quote (car p))))
|
|
3071 (while p
|
|
3072 (if (floatp-safe (car p)) (setq mb t)
|
|
3073 (or (integerp (car p)) (symbolp (car p)) (setq mq t)))
|
|
3074 (setq p (cdr p)))
|
|
3075 (if (not mb) (list 'memq a list)
|
|
3076 (if (not mq) (list 'member a list) form))))
|
|
3077 form)))
|
|
3078 (t form))))
|
|
3079
|
|
3080 (define-compiler-macro assoc* (&whole form a list &rest keys)
|
|
3081 (let ((test (and (= (length keys) 2) (eq (car keys) ':test)
|
|
3082 (cl-const-expr-val (nth 1 keys)))))
|
|
3083 (cond ((eq test 'eq) (list 'assq a list))
|
|
3084 ((eq test 'equal) (list 'assoc a list))
|
|
3085 ((and (eq (cl-const-expr-p a) t) (or (null keys) (eq test 'eql)))
|
|
3086 (if (floatp-safe (cl-const-expr-val a))
|
|
3087 (list 'assoc a list) (list 'assq a list)))
|
|
3088 (t form))))
|
|
3089
|
|
3090 (define-compiler-macro adjoin (&whole form a list &rest keys)
|
|
3091 (if (and (cl-simple-expr-p a) (cl-simple-expr-p list)
|
|
3092 (not (memq ':key keys)))
|
|
3093 (list 'if (list* 'member* a list keys) list (list 'cons a list))
|
|
3094 form))
|
|
3095
|
|
3096 (define-compiler-macro list* (arg &rest others)
|
|
3097 (let* ((args (reverse (cons arg others)))
|
|
3098 (form (car args)))
|
|
3099 (while (setq args (cdr args))
|
|
3100 (setq form (list 'cons (car args) form)))
|
|
3101 form))
|
|
3102
|
440
|
3103 (define-compiler-macro get* (sym prop &optional default)
|
|
3104 (list 'get sym prop default))
|
428
|
3105
|
442
|
3106 (define-compiler-macro getf (sym prop &optional default)
|
|
3107 (list 'plist-get sym prop default))
|
|
3108
|
428
|
3109 (define-compiler-macro typep (&whole form val type)
|
|
3110 (if (cl-const-expr-p type)
|
|
3111 (let ((res (cl-make-type-test val (cl-const-expr-val type))))
|
|
3112 (if (or (memq (cl-expr-contains res val) '(nil 1))
|
|
3113 (cl-simple-expr-p val)) res
|
|
3114 (let ((temp (gensym)))
|
|
3115 (list 'let (list (list temp val)) (subst temp val res)))))
|
|
3116 form))
|
|
3117
|
|
3118
|
|
3119 (mapc
|
|
3120 #'(lambda (y)
|
|
3121 (put (car y) 'side-effect-free t)
|
|
3122 (put (car y) 'byte-compile 'cl-byte-compile-compiler-macro)
|
|
3123 (put (car y) 'cl-compiler-macro
|
|
3124 (list 'lambda '(w x)
|
|
3125 (if (symbolp (cadr y))
|
|
3126 (list 'list (list 'quote (cadr y))
|
|
3127 (list 'list (list 'quote (caddr y)) 'x))
|
|
3128 (cons 'list (cdr y))))))
|
|
3129 '((first 'car x) (second 'cadr x) (third 'caddr x) (fourth 'cadddr x)
|
|
3130 (fifth 'nth 4 x) (sixth 'nth 5 x) (seventh 'nth 6 x)
|
|
3131 (eighth 'nth 7 x) (ninth 'nth 8 x) (tenth 'nth 9 x)
|
|
3132 (rest 'cdr x) (endp 'null x) (plusp '> x 0) (minusp '< x 0)
|
446
|
3133 (oddp 'eq (list 'logand x 1) 1)
|
|
3134 (evenp 'eq (list 'logand x 1) 0)
|
428
|
3135 (caar car car) (cadr car cdr) (cdar cdr car) (cddr cdr cdr)
|
|
3136 (caaar car caar) (caadr car cadr) (cadar car cdar)
|
|
3137 (caddr car cddr) (cdaar cdr caar) (cdadr cdr cadr)
|
|
3138 (cddar cdr cdar) (cdddr cdr cddr) (caaaar car caaar)
|
|
3139 (caaadr car caadr) (caadar car cadar) (caaddr car caddr)
|
|
3140 (cadaar car cdaar) (cadadr car cdadr) (caddar car cddar)
|
|
3141 (cadddr car cdddr) (cdaaar cdr caaar) (cdaadr cdr caadr)
|
|
3142 (cdadar cdr cadar) (cdaddr cdr caddr) (cddaar cdr cdaar)
|
|
3143 (cddadr cdr cdadr) (cdddar cdr cddar) (cddddr cdr cdddr)))
|
|
3144
|
|
3145 ;;; Things that are inline.
|
|
3146 (proclaim '(inline floatp-safe acons map concatenate notany notevery
|
|
3147 ;; XEmacs change
|
|
3148 cl-set-elt revappend nreconc
|
|
3149 ))
|
|
3150
|
|
3151 ;;; Things that are side-effect-free. Moved to byte-optimize.el
|
|
3152 ;(dolist (fun '(oddp evenp plusp minusp
|
|
3153 ; abs expt signum last butlast ldiff
|
|
3154 ; pairlis gcd lcm
|
|
3155 ; isqrt floor* ceiling* truncate* round* mod* rem* subseq
|
440
|
3156 ; list-length getf))
|
428
|
3157 ; (put fun 'side-effect-free t))
|
|
3158
|
|
3159 ;;; Things that are side-effect-and-error-free. Moved to byte-optimize.el
|
|
3160 ;(dolist (fun '(eql floatp-safe list* subst acons equalp random-state-p
|
|
3161 ; copy-tree sublis))
|
|
3162 ; (put fun 'side-effect-free 'error-free))
|
|
3163
|
|
3164
|
|
3165 (run-hooks 'cl-macs-load-hook)
|
|
3166
|
|
3167 ;;; cl-macs.el ends here
|