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