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