* Makefile.in (install-etc): Fix previous change.
[bpt/emacs.git] / lisp / emacs-lisp / cl-macs.el
CommitLineData
bc8ce89b 1;;; cl-macs.el --- Common Lisp macros
fcd73769 2
acaf905b 3;; Copyright (C) 1993, 2001-2012 Free Software Foundation, Inc.
fcd73769
RS
4
5;; Author: Dave Gillespie <daveg@synaptics.com>
6;; Version: 2.02
7;; Keywords: extensions
bd78fa1d 8;; Package: emacs
fcd73769
RS
9
10;; This file is part of GNU Emacs.
11
d6cba7ae 12;; GNU Emacs is free software: you can redistribute it and/or modify
fcd73769 13;; it under the terms of the GNU General Public License as published by
d6cba7ae
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
fcd73769
RS
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
d6cba7ae 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
fcd73769 24
07b3798c 25;;; Commentary:
fcd73769
RS
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;;
fcd73769
RS
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
07b3798c 44;;; Code:
fcd73769 45
56565c74 46(require 'cl)
fcd73769 47
fcd73769
RS
48(defmacro cl-pop2 (place)
49 (list 'prog1 (list 'car (list 'cdr place))
50 (list 'setq place (list 'cdr (list 'cdr place)))))
fcd73769
RS
51(put 'cl-pop2 'edebug-form-spec 'edebug-sexps)
52
fcd73769
RS
53(defvar cl-optimize-safety)
54(defvar cl-optimize-speed)
55
56
d0aa0542
SM
57;; This kludge allows macros which use cl-transform-function-property
58;; to be called at compile-time.
fcd73769
RS
59
60(require
61 (progn
fcd73769
RS
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
b7b95a1e
DL
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)
e754e83b
SM
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.
b7b95a1e
DL
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)
e129292c
CS
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.
b7b95a1e
DL
150 (cond ((equal y x) 1)
151 ((and (consp x) (not (memq (car-safe x) '(quote function function*))))
152 (let ((sum 0))
e129292c 153 (while (consp x)
b7b95a1e 154 (setq sum (+ sum (or (cl-expr-contains (pop x) y) 0))))
e129292c 155 (setq sum (+ sum (or (cl-expr-contains x y) 0)))
b7b95a1e
DL
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
fcd73769
RS
168;;; Symbols.
169
170(defvar *gensym-counter*)
ebacfcc6 171;;;###autoload
a766dfa1 172(defun gensym (&optional prefix)
fcd73769
RS
173 "Generate a new uninterned symbol.
174The name is made by appending a number to PREFIX, default \"G\"."
a766dfa1
JB
175 (let ((pfix (if (stringp prefix) prefix "G"))
176 (num (if (integerp prefix) prefix
fcd73769
RS
177 (prog1 *gensym-counter*
178 (setq *gensym-counter* (1+ *gensym-counter*))))))
a766dfa1 179 (make-symbol (format "%s%d" pfix num))))
fcd73769 180
ebacfcc6 181;;;###autoload
a766dfa1 182(defun gentemp (&optional prefix)
fcd73769
RS
183 "Generate a new interned symbol with a unique name.
184The name is made by appending a number to PREFIX, default \"G\"."
a766dfa1 185 (let ((pfix (if (stringp prefix) prefix "G"))
fcd73769 186 name)
a766dfa1 187 (while (intern-soft (setq name (format "%s%d" pfix *gensym-counter*)))
fcd73769
RS
188 (setq *gensym-counter* (1+ *gensym-counter*)))
189 (intern name)))
190
191
192;;; Program structure.
193
ebacfcc6 194;;;###autoload
fcd73769 195(defmacro defun* (name args &rest body)
69d8fb1e 196 "Define NAME as a function.
fcd73769 197Like normal `defun', except ARGLIST allows full Common Lisp conventions,
69d8fb1e
SM
198and BODY is implicitly surrounded by (block NAME ...).
199
200\(fn NAME ARGLIST [DOCSTRING] BODY...)"
fcd73769
RS
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
ebacfcc6 205;;;###autoload
fcd73769 206(defmacro defmacro* (name args &rest body)
69d8fb1e 207 "Define NAME as a macro.
fcd73769 208Like normal `defmacro', except ARGLIST allows full Common Lisp conventions,
69d8fb1e
SM
209and BODY is implicitly surrounded by (block NAME ...).
210
211\(fn NAME ARGLIST [DOCSTRING] BODY...)"
fcd73769
RS
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
ebacfcc6 216;;;###autoload
fcd73769 217(defmacro function* (func)
64a4c526 218 "Introduce a function.
3187ba1c
JB
219Like normal `function', except that if argument is a lambda form,
220its argument list allows full Common Lisp conventions."
fcd73769
RS
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
f9a9e6ea
SM
236(defvar cl-macro-environment nil
237 "Keep the list of currently active macros.
238It 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.")
fcd73769
RS
241(defvar bind-block) (defvar bind-defs) (defvar bind-enquote)
242(defvar bind-inits) (defvar bind-lets) (defvar bind-forms)
243
b4587710
JB
244(declare-function help-add-fundoc-usage "help-fns" (docstring arglist))
245
673e08bb
SM
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
fcd73769 277(defun cl-transform-lambda (form bind-block)
69d8fb1e 278 (let* ((args (car form)) (body (cdr form)) (orig-args args)
fcd73769
RS
279 (bind-defs nil) (bind-enquote nil)
280 (bind-inits nil) (bind-lets nil) (bind-forms nil)
281 (header nil) (simple-args nil))
a5ad278d
EZ
282 (while (or (stringp (car body))
283 (memq (car-safe (car body)) '(interactive declare)))
69d8fb1e 284 (push (pop body) header))
fcd73769
RS
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))))))
69d8fb1e 300 (push (pop args) simple-args))
fcd73769
RS
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))
69d8fb1e 305 (if (memq '&optional simple-args) (push '&optional args))
fcd73769
RS
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)
69d8fb1e
SM
312 (list '&rest (car (pop bind-lets))))
313 (nconc (let ((hdr (nreverse header)))
4d78a860
SM
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))
ac348012
SM
321 (format "%S"
322 (cons 'fn
323 (cl--make-usage-args orig-args))))
4d78a860 324 hdr)))
fcd73769
RS
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)
69d8fb1e 332 (push (list args expr) bind-lets))
fcd73769
RS
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))
e542ea4b 344 (setq restarg (make-symbol "--cl-rest--"))
fcd73769 345 (setq restarg (cadr restarg)))
69d8fb1e 346 (push (list restarg expr) bind-lets)
fcd73769 347 (if (eq (car args) '&whole)
69d8fb1e 348 (push (list (cl-pop2 args) restarg) bind-lets))
fcd73769
RS
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
69d8fb1e 362 (pop args)
fcd73769
RS
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))
69d8fb1e 370 (while (and (eq (car args) '&optional) (pop args))
fcd73769 371 (while (and args (not (memq (car args) lambda-list-keywords)))
69d8fb1e 372 (let ((arg (pop args)))
fcd73769
RS
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
69d8fb1e 387 (push (list 'if restarg
fcd73769
RS
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)))
69d8fb1e 394 (while (and (eq (car args) '&key) (pop args))
fcd73769 395 (while (and args (not (memq (car args) lambda-list-keywords)))
69d8fb1e 396 (let ((arg (pop args)))
dd441b46 397 (or (consp arg) (setq arg (list arg)))
fcd73769
RS
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)))))
dd441b46 403 (look (list 'memq (list 'quote karg) restarg)))
fcd73769
RS
404 (and def bind-enquote (setq def (list 'quote def)))
405 (if (cddr arg)
e542ea4b 406 (let* ((temp (or (nth 2 arg) (make-symbol "--cl-var--")))
fcd73769
RS
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))))))))
69d8fb1e 425 (push karg keys)))))
fcd73769 426 (setq keys (nreverse keys))
69d8fb1e 427 (or (and (eq (car args) '&allow-other-keys) (pop args))
fcd73769 428 (null keys) (= safety 0)
e542ea4b 429 (let* ((var (make-symbol "--cl-keys--"))
fcd73769
RS
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))))
dd441b46
GM
438 (list (list 'car
439 (list 'cdr
440 (list 'memq (cons 'quote allow)
441 restarg)))
fcd73769
RS
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)))))))
69d8fb1e
SM
449 (push (list 'let (list (list var restarg)) check) bind-forms)))
450 (while (and (eq (car args) '&aux) (pop args))
fcd73769
RS
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)
69d8fb1e
SM
455 (list 'quote (cadr (pop args))))
456 (cl-do-arglist (caar args) (cadr (pop args))))
457 (cl-do-arglist (pop args) nil))))
fcd73769
RS
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)
69d8fb1e 464 (setq arg (pop args))
fcd73769 465 (if (memq arg lambda-list-keywords) (setq kind arg)
69d8fb1e 466 (if (eq arg '&cl-defs) (pop args)
fcd73769
RS
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
ebacfcc6 472;;;###autoload
fcd73769
RS
473(defmacro destructuring-bind (args expr &rest body)
474 (let* ((bind-lets nil) (bind-forms nil) (bind-inits nil)
37a7e764 475 (bind-defs nil) (bind-block 'cl-none) (bind-enquote nil))
fcd73769
RS
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
ebacfcc6 486;;;###autoload
fcd73769 487(defmacro eval-when (when &rest body)
69d8fb1e 488 "Control when BODY is evaluated.
fcd73769
RS
489If `compile' is in WHEN, BODY is evaluated when compiled at top-level.
490If `load' is in WHEN, BODY is evaluated when loaded after top-level compile.
69d8fb1e
SM
491If `eval' is in WHEN, BODY is evaluated when interpreted or at non-top-level.
492
493\(fn (WHEN...) BODY...)"
fcd73769
RS
494 (if (and (fboundp 'cl-compiling-file) (cl-compiling-file)
495 (not cl-not-toplevel) (not (boundp 'for-effect))) ; horrible kludge
64a4c526 496 (let ((comp (or (memq 'compile when) (memq :compile-toplevel when)))
fcd73769 497 (cl-not-toplevel t))
64a4c526 498 (if (or (memq 'load when) (memq :load-toplevel when))
fcd73769
RS
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)))
64a4c526 502 (and (or (memq 'eval when) (memq :execute when))
fcd73769
RS
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)))
64a4c526 513 (if (or (memq 'eval when) (memq :execute when))
fcd73769
RS
514 (list* 'eval-when (cons 'compile when) (cddr form))
515 form)))
516 (t (eval form) form)))
517
ebacfcc6 518;;;###autoload
fcd73769
RS
519(defmacro load-time-value (form &optional read-only)
520 "Like `progn', but evaluates the body at load time.
521The 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)))
7200d79c 534 (print set (symbol-value 'byte-compile--outbuffer)))
fcd73769
RS
535 (list 'symbol-value (list 'quote temp)))
536 (list 'quote (eval form))))
537
538
539;;; Conditional control structures.
540
ebacfcc6 541;;;###autoload
fcd73769 542(defmacro case (expr &rest clauses)
3187ba1c 543 "Eval EXPR and choose among clauses on that value.
fcd73769
RS
544Each clause looks like (KEYLIST BODY...). EXPR is evaluated and compared
545against each key in each KEYLIST; the corresponding BODY is evaluated.
546If no clause succeeds, case returns nil. A single atom may be used in
a8ea72a0 547place of a KEYLIST of one atom. A KEYLIST of t or `otherwise' is
fcd73769 548allowed only in the final clause, and matches if no other keys match.
3187ba1c
JB
549Key values are compared by `eql'.
550\n(fn EXPR (KEYLIST BODY...)...)"
e542ea4b 551 (let* ((temp (if (cl-simple-expr-p expr 3) expr (make-symbol "--cl-var--")))
fcd73769
RS
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)))
69d8fb1e 569 (push (car c) head-list)
fcd73769
RS
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
ebacfcc6 576;;;###autoload
fcd73769 577(defmacro ecase (expr &rest clauses)
64a4c526 578 "Like `case', but error if no case fits.
3187ba1c
JB
579`otherwise'-clauses are not allowed.
580\n(fn EXPR (KEYLIST BODY...)...)"
fcd73769
RS
581 (list* 'case expr (append clauses '((ecase-error-flag)))))
582
ebacfcc6 583;;;###autoload
fcd73769 584(defmacro typecase (expr &rest clauses)
3187ba1c 585 "Evals EXPR, chooses among clauses on that value.
fcd73769
RS
586Each clause looks like (TYPE BODY...). EXPR is evaluated and, if it
587satisfies TYPE, the corresponding BODY is evaluated. If no clause succeeds,
a8ea72a0 588typecase returns nil. A TYPE of t or `otherwise' is allowed only in the
3187ba1c
JB
589final clause, and matches if no other keys match.
590\n(fn EXPR (TYPE BODY...)...)"
e542ea4b 591 (let* ((temp (if (cl-simple-expr-p expr 3) expr (make-symbol "--cl-var--")))
fcd73769
RS
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
69d8fb1e 603 (push (car c) type-list)
fcd73769
RS
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
ebacfcc6 610;;;###autoload
fcd73769 611(defmacro etypecase (expr &rest clauses)
64a4c526 612 "Like `typecase', but error if no case fits.
3187ba1c
JB
613`otherwise'-clauses are not allowed.
614\n(fn EXPR (TYPE BODY...)...)"
fcd73769
RS
615 (list* 'typecase expr (append clauses '((ecase-error-flag)))))
616
617
618;;; Blocks and exits.
619
ebacfcc6 620;;;###autoload
fcd73769 621(defmacro block (name &rest body)
64a4c526 622 "Define a lexically-scoped block named NAME.
fcd73769
RS
623NAME may be any symbol. Code inside the BODY forms can call `return-from'
624to jump prematurely out of the block. This differs from `catch' and `throw'
625in two respects: First, the NAME is an unevaluated symbol rather than a
626quoted symbol or other form; and second, NAME is lexically rather than
627dynamically scoped: Only references to it within BODY will work. These
628references may appear inside macro expansions, but not inside functions
629called 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
ebacfcc6 635;;;###autoload
64a4c526
DL
636(defmacro return (&optional result)
637 "Return from the block named nil.
fcd73769 638This is equivalent to `(return-from nil RESULT)'."
64a4c526 639 (list 'return-from nil result))
fcd73769 640
ebacfcc6 641;;;###autoload
64a4c526
DL
642(defmacro return-from (name &optional result)
643 "Return from the block named NAME.
a60287ff 644This jumps out to the innermost enclosing `(block NAME ...)' form,
fcd73769
RS
645returning RESULT from that form (or nil if RESULT is omitted).
646This 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))))
64a4c526 649 (list 'cl-block-throw (list 'quote name2) result)))
fcd73769
RS
650
651
652;;; The "loop" macro.
653
215461a8 654(defvar loop-args) (defvar loop-accum-var) (defvar loop-accum-vars)
fcd73769
RS
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
ebacfcc6 661;;;###autoload
215461a8 662(defmacro loop (&rest loop-args)
69d8fb1e 663 "The Common Lisp `loop' macro.
fcd73769
RS
664Valid 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,
69d8fb1e
SM
674 finally return EXPR, named NAME.
675
676\(fn CLAUSE...)"
215461a8
GM
677 (if (not (memq t (mapcar 'symbolp (delq nil (delq t (copy-list loop-args))))))
678 (list 'block nil (list* 'while t loop-args))
fcd73769
RS
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))
215461a8
GM
687 (setq loop-args (append loop-args '(cl-end-loop)))
688 (while (not (eq (car loop-args) 'cl-end-loop)) (cl-parse-loop-clause))
fcd73769 689 (if loop-finish-flag
e542ea4b 690 (push `((,loop-finish-flag t)) loop-bindings))
fcd73769 691 (if loop-first-flag
e542ea4b
SM
692 (progn (push `((,loop-first-flag t)) loop-bindings)
693 (push `(setq ,loop-first-flag nil) loop-steps)))
fcd73769
RS
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
e542ea4b
SM
704 (cons `(or ,(car ands)
705 (return-from --cl-finish--
706 nil))
fcd73769
RS
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)
e542ea4b
SM
712 `((if ,loop-finish-flag
713 (progn ,@epilogue) ,loop-result-var)))
fcd73769 714 epilogue))))
69d8fb1e 715 (if loop-result-var (push (list loop-result-var) loop-bindings))
fcd73769
RS
716 (while loop-bindings
717 (if (cdar loop-bindings)
69d8fb1e 718 (setq body (list (cl-loop-let (pop loop-bindings) body t)))
fcd73769
RS
719 (let ((lets nil))
720 (while (and loop-bindings
721 (not (cdar loop-bindings)))
69d8fb1e 722 (push (car (pop loop-bindings)) lets))
fcd73769
RS
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
215461a8
GM
728(defun cl-parse-loop-clause () ; uses loop-*
729 (let ((word (pop loop-args))
fcd73769
RS
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
215461a8 735 ((null loop-args)
fcd73769
RS
736 (error "Malformed `loop' macro"))
737
738 ((eq word 'named)
215461a8 739 (setq loop-name (pop loop-args)))
fcd73769
RS
740
741 ((eq word 'initially)
215461a8
GM
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)))
fcd73769
RS
746
747 ((eq word 'finally)
215461a8
GM
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)))))
fcd73769
RS
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
443b961a
SM
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.
215461a8
GM
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)))
fcd73769 768 (if (memq word '(buffer buffers))
215461a8 769 (setq word 'in loop-args (cons '(buffer-list) loop-args)))
fcd73769
RS
770 (cond
771
772 ((memq word '(from downfrom upfrom to downto upto
773 above below by))
215461a8
GM
774 (push word loop-args)
775 (if (memq (car loop-args) '(downto above))
fcd73769 776 (error "Must specify `from' value for downward loop"))
215461a8
GM
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)
fcd73769 784 '(to upto downto above below))
215461a8
GM
785 (cl-pop2 loop-args)))
786 (step (and (eq (car loop-args) 'by) (cl-pop2 loop-args)))
e542ea4b
SM
787 (end-var (and (not (cl-const-expr-p end))
788 (make-symbol "--cl-var--")))
fcd73769 789 (step-var (and (not (cl-const-expr-p step))
e542ea4b 790 (make-symbol "--cl-var--"))))
fcd73769
RS
791 (and step (numberp step) (<= step 0)
792 (error "Loop `by' value is not positive: %s" step))
69d8fb1e
SM
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)
e542ea4b 796 loop-for-bindings))
fcd73769 797 (if end
69d8fb1e 798 (push (list
e542ea4b
SM
799 (if down (if excl '> '>=) (if excl '< '<=))
800 var (or end-var end)) loop-body))
69d8fb1e 801 (push (list var (list (if down '- '+) var
e542ea4b
SM
802 (or step-var step 1)))
803 loop-for-steps)))
fcd73769
RS
804
805 ((memq word '(in in-ref on))
806 (let* ((on (eq word 'on))
e542ea4b
SM
807 (temp (if (and on (symbolp var))
808 var (make-symbol "--cl-var--"))))
215461a8 809 (push (list temp (pop loop-args)) loop-for-bindings)
69d8fb1e 810 (push (list 'consp temp) loop-body)
fcd73769 811 (if (eq word 'in-ref)
69d8fb1e 812 (push (list var (list 'car temp)) loop-symbol-macs)
fcd73769
RS
813 (or (eq temp var)
814 (progn
69d8fb1e
SM
815 (push (list var nil) loop-for-bindings)
816 (push (list var (if on temp (list 'car temp)))
e542ea4b 817 loop-for-sets))))
69d8fb1e 818 (push (list temp
215461a8
GM
819 (if (eq (car loop-args) 'by)
820 (let ((step (cl-pop2 loop-args)))
e542ea4b
SM
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)))
fcd73769
RS
829
830 ((eq word '=)
215461a8
GM
831 (let* ((start (pop loop-args))
832 (then (if (eq (car loop-args) 'then) (cl-pop2 loop-args) start)))
69d8fb1e 833 (push (list var nil) loop-for-bindings)
215461a8 834 (if (or ands (eq (car loop-args) 'and))
fcd73769 835 (progn
e542ea4b
SM
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)
69d8fb1e
SM
842 (push (list var then) loop-for-steps))
843 (push (list var
e542ea4b
SM
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))))
fcd73769
RS
850
851 ((memq word '(across across-ref))
e542ea4b
SM
852 (let ((temp-vec (make-symbol "--cl-vec--"))
853 (temp-idx (make-symbol "--cl-idx--")))
215461a8 854 (push (list temp-vec (pop loop-args)) loop-for-bindings)
69d8fb1e
SM
855 (push (list temp-idx -1) loop-for-bindings)
856 (push (list '< (list 'setq temp-idx (list '1+ temp-idx))
e542ea4b 857 (list 'length temp-vec)) loop-body)
fcd73769 858 (if (eq word 'across-ref)
69d8fb1e 859 (push (list var (list 'aref temp-vec temp-idx))
e542ea4b 860 loop-symbol-macs)
69d8fb1e
SM
861 (push (list var nil) loop-for-bindings)
862 (push (list var (list 'aref temp-vec temp-idx))
e542ea4b 863 loop-for-sets))))
fcd73769
RS
864
865 ((memq word '(element elements))
215461a8
GM
866 (let ((ref (or (memq (car loop-args) '(in-ref of-ref))
867 (and (not (memq (car loop-args) '(in of)))
fcd73769 868 (error "Expected `of'"))))
215461a8 869 (seq (cl-pop2 loop-args))
e542ea4b 870 (temp-seq (make-symbol "--cl-seq--"))
215461a8
GM
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))
fcd73769 875 (error "Bad `using' clause"))
e542ea4b 876 (make-symbol "--cl-idx--"))))
69d8fb1e
SM
877 (push (list temp-seq seq) loop-for-bindings)
878 (push (list temp-idx 0) loop-for-bindings)
fcd73769 879 (if ref
e542ea4b 880 (let ((temp-len (make-symbol "--cl-len--")))
69d8fb1e 881 (push (list temp-len (list 'length temp-seq))
e542ea4b 882 loop-for-bindings)
69d8fb1e 883 (push (list var (list 'elt temp-seq temp-idx))
e542ea4b 884 loop-symbol-macs)
69d8fb1e
SM
885 (push (list '< temp-idx temp-len) loop-body))
886 (push (list var nil) loop-for-bindings)
887 (push (list 'and temp-seq
e542ea4b
SM
888 (list 'or (list 'consp temp-seq)
889 (list '< temp-idx
890 (list 'length temp-seq))))
891 loop-body)
69d8fb1e 892 (push (list var (list 'if (list 'consp temp-seq)
e542ea4b
SM
893 (list 'pop temp-seq)
894 (list 'aref temp-seq temp-idx)))
895 loop-for-sets))
69d8fb1e 896 (push (list temp-idx (list '1+ temp-idx))
e542ea4b 897 loop-for-steps)))
fcd73769
RS
898
899 ((memq word hash-types)
215461a8
GM
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))
fcd73769 907 (error "Bad `using' clause"))
e542ea4b 908 (make-symbol "--cl-var--"))))
fcd73769
RS
909 (if (memq word '(hash-value hash-values))
910 (setq var (prog1 other (setq other var))))
911 (setq loop-map-form
e542ea4b 912 `(maphash (lambda (,var ,other) . --cl-map) ,table))))
fcd73769
RS
913
914 ((memq word '(symbol present-symbol external-symbol
915 symbols present-symbols external-symbols))
215461a8 916 (let ((ob (and (memq (car loop-args) '(in of)) (cl-pop2 loop-args))))
fcd73769 917 (setq loop-map-form
e542ea4b 918 `(mapatoms (lambda (,var) . --cl-map) ,ob))))
fcd73769
RS
919
920 ((memq word '(overlay overlays extent extents))
921 (let ((buf nil) (from nil) (to nil))
215461a8
GM
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)))))
fcd73769 926 (setq loop-map-form
e542ea4b
SM
927 `(cl-map-extents
928 (lambda (,var ,(make-symbol "--cl-var--"))
929 (progn . --cl-map) nil)
930 ,buf ,from ,to))))
fcd73769
RS
931
932 ((memq word '(interval intervals))
933 (let ((buf nil) (prop nil) (from nil) (to nil)
e542ea4b
SM
934 (var1 (make-symbol "--cl-var1--"))
935 (var2 (make-symbol "--cl-var2--")))
215461a8
GM
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)))))
fcd73769
RS
942 (if (and (consp var) (symbolp (car var)) (symbolp (cdr var)))
943 (setq var1 (car var) var2 (cdr var))
69d8fb1e 944 (push (list var (list 'cons var1 var2)) loop-for-sets))
fcd73769 945 (setq loop-map-form
e542ea4b
SM
946 `(cl-map-intervals
947 (lambda (,var1 ,var2) . --cl-map)
948 ,buf ,prop ,from ,to))))
fcd73769
RS
949
950 ((memq word key-types)
215461a8
GM
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))
fcd73769 958 (error "Bad `using' clause"))
e542ea4b 959 (make-symbol "--cl-var--"))))
fcd73769
RS
960 (if (memq word '(key-binding key-bindings))
961 (setq var (prog1 other (setq other var))))
962 (setq loop-map-form
e542ea4b
SM
963 `(,(if (memq word '(key-seq key-seqs))
964 'cl-map-keymap-recursively 'map-keymap)
965 (lambda (,var ,other) . --cl-map) ,map))))
fcd73769
RS
966
967 ((memq word '(frame frames screen screens))
e542ea4b 968 (let ((temp (make-symbol "--cl-var--")))
69d8fb1e 969 (push (list var '(selected-frame))
e542ea4b 970 loop-for-bindings)
69d8fb1e
SM
971 (push (list temp nil) loop-for-bindings)
972 (push (list 'prog1 (list 'not (list 'eq var temp))
e542ea4b
SM
973 (list 'or temp (list 'setq temp var)))
974 loop-body)
69d8fb1e 975 (push (list var (list 'next-frame var))
e542ea4b 976 loop-for-steps)))
fcd73769
RS
977
978 ((memq word '(window windows))
215461a8 979 (let ((scr (and (memq (car loop-args) '(in of)) (cl-pop2 loop-args)))
05907bb3
GM
980 (temp (make-symbol "--cl-var--"))
981 (minip (make-symbol "--cl-minip--")))
69d8fb1e 982 (push (list var (if scr
e542ea4b
SM
983 (list 'frame-selected-window scr)
984 '(selected-window)))
985 loop-for-bindings)
05907bb3
GM
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)
69d8fb1e
SM
993 (push (list temp nil) loop-for-bindings)
994 (push (list 'prog1 (list 'not (list 'eq var temp))
e542ea4b
SM
995 (list 'or temp (list 'setq temp var)))
996 loop-body)
05907bb3
GM
997 (push (list var (list 'next-window var minip))
998 loop-for-steps)))
fcd73769
RS
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)))))
215461a8 1006 (eq (car loop-args) 'and))
fcd73769 1007 (setq ands t)
215461a8 1008 (pop loop-args))
fcd73769 1009 (if (and ands loop-for-bindings)
69d8fb1e 1010 (push (nreverse loop-for-bindings) loop-bindings)
fcd73769
RS
1011 (setq loop-bindings (nconc (mapcar 'list loop-for-bindings)
1012 loop-bindings)))
1013 (if loop-for-sets
69d8fb1e 1014 (push (list 'progn
e542ea4b
SM
1015 (cl-loop-let (nreverse loop-for-sets) 'setq ands)
1016 t) loop-body))
fcd73769 1017 (if loop-for-steps
69d8fb1e 1018 (push (cons (if ands 'psetq 'setq)
e542ea4b
SM
1019 (apply 'append (nreverse loop-for-steps)))
1020 loop-steps))))
fcd73769
RS
1021
1022 ((eq word 'repeat)
e542ea4b 1023 (let ((temp (make-symbol "--cl-var--")))
215461a8 1024 (push (list (list temp (pop loop-args))) loop-bindings)
69d8fb1e 1025 (push (list '>= (list 'setq temp (list '1- temp)) 0) loop-body)))
fcd73769 1026
ae1aa776 1027 ((memq word '(collect collecting))
215461a8 1028 (let ((what (pop loop-args))
fcd73769
RS
1029 (var (cl-loop-handle-accum nil 'nreverse)))
1030 (if (eq var loop-accum-var)
69d8fb1e
SM
1031 (push (list 'progn (list 'push what var) t) loop-body)
1032 (push (list 'progn
e542ea4b
SM
1033 (list 'setq var (list 'nconc var (list 'list what)))
1034 t) loop-body))))
fcd73769
RS
1035
1036 ((memq word '(nconc nconcing append appending))
215461a8 1037 (let ((what (pop loop-args))
fcd73769 1038 (var (cl-loop-handle-accum nil 'nreverse)))
69d8fb1e 1039 (push (list 'progn
e542ea4b
SM
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)))
fcd73769
RS
1050
1051 ((memq word '(concat concating))
215461a8 1052 (let ((what (pop loop-args))
fcd73769 1053 (var (cl-loop-handle-accum "")))
69d8fb1e 1054 (push (list 'progn (list 'callf 'concat var what) t) loop-body)))
fcd73769
RS
1055
1056 ((memq word '(vconcat vconcating))
215461a8 1057 (let ((what (pop loop-args))
fcd73769 1058 (var (cl-loop-handle-accum [])))
69d8fb1e 1059 (push (list 'progn (list 'callf 'vconcat var what) t) loop-body)))
fcd73769
RS
1060
1061 ((memq word '(sum summing))
215461a8 1062 (let ((what (pop loop-args))
fcd73769 1063 (var (cl-loop-handle-accum 0)))
69d8fb1e 1064 (push (list 'progn (list 'incf var what) t) loop-body)))
fcd73769
RS
1065
1066 ((memq word '(count counting))
215461a8 1067 (let ((what (pop loop-args))
fcd73769 1068 (var (cl-loop-handle-accum 0)))
69d8fb1e 1069 (push (list 'progn (list 'if what (list 'incf var)) t) loop-body)))
fcd73769
RS
1070
1071 ((memq word '(minimize minimizing maximize maximizing))
215461a8 1072 (let* ((what (pop loop-args))
e542ea4b 1073 (temp (if (cl-simple-expr-p what) what (make-symbol "--cl-var--")))
fcd73769
RS
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))))
69d8fb1e 1077 (push (list 'progn (if (eq temp what) set
e542ea4b
SM
1078 (list 'let (list (list temp what)) set))
1079 t) loop-body)))
fcd73769
RS
1080
1081 ((eq word 'with)
1082 (let ((bindings nil))
215461a8
GM
1083 (while (progn (push (list (pop loop-args)
1084 (and (eq (car loop-args) '=) (cl-pop2 loop-args)))
e542ea4b 1085 bindings)
215461a8
GM
1086 (eq (car loop-args) 'and))
1087 (pop loop-args))
69d8fb1e 1088 (push (nreverse bindings) loop-bindings)))
fcd73769
RS
1089
1090 ((eq word 'while)
215461a8 1091 (push (pop loop-args) loop-body))
fcd73769
RS
1092
1093 ((eq word 'until)
215461a8 1094 (push (list 'not (pop loop-args)) loop-body))
fcd73769
RS
1095
1096 ((eq word 'always)
e542ea4b 1097 (or loop-finish-flag (setq loop-finish-flag (make-symbol "--cl-flag--")))
215461a8 1098 (push (list 'setq loop-finish-flag (pop loop-args)) loop-body)
fcd73769
RS
1099 (setq loop-result t))
1100
1101 ((eq word 'never)
e542ea4b 1102 (or loop-finish-flag (setq loop-finish-flag (make-symbol "--cl-flag--")))
215461a8 1103 (push (list 'setq loop-finish-flag (list 'not (pop loop-args)))
e542ea4b 1104 loop-body)
fcd73769
RS
1105 (setq loop-result t))
1106
1107 ((eq word 'thereis)
e542ea4b
SM
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--")))
69d8fb1e 1110 (push (list 'setq loop-finish-flag
215461a8 1111 (list 'not (list 'setq loop-result-var (pop loop-args))))
e542ea4b 1112 loop-body))
fcd73769
RS
1113
1114 ((memq word '(if when unless))
215461a8 1115 (let* ((cond (pop loop-args))
fcd73769
RS
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))
215461a8
GM
1120 (if (eq (car loop-args) 'else)
1121 (progn (pop loop-args) (cl-parse-loop-clause)))
fcd73769
RS
1122 (cl-loop-build-ands (nreverse loop-body))))
1123 (simple (and (eq (car then) t) (eq (car else) t))))
215461a8 1124 (if (eq (car loop-args) 'end) (pop loop-args))
fcd73769
RS
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)
e542ea4b 1129 (let ((temp (make-symbol "--cl-var--")))
69d8fb1e 1130 (push (list temp) loop-bindings)
fcd73769
RS
1131 (setq form (list* 'if (list 'setq temp cond)
1132 (subst temp 'it form))))
1133 (setq form (list* 'if cond form)))
69d8fb1e 1134 (push (if simple (list 'progn form t) form) loop-body))))
fcd73769
RS
1135
1136 ((memq word '(do doing))
1137 (let ((body nil))
215461a8
GM
1138 (or (consp (car loop-args)) (error "Syntax error on `do' clause"))
1139 (while (consp (car loop-args)) (push (pop loop-args) body))
69d8fb1e 1140 (push (cons 'progn (nreverse (cons t body))) loop-body)))
fcd73769
RS
1141
1142 ((eq word 'return)
e542ea4b
SM
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--")))
215461a8 1145 (push (list 'setq loop-result-var (pop loop-args)
e542ea4b 1146 loop-finish-flag nil) loop-body))
fcd73769
RS
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))))
215461a8
GM
1152 (if (eq (car loop-args) 'and)
1153 (progn (pop loop-args) (cl-parse-loop-clause)))))
fcd73769
RS
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))
e542ea4b 1164 (let ((temp (make-symbol "--cl-var--")))
69d8fb1e 1165 (push (list temp (cadar p)) temps)
fcd73769
RS
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)
69d8fb1e 1171 (expr (cadr (pop specs)))
fcd73769 1172 (temp (cdr (or (assq spec loop-destr-temps)
69d8fb1e 1173 (car (push (cons spec (or (last spec 0)
e542ea4b
SM
1174 (make-symbol "--cl-var--")))
1175 loop-destr-temps))))))
69d8fb1e 1176 (push (list temp expr) new)
fcd73769 1177 (while (consp spec)
69d8fb1e 1178 (push (list (pop spec)
fcd73769
RS
1179 (and expr (list (if spec 'pop 'car) temp)))
1180 nspecs))
1181 (setq specs (nconc (nreverse nspecs) specs)))
69d8fb1e 1182 (push (pop specs) new)))
fcd73769
RS
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
215461a8
GM
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)))
fcd73769 1192 (or (memq var loop-accum-vars)
69d8fb1e
SM
1193 (progn (push (list (list var def)) loop-bindings)
1194 (push var loop-accum-vars)))
fcd73769
RS
1195 var)
1196 (or loop-accum-var
1197 (progn
e542ea4b 1198 (push (list (list (setq loop-accum-var (make-symbol "--cl-var--")) def))
fcd73769
RS
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)))
69d8fb1e
SM
1217 (setq body (cdr (butlast (pop clauses)))))
1218 (push (pop clauses) ands)))
fcd73769
RS
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
ebacfcc6 1230;;;###autoload
fcd73769
RS
1231(defmacro do (steps endtest &rest body)
1232 "The Common Lisp `do' loop.
a766dfa1
JB
1233
1234\(fn ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)"
fcd73769
RS
1235 (cl-expand-do-loop steps endtest body nil))
1236
ebacfcc6 1237;;;###autoload
fcd73769
RS
1238(defmacro do* (steps endtest &rest body)
1239 "The Common Lisp `do*' loop.
a766dfa1
JB
1240
1241\(fn ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)"
fcd73769
RS
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
ebacfcc6 1265;;;###autoload
63744c0f 1266(defmacro dolist (spec &rest body)
69d8fb1e 1267 "Loop over a list.
63744c0f 1268Evaluate BODY with VAR bound to each `car' from LIST, in turn.
69d8fb1e 1269Then evaluate RESULT to get return value, default nil.
ce887515 1270An implicit nil block is established around the loop.
69d8fb1e
SM
1271
1272\(fn (VAR LIST [RESULT]) BODY...)"
e542ea4b 1273 (let ((temp (make-symbol "--cl-dolist-temp--")))
2462470b
SM
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))))))))
63744c0f 1297
ebacfcc6 1298;;;###autoload
63744c0f 1299(defmacro dotimes (spec &rest body)
69d8fb1e 1300 "Loop a certain number of times.
63744c0f
DL
1301Evaluate BODY with VAR bound to successive integers from 0, inclusive,
1302to COUNT, exclusive. Then evaluate RESULT to get return value, default
69d8fb1e
SM
1303nil.
1304
1305\(fn (VAR COUNT [RESULT]) BODY...)"
2462470b
SM
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)))))))
63744c0f 1330
ebacfcc6 1331;;;###autoload
fcd73769 1332(defmacro do-symbols (spec &rest body)
69d8fb1e 1333 "Loop over all symbols.
fcd73769 1334Evaluate BODY with VAR bound to each interned symbol, or to each symbol
69d8fb1e
SM
1335from OBARRAY.
1336
1337\(fn (VAR [OBARRAY [RESULT]]) BODY...)"
fcd73769
RS
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
ebacfcc6 1346;;;###autoload
fcd73769
RS
1347(defmacro do-all-symbols (spec &rest body)
1348 (list* 'do-symbols (list (car spec) nil (cadr spec)) body))
1349
1350
1351;;; Assignments.
1352
ebacfcc6 1353;;;###autoload
fcd73769 1354(defmacro psetq (&rest args)
69d8fb1e 1355 "Set SYMs to the values VALs in parallel.
fcd73769 1356This is like `setq', except that all VAL forms are evaluated (in order)
69d8fb1e
SM
1357before assigning any symbols SYM to the corresponding values.
1358
1359\(fn SYM VAL SYM VAL ...)"
fcd73769
RS
1360 (cons 'psetf args))
1361
1362
1363;;; Binding control structures.
1364
ebacfcc6 1365;;;###autoload
fcd73769 1366(defmacro progv (symbols values &rest body)
64a4c526 1367 "Bind SYMBOLS to VALUES dynamically in BODY.
fcd73769 1368The forms SYMBOLS and VALUES are evaluated, and must evaluate to lists.
3187ba1c 1369Each symbol in the first list is bound to the corresponding value in the
fcd73769
RS
1370second list (or made unbound if VALUES is shorter than SYMBOLS); then the
1371BODY forms are executed and their result is returned. This is much like
1372a `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.
ebacfcc6 1379;;;###autoload
fcd73769 1380(defmacro flet (bindings &rest body)
3187ba1c 1381 "Make temporary function definitions.
fcd73769
RS
1382This is an analogue of `let' that operates on the function cell of FUNC
1383rather than its value cell. The FORMs are evaluated with the specified
1384function definitions in place, then the definitions are undone (the FUNCs
69d8fb1e
SM
1385go back to their previous definitions, or lack thereof).
1386
1387\(fn ((FUNC ARGLIST BODY...) ...) FORM...)"
fcd73769
RS
1388 (list* 'letf*
1389 (mapcar
1390 (function
1391 (lambda (x)
36f0f2b1
RS
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"))
fcd73769
RS
1396 (let ((func (list 'function*
1397 (list 'lambda (cadr x)
1398 (list* 'block (car x) (cddr x))))))
0ee35e51
GM
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' \
1403will not work - use `labels' instead" (symbol-name (car x))))
a54fdddb
GM
1404 ;; FIXME This affects the rest of the file, when it
1405 ;; should be restricted to the flet body.
0ee35e51
GM
1406 (and (boundp 'byte-compile-function-environment)
1407 (push (cons (car x) (eval func))
1408 byte-compile-function-environment)))
fcd73769
RS
1409 (list (list 'symbol-function (list 'quote (car x))) func))))
1410 bindings)
1411 body))
1412
ebacfcc6 1413;;;###autoload
36f0f2b1 1414(defmacro labels (bindings &rest body)
3187ba1c 1415 "Make temporary function bindings.
36f0f2b1 1416This is like `flet', except the bindings are lexical instead of dynamic.
69d8fb1e
SM
1417Unlike `flet', this macro is fully compliant with the Common Lisp standard.
1418
1419\(fn ((FUNC ARGLIST BODY...) ...) FORM...)"
36f0f2b1
RS
1420 (let ((vars nil) (sets nil) (cl-macro-environment cl-macro-environment))
1421 (while bindings
443b961a
SM
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--")))
69d8fb1e
SM
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)
36f0f2b1
RS
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)))
fcd73769
RS
1435
1436;; The following ought to have a better definition for use with newer
1437;; byte compilers.
ebacfcc6 1438;;;###autoload
fcd73769 1439(defmacro macrolet (bindings &rest body)
4342e957 1440 "Make temporary macro definitions.
69d8fb1e
SM
1441This is like `flet', but for macros instead of functions.
1442
1443\(fn ((NAME ARGLIST BODY...) ...) FORM...)"
fcd73769
RS
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
ebacfcc6 1455;;;###autoload
fcd73769 1456(defmacro symbol-macrolet (bindings &rest body)
4342e957 1457 "Make symbol macro definitions.
fcd73769 1458Within the body FORMs, references to the variable NAME will be replaced
69d8fb1e
SM
1459by EXPANSION, and (setq NAME ...) will act like (setf EXPANSION ...).
1460
1461\(fn ((NAME EXPANSION) ...) FORM...)"
fcd73769
RS
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)
ebacfcc6 1472;;;###autoload
fcd73769 1473(defmacro lexical-let (bindings &rest body)
64a4c526 1474 "Like `let', but lexically scoped.
fcd73769 1475The main visible difference is that lambdas inside BODY will create
3187ba1c 1476lexical closures as in Common Lisp.
414dbb00 1477\n(fn BINDINGS BODY)"
fcd73769
RS
1478 (let* ((cl-closure-vars cl-closure-vars)
1479 (vars (mapcar (function
1480 (lambda (x)
1481 (or (consp x) (setq x (list x)))
e542ea4b
SM
1482 (push (make-symbol (format "--cl-%s--" (car x)))
1483 cl-closure-vars)
70a036cf 1484 (set (car cl-closure-vars) [bad-lexical-ref])
fcd73769
RS
1485 (list (car x) (cadr x) (car cl-closure-vars))))
1486 bindings))
a1506d29 1487 (ebody
fcd73769
RS
1488 (cl-macroexpand-all
1489 (cons 'progn body)
1490 (nconc (mapcar (function (lambda (x)
1491 (list (symbol-name (car x))
0d42eb3e 1492 (list 'symbol-value (caddr x))
fcd73769
RS
1493 t))) vars)
1494 (list '(defun . cl-defun-expander))
1495 cl-macro-environment))))
1496 (if (not (get (car (last cl-closure-vars)) 'used))
0d42eb3e
SM
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)))
fcd73769
RS
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
ebacfcc6 1522;;;###autoload
fcd73769 1523(defmacro lexical-let* (bindings &rest body)
64a4c526 1524 "Like `let*', but lexically scoped.
143770f2 1525The main visible difference is that lambdas inside BODY, and in
414dbb00 1526successive bindings within BINDINGS, will create lexical closures
143770f2
CY
1527as in Common Lisp. This is similar to the behavior of `let*' in
1528Common Lisp.
414dbb00 1529\n(fn BINDINGS BODY)"
fcd73769
RS
1530 (if (null bindings) (cons 'progn body)
1531 (setq bindings (reverse bindings))
1532 (while bindings
69d8fb1e 1533 (setq body (list (list* 'lexical-let (list (pop bindings)) body))))
fcd73769
RS
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
ebacfcc6 1545;;;###autoload
fcd73769 1546(defmacro multiple-value-bind (vars form &rest body)
69d8fb1e 1547 "Collect multiple return values.
fcd73769
RS
1548FORM must return a list; the BODY is then executed with the first N elements
1549of this list bound (`let'-style) to each of the symbols SYM in turn. This
1550is analogous to the Common Lisp `multiple-value-bind' macro, using lists to
1551simulate true multiple return values. For compatibility, (values A B C) is
69d8fb1e
SM
1552a synonym for (list A B C).
1553
3187ba1c 1554\(fn (SYM...) FORM BODY)"
e542ea4b 1555 (let ((temp (make-symbol "--cl-var--")) (n -1))
fcd73769
RS
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
ebacfcc6 1563;;;###autoload
fcd73769 1564(defmacro multiple-value-setq (vars form)
69d8fb1e 1565 "Collect multiple return values.
fcd73769
RS
1566FORM must return a list; the first N elements of this list are stored in
1567each 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
69d8fb1e
SM
1569values. For compatibility, (values A B C) is a synonym for (list A B C).
1570
3187ba1c 1571\(fn (SYM...) FORM)"
fcd73769
RS
1572 (cond ((null vars) (list 'progn form nil))
1573 ((null (cdr vars)) (list 'setq (car vars) (list 'car form)))
1574 (t
e542ea4b 1575 (let* ((temp (make-symbol "--cl-var--")) (n 0))
fcd73769 1576 (list 'let (list (list temp form))
69d8fb1e 1577 (list 'prog1 (list 'setq (pop vars) (list 'car temp))
fcd73769
RS
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
ebacfcc6 1590;;;###autoload
fcd73769 1591(defmacro locally (&rest body) (cons 'progn body))
ebacfcc6 1592;;;###autoload
fcd73769
RS
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)
69d8fb1e 1599 (and hist (listp cl-proclaim-history) (push spec cl-proclaim-history))
fcd73769
RS
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))
fcd73769
RS
1630 (while (setq spec (cdr spec))
1631 (if (consp (car spec))
1632 (if (eq (cadar spec) 0)
bc8ce89b
GM
1633 (byte-compile-disable-warning (caar spec))
1634 (byte-compile-enable-warning (caar spec)))))))
fcd73769
RS
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)))
69d8fb1e 1640 (while p (cl-do-proclaim (pop p) t))
fcd73769
RS
1641 (setq cl-proclaims-deferred nil))
1642
ebacfcc6 1643;;;###autoload
fcd73769 1644(defmacro declare (&rest specs)
c39da690 1645 "Declare SPECS about the current function while compiling.
4bf0979f
LMI
1646For instance
1647
1648 \(declare (warn 0))
1649
dbc44fcd
LMI
1650will turn off byte-compile warnings in the function.
1651See Info node `(cl)Declarations' for details."
fcd73769
RS
1652 (if (cl-compiling-file)
1653 (while specs
69d8fb1e
SM
1654 (if (listp cl-declare-stack) (push (car specs) cl-declare-stack))
1655 (cl-do-proclaim (pop specs) nil)))
fcd73769
RS
1656 nil)
1657
1658
1659
1660;;; Generalized variables.
1661
ebacfcc6 1662;;;###autoload
fcd73769 1663(defmacro define-setf-method (func args &rest body)
69d8fb1e 1664 "Define a `setf' method.
fcd73769
RS
1665This method shows how to handle `setf's to places of the form (NAME ARGS...).
1666The argument forms ARGS are bound according to ARGLIST, as if NAME were
1667going to be expanded as a macro, then the BODY forms are executed and must
1668return a list of five elements: a temporary-variables list, a value-forms
1669list, a store-variables list (of length one), a store-form, and an access-
69d8fb1e
SM
1670form. See `defsetf' for a simpler way to define most setf-methods.
1671
1672\(fn NAME ARGLIST BODY...)"
fcd73769
RS
1673 (append '(eval-when (compile load eval))
1674 (if (stringp (car body))
1675 (list (list 'put (list 'quote func) '(quote setf-documentation)
69d8fb1e 1676 (pop body))))
fcd73769
RS
1677 (list (cl-transform-function-property
1678 func 'setf-method (cons args body)))))
6a78f30f 1679(defalias 'define-setf-expander 'define-setf-method)
fcd73769 1680
ebacfcc6 1681;;;###autoload
fcd73769 1682(defmacro defsetf (func arg1 &rest args)
d0aa0542 1683 "Define a `setf' method.
fcd73769
RS
1684This macro is an easy-to-use substitute for `define-setf-method' that works
1685well for simple place forms. In the simple `defsetf' form, `setf's of
1686the form (setf (NAME ARGS...) VAL) are transformed to function or macro
358e4d6d
JB
1687calls of the form (FUNC ARGS... VAL). Example:
1688
1689 (defsetf aref aset)
1690
fcd73769
RS
1691Alternate form: (defsetf NAME ARGLIST (STORE) BODY...).
1692Here, the above `setf' call is expanded by binding the argument forms ARGS
1693according to ARGLIST, binding the value form VAL to STORE, then executing
1694BODY, which must return a Lisp form that does the necessary `setf' operation.
1695Actually, ARGLIST and STORE may be bound to temporary variables which are
1696introduced automatically to preserve proper execution order of the arguments.
358e4d6d
JB
1697Example:
1698
1699 (defsetf nth (n x) (v) (list 'setcar (list 'nthcdr n x) v))
1700
1701\(fn NAME [FUNC | ARGLIST (STORE) BODY...])"
cb7d9531 1702 (if (and (listp arg1) (consp args))
fcd73769
RS
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
e542ea4b
SM
1729 (setq lets1 (cons `(,(car p2)
1730 (make-symbol ,(format "--cl-%s--" (car p1))))
fcd73769
RS
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)))
e542ea4b
SM
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)))))
fcd73769
RS
1764
1765;;; Some standard place types from Common Lisp.
1766(defsetf aref aset)
1767(defsetf car setcar)
1768(defsetf cdr setcdr)
c382fb0a
GM
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))
fcd73769
RS
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))
69d8fb1e 1778(defsetf gethash (x h &optional d) (store) (list 'puthash x store h))
fcd73769
RS
1779(defsetf nth (n x) (store) (list 'setcar (list 'nthcdr n x) store))
1780(defsetf subseq (seq start &optional end) (new)
64a4c526 1781 (list 'progn (list 'replace seq new :start1 start :end1 end) new))
fcd73769
RS
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)
8146c81d
RS
1801(defsetf buffer-modified-p (&optional buf) (flag)
1802 (list 'with-current-buffer buf
1803 (list 'set-buffer-modified-p flag)))
fcd73769
RS
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)
fcd73769
RS
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)
a15b3f43 1831(defsetf frame-parameter set-frame-parameter t)
f44379e7 1832(defsetf terminal-parameter set-terminal-parameter)
fcd73769
RS
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)
cdef3323 1841(defsetf match-data set-match-data t)
fcd73769
RS
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)
7bd4aefb 1859(defsetf process-get process-put)
fcd73769
RS
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)
7bce8510 1876(defsetf window-parameter set-window-parameter)
fcd73769
RS
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))
fcd73769
RS
1881(defsetf x-get-secondary-selection x-own-secondary-selection t)
1882(defsetf x-get-selection x-own-selection t)
1883
f44379e7
SM
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.
80ac5d4d
SM
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))))
f44379e7 1899
fcd73769 1900;;; More complex setf-methods.
f44379e7
SM
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.
fcd73769
RS
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))
e542ea4b
SM
1924 (n-temp (make-symbol "--cl-nthcdr-n--"))
1925 (store-temp (make-symbol "--cl-nthcdr-store--")))
fcd73769
RS
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))
e542ea4b
SM
1937 (tag-temp (make-symbol "--cl-getf-tag--"))
1938 (def-temp (make-symbol "--cl-getf-def--"))
1939 (store-temp (make-symbol "--cl-getf-store--")))
fcd73769
RS
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))
e542ea4b
SM
1951 (from-temp (make-symbol "--cl-substring-from--"))
1952 (to-temp (make-symbol "--cl-substring-to--"))
1953 (store-temp (make-symbol "--cl-substring-store--")))
fcd73769
RS
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.
ebacfcc6 1964;;;###autoload
fcd73769
RS
1965(defun get-setf-method (place &optional env)
1966 "Return a list of five values describing the setf-method for PLACE.
1967PLACE may be any Lisp form which can appear as the PLACE argument to
1968a macro like `setf' or `incf'."
1969 (if (symbolp place)
e542ea4b 1970 (let ((temp (make-symbol "--cl-setf--")))
fcd73769
RS
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)))
20e3d3f1 1984 (and (string-match-p "\\`c[ad][ad][ad]?[ad]?r\\'" name)
fcd73769
RS
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)))
69d8fb1e
SM
2009 (push (cons (pop temps) (pop values)) subs)
2010 (push (list (pop temps) (pop values)) lets)))
fcd73769
RS
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.
ebacfcc6 2031;;;###autoload
fcd73769 2032(defmacro setf (&rest args)
69d8fb1e 2033 "Set each PLACE to the value of its VAL.
fcd73769
RS
2034This is a generalized version of `setq'; the PLACEs may be symbolic
2035references such as (car x) or (aref x i), as well as plain symbols.
2036For example, (setf (cadar x) y) is equivalent to (setcar (cdar x) y).
69d8fb1e
SM
2037The return value is the last VAL in the list.
2038
2039\(fn PLACE VAL PLACE VAL ...)"
fcd73769
RS
2040 (if (cdr (cdr args))
2041 (let ((sets nil))
69d8fb1e 2042 (while args (push (list 'setf (pop args) (pop args)) sets))
fcd73769
RS
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
ebacfcc6 2050;;;###autoload
fcd73769 2051(defmacro psetf (&rest args)
69d8fb1e 2052 "Set PLACEs to the values VALs in parallel.
fcd73769 2053This is like `setf', except that all VAL forms are evaluated (in order)
69d8fb1e
SM
2054before assigning any PLACEs to the corresponding values.
2055
2056\(fn PLACE VAL PLACE VAL ...)"
fcd73769
RS
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)))
69d8fb1e 2063 (push (pop p) vars)
fcd73769 2064 (or p (error "Odd number of arguments to psetf"))
69d8fb1e 2065 (pop p))
fcd73769
RS
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
ebacfcc6 2074;;;###autoload
fcd73769
RS
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))
e542ea4b 2079 (temp (make-symbol "--cl-pop--")))
fcd73769
RS
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
ebacfcc6 2087;;;###autoload
fcd73769 2088(defmacro remf (place tag)
64a4c526 2089 "Remove TAG from property list PLACE.
fcd73769
RS
2090PLACE may be a symbol, or any generalized variable allowed by `setf'.
2091The form returns true if TAG was found and removed, nil otherwise."
2092 (let* ((method (cl-setf-do-modify place t))
e542ea4b 2093 (tag-temp (and (not (cl-const-expr-p tag)) (make-symbol "--cl-remf-tag--")))
fcd73769 2094 (val-temp (and (not (cl-simple-expr-p place))
e542ea4b 2095 (make-symbol "--cl-remf-place--")))
fcd73769
RS
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
ebacfcc6 2108;;;###autoload
fcd73769 2109(defmacro shiftf (place &rest args)
69d8fb1e 2110 "Shift left among PLACEs.
fcd73769 2111Example: (shiftf A B C) sets A to B, B to C, and returns the old A.
69d8fb1e
SM
2112Each PLACE may be a symbol, or any generalized variable allowed by `setf'.
2113
3187ba1c 2114\(fn PLACE... VAL)"
2fa5eef4
SM
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))))))))
fcd73769 2123
ebacfcc6 2124;;;###autoload
fcd73769 2125(defmacro rotatef (&rest args)
69d8fb1e 2126 "Rotate left among PLACEs.
fcd73769 2127Example: (rotatef A B C) sets A to B, B to C, and C to A. It returns nil.
69d8fb1e
SM
2128Each PLACE may be a symbol, or any generalized variable allowed by `setf'.
2129
2130\(fn PLACE...)"
fcd73769
RS
2131 (if (not (memq nil (mapcar 'symbolp args)))
2132 (and (cdr args)
2133 (let ((sets nil)
2134 (first (car args)))
2135 (while (cdr args)
69d8fb1e 2136 (setq sets (nconc sets (list (pop args) (car args)))))
fcd73769
RS
2137 (nconc (list 'psetf) sets (list (car args) first))))
2138 (let* ((places (reverse args))
e542ea4b 2139 (temp (make-symbol "--cl-rotatef--"))
fcd73769
RS
2140 (form temp))
2141 (while (cdr places)
69d8fb1e 2142 (let ((method (cl-setf-do-modify (pop places) 'unsafe)))
fcd73769
RS
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
ebacfcc6 2150;;;###autoload
fcd73769 2151(defmacro letf (bindings &rest body)
69d8fb1e 2152 "Temporarily bind to PLACEs.
fcd73769
RS
2153This is the analogue of `let', but with generalized variables (in the
2154sense of `setf') for the PLACEs. Each PLACE is set to the corresponding
2155VALUE, then the BODY forms are executed. On exit, either normally or
2156because of a `throw' or error, the PLACEs are set back to their original
2157values. Note that this macro is *not* available in Common Lisp.
2158As a special case, if `(PLACE)' is used instead of `(PLACE VALUE)',
69d8fb1e
SM
2159the PLACE is not modified before executing BODY.
2160
2161\(fn ((PLACE VALUE) ...) BODY...)"
fcd73769
RS
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))
e542ea4b 2172 (save (make-symbol "--cl-letf-save--"))
fcd73769 2173 (bound (and (memq (car place) '(symbol-value symbol-function))
e542ea4b 2174 (make-symbol "--cl-letf-bound--")))
fcd73769 2175 (temp (and (not (cl-const-expr-p value)) (cdr bindings)
e542ea4b 2176 (make-symbol "--cl-letf-val--"))))
fcd73769
RS
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
ebacfcc6 2207;;;###autoload
fcd73769 2208(defmacro letf* (bindings &rest body)
69d8fb1e 2209 "Temporarily bind to PLACEs.
fcd73769
RS
2210This is the analogue of `let*', but with generalized variables (in the
2211sense of `setf') for the PLACEs. Each PLACE is set to the corresponding
2212VALUE, then the BODY forms are executed. On exit, either normally or
2213because of a `throw' or error, the PLACEs are set back to their original
2214values. Note that this macro is *not* available in Common Lisp.
2215As a special case, if `(PLACE)' is used instead of `(PLACE VALUE)',
69d8fb1e
SM
2216the PLACE is not modified before executing BODY.
2217
2218\(fn ((PLACE VALUE) ...) BODY...)"
fcd73769
RS
2219 (if (null bindings)
2220 (cons 'progn body)
2221 (setq bindings (reverse bindings))
2222 (while bindings
69d8fb1e 2223 (setq body (list (list* 'letf (list (pop bindings)) body))))
fcd73769
RS
2224 (car body)))
2225
ebacfcc6 2226;;;###autoload
fcd73769 2227(defmacro callf (func place &rest args)
69d8fb1e 2228 "Set PLACE to (FUNC PLACE ARGS...).
fcd73769 2229FUNC should be an unquoted function name. PLACE may be a symbol,
69d8fb1e
SM
2230or any generalized variable allowed by `setf'.
2231
2232\(fn FUNC PLACE ARGS...)"
fcd73769
RS
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
ebacfcc6 2241;;;###autoload
fcd73769 2242(defmacro callf2 (func arg1 place &rest args)
69d8fb1e
SM
2243 "Set PLACE to (FUNC ARG1 PLACE ARGS...).
2244Like `callf', but PLACE is the second argument of FUNC, not the first.
2245
2246\(fn FUNC ARG1 PLACE ARGS...)"
fcd73769
RS
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)))
e542ea4b 2250 (temp (and (not (cl-const-expr-p arg1)) (make-symbol "--cl-arg1--")))
fcd73769
RS
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
ebacfcc6 2258;;;###autoload
fcd73769 2259(defmacro define-modify-macro (name arglist func &optional doc)
64a4c526 2260 "Define a `setf'-like modify macro.
fcd73769
RS
2261If NAME is called, it combines its PLACE argument with the other arguments
2262from ARGLIST using FUNC: (define-modify-macro incf (&optional (n 1)) +)"
2263 (if (memq '&key arglist) (error "&key not allowed in define-modify-macro"))
e542ea4b 2264 (let ((place (make-symbol "--cl-place--")))
fcd73769
RS
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
ebacfcc6 2273;;;###autoload
fcd73769 2274(defmacro defstruct (struct &rest descs)
69d8fb1e 2275 "Define a struct type.
c7dc1ac1
CY
2276This macro defines a new data type called NAME that stores data
2277in SLOTs. It defines a `make-NAME' constructor, a `copy-NAME'
2278copier, a `NAME-p' predicate, and slot accessors named `NAME-SLOT'.
2279You can use the accessors to set the corresponding slots, via `setf'.
69d8fb1e 2280
c7dc1ac1
CY
2281NAME may instead take the form (NAME OPTIONS...), where each
2282OPTION is either a single keyword or (KEYWORD VALUE).
2283See Info node `(cl)Structures' for a list of valid keywords.
2284
2285Each SLOT may instead take the form (SLOT SLOT-OPTS...), where
2286SLOT-OPTS are keyword-value pairs for that slot. Currently, only
2287one keyword is supported, `:read-only'. If this has a non-nil
2288value, that slot cannot be set via `setf'.
2289
2290\(fn NAME SLOTS...)"
fcd73769
RS
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)
fcd73769
RS
2306 (side-eff nil)
2307 (type nil)
2308 (named nil)
2309 (forms nil)
2310 pred-form pred-check)
2311 (if (stringp (car descs))
69d8fb1e
SM
2312 (push (list 'put (list 'quote name) '(quote structure-documentation)
2313 (pop descs)) forms))
fcd73769
RS
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)))
69d8fb1e 2319 (args (cdr-safe (pop opts))))
64a4c526 2320 (cond ((eq opt :conc-name)
fcd73769
RS
2321 (if args
2322 (setq conc-name (if (car args)
2323 (symbol-name (car args)) ""))))
64a4c526 2324 ((eq opt :constructor)
fcd73769 2325 (if (cdr args)
40e8a6c3
SM
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))
fcd73769 2332 (if args (setq constructor (car args)))))
64a4c526 2333 ((eq opt :copier)
fcd73769 2334 (if args (setq copier (car args))))
64a4c526 2335 ((eq opt :predicate)
fcd73769 2336 (if args (setq predicate (car args))))
64a4c526 2337 ((eq opt :include)
fcd73769
RS
2338 (setq include (car args)
2339 include-descs (mapcar (function
2340 (lambda (x)
2341 (if (consp x) x (list x))))
36f0f2b1 2342 (cdr args))))
64a4c526 2343 ((eq opt :print-function)
fcd73769 2344 (setq print-func (car args)))
64a4c526 2345 ((eq opt :type)
fcd73769 2346 (setq type (car args)))
64a4c526 2347 ((eq opt :named)
fcd73769 2348 (setq named t))
64a4c526 2349 ((eq opt :initial-offset)
fcd73769
RS
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)
69d8fb1e 2375 (pop include-descs)))
fcd73769
RS
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))
36f0f2b1
RS
2380 (let ((incl include))
2381 (while incl
69d8fb1e 2382 (push (list 'pushnew (list 'quote tag)
36f0f2b1
RS
2383 (intern (format "cl-struct-%s-tags" incl)))
2384 forms)
2385 (setq incl (get incl 'cl-struct-include)))))
fcd73769
RS
2386 (if type
2387 (progn
2388 (or (memq type '(vector list))
4920bd1e 2389 (error "Invalid :type specifier: %s" type))
fcd73769
RS
2390 (if named (setq tag name)))
2391 (setq type 'vector named 'true)))
2392 (or named (setq descs (delq (assq 'cl-tag-slot descs) descs)))
69d8fb1e 2393 (push (list 'defvar tag-symbol) forms)
fcd73769
RS
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
69d8fb1e 2414 (let* ((desc (pop descp))
fcd73769
RS
2415 (slot (car desc)))
2416 (if (memq slot '(cl-tag-slot cl-skip-slot))
2417 (progn
69d8fb1e
SM
2418 (push nil slots)
2419 (push (and (eq slot 'cl-tag-slot) (list 'quote tag))
fcd73769
RS
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))))
69d8fb1e
SM
2424 (push slot slots)
2425 (push (nth 1 desc) defaults)
2426 (push (list*
fcd73769
RS
2427 'defsubst* accessor '(cl-x)
2428 (append
2429 (and pred-check
2430 (list (list 'or pred-check
f8ccf167
LL
2431 `(error "%s accessing a non-%s"
2432 ',accessor ',name))))
fcd73769
RS
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)
69d8fb1e
SM
2436 (push (cons accessor t) side-eff)
2437 (push (list 'define-setf-method accessor '(cl-x)
64a4c526 2438 (if (cadr (memq :read-only (cddr desc)))
0794775d 2439 (list 'progn '(ignore cl-x)
f8ccf167
LL
2440 `(error "%s is a read-only slot"
2441 ',accessor))
b9b14938
RS
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))))
fcd73769
RS
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
69d8fb1e 2460 (progn (push (list 'defsubst* predicate '(cl-x)
fcd73769
RS
2461 (if (eq (car pred-form) 'and)
2462 (append pred-form '(t))
2463 (list 'and pred-form t))) forms)
69d8fb1e 2464 (push (cons predicate 'error-free) side-eff)))
fcd73769 2465 (and copier
69d8fb1e
SM
2466 (progn (push (list 'defun copier '(x) '(copy-sequence x)) forms)
2467 (push (cons copier t) side-eff)))
fcd73769 2468 (if constructor
69d8fb1e 2469 (push (list constructor
fcd73769
RS
2470 (cons '&key (delq nil (copy-sequence slots))))
2471 constrs))
2472 (while constrs
2473 (let* ((name (caar constrs))
69d8fb1e 2474 (args (cadr (pop constrs)))
fcd73769
RS
2475 (anames (cl-arglist-args args))
2476 (make (mapcar* (function (lambda (s d) (if (memq s anames) s d)))
2477 slots defaults)))
69d8fb1e 2478 (push (list 'defsubst* name
fcd73769
RS
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)))
69d8fb1e 2482 (push (cons name t) side-eff))))
fcd73769
RS
2483 (if print-auto (nconc print-func (list '(princ ")" cl-s) t)))
2484 (if print-func
ba83908c
SM
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))
69d8fb1e
SM
2492 (push (list 'setq tag-symbol (list 'list (list 'quote tag))) forms)
2493 (push (list* 'eval-when '(compile load eval)
fcd73769
RS
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))))
36f0f2b1
RS
2498 (list 'put (list 'quote name) '(quote cl-struct-include)
2499 (list 'quote include))
fcd73769
RS
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
ebacfcc6 2510;;;###autoload
fcd73769 2511(defun cl-struct-setf-expander (x name accessor pred-form pos)
e542ea4b 2512 (let* ((temp (make-symbol "--cl-x--")) (store (make-symbol "--cl-store--")))
fcd73769
RS
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
6b61353c 2519 "%s storing a non-%s" accessor name)))))
fcd73769
RS
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
15120dec 2535;;;###autoload
64a4c526
DL
2536(defmacro deftype (name arglist &rest body)
2537 "Define NAME as a new data type.
fcd73769
RS
2538The type name can then be used in `typecase', `check-type', etc."
2539 (list 'eval-when '(compile load eval)
2540 (cl-transform-function-property
64a4c526 2541 name 'cl-deftype-handler (cons (list* '&cl-defs ''('*) arglist) body))))
fcd73769
RS
2542
2543(defun cl-make-type-test (val type)
fcd73769
RS
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)
e0b16322 2548 ((eq type 'null) `(null ,val))
578f8106 2549 ((eq type 'atom) `(atom ,val))
e0b16322
SM
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
31ff0ac1 2554 ((memq type '(character string-char)) `(characterp ,val))
fcd73769
RS
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))))
e0b16322 2563 ((memq (car type) '(integer float real number))
cb35e559 2564 (delq t (list 'and (cl-make-type-test val (car type))
fcd73769
RS
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)))))))
e0b16322 2571 ((memq (car type) '(and or not))
fcd73769
RS
2572 (cons (car type)
2573 (mapcar (function (lambda (x) (cl-make-type-test val x)))
2574 (cdr type))))
e0b16322 2575 ((memq (car type) '(member member*))
fcd73769 2576 (list 'and (list 'member* val (list 'quote (cdr type))) t))
e0b16322 2577 ((eq (car type) 'satisfies) (list (cadr type) val))
fcd73769
RS
2578 (t (error "Bad type spec: %s" type)))))
2579
ebacfcc6 2580;;;###autoload
a766dfa1 2581(defun typep (object type) ; See compiler macro below.
fcd73769
RS
2582 "Check that OBJECT is of type TYPE.
2583TYPE is a Common Lisp-style type specifier."
a766dfa1 2584 (eval (cl-make-type-test 'object type)))
fcd73769 2585
ebacfcc6 2586;;;###autoload
fcd73769
RS
2587(defmacro check-type (form type &optional string)
2588 "Verify that FORM is of type TYPE; signal an error if not.
2589STRING is an optional description of the desired type."
2590 (and (or (not (cl-compiling-file))
2591 (< cl-optimize-speed 3) (= cl-optimize-safety 3))
e542ea4b
SM
2592 (let* ((temp (if (cl-simple-expr-p form 3)
2593 form (make-symbol "--cl-var--")))
fcd73769
RS
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
ebacfcc6 2601;;;###autoload
fcd73769
RS
2602(defmacro assert (form &optional show-args string &rest args)
2603 "Verify that FORM returns non-nil; signal an error if not.
2604Second arg SHOW-ARGS means to include arguments of FORM in message.
2605Other args STRING and ARGS... are arguments to be passed to `error'.
2606They are not evaluated unless the assertion fails. If STRING is
2607omitted, a default message listing FORM itself is used."
2608 (and (or (not (cl-compiling-file))
2609 (< cl-optimize-speed 3) (= cl-optimize-safety 3))
2b88d62a
RS
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))))))
fcd73769
RS
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
fcd73769
RS
2624;;; Compiler macros.
2625
ebacfcc6 2626;;;###autoload
fcd73769 2627(defmacro define-compiler-macro (func args &rest body)
64a4c526 2628 "Define a compiler-only macro.
fcd73769
RS
2629This is like `defmacro', but macro expansion occurs only if the call to
2630FUNC is compiled (i.e., not interpreted). Compiler macros should be used
2631for optimizing the way calls to FUNC are compiled; the form returned by
2632BODY should do the same thing as a call to the normal function called
2633FUNC, though possibly more efficiently. Note that, like regular macros,
2634compiler macros are expanded repeatedly until no further expansions are
2635possible. Unlike regular macros, BODY can decide to \"punt\" and leave the
2636original function call alone by declaring an initial `&whole foo' parameter
2637and then returning foo."
16c9c10f 2638 (let ((p args) (res nil))
69d8fb1e 2639 (while (consp p) (push (pop p) res))
16c9c10f 2640 (setq args (nconc (nreverse res) (and p (list '&rest p)))))
fcd73769
RS
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)
ba83908c 2645 (cons '_cl-whole-arg args)) body))
fcd73769 2646 (list 'or (list 'get (list 'quote func) '(quote byte-compile))
ae2fd042
GM
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)
76410c3e 2662 '(purecopy (file-name-nondirectory file)))))))))
fcd73769 2663
ebacfcc6 2664;;;###autoload
fcd73769
RS
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
414dbb00
SM
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)
a08a25d7 2696 `(catch ,(nth 1 cl-form) ,@(cdr cl-body))
414dbb00
SM
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
eb123b12 2704;;;###autoload
fcd73769 2705(defmacro defsubst* (name args &rest body)
69d8fb1e 2706 "Define NAME as a function.
fcd73769
RS
2707Like `defun', except the function is automatically declared `inline',
2708ARGLIST allows full Common Lisp conventions, and BODY is implicitly
69d8fb1e
SM
2709surrounded by (block NAME ...).
2710
2711\(fn NAME ARGLIST [DOCSTRING] BODY...)"
fcd73769
RS
2712 (let* ((argns (cl-arglist-args args)) (p argns)
2713 (pbody (cons 'progn body))
2714 (unsafe (not (cl-safe-expr-p pbody))))
69d8fb1e 2715 (while (and p (eq (cl-expr-contains args (car p)) 1)) (pop p))
fcd73769
RS
2716 (list 'progn
2717 (if p nil ; give up if defaults refer to earlier args
2718 (list 'define-compiler-macro name
cc1084a8
SM
2719 (if (memq '&key args)
2720 (list* '&whole 'cl-whole '&cl-quote args)
2721 (cons '&cl-quote args))
fcd73769
RS
2722 (list* 'cl-defsubst-expand (list 'quote argns)
2723 (list 'quote (list* 'block name body))
e754e83b
SM
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
fcd73769
RS
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))
e754e83b
SM
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))))
fcd73769
RS
2754 (if lets (list 'let lets body) body))))
2755
2756
9b77469a
SM
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.
fcd73769
RS
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)
64a4c526 2785 (let ((test (and (= (length keys) 2) (eq (car keys) :test)
fcd73769
RS
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))
eae51075 2789 ((or (null keys) (eq test 'eql)) (list 'memql a list))
fcd73769
RS
2790 (t form))))
2791
2792(define-compiler-macro assoc* (&whole form a list &rest keys)
64a4c526 2793 (let ((test (and (= (length keys) 2) (eq (car keys) :test)
fcd73769
RS
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)
64a4c526 2804 (not (memq :key keys)))
fcd73769
RS
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
e542ea4b 2825 (let ((temp (make-symbol "--cl-var--")))
fcd73769
RS
2826 (list 'let (list (list temp val)) (subst temp val res)))))
2827 form))
2828
2829
e542ea4b
SM
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) ))
fcd73769
RS
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.
e542ea4b
SM
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))
fcd73769
RS
2861
2862;;; Things that are side-effect-and-error-free.
e542ea4b
SM
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))
fcd73769
RS
2866
2867
2868(run-hooks 'cl-macs-load-hook)
2869
ebacfcc6 2870;; Local variables:
bc8ce89b
GM
2871;; byte-compile-dynamic: t
2872;; byte-compile-warnings: (not cl-functions)
ebacfcc6
SM
2873;; generated-autoload-file: "cl-loaddefs.el"
2874;; End:
b69a3374 2875
fcd73769 2876;;; cl-macs.el ends here