1 ;;; cl-macs.el --- Common Lisp macros -*- lexical-binding: t; coding: utf-8 -*-
3 ;; Copyright (C) 1993, 2001-2013 Free Software Foundation, Inc.
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
7 ;; Keywords: extensions
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; These are extensions to Emacs Lisp that provide a degree of
28 ;; Common Lisp compatibility, beyond what is already built-in
31 ;; This package was written by Dave Gillespie; it is a complete
32 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
34 ;; Bug reports, comments, and suggestions are welcome!
36 ;; This file contains the portions of the Common Lisp extensions
37 ;; package which should be autoloaded, but need only be present
38 ;; if the compiler or interpreter is used---this file is not
39 ;; necessary for executing compiled code.
41 ;; See cl.el for Change Log.
48 ;; `gv' is required here because cl-macs can be loaded before loaddefs.el.
51 (defmacro cl--pop2 (place)
52 (declare (debug edebug-sexps))
53 `(prog1 (car (cdr ,place))
54 (setq ,place (cdr (cdr ,place)))))
56 (defvar cl--optimize-safety)
57 (defvar cl--optimize-speed)
61 ;; Place compiler macros at the beginning, otherwise uses of the corresponding
62 ;; functions can lead to recursive-loads that prevent the calls from
66 (defun cl--compiler-macro-list* (_form arg &rest others)
67 (let* ((args (reverse (cons arg others)))
69 (while (setq args (cdr args))
70 (setq form `(cons ,(car args) ,form)))
74 (defun cl--compiler-macro-cXXr (form x)
75 (let* ((head (car form))
76 (n (symbol-name (car form)))
78 (if (not (string-match "c[ad]+r\\'" n))
79 (if (and (fboundp head) (symbolp (symbol-function head)))
80 (cl--compiler-macro-cXXr (cons (symbol-function head) (cdr form))
82 (error "Compiler macro for cXXr applied to non-cXXr form"))
83 (while (> i (match-beginning 0))
84 (setq x (list (if (eq (aref n i) ?a) 'car 'cdr) x))
88 ;;; Some predicates for analyzing Lisp forms.
89 ;; These are used by various
90 ;; macro expanders to optimize the results in certain common cases.
92 (defconst cl--simple-funcs '(car cdr nth aref elt if and or + - 1+ 1- min max
93 car-safe cdr-safe progn prog1 prog2))
94 (defconst cl--safe-funcs '(* / % length memq list vector vectorp
97 (defun cl--simple-expr-p (x &optional size)
98 "Check if no side effects, and executes quickly."
99 (or size (setq size 10))
100 (if (and (consp x) (not (memq (car x) '(quote function cl-function))))
101 (and (symbolp (car x))
102 (or (memq (car x) cl--simple-funcs)
103 (get (car x) 'side-effect-free))
105 (setq size (1- size))
106 (while (and (setq x (cdr x))
107 (setq size (cl--simple-expr-p (car x) size))))
108 (and (null x) (>= size 0) size)))
109 (and (> size 0) (1- size))))
111 (defun cl--simple-exprs-p (xs)
112 (while (and xs (cl--simple-expr-p (car xs)))
116 (defun cl--safe-expr-p (x)
117 "Check if no side effects."
118 (or (not (and (consp x) (not (memq (car x) '(quote function cl-function)))))
119 (and (symbolp (car x))
120 (or (memq (car x) cl--simple-funcs)
121 (memq (car x) cl--safe-funcs)
122 (get (car x) 'side-effect-free))
124 (while (and (setq x (cdr x)) (cl--safe-expr-p (car x))))
127 ;;; Check if constant (i.e., no side effects or dependencies).
128 (defun cl--const-expr-p (x)
130 (or (eq (car x) 'quote)
131 (and (memq (car x) '(function cl-function))
132 (or (symbolp (nth 1 x))
133 (and (eq (car-safe (nth 1 x)) 'lambda) 'func)))))
134 ((symbolp x) (and (memq x '(nil t)) t))
137 (defun cl--const-expr-val (x)
138 (and (macroexp-const-p x) (if (consp x) (nth 1 x) x)))
140 (defun cl--expr-contains (x y)
141 "Count number of times X refers to Y. Return nil for 0 times."
142 ;; FIXME: This is naive, and it will cl-count Y as referred twice in
143 ;; (let ((Y 1)) Y) even though it should be 0. Also it is often called on
144 ;; non-macroexpanded code, so it may also miss some occurrences that would
145 ;; only appear in the expanded code.
146 (cond ((equal y x) 1)
147 ((and (consp x) (not (memq (car x) '(quote function cl-function))))
150 (setq sum (+ sum (or (cl--expr-contains (pop x) y) 0))))
151 (setq sum (+ sum (or (cl--expr-contains x y) 0)))
152 (and (> sum 0) sum)))
155 (defun cl--expr-contains-any (x y)
156 (while (and y (not (cl--expr-contains x (car y)))) (pop y))
159 (defun cl--expr-depends-p (x y)
160 "Check whether X may depend on any of the symbols in Y."
161 (and (not (macroexp-const-p x))
162 (or (not (cl--safe-expr-p x)) (cl--expr-contains-any x y))))
166 (defvar cl--gensym-counter)
168 (defun cl-gensym (&optional prefix)
169 "Generate a new uninterned symbol.
170 The name is made by appending a number to PREFIX, default \"G\"."
171 (let ((pfix (if (stringp prefix) prefix "G"))
172 (num (if (integerp prefix) prefix
173 (prog1 cl--gensym-counter
174 (setq cl--gensym-counter (1+ cl--gensym-counter))))))
175 (make-symbol (format "%s%d" pfix num))))
178 (defun cl-gentemp (&optional prefix)
179 "Generate a new interned symbol with a unique name.
180 The name is made by appending a number to PREFIX, default \"G\"."
181 (let ((pfix (if (stringp prefix) prefix "G"))
183 (while (intern-soft (setq name (format "%s%d" pfix cl--gensym-counter)))
184 (setq cl--gensym-counter (1+ cl--gensym-counter)))
188 ;;; Program structure.
190 (def-edebug-spec cl-declarations
191 (&rest ("cl-declare" &rest sexp)))
193 (def-edebug-spec cl-declarations-or-string
194 (&or stringp cl-declarations))
196 (def-edebug-spec cl-lambda-list
198 [&optional ["&optional" cl-&optional-arg &rest cl-&optional-arg]]
199 [&optional ["&rest" arg]]
200 [&optional ["&key" [cl-&key-arg &rest cl-&key-arg]
201 &optional "&allow-other-keys"]]
202 [&optional ["&aux" &rest
203 &or (symbolp &optional def-form) symbolp]]
206 (def-edebug-spec cl-&optional-arg
207 (&or (arg &optional def-form arg) arg))
209 (def-edebug-spec cl-&key-arg
210 (&or ([&or (symbolp arg) arg] &optional def-form arg) arg))
212 (defconst cl--lambda-list-keywords
213 '(&optional &rest &key &allow-other-keys &aux &whole &body &environment))
215 (defvar cl--bind-block) (defvar cl--bind-defs) (defvar cl--bind-enquote)
216 (defvar cl--bind-inits) (defvar cl--bind-lets) (defvar cl--bind-forms)
218 (defun cl--transform-lambda (form bind-block)
219 "Transform a function form FORM of name BIND-BLOCK.
220 BIND-BLOCK is the name of the symbol to which the function will be bound,
221 and which will be used for the name of the `cl-block' surrounding the
223 FORM is of the form (ARGS . BODY)."
224 (let* ((args (car form)) (body (cdr form)) (orig-args args)
225 (cl--bind-block bind-block) (cl--bind-defs nil) (cl--bind-enquote nil)
226 (cl--bind-inits nil) (cl--bind-lets nil) (cl--bind-forms nil)
227 (header nil) (simple-args nil))
228 (while (or (stringp (car body))
229 (memq (car-safe (car body)) '(interactive declare cl-declare)))
230 (push (pop body) header))
231 (setq args (if (listp args) (cl-copy-list args) (list '&rest args)))
232 (let ((p (last args))) (if (cdr p) (setcdr p (list '&rest (cdr p)))))
233 (if (setq cl--bind-defs (cadr (memq '&cl-defs args)))
234 (setq args (delq '&cl-defs (delq cl--bind-defs args))
235 cl--bind-defs (cadr cl--bind-defs)))
236 (if (setq cl--bind-enquote (memq '&cl-quote args))
237 (setq args (delq '&cl-quote args)))
238 (if (memq '&whole args) (error "&whole not currently implemented"))
239 (let* ((p (memq '&environment args)) (v (cadr p))
240 (env-exp 'macroexpand-all-environment))
241 (if p (setq args (nconc (delq (car p) (delq v args))
242 (list '&aux (list v env-exp))))))
243 (while (and args (symbolp (car args))
244 (not (memq (car args) '(nil &rest &body &key &aux)))
245 (not (and (eq (car args) '&optional)
246 (or cl--bind-defs (consp (cadr args))))))
247 (push (pop args) simple-args))
248 (or (eq cl--bind-block 'cl-none)
249 (setq body (list `(cl-block ,cl--bind-block ,@body))))
251 (cl-list* nil (nreverse simple-args) (nconc (nreverse header) body))
252 (if (memq '&optional simple-args) (push '&optional args))
253 (cl--do-arglist args nil (- (length simple-args)
254 (if (memq '&optional simple-args) 1 0)))
255 (setq cl--bind-lets (nreverse cl--bind-lets))
256 (cl-list* (and cl--bind-inits `(cl-eval-when (compile load eval)
257 ,@(nreverse cl--bind-inits)))
258 (nconc (nreverse simple-args)
259 (list '&rest (car (pop cl--bind-lets))))
260 (nconc (let ((hdr (nreverse header)))
261 ;; Macro expansion can take place in the middle of
262 ;; apparently harmless computation, so it should not
263 ;; touch the match-data.
266 (cons (help-add-fundoc-usage
267 (if (stringp (car hdr)) (pop hdr))
268 ;; Be careful with make-symbol and (back)quote,
270 (let ((print-gensym nil) (print-quoted t))
271 (format "%S" (cons 'fn (cl--make-usage-args
274 (list `(let* ,cl--bind-lets
275 ,@(nreverse cl--bind-forms)
279 (defmacro cl-defun (name args &rest body)
280 "Define NAME as a function.
281 Like normal `defun', except ARGLIST allows full Common Lisp conventions,
282 and BODY is implicitly surrounded by (cl-block NAME ...).
284 \(fn NAME ARGLIST [DOCSTRING] BODY...)"
286 ;; Same as defun but use cl-lambda-list.
287 (&define [&or name ("setf" :name setf name)]
289 cl-declarations-or-string
290 [&optional ("interactive" interactive)]
294 (let* ((res (cl--transform-lambda (cons args body) name))
295 (form `(defun ,name ,@(cdr res))))
296 (if (car res) `(progn ,(car res) ,form) form)))
298 ;; The lambda list for macros is different from that of normal lambdas.
299 ;; Note that &environment is only allowed as first or last items in the
302 (def-edebug-spec cl-macro-list
303 (([&optional "&environment" arg]
305 [&optional ["&optional" &rest
306 &or (cl-macro-arg &optional def-form cl-macro-arg) arg]]
307 [&optional [[&or "&rest" "&body"] cl-macro-arg]]
308 [&optional ["&key" [&rest
309 [&or ([&or (symbolp cl-macro-arg) arg]
310 &optional def-form cl-macro-arg)
312 &optional "&allow-other-keys"]]
313 [&optional ["&aux" &rest
314 &or (symbolp &optional def-form) symbolp]]
315 [&optional "&environment" arg]
318 (def-edebug-spec cl-macro-arg
319 (&or arg cl-macro-list1))
321 (def-edebug-spec cl-macro-list1
322 (([&optional "&whole" arg] ;; only allowed at lower levels
324 [&optional ["&optional" &rest
325 &or (cl-macro-arg &optional def-form cl-macro-arg) arg]]
326 [&optional [[&or "&rest" "&body"] cl-macro-arg]]
327 [&optional ["&key" [&rest
328 [&or ([&or (symbolp cl-macro-arg) arg]
329 &optional def-form cl-macro-arg)
331 &optional "&allow-other-keys"]]
332 [&optional ["&aux" &rest
333 &or (symbolp &optional def-form) symbolp]]
337 (defmacro cl-defmacro (name args &rest body)
338 "Define NAME as a macro.
339 Like normal `defmacro', except ARGLIST allows full Common Lisp conventions,
340 and BODY is implicitly surrounded by (cl-block NAME ...).
342 \(fn NAME ARGLIST [DOCSTRING] BODY...)"
344 (&define name cl-macro-list cl-declarations-or-string def-body))
347 (let* ((res (cl--transform-lambda (cons args body) name))
348 (form `(defmacro ,name ,@(cdr res))))
349 (if (car res) `(progn ,(car res) ,form) form)))
351 (def-edebug-spec cl-lambda-expr
352 (&define ("lambda" cl-lambda-list
353 ;;cl-declarations-or-string
354 ;;[&optional ("interactive" interactive)]
357 ;; Redefine function-form to also match cl-function
358 (def-edebug-spec function-form
359 ;; form at the end could also handle "function",
360 ;; but recognize it specially to avoid wrapping function forms.
361 (&or ([&or "quote" "function"] &or symbolp lambda-expr)
362 ("cl-function" cl-function)
366 (defmacro cl-function (func)
367 "Introduce a function.
368 Like normal `function', except that if argument is a lambda form,
369 its argument list allows full Common Lisp conventions."
370 (declare (debug (&or symbolp cl-lambda-expr)))
371 (if (eq (car-safe func) 'lambda)
372 (let* ((res (cl--transform-lambda (cdr func) 'cl-none))
373 (form `(function (lambda . ,(cdr res)))))
374 (if (car res) `(progn ,(car res) ,form) form))
377 (declare-function help-add-fundoc-usage "help-fns" (docstring arglist))
379 (defun cl--make-usage-var (x)
380 "X can be a var or a (destructuring) lambda-list."
382 ((symbolp x) (make-symbol (upcase (symbol-name x))))
383 ((consp x) (cl--make-usage-args x))
386 (defun cl--make-usage-args (arglist)
387 (if (cdr-safe (last arglist)) ;Not a proper list.
388 (let* ((last (last arglist))
393 (nconc (cl--make-usage-args arglist) (cl--make-usage-var tail)))
395 ;; `orig-args' can contain &cl-defs (an internal
396 ;; CL thingy I don't understand), so remove it.
397 (let ((x (memq '&cl-defs arglist)))
398 (when x (setq arglist (delq (car x) (remq (cadr x) arglist)))))
403 (let ((first (aref (symbol-name x) 0)))
406 ;; Strip a leading underscore, since it only
407 ;; means that this argument is unused.
408 (make-symbol (upcase (if (eq ?_ first)
409 (substring (symbol-name x) 1)
410 (symbol-name x)))))))
412 ((memq state '(nil &rest)) (cl--make-usage-args x))
413 (t ;(VAR INITFORM SVAR) or ((KEYWORD VAR) INITFORM SVAR).
415 (if (and (consp (car x)) (eq state '&key))
416 (list (caar x) (cl--make-usage-var (nth 1 (car x))))
417 (cl--make-usage-var (car x)))
419 (cl--make-usage-args (nthcdr 2 x)) ;SVAR.
423 (defun cl--do-arglist (args expr &optional num) ; uses bind-*
425 (if (or (memq args cl--lambda-list-keywords) (not (symbolp args)))
426 (error "Invalid argument name: %s" args)
427 (push (list args expr) cl--bind-lets))
428 (setq args (cl-copy-list args))
429 (let ((p (last args))) (if (cdr p) (setcdr p (list '&rest (cdr p)))))
430 (let ((p (memq '&body args))) (if p (setcar p '&rest)))
431 (if (memq '&environment args) (error "&environment used incorrectly"))
432 (let ((save-args args)
433 (restarg (memq '&rest args))
434 (safety (if (cl--compiling-file) cl--optimize-safety 3))
436 (laterarg nil) (exactarg nil) minarg)
437 (or num (setq num 0))
438 (if (listp (cadr restarg))
439 (setq restarg (make-symbol "--cl-rest--"))
440 (setq restarg (cadr restarg)))
441 (push (list restarg expr) cl--bind-lets)
442 (if (eq (car args) '&whole)
443 (push (list (cl--pop2 args) restarg) cl--bind-lets))
445 (setq minarg restarg)
446 (while (and p (not (memq (car p) cl--lambda-list-keywords)))
447 (or (eq p args) (setq minarg (list 'cdr minarg)))
449 (if (memq (car p) '(nil &aux))
450 (setq minarg `(= (length ,restarg)
451 ,(length (cl-ldiff args p)))
452 exactarg (not (eq args p)))))
453 (while (and args (not (memq (car args) cl--lambda-list-keywords)))
454 (let ((poparg (list (if (or (cdr args) (not exactarg)) 'pop 'car)
458 (if (or laterarg (= safety 0)) poparg
460 (signal 'wrong-number-of-arguments
461 (list ,(and (not (eq cl--bind-block 'cl-none))
463 (length ,restarg)))))))
464 (setq num (1+ num) laterarg t))
465 (while (and (eq (car args) '&optional) (pop args))
466 (while (and args (not (memq (car args) cl--lambda-list-keywords)))
467 (let ((arg (pop args)))
468 (or (consp arg) (setq arg (list arg)))
469 (if (cddr arg) (cl--do-arglist (nth 2 arg) `(and ,restarg t)))
470 (let ((def (if (cdr arg) (nth 1 arg)
471 (or (car cl--bind-defs)
472 (nth 1 (assq (car arg) cl--bind-defs)))))
473 (poparg `(pop ,restarg)))
474 (and def cl--bind-enquote (setq def `',def))
475 (cl--do-arglist (car arg)
476 (if def `(if ,restarg ,poparg ,def) poparg))
477 (setq num (1+ num))))))
478 (if (eq (car args) '&rest)
479 (let ((arg (cl--pop2 args)))
480 (if (consp arg) (cl--do-arglist arg restarg)))
481 (or (eq (car args) '&key) (= safety 0) exactarg
483 (signal 'wrong-number-of-arguments
485 ,(and (not (eq cl--bind-block 'cl-none))
487 (+ ,num (length ,restarg)))))
489 (while (and (eq (car args) '&key) (pop args))
490 (while (and args (not (memq (car args) cl--lambda-list-keywords)))
491 (let ((arg (pop args)))
492 (or (consp arg) (setq arg (list arg)))
493 (let* ((karg (if (consp (car arg)) (caar arg)
494 (let ((name (symbol-name (car arg))))
495 ;; Strip a leading underscore, since it only
496 ;; means that this argument is unused, but
497 ;; shouldn't affect the key's name (bug#12367).
498 (if (eq ?_ (aref name 0))
499 (setq name (substring name 1)))
500 (intern (format ":%s" name)))))
501 (varg (if (consp (car arg)) (cl-cadar arg) (car arg)))
502 (def (if (cdr arg) (cadr arg)
503 (or (car cl--bind-defs) (cadr (assq varg cl--bind-defs)))))
504 (look `(memq ',karg ,restarg)))
505 (and def cl--bind-enquote (setq def `',def))
507 (let* ((temp (or (nth 2 arg) (make-symbol "--cl-var--")))
508 (val `(car (cdr ,temp))))
509 (cl--do-arglist temp look)
512 (prog1 ,val (setq ,temp t))
516 `(car (cdr ,(if (null def)
519 ,(if (eq (cl--const-expr-p def) t)
520 `'(nil ,(cl--const-expr-val def))
521 `(list nil ,def))))))))
523 (setq keys (nreverse keys))
524 (or (and (eq (car args) '&allow-other-keys) (pop args))
525 (null keys) (= safety 0)
526 (let* ((var (make-symbol "--cl-keys--"))
527 (allow '(:allow-other-keys))
530 ((memq (car ,var) ',(append keys allow))
531 (setq ,var (cdr (cdr ,var))))
532 ((car (cdr (memq (quote ,@allow) ,restarg)))
536 ,(format "Keyword argument %%s not one of %s"
539 (push `(let ((,var ,restarg)) ,check) cl--bind-forms)))
540 (while (and (eq (car args) '&aux) (pop args))
541 (while (and args (not (memq (car args) cl--lambda-list-keywords)))
542 (if (consp (car args))
543 (if (and cl--bind-enquote (cl-cadar args))
544 (cl--do-arglist (caar args)
545 `',(cadr (pop args)))
546 (cl--do-arglist (caar args) (cadr (pop args))))
547 (cl--do-arglist (pop args) nil))))
548 (if args (error "Malformed argument list %s" save-args)))))
550 (defun cl--arglist-args (args)
551 (if (nlistp args) (list args)
552 (let ((res nil) (kind nil) arg)
554 (setq arg (pop args))
555 (if (memq arg cl--lambda-list-keywords) (setq kind arg)
556 (if (eq arg '&cl-defs) (pop args)
557 (and (consp arg) kind (setq arg (car arg)))
558 (and (consp arg) (cdr arg) (eq kind '&key) (setq arg (cadr arg)))
559 (setq res (nconc res (cl--arglist-args arg))))))
560 (nconc res (and args (list args))))))
563 (defmacro cl-destructuring-bind (args expr &rest body)
564 "Bind the variables in ARGS to the result of EXPR and execute BODY."
566 (debug (&define cl-macro-list def-form cl-declarations def-body)))
567 (let* ((cl--bind-lets nil) (cl--bind-forms nil) (cl--bind-inits nil)
568 (cl--bind-defs nil) (cl--bind-block 'cl-none) (cl--bind-enquote nil))
569 (cl--do-arglist (or args '(&aux)) expr)
570 (append '(progn) cl--bind-inits
571 (list `(let* ,(nreverse cl--bind-lets)
572 ,@(nreverse cl--bind-forms) ,@body)))))
575 ;;; The `cl-eval-when' form.
577 (defvar cl--not-toplevel nil)
580 (defmacro cl-eval-when (when &rest body)
581 "Control when BODY is evaluated.
582 If `compile' is in WHEN, BODY is evaluated when compiled at top-level.
583 If `load' is in WHEN, BODY is evaluated when loaded after top-level compile.
584 If `eval' is in WHEN, BODY is evaluated when interpreted or at non-top-level.
586 \(fn (WHEN...) BODY...)"
587 (declare (indent 1) (debug ((&rest &or "compile" "load" "eval") body)))
588 (if (and (fboundp 'cl--compiling-file) (cl--compiling-file)
589 (not cl--not-toplevel) (not (boundp 'for-effect))) ;Horrible kludge.
590 (let ((comp (or (memq 'compile when) (memq :compile-toplevel when)))
591 (cl--not-toplevel t))
592 (if (or (memq 'load when) (memq :load-toplevel when))
593 (if comp (cons 'progn (mapcar 'cl--compile-time-too body))
594 `(if nil nil ,@body))
595 (progn (if comp (eval (cons 'progn body))) nil)))
596 (and (or (memq 'eval when) (memq :execute when))
597 (cons 'progn body))))
599 (defun cl--compile-time-too (form)
600 (or (and (symbolp (car-safe form)) (get (car-safe form) 'byte-hunk-handler))
601 (setq form (macroexpand
602 form (cons '(cl-eval-when) byte-compile-macro-environment))))
603 (cond ((eq (car-safe form) 'progn)
604 (cons 'progn (mapcar 'cl--compile-time-too (cdr form))))
605 ((eq (car-safe form) 'cl-eval-when)
606 (let ((when (nth 1 form)))
607 (if (or (memq 'eval when) (memq :execute when))
608 `(cl-eval-when (compile ,@when) ,@(cddr form))
610 (t (eval form) form)))
613 (defmacro cl-load-time-value (form &optional _read-only)
614 "Like `progn', but evaluates the body at load time.
615 The result of the body appears to the compiler as a quoted constant."
616 (declare (debug (form &optional sexp)))
617 (if (cl--compiling-file)
618 (let* ((temp (cl-gentemp "--cl-load-time--"))
619 (set `(set ',temp ,form)))
620 (if (and (fboundp 'byte-compile-file-form-defmumble)
621 (boundp 'this-kind) (boundp 'that-one))
622 (fset 'byte-compile-file-form
624 (fset 'byte-compile-file-form
625 ',(symbol-function 'byte-compile-file-form))
626 (byte-compile-file-form ',set)
627 (byte-compile-file-form form)))
628 (print set (symbol-value 'byte-compile--outbuffer)))
629 `(symbol-value ',temp))
633 ;;; Conditional control structures.
636 (defmacro cl-case (expr &rest clauses)
637 "Eval EXPR and choose among clauses on that value.
638 Each clause looks like (KEYLIST BODY...). EXPR is evaluated and compared
639 against each key in each KEYLIST; the corresponding BODY is evaluated.
640 If no clause succeeds, cl-case returns nil. A single atom may be used in
641 place of a KEYLIST of one atom. A KEYLIST of t or `otherwise' is
642 allowed only in the final clause, and matches if no other keys match.
643 Key values are compared by `eql'.
644 \n(fn EXPR (KEYLIST BODY...)...)"
645 (declare (indent 1) (debug (form &rest (sexp body))))
646 (let* ((temp (if (cl--simple-expr-p expr 3) expr (make-symbol "--cl-var--")))
653 (cons (cond ((memq (car c) '(t otherwise)) t)
654 ((eq (car c) 'cl--ecase-error-flag)
655 `(error "cl-ecase failed: %s, %s"
656 ,temp ',(reverse head-list)))
658 (setq head-list (append (car c) head-list))
659 `(cl-member ,temp ',(car c)))
661 (if (memq (car c) head-list)
662 (error "Duplicate key in case: %s"
664 (push (car c) head-list)
665 `(eql ,temp ',(car c))))
666 (or (cdr c) '(nil)))))
668 (if (eq temp expr) body
669 `(let ((,temp ,expr)) ,body))))
672 (defmacro cl-ecase (expr &rest clauses)
673 "Like `cl-case', but error if no case fits.
674 `otherwise'-clauses are not allowed.
675 \n(fn EXPR (KEYLIST BODY...)...)"
676 (declare (indent 1) (debug cl-case))
677 `(cl-case ,expr ,@clauses (cl--ecase-error-flag)))
680 (defmacro cl-typecase (expr &rest clauses)
681 "Evals EXPR, chooses among clauses on that value.
682 Each clause looks like (TYPE BODY...). EXPR is evaluated and, if it
683 satisfies TYPE, the corresponding BODY is evaluated. If no clause succeeds,
684 cl-typecase returns nil. A TYPE of t or `otherwise' is allowed only in the
685 final clause, and matches if no other keys match.
686 \n(fn EXPR (TYPE BODY...)...)"
688 (debug (form &rest ([&or cl-type-spec "otherwise"] body))))
689 (let* ((temp (if (cl--simple-expr-p expr 3) expr (make-symbol "--cl-var--")))
696 (cons (cond ((eq (car c) 'otherwise) t)
697 ((eq (car c) 'cl--ecase-error-flag)
698 `(error "cl-etypecase failed: %s, %s"
699 ,temp ',(reverse type-list)))
701 (push (car c) type-list)
702 (cl--make-type-test temp (car c))))
703 (or (cdr c) '(nil)))))
705 (if (eq temp expr) body
706 `(let ((,temp ,expr)) ,body))))
709 (defmacro cl-etypecase (expr &rest clauses)
710 "Like `cl-typecase', but error if no case fits.
711 `otherwise'-clauses are not allowed.
712 \n(fn EXPR (TYPE BODY...)...)"
713 (declare (indent 1) (debug cl-typecase))
714 `(cl-typecase ,expr ,@clauses (cl--ecase-error-flag)))
717 ;;; Blocks and exits.
720 (defmacro cl-block (name &rest body)
721 "Define a lexically-scoped block named NAME.
722 NAME may be any symbol. Code inside the BODY forms can call `cl-return-from'
723 to jump prematurely out of the block. This differs from `catch' and `throw'
724 in two respects: First, the NAME is an unevaluated symbol rather than a
725 quoted symbol or other form; and second, NAME is lexically rather than
726 dynamically scoped: Only references to it within BODY will work. These
727 references may appear inside macro expansions, but not inside functions
729 (declare (indent 1) (debug (symbolp body)))
730 (if (cl--safe-expr-p `(progn ,@body)) `(progn ,@body)
732 (catch ',(intern (format "--cl-block-%s--" name))
736 (defmacro cl-return (&optional result)
737 "Return from the block named nil.
738 This is equivalent to `(cl-return-from nil RESULT)'."
739 (declare (debug (&optional form)))
740 `(cl-return-from nil ,result))
743 (defmacro cl-return-from (name &optional result)
744 "Return from the block named NAME.
745 This jumps out to the innermost enclosing `(cl-block NAME ...)' form,
746 returning RESULT from that form (or nil if RESULT is omitted).
747 This is compatible with Common Lisp, but note that `defun' and
748 `defmacro' do not create implicit blocks as they do in Common Lisp."
749 (declare (indent 1) (debug (symbolp &optional form)))
750 (let ((name2 (intern (format "--cl-block-%s--" name))))
751 `(cl--block-throw ',name2 ,result)))
754 ;;; The "cl-loop" macro.
756 (defvar cl--loop-args) (defvar cl--loop-accum-var) (defvar cl--loop-accum-vars)
757 (defvar cl--loop-bindings) (defvar cl--loop-body) (defvar cl--loop-destr-temps)
758 (defvar cl--loop-finally) (defvar cl--loop-finish-flag)
759 (defvar cl--loop-first-flag)
760 (defvar cl--loop-initially) (defvar cl--loop-map-form) (defvar cl--loop-name)
761 (defvar cl--loop-result) (defvar cl--loop-result-explicit)
762 (defvar cl--loop-result-var) (defvar cl--loop-steps)
763 (defvar cl--loop-symbol-macs)
766 (defmacro cl-loop (&rest loop-args)
767 "The Common Lisp `loop' macro.
769 for VAR from/upfrom/downfrom NUM to/upto/downto/above/below NUM by NUM,
770 for VAR in LIST by FUNC, for VAR on LIST by FUNC, for VAR = INIT then EXPR,
771 for VAR across ARRAY, repeat NUM, with VAR = INIT, while COND, until COND,
772 always COND, never COND, thereis COND, collect EXPR into VAR,
773 append EXPR into VAR, nconc EXPR into VAR, sum EXPR into VAR,
774 count EXPR into VAR, maximize EXPR into VAR, minimize EXPR into VAR,
775 if COND CLAUSE [and CLAUSE]... else CLAUSE [and CLAUSE...],
776 unless COND CLAUSE [and CLAUSE]... else CLAUSE [and CLAUSE...],
777 do EXPRS..., initially EXPRS..., finally EXPRS..., return EXPR,
778 finally return EXPR, named NAME.
781 (declare (debug (&rest &or
782 ;; These are usually followed by a symbol, but it can
783 ;; actually be any destructuring-bind pattern, which
784 ;; would erroneously match `form'.
785 [[&or "for" "as" "with" "and"] sexp]
786 ;; These are followed by expressions which could
787 ;; erroneously match `symbolp'.
788 [[&or "from" "upfrom" "downfrom" "to" "upto" "downto"
789 "above" "below" "by" "in" "on" "=" "across"
790 "repeat" "while" "until" "always" "never"
791 "thereis" "collect" "append" "nconc" "sum"
792 "count" "maximize" "minimize" "if" "unless"
794 ;; Simple default, which covers 99% of the cases.
796 (if (not (memq t (mapcar #'symbolp
797 (delq nil (delq t (cl-copy-list loop-args))))))
798 `(cl-block nil (while t ,@loop-args))
799 (let ((cl--loop-args loop-args) (cl--loop-name nil) (cl--loop-bindings nil)
800 (cl--loop-body nil) (cl--loop-steps nil)
801 (cl--loop-result nil) (cl--loop-result-explicit nil)
802 (cl--loop-result-var nil) (cl--loop-finish-flag nil)
803 (cl--loop-accum-var nil) (cl--loop-accum-vars nil)
804 (cl--loop-initially nil) (cl--loop-finally nil)
805 (cl--loop-map-form nil) (cl--loop-first-flag nil)
806 (cl--loop-destr-temps nil) (cl--loop-symbol-macs nil))
807 (setq cl--loop-args (append cl--loop-args '(cl-end-loop)))
808 (while (not (eq (car cl--loop-args) 'cl-end-loop))
809 (cl--parse-loop-clause))
810 (if cl--loop-finish-flag
811 (push `((,cl--loop-finish-flag t)) cl--loop-bindings))
812 (if cl--loop-first-flag
813 (progn (push `((,cl--loop-first-flag t)) cl--loop-bindings)
814 (push `(setq ,cl--loop-first-flag nil) cl--loop-steps)))
815 (let* ((epilogue (nconc (nreverse cl--loop-finally)
816 (list (or cl--loop-result-explicit
818 (ands (cl--loop-build-ands (nreverse cl--loop-body)))
819 (while-body (nconc (cadr ands) (nreverse cl--loop-steps)))
821 (nreverse cl--loop-initially)
822 (list (if cl--loop-map-form
823 `(cl-block --cl-finish--
825 (if (eq (car ands) t) while-body
826 (cons `(or ,(car ands)
827 (cl-return-from --cl-finish--
830 '--cl-map cl--loop-map-form))
831 `(while ,(car ands) ,@while-body)))
832 (if cl--loop-finish-flag
833 (if (equal epilogue '(nil)) (list cl--loop-result-var)
834 `((if ,cl--loop-finish-flag
835 (progn ,@epilogue) ,cl--loop-result-var)))
837 (if cl--loop-result-var
838 (push (list cl--loop-result-var) cl--loop-bindings))
839 (while cl--loop-bindings
840 (if (cdar cl--loop-bindings)
841 (setq body (list (cl--loop-let (pop cl--loop-bindings) body t)))
843 (while (and cl--loop-bindings
844 (not (cdar cl--loop-bindings)))
845 (push (car (pop cl--loop-bindings)) lets))
846 (setq body (list (cl--loop-let lets body nil))))))
847 (if cl--loop-symbol-macs
849 (list `(cl-symbol-macrolet ,cl--loop-symbol-macs ,@body))))
850 `(cl-block ,cl--loop-name ,@body)))))
852 ;; Below is a complete spec for cl-loop, in several parts that correspond
853 ;; to the syntax given in CLtL2. The specs do more than specify where
854 ;; the forms are; it also specifies, as much as Edebug allows, all the
855 ;; syntactically valid cl-loop clauses. The disadvantage of this
856 ;; completeness is rigidity, but the "for ... being" clause allows
857 ;; arbitrary extensions of the form: [symbolp &rest &or symbolp form].
859 ;; (def-edebug-spec cl-loop
860 ;; ([&optional ["named" symbolp]]
866 ;; loop-initial-final]
867 ;; [&rest loop-clause]
870 ;; (def-edebug-spec loop-with
873 ;; [&optional ["=" form]]
874 ;; &rest ["and" loop-var
876 ;; [&optional ["=" form]]]))
878 ;; (def-edebug-spec loop-for-as
879 ;; ([&or "for" "as"] loop-for-as-subclause
880 ;; &rest ["and" loop-for-as-subclause]))
882 ;; (def-edebug-spec loop-for-as-subclause
886 ;; [[&or "in" "on" "in-ref" "across-ref"]
887 ;; form &optional ["by" function-form]]
889 ;; ["=" form &optional ["then" form]]
892 ;; [&or "the" "each"]
894 ;; [[&or "element" "elements"]
895 ;; [&or "of" "in" "of-ref"] form
896 ;; &optional "using" ["index" symbolp]];; is this right?
897 ;; [[&or "hash-key" "hash-keys"
898 ;; "hash-value" "hash-values"]
900 ;; hash-table-p &optional ["using" ([&or "hash-value" "hash-values"
901 ;; "hash-key" "hash-keys"] sexp)]]
903 ;; [[&or "symbol" "present-symbol" "external-symbol"
904 ;; "symbols" "present-symbols" "external-symbols"]
905 ;; [&or "in" "of"] package-p]
907 ;; ;; Extensions for Emacs Lisp, including Lucid Emacs.
908 ;; [[&or "frame" "frames"
909 ;; "screen" "screens"
910 ;; "buffer" "buffers"]]
912 ;; [[&or "window" "windows"]
913 ;; [&or "of" "in"] form]
915 ;; [[&or "overlay" "overlays"
916 ;; "extent" "extents"]
917 ;; [&or "of" "in"] form
918 ;; &optional [[&or "from" "to"] form]]
920 ;; [[&or "interval" "intervals"]
921 ;; [&or "in" "of"] form
922 ;; &optional [[&or "from" "to"] form]
923 ;; ["property" form]]
925 ;; [[&or "key-code" "key-codes"
926 ;; "key-seq" "key-seqs"
927 ;; "key-binding" "key-bindings"]
928 ;; [&or "in" "of"] form
929 ;; &optional ["using" ([&or "key-code" "key-codes"
930 ;; "key-seq" "key-seqs"
931 ;; "key-binding" "key-bindings"]
933 ;; ;; For arbitrary extensions, recognize anything else.
934 ;; [symbolp &rest &or symbolp form]
937 ;; ;; arithmetic - must be last since all parts are optional.
938 ;; [[&optional [[&or "from" "downfrom" "upfrom"] form]]
939 ;; [&optional [[&or "to" "downto" "upto" "below" "above"] form]]
940 ;; [&optional ["by" form]]
943 ;; (def-edebug-spec loop-initial-final
945 ;; ;; [&optional &or "do" "doing"] ;; CLtL2 doesn't allow this.
946 ;; &rest loop-non-atomic-expr]
948 ;; [[&optional &or "do" "doing"] &rest loop-non-atomic-expr]
949 ;; ["return" form]]))
951 ;; (def-edebug-spec loop-and-clause
952 ;; (loop-clause &rest ["and" loop-clause]))
954 ;; (def-edebug-spec loop-clause
956 ;; [[&or "while" "until" "always" "never" "thereis"] form]
958 ;; [[&or "collect" "collecting"
959 ;; "append" "appending"
960 ;; "nconc" "nconcing"
961 ;; "concat" "vconcat"] form
962 ;; [&optional ["into" loop-var]]]
964 ;; [[&or "count" "counting"
966 ;; "maximize" "maximizing"
967 ;; "minimize" "minimizing"] form
968 ;; [&optional ["into" loop-var]]
971 ;; [[&or "if" "when" "unless"]
972 ;; form loop-and-clause
973 ;; [&optional ["else" loop-and-clause]]
974 ;; [&optional "end"]]
976 ;; [[&or "do" "doing"] &rest loop-non-atomic-expr]
979 ;; loop-initial-final
982 ;; (def-edebug-spec loop-non-atomic-expr
983 ;; ([¬ atom] form))
985 ;; (def-edebug-spec loop-var
986 ;; ;; The symbolp must be last alternative to recognize e.g. (a b . c)
988 ;; ;; (loop-var . [&or nil loop-var])
989 ;; ;; (symbolp . [&or nil loop-var])
990 ;; ;; (symbolp . loop-var)
991 ;; ;; (symbolp . (symbolp . [&or nil loop-var]))
992 ;; ;; (symbolp . (symbolp . loop-var))
993 ;; ;; (symbolp . (symbolp . symbolp)) == (symbolp symbolp . symbolp)
994 ;; (&or (loop-var . [&or nil loop-var]) [gate symbolp]))
996 ;; (def-edebug-spec loop-type-spec
997 ;; (&optional ["of-type" loop-d-type-spec]))
999 ;; (def-edebug-spec loop-d-type-spec
1000 ;; (&or (loop-d-type-spec . [&or nil loop-d-type-spec]) cl-type-spec))
1004 (defun cl--parse-loop-clause () ; uses loop-*
1005 (let ((word (pop cl--loop-args))
1006 (hash-types '(hash-key hash-keys hash-value hash-values))
1007 (key-types '(key-code key-codes key-seq key-seqs
1008 key-binding key-bindings)))
1011 ((null cl--loop-args)
1012 (error "Malformed `cl-loop' macro"))
1015 (setq cl--loop-name (pop cl--loop-args)))
1017 ((eq word 'initially)
1018 (if (memq (car cl--loop-args) '(do doing)) (pop cl--loop-args))
1019 (or (consp (car cl--loop-args))
1020 (error "Syntax error on `initially' clause"))
1021 (while (consp (car cl--loop-args))
1022 (push (pop cl--loop-args) cl--loop-initially)))
1025 (if (eq (car cl--loop-args) 'return)
1026 (setq cl--loop-result-explicit
1027 (or (cl--pop2 cl--loop-args) '(quote nil)))
1028 (if (memq (car cl--loop-args) '(do doing)) (pop cl--loop-args))
1029 (or (consp (car cl--loop-args))
1030 (error "Syntax error on `finally' clause"))
1031 (if (and (eq (caar cl--loop-args) 'return) (null cl--loop-name))
1032 (setq cl--loop-result-explicit
1033 (or (nth 1 (pop cl--loop-args)) '(quote nil)))
1034 (while (consp (car cl--loop-args))
1035 (push (pop cl--loop-args) cl--loop-finally)))))
1037 ((memq word '(for as))
1038 (let ((loop-for-bindings nil) (loop-for-sets nil) (loop-for-steps nil)
1041 ;; Use `cl-gensym' rather than `make-symbol'. It's important that
1042 ;; (not (eq (symbol-name var1) (symbol-name var2))) because
1043 ;; these vars get added to the macro-environment.
1044 (let ((var (or (pop cl--loop-args) (cl-gensym "--cl-var--"))))
1045 (setq word (pop cl--loop-args))
1046 (if (eq word 'being) (setq word (pop cl--loop-args)))
1047 (if (memq word '(the each)) (setq word (pop cl--loop-args)))
1048 (if (memq word '(buffer buffers))
1050 cl--loop-args (cons '(buffer-list) cl--loop-args)))
1053 ((memq word '(from downfrom upfrom to downto upto
1055 (push word cl--loop-args)
1056 (if (memq (car cl--loop-args) '(downto above))
1057 (error "Must specify `from' value for downward cl-loop"))
1058 (let* ((down (or (eq (car cl--loop-args) 'downfrom)
1059 (memq (cl-caddr cl--loop-args)
1061 (excl (or (memq (car cl--loop-args) '(above below))
1062 (memq (cl-caddr cl--loop-args)
1064 (start (and (memq (car cl--loop-args)
1065 '(from upfrom downfrom))
1066 (cl--pop2 cl--loop-args)))
1067 (end (and (memq (car cl--loop-args)
1068 '(to upto downto above below))
1069 (cl--pop2 cl--loop-args)))
1070 (step (and (eq (car cl--loop-args) 'by)
1071 (cl--pop2 cl--loop-args)))
1072 (end-var (and (not (macroexp-const-p end))
1073 (make-symbol "--cl-var--")))
1074 (step-var (and (not (macroexp-const-p step))
1075 (make-symbol "--cl-var--"))))
1076 (and step (numberp step) (<= step 0)
1077 (error "Loop `by' value is not positive: %s" step))
1078 (push (list var (or start 0)) loop-for-bindings)
1079 (if end-var (push (list end-var end) loop-for-bindings))
1080 (if step-var (push (list step-var step)
1084 (if down (if excl '> '>=) (if excl '< '<=))
1085 var (or end-var end)) cl--loop-body))
1086 (push (list var (list (if down '- '+) var
1087 (or step-var step 1)))
1090 ((memq word '(in in-ref on))
1091 (let* ((on (eq word 'on))
1092 (temp (if (and on (symbolp var))
1093 var (make-symbol "--cl-var--"))))
1094 (push (list temp (pop cl--loop-args)) loop-for-bindings)
1095 (push `(consp ,temp) cl--loop-body)
1096 (if (eq word 'in-ref)
1097 (push (list var `(car ,temp)) cl--loop-symbol-macs)
1100 (push (list var nil) loop-for-bindings)
1101 (push (list var (if on temp `(car ,temp)))
1104 (if (eq (car cl--loop-args) 'by)
1105 (let ((step (cl--pop2 cl--loop-args)))
1106 (if (and (memq (car-safe step)
1109 (symbolp (nth 1 step)))
1110 (list (nth 1 step) temp)
1111 `(funcall ,step ,temp)))
1116 (let* ((start (pop cl--loop-args))
1117 (then (if (eq (car cl--loop-args) 'then)
1118 (cl--pop2 cl--loop-args) start)))
1119 (push (list var nil) loop-for-bindings)
1120 (if (or ands (eq (car cl--loop-args) 'and))
1123 (if ,(or cl--loop-first-flag
1124 (setq cl--loop-first-flag
1125 (make-symbol "--cl-var--")))
1128 (push (list var then) loop-for-steps))
1130 (if (eq start then) start
1131 `(if ,(or cl--loop-first-flag
1132 (setq cl--loop-first-flag
1133 (make-symbol "--cl-var--")))
1137 ((memq word '(across across-ref))
1138 (let ((temp-vec (make-symbol "--cl-vec--"))
1139 (temp-idx (make-symbol "--cl-idx--")))
1140 (push (list temp-vec (pop cl--loop-args)) loop-for-bindings)
1141 (push (list temp-idx -1) loop-for-bindings)
1142 (push `(< (setq ,temp-idx (1+ ,temp-idx))
1143 (length ,temp-vec)) cl--loop-body)
1144 (if (eq word 'across-ref)
1145 (push (list var `(aref ,temp-vec ,temp-idx))
1146 cl--loop-symbol-macs)
1147 (push (list var nil) loop-for-bindings)
1148 (push (list var `(aref ,temp-vec ,temp-idx))
1151 ((memq word '(element elements))
1152 (let ((ref (or (memq (car cl--loop-args) '(in-ref of-ref))
1153 (and (not (memq (car cl--loop-args) '(in of)))
1154 (error "Expected `of'"))))
1155 (seq (cl--pop2 cl--loop-args))
1156 (temp-seq (make-symbol "--cl-seq--"))
1158 (if (eq (car cl--loop-args) 'using)
1159 (if (and (= (length (cadr cl--loop-args)) 2)
1160 (eq (cl-caadr cl--loop-args) 'index))
1161 (cadr (cl--pop2 cl--loop-args))
1162 (error "Bad `using' clause"))
1163 (make-symbol "--cl-idx--"))))
1164 (push (list temp-seq seq) loop-for-bindings)
1165 (push (list temp-idx 0) loop-for-bindings)
1167 (let ((temp-len (make-symbol "--cl-len--")))
1168 (push (list temp-len `(length ,temp-seq))
1170 (push (list var `(elt ,temp-seq ,temp-idx))
1171 cl--loop-symbol-macs)
1172 (push `(< ,temp-idx ,temp-len) cl--loop-body))
1173 (push (list var nil) loop-for-bindings)
1174 (push `(and ,temp-seq
1175 (or (consp ,temp-seq)
1176 (< ,temp-idx (length ,temp-seq))))
1178 (push (list var `(if (consp ,temp-seq)
1180 (aref ,temp-seq ,temp-idx)))
1182 (push (list temp-idx `(1+ ,temp-idx))
1185 ((memq word hash-types)
1186 (or (memq (car cl--loop-args) '(in of))
1187 (error "Expected `of'"))
1188 (let* ((table (cl--pop2 cl--loop-args))
1190 (if (eq (car cl--loop-args) 'using)
1191 (if (and (= (length (cadr cl--loop-args)) 2)
1192 (memq (cl-caadr cl--loop-args) hash-types)
1193 (not (eq (cl-caadr cl--loop-args) word)))
1194 (cadr (cl--pop2 cl--loop-args))
1195 (error "Bad `using' clause"))
1196 (make-symbol "--cl-var--"))))
1197 (if (memq word '(hash-value hash-values))
1198 (setq var (prog1 other (setq other var))))
1199 (setq cl--loop-map-form
1200 `(maphash (lambda (,var ,other) . --cl-map) ,table))))
1202 ((memq word '(symbol present-symbol external-symbol
1203 symbols present-symbols external-symbols))
1204 (let ((ob (and (memq (car cl--loop-args) '(in of))
1205 (cl--pop2 cl--loop-args))))
1206 (setq cl--loop-map-form
1207 `(mapatoms (lambda (,var) . --cl-map) ,ob))))
1209 ((memq word '(overlay overlays extent extents))
1210 (let ((buf nil) (from nil) (to nil))
1211 (while (memq (car cl--loop-args) '(in of from to))
1212 (cond ((eq (car cl--loop-args) 'from)
1213 (setq from (cl--pop2 cl--loop-args)))
1214 ((eq (car cl--loop-args) 'to)
1215 (setq to (cl--pop2 cl--loop-args)))
1216 (t (setq buf (cl--pop2 cl--loop-args)))))
1217 (setq cl--loop-map-form
1219 (lambda (,var ,(make-symbol "--cl-var--"))
1220 (progn . --cl-map) nil)
1223 ((memq word '(interval intervals))
1224 (let ((buf nil) (prop nil) (from nil) (to nil)
1225 (var1 (make-symbol "--cl-var1--"))
1226 (var2 (make-symbol "--cl-var2--")))
1227 (while (memq (car cl--loop-args) '(in of property from to))
1228 (cond ((eq (car cl--loop-args) 'from)
1229 (setq from (cl--pop2 cl--loop-args)))
1230 ((eq (car cl--loop-args) 'to)
1231 (setq to (cl--pop2 cl--loop-args)))
1232 ((eq (car cl--loop-args) 'property)
1233 (setq prop (cl--pop2 cl--loop-args)))
1234 (t (setq buf (cl--pop2 cl--loop-args)))))
1235 (if (and (consp var) (symbolp (car var)) (symbolp (cdr var)))
1236 (setq var1 (car var) var2 (cdr var))
1237 (push (list var `(cons ,var1 ,var2)) loop-for-sets))
1238 (setq cl--loop-map-form
1240 (lambda (,var1 ,var2) . --cl-map)
1241 ,buf ,prop ,from ,to))))
1243 ((memq word key-types)
1244 (or (memq (car cl--loop-args) '(in of))
1245 (error "Expected `of'"))
1246 (let ((cl-map (cl--pop2 cl--loop-args))
1248 (if (eq (car cl--loop-args) 'using)
1249 (if (and (= (length (cadr cl--loop-args)) 2)
1250 (memq (cl-caadr cl--loop-args) key-types)
1251 (not (eq (cl-caadr cl--loop-args) word)))
1252 (cadr (cl--pop2 cl--loop-args))
1253 (error "Bad `using' clause"))
1254 (make-symbol "--cl-var--"))))
1255 (if (memq word '(key-binding key-bindings))
1256 (setq var (prog1 other (setq other var))))
1257 (setq cl--loop-map-form
1258 `(,(if (memq word '(key-seq key-seqs))
1259 'cl--map-keymap-recursively 'map-keymap)
1260 (lambda (,var ,other) . --cl-map) ,cl-map))))
1262 ((memq word '(frame frames screen screens))
1263 (let ((temp (make-symbol "--cl-var--")))
1264 (push (list var '(selected-frame))
1266 (push (list temp nil) loop-for-bindings)
1267 (push `(prog1 (not (eq ,var ,temp))
1268 (or ,temp (setq ,temp ,var)))
1270 (push (list var `(next-frame ,var))
1273 ((memq word '(window windows))
1274 (let ((scr (and (memq (car cl--loop-args) '(in of))
1275 (cl--pop2 cl--loop-args)))
1276 (temp (make-symbol "--cl-var--"))
1277 (minip (make-symbol "--cl-minip--")))
1278 (push (list var (if scr
1279 `(frame-selected-window ,scr)
1280 '(selected-window)))
1282 ;; If we started in the minibuffer, we need to
1283 ;; ensure that next-window will bring us back there
1284 ;; at some point. (Bug#7492).
1285 ;; (Consider using walk-windows instead of cl-loop if
1286 ;; you care about such things.)
1287 (push (list minip `(minibufferp (window-buffer ,var)))
1289 (push (list temp nil) loop-for-bindings)
1290 (push `(prog1 (not (eq ,var ,temp))
1291 (or ,temp (setq ,temp ,var)))
1293 (push (list var `(next-window ,var ,minip))
1297 ;; This is an advertised interface: (info "(cl)Other Clauses").
1298 (let ((handler (and (symbolp word)
1299 (get word 'cl-loop-for-handler))))
1301 (funcall handler var)
1302 (error "Expected a `for' preposition, found %s" word)))))
1303 (eq (car cl--loop-args) 'and))
1305 (pop cl--loop-args))
1306 (if (and ands loop-for-bindings)
1307 (push (nreverse loop-for-bindings) cl--loop-bindings)
1308 (setq cl--loop-bindings (nconc (mapcar 'list loop-for-bindings)
1309 cl--loop-bindings)))
1312 ,(cl--loop-let (nreverse loop-for-sets) 'setq ands)
1315 (push (cons (if ands 'cl-psetq 'setq)
1316 (apply 'append (nreverse loop-for-steps)))
1320 (let ((temp (make-symbol "--cl-var--")))
1321 (push (list (list temp (pop cl--loop-args))) cl--loop-bindings)
1322 (push `(>= (setq ,temp (1- ,temp)) 0) cl--loop-body)))
1324 ((memq word '(collect collecting))
1325 (let ((what (pop cl--loop-args))
1326 (var (cl--loop-handle-accum nil 'nreverse)))
1327 (if (eq var cl--loop-accum-var)
1328 (push `(progn (push ,what ,var) t) cl--loop-body)
1330 (setq ,var (nconc ,var (list ,what)))
1331 t) cl--loop-body))))
1333 ((memq word '(nconc nconcing append appending))
1334 (let ((what (pop cl--loop-args))
1335 (var (cl--loop-handle-accum nil 'nreverse)))
1338 ,(if (eq var cl--loop-accum-var)
1340 (,(if (memq word '(nconc nconcing))
1341 #'nreverse #'reverse)
1344 `(,(if (memq word '(nconc nconcing))
1346 ,var ,what))) t) cl--loop-body)))
1348 ((memq word '(concat concating))
1349 (let ((what (pop cl--loop-args))
1350 (var (cl--loop-handle-accum "")))
1351 (push `(progn (cl-callf concat ,var ,what) t) cl--loop-body)))
1353 ((memq word '(vconcat vconcating))
1354 (let ((what (pop cl--loop-args))
1355 (var (cl--loop-handle-accum [])))
1356 (push `(progn (cl-callf vconcat ,var ,what) t) cl--loop-body)))
1358 ((memq word '(sum summing))
1359 (let ((what (pop cl--loop-args))
1360 (var (cl--loop-handle-accum 0)))
1361 (push `(progn (cl-incf ,var ,what) t) cl--loop-body)))
1363 ((memq word '(count counting))
1364 (let ((what (pop cl--loop-args))
1365 (var (cl--loop-handle-accum 0)))
1366 (push `(progn (if ,what (cl-incf ,var)) t) cl--loop-body)))
1368 ((memq word '(minimize minimizing maximize maximizing))
1369 (let* ((what (pop cl--loop-args))
1370 (temp (if (cl--simple-expr-p what) what
1371 (make-symbol "--cl-var--")))
1372 (var (cl--loop-handle-accum nil))
1373 (func (intern (substring (symbol-name word) 0 3)))
1374 (set `(setq ,var (if ,var (,func ,var ,temp) ,temp))))
1375 (push `(progn ,(if (eq temp what) set
1376 `(let ((,temp ,what)) ,set))
1380 (let ((bindings nil))
1381 (while (progn (push (list (pop cl--loop-args)
1382 (and (eq (car cl--loop-args) '=)
1383 (cl--pop2 cl--loop-args)))
1385 (eq (car cl--loop-args) 'and))
1386 (pop cl--loop-args))
1387 (push (nreverse bindings) cl--loop-bindings)))
1390 (push (pop cl--loop-args) cl--loop-body))
1393 (push `(not ,(pop cl--loop-args)) cl--loop-body))
1396 (or cl--loop-finish-flag
1397 (setq cl--loop-finish-flag (make-symbol "--cl-flag--")))
1398 (push `(setq ,cl--loop-finish-flag ,(pop cl--loop-args)) cl--loop-body)
1399 (setq cl--loop-result t))
1402 (or cl--loop-finish-flag
1403 (setq cl--loop-finish-flag (make-symbol "--cl-flag--")))
1404 (push `(setq ,cl--loop-finish-flag (not ,(pop cl--loop-args)))
1406 (setq cl--loop-result t))
1409 (or cl--loop-finish-flag
1410 (setq cl--loop-finish-flag (make-symbol "--cl-flag--")))
1411 (or cl--loop-result-var
1412 (setq cl--loop-result-var (make-symbol "--cl-var--")))
1413 (push `(setq ,cl--loop-finish-flag
1414 (not (setq ,cl--loop-result-var ,(pop cl--loop-args))))
1417 ((memq word '(if when unless))
1418 (let* ((cond (pop cl--loop-args))
1419 (then (let ((cl--loop-body nil))
1420 (cl--parse-loop-clause)
1421 (cl--loop-build-ands (nreverse cl--loop-body))))
1422 (else (let ((cl--loop-body nil))
1423 (if (eq (car cl--loop-args) 'else)
1424 (progn (pop cl--loop-args) (cl--parse-loop-clause)))
1425 (cl--loop-build-ands (nreverse cl--loop-body))))
1426 (simple (and (eq (car then) t) (eq (car else) t))))
1427 (if (eq (car cl--loop-args) 'end) (pop cl--loop-args))
1428 (if (eq word 'unless) (setq then (prog1 else (setq else then))))
1429 (let ((form (cons (if simple (cons 'progn (nth 1 then)) (nth 2 then))
1430 (if simple (nth 1 else) (list (nth 2 else))))))
1431 (if (cl--expr-contains form 'it)
1432 (let ((temp (make-symbol "--cl-var--")))
1433 (push (list temp) cl--loop-bindings)
1434 (setq form `(if (setq ,temp ,cond)
1435 ,@(cl-subst temp 'it form))))
1436 (setq form `(if ,cond ,@form)))
1437 (push (if simple `(progn ,form t) form) cl--loop-body))))
1439 ((memq word '(do doing))
1441 (or (consp (car cl--loop-args)) (error "Syntax error on `do' clause"))
1442 (while (consp (car cl--loop-args)) (push (pop cl--loop-args) body))
1443 (push (cons 'progn (nreverse (cons t body))) cl--loop-body)))
1446 (or cl--loop-finish-flag
1447 (setq cl--loop-finish-flag (make-symbol "--cl-var--")))
1448 (or cl--loop-result-var
1449 (setq cl--loop-result-var (make-symbol "--cl-var--")))
1450 (push `(setq ,cl--loop-result-var ,(pop cl--loop-args)
1451 ,cl--loop-finish-flag nil) cl--loop-body))
1454 ;; This is an advertised interface: (info "(cl)Other Clauses").
1455 (let ((handler (and (symbolp word) (get word 'cl-loop-handler))))
1456 (or handler (error "Expected a cl-loop keyword, found %s" word))
1457 (funcall handler))))
1458 (if (eq (car cl--loop-args) 'and)
1459 (progn (pop cl--loop-args) (cl--parse-loop-clause)))))
1461 (defun cl--loop-let (specs body par) ; uses loop-*
1462 (let ((p specs) (temps nil) (new nil))
1463 (while (and p (or (symbolp (car-safe (car p))) (null (cl-cadar p))))
1467 (setq par nil p specs)
1469 (or (macroexp-const-p (cl-cadar p))
1470 (let ((temp (make-symbol "--cl-var--")))
1471 (push (list temp (cl-cadar p)) temps)
1472 (setcar (cdar p) temp)))
1475 (if (and (consp (car specs)) (listp (caar specs)))
1476 (let* ((spec (caar specs)) (nspecs nil)
1477 (expr (cadr (pop specs)))
1479 (cdr (or (assq spec cl--loop-destr-temps)
1480 (car (push (cons spec
1482 (make-symbol "--cl-var--")))
1483 cl--loop-destr-temps))))))
1484 (push (list temp expr) new)
1486 (push (list (pop spec)
1487 (and expr (list (if spec 'pop 'car) temp)))
1489 (setq specs (nconc (nreverse nspecs) specs)))
1490 (push (pop specs) new)))
1492 (let ((set (cons (if par 'cl-psetq 'setq)
1493 (apply 'nconc (nreverse new)))))
1494 (if temps `(let* ,(nreverse temps) ,set) set))
1495 `(,(if par 'let 'let*)
1496 ,(nconc (nreverse temps) (nreverse new)) ,@body))))
1498 (defun cl--loop-handle-accum (def &optional func) ; uses loop-*
1499 (if (eq (car cl--loop-args) 'into)
1500 (let ((var (cl--pop2 cl--loop-args)))
1501 (or (memq var cl--loop-accum-vars)
1502 (progn (push (list (list var def)) cl--loop-bindings)
1503 (push var cl--loop-accum-vars)))
1505 (or cl--loop-accum-var
1508 (setq cl--loop-accum-var (make-symbol "--cl-var--"))
1511 (setq cl--loop-result (if func (list func cl--loop-accum-var)
1512 cl--loop-accum-var))
1513 cl--loop-accum-var))))
1515 (defun cl--loop-build-ands (clauses)
1516 "Return various representations of (and . CLAUSES).
1517 CLAUSES is a list of Elisp expressions, where clauses of the form
1518 \(progn E1 E2 E3 .. t) are the focus of particular optimizations.
1519 The return value has shape (COND BODY COMBO)
1520 such that COMBO is equivalent to (and . CLAUSES)."
1523 ;; Look through `clauses', trying to optimize (progn ,@A t) (progn ,@B) ,@C
1524 ;; into (progn ,@A ,@B) ,@C.
1526 (if (and (eq (car-safe (car clauses)) 'progn)
1527 (eq (car (last (car clauses))) t))
1529 (setq clauses (cons (nconc (butlast (car clauses))
1530 (if (eq (car-safe (cadr clauses))
1533 (list (cadr clauses))))
1535 ;; A final (progn ,@A t) is moved outside of the `and'.
1536 (setq body (cdr (butlast (pop clauses)))))
1537 (push (pop clauses) ands)))
1538 (setq ands (or (nreverse ands) (list t)))
1539 (list (if (cdr ands) (cons 'and ands) (car ands))
1541 (let ((full (if body
1542 (append ands (list (cons 'progn (append body '(t)))))
1544 (if (cdr full) (cons 'and full) (car full))))))
1547 ;;; Other iteration control structures.
1550 (defmacro cl-do (steps endtest &rest body)
1551 "The Common Lisp `do' loop.
1553 \(fn ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)"
1556 ((&rest &or symbolp (symbolp &optional form form))
1558 cl-declarations body)))
1559 (cl--expand-do-loop steps endtest body nil))
1562 (defmacro cl-do* (steps endtest &rest body)
1563 "The Common Lisp `do*' loop.
1565 \(fn ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)"
1566 (declare (indent 2) (debug cl-do))
1567 (cl--expand-do-loop steps endtest body t))
1569 (defun cl--expand-do-loop (steps endtest body star)
1571 (,(if star 'let* 'let)
1572 ,(mapcar (lambda (c) (if (consp c) (list (car c) (nth 1 c)) c))
1574 (while (not ,(car endtest))
1576 ,@(let ((sets (mapcar (lambda (c)
1577 (and (consp c) (cdr (cdr c))
1578 (list (car c) (nth 2 c))))
1580 (setq sets (delq nil sets))
1582 (list (cons (if (or star (not (cdr sets)))
1584 (apply 'append sets))))))
1585 ,@(or (cdr endtest) '(nil)))))
1588 (defmacro cl-dolist (spec &rest body)
1590 Evaluate BODY with VAR bound to each `car' from LIST, in turn.
1591 Then evaluate RESULT to get return value, default nil.
1592 An implicit nil block is established around the loop.
1594 \(fn (VAR LIST [RESULT]) BODY...)"
1595 (declare (debug ((symbolp form &optional form) cl-declarations body))
1597 (let ((loop `(dolist ,spec ,@body)))
1598 (if (advice-member-p #'cl--wrap-in-nil-block 'dolist)
1599 loop `(cl-block nil ,loop))))
1602 (defmacro cl-dotimes (spec &rest body)
1603 "Loop a certain number of times.
1604 Evaluate BODY with VAR bound to successive integers from 0, inclusive,
1605 to COUNT, exclusive. Then evaluate RESULT to get return value, default
1608 \(fn (VAR COUNT [RESULT]) BODY...)"
1609 (declare (debug cl-dolist) (indent 1))
1610 (let ((loop `(dotimes ,spec ,@body)))
1611 (if (advice-member-p #'cl--wrap-in-nil-block 'dotimes)
1612 loop `(cl-block nil ,loop))))
1614 (defvar cl--tagbody-alist nil)
1617 (defmacro cl-tagbody (&rest labels-or-stmts)
1618 "Execute statements while providing for control transfers to labels.
1619 Each element of LABELS-OR-STMTS can be either a label (integer or symbol)
1620 or a `cons' cell, in which case it's taken to be a statement.
1621 This distinction is made before performing macroexpansion.
1622 Statements are executed in sequence left to right, discarding any return value,
1623 stopping only when reaching the end of LABELS-OR-STMTS.
1624 Any statement can transfer control at any time to the statements that follow
1625 one of the labels with the special form (go LABEL).
1626 Labels have lexical scope and dynamic extent."
1628 (first-label (if (consp (car labels-or-stmts))
1629 'cl--preamble (pop labels-or-stmts))))
1630 (let ((block (list first-label)))
1631 (dolist (label-or-stmt labels-or-stmts)
1632 (if (consp label-or-stmt) (push label-or-stmt block)
1633 ;; Add a "go to next block" to implement the fallthrough.
1634 (unless (eq 'go (car-safe (car-safe block)))
1635 (push `(go ,label-or-stmt) block))
1636 (push (nreverse block) blocks)
1637 (setq block (list label-or-stmt))))
1638 (unless (eq 'go (car-safe (car-safe block)))
1639 (push `(go cl--exit) block))
1640 (push (nreverse block) blocks))
1641 (let ((catch-tag (make-symbol "cl--tagbody-tag")))
1642 (push (cons 'cl--exit catch-tag) cl--tagbody-alist)
1643 (dolist (block blocks)
1644 (push (cons (car block) catch-tag) cl--tagbody-alist))
1646 `(let ((next-label ',first-label))
1648 (not (eq (setq next-label
1653 `((go . ,(lambda (label)
1654 (let ((catch-tag (cdr (assq label cl--tagbody-alist))))
1656 (error "Unknown cl-tagbody go label `%S'" label))
1657 `(throw ',catch-tag ',label))))
1658 ,@macroexpand-all-environment)))))
1661 (defmacro cl-do-symbols (spec &rest body)
1662 "Loop over all symbols.
1663 Evaluate BODY with VAR bound to each interned symbol, or to each symbol
1666 \(fn (VAR [OBARRAY [RESULT]]) BODY...)"
1668 (debug ((symbolp &optional form form) cl-declarations body)))
1669 ;; Apparently this doesn't have an implicit block.
1672 (mapatoms #'(lambda (,(car spec)) ,@body)
1673 ,@(and (cadr spec) (list (cadr spec))))
1677 (defmacro cl-do-all-symbols (spec &rest body)
1678 "Like `cl-do-symbols', but use the default obarray.
1680 \(fn (VAR [RESULT]) BODY...)"
1681 (declare (indent 1) (debug ((symbolp &optional form) cl-declarations body)))
1682 `(cl-do-symbols (,(car spec) nil ,(cadr spec)) ,@body))
1688 (defmacro cl-psetq (&rest args)
1689 "Set SYMs to the values VALs in parallel.
1690 This is like `setq', except that all VAL forms are evaluated (in order)
1691 before assigning any symbols SYM to the corresponding values.
1693 \(fn SYM VAL SYM VAL ...)"
1694 (declare (debug setq))
1695 (cons 'cl-psetf args))
1698 ;;; Binding control structures.
1701 (defmacro cl-progv (symbols values &rest body)
1702 "Bind SYMBOLS to VALUES dynamically in BODY.
1703 The forms SYMBOLS and VALUES are evaluated, and must evaluate to lists.
1704 Each symbol in the first list is bound to the corresponding value in the
1705 second list (or to nil if VALUES is shorter than SYMBOLS); then the
1706 BODY forms are executed and their result is returned. This is much like
1707 a `let' form, except that the list of symbols can be computed at run-time."
1708 (declare (indent 2) (debug (form form body)))
1709 (let ((bodyfun (make-symbol "body"))
1710 (binds (make-symbol "binds"))
1711 (syms (make-symbol "syms"))
1712 (vals (make-symbol "vals")))
1714 (let* ((,syms ,symbols)
1716 (,bodyfun (lambda () ,@body))
1719 (push (list (pop ,syms) (list 'quote (pop ,vals))) ,binds))
1720 (eval (list 'let ,binds (list 'funcall (list 'quote ,bodyfun))))))))
1722 (defvar cl--labels-convert-cache nil)
1724 (defun cl--labels-convert (f)
1725 "Special macro-expander to rename (function F) references in `cl-labels'."
1727 ;; ¡¡Big Ugly Hack!! We can't use a compiler-macro because those are checked
1728 ;; *after* handling `function', but we want to stop macroexpansion from
1729 ;; being applied infinitely, so we use a cache to return the exact `form'
1730 ;; being expanded even though we don't receive it.
1731 ((eq f (car cl--labels-convert-cache)) (cdr cl--labels-convert-cache))
1733 (let ((found (assq f macroexpand-all-environment)))
1734 (if (and found (ignore-errors
1735 (eq (cadr (cl-caddr found)) 'cl-labels-args)))
1736 (cadr (cl-caddr (cl-cadddr found)))
1737 (let ((res `(function ,f)))
1738 (setq cl--labels-convert-cache (cons f res))
1742 (defmacro cl-flet (bindings &rest body)
1743 "Make local function definitions.
1744 Like `cl-labels' but the definitions are not recursive.
1746 \(fn ((FUNC ARGLIST BODY...) ...) FORM...)"
1747 (declare (indent 1) (debug ((&rest (cl-defun)) cl-declarations body)))
1748 (let ((binds ()) (newenv macroexpand-all-environment))
1749 (dolist (binding bindings)
1750 (let ((var (make-symbol (format "--cl-%s--" (car binding)))))
1751 (push (list var `(cl-function (lambda . ,(cdr binding)))) binds)
1752 (push (cons (car binding)
1753 `(lambda (&rest cl-labels-args)
1754 (cl-list* 'funcall ',var
1757 `(let ,(nreverse binds)
1761 ;; Don't override lexical-let's macro-expander.
1762 (if (assq 'function newenv) newenv
1763 (cons (cons 'function #'cl--labels-convert) newenv)))))))
1766 (defmacro cl-flet* (bindings &rest body)
1767 "Make local function definitions.
1768 Like `cl-flet' but the definitions can refer to previous ones.
1770 \(fn ((FUNC ARGLIST BODY...) ...) FORM...)"
1771 (declare (indent 1) (debug cl-flet))
1773 ((null bindings) (macroexp-progn body))
1774 ((null (cdr bindings)) `(cl-flet ,bindings ,@body))
1775 (t `(cl-flet (,(pop bindings)) (cl-flet* ,bindings ,@body)))))
1778 (defmacro cl-labels (bindings &rest body)
1779 "Make temporary function bindings.
1780 The bindings can be recursive and the scoping is lexical, but capturing them
1781 in closures will only work if `lexical-binding' is in use.
1783 \(fn ((FUNC ARGLIST BODY...) ...) FORM...)"
1784 (declare (indent 1) (debug cl-flet))
1785 (let ((binds ()) (newenv macroexpand-all-environment))
1786 (dolist (binding bindings)
1787 (let ((var (make-symbol (format "--cl-%s--" (car binding)))))
1788 (push (list var `(cl-function (lambda . ,(cdr binding)))) binds)
1789 (push (cons (car binding)
1790 `(lambda (&rest cl-labels-args)
1791 (cl-list* 'funcall ',var
1794 (macroexpand-all `(letrec ,(nreverse binds) ,@body)
1795 ;; Don't override lexical-let's macro-expander.
1796 (if (assq 'function newenv) newenv
1797 (cons (cons 'function #'cl--labels-convert) newenv)))))
1799 ;; The following ought to have a better definition for use with newer
1802 (defmacro cl-macrolet (bindings &rest body)
1803 "Make temporary macro definitions.
1804 This is like `cl-flet', but for macros instead of functions.
1806 \(fn ((NAME ARGLIST BODY...) ...) FORM...)"
1809 ((&rest (&define name (&rest arg) cl-declarations-or-string
1811 cl-declarations body)))
1813 `(cl-macrolet (,(car bindings)) (cl-macrolet ,(cdr bindings) ,@body))
1814 (if (null bindings) (cons 'progn body)
1815 (let* ((name (caar bindings))
1816 (res (cl--transform-lambda (cdar bindings) name)))
1818 (macroexpand-all (cons 'progn body)
1819 (cons (cons name `(lambda ,@(cdr res)))
1820 macroexpand-all-environment))))))
1822 (defconst cl--old-macroexpand
1823 (if (and (boundp 'cl--old-macroexpand)
1824 (eq (symbol-function 'macroexpand)
1825 #'cl--sm-macroexpand))
1827 (symbol-function 'macroexpand)))
1829 (defun cl--sm-macroexpand (exp &optional env)
1830 "Special macro expander used inside `cl-symbol-macrolet'.
1831 This function replaces `macroexpand' during macro expansion
1832 of `cl-symbol-macrolet', and does the same thing as `macroexpand'
1833 except that it additionally expands symbol macros."
1834 (let ((macroexpand-all-environment env))
1837 (setq exp (funcall cl--old-macroexpand exp env))
1840 ;; Perform symbol-macro expansion.
1841 (when (cdr (assq (symbol-name exp) env))
1842 (setq exp (cadr (assq (symbol-name exp) env)))))
1844 ;; Convert setq to setf if required by symbol-macro expansion.
1845 (let* ((args (mapcar (lambda (f) (cl--sm-macroexpand f env))
1848 (while (and p (symbolp (car p))) (setq p (cddr p)))
1849 (if p (setq exp (cons 'setf args))
1850 (setq exp (cons 'setq args))
1851 ;; Don't loop further.
1853 (`(,(or `let `let*) . ,(or `(,bindings . ,body) dontcare))
1854 ;; CL's symbol-macrolet treats re-bindings as candidates for
1855 ;; expansion (turning the let into a letf if needed), contrary to
1856 ;; Common-Lisp where such re-bindings hide the symbol-macro.
1857 (let ((letf nil) (found nil) (nbs ()))
1858 (dolist (binding bindings)
1859 (let* ((var (if (symbolp binding) binding (car binding)))
1860 (sm (assq (symbol-name var) env)))
1861 (push (if (not (cdr sm))
1863 (let ((nexp (cadr sm)))
1865 (unless (symbolp nexp) (setq letf t))
1866 (cons nexp (cdr-safe binding))))
1869 (setq exp `(,(if letf
1870 (if (eq (car exp) 'let) 'cl-letf 'cl-letf*)
1874 ;; FIXME: The behavior of CL made sense in a dynamically scoped
1875 ;; language, but for lexical scoping, Common-Lisp's behavior might
1876 ;; make more sense (and indeed, CL behaves like Common-Lisp w.r.t
1877 ;; lexical-let), so maybe we should adjust the behavior based on
1878 ;; the use of lexical-binding.
1879 ;; (`(,(or `let `let*) . ,(or `(,bindings . ,body) dontcare))
1880 ;; (let ((nbs ()) (found nil))
1881 ;; (dolist (binding bindings)
1882 ;; (let* ((var (if (symbolp binding) binding (car binding)))
1883 ;; (name (symbol-name var))
1884 ;; (val (and found (consp binding) (eq 'let* (car exp))
1885 ;; (list (macroexpand-all (cadr binding)
1887 ;; (push (if (assq name env)
1888 ;; ;; This binding should hide its symbol-macro,
1889 ;; ;; but given the way macroexpand-all works, we
1890 ;; ;; can't prevent application of `env' to the
1891 ;; ;; sub-expressions, so we need to α-rename this
1892 ;; ;; variable instead.
1893 ;; (let ((nvar (make-symbol
1894 ;; (copy-sequence name))))
1896 ;; (push (list name nvar) env)
1897 ;; (cons nvar (or val (cdr-safe binding))))
1898 ;; (if val (cons var val) binding))
1901 ;; (setq exp `(,(car exp)
1903 ;; ,@(macroexp-unprogn
1904 ;; (macroexpand-all (macroexp-progn body)
1911 (defmacro cl-symbol-macrolet (bindings &rest body)
1912 "Make symbol macro definitions.
1913 Within the body FORMs, references to the variable NAME will be replaced
1914 by EXPANSION, and (setq NAME ...) will act like (setf EXPANSION ...).
1916 \(fn ((NAME EXPANSION) ...) FORM...)"
1917 (declare (indent 1) (debug ((&rest (symbol sexp)) cl-declarations body)))
1920 `(cl-symbol-macrolet (,(car bindings))
1921 (cl-symbol-macrolet ,(cdr bindings) ,@body)))
1922 ((null bindings) (macroexp-progn body))
1924 (let ((previous-macroexpand (symbol-function 'macroexpand)))
1927 (fset 'macroexpand #'cl--sm-macroexpand)
1928 ;; FIXME: For N bindings, this will traverse `body' N times!
1929 (macroexpand-all (cons 'progn body)
1930 (cons (list (symbol-name (caar bindings))
1931 (cl-cadar bindings))
1932 macroexpand-all-environment)))
1933 (fset 'macroexpand previous-macroexpand))))))
1935 ;;; Multiple values.
1938 (defmacro cl-multiple-value-bind (vars form &rest body)
1939 "Collect multiple return values.
1940 FORM must return a list; the BODY is then executed with the first N elements
1941 of this list bound (`let'-style) to each of the symbols SYM in turn. This
1942 is analogous to the Common Lisp `cl-multiple-value-bind' macro, using lists to
1943 simulate true multiple return values. For compatibility, (cl-values A B C) is
1944 a synonym for (list A B C).
1946 \(fn (SYM...) FORM BODY)"
1947 (declare (indent 2) (debug ((&rest symbolp) form body)))
1948 (let ((temp (make-symbol "--cl-var--")) (n -1))
1949 `(let* ((,temp ,form)
1950 ,@(mapcar (lambda (v)
1951 (list v `(nth ,(setq n (1+ n)) ,temp)))
1956 (defmacro cl-multiple-value-setq (vars form)
1957 "Collect multiple return values.
1958 FORM must return a list; the first N elements of this list are stored in
1959 each of the symbols SYM in turn. This is analogous to the Common Lisp
1960 `cl-multiple-value-setq' macro, using lists to simulate true multiple return
1961 values. For compatibility, (cl-values A B C) is a synonym for (list A B C).
1963 \(fn (SYM...) FORM)"
1964 (declare (indent 1) (debug ((&rest symbolp) form)))
1965 (cond ((null vars) `(progn ,form nil))
1966 ((null (cdr vars)) `(setq ,(car vars) (car ,form)))
1968 (let* ((temp (make-symbol "--cl-var--")) (n 0))
1969 `(let ((,temp ,form))
1970 (prog1 (setq ,(pop vars) (car ,temp))
1971 (setq ,@(apply #'nconc
1973 (list v `(nth ,(setq n (1+ n))
1981 (defmacro cl-locally (&rest body)
1982 "Equivalent to `progn'."
1986 (defmacro cl-the (_type form)
1987 "At present this ignores _TYPE and is simply equivalent to FORM."
1988 (declare (indent 1) (debug (cl-type-spec form)))
1991 (defvar cl--proclaim-history t) ; for future compilers
1992 (defvar cl--declare-stack t) ; for future compilers
1994 (defun cl--do-proclaim (spec hist)
1995 (and hist (listp cl--proclaim-history) (push spec cl--proclaim-history))
1996 (cond ((eq (car-safe spec) 'special)
1997 (if (boundp 'byte-compile-bound-variables)
1998 (setq byte-compile-bound-variables
1999 (append (cdr spec) byte-compile-bound-variables))))
2001 ((eq (car-safe spec) 'inline)
2002 (while (setq spec (cdr spec))
2003 (or (memq (get (car spec) 'byte-optimizer)
2004 '(nil byte-compile-inline-expand))
2005 (error "%s already has a byte-optimizer, can't make it inline"
2007 (put (car spec) 'byte-optimizer 'byte-compile-inline-expand)))
2009 ((eq (car-safe spec) 'notinline)
2010 (while (setq spec (cdr spec))
2011 (if (eq (get (car spec) 'byte-optimizer)
2012 'byte-compile-inline-expand)
2013 (put (car spec) 'byte-optimizer nil))))
2015 ((eq (car-safe spec) 'optimize)
2016 (let ((speed (assq (nth 1 (assq 'speed (cdr spec)))
2017 '((0 nil) (1 t) (2 t) (3 t))))
2018 (safety (assq (nth 1 (assq 'safety (cdr spec)))
2019 '((0 t) (1 t) (2 t) (3 nil)))))
2020 (if speed (setq cl--optimize-speed (car speed)
2021 byte-optimize (nth 1 speed)))
2022 (if safety (setq cl--optimize-safety (car safety)
2023 byte-compile-delete-errors (nth 1 safety)))))
2025 ((and (eq (car-safe spec) 'warn) (boundp 'byte-compile-warnings))
2026 (while (setq spec (cdr spec))
2027 (if (consp (car spec))
2028 (if (eq (cl-cadar spec) 0)
2029 (byte-compile-disable-warning (caar spec))
2030 (byte-compile-enable-warning (caar spec)))))))
2033 ;;; Process any proclamations made before cl-macs was loaded.
2034 (defvar cl--proclaims-deferred)
2035 (let ((p (reverse cl--proclaims-deferred)))
2036 (while p (cl--do-proclaim (pop p) t))
2037 (setq cl--proclaims-deferred nil))
2040 (defmacro cl-declare (&rest specs)
2041 "Declare SPECS about the current function while compiling.
2044 \(cl-declare (warn 0))
2046 will turn off byte-compile warnings in the function.
2047 See Info node `(cl)Declarations' for details."
2048 (if (cl--compiling-file)
2050 (if (listp cl--declare-stack) (push (car specs) cl--declare-stack))
2051 (cl--do-proclaim (pop specs) nil)))
2054 ;;; The standard modify macros.
2056 ;; `setf' is now part of core Elisp, defined in gv.el.
2059 (defmacro cl-psetf (&rest args)
2060 "Set PLACEs to the values VALs in parallel.
2061 This is like `setf', except that all VAL forms are evaluated (in order)
2062 before assigning any PLACEs to the corresponding values.
2064 \(fn PLACE VAL PLACE VAL ...)"
2065 (declare (debug setf))
2066 (let ((p args) (simple t) (vars nil))
2068 (if (or (not (symbolp (car p))) (cl--expr-depends-p (nth 1 p) vars))
2070 (if (memq (car p) vars)
2071 (error "Destination duplicated in psetf: %s" (car p)))
2073 (or p (error "Odd number of arguments to cl-psetf"))
2076 `(progn (setq ,@args) nil)
2077 (setq args (reverse args))
2078 (let ((expr `(setf ,(cadr args) ,(car args))))
2079 (while (setq args (cddr args))
2080 (setq expr `(setf ,(cadr args) (prog1 ,(car args) ,expr))))
2081 `(progn ,expr nil)))))
2084 (defmacro cl-remf (place tag)
2085 "Remove TAG from property list PLACE.
2086 PLACE may be a symbol, or any generalized variable allowed by `setf'.
2087 The form returns true if TAG was found and removed, nil otherwise."
2088 (declare (debug (place form)))
2089 (gv-letplace (tval setter) place
2090 (macroexp-let2 macroexp-copyable-p ttag tag
2091 `(if (eq ,ttag (car ,tval))
2092 (progn ,(funcall setter `(cddr ,tval))
2094 (cl--do-remf ,tval ,ttag)))))
2097 (defmacro cl-shiftf (place &rest args)
2098 "Shift left among PLACEs.
2099 Example: (cl-shiftf A B C) sets A to B, B to C, and returns the old A.
2100 Each PLACE may be a symbol, or any generalized variable allowed by `setf'.
2103 (declare (debug (&rest place)))
2106 ((symbolp place) `(prog1 ,place (setq ,place (cl-shiftf ,@args))))
2108 (gv-letplace (getter setter) place
2110 ,(funcall setter `(cl-shiftf ,@args)))))))
2113 (defmacro cl-rotatef (&rest args)
2114 "Rotate left among PLACEs.
2115 Example: (cl-rotatef A B C) sets A to B, B to C, and C to A. It returns nil.
2116 Each PLACE may be a symbol, or any generalized variable allowed by `setf'.
2119 (declare (debug (&rest place)))
2120 (if (not (memq nil (mapcar 'symbolp args)))
2125 (setq sets (nconc sets (list (pop args) (car args)))))
2126 `(cl-psetf ,@sets ,(car args) ,first)))
2127 (let* ((places (reverse args))
2128 (temp (make-symbol "--cl-rotatef--"))
2132 (gv-letplace (getter setter) (pop places)
2133 `(prog1 ,getter ,(funcall setter form)))))
2134 (gv-letplace (getter setter) (car places)
2135 (macroexp-let* `((,temp ,getter))
2136 `(progn ,(funcall setter form) nil))))))
2138 ;; FIXME: `letf' is unsatisfactory because it does not really "restore" the
2139 ;; previous state. If the getter/setter loses information, that info is
2142 (defun cl--letf (bindings simplebinds binds body)
2143 ;; It's not quite clear what the semantics of cl-letf should be.
2144 ;; E.g. in (cl-letf ((PLACE1 VAL1) (PLACE2 VAL2)) BODY), while it's clear
2145 ;; that the actual assignments ("bindings") should only happen after
2146 ;; evaluating VAL1 and VAL2, it's not clear when the sub-expressions of
2147 ;; PLACE1 and PLACE2 should be evaluated. Should we have
2148 ;; PLACE1; VAL1; PLACE2; VAL2; bind1; bind2
2150 ;; VAL1; VAL2; PLACE1; PLACE2; bind1; bind2
2152 ;; VAL1; VAL2; PLACE1; bind1; PLACE2; bind2
2153 ;; Common-Lisp's `psetf' does the first, so we'll do the same.
2155 (if (and (null binds) (null simplebinds)) (macroexp-progn body)
2156 `(let* (,@(mapcar (lambda (x)
2157 (pcase-let ((`(,vold ,getter ,_setter ,_vnew) x))
2158 (list vold getter)))
2167 ;; If there's no vnew, do nothing.
2168 (`(,_vold ,_getter ,setter ,vnew)
2169 (funcall setter vnew))))
2172 ,@(mapcar (lambda (x)
2173 (pcase-let ((`(,vold ,_getter ,setter ,_vnew) x))
2174 (funcall setter vold)))
2176 (let ((binding (car bindings)))
2177 (gv-letplace (getter setter) (car binding)
2178 (macroexp-let2 nil vnew (cadr binding)
2179 (if (symbolp (car binding))
2180 ;; Special-case for simple variables.
2181 (cl--letf (cdr bindings)
2182 (cons `(,getter ,(if (cdr binding) vnew getter))
2185 (cl--letf (cdr bindings) simplebinds
2186 (cons `(,(make-symbol "old") ,getter ,setter
2187 ,@(if (cdr binding) (list vnew)))
2192 (defmacro cl-letf (bindings &rest body)
2193 "Temporarily bind to PLACEs.
2194 This is the analogue of `let', but with generalized variables (in the
2195 sense of `setf') for the PLACEs. Each PLACE is set to the corresponding
2196 VALUE, then the BODY forms are executed. On exit, either normally or
2197 because of a `throw' or error, the PLACEs are set back to their original
2198 values. Note that this macro is *not* available in Common Lisp.
2199 As a special case, if `(PLACE)' is used instead of `(PLACE VALUE)',
2200 the PLACE is not modified before executing BODY.
2202 \(fn ((PLACE VALUE) ...) BODY...)"
2203 (declare (indent 1) (debug ((&rest (gate gv-place &optional form)) body)))
2204 (if (and (not (cdr bindings)) (cdar bindings) (symbolp (caar bindings)))
2205 `(let ,bindings ,@body)
2206 (cl--letf bindings () () body)))
2209 (defmacro cl-letf* (bindings &rest body)
2210 "Temporarily bind to PLACEs.
2211 Like `cl-letf' but where the bindings are performed one at a time,
2212 rather than all at the end (i.e. like `let*' rather than like `let')."
2213 (declare (indent 1) (debug cl-letf))
2214 (dolist (binding (reverse bindings))
2215 (setq body (list `(cl-letf (,binding) ,@body))))
2216 (macroexp-progn body))
2219 (defmacro cl-callf (func place &rest args)
2220 "Set PLACE to (FUNC PLACE ARGS...).
2221 FUNC should be an unquoted function name. PLACE may be a symbol,
2222 or any generalized variable allowed by `setf'."
2223 (declare (indent 2) (debug (cl-function place &rest form)))
2224 (gv-letplace (getter setter) place
2225 (let* ((rargs (cons getter args)))
2227 (if (symbolp func) (cons func rargs)
2228 `(funcall #',func ,@rargs))))))
2231 (defmacro cl-callf2 (func arg1 place &rest args)
2232 "Set PLACE to (FUNC ARG1 PLACE ARGS...).
2233 Like `cl-callf', but PLACE is the second argument of FUNC, not the first.
2235 \(fn FUNC ARG1 PLACE ARGS...)"
2236 (declare (indent 3) (debug (cl-function form place &rest form)))
2237 (if (and (cl--safe-expr-p arg1) (cl--simple-expr-p place) (symbolp func))
2238 `(setf ,place (,func ,arg1 ,place ,@args))
2239 (macroexp-let2 nil a1 arg1
2240 (gv-letplace (getter setter) place
2241 (let* ((rargs (cl-list* a1 getter args)))
2243 (if (symbolp func) (cons func rargs)
2244 `(funcall #',func ,@rargs))))))))
2249 (defmacro cl-defstruct (struct &rest descs)
2250 "Define a struct type.
2251 This macro defines a new data type called NAME that stores data
2252 in SLOTs. It defines a `make-NAME' constructor, a `copy-NAME'
2253 copier, a `NAME-p' predicate, and slot accessors named `NAME-SLOT'.
2254 You can use the accessors to set the corresponding slots, via `setf'.
2256 NAME may instead take the form (NAME OPTIONS...), where each
2257 OPTION is either a single keyword or (KEYWORD VALUE) where
2258 KEYWORD can be one of :conc-name, :constructor, :copier, :predicate,
2259 :type, :named, :initial-offset, :print-function, or :include.
2261 Each SLOT may instead take the form (SLOT SLOT-OPTS...), where
2262 SLOT-OPTS are keyword-value pairs for that slot. Currently, only
2263 one keyword is supported, `:read-only'. If this has a non-nil
2264 value, that slot cannot be set via `setf'.
2266 \(fn NAME SLOTS...)"
2267 (declare (doc-string 2) (indent 1)
2269 (&define ;Makes top-level form not be wrapped.
2273 (&or [":conc-name" symbolp]
2274 [":constructor" symbolp &optional cl-lambda-list]
2276 [":predicate" symbolp]
2277 [":include" symbolp &rest sexp] ;; Not finished.
2278 ;; The following are not supported.
2279 ;; [":print-function" ...]
2281 ;; [":initial-offset" ...]
2284 ;; All the above is for the following def-form.
2285 &rest &or symbolp (symbolp def-form
2286 &optional ":read-only" sexp))))
2287 (let* ((name (if (consp struct) (car struct) struct))
2288 (opts (cdr-safe struct))
2291 (conc-name (concat (symbol-name name) "-"))
2292 (constructor (intern (format "make-%s" name)))
2294 (copier (intern (format "copy-%s" name)))
2295 (predicate (intern (format "%s-p" name)))
2296 (print-func nil) (print-auto nil)
2297 (safety (if (cl--compiling-file) cl--optimize-safety 3))
2299 (tag (intern (format "cl-struct-%s" name)))
2300 (tag-symbol (intern (format "cl-struct-%s-tags" name)))
2306 pred-form pred-check)
2307 (if (stringp (car descs))
2308 (push `(put ',name 'structure-documentation
2309 ,(pop descs)) forms))
2310 (setq descs (cons '(cl-tag-slot)
2311 (mapcar (function (lambda (x) (if (consp x) x (list x))))
2314 (let ((opt (if (consp (car opts)) (caar opts) (car opts)))
2315 (args (cdr-safe (pop opts))))
2316 (cond ((eq opt :conc-name)
2318 (setq conc-name (if (car args)
2319 (symbol-name (car args)) ""))))
2320 ((eq opt :constructor)
2323 ;; If this defines a constructor of the same name as
2324 ;; the default one, don't define the default.
2325 (if (eq (car args) constructor)
2326 (setq constructor nil))
2327 (push args constrs))
2328 (if args (setq constructor (car args)))))
2330 (if args (setq copier (car args))))
2331 ((eq opt :predicate)
2332 (if args (setq predicate (car args))))
2334 (setq include (car args)
2335 include-descs (mapcar (function
2337 (if (consp x) x (list x))))
2339 ((eq opt :print-function)
2340 (setq print-func (car args)))
2342 (setq type (car args)))
2345 ((eq opt :initial-offset)
2346 (setq descs (nconc (make-list (car args) '(cl-skip-slot))
2349 (error "Slot option %s unrecognized" opt)))))
2352 `(progn (funcall #',print-func cl-x cl-s cl-n) t))
2353 (or type (and include (not (get include 'cl-struct-print)))
2355 print-func (and (or (not (or include type)) (null print-func))
2357 (princ ,(format "#S(%s" name) cl-s))))))
2359 (let ((inc-type (get include 'cl-struct-type))
2360 (old-descs (get include 'cl-struct-slots)))
2361 (or inc-type (error "%s is not a struct name" include))
2362 (and type (not (eq (car inc-type) type))
2363 (error ":type disagrees with :include for %s" name))
2364 (while include-descs
2365 (setcar (memq (or (assq (caar include-descs) old-descs)
2366 (error "No slot %s in included struct %s"
2367 (caar include-descs) include))
2369 (pop include-descs)))
2370 (setq descs (append old-descs (delq (assq 'cl-tag-slot descs) descs))
2372 named (assq 'cl-tag-slot descs))
2373 (if (cadr inc-type) (setq tag name named t))
2374 (let ((incl include))
2376 (push `(cl-pushnew ',tag
2377 ,(intern (format "cl-struct-%s-tags" incl)))
2379 (setq incl (get incl 'cl-struct-include)))))
2382 (or (memq type '(vector list))
2383 (error "Invalid :type specifier: %s" type))
2384 (if named (setq tag name)))
2385 (setq type 'vector named 'true)))
2386 (or named (setq descs (delq (assq 'cl-tag-slot descs) descs)))
2387 (push `(defvar ,tag-symbol) forms)
2388 (setq pred-form (and named
2389 (let ((pos (- (length descs)
2390 (length (memq (assq 'cl-tag-slot descs)
2392 (if (eq type 'vector)
2393 `(and (vectorp cl-x)
2394 (>= (length cl-x) ,(length descs))
2395 (memq (aref cl-x ,pos) ,tag-symbol))
2397 `(memq (car-safe cl-x) ,tag-symbol)
2399 (memq (nth ,pos cl-x) ,tag-symbol))))))
2400 pred-check (and pred-form (> safety 0)
2401 (if (and (eq (cl-caadr pred-form) 'vectorp)
2403 (cons 'and (cl-cdddr pred-form)) pred-form)))
2404 (let ((pos 0) (descp descs))
2406 (let* ((desc (pop descp))
2408 (if (memq slot '(cl-tag-slot cl-skip-slot))
2411 (push (and (eq slot 'cl-tag-slot) `',tag)
2413 (if (assq slot descp)
2414 (error "Duplicate slots named %s in %s" slot name))
2415 (let ((accessor (intern (format "%s%s" conc-name slot))))
2417 (push (nth 1 desc) defaults)
2418 (push `(cl-defsubst ,accessor (cl-x)
2420 (list `(or ,pred-check
2421 (error "%s accessing a non-%s"
2422 ',accessor ',name))))
2423 ,(if (eq type 'vector) `(aref cl-x ,pos)
2424 (if (= pos 0) '(car cl-x)
2425 `(nth ,pos cl-x)))) forms)
2426 (push (cons accessor t) side-eff)
2427 (if (cadr (memq :read-only (cddr desc)))
2428 (push `(gv-define-expander ,accessor
2429 (lambda (_cl-do _cl-x)
2430 (error "%s is a read-only slot" ',accessor)))
2432 ;; For normal slots, we don't need to define a setf-expander,
2433 ;; since gv-get can use the compiler macro to get the
2435 ;; (push `(gv-define-setter ,accessor (cl-val cl-x)
2436 ;; ;; If cl is loaded only for compilation,
2437 ;; ;; the call to cl--struct-setf-expander would
2438 ;; ;; cause a warning because it may not be
2439 ;; ;; defined at run time. Suppress that warning.
2441 ;; (declare-function
2442 ;; cl--struct-setf-expander "cl-macs"
2443 ;; (x name accessor pred-form pos))
2444 ;; (cl--struct-setf-expander
2445 ;; cl-val cl-x ',name ',accessor
2446 ;; ,(and pred-check `',pred-check)
2452 (list `(princ ,(format " %s" slot) cl-s)
2453 `(prin1 (,accessor cl-x) cl-s)))))))
2454 (setq pos (1+ pos))))
2455 (setq slots (nreverse slots)
2456 defaults (nreverse defaults))
2457 (and predicate pred-form
2458 (progn (push `(cl-defsubst ,predicate (cl-x)
2459 ,(if (eq (car pred-form) 'and)
2460 (append pred-form '(t))
2461 `(and ,pred-form t))) forms)
2462 (push (cons predicate 'error-free) side-eff)))
2464 (progn (push `(defun ,copier (x) (copy-sequence x)) forms)
2465 (push (cons copier t) side-eff)))
2467 (push (list constructor
2468 (cons '&key (delq nil (copy-sequence slots))))
2471 (let* ((name (caar constrs))
2472 (args (cadr (pop constrs)))
2473 (anames (cl--arglist-args args))
2474 (make (cl-mapcar (function (lambda (s d) (if (memq s anames) s d)))
2476 (push `(cl-defsubst ,name
2477 (&cl-defs '(nil ,@descs) ,@args)
2478 (,type ,@make)) forms)
2479 (if (cl--safe-expr-p `(progn ,@(mapcar #'cl-second descs)))
2480 (push (cons name t) side-eff))))
2481 (if print-auto (nconc print-func (list '(princ ")" cl-s) t)))
2482 ;; Don't bother adding to cl-custom-print-functions since it's not used
2483 ;; by anything anyway!
2485 ;; (push `(if (boundp 'cl-custom-print-functions)
2487 ;; ;; The auto-generated function does not pay attention to
2488 ;; ;; the depth argument cl-n.
2489 ;; (lambda (cl-x cl-s ,(if print-auto '_cl-n 'cl-n))
2490 ;; (and ,pred-form ,print-func))
2491 ;; cl-custom-print-functions))
2493 (push `(setq ,tag-symbol (list ',tag)) forms)
2494 (push `(cl-eval-when (compile load eval)
2495 (put ',name 'cl-struct-slots ',descs)
2496 (put ',name 'cl-struct-type ',(list type (eq named t)))
2497 (put ',name 'cl-struct-include ',include)
2498 (put ',name 'cl-struct-print ,print-auto)
2499 ,@(mapcar (lambda (x)
2500 `(put ',(car x) 'side-effect-free ',(cdr x)))
2503 `(progn ,@(nreverse (cons `',name forms)))))
2505 ;;; Types and assertions.
2508 (defmacro cl-deftype (name arglist &rest body)
2509 "Define NAME as a new data type.
2510 The type name can then be used in `cl-typecase', `cl-check-type', etc."
2511 (declare (debug cl-defmacro) (doc-string 3))
2512 `(cl-eval-when (compile load eval)
2513 (put ',name 'cl-deftype-handler
2514 (cl-function (lambda (&cl-defs '('*) ,@arglist) ,@body)))))
2516 (defun cl--make-type-test (val type)
2518 (cond ((get type 'cl-deftype-handler)
2519 (cl--make-type-test val (funcall (get type 'cl-deftype-handler))))
2520 ((memq type '(nil t)) type)
2521 ((eq type 'null) `(null ,val))
2522 ((eq type 'atom) `(atom ,val))
2523 ((eq type 'float) `(floatp ,val))
2524 ((eq type 'real) `(numberp ,val))
2525 ((eq type 'fixnum) `(integerp ,val))
2526 ;; FIXME: Should `character' accept things like ?\C-\M-a ? --Stef
2527 ((memq type '(character string-char)) `(characterp ,val))
2529 (let* ((name (symbol-name type))
2530 (namep (intern (concat name "p"))))
2531 (if (fboundp namep) (list namep val)
2532 (list (intern (concat name "-p")) val)))))
2533 (cond ((get (car type) 'cl-deftype-handler)
2534 (cl--make-type-test val (apply (get (car type) 'cl-deftype-handler)
2536 ((memq (car type) '(integer float real number))
2537 (delq t `(and ,(cl--make-type-test val (car type))
2538 ,(if (memq (cadr type) '(* nil)) t
2539 (if (consp (cadr type)) `(> ,val ,(cl-caadr type))
2540 `(>= ,val ,(cadr type))))
2541 ,(if (memq (cl-caddr type) '(* nil)) t
2542 (if (consp (cl-caddr type))
2543 `(< ,val ,(cl-caaddr type))
2544 `(<= ,val ,(cl-caddr type)))))))
2545 ((memq (car type) '(and or not))
2547 (mapcar (function (lambda (x) (cl--make-type-test val x)))
2549 ((memq (car type) '(member cl-member))
2550 `(and (cl-member ,val ',(cdr type)) t))
2551 ((eq (car type) 'satisfies) (list (cadr type) val))
2552 (t (error "Bad type spec: %s" type)))))
2556 (defun cl-typep (object type) ; See compiler macro below.
2557 "Check that OBJECT is of type TYPE.
2558 TYPE is a Common Lisp-style type specifier."
2559 (let ((cl--object object)) ;; Yuck!!
2560 (eval (cl--make-type-test 'cl--object type))))
2563 (defmacro cl-check-type (form type &optional string)
2564 "Verify that FORM is of type TYPE; signal an error if not.
2565 STRING is an optional description of the desired type."
2566 (declare (debug (place cl-type-spec &optional stringp)))
2567 (and (or (not (cl--compiling-file))
2568 (< cl--optimize-speed 3) (= cl--optimize-safety 3))
2569 (let* ((temp (if (cl--simple-expr-p form 3)
2570 form (make-symbol "--cl-var--")))
2571 (body `(or ,(cl--make-type-test temp type)
2572 (signal 'wrong-type-argument
2573 (list ,(or string `',type)
2575 (if (eq temp form) `(progn ,body nil)
2576 `(let ((,temp ,form)) ,body nil)))))
2579 (defmacro cl-assert (form &optional show-args string &rest args)
2580 ;; FIXME: This is actually not compatible with Common-Lisp's `assert'.
2581 "Verify that FORM returns non-nil; signal an error if not.
2582 Second arg SHOW-ARGS means to include arguments of FORM in message.
2583 Other args STRING and ARGS... are arguments to be passed to `error'.
2584 They are not evaluated unless the assertion fails. If STRING is
2585 omitted, a default message listing FORM itself is used."
2586 (declare (debug (form &rest form)))
2587 (and (or (not (cl--compiling-file))
2588 (< cl--optimize-speed 3) (= cl--optimize-safety 3))
2589 (let ((sargs (and show-args
2590 (delq nil (mapcar (lambda (x)
2591 (unless (macroexp-const-p x)
2597 `(error ,string ,@sargs ,@args)
2598 `(signal 'cl-assertion-failed
2599 (list ',form ,@sargs))))
2602 ;;; Compiler macros.
2605 (defmacro cl-define-compiler-macro (func args &rest body)
2606 "Define a compiler-only macro.
2607 This is like `defmacro', but macro expansion occurs only if the call to
2608 FUNC is compiled (i.e., not interpreted). Compiler macros should be used
2609 for optimizing the way calls to FUNC are compiled; the form returned by
2610 BODY should do the same thing as a call to the normal function called
2611 FUNC, though possibly more efficiently. Note that, like regular macros,
2612 compiler macros are expanded repeatedly until no further expansions are
2613 possible. Unlike regular macros, BODY can decide to \"punt\" and leave the
2614 original function call alone by declaring an initial `&whole foo' parameter
2615 and then returning foo."
2616 (declare (debug cl-defmacro))
2617 (let ((p args) (res nil))
2618 (while (consp p) (push (pop p) res))
2619 (setq args (nconc (nreverse res) (and p (list '&rest p)))))
2620 `(cl-eval-when (compile load eval)
2621 (put ',func 'compiler-macro
2622 (cl-function (lambda ,(if (memq '&whole args) (delq '&whole args)
2623 (cons '_cl-whole-arg args))
2625 ;; This is so that describe-function can locate
2626 ;; the macro definition.
2627 (let ((file ,(or buffer-file-name
2628 (and (boundp 'byte-compile-current-file)
2629 (stringp byte-compile-current-file)
2630 byte-compile-current-file))))
2631 (if file (put ',func 'compiler-macro-file
2632 (purecopy (file-name-nondirectory file)))))))
2635 (defun cl-compiler-macroexpand (form)
2636 "Like `macroexpand', but for compiler macros.
2637 Expands FORM repeatedly until no further expansion is possible.
2638 Returns FORM unchanged if it has no compiler macro, or if it has a
2639 macro that returns its `&whole' argument."
2641 (let ((func (car-safe form)) (handler nil))
2642 (while (and (symbolp func)
2643 (not (setq handler (get func 'compiler-macro)))
2645 (or (not (autoloadp (symbol-function func)))
2646 (autoload-do-load (symbol-function func) func)))
2647 (setq func (symbol-function func)))
2649 (not (eq form (setq form (apply handler form (cdr form))))))))
2652 ;; Optimize away unused block-wrappers.
2654 (defvar cl--active-block-names nil)
2656 (cl-define-compiler-macro cl--block-wrapper (cl-form)
2657 (let* ((cl-entry (cons (nth 1 (nth 1 cl-form)) nil))
2658 (cl--active-block-names (cons cl-entry cl--active-block-names))
2659 (cl-body (macroexpand-all ;Performs compiler-macro expansions.
2660 (cons 'progn (cddr cl-form))
2661 macroexpand-all-environment)))
2662 ;; FIXME: To avoid re-applying macroexpand-all, we'd like to be able
2663 ;; to indicate that this return value is already fully expanded.
2665 `(catch ,(nth 1 cl-form) ,@(cdr cl-body))
2668 (cl-define-compiler-macro cl--block-throw (cl-tag cl-value)
2669 (let ((cl-found (assq (nth 1 cl-tag) cl--active-block-names)))
2670 (if cl-found (setcdr cl-found t)))
2671 `(throw ,cl-tag ,cl-value))
2674 (defmacro cl-defsubst (name args &rest body)
2675 "Define NAME as a function.
2676 Like `defun', except the function is automatically declared `inline',
2677 ARGLIST allows full Common Lisp conventions, and BODY is implicitly
2678 surrounded by (cl-block NAME ...).
2680 \(fn NAME ARGLIST [DOCSTRING] BODY...)"
2681 (declare (debug cl-defun) (indent 2))
2682 (let* ((argns (cl--arglist-args args)) (p argns)
2683 (pbody (cons 'progn body))
2684 (unsafe (not (cl--safe-expr-p pbody))))
2685 (while (and p (eq (cl--expr-contains args (car p)) 1)) (pop p))
2687 ,(if p nil ; give up if defaults refer to earlier args
2688 `(cl-define-compiler-macro ,name
2689 ,(if (memq '&key args)
2690 `(&whole cl-whole &cl-quote ,@args)
2691 (cons '&cl-quote args))
2692 (cl--defsubst-expand
2693 ',argns '(cl-block ,name ,@body)
2694 ;; We used to pass `simple' as
2695 ;; (not (or unsafe (cl-expr-access-order pbody argns)))
2696 ;; But this is much too simplistic since it
2697 ;; does not pay attention to the argvs (and
2698 ;; cl-expr-access-order itself is also too naive).
2700 ,(and (memq '&key args) 'cl-whole) ,unsafe ,@argns)))
2701 (cl-defun ,name ,args ,@body))))
2703 (defun cl--defsubst-expand (argns body simple whole unsafe &rest argvs)
2704 (if (and whole (not (cl--safe-expr-p (cons 'progn argvs)))) whole
2705 (if (cl--simple-exprs-p argvs) (setq simple t))
2708 (cl-mapcar (lambda (argn argv)
2709 (if (or simple (macroexp-const-p argv))
2710 (progn (push (cons argn argv) substs)
2711 (and unsafe (list argn argv)))
2714 ;; FIXME: `sublis/subst' will happily substitute the symbol
2715 ;; `argn' in places where it's not used as a reference
2717 ;; FIXME: `sublis/subst' will happily copy `argv' to a different
2718 ;; scope, leading to name capture.
2719 (setq body (cond ((null substs) body)
2720 ((null (cdr substs))
2721 (cl-subst (cdar substs) (caar substs) body))
2722 (t (cl-sublis substs body))))
2723 (if lets `(let ,lets ,body) body))))
2726 ;; Compile-time optimizations for some functions defined in this package.
2728 (defun cl--compiler-macro-member (form a list &rest keys)
2729 (let ((test (and (= (length keys) 2) (eq (car keys) :test)
2730 (cl--const-expr-val (nth 1 keys)))))
2731 (cond ((eq test 'eq) `(memq ,a ,list))
2732 ((eq test 'equal) `(member ,a ,list))
2733 ((or (null keys) (eq test 'eql)) `(memql ,a ,list))
2736 (defun cl--compiler-macro-assoc (form a list &rest keys)
2737 (let ((test (and (= (length keys) 2) (eq (car keys) :test)
2738 (cl--const-expr-val (nth 1 keys)))))
2739 (cond ((eq test 'eq) `(assq ,a ,list))
2740 ((eq test 'equal) `(assoc ,a ,list))
2741 ((and (macroexp-const-p a) (or (null keys) (eq test 'eql)))
2742 (if (floatp (cl--const-expr-val a))
2743 `(assoc ,a ,list) `(assq ,a ,list)))
2747 (defun cl--compiler-macro-adjoin (form a list &rest keys)
2748 (if (and (cl--simple-expr-p a) (cl--simple-expr-p list)
2749 (not (memq :key keys)))
2750 `(if (cl-member ,a ,list ,@keys) ,list (cons ,a ,list))
2753 (defun cl--compiler-macro-get (_form sym prop &optional def)
2755 `(cl-getf (symbol-plist ,sym) ,prop ,def)
2758 (cl-define-compiler-macro cl-typep (&whole form val type)
2759 (if (macroexp-const-p type)
2760 (macroexp-let2 macroexp-copyable-p temp val
2761 (cl--make-type-test temp (cl--const-expr-val type)))
2764 (dolist (y '(cl-first cl-second cl-third cl-fourth
2765 cl-fifth cl-sixth cl-seventh
2766 cl-eighth cl-ninth cl-tenth
2767 cl-rest cl-endp cl-plusp cl-minusp
2768 cl-caaar cl-caadr cl-cadar
2769 cl-caddr cl-cdaar cl-cdadr
2770 cl-cddar cl-cdddr cl-caaaar
2771 cl-caaadr cl-caadar cl-caaddr
2772 cl-cadaar cl-cadadr cl-caddar
2773 cl-cadddr cl-cdaaar cl-cdaadr
2774 cl-cdadar cl-cdaddr cl-cddaar
2775 cl-cddadr cl-cdddar cl-cddddr))
2776 (put y 'side-effect-free t))
2778 ;;; Things that are inline.
2779 (cl-proclaim '(inline cl-acons cl-map cl-concatenate cl-notany
2780 cl-notevery cl--set-elt cl-revappend cl-nreconc gethash))
2782 ;;; Things that are side-effect-free.
2783 (mapc (lambda (x) (put x 'side-effect-free t))
2784 '(cl-oddp cl-evenp cl-signum last butlast cl-ldiff cl-pairlis cl-gcd
2785 cl-lcm cl-isqrt cl-floor cl-ceiling cl-truncate cl-round cl-mod cl-rem
2786 cl-subseq cl-list-length cl-get cl-getf))
2788 ;;; Things that are side-effect-and-error-free.
2789 (mapc (lambda (x) (put x 'side-effect-free 'error-free))
2790 '(eql cl-list* cl-subst cl-acons cl-equalp
2791 cl-random-state-p copy-tree cl-sublis))
2794 (run-hooks 'cl-macs-load-hook)
2797 ;; byte-compile-dynamic: t
2798 ;; generated-autoload-file: "cl-loaddefs.el"
2803 ;;; cl-macs.el ends here