1 ;;; cl-macs.el --- Common Lisp extensions for GNU Emacs Lisp (part four)
3 ;; Copyright (C) 1993 Free Software Foundation, Inc.
4 ;; Copyright (C) 2002 Ben Wing.
6 ;; Author: Dave Gillespie <daveg@synaptics.com>
8 ;; Keywords: extensions
10 ;; This file is part of SXEmacs.
12 ;; SXEmacs 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 ;; SXEmacs 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 this program. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Synched up with: FSF 19.34.
29 ;; These are extensions to Emacs Lisp that provide a degree of
30 ;; Common Lisp compatibility, beyond what is already built-in
33 ;; This package was written by Dave Gillespie; it is a complete
34 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
36 ;; This package works with Emacs 18, Emacs 19, and Lucid Emacs 19.
38 ;; Bug reports, comments, and suggestions are welcome!
40 ;; This file contains the portions of the Common Lisp extensions
41 ;; package which should be autoloaded, but need only be present
42 ;; if the compiler or interpreter is used---this file is not
43 ;; necessary for executing compiled code.
45 ;; See cl.el for Change Log.
50 (or (memq 'cl-19 features)
51 (error "Tried to load `cl-macs' before `cl'!"))
54 ;;; We define these here so that this file can compile without having
55 ;;; loaded the cl.el file already.
57 (defmacro cl-push (x place) (list 'setq place (list 'cons x place)))
58 (defmacro cl-pop (place)
59 (list 'car (list 'prog1 place (list 'setq place (list 'cdr place)))))
60 (defmacro cl-pop2 (place)
61 (list 'prog1 (list 'car (list 'cdr place))
62 (list 'setq place (list 'cdr (list 'cdr place)))))
63 (put 'cl-push 'edebug-form-spec 'edebug-sexps)
64 (put 'cl-pop 'edebug-form-spec 'edebug-sexps)
65 (put 'cl-pop2 'edebug-form-spec 'edebug-sexps)
67 (defvar cl-emacs-type)
68 (defvar cl-optimize-safety)
69 (defvar cl-optimize-speed)
72 ;;; This kludge allows macros which use cl-transform-function-property
73 ;;; to be called at compile-time.
77 (or (fboundp 'defalias) (fset 'defalias 'fset))
78 (or (fboundp 'cl-transform-function-property)
79 (defalias 'cl-transform-function-property
81 (list 'put (list 'quote n) (list 'quote p)
82 (list 'function (cons 'lambda f))))))
88 (defvar cl-old-bc-file-form nil)
90 ;; Patch broken Emacs 18 compiler (re top-level macros).
91 ;; Emacs 19 compiler doesn't need this patch.
92 ;; Also, undo broken definition of `eql' that uses same bytecode as `eq'.
95 (defun cl-compile-time-init ()
96 (setq cl-old-bc-file-form (symbol-function 'byte-compile-file-form))
97 (or (fboundp 'byte-compile-flush-pending) ; Emacs 19 compiler?
98 (defalias 'byte-compile-file-form
100 (setq form (macroexpand form byte-compile-macro-environment))
101 (if (eq (car-safe form) 'progn)
102 (cons 'progn (mapcar 'byte-compile-file-form (cdr form)))
103 (funcall cl-old-bc-file-form form)))))
104 (put 'eql 'byte-compile 'cl-byte-compile-compiler-macro)
105 (run-hooks 'cl-hack-bytecomp-hook))
108 ;;; Program structure.
111 (defmacro defun* (name args &rest body)
112 "(defun* NAME ARGLIST [DOCSTRING] BODY...): define NAME as a function.
113 Like normal `defun', except ARGLIST allows full Common Lisp conventions,
114 and BODY is implicitly surrounded by (block NAME ...)."
115 (let* ((res (cl-transform-lambda (cons args body) name))
116 (form (list* 'defun name (cdr res))))
117 (if (car res) (list 'progn (car res) form) form)))
120 (defmacro defmacro* (name args &rest body)
121 "(defmacro* NAME ARGLIST [DOCSTRING] BODY...): define NAME as a macro.
122 Like normal `defmacro', except ARGLIST allows full Common Lisp conventions,
123 and BODY is implicitly surrounded by (block NAME ...)."
124 (let* ((res (cl-transform-lambda (cons args body) name))
125 (form (list* 'defmacro name (cdr res))))
126 (if (car res) (list 'progn (car res) form) form)))
129 (defmacro function* (func)
130 "(function* SYMBOL-OR-LAMBDA): introduce a function.
131 Like normal `function', except that if argument is a lambda form, its
132 ARGLIST allows full Common Lisp conventions."
133 (if (eq (car-safe func) 'lambda)
134 (let* ((res (cl-transform-lambda (cdr func) 'cl-none))
135 (form (list 'function (cons 'lambda (cdr res)))))
136 (if (car res) (list 'progn (car res) form) form))
137 (list 'function func)))
139 (defun cl-transform-function-property (func prop form)
140 (let ((res (cl-transform-lambda form func)))
141 (append '(progn) (cdr (cdr (car res)))
142 (list (list 'put (list 'quote func) (list 'quote prop)
143 (list 'function (cons 'lambda (cdr res))))))))
145 (defconst lambda-list-keywords
146 '(&optional &rest &key &allow-other-keys &aux &whole &body &environment))
148 (defvar cl-macro-environment nil)
149 (defvar bind-block) (defvar bind-defs) (defvar bind-enquote)
150 (defvar bind-inits) (defvar bind-lets) (defvar bind-forms)
154 (defun cl-upcase-arg (arg)
155 ;; Changes all non-keyword symbols in `ARG' to symbols
156 ;; with name in upper case.
157 ;; ARG is either symbol or list of symbols or lists
159 ;; Do not upcase &optional, &key etc.
160 (if (memq arg lambda-list-keywords)
162 (make-symbol (upcase (symbol-name arg)))))
164 (let ((arg (copy-list arg)) junk)
166 (let ((p (last arg))) (if (cdr p) (setcdr p (list '&rest (cdr p)))))
167 (if (setq junk (cadr (memq '&cl-defs arg)))
168 (setq arg (delq '&cl-defs (delq junk arg))))
169 (if (memq '&cl-quote arg)
170 (setq arg (delq '&cl-quote arg)))
171 (mapcar 'cl-upcase-arg arg)))
172 (t arg))) ; Maybe we are in initializer
176 (defun cl-function-arglist (name arglist)
177 "Returns string with printed representation of arguments list.
178 Supports Common Lisp lambda lists."
179 (if (not (or (listp arglist) (symbolp arglist)))
181 (check-argument-type #'true-list-p arglist)
182 (let ((print-gensym nil))
185 (cons (if (eq name 'cl-none) 'lambda name)
186 (cond ((null arglist) nil)
187 ((listp arglist) (cl-upcase-arg arglist))
189 (cl-upcase-arg (list '&rest arglist)))
190 (t (wrong-type-argument 'listp arglist)))))
191 (t "Not available")))))
193 (defun cl-transform-lambda (form bind-block)
194 (let* ((args (car form)) (body (cdr form))
195 (bind-defs nil) (bind-enquote nil)
196 (bind-inits nil) (bind-lets nil) (bind-forms nil)
197 (header nil) (simple-args nil)
198 (complex-arglist (cl-function-arglist bind-block args))
200 (while (or (stringp (car body)) (eq (car-safe (car body)) 'interactive))
201 (push (pop body) header))
202 (setq args (if (listp args) (copy-list args) (list '&rest args)))
203 (let ((p (last args))) (if (cdr p) (setcdr p (list '&rest (cdr p)))))
204 (if (setq bind-defs (cadr (memq '&cl-defs args)))
205 (setq args (delq '&cl-defs (delq bind-defs args))
206 bind-defs (cadr bind-defs)))
207 (if (setq bind-enquote (memq '&cl-quote args))
208 (setq args (delq '&cl-quote args)))
209 (if (memq '&whole args) (error "&whole not currently implemented"))
210 (let* ((p (memq '&environment args)) (v (cadr p)))
211 (if p (setq args (nconc (delq (car p) (delq v args))
212 (list '&aux (list v 'cl-macro-environment))))))
213 (while (and args (symbolp (car args))
214 (not (memq (car args) '(nil &rest &body &key &aux)))
215 (not (and (eq (car args) '&optional)
216 (or bind-defs (consp (cadr args))))))
217 (push (pop args) simple-args))
218 (or (eq bind-block 'cl-none)
219 (setq body (list (list* 'block bind-block body))))
220 (setq simple-args (nreverse simple-args)
221 header (nreverse header))
222 ;; Add CL lambda list to documentation, if the CL lambda list differs
223 ;; from the non-CL lambda list. npak@ispras.ru
224 (unless (equal complex-arglist
225 (cl-function-arglist bind-block simple-args))
226 (and (stringp (car header)) (setq doc (pop header)))
228 "\n\nCommon Lisp lambda list:\n"
229 " " complex-arglist "\n\n")
232 (list* nil simple-args (nconc header body))
233 (if (memq '&optional simple-args) (push '&optional args))
234 (cl-do-arglist args nil (- (length simple-args)
235 (if (memq '&optional simple-args) 1 0)))
236 (setq bind-lets (nreverse bind-lets))
237 (list* (and bind-inits (list* 'eval-when '(compile load eval)
238 (nreverse bind-inits)))
240 (list '&rest (car (pop bind-lets))))
241 ;; XEmacs change: we add usage information using Nickolay's
244 (list (nconc (list 'let* bind-lets)
245 (nreverse bind-forms) body)))))))
247 (defun cl-do-arglist (args expr &optional num) ; uses bind-*
249 (if (or (memq args lambda-list-keywords) (not (symbolp args)))
250 (error "Invalid argument name: %s" args)
251 (cl-push (list args expr) bind-lets))
252 (setq args (copy-list args))
253 (let ((p (last args))) (if (cdr p) (setcdr p (list '&rest (cdr p)))))
254 (let ((p (memq '&body args))) (if p (setcar p '&rest)))
255 (if (memq '&environment args) (error "&environment used incorrectly"))
256 (let ((save-args args)
257 (restarg (memq '&rest args))
258 (safety (if (cl-compiling-file) cl-optimize-safety 3))
260 (laterarg nil) (exactarg nil) minarg)
261 (or num (setq num 0))
262 (if (listp (cadr restarg))
263 (setq restarg (gensym "--rest--"))
264 (setq restarg (cadr restarg)))
265 (cl-push (list restarg expr) bind-lets)
266 (if (eq (car args) '&whole)
267 (cl-push (list (cl-pop2 args) restarg) bind-lets))
269 (setq minarg restarg)
270 (while (and p (not (memq (car p) lambda-list-keywords)))
271 (or (eq p args) (setq minarg (list 'cdr minarg)))
273 (if (memq (car p) '(nil &aux))
274 (setq minarg (list '= (list 'length restarg)
275 (length (ldiff args p)))
276 exactarg (not (eq args p)))))
277 (while (and args (not (memq (car args) lambda-list-keywords)))
278 (let ((poparg (list (if (or (cdr args) (not exactarg)) 'pop 'car)
282 (if (or laterarg (= safety 0)) poparg
283 (list 'if minarg poparg
284 (list 'signal '(quote wrong-number-of-arguments)
285 (list 'list (and (not (eq bind-block 'cl-none))
286 (list 'quote bind-block))
287 (list 'length restarg)))))))
288 (setq num (1+ num) laterarg t))
289 (while (and (eq (car args) '&optional) (cl-pop args))
290 (while (and args (not (memq (car args) lambda-list-keywords)))
291 (let ((arg (cl-pop args)))
292 (or (consp arg) (setq arg (list arg)))
293 (if (cddr arg) (cl-do-arglist (nth 2 arg) (list 'and restarg t)))
294 (let ((def (if (cdr arg) (nth 1 arg)
296 (nth 1 (assq (car arg) bind-defs)))))
297 (poparg (list 'pop restarg)))
298 (and def bind-enquote (setq def (list 'quote def)))
299 (cl-do-arglist (car arg)
300 (if def (list 'if restarg poparg def) poparg))
301 (setq num (1+ num))))))
302 (if (eq (car args) '&rest)
303 (let ((arg (cl-pop2 args)))
304 (if (consp arg) (cl-do-arglist arg restarg)))
305 (or (eq (car args) '&key) (= safety 0) exactarg
306 (cl-push (list 'if restarg
307 (list 'signal '(quote wrong-number-of-arguments)
309 (and (not (eq bind-block 'cl-none))
310 (list 'quote bind-block))
311 (list '+ num (list 'length restarg)))))
313 (while (and (eq (car args) '&key) (cl-pop args))
314 (while (and args (not (memq (car args) lambda-list-keywords)))
315 (let ((arg (cl-pop args)))
316 (or (consp arg) (setq arg (list arg)))
317 (let* ((karg (if (consp (car arg)) (caar arg)
318 (intern (format ":%s" (car arg)))))
319 (varg (if (consp (car arg)) (cadar arg) (car arg)))
320 (def (if (cdr arg) (cadr arg)
321 (or (car bind-defs) (cadr (assq varg bind-defs)))))
322 (look (list 'memq (list 'quote karg) restarg)))
323 (and def bind-enquote (setq def (list 'quote def)))
325 (let* ((temp (or (nth 2 arg) (gensym)))
326 (val (list 'car (list 'cdr temp))))
327 (cl-do-arglist temp look)
330 (list 'prog1 val (list 'setq temp t))
339 (if (eq (cl-const-expr-p def) t)
342 (list nil (cl-const-expr-val def)))
343 (list 'list nil def))))))))
345 (if (= (aref (symbol-name karg) 0) ?:)
346 (progn (set karg karg)
347 (cl-push (list 'setq karg (list 'quote karg))
349 (setq keys (nreverse keys))
350 (or (and (eq (car args) '&allow-other-keys) (cl-pop args))
351 (null keys) (= safety 0)
352 (let* ((var (gensym "--keys--"))
353 (allow '(:allow-other-keys))
358 (list (list 'memq (list 'car var)
359 (list 'quote (append keys allow)))
360 (list 'setq var (list 'cdr (list 'cdr var))))
363 (list 'memq (cons 'quote allow)
365 (list 'setq var nil))
369 (format "Keyword argument %%s not one of %s"
371 (list 'car var)))))))
372 (cl-push (list 'let (list (list var restarg)) check) bind-forms)))
373 (while (and (eq (car args) '&aux) (cl-pop args))
374 (while (and args (not (memq (car args) lambda-list-keywords)))
375 (if (consp (car args))
376 (if (and bind-enquote (cadar args))
377 (cl-do-arglist (caar args)
378 (list 'quote (cadr (cl-pop args))))
379 (cl-do-arglist (caar args) (cadr (cl-pop args))))
380 (cl-do-arglist (cl-pop args) nil))))
381 (if args (error "Malformed argument list %s" save-args)))))
383 (defun cl-arglist-args (args)
384 (if (nlistp args) (list args)
385 (let ((res nil) (kind nil) arg)
387 (setq arg (cl-pop args))
388 (if (memq arg lambda-list-keywords) (setq kind arg)
389 (if (eq arg '&cl-defs) (cl-pop args)
390 (and (consp arg) kind (setq arg (car arg)))
391 (and (consp arg) (cdr arg) (eq kind '&key) (setq arg (cadr arg)))
392 (setq res (nconc res (cl-arglist-args arg))))))
393 (nconc res (and args (list args))))))
396 (defmacro destructuring-bind (args expr &rest body)
397 "Bind the arguments in ARGS to EXPR then eval BODY.
398 This is similar to `let' but it does \"destructuring\", in that it matches
399 the structure of ARGS to the structure of EXPR and binds corresponding
400 arguments in ARGS to their values in EXPR. The format of ARGS, and the
401 way the destructuring works, is exactly like the destructuring that occurs
402 in `defmacro*'; see that for more information.
404 An alternative means of destructuring is using the `loop' macro. `loop'
405 gives practical examples of destructuring. `defmacro*' describes the
406 differences between loop and macro-style destructuring.
408 You can rewrite a call to (destructuring-bind ARGS EXPR &rest BODY) using
409 `loop', approximately like this:
411 (loop for ARGS = EXPR
414 I say \"approximately\" because the destructuring works in a somewhat
415 different fashion, although for most reasonably simple constructs the
416 results will be the same."
417 (let* ((bind-lets nil) (bind-forms nil) (bind-inits nil)
418 (bind-defs nil) (bind-block 'cl-none))
419 (cl-do-arglist (or args '(&aux)) expr)
420 (append '(progn) bind-inits
421 (list (nconc (list 'let* (nreverse bind-lets))
422 (nreverse bind-forms) body)))))
425 ;;; The `eval-when' form.
427 (defvar cl-not-toplevel nil)
430 (defmacro eval-when (when &rest body)
431 "(eval-when (WHEN...) BODY...): control when BODY is evaluated.
432 If `compile' is in WHEN, BODY is evaluated when compiled at top-level.
433 If `load' is in WHEN, BODY is evaluated when loaded after top-level compile.
434 If `eval' is in WHEN, BODY is evaluated when interpreted or at non-top-level."
435 (if (and (fboundp 'cl-compiling-file) (cl-compiling-file)
436 (not cl-not-toplevel) (not (boundp 'for-effect))) ; horrible kludge
437 (let ((comp (or (memq 'compile when) (memq ':compile-toplevel when)))
439 (if (or (memq 'load when) (memq ':load-toplevel when))
440 (if comp (cons 'progn (mapcar 'cl-compile-time-too body))
441 (list* 'if nil nil body))
442 (progn (if comp (eval (cons 'progn body))) nil)))
443 (and (or (memq 'eval when) (memq ':execute when))
444 (cons 'progn body))))
446 (defun cl-compile-time-too (form)
447 (or (and (symbolp (car-safe form)) (get (car-safe form) 'byte-hunk-handler))
448 (setq form (macroexpand
449 form (cons '(eval-when) byte-compile-macro-environment))))
450 (cond ((eq (car-safe form) 'progn)
451 (cons 'progn (mapcar 'cl-compile-time-too (cdr form))))
452 ((eq (car-safe form) 'eval-when)
453 (let ((when (nth 1 form)))
454 (if (or (memq 'eval when) (memq ':execute when))
455 (list* 'eval-when (cons 'compile when) (cddr form))
457 (t (eval form) form)))
459 (or (and (fboundp 'eval-when-compile)
460 (not (eq (car-safe (symbol-function 'eval-when-compile)) 'autoload)))
461 (eval '(defmacro eval-when-compile (&rest body)
462 "Like `progn', but evaluates the body at compile time.
463 The result of the body appears to the compiler as a quoted constant."
464 (list 'quote (eval (cons 'progn body))))))
467 (defmacro load-time-value (form &optional read-only)
468 "Like `progn', but evaluates the body at load time.
469 The result of the body appears to the compiler as a quoted constant."
470 (if (cl-compiling-file)
471 (let* ((temp (gentemp "--cl-load-time--"))
472 (set (list 'set (list 'quote temp) form)))
473 (if (and (fboundp 'byte-compile-file-form-defmumble)
474 (boundp 'this-kind) (boundp 'that-one))
475 (fset 'byte-compile-file-form
476 (list 'lambda '(form)
477 (list 'fset '(quote byte-compile-file-form)
479 (symbol-function 'byte-compile-file-form)))
480 (list 'byte-compile-file-form (list 'quote set))
481 '(byte-compile-file-form form)))
483 (print set (symbol-value ;;'outbuffer
484 'byte-compile-output-buffer
486 (list 'symbol-value (list 'quote temp)))
487 (list 'quote (eval form))))
490 ;;; Conditional control structures.
493 (defmacro case (expr &rest clauses)
494 "(case EXPR CLAUSES...): evals EXPR, chooses from CLAUSES on that value.
495 Each clause looks like (KEYLIST BODY...). EXPR is evaluated and compared
496 against each key in each KEYLIST; the corresponding BODY is evaluated.
497 If no clause succeeds, case returns nil. A single atom may be used in
498 place of a KEYLIST of one atom. A KEYLIST of `t' or `otherwise' is
499 allowed only in the final clause, and matches if no other keys match.
500 Key values are compared by `eql'."
501 (let* ((temp (if (cl-simple-expr-p expr 3) expr (gensym)))
503 (last-clause (car (last clauses)))
508 (cons (cond ((memq (car c) '(t otherwise))
509 (or (eq c last-clause)
511 "`%s' is allowed only as the last case clause"
514 ((eq (car c) 'ecase-error-flag)
515 (list 'error "ecase failed: %s, %s"
516 temp (list 'quote (reverse head-list))))
518 (setq head-list (append (car c) head-list))
519 (list 'member* temp (list 'quote (car c))))
521 (if (memq (car c) head-list)
522 (error "Duplicate key in case: %s"
524 (cl-push (car c) head-list)
525 (list 'eql temp (list 'quote (car c)))))
526 (or (cdr c) '(nil))))
528 (if (eq temp expr) body
529 (list 'let (list (list temp expr)) body))))
531 ;; #### CL standard also requires `ccase', which signals a continuable
532 ;; error (`cerror' in XEmacs). However, I don't think it buys us
533 ;; anything to introduce it, as there is probably much more CL stuff
534 ;; missing, and the feature is not essential. --hniksic
537 (defmacro ecase (expr &rest clauses)
538 "(ecase EXPR CLAUSES...): like `case', but error if no case fits.
539 `otherwise'-clauses are not allowed."
540 (let ((disallowed (or (assq t clauses)
541 (assq 'otherwise clauses))))
543 (error "`%s' is not allowed in ecase" (car disallowed))))
544 (list* 'case expr (append clauses '((ecase-error-flag)))))
547 (defmacro typecase (expr &rest clauses)
548 "(typecase EXPR CLAUSES...): evals EXPR, chooses from CLAUSES on that value.
549 Each clause looks like (TYPE BODY...). EXPR is evaluated and, if it
550 satisfies TYPE, the corresponding BODY is evaluated. If no clause succeeds,
551 typecase returns nil. A TYPE of `t' or `otherwise' is allowed only in the
552 final clause, and matches if no other keys match."
553 (let* ((temp (if (cl-simple-expr-p expr 3) expr (gensym)))
559 (cons (cond ((eq (car c) 'otherwise) t)
560 ((eq (car c) 'ecase-error-flag)
561 (list 'error "etypecase failed: %s, %s"
562 temp (list 'quote (reverse type-list))))
564 (cl-push (car c) type-list)
565 (cl-make-type-test temp (car c))))
566 (or (cdr c) '(nil))))
568 (if (eq temp expr) body
569 (list 'let (list (list temp expr)) body))))
572 (defmacro etypecase (expr &rest clauses)
573 "(etypecase EXPR CLAUSES...): like `typecase', but error if no case fits.
574 `otherwise'-clauses are not allowed."
575 (list* 'typecase expr (append clauses '((ecase-error-flag)))))
578 ;;; Blocks and exits.
581 (defmacro block (name &rest body)
582 "(block NAME BODY...): define a lexically-scoped block named NAME.
583 NAME may be any symbol. Code inside the BODY forms can call `return-from'
584 to jump prematurely out of the block. This differs from `catch' and `throw'
585 in two respects: First, the NAME is an unevaluated symbol rather than a
586 quoted symbol or other form; and second, NAME is lexically rather than
587 dynamically scoped: Only references to it within BODY will work. These
588 references may appear inside macro expansions, but not inside functions
590 (if (cl-safe-expr-p (cons 'progn body)) (cons 'progn body)
591 (list 'cl-block-wrapper
592 (list* 'catch (list 'quote (intern (format "--cl-block-%s--" name)))
595 (defvar cl-active-block-names nil)
597 (put 'cl-block-wrapper 'byte-compile 'cl-byte-compile-block)
598 (defun cl-byte-compile-block (cl-form)
599 (if (fboundp 'byte-compile-form-do-effect) ; Check for optimizing compiler
601 (let* ((cl-entry (cons (nth 1 (nth 1 (nth 1 cl-form))) nil))
602 (cl-active-block-names (cons cl-entry cl-active-block-names))
603 (cl-body (byte-compile-top-level
604 (cons 'progn (cddr (nth 1 cl-form))))))
606 (byte-compile-form (list 'catch (nth 1 (nth 1 cl-form)) cl-body))
607 (byte-compile-form cl-body))))
608 (byte-compile-form (nth 1 cl-form))))
610 (put 'cl-block-throw 'byte-compile 'cl-byte-compile-throw)
611 (defun cl-byte-compile-throw (cl-form)
612 (let ((cl-found (assq (nth 1 (nth 1 cl-form)) cl-active-block-names)))
613 (if cl-found (setcdr cl-found t)))
614 (byte-compile-normal-call (cons 'throw (cdr cl-form))))
617 (defmacro return (&optional res)
618 "(return [RESULT]): return from the block named nil.
619 This is equivalent to `(return-from nil RESULT)'."
620 (list 'return-from nil res))
623 (defmacro return-from (name &optional res)
624 "(return-from NAME [RESULT]): return from the block named NAME.
625 This jumps out to the innermost enclosing `(block NAME ...)' form,
626 returning RESULT from that form (or nil if RESULT is omitted).
627 This is compatible with Common Lisp, but note that `defun' and
628 `defmacro' do not create implicit blocks as they do in Common Lisp."
629 (let ((name2 (intern (format "--cl-block-%s--" name))))
630 (list 'cl-block-throw (list 'quote name2) res)))
633 ;;; The "loop" macro.
635 (defvar args) (defvar loop-accum-var) (defvar loop-accum-vars)
636 (defvar loop-bindings) (defvar loop-body) (defvar loop-destr-temps)
637 (defvar loop-finally) (defvar loop-finish-flag) (defvar loop-first-flag)
638 (defvar loop-initially) (defvar loop-map-form) (defvar loop-name)
639 (defvar loop-result) (defvar loop-result-explicit)
640 (defvar loop-result-var) (defvar loop-steps) (defvar loop-symbol-macs)
643 (defmacro loop (&rest args)
644 "(loop CLAUSE...): The Common Lisp `loop' macro.
646 Overview of valid clauses:
647 for VAR from/upfrom/downfrom NUM to/upto/downto/above/below NUM by NUM,
648 for VAR in LIST by FUNC, for VAR on LIST by FUNC, for VAR = INIT then EXPR,
649 for VAR across ARRAY, repeat NUM, with VAR = INIT, while COND, until COND,
650 always COND, never COND, thereis COND, collect EXPR into VAR,
651 append EXPR into VAR, nconc EXPR into VAR, sum EXPR into VAR,
652 count EXPR into VAR, maximize EXPR into VAR, minimize EXPR into VAR,
653 if COND CLAUSE [and CLAUSE]... else CLAUSE [and CLAUSE...],
654 unless COND CLAUSE [and CLAUSE]... else CLAUSE [and CLAUSE...],
655 do EXPRS..., initially EXPRS..., finally EXPRS..., return EXPR,
656 finally return EXPR, named NAME.
658 The loop macro consists of a series of clauses, which do things like
659 iterate variables, set conditions for exiting the loop, accumulating values
660 to be returned as the return value of the loop, and executing arbitrary
661 blocks of code. Each clause is proceed in turn, and the loop executes its
662 body repeatedly until an exit condition is hit.
664 It's important to understand that loop clauses such as `for' and `while',
665 which look like loop-establishing constructs, don't actually *establish* a
666 loop\; the looping is established by the `loop' clause itself, which will
667 repeatedly process its body until told to stop. `while' merely establishes
668 a condition which, when true, causes the loop to finish, and `for' sets a
669 variable to different values on each iteration (e.g. successive elements of
670 a list) and sets an exit condition when there are no more values. This
671 means, for example, that if two `for' clauses appear, you don't get two
672 nested loops, but instead two variables that are stepped in parallel, and
673 two exit conditions, either of which, if triggered, will cause the loop to
674 end. Similarly for a loop with a `for' and a `while' clause. For example:
681 In each successive iteration, X is set to the next element of the list. If
682 there are no more elements, or if any element is nil (the `while' clause),
683 the loop exits. Otherwise, the block of code following `do' is executed.)
685 This example also shows that some clauses establish variable bindings --
686 essentially like a `let' binding -- and that following clauses can
687 reference these variables. Furthermore, the entire loop is surrounded by a
688 block named nil (unless the `named' clause is given), so you can return
689 from the loop using the macro `return'. (The other way to exit the loop is
690 through the macro `loop-finish'. The difference is that some loop clauses
691 establish or accumulate a value to be returned, and `loop-finish' returns
692 this. `return', however, can only return an explicitly-specified value.
693 NOTE CAREFULLY: There is a loop clause called `return' as well as a
694 standard Lisp macro called `return'. Normally they work similarly\; but if
695 you give the loop a name with `named', you will need to use the macro
698 Another extremely useful feature of loops is called \"destructuring\". If,
699 in place of VAR, a list (possibly dotted, possibly a tree of arbitary
700 complexity) is given, the value to be assigned is assumed to have a similar
701 structure to the list given, and variables in the list will be matched up
702 with corresponding elements in the structure. For example:
705 for (x y) in '((foo 1) (bar 2) (baz 3))
706 do (puthash x y some-hash-table))
708 will add three elements to a hash table, mapping foo -> 1, bar -> 2, and
709 baz -> 3. As other examples, you can conveniently process alists using
711 \(loop for (x . y) in alist do ...)
715 \(loop for (x y) on plist by #'cddr do ...)
717 Destructuring is forgiving in that mismatches in the number of elements on
718 either size will be handled gracefully, either by ignoring or initializing
721 If you don't understand how a particular loop clause works, create an
722 example and use `macroexpand-sexp' to expand the macro.
724 In greater detail, valid clauses are:
726 \(NOTE: Keywords in lowercase\; slashes separate different possibilities
727 for keywords, some of which are synonymous\; brackets indicate optional
728 parts of the clause. In all of the clauses with `being', the word `being',
729 the words `each' or `the', and the difference between singular and plural
730 keywords are all just syntactic sugar. Stylistically, you should write
731 either `being each foo' or `being the foos'.)
733 for VAR from/upfrom/downfrom NUM1 to/upto/downto/above/below NUM2 [by NUMSTEP]
734 Step VAR across numbers. `upfrom', `upto', and `below' explicitly
735 indicate upward stepping\; `downfrom', `downto', and `above' explicitly
736 indicate downward stepping. (If none of these is given, the default is
737 upward.) `to', `upto', and `downto' cause stepping to include NUM2 as
738 the last iteration, while `above' and `below' stop just before reaching
739 NUM2. `by' can be given to indicate a stepping increment other than 1.
741 for VAR in LIST [by FUNC]
742 Step VAR over elements of a LIST. FUNC specifies how to get successive
743 sublists and defaults to `cdr'.
745 for VAR on LIST [by FUNC]
746 Step VAR over tails of a LIST. FUNC specifies how to get successive
747 sublists and defaults to `cdr'.
749 for VAR in-ref LIST [by FUNC]
750 Step VAR over elements of a LIST, like `for ... in', except the VAR is
751 bound using `symbol-macrolet' instead of `let'. In essence, VAR is set
752 to a \"reference\" to the list element instead of the element itself\;
753 this us, you can destructively modify the list using `setf' on VAR, and
754 any changes to the list will \"magically\" reflect themselves in
755 subsequent uses of VAR.
757 for VAR = INIT [then EXPR]
758 Set VAR on each iteration of the loop. If only INIT is given, use it
759 on each iteration. Otherwise, use INIT on the first iteration and EXPR
762 for VAR across/across-ref ARRAY
763 Step VAR across a sequence other than a list (string, vector, bit
764 vector). If `across-ref' is given, VAR is bound using
765 `symbol-macrolet' instead of `let' -- see above.
767 for VAR being each/the element/elements in/of/in-ref/of-ref SEQUENCE [using (index INDEX-VAR)]
768 Step VAR across any sequence. A variable can be specified with a
769 `using' phrase to receive the index, starting at 0. If `in-ref' or
770 `of-ref' is given, VAR is bound using `symbol-macrolet' instead of
773 for VAR being each/the hash-key/hash-keys/hash-value/hash-values in/of HASH-TABLE [using (hash-value/hash-key OTHER-VAR)]
775 for VAR being each/the hash-key/hash-keys/hash-value/hash-values in/of HASH-TABLE [using (hash-value/hash-key OTHER-VAR)]
776 Map VAR over a hash table. The various keywords are synonymous except
777 those that distinguish between keys and values. The `using' phrase is
778 optional and allows both key and value to be bound.
780 for VAR being each/the symbol/present-symbol/external-symbol/symbols/present-symbols/external-symbols in/of OBARRAY
781 Map VAR over the symbols in an obarray. All symbol keywords are
782 currently synonymous.
784 for VAR being each/the extent/extents [in/of BUFFER-OR-STRING] [from POS] [to POS]
785 Map VAR over the extents in a buffer or string, defaulting to the
786 current buffer, the beginning and the end, respectively.
788 for VAR being each/the interval/intervals [in/of BUFFER-OR-STRING] [property PROPERTY] [from POS] [to POS]
789 Map VAR over the intervals without property change in a buffer or
790 string, defaulting to the current buffer, the beginning and the end,
791 respectively. If PROPERTY is given, iteration occurs using
792 `next-single-property-change'\; otherwise, using
793 `next-property-change'.
795 for VAR being each/the window/windows [in/of FRAME]
796 Step VAR over the windows in FRAME, defaulting to the selected frame.
798 for VAR being each/the frame/frames
799 Step VAR over all frames.
801 for VAR being each/the buffer/buffers [by FUNC]
802 Step VAR over all buffers. This is actually equivalent to
803 `for VAR in (buffer-list) [by FUNC]'.
805 for VAR being each/the key-code/key-codes/key-seq/key-seqs/key-binding/key-bindings in KEYMAP [using (key-code/key-codes/key-seq/key-seqs/key-binding/key-bindings OTHER-VAR)]
806 Map VAR over the entries in a keymap. Keyword `key-seq' causes
807 recursive mapping over prefix keymaps occurring in the keymap, with VAR
808 getting the built-up sequence (a vector). Otherwise, mapping does not
809 occur recursively. `key-code' and `key-seq' refer to what is bound
810 (second argument of `define-key'), and `key-binding' what it's bound to
811 (third argument of `define-key').
814 `as' is a synonym for `for'.
817 `and' clauses have the same syntax as `for' clauses except that the
818 variables in the clause are bound in parallel with a preceding
819 `and'/`for' clause instead of in series.
822 Set VAR to INIT once, before doing any iterations.
825 Exit the loop if more than NUM iterations have occurred.
828 Exit the loop if COND isn't true.
831 Exit the loop if COND is true.
833 collect EXPR [into VAR]
834 Push EXPR onto the end of a list of values -- stored either in VAR or a
835 temporary variable that will be returned as the return value of the
836 loop if it terminates through an exit condition or a call to
839 append EXPR [into VAR]
840 Append EXPR (a list) onto the end of a list of values, like `collect'.
842 nconc EXPR [into VAR]
843 Nconc EXPR (a list) onto the end of a list of values, like `collect'.
845 concat EXPR [into VAR]
846 Concatenate EXPR (a string) onto the end of a string of values, like
849 vconcat EXPR [into VAR]
850 Concatenate EXPR (a vector) onto the end of a vector of values, like
853 bvconcat EXPR [into VAR]
854 Concatenate EXPR (a bit vector) onto the end of a bit vector of values,
858 Add EXPR to a value, like `collect'.
860 count EXPR [into VAR]
861 If EXPR is true, increment a value by 1, like `collect'.
863 maximize EXPR [into VAR]
864 IF EXPR is greater than a value, replace the value with EXPR, like
867 minimize EXPR [into VAR]
868 IF EXPR is less than a value, replace the value with EXPR, like
872 If COND is true, continue the loop and set the loop return value (the
873 same value that's manipulated by `collect' and friends and is returned
874 by a normal loop exit or an exit using `loop-finish') to t\; otherwise,
875 exit the loop and return nil. The effect is to determine and return
876 whether a condition is true \"always\" (all iterations of the loop).
879 If COND is false, continue the loop and set the loop return value (like
880 `always') to t\; otherwise, exit the loop and return nil. The effect
881 is to determine and return whether a condition is \"never\" true (all
882 iterations of the loop).
885 If COND is true, exit the loop and return COND.
887 if/when COND CLAUSE [and CLAUSE]... else CLAUSE [and CLAUSE...]
888 If COND is true, execute the directly following clause(s)\; otherwise,
889 execute the clauses following `else'.
891 unless COND CLAUSE [and CLAUSE]... else CLAUSE [and CLAUSE...]
892 If COND is false, execute the directly following clause(s)\; otherwise, execute the clauses following `else'.
895 Execute the expressions (any Lisp forms).
898 Execute EXPR once, before doing any iterations, and after values have
899 been set using `with'.
902 Execute EXPR once, directly before the loop terminates. This will not
903 be executed if the loop terminates prematurely as a result of `always',
904 `never', `thereis', or `return'.
907 Exit from the loop and return EXPR.
910 Specify the value to be returned when the loop exits. (Unlike `return',
911 this doesn't cause the loop to immediately exit\; it will exit whenever
912 it normally would have.) This takes precedence over a return value
913 specified with `collect' and friends or `always' and friends.
916 Specify the name for block surrounding the loop, in place of nil.
919 (if (not (memq t (mapcar 'symbolp (delq nil (delq t (copy-list args))))))
920 (list 'block nil (list* 'while t args))
921 (let ((loop-name nil) (loop-bindings nil)
922 (loop-body nil) (loop-steps nil)
923 (loop-result nil) (loop-result-explicit nil)
924 (loop-result-var nil) (loop-finish-flag nil)
925 (loop-accum-var nil) (loop-accum-vars nil)
926 (loop-initially nil) (loop-finally nil)
927 (loop-map-form nil) (loop-first-flag nil)
928 (loop-destr-temps nil) (loop-symbol-macs nil))
929 (setq args (append args '(cl-end-loop)))
930 (while (not (eq (car args) 'cl-end-loop)) (cl-parse-loop-clause))
932 (cl-push (list (list loop-finish-flag t)) loop-bindings))
934 (progn (cl-push (list (list loop-first-flag t)) loop-bindings)
935 (cl-push (list 'setq loop-first-flag nil) loop-steps)))
936 (let* ((epilogue (nconc (nreverse loop-finally)
937 (list (or loop-result-explicit loop-result))))
938 (ands (cl-loop-build-ands (nreverse loop-body)))
939 (while-body (nconc (cadr ands) (nreverse loop-steps)))
941 (nreverse loop-initially)
942 (list (if loop-map-form
943 (list 'block '--cl-finish--
945 (if (eq (car ands) t) while-body
946 (cons (list 'or (car ands)
947 '(return-from --cl-finish--
950 '--cl-map loop-map-form))
951 (list* 'while (car ands) while-body)))
953 (if (equal epilogue '(nil)) (list loop-result-var)
954 (list (list 'if loop-finish-flag
955 (cons 'progn epilogue) loop-result-var)))
957 (if loop-result-var (cl-push (list loop-result-var) loop-bindings))
959 (if (cdar loop-bindings)
960 (setq body (list (cl-loop-let (cl-pop loop-bindings) body t)))
962 (while (and loop-bindings
963 (not (cdar loop-bindings)))
964 (cl-push (car (cl-pop loop-bindings)) lets))
965 (setq body (list (cl-loop-let lets body nil))))))
967 (setq body (list (list* 'symbol-macrolet loop-symbol-macs body))))
968 (list* 'block loop-name body)))))
970 (defun cl-parse-loop-clause () ; uses args, loop-*
971 (let ((word (cl-pop args))
972 (hash-types '(hash-key hash-keys hash-value hash-values))
973 (key-types '(key-code key-codes key-seq key-seqs
974 key-binding key-bindings)))
978 (error "Malformed `loop' macro"))
981 (setq loop-name (cl-pop args)))
983 ((eq word 'initially)
984 (if (memq (car args) '(do doing)) (cl-pop args))
985 (or (consp (car args)) (error "Syntax error on `initially' clause"))
986 (while (consp (car args))
987 (cl-push (cl-pop args) loop-initially)))
990 (if (eq (car args) 'return)
991 (setq loop-result-explicit (or (cl-pop2 args) '(quote nil)))
992 (if (memq (car args) '(do doing)) (cl-pop args))
993 (or (consp (car args)) (error "Syntax error on `finally' clause"))
994 (if (and (eq (caar args) 'return) (null loop-name))
995 (setq loop-result-explicit (or (nth 1 (cl-pop args)) '(quote nil)))
996 (while (consp (car args))
997 (cl-push (cl-pop args) loop-finally)))))
999 ((memq word '(for as))
1000 (let ((loop-for-bindings nil) (loop-for-sets nil) (loop-for-steps nil)
1003 (let ((var (or (cl-pop args) (gensym))))
1004 (setq word (cl-pop args))
1005 (if (eq word 'being) (setq word (cl-pop args)))
1006 (if (memq word '(the each)) (setq word (cl-pop args)))
1007 (if (memq word '(buffer buffers))
1008 (setq word 'in args (cons '(buffer-list) args)))
1011 ((memq word '(from downfrom upfrom to downto upto
1014 (if (memq (car args) '(downto above))
1015 (error "Must specify `from' value for downward loop"))
1016 (let* ((down (or (eq (car args) 'downfrom)
1017 (memq (caddr args) '(downto above))))
1018 (excl (or (memq (car args) '(above below))
1019 (memq (caddr args) '(above below))))
1020 (start (and (memq (car args) '(from upfrom downfrom))
1022 (end (and (memq (car args)
1023 '(to upto downto above below))
1025 (step (and (eq (car args) 'by) (cl-pop2 args)))
1026 (end-var (and (not (cl-const-expr-p end)) (gensym)))
1027 (step-var (and (not (cl-const-expr-p step))
1029 (and step (numberp step) (<= step 0)
1030 (error "Loop `by' value is not positive: %s" step))
1031 (cl-push (list var (or start 0)) loop-for-bindings)
1032 (if end-var (cl-push (list end-var end) loop-for-bindings))
1033 (if step-var (cl-push (list step-var step)
1037 (if down (if excl '> '>=) (if excl '< '<=))
1038 var (or end-var end)) loop-body))
1039 (cl-push (list var (list (if down '- '+) var
1040 (or step-var step 1)))
1043 ((memq word '(in in-ref on))
1044 (let* ((on (eq word 'on))
1045 (temp (if (and on (symbolp var)) var (gensym))))
1046 (cl-push (list temp (cl-pop args)) loop-for-bindings)
1047 (cl-push (list 'consp temp) loop-body)
1048 (if (eq word 'in-ref)
1049 (cl-push (list var (list 'car temp)) loop-symbol-macs)
1052 (cl-push (list var nil) loop-for-bindings)
1053 (cl-push (list var (if on temp (list 'car temp)))
1056 (if (eq (car args) 'by)
1057 (let ((step (cl-pop2 args)))
1058 (if (and (memq (car-safe step)
1061 (symbolp (nth 1 step)))
1062 (list (nth 1 step) temp)
1063 (list 'funcall step temp)))
1068 (let* ((start (cl-pop args))
1069 (then (if (eq (car args) 'then) (cl-pop2 args) start)))
1070 (cl-push (list var nil) loop-for-bindings)
1071 (if (or ands (eq (car args) 'and))
1076 (setq loop-first-flag
1080 (cl-push (list var then) loop-for-steps))
1082 (if (eq start then) start
1085 (setq loop-first-flag (gensym)))
1089 ((memq word '(across across-ref))
1090 (let ((temp-vec (gensym)) (temp-idx (gensym)))
1091 (cl-push (list temp-vec (cl-pop args)) loop-for-bindings)
1092 (cl-push (list temp-idx -1) loop-for-bindings)
1093 (cl-push (list '< (list 'setq temp-idx (list '1+ temp-idx))
1094 (list 'length temp-vec)) loop-body)
1095 (if (eq word 'across-ref)
1096 (cl-push (list var (list 'aref temp-vec temp-idx))
1098 (cl-push (list var nil) loop-for-bindings)
1099 (cl-push (list var (list 'aref temp-vec temp-idx))
1102 ((memq word '(element elements))
1103 (let ((ref (or (memq (car args) '(in-ref of-ref))
1104 (and (not (memq (car args) '(in of)))
1105 (error "Expected `of'"))))
1106 (seq (cl-pop2 args))
1108 (temp-idx (if (eq (car args) 'using)
1109 (if (and (= (length (cadr args)) 2)
1110 (eq (caadr args) 'index))
1111 (cadr (cl-pop2 args))
1112 (error "Bad `using' clause"))
1114 (cl-push (list temp-seq seq) loop-for-bindings)
1115 (cl-push (list temp-idx 0) loop-for-bindings)
1117 (let ((temp-len (gensym)))
1118 (cl-push (list temp-len (list 'length temp-seq))
1120 (cl-push (list var (list 'elt temp-seq temp-idx))
1122 (cl-push (list '< temp-idx temp-len) loop-body))
1123 (cl-push (list var nil) loop-for-bindings)
1124 (cl-push (list 'and temp-seq
1125 (list 'or (list 'consp temp-seq)
1127 (list 'length temp-seq))))
1129 (cl-push (list var (list 'if (list 'consp temp-seq)
1130 (list 'pop temp-seq)
1131 (list 'aref temp-seq temp-idx)))
1133 (cl-push (list temp-idx (list '1+ temp-idx))
1136 ((memq word hash-types)
1137 (or (memq (car args) '(in of)) (error "Expected `of'"))
1138 (let* ((table (cl-pop2 args))
1139 (other (if (eq (car args) 'using)
1140 (if (and (= (length (cadr args)) 2)
1141 (memq (caadr args) hash-types)
1142 (not (eq (caadr args) word)))
1143 (cadr (cl-pop2 args))
1144 (error "Bad `using' clause"))
1146 (if (memq word '(hash-value hash-values))
1147 (setq var (prog1 other (setq other var))))
1149 (list 'maphash (list 'function
1150 (list* 'lambda (list var other)
1151 '--cl-map)) table))))
1153 ((memq word '(symbol present-symbol external-symbol
1154 symbols present-symbols external-symbols))
1155 (let ((ob (and (memq (car args) '(in of)) (cl-pop2 args))))
1157 (list 'mapatoms (list 'function
1158 (list* 'lambda (list var)
1161 ((memq word '(overlay overlays extent extents))
1162 (let ((buf nil) (from nil) (to nil))
1163 (while (memq (car args) '(in of from to))
1164 (cond ((eq (car args) 'from) (setq from (cl-pop2 args)))
1165 ((eq (car args) 'to) (setq to (cl-pop2 args)))
1166 (t (setq buf (cl-pop2 args)))))
1168 (list 'cl-map-extents
1169 (list 'function (list 'lambda (list var (gensym))
1170 '(progn . --cl-map) nil))
1173 ((memq word '(interval intervals))
1174 (let ((buf nil) (prop nil) (from nil) (to nil)
1175 (var1 (gensym)) (var2 (gensym)))
1176 (while (memq (car args) '(in of property from to))
1177 (cond ((eq (car args) 'from) (setq from (cl-pop2 args)))
1178 ((eq (car args) 'to) (setq to (cl-pop2 args)))
1179 ((eq (car args) 'property)
1180 (setq prop (cl-pop2 args)))
1181 (t (setq buf (cl-pop2 args)))))
1182 (if (and (consp var) (symbolp (car var)) (symbolp (cdr var)))
1183 (setq var1 (car var) var2 (cdr var))
1184 (cl-push (list var (list 'cons var1 var2)) loop-for-sets))
1186 (list 'cl-map-intervals
1187 (list 'function (list 'lambda (list var1 var2)
1188 '(progn . --cl-map)))
1189 buf prop from to))))
1191 ((memq word key-types)
1192 (or (memq (car args) '(in of)) (error "Expected `of'"))
1193 (let* ((map (cl-pop2 args))
1195 (other (if (eq (car args) 'using)
1196 (if (and (= (length (cadr args)) 2)
1197 (memq (setq other-word (caadr args))
1199 (not (eq (caadr args) word)))
1200 (cadr (cl-pop2 args))
1201 (error "Bad `using' clause"))
1203 (when (memq word '(key-binding key-bindings))
1204 (setq var (prog1 other (setq other var)))
1205 (and other-word (setq word other-word)))
1207 (list (if (memq word '(key-seq key-seqs))
1208 'cl-map-keymap-recursively 'cl-map-keymap)
1209 (list 'function (list* 'lambda (list var other)
1212 ((memq word '(frame frames screen screens))
1213 (let ((temp (gensym)))
1214 (cl-push (list var '(selected-frame))
1216 (cl-push (list temp nil) loop-for-bindings)
1217 (cl-push (list 'prog1 (list 'not (list 'eq var temp))
1218 (list 'or temp (list 'setq temp var)))
1220 (cl-push (list var (list 'next-frame var))
1223 ((memq word '(window windows))
1224 (let ((scr (and (memq (car args) '(in of)) (cl-pop2 args)))
1226 (cl-push (list var (if scr
1227 (list 'frame-selected-window scr)
1228 '(selected-window)))
1230 (cl-push (list temp nil) loop-for-bindings)
1231 (cl-push (list 'prog1 (list 'not (list 'eq var temp))
1232 (list 'or temp (list 'setq temp var)))
1234 (cl-push (list var (list 'next-window var)) loop-for-steps)))
1237 (let ((handler (and (symbolp word)
1238 (get word 'cl-loop-for-handler))))
1240 (funcall handler var)
1241 (error "Expected a `for' preposition, found %s" word)))))
1242 (eq (car args) 'and))
1245 (if (and ands loop-for-bindings)
1246 (cl-push (nreverse loop-for-bindings) loop-bindings)
1247 (setq loop-bindings (nconc (mapcar 'list loop-for-bindings)
1250 (cl-push (list 'progn
1251 (cl-loop-let (nreverse loop-for-sets) 'setq ands)
1254 (cl-push (cons (if ands 'psetq 'setq)
1255 (apply 'append (nreverse loop-for-steps)))
1259 (let ((temp (gensym)))
1260 (cl-push (list (list temp (cl-pop args))) loop-bindings)
1261 (cl-push (list '>= (list 'setq temp (list '1- temp)) 0) loop-body)))
1264 (let ((what (cl-pop args))
1265 (var (cl-loop-handle-accum nil 'nreverse)))
1266 (if (eq var loop-accum-var)
1267 (cl-push (list 'progn (list 'push what var) t) loop-body)
1268 (cl-push (list 'progn
1269 (list 'setq var (list 'nconc var (list 'list what)))
1272 ((memq word '(nconc nconcing append appending))
1273 (let ((what (cl-pop args))
1274 (var (cl-loop-handle-accum nil 'nreverse)))
1275 (cl-push (list 'progn
1277 (if (eq var loop-accum-var)
1279 (list (if (memq word '(nconc nconcing))
1283 (list (if (memq word '(nconc nconcing))
1285 var what))) t) loop-body)))
1287 ((memq word '(concat concating))
1288 (let ((what (cl-pop args))
1289 (var (cl-loop-handle-accum "")))
1290 (cl-push (list 'progn (list 'callf 'concat var what) t) loop-body)))
1292 ((memq word '(vconcat vconcating))
1293 (let ((what (cl-pop args))
1294 (var (cl-loop-handle-accum [])))
1295 (cl-push (list 'progn (list 'callf 'vconcat var what) t) loop-body)))
1297 ((memq word '(bvconcat bvconcating))
1298 (let ((what (cl-pop args))
1299 (var (cl-loop-handle-accum #*)))
1300 (cl-push (list 'progn (list 'callf 'bvconcat var what) t) loop-body)))
1302 ((memq word '(sum summing))
1303 (let ((what (cl-pop args))
1304 (var (cl-loop-handle-accum 0)))
1305 (cl-push (list 'progn (list 'incf var what) t) loop-body)))
1307 ((memq word '(count counting))
1308 (let ((what (cl-pop args))
1309 (var (cl-loop-handle-accum 0)))
1310 (cl-push (list 'progn (list 'if what (list 'incf var)) t) loop-body)))
1312 ((memq word '(minimize minimizing maximize maximizing))
1313 (let* ((what (cl-pop args))
1314 (temp (if (cl-simple-expr-p what) what (gensym)))
1315 (var (cl-loop-handle-accum nil))
1316 (func (intern (substring (symbol-name word) 0 3)))
1317 (set (list 'setq var (list 'if var (list func var temp) temp))))
1318 (cl-push (list 'progn (if (eq temp what) set
1319 (list 'let (list (list temp what)) set))
1323 (let ((bindings nil))
1324 (while (progn (cl-push (list (cl-pop args)
1325 (and (eq (car args) '=) (cl-pop2 args)))
1327 (eq (car args) 'and))
1329 (cl-push (nreverse bindings) loop-bindings)))
1332 (cl-push (cl-pop args) loop-body))
1335 (cl-push (list 'not (cl-pop args)) loop-body))
1338 (or loop-finish-flag (setq loop-finish-flag (gensym)))
1339 (cl-push (list 'setq loop-finish-flag (cl-pop args)) loop-body)
1340 (setq loop-result t))
1343 (or loop-finish-flag (setq loop-finish-flag (gensym)))
1344 (cl-push (list 'setq loop-finish-flag (list 'not (cl-pop args)))
1346 (setq loop-result t))
1349 (or loop-finish-flag (setq loop-finish-flag (gensym)))
1350 (or loop-result-var (setq loop-result-var (gensym)))
1351 (cl-push (list 'setq loop-finish-flag
1352 (list 'not (list 'setq loop-result-var (cl-pop args))))
1355 ((memq word '(if when unless))
1356 (let* ((cond (cl-pop args))
1357 (then (let ((loop-body nil))
1358 (cl-parse-loop-clause)
1359 (cl-loop-build-ands (nreverse loop-body))))
1360 (else (let ((loop-body nil))
1361 (if (eq (car args) 'else)
1362 (progn (cl-pop args) (cl-parse-loop-clause)))
1363 (cl-loop-build-ands (nreverse loop-body))))
1364 (simple (and (eq (car then) t) (eq (car else) t))))
1365 (if (eq (car args) 'end) (cl-pop args))
1366 (if (eq word 'unless) (setq then (prog1 else (setq else then))))
1367 (let ((form (cons (if simple (cons 'progn (nth 1 then)) (nth 2 then))
1368 (if simple (nth 1 else) (list (nth 2 else))))))
1369 (if (cl-expr-contains form 'it)
1370 (let ((temp (gensym)))
1371 (cl-push (list temp) loop-bindings)
1372 (setq form (list* 'if (list 'setq temp cond)
1373 (subst temp 'it form))))
1374 (setq form (list* 'if cond form)))
1375 (cl-push (if simple (list 'progn form t) form) loop-body))))
1377 ((memq word '(do doing))
1379 (or (consp (car args)) (error "Syntax error on `do' clause"))
1380 (while (consp (car args)) (cl-push (cl-pop args) body))
1381 (cl-push (cons 'progn (nreverse (cons t body))) loop-body)))
1384 (or loop-finish-flag (setq loop-finish-flag (gensym)))
1385 (or loop-result-var (setq loop-result-var (gensym)))
1386 (cl-push (list 'setq loop-result-var (cl-pop args)
1387 loop-finish-flag nil) loop-body))
1390 (let ((handler (and (symbolp word) (get word 'cl-loop-handler))))
1391 (or handler (error "Expected a loop keyword, found %s" word))
1392 (funcall handler))))
1393 (if (eq (car args) 'and)
1394 (progn (cl-pop args) (cl-parse-loop-clause)))))
1396 (defun cl-loop-let (specs body par) ; uses loop-*
1397 (let ((p specs) (temps nil) (new nil))
1398 (while (and p (or (symbolp (car-safe (car p))) (null (cadar p))))
1402 (setq par nil p specs)
1404 (or (cl-const-expr-p (cadar p))
1405 (let ((temp (gensym)))
1406 (cl-push (list temp (cadar p)) temps)
1407 (setcar (cdar p) temp)))
1410 (if (and (consp (car specs)) (listp (caar specs)))
1411 (let* ((spec (caar specs)) (nspecs nil)
1412 (expr (cadr (cl-pop specs)))
1413 (temp (cdr (or (assq spec loop-destr-temps)
1414 (car (cl-push (cons spec (or (last spec 0)
1416 loop-destr-temps))))))
1417 (cl-push (list temp expr) new)
1419 (cl-push (list (cl-pop spec)
1420 (and expr (list (if spec 'pop 'car) temp)))
1422 (setq specs (nconc (nreverse nspecs) specs)))
1423 (cl-push (cl-pop specs) new)))
1425 (let ((set (cons (if par 'psetq 'setq) (apply 'nconc (nreverse new)))))
1426 (if temps (list 'let* (nreverse temps) set) set))
1427 (list* (if par 'let 'let*)
1428 (nconc (nreverse temps) (nreverse new)) body))))
1430 (defun cl-loop-handle-accum (def &optional func) ; uses args, loop-*
1431 (if (eq (car args) 'into)
1432 (let ((var (cl-pop2 args)))
1433 (or (memq var loop-accum-vars)
1434 (progn (cl-push (list (list var def)) loop-bindings)
1435 (cl-push var loop-accum-vars)))
1439 (cl-push (list (list (setq loop-accum-var (gensym)) def))
1441 (setq loop-result (if func (list func loop-accum-var)
1445 (defun cl-loop-build-ands (clauses)
1449 (if (and (eq (car-safe (car clauses)) 'progn)
1450 (eq (car (last (car clauses))) t))
1452 (setq clauses (cons (nconc (butlast (car clauses))
1453 (if (eq (car-safe (cadr clauses))
1456 (list (cadr clauses))))
1458 (setq body (cdr (butlast (cl-pop clauses)))))
1459 (cl-push (cl-pop clauses) ands)))
1460 (setq ands (or (nreverse ands) (list t)))
1461 (list (if (cdr ands) (cons 'and ands) (car ands))
1463 (let ((full (if body
1464 (append ands (list (cons 'progn (append body '(t)))))
1466 (if (cdr full) (cons 'and full) (car full))))))
1469 ;;; Other iteration control structures.
1472 (defmacro do (steps endtest &rest body)
1473 "The Common Lisp `do' loop.
1474 Format is: (do ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)"
1475 (cl-expand-do-loop steps endtest body nil))
1478 (defmacro do* (steps endtest &rest body)
1479 "The Common Lisp `do*' loop.
1480 Format is: (do* ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)"
1481 (cl-expand-do-loop steps endtest body t))
1483 (defun cl-expand-do-loop (steps endtest body star)
1485 (list* (if star 'let* 'let)
1486 (mapcar #'(lambda (c) (if (consp c) (list (car c) (nth 1 c)) c))
1488 (list* 'while (list 'not (car endtest))
1492 (and (consp c) (cdr (cdr c))
1493 (list (car c) (nth 2 c))))
1495 (setq sets (delq nil sets))
1497 (list (cons (if (or star (not (cdr sets)))
1499 (apply 'append sets)))))))
1500 (or (cdr endtest) '(nil)))))
1503 (defmacro dolist (spec &rest body)
1504 "(dolist (VAR LIST [RESULT]) BODY...): loop over a list.
1505 Evaluate BODY with VAR bound to each `car' from LIST, in turn.
1506 Then evaluate RESULT to get return value, default nil."
1507 (let ((temp (gensym "--dolist-temp--")))
1509 (list* 'let (list (list temp (nth 1 spec)) (car spec))
1510 (list* 'while temp (list 'setq (car spec) (list 'car temp))
1511 (append body (list (list 'setq temp
1512 (list 'cdr temp)))))
1513 (if (cdr (cdr spec))
1514 (cons (list 'setq (car spec) nil) (cdr (cdr spec)))
1518 (defmacro dotimes (spec &rest body)
1519 "(dotimes (VAR COUNT [RESULT]) BODY...): loop a certain number of times.
1520 Evaluate BODY with VAR bound to successive integers from 0, inclusive,
1521 to COUNT, exclusive. Then evaluate RESULT to get return value, default
1523 (let ((temp (gensym "--dotimes-temp--")))
1525 (list* 'let (list (list temp (nth 1 spec)) (list (car spec) 0))
1526 (list* 'while (list '< (car spec) temp)
1527 (append body (list (list 'incf (car spec)))))
1528 (or (cdr (cdr spec)) '(nil))))))
1531 (defmacro do-symbols (spec &rest body)
1532 "(dosymbols (VAR [OBARRAY [RESULT]]) BODY...): loop over all symbols.
1533 Evaluate BODY with VAR bound to each interned symbol, or to each symbol
1535 ;; Apparently this doesn't have an implicit block.
1537 (list 'let (list (car spec))
1539 (list 'function (list* 'lambda (list (car spec)) body))
1540 (and (cadr spec) (list (cadr spec))))
1544 (defmacro do-all-symbols (spec &rest body)
1545 (list* 'do-symbols (list (car spec) nil (cadr spec)) body))
1551 (defmacro psetq (&rest args)
1552 "(psetq SYM VAL SYM VAL ...): set SYMs to the values VALs in parallel.
1553 This is like `setq', except that all VAL forms are evaluated (in order)
1554 before assigning any symbols SYM to the corresponding values."
1558 ;;; Binding control structures.
1561 (defmacro progv (symbols values &rest body)
1562 "(progv SYMBOLS VALUES BODY...): bind SYMBOLS to VALUES dynamically in BODY.
1563 The forms SYMBOLS and VALUES are evaluated, and must evaluate to lists.
1564 Each SYMBOL in the first list is bound to the corresponding VALUE in the
1565 second list (or made unbound if VALUES is shorter than SYMBOLS); then the
1566 BODY forms are executed and their result is returned. This is much like
1567 a `let' form, except that the list of symbols can be computed at run-time."
1568 (list 'let '((cl-progv-save nil))
1569 (list 'unwind-protect
1570 (list* 'progn (list 'cl-progv-before symbols values) body)
1571 '(cl-progv-after))))
1573 ;;; This should really have some way to shadow 'byte-compile properties, etc.
1575 (defmacro flet (bindings &rest body)
1576 "(flet ((FUNC ARGLIST BODY...) ...) FORM...): make temporary function defns.
1577 This is an analogue of `let' that operates on the function cell of FUNC
1578 rather than its value cell. The FORMs are evaluated with the specified
1579 function definitions in place, then the definitions are undone (the FUNCs
1580 go back to their previous definitions, or lack thereof)."
1584 (if (or (and (fboundp (car x))
1585 (eq (car-safe (symbol-function (car x))) 'macro))
1586 (cdr (assq (car x) cl-macro-environment)))
1587 (error "Use `labels', not `flet', to rebind macro names"))
1588 (let ((func (list 'function*
1589 (list 'lambda (cadr x)
1590 (list* 'block (car x) (cddr x))))))
1591 (if (and (cl-compiling-file)
1592 (boundp 'byte-compile-function-environment))
1593 (cl-push (cons (car x) (eval func))
1594 byte-compile-function-environment))
1595 (list (list 'symbol-function (list 'quote (car x))) func)))
1600 (defmacro labels (bindings &rest body)
1601 "(labels ((FUNC ARGLIST BODY...) ...) FORM...): make temporary func bindings.
1602 This is like `flet', except the bindings are lexical instead of dynamic.
1603 Unlike `flet', this macro is fully compliant with the Common Lisp standard."
1604 (let ((vars nil) (sets nil) (cl-macro-environment cl-macro-environment))
1606 (let ((var (gensym)))
1608 (cl-push (list 'function* (cons 'lambda (cdar bindings))) sets)
1610 (cl-push (list (car (cl-pop bindings)) 'lambda '(&rest cl-labels-args)
1611 (list 'list* '(quote funcall) (list 'quote var)
1613 cl-macro-environment)))
1614 (cl-macroexpand-all (list* 'lexical-let vars (cons (cons 'setq sets) body))
1615 cl-macro-environment)))
1617 ;; The following ought to have a better definition for use with newer
1620 (defmacro macrolet (bindings &rest body)
1621 "(macrolet ((NAME ARGLIST BODY...) ...) FORM...): make temporary macro defns.
1622 This is like `flet', but for macros instead of functions."
1625 (list (car bindings)) (list* 'macrolet (cdr bindings) body))
1626 (if (null bindings) (cons 'progn body)
1627 (let* ((name (caar bindings))
1628 (res (cl-transform-lambda (cdar bindings) name)))
1630 (cl-macroexpand-all (cons 'progn body)
1631 (cons (list* name 'lambda (cdr res))
1632 cl-macro-environment))))))
1635 (defmacro symbol-macrolet (bindings &rest body)
1636 "(symbol-macrolet ((NAME EXPANSION) ...) FORM...): make symbol macro defns.
1637 Within the body FORMs, references to the variable NAME will be replaced
1638 by EXPANSION, and (setq NAME ...) will act like (setf EXPANSION ...)."
1640 (list 'symbol-macrolet
1641 (list (car bindings)) (list* 'symbol-macrolet (cdr bindings) body))
1642 (if (null bindings) (cons 'progn body)
1643 (cl-macroexpand-all (cons 'progn body)
1644 (cons (list (symbol-name (caar bindings))
1646 cl-macro-environment)))))
1649 (defmacro define-symbol-macro (symbol expansion)
1650 "Provides a mechanism for globally affecting the macro expansion of
1651 the indicated SYMBOL. Any time SYMBOL is referenced, the EXPANSION
1652 is actually used in place of SYMBOL.
1653 Any use of setq to set the value of the symbol while in the scope of this
1654 definition is treated as if it were a setf.
1655 A binding for a symbol macro can be shadowed by `let' or `symbol-macrolet'."
1656 (cond ((not (symbolp symbol))
1657 (error "define-symbol-macro: %S is not a symbol"
1661 (put ',symbol 'symbol-macro ',expansion)
1664 (defvar cl-closure-vars nil)
1666 (defmacro lexical-let (bindings &rest body)
1667 "(lexical-let BINDINGS BODY...): like `let', but lexically scoped.
1668 The main visible difference is that lambdas inside BODY will create
1669 lexical closures as in Common Lisp."
1670 (let* ((cl-closure-vars cl-closure-vars)
1671 (vars (mapcar #'(lambda (x)
1672 (or (consp x) (setq x (list x)))
1673 (cl-push (gensym (format "--%s--" (car x)))
1675 (list (car x) (cadr x) (car cl-closure-vars)))
1680 (nconc (mapcar #'(lambda (x)
1681 (list (symbol-name (car x))
1682 (list 'symbol-value (caddr x))
1685 (list '(defun . cl-defun-expander))
1686 cl-macro-environment))))
1687 (if (not (get (car (last cl-closure-vars)) 'used))
1688 (list 'let (mapcar #'(lambda (x) (list (caddr x) (cadr x))) vars)
1689 (sublis (mapcar #'(lambda (x)
1690 (cons (caddr x) (list 'quote (caddr x))))
1693 (list 'let (mapcar #'(lambda (x)
1696 (format "--%s--" (car x)))))
1698 (apply 'append '(setf)
1699 (mapcar #'(lambda (x)
1700 (list (list 'symbol-value (caddr x)) (cadr x)))
1705 (defmacro lexical-let* (bindings &rest body)
1706 "(lexical-let* BINDINGS BODY...): like `let*', but lexically scoped.
1707 The main visible difference is that lambdas inside BODY will create
1708 lexical closures as in Common Lisp."
1709 (if (null bindings) (cons 'progn body)
1710 (setq bindings (reverse bindings))
1712 (setq body (list (list* 'lexical-let (list (cl-pop bindings)) body))))
1715 (defun cl-defun-expander (func &rest rest)
1717 (list 'defalias (list 'quote func)
1718 (list 'function (cons 'lambda rest)))
1719 (list 'quote func)))
1722 ;;; Multiple values.
1725 (defmacro multiple-value-bind (vars form &rest body)
1726 "(multiple-value-bind (SYM SYM...) FORM BODY): collect multiple return values.
1727 FORM must return a list; the BODY is then executed with the first N elements
1728 of this list bound (`let'-style) to each of the symbols SYM in turn. This
1729 is analogous to the Common Lisp `multiple-value-bind' macro, using lists to
1730 simulate true multiple return values. For compatibility, (values A B C) is
1731 a synonym for (list A B C)."
1732 (let ((temp (gensym)) (n -1))
1733 (list* 'let* (cons (list temp form)
1734 (mapcar #'(lambda (v)
1735 (list v (list 'nth (setq n (1+ n)) temp)))
1740 (defmacro multiple-value-setq (vars form)
1741 "(multiple-value-setq (SYM SYM...) FORM): collect multiple return values.
1742 FORM must return a list; the first N elements of this list are stored in
1743 each of the symbols SYM in turn. This is analogous to the Common Lisp
1744 `multiple-value-setq' macro, using lists to simulate true multiple return
1745 values. For compatibility, (values A B C) is a synonym for (list A B C)."
1746 (cond ((null vars) (list 'progn form nil))
1747 ((null (cdr vars)) (list 'setq (car vars) (list 'car form)))
1749 (let* ((temp (gensym)) (n 0))
1750 (list 'let (list (list temp form))
1751 (list 'prog1 (list 'setq (cl-pop vars) (list 'car temp))
1766 (defmacro locally (&rest body) (cons 'progn body))
1768 (defmacro the (type form) form)
1770 (defvar cl-proclaim-history t) ; for future compilers
1771 (defvar cl-declare-stack t) ; for future compilers
1773 (defun cl-do-proclaim (spec hist)
1774 (and hist (listp cl-proclaim-history) (cl-push spec cl-proclaim-history))
1775 (cond ((eq (car-safe spec) 'special)
1776 (if (boundp 'byte-compile-bound-variables)
1777 (setq byte-compile-bound-variables
1779 (mapcar #'(lambda (v) (cons v byte-compile-global-bit))
1781 byte-compile-bound-variables))))
1783 ((eq (car-safe spec) 'inline)
1784 (while (setq spec (cdr spec))
1785 (or (memq (get (car spec) 'byte-optimizer)
1786 '(nil byte-compile-inline-expand))
1787 (error "%s already has a byte-optimizer, can't make it inline"
1789 (put (car spec) 'byte-optimizer 'byte-compile-inline-expand)))
1791 ((eq (car-safe spec) 'notinline)
1792 (while (setq spec (cdr spec))
1793 (if (eq (get (car spec) 'byte-optimizer)
1794 'byte-compile-inline-expand)
1795 (put (car spec) 'byte-optimizer nil))))
1797 ((eq (car-safe spec) 'optimize)
1798 (let ((speed (assq (nth 1 (assq 'speed (cdr spec)))
1799 '((0 . nil) (1 . t) (2 . t) (3 . t))))
1800 (safety (assq (nth 1 (assq 'safety (cdr spec)))
1801 '((0 . t) (1 . t) (2 . t) (3 . nil)))))
1803 (setq cl-optimize-speed (car speed)
1804 byte-optimize (cdr speed)))
1806 (setq cl-optimize-safety (car safety)
1807 byte-compile-delete-errors (cdr safety)))))
1809 ((and (eq (car-safe spec) 'warn) (boundp 'byte-compile-warnings))
1810 (if (eq byte-compile-warnings t)
1812 (setq byte-compile-warnings byte-compile-default-warnings))
1813 (while (setq spec (cdr spec))
1814 (if (consp (car spec))
1815 (if (eq (cadar spec) 0)
1816 (setq byte-compile-warnings
1817 (delq (caar spec) byte-compile-warnings))
1818 (setq byte-compile-warnings
1819 (adjoin (caar spec) byte-compile-warnings)))))))
1822 ;;; Process any proclamations made before cl-macs was loaded.
1823 (defvar cl-proclaims-deferred)
1824 (let ((p (reverse cl-proclaims-deferred)))
1825 (while p (cl-do-proclaim (cl-pop p) t))
1826 (setq cl-proclaims-deferred nil))
1829 (defmacro declare (&rest specs)
1830 (if (cl-compiling-file)
1832 (if (listp cl-declare-stack) (cl-push (car specs) cl-declare-stack))
1833 (cl-do-proclaim (cl-pop specs) nil)))
1838 ;;; Generalized variables.
1841 (defmacro define-setf-method (func args &rest body)
1842 "(define-setf-method NAME ARGLIST BODY...): define a `setf' method.
1843 This method shows how to handle `setf's to places of the form (NAME ARGS...).
1844 The argument forms ARGS are bound according to ARGLIST, as if NAME were
1845 going to be expanded as a macro, then the BODY forms are executed and must
1846 return a list of five elements: a temporary-variables list, a value-forms
1847 list, a store-variables list (of length one), a store-form, and an access-
1848 form. See `defsetf' for a simpler way to define most setf-methods."
1849 (append '(eval-when (compile load eval))
1850 (if (stringp (car body))
1851 (list (list 'put (list 'quote func) '(quote setf-documentation)
1853 (list (cl-transform-function-property
1854 func 'setf-method (cons args body)))))
1857 (defmacro defsetf (func arg1 &rest args)
1858 "(defsetf NAME FUNC): define a `setf' method.
1859 This macro is an easy-to-use substitute for `define-setf-method' that works
1860 well for simple place forms. In the simple `defsetf' form, `setf's of
1861 the form (setf (NAME ARGS...) VAL) are transformed to function or macro
1862 calls of the form (FUNC ARGS... VAL). Example: (defsetf aref aset).
1863 Alternate form: (defsetf NAME ARGLIST (STORE) BODY...).
1864 Here, the above `setf' call is expanded by binding the argument forms ARGS
1865 according to ARGLIST, binding the value form VAL to STORE, then executing
1866 BODY, which must return a Lisp form that does the necessary `setf' operation.
1867 Actually, ARGLIST and STORE may be bound to temporary variables which are
1868 introduced automatically to preserve proper execution order of the arguments.
1869 Example: (defsetf nth (n x) (v) (list 'setcar (list 'nthcdr n x) v))."
1871 (let* ((largs nil) (largsr nil)
1872 (temps nil) (tempsr nil)
1873 (restarg nil) (rest-temps nil)
1874 (store-var (car (prog1 (car args) (setq args (cdr args)))))
1875 (store-temp (intern (format "--%s--temp--" store-var)))
1876 (lets1 nil) (lets2 nil)
1877 (docstr nil) (p arg1))
1878 (if (stringp (car args))
1879 (setq docstr (prog1 (car args) (setq args (cdr args)))))
1880 (while (and p (not (eq (car p) '&aux)))
1881 (if (eq (car p) '&rest)
1882 (setq p (cdr p) restarg (car p))
1883 (or (memq (car p) '(&optional &key &allow-other-keys))
1884 (setq largs (cons (if (consp (car p)) (car (car p)) (car p))
1886 temps (cons (intern (format "--%s--temp--" (car largs)))
1889 (setq largs (nreverse largs) temps (nreverse temps))
1891 (setq largsr (append largs (list restarg))
1892 rest-temps (intern (format "--%s--temp--" restarg))
1893 tempsr (append temps (list rest-temps)))
1894 (setq largsr largs tempsr temps))
1895 (let ((p1 largs) (p2 temps))
1897 (setq lets1 (cons (list (car p2)
1898 (list 'gensym (format "--%s--" (car p1))))
1900 lets2 (cons (list (car p1) (car p2)) lets2)
1901 p1 (cdr p1) p2 (cdr p2))))
1902 (if restarg (setq lets2 (cons (list restarg rest-temps) lets2)))
1903 (append (list 'define-setf-method func arg1)
1904 (and docstr (list docstr))
1908 (cons (list store-temp
1909 (list 'gensym (format "--%s--" store-var)))
1914 (list 'mapcar '(quote gensym)
1918 (list 'list ; 'values
1919 (cons (if restarg 'list* 'list) tempsr)
1920 (cons (if restarg 'list* 'list) largsr)
1921 (list 'list store-temp)
1924 (cons (list store-var store-temp)
1927 (cons (if restarg 'list* 'list)
1928 (cons (list 'quote func) tempsr)))))))
1929 (list 'defsetf func '(&rest args) '(store)
1930 (let ((call (list 'cons (list 'quote arg1)
1931 '(append args (list store)))))
1933 (list 'list '(quote progn) call 'store)
1936 ;;; Some standard place types from Common Lisp.
1937 (eval-when-compile (defvar ignored-arg)) ; Warning suppression
1939 (defsetf car setcar)
1940 (defsetf cdr setcdr)
1941 (defsetf elt (seq n) (store)
1942 (list 'if (list 'listp seq) (list 'setcar (list 'nthcdr n seq) store)
1943 (list 'aset seq n store)))
1944 (defsetf get (x y &optional ignored-arg) (store) (list 'put x y store))
1945 (defsetf get* (x y &optional ignored-arg) (store) (list 'put x y store))
1946 (defsetf gethash (x h &optional ignored-arg) (store) (list 'cl-puthash x store h))
1947 (defsetf nth (n x) (store) (list 'setcar (list 'nthcdr n x) store))
1948 (defsetf subseq (seq start &optional end) (new)
1949 (list 'progn (list 'replace seq new ':start1 start ':end1 end) new))
1950 (defsetf symbol-function fset)
1951 (defsetf symbol-plist setplist)
1952 (defsetf symbol-value set)
1954 ;;; Various car/cdr aliases. Note that `cadr' is handled specially.
1955 (defsetf first setcar)
1956 (defsetf second (x) (store) (list 'setcar (list 'cdr x) store))
1957 (defsetf third (x) (store) (list 'setcar (list 'cddr x) store))
1958 (defsetf fourth (x) (store) (list 'setcar (list 'cdddr x) store))
1959 (defsetf fifth (x) (store) (list 'setcar (list 'nthcdr 4 x) store))
1960 (defsetf sixth (x) (store) (list 'setcar (list 'nthcdr 5 x) store))
1961 (defsetf seventh (x) (store) (list 'setcar (list 'nthcdr 6 x) store))
1962 (defsetf eighth (x) (store) (list 'setcar (list 'nthcdr 7 x) store))
1963 (defsetf ninth (x) (store) (list 'setcar (list 'nthcdr 8 x) store))
1964 (defsetf tenth (x) (store) (list 'setcar (list 'nthcdr 9 x) store))
1965 (defsetf rest setcdr)
1967 ;;; Some more Emacs-related place types.
1968 (defsetf buffer-file-name set-visited-file-name t)
1969 (defsetf buffer-modified-p set-buffer-modified-p t)
1970 (defsetf buffer-name rename-buffer t)
1971 (defsetf buffer-string () (store)
1972 (list 'progn '(erase-buffer) (list 'insert store)))
1973 (defsetf buffer-substring cl-set-buffer-substring)
1974 (defsetf current-buffer set-buffer)
1975 (defsetf current-case-table set-case-table)
1976 (defsetf current-column move-to-column t)
1977 (defsetf current-global-map use-global-map t)
1978 (defsetf current-input-mode () (store)
1979 (list 'progn (list 'apply 'set-input-mode store) store))
1980 (defsetf current-local-map use-local-map t)
1981 (defsetf current-window-configuration set-window-configuration t)
1982 (defsetf default-file-modes set-default-file-modes t)
1983 (defsetf default-value set-default)
1984 (defsetf documentation-property put)
1985 (defsetf extent-face set-extent-face)
1986 (defsetf extent-priority set-extent-priority)
1987 (defsetf extent-property (x y &optional ignored-arg) (arg)
1988 (list 'set-extent-property x y arg))
1989 (defsetf extent-start-position (ext) (store)
1990 `(progn (set-extent-endpoints ,ext ,store (extent-end-position ,ext))
1992 (defsetf extent-end-position (ext) (store)
1993 `(progn (set-extent-endpoints ,ext (extent-start-position ,ext) ,store)
1995 (defsetf face-background (f &optional s) (x) (list 'set-face-background f x s))
1996 (defsetf face-background-pixmap (f &optional s) (x)
1997 (list 'set-face-background-pixmap f x s))
1998 (defsetf face-font (f &optional s) (x) (list 'set-face-font f x s))
1999 (defsetf face-foreground (f &optional s) (x) (list 'set-face-foreground f x s))
2000 (defsetf face-underline-p (f &optional s) (x)
2001 (list 'set-face-underline-p f x s))
2002 (defsetf file-modes set-file-modes t)
2003 (defsetf frame-parameters modify-frame-parameters t)
2004 (defsetf frame-visible-p cl-set-frame-visible-p)
2005 (defsetf frame-properties (&optional f) (p)
2006 `(progn (set-frame-properties ,f ,p) ,p))
2007 (defsetf frame-property (f p &optional ignored-arg) (v)
2008 `(progn (set-frame-property ,f ,v) ,p))
2009 (defsetf frame-width (&optional f) (v)
2010 `(progn (set-frame-width ,f ,v) ,v))
2011 (defsetf frame-height (&optional f) (v)
2012 `(progn (set-frame-height ,f ,v) ,v))
2013 (defsetf current-frame-configuration set-frame-configuration)
2015 ;; XEmacs: new stuff
2017 (defsetf selected-console select-console t)
2018 (defsetf selected-device select-device t)
2019 (defsetf device-baud-rate (&optional d) (v)
2020 `(set-device-baud-rate ,d ,v))
2021 ;; This setf method is a bad idea, because set-specifier *adds* a
2022 ;; specification, rather than just setting it. The net effect is that
2023 ;; it makes specifier-instance return VAL, but other things don't work
2024 ;; as expected -- letf, to name one.
2025 ;(defsetf specifier-instance (spec &optional dom def nof) (val)
2026 ; `(set-specifier ,spec ,val ,dom))
2029 (defsetf annotation-glyph set-annotation-glyph)
2030 (defsetf annotation-down-glyph set-annotation-down-glyph)
2031 (defsetf annotation-face set-annotation-face)
2032 (defsetf annotation-layout set-annotation-layout)
2033 (defsetf annotation-data set-annotation-data)
2034 (defsetf annotation-action set-annotation-action)
2035 (defsetf annotation-menu set-annotation-menu)
2037 (defsetf widget-get widget-put t)
2038 (defsetf widget-value widget-value-set t)
2041 (defsetf recent-keys-ring-size set-recent-keys-ring-size)
2042 (defsetf symbol-value-in-buffer (s b &optional ignored-arg) (store)
2043 `(with-current-buffer ,b (set ,s ,store)))
2044 (defsetf symbol-value-in-console (s c &optional ignored-arg) (store)
2045 `(letf (((selected-console) ,c))
2048 (defsetf buffer-dedicated-frame (&optional b) (v)
2049 `(set-buffer-dedicated-frame ,b ,v))
2050 (defsetf console-type-image-conversion-list
2051 set-console-type-image-conversion-list)
2052 (defsetf default-toolbar-position set-default-toolbar-position)
2053 (defsetf device-class (&optional d) (v)
2054 `(set-device-class ,d ,v))
2055 (defsetf extent-begin-glyph set-extent-begin-glyph)
2056 (defsetf extent-begin-glyph-layout set-extent-begin-glyph-layout)
2057 (defsetf extent-end-glyph set-extent-end-glyph)
2058 (defsetf extent-end-glyph-layout set-extent-end-glyph-layout)
2059 (defsetf extent-keymap set-extent-keymap)
2060 (defsetf extent-parent set-extent-parent)
2061 (defsetf extent-properties set-extent-properties)
2062 ;; Avoid adding various face and glyph functions.
2063 (defsetf frame-selected-window (&optional f) (v)
2064 `(set-frame-selected-window ,f ,v))
2065 (defsetf glyph-image (glyph &optional domain) (i)
2066 (list 'set-glyph-image glyph i domain))
2067 (defsetf itimer-function set-itimer-function)
2068 (defsetf itimer-function-arguments set-itimer-function-arguments)
2069 (defsetf itimer-is-idle set-itimer-is-idle)
2070 (defsetf itimer-recorded-run-time set-itimer-recorded-run-time)
2071 (defsetf itimer-restart set-itimer-restart)
2072 (defsetf itimer-uses-arguments set-itimer-uses-arguments)
2073 (defsetf itimer-value set-itimer-value)
2074 (defsetf keymap-parents set-keymap-parents)
2075 (defsetf marker-insertion-type set-marker-insertion-type)
2076 (defsetf mouse-pixel-position (&optional d) (v)
2078 (set-mouse-pixel-position ,d ,(car v) ,(car (cdr v)) ,(cdr (cdr v)))
2080 (defsetf trunc-stack-length set-trunc-stack-length)
2081 (defsetf trunc-stack-stack set-trunc-stack-stack)
2082 (defsetf undoable-stack-max set-undoable-stack-max)
2083 (defsetf weak-list-list set-weak-list-list)
2086 (defsetf getenv setenv t)
2087 (defsetf get-register set-register)
2088 (defsetf global-key-binding global-set-key)
2089 (defsetf keymap-parent set-keymap-parent)
2090 (defsetf keymap-name set-keymap-name)
2091 (defsetf keymap-prompt set-keymap-prompt)
2092 (defsetf keymap-default-binding set-keymap-default-binding)
2093 (defsetf local-key-binding local-set-key)
2094 (defsetf mark set-mark t)
2095 (defsetf mark-marker set-mark t)
2096 (defsetf marker-position set-marker t)
2097 (defsetf match-data store-match-data t)
2098 (defsetf mouse-position (scr) (store)
2099 (list 'set-mouse-position scr (list 'car store) (list 'cadr store)
2100 (list 'cddr store)))
2101 (defsetf overlay-get overlay-put)
2102 (defsetf overlay-start (ov) (store)
2103 (list 'progn (list 'move-overlay ov store (list 'overlay-end ov)) store))
2104 (defsetf overlay-end (ov) (store)
2105 (list 'progn (list 'move-overlay ov (list 'overlay-start ov) store) store))
2106 (defsetf point goto-char)
2107 (defsetf point-marker goto-char t)
2108 (defsetf point-max () (store)
2109 (list 'progn (list 'narrow-to-region '(point-min) store) store))
2110 (defsetf point-min () (store)
2111 (list 'progn (list 'narrow-to-region store '(point-max)) store))
2112 (defsetf process-buffer set-process-buffer)
2113 (defsetf process-filter set-process-filter)
2114 (defsetf process-sentinel set-process-sentinel)
2115 (defsetf read-mouse-position (scr) (store)
2116 (list 'set-mouse-position scr (list 'car store) (list 'cdr store)))
2117 (defsetf selected-window select-window)
2118 (defsetf selected-frame select-frame)
2119 (defsetf standard-case-table set-standard-case-table)
2120 (defsetf syntax-table set-syntax-table)
2121 (defsetf visited-file-modtime set-visited-file-modtime t)
2122 (defsetf window-buffer set-window-buffer t)
2123 (defsetf window-display-table set-window-display-table t)
2124 (defsetf window-dedicated-p set-window-dedicated-p t)
2125 (defsetf window-height (&optional window) (store)
2126 `(progn (enlarge-window (- ,store (window-height)) nil ,window) ,store))
2127 (defsetf window-hscroll set-window-hscroll)
2128 (defsetf window-point set-window-point)
2129 (defsetf window-start set-window-start)
2130 (defsetf window-width (&optional window) (store)
2131 `(progn (enlarge-window (- ,store (window-width)) t ,window) ,store))
2132 (defsetf x-get-cutbuffer x-store-cutbuffer t)
2133 (defsetf x-get-cut-buffer x-store-cut-buffer t) ; groan.
2134 (defsetf x-get-secondary-selection x-own-secondary-selection t)
2135 (defsetf x-get-selection x-own-selection t)
2136 (defsetf get-selection own-selection t)
2138 ;;; More complex setf-methods.
2139 ;;; These should take &environment arguments, but since full arglists aren't
2140 ;;; available while compiling cl-macs, we fake it by referring to the global
2141 ;;; variable cl-macro-environment directly.
2143 (define-setf-method apply (func arg1 &rest rest)
2144 (or (and (memq (car-safe func) '(quote function function*))
2145 (symbolp (car-safe (cdr-safe func))))
2146 (error "First arg to apply in setf is not (function SYM): %s" func))
2147 (let* ((form (cons (nth 1 func) (cons arg1 rest)))
2148 (method (get-setf-method form cl-macro-environment)))
2149 (list (car method) (nth 1 method) (nth 2 method)
2150 (cl-setf-make-apply (nth 3 method) (cadr func) (car method))
2151 (cl-setf-make-apply (nth 4 method) (cadr func) (car method)))))
2153 (defun cl-setf-make-apply (form func temps)
2154 (if (eq (car form) 'progn)
2155 (list* 'progn (cl-setf-make-apply (cadr form) func temps) (cddr form))
2156 (or (equal (last form) (last temps))
2157 (error "%s is not suitable for use with setf-of-apply" func))
2158 (list* 'apply (list 'quote (car form)) (cdr form))))
2160 (define-setf-method nthcdr (n place)
2161 (let ((method (get-setf-method place cl-macro-environment))
2162 (n-temp (gensym "--nthcdr-n--"))
2163 (store-temp (gensym "--nthcdr-store--")))
2164 (list (cons n-temp (car method))
2165 (cons n (nth 1 method))
2167 (list 'let (list (list (car (nth 2 method))
2168 (list 'cl-set-nthcdr n-temp (nth 4 method)
2170 (nth 3 method) store-temp)
2171 (list 'nthcdr n-temp (nth 4 method)))))
2173 (define-setf-method getf (place tag &optional def)
2174 (let ((method (get-setf-method place cl-macro-environment))
2175 (tag-temp (gensym "--getf-tag--"))
2176 (def-temp (gensym "--getf-def--"))
2177 (store-temp (gensym "--getf-store--")))
2178 (list (append (car method) (list tag-temp def-temp))
2179 (append (nth 1 method) (list tag def))
2181 (list 'let (list (list (car (nth 2 method))
2182 (list 'cl-set-getf (nth 4 method)
2183 tag-temp store-temp)))
2184 (nth 3 method) store-temp)
2185 (list 'getf (nth 4 method) tag-temp def-temp))))
2187 (define-setf-method substring (place from &optional to)
2188 (let ((method (get-setf-method place cl-macro-environment))
2189 (from-temp (gensym "--substring-from--"))
2190 (to-temp (gensym "--substring-to--"))
2191 (store-temp (gensym "--substring-store--")))
2192 (list (append (car method) (list from-temp to-temp))
2193 (append (nth 1 method) (list from to))
2195 (list 'let (list (list (car (nth 2 method))
2196 (list 'cl-set-substring (nth 4 method)
2197 from-temp to-temp store-temp)))
2198 (nth 3 method) store-temp)
2199 (list 'substring (nth 4 method) from-temp to-temp))))
2201 (define-setf-method values (&rest args)
2202 (let ((methods (mapcar #'(lambda (x)
2203 (get-setf-method x cl-macro-environment))
2205 (store-temp (gensym "--values-store--")))
2206 (list (apply 'append (mapcar 'first methods))
2207 (apply 'append (mapcar 'second methods))
2210 (mapcar #'(lambda (m)
2211 (cl-setf-do-store (cons (car (third m)) (fourth m))
2212 (list 'pop store-temp)))
2214 (cons 'list (mapcar 'fifth methods)))))
2216 ;;; Getting and optimizing setf-methods.
2218 (defun get-setf-method (place &optional env)
2219 "Return a list of five values describing the setf-method for PLACE.
2220 PLACE may be any Lisp form which can appear as the PLACE argument to
2221 a macro like `setf' or `incf'."
2223 (let ((temp (gensym "--setf--")))
2224 (list nil nil (list temp) (list 'setq place temp) place))
2225 (or (and (symbolp (car place))
2226 (let* ((func (car place))
2227 (name (symbol-name func))
2228 (method (get func 'setf-method))
2229 (case-fold-search nil))
2231 (let ((cl-macro-environment env))
2232 (setq method (apply method (cdr place))))
2233 (if (and (consp method) (= (length method) 5))
2235 (error "Setf-method for %s returns malformed method"
2237 (and (save-match-data
2238 (string-match #r"\`c[ad][ad][ad]?[ad]?r\'" name))
2239 (get-setf-method (compiler-macroexpand place)))
2240 (and (eq func 'edebug-after)
2241 (get-setf-method (nth (1- (length place)) place)
2243 (if (eq place (setq place (macroexpand place env)))
2244 (if (and (symbolp (car place)) (fboundp (car place))
2245 (symbolp (symbol-function (car place))))
2246 (get-setf-method (cons (symbol-function (car place))
2248 (error "No setf-method known for %s" (car place)))
2249 (get-setf-method place env)))))
2251 (defun cl-setf-do-modify (place opt-expr)
2252 (let* ((method (get-setf-method place cl-macro-environment))
2253 (temps (car method)) (values (nth 1 method))
2254 (lets nil) (subs nil)
2255 (optimize (and (not (eq opt-expr 'no-opt))
2256 (or (and (not (eq opt-expr 'unsafe))
2257 (cl-safe-expr-p opt-expr))
2258 (cl-setf-simple-store-p (car (nth 2 method))
2260 (simple (and optimize (consp place) (cl-simple-exprs-p (cdr place)))))
2262 (if (or simple (cl-const-expr-p (car values)))
2263 (cl-push (cons (cl-pop temps) (cl-pop values)) subs)
2264 (cl-push (list (cl-pop temps) (cl-pop values)) lets)))
2265 (list (nreverse lets)
2266 (cons (car (nth 2 method)) (sublis subs (nth 3 method)))
2267 (sublis subs (nth 4 method)))))
2269 (defun cl-setf-do-store (spec val)
2270 (let ((sym (car spec))
2272 (if (or (cl-const-expr-p val)
2273 (and (cl-simple-expr-p val) (eq (cl-expr-contains form sym) 1))
2274 (cl-setf-simple-store-p sym form))
2275 (subst val sym form)
2276 (list 'let (list (list sym val)) form))))
2278 (defun cl-setf-simple-store-p (sym form)
2279 (and (consp form) (eq (cl-expr-contains form sym) 1)
2280 (eq (nth (1- (length form)) form) sym)
2281 (symbolp (car form)) (fboundp (car form))
2282 (not (eq (car-safe (symbol-function (car form))) 'macro))))
2284 ;;; The standard modify macros.
2286 (defmacro setf (&rest args)
2287 "(setf PLACE VAL PLACE VAL ...): set each PLACE to the value of its VAL.
2288 This is a generalized version of `setq'; the PLACEs may be symbolic
2289 references such as (car x) or (aref x i), as well as plain symbols.
2290 For example, (setf (cadar x) y) is equivalent to (setcar (cdar x) y).
2291 The return value is the last VAL in the list."
2292 (if (cdr (cdr args))
2294 (while args (cl-push (list 'setf (cl-pop args) (cl-pop args)) sets))
2295 (cons 'progn (nreverse sets)))
2296 (if (symbolp (car args))
2297 (and args (cons 'setq args))
2298 (let* ((method (cl-setf-do-modify (car args) (nth 1 args)))
2299 (store (cl-setf-do-store (nth 1 method) (nth 1 args))))
2300 (if (car method) (list 'let* (car method) store) store)))))
2303 (defmacro psetf (&rest args)
2304 "(psetf PLACE VAL PLACE VAL ...): set PLACEs to the values VALs in parallel.
2305 This is like `setf', except that all VAL forms are evaluated (in order)
2306 before assigning any PLACEs to the corresponding values."
2307 (let ((p args) (simple t) (vars nil))
2309 (if (or (not (symbolp (car p))) (cl-expr-depends-p (nth 1 p) vars))
2311 (if (memq (car p) vars)
2312 (error "Destination duplicated in psetf: %s" (car p)))
2313 (cl-push (cl-pop p) vars)
2314 (or p (error "Odd number of arguments to psetf"))
2317 (list 'progn (cons 'setf args) nil)
2318 (setq args (reverse args))
2319 (let ((expr (list 'setf (cadr args) (car args))))
2320 (while (setq args (cddr args))
2321 (setq expr (list 'setf (cadr args) (list 'prog1 (car args) expr))))
2322 (list 'progn expr nil)))))
2325 (defun cl-do-pop (place)
2326 (if (cl-simple-expr-p place)
2327 (list 'prog1 (list 'car place) (list 'setf place (list 'cdr place)))
2328 (let* ((method (cl-setf-do-modify place t))
2329 (temp (gensym "--pop--")))
2331 (append (car method)
2332 (list (list temp (nth 2 method))))
2335 (cl-setf-do-store (nth 1 method) (list 'cdr temp)))))))
2338 (defmacro remf (place tag)
2339 "(remf PLACE TAG): remove TAG from property list PLACE.
2340 PLACE may be a symbol, or any generalized variable allowed by `setf'.
2341 The form returns true if TAG was found and removed, nil otherwise."
2342 (let* ((method (cl-setf-do-modify place t))
2343 (tag-temp (and (not (cl-const-expr-p tag)) (gensym "--remf-tag--")))
2344 (val-temp (and (not (cl-simple-expr-p place))
2345 (gensym "--remf-place--")))
2346 (ttag (or tag-temp tag))
2347 (tval (or val-temp (nth 2 method))))
2349 (append (car method)
2350 (and val-temp (list (list val-temp (nth 2 method))))
2351 (and tag-temp (list (list tag-temp tag))))
2352 (list 'if (list 'eq ttag (list 'car tval))
2354 (cl-setf-do-store (nth 1 method) (list 'cddr tval))
2356 (list 'cl-do-remf tval ttag)))))
2359 (defmacro shiftf (place &rest args)
2360 "(shiftf PLACE PLACE... VAL): shift left among PLACEs.
2361 Example: (shiftf A B C) sets A to B, B to C, and returns the old A.
2362 Each PLACE may be a symbol, or any generalized variable allowed by `setf'."
2363 (if (not (memq nil (mapcar 'symbolp (butlast (cons place args)))))
2367 (cl-push (list 'setq place (car args)) sets)
2368 (setq place (cl-pop args)))
2370 (let* ((places (reverse (cons place args)))
2371 (form (cl-pop places)))
2373 (let ((method (cl-setf-do-modify (cl-pop places) 'unsafe)))
2374 (setq form (list 'let* (car method)
2375 (list 'prog1 (nth 2 method)
2376 (cl-setf-do-store (nth 1 method) form))))))
2380 (defmacro rotatef (&rest args)
2381 "(rotatef PLACE...): rotate left among PLACEs.
2382 Example: (rotatef A B C) sets A to B, B to C, and C to A. It returns nil.
2383 Each PLACE may be a symbol, or any generalized variable allowed by `setf'."
2384 (if (not (memq nil (mapcar 'symbolp args)))
2389 (setq sets (nconc sets (list (cl-pop args) (car args)))))
2390 (nconc (list 'psetf) sets (list (car args) first))))
2391 (let* ((places (reverse args))
2392 (temp (gensym "--rotatef--"))
2395 (let ((method (cl-setf-do-modify (cl-pop places) 'unsafe)))
2396 (setq form (list 'let* (car method)
2397 (list 'prog1 (nth 2 method)
2398 (cl-setf-do-store (nth 1 method) form))))))
2399 (let ((method (cl-setf-do-modify (car places) 'unsafe)))
2400 (list 'let* (append (car method) (list (list temp (nth 2 method))))
2401 (cl-setf-do-store (nth 1 method) form) nil)))))
2404 (defmacro letf (bindings &rest body)
2405 "(letf ((PLACE VALUE) ...) BODY...): temporarily bind to PLACEs.
2406 This is the analogue of `let', but with generalized variables (in the
2407 sense of `setf') for the PLACEs. Each PLACE is set to the corresponding
2408 VALUE, then the BODY forms are executed. On exit, either normally or
2409 because of a `throw' or error, the PLACEs are set back to their original
2410 values. Note that this macro is *not* available in Common Lisp.
2411 As a special case, if `(PLACE)' is used instead of `(PLACE VALUE)',
2412 the PLACE is not modified before executing BODY."
2413 (if (and (not (cdr bindings)) (cdar bindings) (symbolp (caar bindings)))
2414 (list* 'let bindings body)
2416 (rev (reverse bindings)))
2418 (let* ((place (if (symbolp (caar rev))
2419 (list 'symbol-value (list 'quote (caar rev)))
2422 (method (cl-setf-do-modify place 'no-opt))
2423 (save (gensym "--letf-save--"))
2424 (bound (and (memq (car place) '(symbol-value symbol-function))
2425 (gensym "--letf-bound--")))
2426 (temp (and (not (cl-const-expr-p value)) (cdr bindings)
2427 (gensym "--letf-val--"))))
2428 (setq lets (nconc (car method)
2431 (list (if (eq (car place)
2434 (nth 1 (nth 2 method))))
2435 (list save (list 'and bound
2437 (list (list save (nth 2 method))))
2438 (and temp (list (list temp value)))
2441 (list 'unwind-protect
2444 (cons (cl-setf-do-store (nth 1 method)
2450 (cl-setf-do-store (nth 1 method) save)
2451 (list (if (eq (car place) 'symbol-value)
2452 'makunbound 'fmakunbound)
2453 (nth 1 (nth 2 method))))
2454 (cl-setf-do-store (nth 1 method) save))))
2456 (list* 'let* lets body))))
2459 (defmacro letf* (bindings &rest body)
2460 "(letf* ((PLACE VALUE) ...) BODY...): temporarily bind to PLACEs.
2461 This is the analogue of `let*', but with generalized variables (in the
2462 sense of `setf') for the PLACEs. Each PLACE is set to the corresponding
2463 VALUE, then the BODY forms are executed. On exit, either normally or
2464 because of a `throw' or error, the PLACEs are set back to their original
2465 values. Note that this macro is *not* available in Common Lisp.
2466 As a special case, if `(PLACE)' is used instead of `(PLACE VALUE)',
2467 the PLACE is not modified before executing BODY."
2470 (setq bindings (reverse bindings))
2472 (setq body (list (list* 'letf (list (cl-pop bindings)) body))))
2476 (defmacro callf (func place &rest args)
2477 "(callf FUNC PLACE ARGS...): set PLACE to (FUNC PLACE ARGS...).
2478 FUNC should be an unquoted function name. PLACE may be a symbol,
2479 or any generalized variable allowed by `setf'."
2480 (let* ((method (cl-setf-do-modify place (cons 'list args)))
2481 (rargs (cons (nth 2 method) args)))
2482 (list 'let* (car method)
2483 (cl-setf-do-store (nth 1 method)
2484 (if (symbolp func) (cons func rargs)
2485 (list* 'funcall (list 'function func)
2489 (defmacro callf2 (func arg1 place &rest args)
2490 "(callf2 FUNC ARG1 PLACE ARGS...): set PLACE to (FUNC ARG1 PLACE ARGS...).
2491 Like `callf', but PLACE is the second argument of FUNC, not the first."
2492 (if (and (cl-safe-expr-p arg1) (cl-simple-expr-p place) (symbolp func))
2493 (list 'setf place (list* func arg1 place args))
2494 (let* ((method (cl-setf-do-modify place (cons 'list args)))
2495 (temp (and (not (cl-const-expr-p arg1)) (gensym "--arg1--")))
2496 (rargs (list* (or temp arg1) (nth 2 method) args)))
2497 (list 'let* (append (and temp (list (list temp arg1))) (car method))
2498 (cl-setf-do-store (nth 1 method)
2499 (if (symbolp func) (cons func rargs)
2500 (list* 'funcall (list 'function func)
2504 (defmacro define-modify-macro (name arglist func &optional doc)
2505 "(define-modify-macro NAME ARGLIST FUNC): define a `setf'-like modify macro.
2506 If NAME is called, it combines its PLACE argument with the other arguments
2507 from ARGLIST using FUNC: (define-modify-macro incf (&optional (n 1)) +)"
2508 (if (memq '&key arglist) (error "&key not allowed in define-modify-macro"))
2509 (let ((place (gensym "--place--")))
2510 (list 'defmacro* name (cons place arglist) doc
2511 (list* (if (memq '&rest arglist) 'list* 'list)
2512 '(quote callf) (list 'quote func) place
2513 (cl-arglist-args arglist)))))
2519 (defmacro defstruct (struct &rest descs)
2520 "(defstruct (NAME OPTIONS...) (SLOT SLOT-OPTS...)...): define a struct type.
2521 This macro defines a new Lisp data type called NAME, which contains data
2522 stored in SLOTs. This defines a `make-NAME' constructor, a `copy-NAME'
2523 copier, a `NAME-p' predicate, and setf-able `NAME-SLOT' accessors."
2524 (let* ((name (if (consp struct) (car struct) struct))
2525 (opts (cdr-safe struct))
2528 (conc-name (concat (symbol-name name) "-"))
2529 (constructor (intern (format "make-%s" name)))
2531 (copier (intern (format "copy-%s" name)))
2532 (predicate (intern (format "%s-p" name)))
2533 (print-func nil) (print-auto nil)
2534 (safety (if (cl-compiling-file) cl-optimize-safety 3))
2536 (tag (intern (format "cl-struct-%s" name)))
2537 (tag-symbol (intern (format "cl-struct-%s-tags" name)))
2543 pred-form pred-check)
2544 (if (stringp (car descs))
2545 (cl-push (list 'put (list 'quote name) '(quote structure-documentation)
2546 (cl-pop descs)) forms))
2547 (setq descs (cons '(cl-tag-slot)
2548 (mapcar #'(lambda (x) (if (consp x) x (list x)))
2551 (let ((opt (if (consp (car opts)) (caar opts) (car opts)))
2552 (args (cdr-safe (cl-pop opts))))
2553 (cond ((eq opt ':conc-name)
2555 (setq conc-name (if (car args)
2556 (symbol-name (car args)) ""))))
2557 ((eq opt ':constructor)
2559 (cl-push args constrs)
2560 (if args (setq constructor (car args)))))
2562 (if args (setq copier (car args))))
2563 ((eq opt ':predicate)
2564 (if args (setq predicate (car args))))
2566 (setq include (car args)
2567 include-descs (mapcar #'(lambda (x)
2568 (if (consp x) x (list x)))
2570 ((eq opt ':print-function)
2571 (setq print-func (car args)))
2573 (setq type (car args)))
2576 ((eq opt ':initial-offset)
2577 (setq descs (nconc (make-list (car args) '(cl-skip-slot))
2580 (error "Slot option %s unrecognized" opt)))))
2582 (setq print-func (list 'progn
2583 (list 'funcall (list 'function print-func)
2584 'cl-x 'cl-s 'cl-n) t))
2585 (or type (and include (not (get include 'cl-struct-print)))
2587 print-func (and (or (not (or include type)) (null print-func))
2589 (list 'princ (format "#S(%s" name)
2592 (let ((inc-type (get include 'cl-struct-type))
2593 (old-descs (get include 'cl-struct-slots)))
2594 (or inc-type (error "%s is not a struct name" include))
2595 (and type (not (eq (car inc-type) type))
2596 (error ":type disagrees with :include for %s" name))
2597 (while include-descs
2598 (setcar (memq (or (assq (caar include-descs) old-descs)
2599 (error "No slot %s in included struct %s"
2600 (caar include-descs) include))
2602 (cl-pop include-descs)))
2603 (setq descs (append old-descs (delq (assq 'cl-tag-slot descs) descs))
2605 named (assq 'cl-tag-slot descs))
2606 (if (cadr inc-type) (setq tag name named t))
2607 (let ((incl include))
2609 (cl-push (list 'pushnew (list 'quote tag)
2610 (intern (format "cl-struct-%s-tags" incl)))
2612 (setq incl (get incl 'cl-struct-include)))))
2615 (or (memq type '(vector list))
2616 (error "Illegal :type specifier: %s" type))
2617 (if named (setq tag name)))
2618 (setq type 'vector named 'true)))
2619 (or named (setq descs (delq (assq 'cl-tag-slot descs) descs)))
2620 (cl-push (list 'defvar tag-symbol) forms)
2621 (setq pred-form (and named
2622 (let ((pos (- (length descs)
2623 (length (memq (assq 'cl-tag-slot descs)
2625 (if (eq type 'vector)
2626 (list 'and '(vectorp cl-x)
2627 (list '>= '(length cl-x) (length descs))
2628 (list 'memq (list 'aref 'cl-x pos)
2631 (list 'memq '(car-safe cl-x) tag-symbol)
2632 (list 'and '(consp cl-x)
2633 (list 'memq (list 'nth pos 'cl-x)
2635 pred-check (and pred-form (> safety 0)
2636 (if (and (eq (caadr pred-form) 'vectorp)
2638 (cons 'and (cdddr pred-form)) pred-form)))
2639 (let ((pos 0) (descp descs))
2641 (let* ((desc (cl-pop descp))
2643 (if (memq slot '(cl-tag-slot cl-skip-slot))
2646 (cl-push (and (eq slot 'cl-tag-slot) (list 'quote tag))
2648 (if (assq slot descp)
2649 (error "Duplicate slots named %s in %s" slot name))
2650 (let ((accessor (intern (format "%s%s" conc-name slot))))
2651 (cl-push slot slots)
2652 (cl-push (nth 1 desc) defaults)
2654 'defsubst* accessor '(cl-x)
2657 (list (list 'or pred-check
2659 (format "%s accessing a non-%s"
2662 (list (if (eq type 'vector) (list 'aref 'cl-x pos)
2663 (if (= pos 0) '(car cl-x)
2664 (list 'nth pos 'cl-x)))))) forms)
2665 (cl-push (cons accessor t) side-eff)
2666 (cl-push (list 'define-setf-method accessor '(cl-x)
2667 (if (cadr (memq ':read-only (cddr desc)))
2668 (list 'error (format "%s is a read-only slot"
2670 (list 'cl-struct-setf-expander 'cl-x
2671 (list 'quote name) (list 'quote accessor)
2672 (and pred-check (list 'quote pred-check))
2677 (list (list 'princ (format " %s" slot) 'cl-s)
2678 (list 'prin1 (list accessor 'cl-x) 'cl-s)))))))
2679 (setq pos (1+ pos))))
2680 (setq slots (nreverse slots)
2681 defaults (nreverse defaults))
2682 (and predicate pred-form
2683 (progn (cl-push (list 'defsubst* predicate '(cl-x)
2684 (if (eq (car pred-form) 'and)
2685 (append pred-form '(t))
2686 (list 'and pred-form t))) forms)
2687 (cl-push (cons predicate 'error-free) side-eff)))
2689 (progn (cl-push (list 'defun copier '(x) '(copy-sequence x)) forms)
2690 (cl-push (cons copier t) side-eff)))
2692 (cl-push (list constructor
2693 (cons '&key (delq nil (copy-sequence slots))))
2696 (let* ((name (caar constrs))
2697 (args (cadr (cl-pop constrs)))
2698 (anames (cl-arglist-args args))
2699 (make (mapcar* #'(lambda (s d) (if (memq s anames) s d))
2701 (cl-push (list 'defsubst* name
2702 (list* '&cl-defs (list 'quote (cons nil descs)) args)
2703 (cons type make)) forms)
2704 (if (cl-safe-expr-p (cons 'progn (mapcar 'second descs)))
2705 (cl-push (cons name t) side-eff))))
2706 (if print-auto (nconc print-func (list '(princ ")" cl-s) t)))
2708 (cl-push (list 'push
2710 (list 'lambda '(cl-x cl-s cl-n)
2711 (list 'and pred-form print-func)))
2712 'custom-print-functions) forms))
2713 (cl-push (list 'setq tag-symbol (list 'list (list 'quote tag))) forms)
2714 (cl-push (list* 'eval-when '(compile load eval)
2715 (list 'put (list 'quote name) '(quote cl-struct-slots)
2716 (list 'quote descs))
2717 (list 'put (list 'quote name) '(quote cl-struct-type)
2718 (list 'quote (list type (eq named t))))
2719 (list 'put (list 'quote name) '(quote cl-struct-include)
2720 (list 'quote include))
2721 (list 'put (list 'quote name) '(quote cl-struct-print)
2723 (mapcar #'(lambda (x)
2724 (list 'put (list 'quote (car x))
2725 '(quote side-effect-free)
2726 (list 'quote (cdr x))))
2729 (cons 'progn (nreverse (cons (list 'quote name) forms)))))
2732 (defun cl-struct-setf-expander (x name accessor pred-form pos)
2733 (let* ((temp (gensym "--x--")) (store (gensym "--store--")))
2734 (list (list temp) (list x) (list store)
2737 (list (list 'or (subst temp 'cl-x pred-form)
2740 "%s storing a non-%s" accessor name)
2742 (list (if (eq (car (get name 'cl-struct-type)) 'vector)
2743 (list 'aset temp pos store)
2747 (while (>= (setq pos (1- pos)) 0)
2748 (setq xx (list 'cdr xx)))
2750 (list 'nthcdr pos temp))
2752 (list accessor temp))))
2755 ;;; Types and assertions.
2758 (defmacro deftype (name args &rest body)
2759 "(deftype NAME ARGLIST BODY...): define NAME as a new data type.
2760 The type name can then be used in `typecase', `check-type', etc."
2761 (list 'eval-when '(compile load eval)
2762 (cl-transform-function-property
2763 name 'cl-deftype-handler (cons (list* '&cl-defs ''('*) args) body))))
2765 (defun cl-make-type-test (val type)
2767 (cond ((get type 'cl-deftype-handler)
2768 (cl-make-type-test val (funcall (get type 'cl-deftype-handler))))
2769 ((memq type '(nil t)) type)
2770 ((eq type 'string-char) (list 'characterp val))
2771 ((eq type 'null) (list 'null val))
2772 ((eq type 'float) (list 'floatp-safe val))
2773 ((eq type 'real) (list 'numberp val))
2774 ((eq type 'fixnum) (list 'integerp val))
2776 (let* ((name (symbol-name type))
2777 (namep (intern (concat name "p"))))
2778 (if (fboundp namep) (list namep val)
2779 (list (intern (concat name "-p")) val)))))
2780 (cond ((get (car type) 'cl-deftype-handler)
2781 (cl-make-type-test val (apply (get (car type) 'cl-deftype-handler)
2783 ((memq (car-safe type) '(integer float real number))
2784 (delq t (list 'and (cl-make-type-test val (car type))
2785 (if (memq (cadr type) '(* nil)) t
2786 (if (consp (cadr type)) (list '> val (caadr type))
2787 (list '>= val (cadr type))))
2788 (if (memq (caddr type) '(* nil)) t
2789 (if (consp (caddr type)) (list '< val (caaddr type))
2790 (list '<= val (caddr type)))))))
2791 ((memq (car-safe type) '(and or not))
2793 (mapcar #'(lambda (x) (cl-make-type-test val x))
2795 ((memq (car-safe type) '(member member*))
2796 (list 'and (list 'member* val (list 'quote (cdr type))) t))
2797 ((eq (car-safe type) 'satisfies) (list (cadr type) val))
2798 (t (error "Bad type spec: %s" type)))))
2801 (defun typep (object type) ; See compiler macro below.
2802 "Check that OBJECT is of type TYPE.
2803 TYPE is a Common Lisp-style type specifier."
2804 (eval (cl-make-type-test 'object type)))
2807 (defmacro check-type (place type &optional string)
2808 "Verify that PLACE is of type TYPE; signal a continuable error if not.
2809 STRING is an optional description of the desired type."
2810 (when (or (not (cl-compiling-file))
2811 (< cl-optimize-speed 3)
2812 (= cl-optimize-safety 3))
2813 (let* ((temp (if (cl-simple-expr-p place 3) place (gensym)))
2814 (test (cl-make-type-test temp type))
2815 (signal-error `(signal 'wrong-type-argument
2816 ,(list 'list (or string (list 'quote type))
2817 temp (list 'quote place))))
2821 ,(macroexpand `(setf ,place ,signal-error)))
2823 `(if ,test (progn ,signal-error nil))))))
2826 `(let ((,temp ,place)) ,body)))))
2829 (defmacro assert (form &optional show-args string &rest args)
2830 "Verify that FORM returns non-nil; signal an error if not.
2831 Second arg SHOW-ARGS means to include arguments of FORM in message.
2832 Other args STRING and ARGS... are arguments to be passed to `error'.
2833 They are not evaluated unless the assertion fails. If STRING is
2834 omitted, a default message listing FORM itself is used."
2835 (and (or (not (cl-compiling-file))
2836 (< cl-optimize-speed 3) (= cl-optimize-safety 3))
2837 (let ((sargs (and show-args (delq nil (mapcar
2839 (and (not (cl-const-expr-p x))
2845 (list* 'error string (append sargs args))
2846 (list 'signal '(quote cl-assertion-failed)
2847 (list* 'list (list 'quote form) sargs))))
2851 (defmacro ignore-errors (&rest body)
2852 "Execute FORMS; if an error occurs, return nil.
2853 Otherwise, return result of last FORM."
2854 `(condition-case nil (progn ,@body) (error nil)))
2857 (defmacro ignore-file-errors (&rest body)
2858 "Execute FORMS; if an error of type `file-error' occurs, return nil.
2859 Otherwise, return result of last FORM."
2860 `(condition-case nil (progn ,@body) (file-error nil)))
2862 ;;; Some predicates for analyzing Lisp forms. These are used by various
2863 ;;; macro expanders to optimize the results in certain common cases.
2865 (defconst cl-simple-funcs '(car cdr nth aref elt if and or + - 1+ 1- min max
2866 car-safe cdr-safe progn prog1 prog2))
2867 (defconst cl-safe-funcs '(* / % length memq list vector vectorp
2870 ;;; Check if no side effects, and executes quickly.
2871 (defun cl-simple-expr-p (x &optional size)
2872 (or size (setq size 10))
2873 (if (and (consp x) (not (memq (car x) '(quote function function*))))
2874 (and (symbolp (car x))
2875 (or (memq (car x) cl-simple-funcs)
2876 (get (car x) 'side-effect-free))
2878 (setq size (1- size))
2879 (while (and (setq x (cdr x))
2880 (setq size (cl-simple-expr-p (car x) size))))
2881 (and (null x) (>= size 0) size)))
2882 (and (> size 0) (1- size))))
2884 (defun cl-simple-exprs-p (xs)
2885 (while (and xs (cl-simple-expr-p (car xs)))
2889 ;;; Check if no side effects.
2890 (defun cl-safe-expr-p (x)
2891 (or (not (and (consp x) (not (memq (car x) '(quote function function*)))))
2892 (and (symbolp (car x))
2893 (or (memq (car x) cl-simple-funcs)
2894 (memq (car x) cl-safe-funcs)
2895 (get (car x) 'side-effect-free))
2897 (while (and (setq x (cdr x)) (cl-safe-expr-p (car x))))
2900 ;;; Check if constant (i.e., no side effects or dependencies).
2901 (defun cl-const-expr-p (x)
2903 (or (eq (car x) 'quote)
2904 (and (memq (car x) '(function function*))
2905 (or (symbolp (nth 1 x))
2906 (and (eq (car-safe (nth 1 x)) 'lambda) 'func)))))
2907 ((symbolp x) (and (memq x '(nil t)) t))
2910 (defun cl-const-exprs-p (xs)
2911 (while (and xs (cl-const-expr-p (car xs)))
2915 (defun cl-const-expr-val (x)
2916 (and (eq (cl-const-expr-p x) t) (if (consp x) (nth 1 x) x)))
2918 (defun cl-expr-access-order (x v)
2919 (if (cl-const-expr-p x) v
2922 (while (setq x (cdr x)) (setq v (cl-expr-access-order (car x) v)))
2924 (if (eq x (car v)) (cdr v) '(t)))))
2926 ;;; Count number of times X refers to Y. Return NIL for 0 times.
2927 (defun cl-expr-contains (x y)
2928 (cond ((equal y x) 1)
2929 ((and (consp x) (not (memq (car-safe x) '(quote function function*))))
2932 (setq sum (+ sum (or (cl-expr-contains (cl-pop x) y) 0))))
2933 (and (> sum 0) sum)))
2936 (defun cl-expr-contains-any (x y)
2937 (while (and y (not (cl-expr-contains x (car y)))) (cl-pop y))
2940 ;;; Check whether X may depend on any of the symbols in Y.
2941 (defun cl-expr-depends-p (x y)
2942 (and (not (cl-const-expr-p x))
2943 (or (not (cl-safe-expr-p x)) (cl-expr-contains-any x y))))
2946 ;;; Compiler macros.
2949 (defmacro define-compiler-macro (func args &rest body)
2950 "(define-compiler-macro FUNC ARGLIST BODY...): Define a compiler-only macro.
2951 This is like `defmacro', but macro expansion occurs only if the call to
2952 FUNC is compiled (i.e., not interpreted). Compiler macros should be used
2953 for optimizing the way calls to FUNC are compiled; the form returned by
2954 BODY should do the same thing as a call to the normal function called
2955 FUNC, though possibly more efficiently. Note that, like regular macros,
2956 compiler macros are expanded repeatedly until no further expansions are
2957 possible. Unlike regular macros, BODY can decide to \"punt\" and leave the
2958 original function call alone by declaring an initial `&whole foo' parameter
2959 and then returning foo."
2960 (let ((p (if (listp args) args (list '&rest args))) (res nil))
2961 (while (consp p) (cl-push (cl-pop p) res))
2962 (setq args (nreverse res)) (setcdr res (and p (list '&rest p))))
2963 (list 'eval-when '(compile load eval)
2964 (cl-transform-function-property
2965 func 'cl-compiler-macro
2966 (cons (if (memq '&whole args) (delq '&whole args)
2967 (cons '--cl-whole-arg-- args)) body))
2968 (list 'or (list 'get (list 'quote func) '(quote byte-compile))
2969 (list 'put (list 'quote func) '(quote byte-compile)
2970 '(quote cl-byte-compile-compiler-macro)))))
2973 (defun compiler-macroexpand (form)
2975 (let ((func (car-safe form)) (handler nil))
2976 (while (and (symbolp func)
2977 (not (setq handler (get func 'cl-compiler-macro)))
2979 (or (not (eq (car-safe (symbol-function func)) 'autoload))
2980 (load (nth 1 (symbol-function func)))))
2981 (setq func (symbol-function func)))
2983 (not (eq form (setq form (apply handler form (cdr form))))))))
2986 (defun cl-byte-compile-compiler-macro (form)
2987 (if (eq form (setq form (compiler-macroexpand form)))
2988 (byte-compile-normal-call form)
2989 (byte-compile-form form)))
2991 (defmacro defsubst* (name args &rest body)
2992 "(defsubst* NAME ARGLIST [DOCSTRING] BODY...): define NAME as a function.
2993 Like `defun', except the function is automatically declared `inline',
2994 ARGLIST allows full Common Lisp conventions, and BODY is implicitly
2995 surrounded by (block NAME ...)."
2996 (let* ((argns (cl-arglist-args args)) (p argns)
2997 (pbody (cons 'progn body))
2998 (unsafe (not (cl-safe-expr-p pbody))))
2999 (while (and p (eq (cl-expr-contains args (car p)) 1)) (cl-pop p))
3001 (if p nil ; give up if defaults refer to earlier args
3002 (list 'define-compiler-macro name
3003 (list* '&whole 'cl-whole '&cl-quote args)
3004 (list* 'cl-defsubst-expand (list 'quote argns)
3005 (list 'quote (list* 'block name body))
3006 (not (or unsafe (cl-expr-access-order pbody argns)))
3007 (and (memq '&key args) 'cl-whole) unsafe argns)))
3008 (list* 'defun* name args body))))
3010 (defun cl-defsubst-expand (argns body simple whole unsafe &rest argvs)
3011 (if (and whole (not (cl-safe-expr-p (cons 'progn argvs)))) whole
3012 (if (cl-simple-exprs-p argvs) (setq simple t))
3013 (let ((lets (delq nil
3014 (mapcar* #'(lambda (argn argv)
3015 (if (or simple (cl-const-expr-p argv))
3016 (progn (setq body (subst argv argn body))
3017 (and unsafe (list argn argv)))
3020 (if lets (list 'let lets body) body))))
3023 ;;; Compile-time optimizations for some functions defined in this package.
3024 ;;; Note that cl.el arranges to force cl-macs to be loaded at compile-time,
3025 ;;; mainly to make sure these macros will be present.
3027 (put 'eql 'byte-compile nil)
3028 (define-compiler-macro eql (&whole form a b)
3029 (cond ((eq (cl-const-expr-p a) t)
3030 (let ((val (cl-const-expr-val a)))
3031 (if (and (numberp val) (not (integerp val)))
3034 ((eq (cl-const-expr-p b) t)
3035 (let ((val (cl-const-expr-val b)))
3036 (if (and (numberp val) (not (integerp val)))
3039 ((cl-simple-expr-p a 5)
3040 (list 'if (list 'numberp a)
3043 ((and (cl-safe-expr-p a)
3044 (cl-simple-expr-p b 5))
3045 (list 'if (list 'numberp b)
3050 (define-compiler-macro member* (&whole form a list &rest keys)
3051 (let ((test (and (= (length keys) 2) (eq (car keys) ':test)
3052 (cl-const-expr-val (nth 1 keys)))))
3053 (cond ((eq test 'eq) (list 'memq a list))
3054 ((eq test 'equal) (list 'member a list))
3055 ((or (null keys) (eq test 'eql))
3056 (if (eq (cl-const-expr-p a) t)
3057 (list (if (floatp-safe (cl-const-expr-val a)) 'member 'memq)
3059 (if (eq (cl-const-expr-p list) t)
3060 (let ((p (cl-const-expr-val list)) (mb nil) (mq nil))
3062 (and p (list 'eql a (list 'quote (car p))))
3064 (if (floatp-safe (car p)) (setq mb t)
3065 (or (integerp (car p)) (symbolp (car p)) (setq mq t)))
3067 (if (not mb) (list 'memq a list)
3068 (if (not mq) (list 'member a list) form))))
3072 (define-compiler-macro assoc* (&whole form a list &rest keys)
3073 (let ((test (and (= (length keys) 2) (eq (car keys) ':test)
3074 (cl-const-expr-val (nth 1 keys)))))
3075 (cond ((eq test 'eq) (list 'assq a list))
3076 ((eq test 'equal) (list 'assoc a list))
3077 ((and (eq (cl-const-expr-p a) t) (or (null keys) (eq test 'eql)))
3078 (if (floatp-safe (cl-const-expr-val a))
3079 (list 'assoc a list) (list 'assq a list)))
3082 (define-compiler-macro adjoin (&whole form a list &rest keys)
3083 (if (and (cl-simple-expr-p a) (cl-simple-expr-p list)
3084 (not (memq ':key keys)))
3085 (list 'if (list* 'member* a list keys) list (list 'cons a list))
3088 (define-compiler-macro list* (arg &rest others)
3089 (let* ((args (reverse (cons arg others)))
3091 (while (setq args (cdr args))
3092 (setq form (list 'cons (car args) form)))
3095 (define-compiler-macro get* (sym prop &optional default)
3096 (list 'get sym prop default))
3098 (define-compiler-macro getf (sym prop &optional default)
3099 (list 'plist-get sym prop default))
3101 (define-compiler-macro typep (&whole form val type)
3102 (if (cl-const-expr-p type)
3103 (let ((res (cl-make-type-test val (cl-const-expr-val type))))
3104 (if (or (memq (cl-expr-contains res val) '(nil 1))
3105 (cl-simple-expr-p val)) res
3106 (let ((temp (gensym)))
3107 (list 'let (list (list temp val)) (subst temp val res)))))
3113 (put (car y) 'side-effect-free t)
3114 (put (car y) 'byte-compile 'cl-byte-compile-compiler-macro)
3115 (put (car y) 'cl-compiler-macro
3116 (list 'lambda '(w x)
3117 (if (symbolp (cadr y))
3118 (list 'list (list 'quote (cadr y))
3119 (list 'list (list 'quote (caddr y)) 'x))
3120 (cons 'list (cdr y))))))
3121 '((first 'car x) (second 'cadr x) (third 'caddr x) (fourth 'cadddr x)
3122 (fifth 'nth 4 x) (sixth 'nth 5 x) (seventh 'nth 6 x)
3123 (eighth 'nth 7 x) (ninth 'nth 8 x) (tenth 'nth 9 x)
3124 (rest 'cdr x) (endp 'null x) (plusp '> x 0) (minusp '< x 0)
3125 (oddp 'eq (list 'logand x 1) 1)
3126 (evenp 'eq (list 'logand x 1) 0)
3127 (caar car car) (cadr car cdr) (cdar cdr car) (cddr cdr cdr)
3128 (caaar car caar) (caadr car cadr) (cadar car cdar)
3129 (caddr car cddr) (cdaar cdr caar) (cdadr cdr cadr)
3130 (cddar cdr cdar) (cdddr cdr cddr) (caaaar car caaar)
3131 (caaadr car caadr) (caadar car cadar) (caaddr car caddr)
3132 (cadaar car cdaar) (cadadr car cdadr) (caddar car cddar)
3133 (cadddr car cdddr) (cdaaar cdr caaar) (cdaadr cdr caadr)
3134 (cdadar cdr cadar) (cdaddr cdr caddr) (cddaar cdr cdaar)
3135 (cddadr cdr cdadr) (cdddar cdr cddar) (cddddr cdr cdddr)))
3137 ;;; Things that are inline.
3138 (proclaim '(inline floatp-safe acons map concatenate notany notevery
3140 cl-set-elt revappend nreconc
3143 ;;; Things that are side-effect-free. Moved to byte-optimize.el
3144 ;(dolist (fun '(oddp evenp plusp minusp
3145 ; abs expt signum last butlast ldiff
3147 ; isqrt floor* ceiling* truncate* round* mod* rem* subseq
3148 ; list-length getf))
3149 ; (put fun 'side-effect-free t))
3151 ;;; Things that are side-effect-and-error-free. Moved to byte-optimize.el
3152 ;(dolist (fun '(eql floatp-safe list* subst acons equalp random-state-p
3153 ; copy-tree sublis))
3154 ; (put fun 'side-effect-free 'error-free))
3157 (run-hooks 'cl-macs-load-hook)
3159 ;;; cl-macs.el ends here