* doc/lispref/customize.texi (Custom Themes): Clarify .el preference.
[emacs.git] / lisp / emacs-lisp / cl-macs.el
blobe7f82ced488e9dca363844658862c6e326f712b6
1 ;;; cl-macs.el --- Common Lisp macros -*- lexical-binding: t -*-
3 ;; Copyright (C) 1993, 2001-2018 Free Software Foundation, Inc.
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
6 ;; Old-Version: 2.02
7 ;; Keywords: extensions
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; These are extensions to Emacs Lisp that provide a degree of
28 ;; Common Lisp compatibility, beyond what is already built-in
29 ;; in Emacs Lisp.
31 ;; This package was written by Dave Gillespie; it is a complete
32 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
34 ;; Bug reports, comments, and suggestions are welcome!
36 ;; This file contains the portions of the Common Lisp extensions
37 ;; package which should be autoloaded, but need only be present
38 ;; if the compiler or interpreter is used---this file is not
39 ;; necessary for executing compiled code.
41 ;; See cl.el for Change Log.
44 ;;; Code:
46 (require 'cl-lib)
47 (require 'macroexp)
48 ;; `gv' is required here because cl-macs can be loaded before loaddefs.el.
49 (require 'gv)
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)
59 ;;; Initialization.
61 ;; Place compiler macros at the beginning, otherwise uses of the corresponding
62 ;; functions can lead to recursive-loads that prevent the calls from
63 ;; being optimized.
65 ;;;###autoload
66 (defun cl--compiler-macro-list* (_form arg &rest others)
67 (let* ((args (reverse (cons arg others)))
68 (form (car args)))
69 (while (setq args (cdr args))
70 (setq form `(cons ,(car args) ,form)))
71 form))
73 ;; Note: `cl--compiler-macro-cXXr' has been copied to
74 ;; `internal--compiler-macro-cXXr' in subr.el. If you amend either
75 ;; one, you may want to amend the other, too.
76 ;;;###autoload
77 (define-obsolete-function-alias 'cl--compiler-macro-cXXr
78 'internal--compiler-macro-cXXr "25.1")
80 ;;; Some predicates for analyzing Lisp forms.
81 ;; These are used by various
82 ;; macro expanders to optimize the results in certain common cases.
84 (defconst cl--simple-funcs '(car cdr nth aref elt if and or + - 1+ 1- min max
85 car-safe cdr-safe progn prog1 prog2))
86 (defconst cl--safe-funcs '(* / % length memq list vector vectorp
87 < > <= >= = error))
89 (defun cl--simple-expr-p (x &optional size)
90 "Check if no side effects, and executes quickly."
91 (or size (setq size 10))
92 (if (and (consp x) (not (memq (car x) '(quote function cl-function))))
93 (and (symbolp (car x))
94 (or (memq (car x) cl--simple-funcs)
95 (get (car x) 'side-effect-free))
96 (progn
97 (setq size (1- size))
98 (while (and (setq x (cdr x))
99 (setq size (cl--simple-expr-p (car x) size))))
100 (and (null x) (>= size 0) size)))
101 (and (> size 0) (1- size))))
103 (defun cl--simple-exprs-p (xs)
104 (while (and xs (cl--simple-expr-p (car xs)))
105 (setq xs (cdr xs)))
106 (not xs))
108 (defun cl--safe-expr-p (x)
109 "Check if no side effects."
110 (or (not (and (consp x) (not (memq (car x) '(quote function cl-function)))))
111 (and (symbolp (car x))
112 (or (memq (car x) cl--simple-funcs)
113 (memq (car x) cl--safe-funcs)
114 (get (car x) 'side-effect-free))
115 (progn
116 (while (and (setq x (cdr x)) (cl--safe-expr-p (car x))))
117 (null x)))))
119 ;;; Check if constant (i.e., no side effects or dependencies).
120 (defun cl--const-expr-p (x)
121 (cond ((consp x)
122 (or (eq (car x) 'quote)
123 (and (memq (car x) '(function cl-function))
124 (or (symbolp (nth 1 x))
125 (and (eq (car-safe (nth 1 x)) 'lambda) 'func)))))
126 ((symbolp x) (and (memq x '(nil t)) t))
127 (t t)))
129 (defun cl--const-expr-val (x)
130 "Return the value of X known at compile-time.
131 If X is not known at compile time, return nil. Before testing
132 whether X is known at compile time, macroexpand it completely in
133 `macroexpand-all-environment'."
134 (let ((x (macroexpand-all x macroexpand-all-environment)))
135 (if (macroexp-const-p x)
136 (if (consp x) (nth 1 x) x))))
138 (defun cl--expr-contains (x y)
139 "Count number of times X refers to Y. Return nil for 0 times."
140 ;; FIXME: This is naive, and it will cl-count Y as referred twice in
141 ;; (let ((Y 1)) Y) even though it should be 0. Also it is often called on
142 ;; non-macroexpanded code, so it may also miss some occurrences that would
143 ;; only appear in the expanded code.
144 (cond ((equal y x) 1)
145 ((and (consp x) (not (memq (car x) '(quote function cl-function))))
146 (let ((sum 0))
147 (while (consp x)
148 (setq sum (+ sum (or (cl--expr-contains (pop x) y) 0))))
149 (setq sum (+ sum (or (cl--expr-contains x y) 0)))
150 (and (> sum 0) sum)))
151 (t nil)))
153 (defun cl--expr-contains-any (x y)
154 (while (and y (not (cl--expr-contains x (car y)))) (pop y))
157 (defun cl--expr-depends-p (x y)
158 "Check whether X may depend on any of the symbols in Y."
159 (and (not (macroexp-const-p x))
160 (or (not (cl--safe-expr-p x)) (cl--expr-contains-any x y))))
162 ;;; Symbols.
164 (defvar cl--gensym-counter 0)
165 ;;;###autoload
166 (defun cl-gensym (&optional prefix)
167 "Generate a new uninterned symbol.
168 The name is made by appending a number to PREFIX, default \"G\"."
169 (let ((pfix (if (stringp prefix) prefix "G"))
170 (num (if (integerp prefix) prefix
171 (prog1 cl--gensym-counter
172 (setq cl--gensym-counter (1+ cl--gensym-counter))))))
173 (make-symbol (format "%s%d" pfix num))))
175 (defvar cl--gentemp-counter 0)
176 ;;;###autoload
177 (defun cl-gentemp (&optional prefix)
178 "Generate a new interned symbol with a unique name.
179 The name is made by appending a number to PREFIX, default \"T\"."
180 (let ((pfix (if (stringp prefix) prefix "T"))
181 name)
182 (while (intern-soft (setq name (format "%s%d" pfix cl--gentemp-counter)))
183 (setq cl--gentemp-counter (1+ cl--gentemp-counter)))
184 (intern name)))
187 ;;; Program structure.
189 (def-edebug-spec cl-declarations
190 (&rest ("cl-declare" &rest sexp)))
192 (def-edebug-spec cl-declarations-or-string
193 (&or lambda-doc cl-declarations))
195 (def-edebug-spec cl-lambda-list
196 (([&rest cl-lambda-arg]
197 [&optional ["&optional" cl-&optional-arg &rest cl-&optional-arg]]
198 [&optional ["&rest" cl-lambda-arg]]
199 [&optional ["&key" [cl-&key-arg &rest cl-&key-arg]
200 &optional "&allow-other-keys"]]
201 [&optional ["&aux" &rest
202 &or (symbolp &optional def-form) symbolp]]
203 . [&or arg nil])))
205 (def-edebug-spec cl-&optional-arg
206 (&or (cl-lambda-arg &optional def-form arg) arg))
208 (def-edebug-spec cl-&key-arg
209 (&or ([&or (symbolp cl-lambda-arg) arg] &optional def-form arg) arg))
211 (def-edebug-spec cl-lambda-arg
212 (&or arg cl-lambda-list1))
214 (def-edebug-spec cl-lambda-list1
215 (([&optional ["&whole" arg]] ;; only allowed at lower levels
216 [&rest cl-lambda-arg]
217 [&optional ["&optional" cl-&optional-arg &rest cl-&optional-arg]]
218 [&optional ["&rest" cl-lambda-arg]]
219 [&optional ["&key" cl-&key-arg &rest cl-&key-arg
220 &optional "&allow-other-keys"]]
221 [&optional ["&aux" &rest
222 &or (symbolp &optional def-form) symbolp]]
223 . [&or arg nil])))
225 (def-edebug-spec cl-type-spec sexp)
227 (defconst cl--lambda-list-keywords
228 '(&optional &rest &key &allow-other-keys &aux &whole &body &environment))
230 ;; Internal hacks used in formal arg lists:
231 ;; - &cl-quote: Added to formal-arglists to mean that any default value
232 ;; mentioned in the formal arglist should be considered as implicitly
233 ;; quoted rather than evaluated. This is used in `cl-defsubst' when
234 ;; performing compiler-macro-expansion, since at that time the
235 ;; arguments hold expressions rather than values.
236 ;; - &cl-defs (DEF . DEFS): Gives the default value to use for missing
237 ;; optional arguments which don't have an explicit default value.
238 ;; DEFS is an alist mapping vars to their default default value.
239 ;; and DEF is the default default to use for all other vars.
241 (defvar cl--bind-block) ;Name of surrounding block, only use for `signal' data.
242 (defvar cl--bind-defs) ;(DEF . DEFS) giving the "default default" for optargs.
243 (defvar cl--bind-enquote) ;Non-nil if &cl-quote was in the formal arglist!
244 (defvar cl--bind-lets) (defvar cl--bind-forms)
246 (defun cl--transform-lambda (form bind-block)
247 "Transform a function form FORM of name BIND-BLOCK.
248 BIND-BLOCK is the name of the symbol to which the function will be bound,
249 and which will be used for the name of the `cl-block' surrounding the
250 function's body.
251 FORM is of the form (ARGS . BODY)."
252 (let* ((args (car form)) (body (cdr form)) (orig-args args)
253 (cl--bind-block bind-block) (cl--bind-defs nil) (cl--bind-enquote nil)
254 (parsed-body (macroexp-parse-body body))
255 (header (car parsed-body)) (simple-args nil))
256 (setq body (cdr parsed-body))
257 ;; "(. X) to (&rest X)" conversion already done in cl--do-arglist, but we
258 ;; do it here as well, so as to be able to see if we can avoid
259 ;; cl--do-arglist.
260 (setq args (if (listp args) (cl-copy-list args) (list '&rest args)))
261 (let ((p (last args))) (if (cdr p) (setcdr p (list '&rest (cdr p)))))
262 (let ((cl-defs (memq '&cl-defs args)))
263 (when cl-defs
264 (setq cl--bind-defs (cadr cl-defs))
265 ;; Remove "&cl-defs DEFS" from args.
266 (setcdr cl-defs (cddr cl-defs))
267 (setq args (delq '&cl-defs args))))
268 (if (setq cl--bind-enquote (memq '&cl-quote args))
269 (setq args (delq '&cl-quote args)))
270 (if (memq '&whole args) (error "&whole not currently implemented"))
271 (let* ((p (memq '&environment args))
272 (v (cadr p)))
273 (if p (setq args (nconc (delq (car p) (delq v args))
274 `(&aux (,v macroexpand-all-environment))))))
275 ;; Take away all the simple args whose parsing can be handled more
276 ;; efficiently by a plain old `lambda' than the manual parsing generated
277 ;; by `cl--do-arglist'.
278 (let ((optional nil))
279 (while (and args (symbolp (car args))
280 (not (memq (car args) '(nil &rest &body &key &aux)))
281 (or (not optional)
282 ;; Optional args whose default is nil are simple.
283 (null (nth 1 (assq (car args) (cdr cl--bind-defs)))))
284 (not (and (eq (car args) '&optional) (setq optional t)
285 (car cl--bind-defs))))
286 (push (pop args) simple-args))
287 (when optional
288 (if args (push '&optional args))
289 ;; Don't keep a dummy trailing &optional without actual optional args.
290 (if (eq '&optional (car simple-args)) (pop simple-args))))
291 (or (eq cl--bind-block 'cl-none)
292 (setq body (list `(cl-block ,cl--bind-block ,@body))))
293 (let* ((cl--bind-lets nil) (cl--bind-forms nil)
294 (rest-args
295 (cond
296 ((null args) nil)
297 ((eq (car args) '&aux)
298 (cl--do-&aux args)
299 (setq cl--bind-lets (nreverse cl--bind-lets))
300 nil)
301 (t ;; `simple-args' doesn't handle all the parsing that we need,
302 ;; so we pass the rest to cl--do-arglist which will do
303 ;; "manual" parsing.
304 (let ((slen (length simple-args)))
305 (when (memq '&optional simple-args)
306 (cl-decf slen))
307 (setq header
308 ;; Macro expansion can take place in the middle of
309 ;; apparently harmless computation, so it should not
310 ;; touch the match-data.
311 (save-match-data
312 (cons (help-add-fundoc-usage
313 (if (stringp (car header)) (pop header))
314 ;; Be careful with make-symbol and (back)quote,
315 ;; see bug#12884.
316 (help--docstring-quote
317 (let ((print-gensym nil) (print-quoted t)
318 (print-escape-newlines t))
319 (format "%S" (cons 'fn (cl--make-usage-args
320 orig-args))))))
321 header)))
322 ;; FIXME: we'd want to choose an arg name for the &rest param
323 ;; and pass that as `expr' to cl--do-arglist, but that ends up
324 ;; generating code with a redundant let-binding, so we instead
325 ;; pass a dummy and then look in cl--bind-lets to find what var
326 ;; this was bound to.
327 (cl--do-arglist args :dummy slen)
328 (setq cl--bind-lets (nreverse cl--bind-lets))
329 ;; (cl-assert (eq :dummy (nth 1 (car cl--bind-lets))))
330 (list '&rest (car (pop cl--bind-lets))))))))
331 `(nil
332 (,@(nreverse simple-args) ,@rest-args)
333 ,@header
334 ,(macroexp-let* cl--bind-lets
335 (macroexp-progn
336 `(,@(nreverse cl--bind-forms)
337 ,@body)))))))
339 ;;;###autoload
340 (defmacro cl-defun (name args &rest body)
341 "Define NAME as a function.
342 Like normal `defun', except ARGLIST allows full Common Lisp conventions,
343 and BODY is implicitly surrounded by (cl-block NAME ...).
345 The full form of a Common Lisp function argument list is
347 (VAR...
348 [&optional (VAR [INITFORM [SVAR]])...]
349 [&rest|&body VAR]
350 [&key (([KEYWORD] VAR) [INITFORM [SVAR]])... [&allow-other-keys]]
351 [&aux (VAR [INITFORM])...])
353 VAR may be replaced recursively with an argument list for
354 destructuring, `&whole' is supported within these sublists. If
355 SVAR, INITFORM, and KEYWORD are all omitted, then `(VAR)' may be
356 written simply `VAR'. See the Info node `(cl)Argument Lists' for
357 more details.
359 \(fn NAME ARGLIST [DOCSTRING] BODY...)"
360 (declare (debug
361 ;; Same as defun but use cl-lambda-list.
362 (&define [&or name ("setf" :name setf name)]
363 cl-lambda-list
364 cl-declarations-or-string
365 [&optional ("interactive" interactive)]
366 def-body))
367 (doc-string 3)
368 (indent 2))
369 (let* ((res (cl--transform-lambda (cons args body) name))
370 (form `(defun ,name ,@(cdr res))))
371 (if (car res) `(progn ,(car res) ,form) form)))
373 ;;;###autoload
374 (defmacro cl-iter-defun (name args &rest body)
375 "Define NAME as a generator function.
376 Like normal `iter-defun', except ARGLIST allows full Common Lisp conventions,
377 and BODY is implicitly surrounded by (cl-block NAME ...).
379 \(fn NAME ARGLIST [DOCSTRING] BODY...)"
380 (declare (debug
381 ;; Same as iter-defun but use cl-lambda-list.
382 (&define [&or name ("setf" :name setf name)]
383 cl-lambda-list
384 cl-declarations-or-string
385 [&optional ("interactive" interactive)]
386 def-body))
387 (doc-string 3)
388 (indent 2))
389 (require 'generator)
390 (let* ((res (cl--transform-lambda (cons args body) name))
391 (form `(iter-defun ,name ,@(cdr res))))
392 (if (car res) `(progn ,(car res) ,form) form)))
394 ;; The lambda list for macros is different from that of normal lambdas.
395 ;; Note that &environment is only allowed as first or last items in the
396 ;; top level list.
398 (def-edebug-spec cl-macro-list
399 (([&optional "&environment" arg]
400 [&rest cl-macro-arg]
401 [&optional ["&optional" &rest
402 &or (cl-macro-arg &optional def-form cl-macro-arg) arg]]
403 [&optional [[&or "&rest" "&body"] cl-macro-arg]]
404 [&optional ["&key" [&rest
405 [&or ([&or (symbolp cl-macro-arg) arg]
406 &optional def-form cl-macro-arg)
407 arg]]
408 &optional "&allow-other-keys"]]
409 [&optional ["&aux" &rest
410 &or (symbolp &optional def-form) symbolp]]
411 [&optional "&environment" arg]
414 (def-edebug-spec cl-macro-arg
415 (&or arg cl-macro-list1))
417 (def-edebug-spec cl-macro-list1
418 (([&optional "&whole" arg] ;; only allowed at lower levels
419 [&rest cl-macro-arg]
420 [&optional ["&optional" &rest
421 &or (cl-macro-arg &optional def-form cl-macro-arg) arg]]
422 [&optional [[&or "&rest" "&body"] cl-macro-arg]]
423 [&optional ["&key" [&rest
424 [&or ([&or (symbolp cl-macro-arg) arg]
425 &optional def-form cl-macro-arg)
426 arg]]
427 &optional "&allow-other-keys"]]
428 [&optional ["&aux" &rest
429 &or (symbolp &optional def-form) symbolp]]
430 . [&or arg nil])))
432 ;;;###autoload
433 (defmacro cl-defmacro (name args &rest body)
434 "Define NAME as a macro.
435 Like normal `defmacro', except ARGLIST allows full Common Lisp conventions,
436 and BODY is implicitly surrounded by (cl-block NAME ...).
438 The full form of a Common Lisp macro argument list is
440 (VAR...
441 [&optional (VAR [INITFORM [SVAR]])...]
442 [&rest|&body VAR]
443 [&key (([KEYWORD] VAR) [INITFORM [SVAR]])... [&allow-other-keys]]
444 [&aux (VAR [INITFORM])...]
445 [&environment VAR])
447 VAR may be replaced recursively with an argument list for
448 destructuring, `&whole' is supported within these sublists. If
449 SVAR, INITFORM, and KEYWORD are all omitted, then `(VAR)' may be
450 written simply `VAR'. See the Info node `(cl)Argument Lists' for
451 more details.
453 \(fn NAME ARGLIST [DOCSTRING] BODY...)"
454 (declare (debug
455 (&define name cl-macro-list cl-declarations-or-string def-body))
456 (doc-string 3)
457 (indent 2))
458 (let* ((res (cl--transform-lambda (cons args body) name))
459 (form `(defmacro ,name ,@(cdr res))))
460 (if (car res) `(progn ,(car res) ,form) form)))
462 (def-edebug-spec cl-lambda-expr
463 (&define ("lambda" cl-lambda-list
464 cl-declarations-or-string
465 [&optional ("interactive" interactive)]
466 def-body)))
468 ;; Redefine function-form to also match cl-function
469 (def-edebug-spec function-form
470 ;; form at the end could also handle "function",
471 ;; but recognize it specially to avoid wrapping function forms.
472 (&or ([&or "quote" "function"] &or symbolp lambda-expr)
473 ("cl-function" cl-function)
474 form))
476 ;;;###autoload
477 (defmacro cl-function (func)
478 "Introduce a function.
479 Like normal `function', except that if argument is a lambda form,
480 its argument list allows full Common Lisp conventions."
481 (declare (debug (&or symbolp cl-lambda-expr)))
482 (if (eq (car-safe func) 'lambda)
483 (let* ((res (cl--transform-lambda (cdr func) 'cl-none))
484 (form `(function (lambda . ,(cdr res)))))
485 (if (car res) `(progn ,(car res) ,form) form))
486 `(function ,func)))
488 (defun cl--make-usage-var (x)
489 "X can be a var or a (destructuring) lambda-list."
490 (cond
491 ((symbolp x) (make-symbol (upcase (symbol-name x))))
492 ((consp x) (cl--make-usage-args x))
493 (t x)))
495 (defun cl--make-usage-args (arglist)
496 (let ((aux (ignore-errors (cl-position '&aux arglist))))
497 (when aux
498 ;; `&aux' args aren't arguments, so let's just drop them from the
499 ;; usage info.
500 (setq arglist (cl-subseq arglist 0 aux))))
501 (if (cdr-safe (last arglist)) ;Not a proper list.
502 (let* ((last (last arglist))
503 (tail (cdr last)))
504 (unwind-protect
505 (progn
506 (setcdr last nil)
507 (nconc (cl--make-usage-args arglist) (cl--make-usage-var tail)))
508 (setcdr last tail)))
509 ;; `orig-args' can contain &cl-defs.
510 (let ((x (memq '&cl-defs arglist)))
511 (when x (setq arglist (delq (car x) (remq (cadr x) arglist)))))
512 (let ((state nil))
513 (mapcar (lambda (x)
514 (cond
515 ((symbolp x)
516 (let ((first (aref (symbol-name x) 0)))
517 (if (eq ?\& first)
518 (setq state x)
519 ;; Strip a leading underscore, since it only
520 ;; means that this argument is unused.
521 (make-symbol (upcase (if (eq ?_ first)
522 (substring (symbol-name x) 1)
523 (symbol-name x)))))))
524 ((not (consp x)) x)
525 ((memq state '(nil &rest)) (cl--make-usage-args x))
526 (t ;(VAR INITFORM SVAR) or ((KEYWORD VAR) INITFORM SVAR).
527 (cl-list*
528 (if (and (consp (car x)) (eq state '&key))
529 (list (caar x) (cl--make-usage-var (nth 1 (car x))))
530 (cl--make-usage-var (car x)))
531 (nth 1 x) ;INITFORM.
532 (cl--make-usage-args (nthcdr 2 x)) ;SVAR.
533 ))))
534 arglist))))
536 (defun cl--do-&aux (args)
537 (while (and (eq (car args) '&aux) (pop args))
538 (while (and args (not (memq (car args) cl--lambda-list-keywords)))
539 (if (consp (car args))
540 (if (and cl--bind-enquote (cl-cadar args))
541 (cl--do-arglist (caar args)
542 `',(cadr (pop args)))
543 (cl--do-arglist (caar args) (cadr (pop args))))
544 (cl--do-arglist (pop args) nil))))
545 (if args (error "Malformed argument list ends with: %S" args)))
547 (defun cl--do-arglist (args expr &optional num) ; uses cl--bind-*
548 (if (nlistp args)
549 (if (or (memq args cl--lambda-list-keywords) (not (symbolp args)))
550 (error "Invalid argument name: %s" args)
551 (push (list args expr) cl--bind-lets))
552 (setq args (cl-copy-list args))
553 (let ((p (last args))) (if (cdr p) (setcdr p (list '&rest (cdr p)))))
554 (let ((p (memq '&body args))) (if p (setcar p '&rest)))
555 (if (memq '&environment args) (error "&environment used incorrectly"))
556 (let ((restarg (memq '&rest args))
557 (safety (if (cl--compiling-file) cl--optimize-safety 3))
558 (keys nil)
559 (laterarg nil) (exactarg nil) minarg)
560 (or num (setq num 0))
561 (setq restarg (if (listp (cadr restarg))
562 (make-symbol "--cl-rest--")
563 (cadr restarg)))
564 (push (list restarg expr) cl--bind-lets)
565 (if (eq (car args) '&whole)
566 (push (list (cl--pop2 args) restarg) cl--bind-lets))
567 (let ((p args))
568 (setq minarg restarg)
569 (while (and p (not (memq (car p) cl--lambda-list-keywords)))
570 (or (eq p args) (setq minarg (list 'cdr minarg)))
571 (setq p (cdr p)))
572 (if (memq (car p) '(nil &aux))
573 (setq minarg `(= (length ,restarg)
574 ,(length (cl-ldiff args p)))
575 exactarg (not (eq args p)))))
576 (while (and args (not (memq (car args) cl--lambda-list-keywords)))
577 (let ((poparg (list (if (or (cdr args) (not exactarg)) 'pop 'car)
578 restarg)))
579 (cl--do-arglist
580 (pop args)
581 (if (or laterarg (= safety 0)) poparg
582 `(if ,minarg ,poparg
583 (signal 'wrong-number-of-arguments
584 (list ,(and (not (eq cl--bind-block 'cl-none))
585 `',cl--bind-block)
586 (length ,restarg)))))))
587 (setq num (1+ num) laterarg t))
588 (while (and (eq (car args) '&optional) (pop args))
589 (while (and args (not (memq (car args) cl--lambda-list-keywords)))
590 (let ((arg (pop args)))
591 (or (consp arg) (setq arg (list arg)))
592 (if (cddr arg) (cl--do-arglist (nth 2 arg) `(and ,restarg t)))
593 (let ((def (if (cdr arg) (nth 1 arg)
594 (or (car cl--bind-defs)
595 (nth 1 (assq (car arg) cl--bind-defs)))))
596 (poparg `(pop ,restarg)))
597 (and def cl--bind-enquote (setq def `',def))
598 (cl--do-arglist (car arg)
599 (if def `(if ,restarg ,poparg ,def) poparg))
600 (setq num (1+ num))))))
601 (if (eq (car args) '&rest)
602 (let ((arg (cl--pop2 args)))
603 (if (consp arg) (cl--do-arglist arg restarg)))
604 (or (eq (car args) '&key) (= safety 0) exactarg
605 (push `(if ,restarg
606 (signal 'wrong-number-of-arguments
607 (list
608 ,(and (not (eq cl--bind-block 'cl-none))
609 `',cl--bind-block)
610 (+ ,num (length ,restarg)))))
611 cl--bind-forms)))
612 (while (and (eq (car args) '&key) (pop args))
613 (while (and args (not (memq (car args) cl--lambda-list-keywords)))
614 (let ((arg (pop args)))
615 (or (consp arg) (setq arg (list arg)))
616 (let* ((karg (if (consp (car arg)) (caar arg)
617 (let ((name (symbol-name (car arg))))
618 ;; Strip a leading underscore, since it only
619 ;; means that this argument is unused, but
620 ;; shouldn't affect the key's name (bug#12367).
621 (if (eq ?_ (aref name 0))
622 (setq name (substring name 1)))
623 (intern (format ":%s" name)))))
624 (varg (if (consp (car arg)) (cl-cadar arg) (car arg)))
625 (def (if (cdr arg) (cadr arg)
626 ;; The ordering between those two or clauses is
627 ;; irrelevant, since in practice only one of the two
628 ;; is ever non-nil (the car is only used for
629 ;; cl-deftype which doesn't use the cdr).
630 (or (car cl--bind-defs)
631 (cadr (assq varg cl--bind-defs)))))
632 (look `(plist-member ,restarg ',karg)))
633 (and def cl--bind-enquote (setq def `',def))
634 (if (cddr arg)
635 (let* ((temp (or (nth 2 arg) (make-symbol "--cl-var--")))
636 (val `(car (cdr ,temp))))
637 (cl--do-arglist temp look)
638 (cl--do-arglist varg
639 `(if ,temp
640 (prog1 ,val (setq ,temp t))
641 ,def)))
642 (cl--do-arglist
643 varg
644 `(car (cdr ,(if (null def)
645 look
646 `(or ,look
647 ,(if (eq (cl--const-expr-p def) t)
648 `'(nil ,(cl--const-expr-val def))
649 `(list nil ,def))))))))
650 (push karg keys)))))
651 (setq keys (nreverse keys))
652 (or (and (eq (car args) '&allow-other-keys) (pop args))
653 (null keys) (= safety 0)
654 (let* ((var (make-symbol "--cl-keys--"))
655 (allow '(:allow-other-keys))
656 (check `(while ,var
657 (cond
658 ((memq (car ,var) ',(append keys allow))
659 (setq ,var (cdr (cdr ,var))))
660 ((car (cdr (memq (quote ,@allow) ,restarg)))
661 (setq ,var nil))
663 (error
664 ,(format "Keyword argument %%s not one of %s"
665 keys)
666 (car ,var)))))))
667 (push `(let ((,var ,restarg)) ,check) cl--bind-forms)))
668 (cl--do-&aux args)
669 nil)))
671 (defun cl--arglist-args (args)
672 (if (nlistp args) (list args)
673 (let ((res nil) (kind nil) arg)
674 (while (consp args)
675 (setq arg (pop args))
676 (if (memq arg cl--lambda-list-keywords) (setq kind arg)
677 (if (eq arg '&cl-defs) (pop args)
678 (and (consp arg) kind (setq arg (car arg)))
679 (and (consp arg) (cdr arg) (eq kind '&key) (setq arg (cadr arg)))
680 (setq res (nconc res (cl--arglist-args arg))))))
681 (nconc res (and args (list args))))))
683 ;;;###autoload
684 (defmacro cl-destructuring-bind (args expr &rest body)
685 "Bind the variables in ARGS to the result of EXPR and execute BODY."
686 (declare (indent 2)
687 (debug (&define cl-macro-list1 def-form cl-declarations def-body)))
688 (let* ((cl--bind-lets nil) (cl--bind-forms nil)
689 (cl--bind-defs nil) (cl--bind-block 'cl-none) (cl--bind-enquote nil))
690 (cl--do-arglist (or args '(&aux)) expr)
691 (macroexp-let* (nreverse cl--bind-lets)
692 (macroexp-progn (append (nreverse cl--bind-forms) body)))))
695 ;;; The `cl-eval-when' form.
697 (defvar cl--not-toplevel nil)
699 ;;;###autoload
700 (defmacro cl-eval-when (when &rest body)
701 "Control when BODY is evaluated.
702 If `compile' is in WHEN, BODY is evaluated when compiled at top-level.
703 If `load' is in WHEN, BODY is evaluated when loaded after top-level compile.
704 If `eval' is in WHEN, BODY is evaluated when interpreted or at non-top-level.
706 \(fn (WHEN...) BODY...)"
707 (declare (indent 1) (debug (sexp body)))
708 (if (and (fboundp 'cl--compiling-file) (cl--compiling-file)
709 (not cl--not-toplevel) (not (boundp 'for-effect))) ;Horrible kludge.
710 (let ((comp (or (memq 'compile when) (memq :compile-toplevel when)))
711 (cl--not-toplevel t))
712 (if (or (memq 'load when) (memq :load-toplevel when))
713 (if comp (cons 'progn (mapcar 'cl--compile-time-too body))
714 `(if nil nil ,@body))
715 (progn (if comp (eval (cons 'progn body))) nil)))
716 (and (or (memq 'eval when) (memq :execute when))
717 (cons 'progn body))))
719 (defun cl--compile-time-too (form)
720 (or (and (symbolp (car-safe form)) (get (car-safe form) 'byte-hunk-handler))
721 (setq form (macroexpand
722 form (cons '(cl-eval-when) byte-compile-macro-environment))))
723 (cond ((eq (car-safe form) 'progn)
724 (cons 'progn (mapcar 'cl--compile-time-too (cdr form))))
725 ((eq (car-safe form) 'cl-eval-when)
726 (let ((when (nth 1 form)))
727 (if (or (memq 'eval when) (memq :execute when))
728 `(cl-eval-when (compile ,@when) ,@(cddr form))
729 form)))
730 (t (eval form) form)))
732 ;;;###autoload
733 (defmacro cl-load-time-value (form &optional _read-only)
734 "Like `progn', but evaluates the body at load time.
735 The result of the body appears to the compiler as a quoted constant."
736 (declare (debug (form &optional sexp)))
737 (if (cl--compiling-file)
738 (let* ((temp (cl-gentemp "--cl-load-time--"))
739 (set `(setq ,temp ,form)))
740 (if (and (fboundp 'byte-compile-file-form-defmumble)
741 (boundp 'this-kind) (boundp 'that-one))
742 ;; Else, we can't output right away, so we have to delay it to the
743 ;; next time we're at the top-level.
744 ;; FIXME: Use advice-add/remove.
745 (fset 'byte-compile-file-form
746 (let ((old (symbol-function 'byte-compile-file-form)))
747 (lambda (form)
748 (fset 'byte-compile-file-form old)
749 (byte-compile-file-form set)
750 (byte-compile-file-form form))))
751 ;; If we're not in the middle of compiling something, we can
752 ;; output directly to byte-compile-outbuffer, to make sure
753 ;; temp is set before we use it.
754 (print set byte-compile--outbuffer))
755 temp)
756 `',(eval form)))
759 ;;; Conditional control structures.
761 ;;;###autoload
762 (defmacro cl-case (expr &rest clauses)
763 "Eval EXPR and choose among clauses on that value.
764 Each clause looks like (KEYLIST BODY...). EXPR is evaluated and compared
765 against each key in each KEYLIST; the corresponding BODY is evaluated.
766 If no clause succeeds, cl-case returns nil. A single atom may be used in
767 place of a KEYLIST of one atom. A KEYLIST of t or `otherwise' is
768 allowed only in the final clause, and matches if no other keys match.
769 Key values are compared by `eql'.
770 \n(fn EXPR (KEYLIST BODY...)...)"
771 (declare (indent 1) (debug (form &rest (sexp body))))
772 (macroexp-let2 macroexp-copyable-p temp expr
773 (let* ((head-list nil))
774 `(cond
775 ,@(mapcar
776 (lambda (c)
777 (cons (cond ((memq (car c) '(t otherwise)) t)
778 ((eq (car c) 'cl--ecase-error-flag)
779 `(error "cl-ecase failed: %s, %s"
780 ,temp ',(reverse head-list)))
781 ((listp (car c))
782 (setq head-list (append (car c) head-list))
783 `(cl-member ,temp ',(car c)))
785 (if (memq (car c) head-list)
786 (error "Duplicate key in case: %s"
787 (car c)))
788 (push (car c) head-list)
789 `(eql ,temp ',(car c))))
790 (or (cdr c) '(nil))))
791 clauses)))))
793 ;;;###autoload
794 (defmacro cl-ecase (expr &rest clauses)
795 "Like `cl-case', but error if no case fits.
796 `otherwise'-clauses are not allowed.
797 \n(fn EXPR (KEYLIST BODY...)...)"
798 (declare (indent 1) (debug cl-case))
799 `(cl-case ,expr ,@clauses (cl--ecase-error-flag)))
801 ;;;###autoload
802 (defmacro cl-typecase (expr &rest clauses)
803 "Evals EXPR, chooses among clauses on that value.
804 Each clause looks like (TYPE BODY...). EXPR is evaluated and, if it
805 satisfies TYPE, the corresponding BODY is evaluated. If no clause succeeds,
806 cl-typecase returns nil. A TYPE of t or `otherwise' is allowed only in the
807 final clause, and matches if no other keys match.
808 \n(fn EXPR (TYPE BODY...)...)"
809 (declare (indent 1)
810 (debug (form &rest ([&or cl-type-spec "otherwise"] body))))
811 (macroexp-let2 macroexp-copyable-p temp expr
812 (let* ((type-list nil))
813 (cons
814 'cond
815 (mapcar
816 (function
817 (lambda (c)
818 (cons (cond ((eq (car c) 'otherwise) t)
819 ((eq (car c) 'cl--ecase-error-flag)
820 `(error "cl-etypecase failed: %s, %s"
821 ,temp ',(reverse type-list)))
823 (push (car c) type-list)
824 `(cl-typep ,temp ',(car c))))
825 (or (cdr c) '(nil)))))
826 clauses)))))
828 ;;;###autoload
829 (defmacro cl-etypecase (expr &rest clauses)
830 "Like `cl-typecase', but error if no case fits.
831 `otherwise'-clauses are not allowed.
832 \n(fn EXPR (TYPE BODY...)...)"
833 (declare (indent 1) (debug cl-typecase))
834 `(cl-typecase ,expr ,@clauses (cl--ecase-error-flag)))
837 ;;; Blocks and exits.
839 ;;;###autoload
840 (defmacro cl-block (name &rest body)
841 "Define a lexically-scoped block named NAME.
842 NAME may be any symbol. Code inside the BODY forms can call `cl-return-from'
843 to jump prematurely out of the block. This differs from `catch' and `throw'
844 in two respects: First, the NAME is an unevaluated symbol rather than a
845 quoted symbol or other form; and second, NAME is lexically rather than
846 dynamically scoped: Only references to it within BODY will work. These
847 references may appear inside macro expansions, but not inside functions
848 called from BODY."
849 (declare (indent 1) (debug (symbolp body)))
850 (if (cl--safe-expr-p `(progn ,@body)) `(progn ,@body)
851 `(cl--block-wrapper
852 (catch ',(intern (format "--cl-block-%s--" name))
853 ,@body))))
855 ;;;###autoload
856 (defmacro cl-return (&optional result)
857 "Return from the block named nil.
858 This is equivalent to `(cl-return-from nil RESULT)'."
859 (declare (debug (&optional form)))
860 `(cl-return-from nil ,result))
862 ;;;###autoload
863 (defmacro cl-return-from (name &optional result)
864 "Return from the block named NAME.
865 This jumps out to the innermost enclosing `(cl-block NAME ...)' form,
866 returning RESULT from that form (or nil if RESULT is omitted).
867 This is compatible with Common Lisp, but note that `defun' and
868 `defmacro' do not create implicit blocks as they do in Common Lisp."
869 (declare (indent 1) (debug (symbolp &optional form)))
870 (let ((name2 (intern (format "--cl-block-%s--" name))))
871 `(cl--block-throw ',name2 ,result)))
874 ;;; The "cl-loop" macro.
876 (defvar cl--loop-args) (defvar cl--loop-accum-var) (defvar cl--loop-accum-vars)
877 (defvar cl--loop-bindings) (defvar cl--loop-body)
878 (defvar cl--loop-finally)
879 (defvar cl--loop-finish-flag) ;Symbol set to nil to exit the loop?
880 (defvar cl--loop-first-flag)
881 (defvar cl--loop-initially) (defvar cl--loop-iterator-function)
882 (defvar cl--loop-name)
883 (defvar cl--loop-result) (defvar cl--loop-result-explicit)
884 (defvar cl--loop-result-var) (defvar cl--loop-steps)
885 (defvar cl--loop-symbol-macs)
887 (defun cl--loop-set-iterator-function (kind iterator)
888 (if cl--loop-iterator-function
889 ;; FIXME: Of course, we could make it work, but why bother.
890 (error "Iteration on %S does not support this combination" kind)
891 (setq cl--loop-iterator-function iterator)))
893 ;;;###autoload
894 (defmacro cl-loop (&rest loop-args)
895 "The Common Lisp `loop' macro.
896 Valid clauses include:
897 For clauses:
898 for VAR from/upfrom/downfrom EXPR1 to/upto/downto/above/below EXPR2 [by EXPR3]
899 for VAR = EXPR1 then EXPR2
900 for VAR in/on/in-ref LIST [by FUNC]
901 for VAR across/across-ref ARRAY
902 for VAR being:
903 the elements of/of-ref SEQUENCE [using (index VAR2)]
904 the symbols [of OBARRAY]
905 the hash-keys/hash-values of HASH-TABLE [using (hash-values/hash-keys V2)]
906 the key-codes/key-bindings/key-seqs of KEYMAP [using (key-bindings VAR2)]
907 the overlays/intervals [of BUFFER] [from POS1] [to POS2]
908 the frames/buffers
909 the windows [of FRAME]
910 Iteration clauses:
911 repeat INTEGER
912 while/until/always/never/thereis CONDITION
913 Accumulation clauses:
914 collect/append/nconc/concat/vconcat/count/sum/maximize/minimize FORM
915 [into VAR]
916 Miscellaneous clauses:
917 with VAR = INIT
918 if/when/unless COND CLAUSE [and CLAUSE]... else CLAUSE [and CLAUSE...]
919 named NAME
920 initially/finally [do] EXPRS...
921 do EXPRS...
922 [finally] return EXPR
924 For more details, see Info node `(cl)Loop Facility'.
926 \(fn CLAUSE...)"
927 (declare (debug (&rest &or
928 ;; These are usually followed by a symbol, but it can
929 ;; actually be any destructuring-bind pattern, which
930 ;; would erroneously match `form'.
931 [[&or "for" "as" "with" "and"] sexp]
932 ;; These are followed by expressions which could
933 ;; erroneously match `symbolp'.
934 [[&or "from" "upfrom" "downfrom" "to" "upto" "downto"
935 "above" "below" "by" "in" "on" "=" "across"
936 "repeat" "while" "until" "always" "never"
937 "thereis" "collect" "append" "nconc" "sum"
938 "count" "maximize" "minimize" "if" "unless"
939 "return"]
940 form]
941 ["using" (symbolp symbolp)]
942 ;; Simple default, which covers 99% of the cases.
943 symbolp form)))
944 (if (not (memq t (mapcar #'symbolp
945 (delq nil (delq t (cl-copy-list loop-args))))))
946 `(cl-block nil (while t ,@loop-args))
947 (let ((cl--loop-args loop-args) (cl--loop-name nil) (cl--loop-bindings nil)
948 (cl--loop-body nil) (cl--loop-steps nil)
949 (cl--loop-result nil) (cl--loop-result-explicit nil)
950 (cl--loop-result-var nil) (cl--loop-finish-flag nil)
951 (cl--loop-accum-var nil) (cl--loop-accum-vars nil)
952 (cl--loop-initially nil) (cl--loop-finally nil)
953 (cl--loop-iterator-function nil) (cl--loop-first-flag nil)
954 (cl--loop-symbol-macs nil))
955 ;; Here is more or less how those dynbind vars are used after looping
956 ;; over cl--parse-loop-clause:
958 ;; (cl-block ,cl--loop-name
959 ;; (cl-symbol-macrolet ,cl--loop-symbol-macs
960 ;; (foldl #'cl--loop-let
961 ;; `((,cl--loop-result-var)
962 ;; ((,cl--loop-first-flag t))
963 ;; ((,cl--loop-finish-flag t))
964 ;; ,@cl--loop-bindings)
965 ;; ,@(nreverse cl--loop-initially)
966 ;; (while ;(well: cl--loop-iterator-function)
967 ;; ,(car (cl--loop-build-ands (nreverse cl--loop-body)))
968 ;; ,@(cadr (cl--loop-build-ands (nreverse cl--loop-body)))
969 ;; ,@(nreverse cl--loop-steps)
970 ;; (setq ,cl--loop-first-flag nil))
971 ;; (if (not ,cl--loop-finish-flag) ;FIXME: Why `if' vs `progn'?
972 ;; ,cl--loop-result-var
973 ;; ,@(nreverse cl--loop-finally)
974 ;; ,(or cl--loop-result-explicit
975 ;; cl--loop-result)))))
977 (setq cl--loop-args (append cl--loop-args '(cl-end-loop)))
978 (while (not (eq (car cl--loop-args) 'cl-end-loop))
979 (cl--parse-loop-clause))
980 (if cl--loop-finish-flag
981 (push `((,cl--loop-finish-flag t)) cl--loop-bindings))
982 (if cl--loop-first-flag
983 (progn (push `((,cl--loop-first-flag t)) cl--loop-bindings)
984 (push `(setq ,cl--loop-first-flag nil) cl--loop-steps)))
985 (let* ((epilogue (nconc (nreverse cl--loop-finally)
986 (list (or cl--loop-result-explicit
987 cl--loop-result))))
988 (ands (cl--loop-build-ands (nreverse cl--loop-body)))
989 (while-body (nconc (cadr ands) (nreverse cl--loop-steps)))
990 (body (append
991 (nreverse cl--loop-initially)
992 (list (if cl--loop-iterator-function
993 `(cl-block --cl-finish--
994 ,(funcall cl--loop-iterator-function
995 (if (eq (car ands) t) while-body
996 (cons `(or ,(car ands)
997 (cl-return-from
998 --cl-finish--
999 nil))
1000 while-body))))
1001 `(while ,(car ands) ,@while-body)))
1002 (if cl--loop-finish-flag
1003 (if (equal epilogue '(nil)) (list cl--loop-result-var)
1004 `((if ,cl--loop-finish-flag
1005 (progn ,@epilogue) ,cl--loop-result-var)))
1006 epilogue))))
1007 (if cl--loop-result-var
1008 (push (list cl--loop-result-var) cl--loop-bindings))
1009 (while cl--loop-bindings
1010 (if (cdar cl--loop-bindings)
1011 (setq body (list (cl--loop-let (pop cl--loop-bindings) body t)))
1012 (let ((lets nil))
1013 (while (and cl--loop-bindings
1014 (not (cdar cl--loop-bindings)))
1015 (push (car (pop cl--loop-bindings)) lets))
1016 (setq body (list (cl--loop-let lets body nil))))))
1017 (if cl--loop-symbol-macs
1018 (setq body
1019 (list `(cl-symbol-macrolet ,cl--loop-symbol-macs ,@body))))
1020 `(cl-block ,cl--loop-name ,@body)))))
1022 ;; Below is a complete spec for cl-loop, in several parts that correspond
1023 ;; to the syntax given in CLtL2. The specs do more than specify where
1024 ;; the forms are; it also specifies, as much as Edebug allows, all the
1025 ;; syntactically valid cl-loop clauses. The disadvantage of this
1026 ;; completeness is rigidity, but the "for ... being" clause allows
1027 ;; arbitrary extensions of the form: [symbolp &rest &or symbolp form].
1029 ;; (def-edebug-spec cl-loop
1030 ;; ([&optional ["named" symbolp]]
1031 ;; [&rest
1032 ;; &or
1033 ;; ["repeat" form]
1034 ;; loop-for-as
1035 ;; loop-with
1036 ;; loop-initial-final]
1037 ;; [&rest loop-clause]
1038 ;; ))
1040 ;; (def-edebug-spec loop-with
1041 ;; ("with" loop-var
1042 ;; loop-type-spec
1043 ;; [&optional ["=" form]]
1044 ;; &rest ["and" loop-var
1045 ;; loop-type-spec
1046 ;; [&optional ["=" form]]]))
1048 ;; (def-edebug-spec loop-for-as
1049 ;; ([&or "for" "as"] loop-for-as-subclause
1050 ;; &rest ["and" loop-for-as-subclause]))
1052 ;; (def-edebug-spec loop-for-as-subclause
1053 ;; (loop-var
1054 ;; loop-type-spec
1055 ;; &or
1056 ;; [[&or "in" "on" "in-ref" "across-ref"]
1057 ;; form &optional ["by" function-form]]
1059 ;; ["=" form &optional ["then" form]]
1060 ;; ["across" form]
1061 ;; ["being"
1062 ;; [&or "the" "each"]
1063 ;; &or
1064 ;; [[&or "element" "elements"]
1065 ;; [&or "of" "in" "of-ref"] form
1066 ;; &optional "using" ["index" symbolp]];; is this right?
1067 ;; [[&or "hash-key" "hash-keys"
1068 ;; "hash-value" "hash-values"]
1069 ;; [&or "of" "in"]
1070 ;; hash-table-p &optional ["using" ([&or "hash-value" "hash-values"
1071 ;; "hash-key" "hash-keys"] sexp)]]
1073 ;; [[&or "symbol" "present-symbol" "external-symbol"
1074 ;; "symbols" "present-symbols" "external-symbols"]
1075 ;; [&or "in" "of"] package-p]
1077 ;; ;; Extensions for Emacs Lisp, including Lucid Emacs.
1078 ;; [[&or "frame" "frames"
1079 ;; "screen" "screens"
1080 ;; "buffer" "buffers"]]
1082 ;; [[&or "window" "windows"]
1083 ;; [&or "of" "in"] form]
1085 ;; [[&or "overlay" "overlays"
1086 ;; "extent" "extents"]
1087 ;; [&or "of" "in"] form
1088 ;; &optional [[&or "from" "to"] form]]
1090 ;; [[&or "interval" "intervals"]
1091 ;; [&or "in" "of"] form
1092 ;; &optional [[&or "from" "to"] form]
1093 ;; ["property" form]]
1095 ;; [[&or "key-code" "key-codes"
1096 ;; "key-seq" "key-seqs"
1097 ;; "key-binding" "key-bindings"]
1098 ;; [&or "in" "of"] form
1099 ;; &optional ["using" ([&or "key-code" "key-codes"
1100 ;; "key-seq" "key-seqs"
1101 ;; "key-binding" "key-bindings"]
1102 ;; sexp)]]
1103 ;; ;; For arbitrary extensions, recognize anything else.
1104 ;; [symbolp &rest &or symbolp form]
1105 ;; ]
1107 ;; ;; arithmetic - must be last since all parts are optional.
1108 ;; [[&optional [[&or "from" "downfrom" "upfrom"] form]]
1109 ;; [&optional [[&or "to" "downto" "upto" "below" "above"] form]]
1110 ;; [&optional ["by" form]]
1111 ;; ]))
1113 ;; (def-edebug-spec loop-initial-final
1114 ;; (&or ["initially"
1115 ;; ;; [&optional &or "do" "doing"] ;; CLtL2 doesn't allow this.
1116 ;; &rest loop-non-atomic-expr]
1117 ;; ["finally" &or
1118 ;; [[&optional &or "do" "doing"] &rest loop-non-atomic-expr]
1119 ;; ["return" form]]))
1121 ;; (def-edebug-spec loop-and-clause
1122 ;; (loop-clause &rest ["and" loop-clause]))
1124 ;; (def-edebug-spec loop-clause
1125 ;; (&or
1126 ;; [[&or "while" "until" "always" "never" "thereis"] form]
1128 ;; [[&or "collect" "collecting"
1129 ;; "append" "appending"
1130 ;; "nconc" "nconcing"
1131 ;; "concat" "vconcat"] form
1132 ;; [&optional ["into" loop-var]]]
1134 ;; [[&or "count" "counting"
1135 ;; "sum" "summing"
1136 ;; "maximize" "maximizing"
1137 ;; "minimize" "minimizing"] form
1138 ;; [&optional ["into" loop-var]]
1139 ;; loop-type-spec]
1141 ;; [[&or "if" "when" "unless"]
1142 ;; form loop-and-clause
1143 ;; [&optional ["else" loop-and-clause]]
1144 ;; [&optional "end"]]
1146 ;; [[&or "do" "doing"] &rest loop-non-atomic-expr]
1148 ;; ["return" form]
1149 ;; loop-initial-final
1150 ;; ))
1152 ;; (def-edebug-spec loop-non-atomic-expr
1153 ;; ([&not atom] form))
1155 ;; (def-edebug-spec loop-var
1156 ;; ;; The symbolp must be last alternative to recognize e.g. (a b . c)
1157 ;; ;; loop-var =>
1158 ;; ;; (loop-var . [&or nil loop-var])
1159 ;; ;; (symbolp . [&or nil loop-var])
1160 ;; ;; (symbolp . loop-var)
1161 ;; ;; (symbolp . (symbolp . [&or nil loop-var]))
1162 ;; ;; (symbolp . (symbolp . loop-var))
1163 ;; ;; (symbolp . (symbolp . symbolp)) == (symbolp symbolp . symbolp)
1164 ;; (&or (loop-var . [&or nil loop-var]) [gate symbolp]))
1166 ;; (def-edebug-spec loop-type-spec
1167 ;; (&optional ["of-type" loop-d-type-spec]))
1169 ;; (def-edebug-spec loop-d-type-spec
1170 ;; (&or (loop-d-type-spec . [&or nil loop-d-type-spec]) cl-type-spec))
1174 (defun cl--parse-loop-clause () ; uses loop-*
1175 (let ((word (pop cl--loop-args))
1176 (hash-types '(hash-key hash-keys hash-value hash-values))
1177 (key-types '(key-code key-codes key-seq key-seqs
1178 key-binding key-bindings)))
1179 (cond
1181 ((null cl--loop-args)
1182 (error "Malformed `cl-loop' macro"))
1184 ((eq word 'named)
1185 (setq cl--loop-name (pop cl--loop-args)))
1187 ((eq word 'initially)
1188 (if (memq (car cl--loop-args) '(do doing)) (pop cl--loop-args))
1189 (or (consp (car cl--loop-args))
1190 (error "Syntax error on `initially' clause"))
1191 (while (consp (car cl--loop-args))
1192 (push (pop cl--loop-args) cl--loop-initially)))
1194 ((eq word 'finally)
1195 (if (eq (car cl--loop-args) 'return)
1196 (setq cl--loop-result-explicit
1197 (or (cl--pop2 cl--loop-args) '(quote nil)))
1198 (if (memq (car cl--loop-args) '(do doing)) (pop cl--loop-args))
1199 (or (consp (car cl--loop-args))
1200 (error "Syntax error on `finally' clause"))
1201 (if (and (eq (caar cl--loop-args) 'return) (null cl--loop-name))
1202 (setq cl--loop-result-explicit
1203 (or (nth 1 (pop cl--loop-args)) '(quote nil)))
1204 (while (consp (car cl--loop-args))
1205 (push (pop cl--loop-args) cl--loop-finally)))))
1207 ((memq word '(for as))
1208 (let ((loop-for-bindings nil) (loop-for-sets nil) (loop-for-steps nil)
1209 (ands nil))
1210 (while
1211 ;; Use `cl-gensym' rather than `make-symbol'. It's important that
1212 ;; (not (eq (symbol-name var1) (symbol-name var2))) because
1213 ;; these vars get added to the macro-environment.
1214 (let ((var (or (pop cl--loop-args) (cl-gensym "--cl-var--"))))
1215 (setq word (pop cl--loop-args))
1216 (if (eq word 'being) (setq word (pop cl--loop-args)))
1217 (if (memq word '(the each)) (setq word (pop cl--loop-args)))
1218 (if (memq word '(buffer buffers))
1219 (setq word 'in
1220 cl--loop-args (cons '(buffer-list) cl--loop-args)))
1221 (cond
1223 ((memq word '(from downfrom upfrom to downto upto
1224 above below by))
1225 (push word cl--loop-args)
1226 (if (memq (car cl--loop-args) '(downto above))
1227 (error "Must specify `from' value for downward cl-loop"))
1228 (let* ((down (or (eq (car cl--loop-args) 'downfrom)
1229 (memq (nth 2 cl--loop-args)
1230 '(downto above))))
1231 (excl (or (memq (car cl--loop-args) '(above below))
1232 (memq (nth 2 cl--loop-args)
1233 '(above below))))
1234 (start (and (memq (car cl--loop-args)
1235 '(from upfrom downfrom))
1236 (cl--pop2 cl--loop-args)))
1237 (end (and (memq (car cl--loop-args)
1238 '(to upto downto above below))
1239 (cl--pop2 cl--loop-args)))
1240 (step (and (eq (car cl--loop-args) 'by)
1241 (cl--pop2 cl--loop-args)))
1242 (end-var (and (not (macroexp-const-p end))
1243 (make-symbol "--cl-var--")))
1244 (step-var (and (not (macroexp-const-p step))
1245 (make-symbol "--cl-var--"))))
1246 (and step (numberp step) (<= step 0)
1247 (error "Loop `by' value is not positive: %s" step))
1248 (push (list var (or start 0)) loop-for-bindings)
1249 (if end-var (push (list end-var end) loop-for-bindings))
1250 (if step-var (push (list step-var step)
1251 loop-for-bindings))
1252 (if end
1253 (push (list
1254 (if down (if excl '> '>=) (if excl '< '<=))
1255 var (or end-var end))
1256 cl--loop-body))
1257 (push (list var (list (if down '- '+) var
1258 (or step-var step 1)))
1259 loop-for-steps)))
1261 ((memq word '(in in-ref on))
1262 (let* ((on (eq word 'on))
1263 (temp (if (and on (symbolp var))
1264 var (make-symbol "--cl-var--"))))
1265 (push (list temp (pop cl--loop-args)) loop-for-bindings)
1266 (push `(consp ,temp) cl--loop-body)
1267 (if (eq word 'in-ref)
1268 (push (list var `(car ,temp)) cl--loop-symbol-macs)
1269 (or (eq temp var)
1270 (progn
1271 (push (list var nil) loop-for-bindings)
1272 (push (list var (if on temp `(car ,temp)))
1273 loop-for-sets))))
1274 (push (list temp
1275 (if (eq (car cl--loop-args) 'by)
1276 (let ((step (cl--pop2 cl--loop-args)))
1277 (if (and (memq (car-safe step)
1278 '(quote function
1279 cl-function))
1280 (symbolp (nth 1 step)))
1281 (list (nth 1 step) temp)
1282 `(funcall ,step ,temp)))
1283 `(cdr ,temp)))
1284 loop-for-steps)))
1286 ((eq word '=)
1287 (let* ((start (pop cl--loop-args))
1288 (then (if (eq (car cl--loop-args) 'then)
1289 (cl--pop2 cl--loop-args) start)))
1290 (push (list var nil) loop-for-bindings)
1291 (if (or ands (eq (car cl--loop-args) 'and))
1292 (progn
1293 (push `(,var
1294 (if ,(or cl--loop-first-flag
1295 (setq cl--loop-first-flag
1296 (make-symbol "--cl-var--")))
1297 ,start ,var))
1298 loop-for-sets)
1299 (push (list var then) loop-for-steps))
1300 (push (list var
1301 (if (eq start then) start
1302 `(if ,(or cl--loop-first-flag
1303 (setq cl--loop-first-flag
1304 (make-symbol "--cl-var--")))
1305 ,start ,then)))
1306 loop-for-sets))))
1308 ((memq word '(across across-ref))
1309 (let ((temp-vec (make-symbol "--cl-vec--"))
1310 (temp-idx (make-symbol "--cl-idx--")))
1311 (push (list temp-vec (pop cl--loop-args)) loop-for-bindings)
1312 (push (list temp-idx -1) loop-for-bindings)
1313 (push `(< (setq ,temp-idx (1+ ,temp-idx))
1314 (length ,temp-vec))
1315 cl--loop-body)
1316 (if (eq word 'across-ref)
1317 (push (list var `(aref ,temp-vec ,temp-idx))
1318 cl--loop-symbol-macs)
1319 (push (list var nil) loop-for-bindings)
1320 (push (list var `(aref ,temp-vec ,temp-idx))
1321 loop-for-sets))))
1323 ((memq word '(element elements))
1324 (let ((ref (or (memq (car cl--loop-args) '(in-ref of-ref))
1325 (and (not (memq (car cl--loop-args) '(in of)))
1326 (error "Expected `of'"))))
1327 (seq (cl--pop2 cl--loop-args))
1328 (temp-seq (make-symbol "--cl-seq--"))
1329 (temp-idx
1330 (if (eq (car cl--loop-args) 'using)
1331 (if (and (= (length (cadr cl--loop-args)) 2)
1332 (eq (cl-caadr cl--loop-args) 'index))
1333 (cadr (cl--pop2 cl--loop-args))
1334 (error "Bad `using' clause"))
1335 (make-symbol "--cl-idx--"))))
1336 (push (list temp-seq seq) loop-for-bindings)
1337 (push (list temp-idx 0) loop-for-bindings)
1338 (if ref
1339 (let ((temp-len (make-symbol "--cl-len--")))
1340 (push (list temp-len `(length ,temp-seq))
1341 loop-for-bindings)
1342 (push (list var `(elt ,temp-seq ,temp-idx))
1343 cl--loop-symbol-macs)
1344 (push `(< ,temp-idx ,temp-len) cl--loop-body))
1345 (push (list var nil) loop-for-bindings)
1346 (push `(and ,temp-seq
1347 (or (consp ,temp-seq)
1348 (< ,temp-idx (length ,temp-seq))))
1349 cl--loop-body)
1350 (push (list var `(if (consp ,temp-seq)
1351 (pop ,temp-seq)
1352 (aref ,temp-seq ,temp-idx)))
1353 loop-for-sets))
1354 (push (list temp-idx `(1+ ,temp-idx))
1355 loop-for-steps)))
1357 ((memq word hash-types)
1358 (or (memq (car cl--loop-args) '(in of))
1359 (error "Expected `of'"))
1360 (let* ((table (cl--pop2 cl--loop-args))
1361 (other
1362 (if (eq (car cl--loop-args) 'using)
1363 (if (and (= (length (cadr cl--loop-args)) 2)
1364 (memq (cl-caadr cl--loop-args) hash-types)
1365 (not (eq (cl-caadr cl--loop-args) word)))
1366 (cadr (cl--pop2 cl--loop-args))
1367 (error "Bad `using' clause"))
1368 (make-symbol "--cl-var--"))))
1369 (if (memq word '(hash-value hash-values))
1370 (setq var (prog1 other (setq other var))))
1371 (cl--loop-set-iterator-function
1372 'hash-tables (lambda (body)
1373 `(maphash (lambda (,var ,other) . ,body)
1374 ,table)))))
1376 ((memq word '(symbol present-symbol external-symbol
1377 symbols present-symbols external-symbols))
1378 (let ((ob (and (memq (car cl--loop-args) '(in of))
1379 (cl--pop2 cl--loop-args))))
1380 (cl--loop-set-iterator-function
1381 'symbols (lambda (body)
1382 `(mapatoms (lambda (,var) . ,body) ,ob)))))
1384 ((memq word '(overlay overlays extent extents))
1385 (let ((buf nil) (from nil) (to nil))
1386 (while (memq (car cl--loop-args) '(in of from to))
1387 (cond ((eq (car cl--loop-args) 'from)
1388 (setq from (cl--pop2 cl--loop-args)))
1389 ((eq (car cl--loop-args) 'to)
1390 (setq to (cl--pop2 cl--loop-args)))
1391 (t (setq buf (cl--pop2 cl--loop-args)))))
1392 (cl--loop-set-iterator-function
1393 'overlays (lambda (body)
1394 `(cl--map-overlays
1395 (lambda (,var ,(make-symbol "--cl-var--"))
1396 (progn . ,body) nil)
1397 ,buf ,from ,to)))))
1399 ((memq word '(interval intervals))
1400 (let ((buf nil) (prop nil) (from nil) (to nil)
1401 (var1 (make-symbol "--cl-var1--"))
1402 (var2 (make-symbol "--cl-var2--")))
1403 (while (memq (car cl--loop-args) '(in of property from to))
1404 (cond ((eq (car cl--loop-args) 'from)
1405 (setq from (cl--pop2 cl--loop-args)))
1406 ((eq (car cl--loop-args) 'to)
1407 (setq to (cl--pop2 cl--loop-args)))
1408 ((eq (car cl--loop-args) 'property)
1409 (setq prop (cl--pop2 cl--loop-args)))
1410 (t (setq buf (cl--pop2 cl--loop-args)))))
1411 (if (and (consp var) (symbolp (car var)) (symbolp (cdr var)))
1412 (setq var1 (car var) var2 (cdr var))
1413 (push (list var `(cons ,var1 ,var2)) loop-for-sets))
1414 (cl--loop-set-iterator-function
1415 'intervals (lambda (body)
1416 `(cl--map-intervals
1417 (lambda (,var1 ,var2) . ,body)
1418 ,buf ,prop ,from ,to)))))
1420 ((memq word key-types)
1421 (or (memq (car cl--loop-args) '(in of))
1422 (error "Expected `of'"))
1423 (let ((cl-map (cl--pop2 cl--loop-args))
1424 (other
1425 (if (eq (car cl--loop-args) 'using)
1426 (if (and (= (length (cadr cl--loop-args)) 2)
1427 (memq (cl-caadr cl--loop-args) key-types)
1428 (not (eq (cl-caadr cl--loop-args) word)))
1429 (cadr (cl--pop2 cl--loop-args))
1430 (error "Bad `using' clause"))
1431 (make-symbol "--cl-var--"))))
1432 (if (memq word '(key-binding key-bindings))
1433 (setq var (prog1 other (setq other var))))
1434 (cl--loop-set-iterator-function
1435 'keys (lambda (body)
1436 `(,(if (memq word '(key-seq key-seqs))
1437 'cl--map-keymap-recursively 'map-keymap)
1438 (lambda (,var ,other) . ,body) ,cl-map)))))
1440 ((memq word '(frame frames screen screens))
1441 (let ((temp (make-symbol "--cl-var--")))
1442 (push (list var '(selected-frame))
1443 loop-for-bindings)
1444 (push (list temp nil) loop-for-bindings)
1445 (push `(prog1 (not (eq ,var ,temp))
1446 (or ,temp (setq ,temp ,var)))
1447 cl--loop-body)
1448 (push (list var `(next-frame ,var))
1449 loop-for-steps)))
1451 ((memq word '(window windows))
1452 (let ((scr (and (memq (car cl--loop-args) '(in of))
1453 (cl--pop2 cl--loop-args)))
1454 (temp (make-symbol "--cl-var--"))
1455 (minip (make-symbol "--cl-minip--")))
1456 (push (list var (if scr
1457 `(frame-selected-window ,scr)
1458 '(selected-window)))
1459 loop-for-bindings)
1460 ;; If we started in the minibuffer, we need to
1461 ;; ensure that next-window will bring us back there
1462 ;; at some point. (Bug#7492).
1463 ;; (Consider using walk-windows instead of cl-loop if
1464 ;; you care about such things.)
1465 (push (list minip `(minibufferp (window-buffer ,var)))
1466 loop-for-bindings)
1467 (push (list temp nil) loop-for-bindings)
1468 (push `(prog1 (not (eq ,var ,temp))
1469 (or ,temp (setq ,temp ,var)))
1470 cl--loop-body)
1471 (push (list var `(next-window ,var ,minip))
1472 loop-for-steps)))
1475 ;; This is an advertised interface: (info "(cl)Other Clauses").
1476 (let ((handler (and (symbolp word)
1477 (get word 'cl-loop-for-handler))))
1478 (if handler
1479 (funcall handler var)
1480 (error "Expected a `for' preposition, found %s" word)))))
1481 (eq (car cl--loop-args) 'and))
1482 (setq ands t)
1483 (pop cl--loop-args))
1484 (if (and ands loop-for-bindings)
1485 (push (nreverse loop-for-bindings) cl--loop-bindings)
1486 (setq cl--loop-bindings (nconc (mapcar 'list loop-for-bindings)
1487 cl--loop-bindings)))
1488 (if loop-for-sets
1489 (push `(progn
1490 ,(cl--loop-let (nreverse loop-for-sets) 'setq ands)
1492 cl--loop-body))
1493 (if loop-for-steps
1494 (push (cons (if ands 'cl-psetq 'setq)
1495 (apply 'append (nreverse loop-for-steps)))
1496 cl--loop-steps))))
1498 ((eq word 'repeat)
1499 (let ((temp (make-symbol "--cl-var--")))
1500 (push (list (list temp (pop cl--loop-args))) cl--loop-bindings)
1501 (push `(>= (setq ,temp (1- ,temp)) 0) cl--loop-body)))
1503 ((memq word '(collect collecting))
1504 (let ((what (pop cl--loop-args))
1505 (var (cl--loop-handle-accum nil 'nreverse)))
1506 (if (eq var cl--loop-accum-var)
1507 (push `(progn (push ,what ,var) t) cl--loop-body)
1508 (push `(progn
1509 (setq ,var (nconc ,var (list ,what)))
1511 cl--loop-body))))
1513 ((memq word '(nconc nconcing append appending))
1514 (let ((what (pop cl--loop-args))
1515 (var (cl--loop-handle-accum nil 'nreverse)))
1516 (push `(progn
1517 (setq ,var
1518 ,(if (eq var cl--loop-accum-var)
1519 `(nconc
1520 (,(if (memq word '(nconc nconcing))
1521 #'nreverse #'reverse)
1522 ,what)
1523 ,var)
1524 `(,(if (memq word '(nconc nconcing))
1525 #'nconc #'append)
1526 ,var ,what)))
1528 cl--loop-body)))
1530 ((memq word '(concat concating))
1531 (let ((what (pop cl--loop-args))
1532 (var (cl--loop-handle-accum "")))
1533 (push `(progn (cl-callf concat ,var ,what) t) cl--loop-body)))
1535 ((memq word '(vconcat vconcating))
1536 (let ((what (pop cl--loop-args))
1537 (var (cl--loop-handle-accum [])))
1538 (push `(progn (cl-callf vconcat ,var ,what) t) cl--loop-body)))
1540 ((memq word '(sum summing))
1541 (let ((what (pop cl--loop-args))
1542 (var (cl--loop-handle-accum 0)))
1543 (push `(progn (cl-incf ,var ,what) t) cl--loop-body)))
1545 ((memq word '(count counting))
1546 (let ((what (pop cl--loop-args))
1547 (var (cl--loop-handle-accum 0)))
1548 (push `(progn (if ,what (cl-incf ,var)) t) cl--loop-body)))
1550 ((memq word '(minimize minimizing maximize maximizing))
1551 (push `(progn ,(macroexp-let2 macroexp-copyable-p temp
1552 (pop cl--loop-args)
1553 (let* ((var (cl--loop-handle-accum nil))
1554 (func (intern (substring (symbol-name word)
1555 0 3))))
1556 `(setq ,var (if ,var (,func ,var ,temp) ,temp))))
1558 cl--loop-body))
1560 ((eq word 'with)
1561 (let ((bindings nil))
1562 (while (progn (push (list (pop cl--loop-args)
1563 (and (eq (car cl--loop-args) '=)
1564 (cl--pop2 cl--loop-args)))
1565 bindings)
1566 (eq (car cl--loop-args) 'and))
1567 (pop cl--loop-args))
1568 (push (nreverse bindings) cl--loop-bindings)))
1570 ((eq word 'while)
1571 (push (pop cl--loop-args) cl--loop-body))
1573 ((eq word 'until)
1574 (push `(not ,(pop cl--loop-args)) cl--loop-body))
1576 ((eq word 'always)
1577 (or cl--loop-finish-flag
1578 (setq cl--loop-finish-flag (make-symbol "--cl-flag--")))
1579 (push `(setq ,cl--loop-finish-flag ,(pop cl--loop-args)) cl--loop-body)
1580 (setq cl--loop-result t))
1582 ((eq word 'never)
1583 (or cl--loop-finish-flag
1584 (setq cl--loop-finish-flag (make-symbol "--cl-flag--")))
1585 (push `(setq ,cl--loop-finish-flag (not ,(pop cl--loop-args)))
1586 cl--loop-body)
1587 (setq cl--loop-result t))
1589 ((eq word 'thereis)
1590 (or cl--loop-finish-flag
1591 (setq cl--loop-finish-flag (make-symbol "--cl-flag--")))
1592 (or cl--loop-result-var
1593 (setq cl--loop-result-var (make-symbol "--cl-var--")))
1594 (push `(setq ,cl--loop-finish-flag
1595 (not (setq ,cl--loop-result-var ,(pop cl--loop-args))))
1596 cl--loop-body))
1598 ((memq word '(if when unless))
1599 (let* ((cond (pop cl--loop-args))
1600 (then (let ((cl--loop-body nil))
1601 (cl--parse-loop-clause)
1602 (cl--loop-build-ands (nreverse cl--loop-body))))
1603 (else (let ((cl--loop-body nil))
1604 (if (eq (car cl--loop-args) 'else)
1605 (progn (pop cl--loop-args) (cl--parse-loop-clause)))
1606 (cl--loop-build-ands (nreverse cl--loop-body))))
1607 (simple (and (eq (car then) t) (eq (car else) t))))
1608 (if (eq (car cl--loop-args) 'end) (pop cl--loop-args))
1609 (if (eq word 'unless) (setq then (prog1 else (setq else then))))
1610 (let ((form (cons (if simple (cons 'progn (nth 1 then)) (nth 2 then))
1611 (if simple (nth 1 else) (list (nth 2 else))))))
1612 (setq form (if (cl--expr-contains form 'it)
1613 `(let ((it ,cond)) (if it ,@form))
1614 `(if ,cond ,@form)))
1615 (push (if simple `(progn ,form t) form) cl--loop-body))))
1617 ((memq word '(do doing))
1618 (let ((body nil))
1619 (or (consp (car cl--loop-args)) (error "Syntax error on `do' clause"))
1620 (while (consp (car cl--loop-args)) (push (pop cl--loop-args) body))
1621 (push (cons 'progn (nreverse (cons t body))) cl--loop-body)))
1623 ((eq word 'return)
1624 (or cl--loop-finish-flag
1625 (setq cl--loop-finish-flag (make-symbol "--cl-var--")))
1626 (or cl--loop-result-var
1627 (setq cl--loop-result-var (make-symbol "--cl-var--")))
1628 (push `(setq ,cl--loop-result-var ,(pop cl--loop-args)
1629 ,cl--loop-finish-flag nil)
1630 cl--loop-body))
1633 ;; This is an advertised interface: (info "(cl)Other Clauses").
1634 (let ((handler (and (symbolp word) (get word 'cl-loop-handler))))
1635 (or handler (error "Expected a cl-loop keyword, found %s" word))
1636 (funcall handler))))
1637 (if (eq (car cl--loop-args) 'and)
1638 (progn (pop cl--loop-args) (cl--parse-loop-clause)))))
1640 (defun cl--unused-var-p (sym)
1641 (or (null sym) (eq ?_ (aref (symbol-name sym) 0))))
1643 (defun cl--loop-let (specs body par) ; modifies cl--loop-bindings
1644 "Build an expression equivalent to (let SPECS BODY).
1645 SPECS can include bindings using `cl-loop's destructuring (not to be
1646 confused with the patterns of `cl-destructuring-bind').
1647 If PAR is nil, do the bindings step by step, like `let*'.
1648 If BODY is `setq', then use SPECS for assignments rather than for bindings."
1649 (let ((temps nil) (new nil))
1650 (when par
1651 (let ((p specs))
1652 (while (and p (or (symbolp (car-safe (car p))) (null (cl-cadar p))))
1653 (setq p (cdr p)))
1654 (when p
1655 (setq par nil)
1656 (dolist (spec specs)
1657 (or (macroexp-const-p (cadr spec))
1658 (let ((temp (make-symbol "--cl-var--")))
1659 (push (list temp (cadr spec)) temps)
1660 (setcar (cdr spec) temp)))))))
1661 (while specs
1662 (let* ((binding (pop specs))
1663 (spec (car-safe binding)))
1664 (if (and (consp binding) (or (consp spec) (cl--unused-var-p spec)))
1665 (let* ((nspecs nil)
1666 (expr (car (cdr-safe binding)))
1667 (temp (last spec 0)))
1668 (if (and (cl--unused-var-p temp) (null expr))
1669 nil ;; Don't bother declaring/setting `temp' since it won't
1670 ;; be used when `expr' is nil, anyway.
1671 (when (or (null temp)
1672 (and (eq body 'setq) (cl--unused-var-p temp)))
1673 ;; Prefer a fresh uninterned symbol over "_to", to avoid
1674 ;; warnings that we set an unused variable.
1675 (setq temp (make-symbol "--cl-var--"))
1676 ;; Make sure this temp variable is locally declared.
1677 (when (eq body 'setq)
1678 (push (list (list temp)) cl--loop-bindings)))
1679 (push (list temp expr) new))
1680 (while (consp spec)
1681 (push (list (pop spec)
1682 (and expr (list (if spec 'pop 'car) temp)))
1683 nspecs))
1684 (setq specs (nconc (nreverse nspecs) specs)))
1685 (push binding new))))
1686 (if (eq body 'setq)
1687 (let ((set (cons (if par 'cl-psetq 'setq)
1688 (apply 'nconc (nreverse new)))))
1689 (if temps `(let* ,(nreverse temps) ,set) set))
1690 `(,(if par 'let 'let*)
1691 ,(nconc (nreverse temps) (nreverse new)) ,@body))))
1693 (defun cl--loop-handle-accum (def &optional func) ; uses loop-*
1694 (if (eq (car cl--loop-args) 'into)
1695 (let ((var (cl--pop2 cl--loop-args)))
1696 (or (memq var cl--loop-accum-vars)
1697 (progn (push (list (list var def)) cl--loop-bindings)
1698 (push var cl--loop-accum-vars)))
1699 var)
1700 (or cl--loop-accum-var
1701 (progn
1702 (push (list (list
1703 (setq cl--loop-accum-var (make-symbol "--cl-var--"))
1704 def))
1705 cl--loop-bindings)
1706 (setq cl--loop-result (if func (list func cl--loop-accum-var)
1707 cl--loop-accum-var))
1708 cl--loop-accum-var))))
1710 (defun cl--loop-build-ands (clauses)
1711 "Return various representations of (and . CLAUSES).
1712 CLAUSES is a list of Elisp expressions, where clauses of the form
1713 \(progn E1 E2 E3 .. t) are the focus of particular optimizations.
1714 The return value has shape (COND BODY COMBO)
1715 such that COMBO is equivalent to (and . CLAUSES)."
1716 (let ((ands nil)
1717 (body nil))
1718 ;; Look through `clauses', trying to optimize (progn ,@A t) (progn ,@B) ,@C
1719 ;; into (progn ,@A ,@B) ,@C.
1720 (while clauses
1721 (if (and (eq (car-safe (car clauses)) 'progn)
1722 (eq (car (last (car clauses))) t))
1723 (if (cdr clauses)
1724 (setq clauses (cons (nconc (butlast (car clauses))
1725 (if (eq (car-safe (cadr clauses))
1726 'progn)
1727 (cl-cdadr clauses)
1728 (list (cadr clauses))))
1729 (cddr clauses)))
1730 ;; A final (progn ,@A t) is moved outside of the `and'.
1731 (setq body (cdr (butlast (pop clauses)))))
1732 (push (pop clauses) ands)))
1733 (setq ands (or (nreverse ands) (list t)))
1734 (list (if (cdr ands) (cons 'and ands) (car ands))
1735 body
1736 (let ((full (if body
1737 (append ands (list (cons 'progn (append body '(t)))))
1738 ands)))
1739 (if (cdr full) (cons 'and full) (car full))))))
1742 ;;; Other iteration control structures.
1744 ;;;###autoload
1745 (defmacro cl-do (steps endtest &rest body)
1746 "The Common Lisp `do' loop.
1748 \(fn ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)"
1749 (declare (indent 2)
1750 (debug
1751 ((&rest &or symbolp (symbolp &optional form form))
1752 (form body)
1753 cl-declarations body)))
1754 (cl--expand-do-loop steps endtest body nil))
1756 ;;;###autoload
1757 (defmacro cl-do* (steps endtest &rest body)
1758 "The Common Lisp `do*' loop.
1760 \(fn ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)"
1761 (declare (indent 2) (debug cl-do))
1762 (cl--expand-do-loop steps endtest body t))
1764 (defun cl--expand-do-loop (steps endtest body star)
1765 `(cl-block nil
1766 (,(if star 'let* 'let)
1767 ,(mapcar (lambda (c) (if (consp c) (list (car c) (nth 1 c)) c))
1768 steps)
1769 (while (not ,(car endtest))
1770 ,@body
1771 ,@(let ((sets (mapcar (lambda (c)
1772 (and (consp c) (cdr (cdr c))
1773 (list (car c) (nth 2 c))))
1774 steps)))
1775 (setq sets (delq nil sets))
1776 (and sets
1777 (list (cons (if (or star (not (cdr sets)))
1778 'setq 'cl-psetq)
1779 (apply 'append sets))))))
1780 ,@(or (cdr endtest) '(nil)))))
1782 ;;;###autoload
1783 (defmacro cl-dolist (spec &rest body)
1784 "Loop over a list.
1785 Evaluate BODY with VAR bound to each `car' from LIST, in turn.
1786 Then evaluate RESULT to get return value, default nil.
1787 An implicit nil block is established around the loop.
1789 \(fn (VAR LIST [RESULT]) BODY...)"
1790 (declare (debug ((symbolp form &optional form) cl-declarations body))
1791 (indent 1))
1792 (let ((loop `(dolist ,spec ,@body)))
1793 (if (advice-member-p 'cl--wrap-in-nil-block 'dolist)
1794 loop `(cl-block nil ,loop))))
1796 ;;;###autoload
1797 (defmacro cl-dotimes (spec &rest body)
1798 "Loop a certain number of times.
1799 Evaluate BODY with VAR bound to successive integers from 0, inclusive,
1800 to COUNT, exclusive. Then evaluate RESULT to get return value, default
1801 nil.
1803 \(fn (VAR COUNT [RESULT]) BODY...)"
1804 (declare (debug cl-dolist) (indent 1))
1805 (let ((loop `(dotimes ,spec ,@body)))
1806 (if (advice-member-p 'cl--wrap-in-nil-block 'dotimes)
1807 loop `(cl-block nil ,loop))))
1809 (defvar cl--tagbody-alist nil)
1811 ;;;###autoload
1812 (defmacro cl-tagbody (&rest labels-or-stmts)
1813 "Execute statements while providing for control transfers to labels.
1814 Each element of LABELS-OR-STMTS can be either a label (integer or symbol)
1815 or a `cons' cell, in which case it's taken to be a statement.
1816 This distinction is made before performing macroexpansion.
1817 Statements are executed in sequence left to right, discarding any return value,
1818 stopping only when reaching the end of LABELS-OR-STMTS.
1819 Any statement can transfer control at any time to the statements that follow
1820 one of the labels with the special form (go LABEL).
1821 Labels have lexical scope and dynamic extent."
1822 (let ((blocks '())
1823 (first-label (if (consp (car labels-or-stmts))
1824 'cl--preamble (pop labels-or-stmts))))
1825 (let ((block (list first-label)))
1826 (dolist (label-or-stmt labels-or-stmts)
1827 (if (consp label-or-stmt) (push label-or-stmt block)
1828 ;; Add a "go to next block" to implement the fallthrough.
1829 (unless (eq 'go (car-safe (car-safe block)))
1830 (push `(go ,label-or-stmt) block))
1831 (push (nreverse block) blocks)
1832 (setq block (list label-or-stmt))))
1833 (unless (eq 'go (car-safe (car-safe block)))
1834 (push `(go cl--exit) block))
1835 (push (nreverse block) blocks))
1836 (let ((catch-tag (make-symbol "cl--tagbody-tag"))
1837 (cl--tagbody-alist cl--tagbody-alist))
1838 (push (cons 'cl--exit catch-tag) cl--tagbody-alist)
1839 (dolist (block blocks)
1840 (push (cons (car block) catch-tag) cl--tagbody-alist))
1841 (macroexpand-all
1842 `(let ((next-label ',first-label))
1843 (while
1844 (not (eq (setq next-label
1845 (catch ',catch-tag
1846 (cl-case next-label
1847 ,@blocks)))
1848 'cl--exit))))
1849 `((go . ,(lambda (label)
1850 (let ((catch-tag (cdr (assq label cl--tagbody-alist))))
1851 (unless catch-tag
1852 (error "Unknown cl-tagbody go label `%S'" label))
1853 `(throw ',catch-tag ',label))))
1854 ,@macroexpand-all-environment)))))
1856 (defun cl--prog (binder bindings body)
1857 (let (decls)
1858 (while (eq 'declare (car-safe (car body)))
1859 (push (pop body) decls))
1860 `(cl-block nil
1861 (,binder ,bindings
1862 ,@(nreverse decls)
1863 (cl-tagbody . ,body)))))
1865 ;;;###autoload
1866 (defmacro cl-prog (bindings &rest body)
1867 "Run BODY like a `cl-tagbody' after setting up the BINDINGS.
1868 Shorthand for (cl-block nil (let BINDINGS (cl-tagbody BODY)))"
1869 (cl--prog 'let bindings body))
1871 ;;;###autoload
1872 (defmacro cl-prog* (bindings &rest body)
1873 "Run BODY like a `cl-tagbody' after setting up the BINDINGS.
1874 Shorthand for (cl-block nil (let* BINDINGS (cl-tagbody BODY)))"
1875 (cl--prog 'let* bindings body))
1877 ;;;###autoload
1878 (defmacro cl-do-symbols (spec &rest body)
1879 "Loop over all symbols.
1880 Evaluate BODY with VAR bound to each interned symbol, or to each symbol
1881 from OBARRAY.
1883 \(fn (VAR [OBARRAY [RESULT]]) BODY...)"
1884 (declare (indent 1)
1885 (debug ((symbolp &optional form form) cl-declarations body)))
1886 ;; Apparently this doesn't have an implicit block.
1887 `(cl-block nil
1888 (let (,(car spec))
1889 (mapatoms #'(lambda (,(car spec)) ,@body)
1890 ,@(and (cadr spec) (list (cadr spec))))
1891 ,(nth 2 spec))))
1893 ;;;###autoload
1894 (defmacro cl-do-all-symbols (spec &rest body)
1895 "Like `cl-do-symbols', but use the default obarray.
1897 \(fn (VAR [RESULT]) BODY...)"
1898 (declare (indent 1) (debug ((symbolp &optional form) cl-declarations body)))
1899 `(cl-do-symbols (,(car spec) nil ,(cadr spec)) ,@body))
1902 ;;; Assignments.
1904 ;;;###autoload
1905 (defmacro cl-psetq (&rest args)
1906 "Set SYMs to the values VALs in parallel.
1907 This is like `setq', except that all VAL forms are evaluated (in order)
1908 before assigning any symbols SYM to the corresponding values.
1910 \(fn SYM VAL SYM VAL ...)"
1911 (declare (debug setq))
1912 (cons 'cl-psetf args))
1915 ;;; Binding control structures.
1917 ;;;###autoload
1918 (defmacro cl-progv (symbols values &rest body)
1919 "Bind SYMBOLS to VALUES dynamically in BODY.
1920 The forms SYMBOLS and VALUES are evaluated, and must evaluate to lists.
1921 Each symbol in the first list is bound to the corresponding value in the
1922 second list (or to nil if VALUES is shorter than SYMBOLS); then the
1923 BODY forms are executed and their result is returned. This is much like
1924 a `let' form, except that the list of symbols can be computed at run-time."
1925 (declare (indent 2) (debug (form form body)))
1926 (let ((bodyfun (make-symbol "body"))
1927 (binds (make-symbol "binds"))
1928 (syms (make-symbol "syms"))
1929 (vals (make-symbol "vals")))
1930 `(progn
1931 (let* ((,syms ,symbols)
1932 (,vals ,values)
1933 (,bodyfun (lambda () ,@body))
1934 (,binds ()))
1935 (while ,syms
1936 (push (list (pop ,syms) (list 'quote (pop ,vals))) ,binds))
1937 (eval (list 'let ,binds (list 'funcall (list 'quote ,bodyfun))))))))
1939 (defconst cl--labels-magic (make-symbol "cl--labels-magic"))
1941 (defvar cl--labels-convert-cache nil)
1943 (defun cl--labels-convert (f)
1944 "Special macro-expander to rename (function F) references in `cl-labels'."
1945 (cond
1946 ;; ¡¡Big Ugly Hack!! We can't use a compiler-macro because those are checked
1947 ;; *after* handling `function', but we want to stop macroexpansion from
1948 ;; being applied infinitely, so we use a cache to return the exact `form'
1949 ;; being expanded even though we don't receive it.
1950 ((eq f (car cl--labels-convert-cache)) (cdr cl--labels-convert-cache))
1952 (let* ((found (assq f macroexpand-all-environment))
1953 (replacement (and found
1954 (ignore-errors
1955 (funcall (cdr found) cl--labels-magic)))))
1956 (if (and replacement (eq cl--labels-magic (car replacement)))
1957 (nth 1 replacement)
1958 (let ((res `(function ,f)))
1959 (setq cl--labels-convert-cache (cons f res))
1960 res))))))
1962 ;;;###autoload
1963 (defmacro cl-flet (bindings &rest body)
1964 "Make local function definitions.
1965 Like `cl-labels' but the definitions are not recursive.
1966 Each binding can take the form (FUNC EXP) where
1967 FUNC is the function name, and EXP is an expression that returns the
1968 function value to which it should be bound, or it can take the more common
1969 form \(FUNC ARGLIST BODY...) which is a shorthand
1970 for (FUNC (lambda ARGLIST BODY)).
1972 \(fn ((FUNC ARGLIST BODY...) ...) FORM...)"
1973 (declare (indent 1) (debug ((&rest (cl-defun)) cl-declarations body)))
1974 (let ((binds ()) (newenv macroexpand-all-environment))
1975 (dolist (binding bindings)
1976 (let ((var (make-symbol (format "--cl-%s--" (car binding))))
1977 (args-and-body (cdr binding)))
1978 (if (and (= (length args-and-body) 1) (symbolp (car args-and-body)))
1979 ;; Optimize (cl-flet ((fun var)) body).
1980 (setq var (car args-and-body))
1981 (push (list var (if (= (length args-and-body) 1)
1982 (car args-and-body)
1983 `(cl-function (lambda . ,args-and-body))))
1984 binds))
1985 (push (cons (car binding)
1986 (lambda (&rest args)
1987 (if (eq (car args) cl--labels-magic)
1988 (list cl--labels-magic var)
1989 `(funcall ,var ,@args))))
1990 newenv)))
1991 ;; FIXME: Eliminate those functions which aren't referenced.
1992 (macroexp-let* (nreverse binds)
1993 (macroexpand-all
1994 `(progn ,@body)
1995 ;; Don't override lexical-let's macro-expander.
1996 (if (assq 'function newenv) newenv
1997 (cons (cons 'function #'cl--labels-convert) newenv))))))
1999 ;;;###autoload
2000 (defmacro cl-flet* (bindings &rest body)
2001 "Make local function definitions.
2002 Like `cl-flet' but the definitions can refer to previous ones.
2004 \(fn ((FUNC ARGLIST BODY...) ...) FORM...)"
2005 (declare (indent 1) (debug cl-flet))
2006 (cond
2007 ((null bindings) (macroexp-progn body))
2008 ((null (cdr bindings)) `(cl-flet ,bindings ,@body))
2009 (t `(cl-flet (,(pop bindings)) (cl-flet* ,bindings ,@body)))))
2011 ;;;###autoload
2012 (defmacro cl-labels (bindings &rest body)
2013 "Make temporary function bindings.
2014 The bindings can be recursive and the scoping is lexical, but capturing them
2015 in closures will only work if `lexical-binding' is in use.
2017 \(fn ((FUNC ARGLIST BODY...) ...) FORM...)"
2018 (declare (indent 1) (debug cl-flet))
2019 (let ((binds ()) (newenv macroexpand-all-environment))
2020 (dolist (binding bindings)
2021 (let ((var (make-symbol (format "--cl-%s--" (car binding)))))
2022 (push (list var `(cl-function (lambda . ,(cdr binding)))) binds)
2023 (push (cons (car binding)
2024 (lambda (&rest args)
2025 (if (eq (car args) cl--labels-magic)
2026 (list cl--labels-magic var)
2027 (cl-list* 'funcall var args))))
2028 newenv)))
2029 (macroexpand-all `(letrec ,(nreverse binds) ,@body)
2030 ;; Don't override lexical-let's macro-expander.
2031 (if (assq 'function newenv) newenv
2032 (cons (cons 'function #'cl--labels-convert) newenv)))))
2034 ;; The following ought to have a better definition for use with newer
2035 ;; byte compilers.
2036 ;;;###autoload
2037 (defmacro cl-macrolet (bindings &rest body)
2038 "Make temporary macro definitions.
2039 This is like `cl-flet', but for macros instead of functions.
2041 \(fn ((NAME ARGLIST BODY...) ...) FORM...)"
2042 (declare (indent 1)
2043 (debug
2044 ((&rest (&define name (&rest arg) cl-declarations-or-string
2045 def-body))
2046 cl-declarations body)))
2047 (if (cdr bindings)
2048 `(cl-macrolet (,(car bindings)) (cl-macrolet ,(cdr bindings) ,@body))
2049 (if (null bindings) (macroexp-progn body)
2050 (let* ((name (caar bindings))
2051 (res (cl--transform-lambda (cdar bindings) name)))
2052 (eval (car res))
2053 (macroexpand-all (macroexp-progn body)
2054 (cons (cons name
2055 (eval `(cl-function (lambda ,@(cdr res))) t))
2056 macroexpand-all-environment))))))
2058 (defconst cl--old-macroexpand
2059 (if (and (boundp 'cl--old-macroexpand)
2060 (eq (symbol-function 'macroexpand)
2061 #'cl--sm-macroexpand))
2062 cl--old-macroexpand
2063 (symbol-function 'macroexpand)))
2065 (defun cl--sm-macroexpand (exp &optional env)
2066 "Special macro expander used inside `cl-symbol-macrolet'.
2067 This function replaces `macroexpand' during macro expansion
2068 of `cl-symbol-macrolet', and does the same thing as `macroexpand'
2069 except that it additionally expands symbol macros."
2070 (let ((macroexpand-all-environment env)
2071 (venv (alist-get :cl-symbol-macros env)))
2072 (while
2073 (progn
2074 (setq exp (funcall cl--old-macroexpand exp env))
2075 (pcase exp
2076 ((pred symbolp)
2077 ;; Perform symbol-macro expansion.
2078 (let ((symval (assq exp venv)))
2079 (when symval
2080 (setq exp (cadr symval)))))
2081 (`(setq . ,_)
2082 ;; Convert setq to setf if required by symbol-macro expansion.
2083 (let* ((args (mapcar (lambda (f) (cl--sm-macroexpand f env))
2084 (cdr exp)))
2085 (p args))
2086 (while (and p (symbolp (car p))) (setq p (cddr p)))
2087 (if p (setq exp (cons 'setf args))
2088 (setq exp (cons 'setq args))
2089 ;; Don't loop further.
2090 nil)))
2091 (`(,(or `let `let*) . ,(or `(,bindings . ,body) dontcare))
2092 ;; CL's symbol-macrolet treats re-bindings as candidates for
2093 ;; expansion (turning the let into a letf if needed), contrary to
2094 ;; Common-Lisp where such re-bindings hide the symbol-macro.
2095 (let ((letf nil) (found nil) (nbs ()))
2096 (dolist (binding bindings)
2097 (let* ((var (if (symbolp binding) binding (car binding)))
2098 (sm (assq var venv)))
2099 (push (if (not (cdr sm))
2100 binding
2101 (let ((nexp (cadr sm)))
2102 (setq found t)
2103 (unless (symbolp nexp) (setq letf t))
2104 (cons nexp (cdr-safe binding))))
2105 nbs)))
2106 (when found
2107 (setq exp `(,(if letf
2108 (if (eq (car exp) 'let) 'cl-letf 'cl-letf*)
2109 (car exp))
2110 ,(nreverse nbs)
2111 ,@body)))))
2112 ;; FIXME: The behavior of CL made sense in a dynamically scoped
2113 ;; language, but for lexical scoping, Common-Lisp's behavior might
2114 ;; make more sense (and indeed, CL behaves like Common-Lisp w.r.t
2115 ;; lexical-let), so maybe we should adjust the behavior based on
2116 ;; the use of lexical-binding.
2117 ;; (`(,(or `let `let*) . ,(or `(,bindings . ,body) dontcare))
2118 ;; (let ((nbs ()) (found nil))
2119 ;; (dolist (binding bindings)
2120 ;; (let* ((var (if (symbolp binding) binding (car binding)))
2121 ;; (name (symbol-name var))
2122 ;; (val (and found (consp binding) (eq 'let* (car exp))
2123 ;; (list (macroexpand-all (cadr binding)
2124 ;; env)))))
2125 ;; (push (if (assq name env)
2126 ;; ;; This binding should hide its symbol-macro,
2127 ;; ;; but given the way macroexpand-all works, we
2128 ;; ;; can't prevent application of `env' to the
2129 ;; ;; sub-expressions, so we need to α-rename this
2130 ;; ;; variable instead.
2131 ;; (let ((nvar (make-symbol
2132 ;; (copy-sequence name))))
2133 ;; (setq found t)
2134 ;; (push (list name nvar) env)
2135 ;; (cons nvar (or val (cdr-safe binding))))
2136 ;; (if val (cons var val) binding))
2137 ;; nbs)))
2138 ;; (when found
2139 ;; (setq exp `(,(car exp)
2140 ;; ,(nreverse nbs)
2141 ;; ,@(macroexp-unprogn
2142 ;; (macroexpand-all (macroexp-progn body)
2143 ;; env)))))
2144 ;; nil))
2146 exp))
2148 ;;;###autoload
2149 (defmacro cl-symbol-macrolet (bindings &rest body)
2150 "Make symbol macro definitions.
2151 Within the body FORMs, references to the variable NAME will be replaced
2152 by EXPANSION, and (setq NAME ...) will act like (setf EXPANSION ...).
2154 \(fn ((NAME EXPANSION) ...) FORM...)"
2155 (declare (indent 1) (debug ((&rest (symbolp sexp)) cl-declarations body)))
2156 (let ((previous-macroexpand (symbol-function 'macroexpand))
2157 (malformed-bindings nil))
2158 (dolist (binding bindings)
2159 (unless (and (consp binding) (symbolp (car binding))
2160 (consp (cdr binding)) (null (cddr binding)))
2161 (push binding malformed-bindings)))
2162 (unwind-protect
2163 (progn
2164 (fset 'macroexpand #'cl--sm-macroexpand)
2165 (let* ((venv (cdr (assq :cl-symbol-macros macroexpand-all-environment)))
2166 (expansion
2167 (macroexpand-all (macroexp-progn body)
2168 (cons (cons :cl-symbol-macros
2169 (append bindings venv))
2170 macroexpand-all-environment))))
2171 (if malformed-bindings
2172 (macroexp--warn-and-return
2173 (format-message "Malformed `cl-symbol-macrolet' binding(s): %S"
2174 (nreverse malformed-bindings))
2175 expansion)
2176 expansion)))
2177 (fset 'macroexpand previous-macroexpand))))
2179 ;;; Multiple values.
2181 ;;;###autoload
2182 (defmacro cl-multiple-value-bind (vars form &rest body)
2183 "Collect multiple return values.
2184 FORM must return a list; the BODY is then executed with the first N elements
2185 of this list bound (`let'-style) to each of the symbols SYM in turn. This
2186 is analogous to the Common Lisp `multiple-value-bind' macro, using lists to
2187 simulate true multiple return values. For compatibility, (cl-values A B C) is
2188 a synonym for (list A B C).
2190 \(fn (SYM...) FORM BODY)"
2191 (declare (indent 2) (debug ((&rest symbolp) form body)))
2192 (let ((temp (make-symbol "--cl-var--")) (n -1))
2193 `(let* ((,temp ,form)
2194 ,@(mapcar (lambda (v)
2195 (list v `(nth ,(setq n (1+ n)) ,temp)))
2196 vars))
2197 ,@body)))
2199 ;;;###autoload
2200 (defmacro cl-multiple-value-setq (vars form)
2201 "Collect multiple return values.
2202 FORM must return a list; the first N elements of this list are stored in
2203 each of the symbols SYM in turn. This is analogous to the Common Lisp
2204 `multiple-value-setq' macro, using lists to simulate true multiple return
2205 values. For compatibility, (cl-values A B C) is a synonym for (list A B C).
2207 \(fn (SYM...) FORM)"
2208 (declare (indent 1) (debug ((&rest symbolp) form)))
2209 (cond ((null vars) `(progn ,form nil))
2210 ((null (cdr vars)) `(setq ,(car vars) (car ,form)))
2212 (let* ((temp (make-symbol "--cl-var--")) (n 0))
2213 `(let ((,temp ,form))
2214 (prog1 (setq ,(pop vars) (car ,temp))
2215 (setq ,@(apply #'nconc
2216 (mapcar (lambda (v)
2217 (list v `(nth ,(setq n (1+ n))
2218 ,temp)))
2219 vars)))))))))
2222 ;;; Declarations.
2224 ;;;###autoload
2225 (defmacro cl-locally (&rest body)
2226 "Equivalent to `progn'."
2227 (declare (debug t))
2228 (cons 'progn body))
2229 ;;;###autoload
2230 (defmacro cl-the (type form)
2231 "Return FORM. If type-checking is enabled, assert that it is of TYPE."
2232 (declare (indent 1) (debug (cl-type-spec form)))
2233 (if (not (or (not (cl--compiling-file))
2234 (< cl--optimize-speed 3)
2235 (= cl--optimize-safety 3)))
2236 form
2237 (macroexp-let2 macroexp-copyable-p temp form
2238 `(progn (unless (cl-typep ,temp ',type)
2239 (signal 'wrong-type-argument
2240 (list ',type ,temp ',form)))
2241 ,temp))))
2243 (defvar cl--proclaim-history t) ; for future compilers
2244 (defvar cl--declare-stack t) ; for future compilers
2246 (defun cl--do-proclaim (spec hist)
2247 (and hist (listp cl--proclaim-history) (push spec cl--proclaim-history))
2248 (cond ((eq (car-safe spec) 'special)
2249 (if (boundp 'byte-compile-bound-variables)
2250 (setq byte-compile-bound-variables
2251 (append (cdr spec) byte-compile-bound-variables))))
2253 ((eq (car-safe spec) 'inline)
2254 (while (setq spec (cdr spec))
2255 (or (memq (get (car spec) 'byte-optimizer)
2256 '(nil byte-compile-inline-expand))
2257 (error "%s already has a byte-optimizer, can't make it inline"
2258 (car spec)))
2259 (put (car spec) 'byte-optimizer 'byte-compile-inline-expand)))
2261 ((eq (car-safe spec) 'notinline)
2262 (while (setq spec (cdr spec))
2263 (if (eq (get (car spec) 'byte-optimizer)
2264 'byte-compile-inline-expand)
2265 (put (car spec) 'byte-optimizer nil))))
2267 ((eq (car-safe spec) 'optimize)
2268 (let ((speed (assq (nth 1 (assq 'speed (cdr spec)))
2269 '((0 nil) (1 t) (2 t) (3 t))))
2270 (safety (assq (nth 1 (assq 'safety (cdr spec)))
2271 '((0 t) (1 t) (2 t) (3 nil)))))
2272 (if speed (setq cl--optimize-speed (car speed)
2273 byte-optimize (nth 1 speed)))
2274 (if safety (setq cl--optimize-safety (car safety)
2275 byte-compile-delete-errors (nth 1 safety)))))
2277 ((and (eq (car-safe spec) 'warn) (boundp 'byte-compile-warnings))
2278 (while (setq spec (cdr spec))
2279 (if (consp (car spec))
2280 (if (eq (cl-cadar spec) 0)
2281 (byte-compile-disable-warning (caar spec))
2282 (byte-compile-enable-warning (caar spec)))))))
2283 nil)
2285 ;;; Process any proclamations made before cl-macs was loaded.
2286 (defvar cl--proclaims-deferred)
2287 (let ((p (reverse cl--proclaims-deferred)))
2288 (while p (cl--do-proclaim (pop p) t))
2289 (setq cl--proclaims-deferred nil))
2291 ;;;###autoload
2292 (defmacro cl-declare (&rest specs)
2293 "Declare SPECS about the current function while compiling.
2294 For instance
2296 (cl-declare (warn 0))
2298 will turn off byte-compile warnings in the function.
2299 See Info node `(cl)Declarations' for details."
2300 (if (cl--compiling-file)
2301 (while specs
2302 (if (listp cl--declare-stack) (push (car specs) cl--declare-stack))
2303 (cl--do-proclaim (pop specs) nil)))
2304 nil)
2306 ;;; The standard modify macros.
2308 ;; `setf' is now part of core Elisp, defined in gv.el.
2310 ;;;###autoload
2311 (defmacro cl-psetf (&rest args)
2312 "Set PLACEs to the values VALs in parallel.
2313 This is like `setf', except that all VAL forms are evaluated (in order)
2314 before assigning any PLACEs to the corresponding values.
2316 \(fn PLACE VAL PLACE VAL ...)"
2317 (declare (debug setf))
2318 (let ((p args) (simple t) (vars nil))
2319 (while p
2320 (if (or (not (symbolp (car p))) (cl--expr-depends-p (nth 1 p) vars))
2321 (setq simple nil))
2322 (if (memq (car p) vars)
2323 (error "Destination duplicated in psetf: %s" (car p)))
2324 (push (pop p) vars)
2325 (or p (error "Odd number of arguments to cl-psetf"))
2326 (pop p))
2327 (if simple
2328 `(progn (setq ,@args) nil)
2329 (setq args (reverse args))
2330 (let ((expr `(setf ,(cadr args) ,(car args))))
2331 (while (setq args (cddr args))
2332 (setq expr `(setf ,(cadr args) (prog1 ,(car args) ,expr))))
2333 `(progn ,expr nil)))))
2335 ;;;###autoload
2336 (defmacro cl-remf (place tag)
2337 "Remove TAG from property list PLACE.
2338 PLACE may be a symbol, or any generalized variable allowed by `setf'.
2339 The form returns true if TAG was found and removed, nil otherwise."
2340 (declare (debug (place form)))
2341 (gv-letplace (tval setter) place
2342 (macroexp-let2 macroexp-copyable-p ttag tag
2343 `(if (eq ,ttag (car ,tval))
2344 (progn ,(funcall setter `(cddr ,tval))
2346 (cl--do-remf ,tval ,ttag)))))
2348 ;;;###autoload
2349 (defmacro cl-shiftf (place &rest args)
2350 "Shift left among PLACEs.
2351 Example: (cl-shiftf A B C) sets A to B, B to C, and returns the old A.
2352 Each PLACE may be a symbol, or any generalized variable allowed by `setf'.
2354 \(fn PLACE... VAL)"
2355 (declare (debug (&rest place)))
2356 (cond
2357 ((null args) place)
2358 ((symbolp place) `(prog1 ,place (setq ,place (cl-shiftf ,@args))))
2360 (gv-letplace (getter setter) place
2361 `(prog1 ,getter
2362 ,(funcall setter `(cl-shiftf ,@args)))))))
2364 ;;;###autoload
2365 (defmacro cl-rotatef (&rest args)
2366 "Rotate left among PLACEs.
2367 Example: (cl-rotatef A B C) sets A to B, B to C, and C to A. It returns nil.
2368 Each PLACE may be a symbol, or any generalized variable allowed by `setf'.
2370 \(fn PLACE...)"
2371 (declare (debug (&rest place)))
2372 (if (not (memq nil (mapcar 'symbolp args)))
2373 (and (cdr args)
2374 (let ((sets nil)
2375 (first (car args)))
2376 (while (cdr args)
2377 (setq sets (nconc sets (list (pop args) (car args)))))
2378 `(cl-psetf ,@sets ,(car args) ,first)))
2379 (let* ((places (reverse args))
2380 (temp (make-symbol "--cl-rotatef--"))
2381 (form temp))
2382 (while (cdr places)
2383 (setq form
2384 (gv-letplace (getter setter) (pop places)
2385 `(prog1 ,getter ,(funcall setter form)))))
2386 (gv-letplace (getter setter) (car places)
2387 (macroexp-let* `((,temp ,getter))
2388 `(progn ,(funcall setter form) nil))))))
2390 ;; FIXME: `letf' is unsatisfactory because it does not really "restore" the
2391 ;; previous state. If the getter/setter loses information, that info is
2392 ;; not recovered.
2394 (defun cl--letf (bindings simplebinds binds body)
2395 ;; It's not quite clear what the semantics of cl-letf should be.
2396 ;; E.g. in (cl-letf ((PLACE1 VAL1) (PLACE2 VAL2)) BODY), while it's clear
2397 ;; that the actual assignments ("bindings") should only happen after
2398 ;; evaluating VAL1 and VAL2, it's not clear when the sub-expressions of
2399 ;; PLACE1 and PLACE2 should be evaluated. Should we have
2400 ;; PLACE1; VAL1; PLACE2; VAL2; bind1; bind2
2401 ;; or
2402 ;; VAL1; VAL2; PLACE1; PLACE2; bind1; bind2
2403 ;; or
2404 ;; VAL1; VAL2; PLACE1; bind1; PLACE2; bind2
2405 ;; Common-Lisp's `psetf' does the first, so we'll do the same.
2406 (if (null bindings)
2407 (if (and (null binds) (null simplebinds)) (macroexp-progn body)
2408 `(let* (,@(mapcar (lambda (x)
2409 (pcase-let ((`(,vold ,getter ,_setter ,_vnew) x))
2410 (list vold getter)))
2411 binds)
2412 ,@simplebinds)
2413 (unwind-protect
2414 ,(macroexp-progn
2415 (append
2416 (delq nil
2417 (mapcar (lambda (x)
2418 (pcase x
2419 ;; If there's no vnew, do nothing.
2420 (`(,_vold ,_getter ,setter ,vnew)
2421 (funcall setter vnew))))
2422 binds))
2423 body))
2424 ,@(mapcar (lambda (x)
2425 (pcase-let ((`(,vold ,_getter ,setter ,_vnew) x))
2426 (funcall setter vold)))
2427 binds))))
2428 (let ((binding (car bindings)))
2429 (gv-letplace (getter setter) (car binding)
2430 (macroexp-let2 nil vnew (cadr binding)
2431 (if (symbolp (car binding))
2432 ;; Special-case for simple variables.
2433 (cl--letf (cdr bindings)
2434 (cons `(,getter ,(if (cdr binding) vnew getter))
2435 simplebinds)
2436 binds body)
2437 (cl--letf (cdr bindings) simplebinds
2438 (cons `(,(make-symbol "old") ,getter ,setter
2439 ,@(if (cdr binding) (list vnew)))
2440 binds)
2441 body)))))))
2443 ;;;###autoload
2444 (defmacro cl-letf (bindings &rest body)
2445 "Temporarily bind to PLACEs.
2446 This is the analogue of `let', but with generalized variables (in the
2447 sense of `setf') for the PLACEs. Each PLACE is set to the corresponding
2448 VALUE, then the BODY forms are executed. On exit, either normally or
2449 because of a `throw' or error, the PLACEs are set back to their original
2450 values. Note that this macro is *not* available in Common Lisp.
2451 As a special case, if `(PLACE)' is used instead of `(PLACE VALUE)',
2452 the PLACE is not modified before executing BODY.
2454 \(fn ((PLACE VALUE) ...) BODY...)"
2455 (declare (indent 1) (debug ((&rest [&or (symbolp form)
2456 (gate gv-place &optional form)])
2457 body)))
2458 (if (and (not (cdr bindings)) (cdar bindings) (symbolp (caar bindings)))
2459 `(let ,bindings ,@body)
2460 (cl--letf bindings () () body)))
2462 ;;;###autoload
2463 (defmacro cl-letf* (bindings &rest body)
2464 "Temporarily bind to PLACEs.
2465 Like `cl-letf' but where the bindings are performed one at a time,
2466 rather than all at the end (i.e. like `let*' rather than like `let')."
2467 (declare (indent 1) (debug cl-letf))
2468 (dolist (binding (reverse bindings))
2469 (setq body (list `(cl-letf (,binding) ,@body))))
2470 (macroexp-progn body))
2472 ;;;###autoload
2473 (defmacro cl-callf (func place &rest args)
2474 "Set PLACE to (FUNC PLACE ARGS...).
2475 FUNC should be an unquoted function name. PLACE may be a symbol,
2476 or any generalized variable allowed by `setf'."
2477 (declare (indent 2) (debug (cl-function place &rest form)))
2478 (gv-letplace (getter setter) place
2479 (let* ((rargs (cons getter args)))
2480 (funcall setter
2481 (if (symbolp func) (cons func rargs)
2482 `(funcall #',func ,@rargs))))))
2484 ;;;###autoload
2485 (defmacro cl-callf2 (func arg1 place &rest args)
2486 "Set PLACE to (FUNC ARG1 PLACE ARGS...).
2487 Like `cl-callf', but PLACE is the second argument of FUNC, not the first.
2489 \(fn FUNC ARG1 PLACE ARGS...)"
2490 (declare (indent 3) (debug (cl-function form place &rest form)))
2491 (if (and (cl--safe-expr-p arg1) (cl--simple-expr-p place) (symbolp func))
2492 `(setf ,place (,func ,arg1 ,place ,@args))
2493 (macroexp-let2 nil a1 arg1
2494 (gv-letplace (getter setter) place
2495 (let* ((rargs (cl-list* a1 getter args)))
2496 (funcall setter
2497 (if (symbolp func) (cons func rargs)
2498 `(funcall #',func ,@rargs))))))))
2500 ;;;###autoload
2501 (defmacro cl-defsubst (name args &rest body)
2502 "Define NAME as a function.
2503 Like `defun', except the function is automatically declared `inline' and
2504 the arguments are immutable.
2505 ARGLIST allows full Common Lisp conventions, and BODY is implicitly
2506 surrounded by (cl-block NAME ...).
2507 The function's arguments should be treated as immutable.
2509 \(fn NAME ARGLIST [DOCSTRING] BODY...)"
2510 (declare (debug cl-defun) (indent 2))
2511 (let* ((argns (cl--arglist-args args))
2512 (real-args (if (eq '&cl-defs (car args)) (cddr args) args))
2513 (p argns)
2514 ;; (pbody (cons 'progn body))
2516 (while (and p (eq (cl--expr-contains real-args (car p)) 1)) (pop p))
2517 `(progn
2518 ,(if p nil ; give up if defaults refer to earlier args
2519 `(cl-define-compiler-macro ,name
2520 ,(if (memq '&key args)
2521 `(&whole cl-whole &cl-quote ,@args)
2522 (cons '&cl-quote args))
2523 ,(format "compiler-macro for inlining `%s'." name)
2524 (cl--defsubst-expand
2525 ',argns '(cl-block ,name ,@(cdr (macroexp-parse-body body)))
2526 ;; We used to pass `simple' as
2527 ;; (not (or unsafe (cl-expr-access-order pbody argns)))
2528 ;; But this is much too simplistic since it
2529 ;; does not pay attention to the argvs (and
2530 ;; cl-expr-access-order itself is also too naive).
2532 ,(and (memq '&key args) 'cl-whole) nil ,@argns)))
2533 (cl-defun ,name ,args ,@body))))
2535 (defun cl--defsubst-expand (argns body simple whole _unsafe &rest argvs)
2536 (if (and whole (not (cl--safe-expr-p (cons 'progn argvs)))) whole
2537 (if (cl--simple-exprs-p argvs) (setq simple t))
2538 (let* ((substs ())
2539 (lets (delq nil
2540 (cl-mapcar (lambda (argn argv)
2541 (if (or simple (macroexp-const-p argv))
2542 (progn (push (cons argn argv) substs)
2543 nil)
2544 (list argn argv)))
2545 argns argvs))))
2546 ;; FIXME: `sublis/subst' will happily substitute the symbol
2547 ;; `argn' in places where it's not used as a reference
2548 ;; to a variable.
2549 ;; FIXME: `sublis/subst' will happily copy `argv' to a different
2550 ;; scope, leading to name capture.
2551 (setq body (cond ((null substs) body)
2552 ((null (cdr substs))
2553 (cl-subst (cdar substs) (caar substs) body))
2554 (t (cl--sublis substs body))))
2555 (if lets `(let ,lets ,body) body))))
2557 (defun cl--sublis (alist tree)
2558 "Perform substitutions indicated by ALIST in TREE (non-destructively)."
2559 (let ((x (assq tree alist)))
2560 (cond
2561 (x (cdr x))
2562 ((consp tree)
2563 (cons (cl--sublis alist (car tree)) (cl--sublis alist (cdr tree))))
2564 (t tree))))
2566 ;;; Structures.
2568 (defmacro cl--find-class (type)
2569 `(get ,type 'cl--class))
2571 ;; Rather than hard code cl-structure-object, we indirect through this variable
2572 ;; for bootstrapping reasons.
2573 (defvar cl--struct-default-parent nil)
2575 ;;;###autoload
2576 (defmacro cl-defstruct (struct &rest descs)
2577 "Define a struct type.
2578 This macro defines a new data type called NAME that stores data
2579 in SLOTs. It defines a `make-NAME' constructor, a `copy-NAME'
2580 copier, a `NAME-p' predicate, and slot accessors named `NAME-SLOT'.
2581 You can use the accessors to set the corresponding slots, via `setf'.
2583 NAME may instead take the form (NAME OPTIONS...), where each
2584 OPTION is either a single keyword or (KEYWORD VALUE) where
2585 KEYWORD can be one of :conc-name, :constructor, :copier, :predicate,
2586 :type, :named, :initial-offset, :print-function, or :include.
2588 Each SLOT may instead take the form (SNAME SDEFAULT SOPTIONS...), where
2589 SDEFAULT is the default value of that slot and SOPTIONS are keyword-value
2590 pairs for that slot.
2591 Currently, only one keyword is supported, `:read-only'. If this has a
2592 non-nil value, that slot cannot be set via `setf'.
2594 \(fn NAME SLOTS...)"
2595 (declare (doc-string 2) (indent 1)
2596 (debug
2597 (&define ;Makes top-level form not be wrapped.
2598 [&or symbolp
2599 (gate
2600 symbolp &rest
2601 [&or symbolp
2602 (&or [":conc-name" symbolp]
2603 [":constructor" symbolp &optional cl-lambda-list]
2604 [":copier" symbolp]
2605 [":predicate" symbolp]
2606 [":include" symbolp &rest sexp] ;; Not finished.
2607 [":print-function" sexp]
2608 [":type" symbolp]
2609 [":named"]
2610 [":initial-offset" natnump])])]
2611 [&optional stringp]
2612 ;; All the above is for the following def-form.
2613 &rest &or symbolp (symbolp &optional def-form &rest sexp))))
2614 (let* ((name (if (consp struct) (car struct) struct))
2615 (opts (cdr-safe struct))
2616 (slots nil)
2617 (defaults nil)
2618 (conc-name (concat (symbol-name name) "-"))
2619 (constructor (intern (format "make-%s" name)))
2620 (constrs nil)
2621 (copier (intern (format "copy-%s" name)))
2622 (predicate (intern (format "%s-p" name)))
2623 (print-func nil) (print-auto nil)
2624 (safety (if (cl--compiling-file) cl--optimize-safety 3))
2625 (include nil)
2626 ;; There are 4 types of structs:
2627 ;; - `vector' type: means we should use a vector, which can come
2628 ;; with or without a tag `name', which is usually in slot 0
2629 ;; but obeys :initial-offset.
2630 ;; - `list' type: same as `vector' but using lists.
2631 ;; - `record' type: means we should use a record, which necessarily
2632 ;; comes tagged in slot 0. Currently we'll use the `name' as
2633 ;; the tag, but we may want to change it so that the class object
2634 ;; is used as the tag.
2635 ;; - nil type: this is the "pre-record default", which uses a vector
2636 ;; with a tag in slot 0 which is a symbol of the form
2637 ;; `cl-struct-NAME'. We need to still support this for backward
2638 ;; compatibility with old .elc files.
2639 (tag name)
2640 (tag-symbol (intern (format "cl-struct-%s-tags" name)))
2641 (include-descs nil)
2642 (include-name nil)
2643 (type nil) ;nil here means not specified explicitly.
2644 (named nil)
2645 (forms nil)
2646 (docstring (if (stringp (car descs)) (pop descs)))
2647 pred-form pred-check)
2648 (setq descs (cons '(cl-tag-slot)
2649 (mapcar (function (lambda (x) (if (consp x) x (list x))))
2650 descs)))
2651 (while opts
2652 (let ((opt (if (consp (car opts)) (caar opts) (car opts)))
2653 (args (cdr-safe (pop opts))))
2654 (cond ((eq opt :conc-name)
2655 (if args
2656 (setq conc-name (if (car args)
2657 (symbol-name (car args)) ""))))
2658 ((eq opt :constructor)
2659 (if (cdr args)
2660 (progn
2661 ;; If this defines a constructor of the same name as
2662 ;; the default one, don't define the default.
2663 (if (eq (car args) constructor)
2664 (setq constructor nil))
2665 (push args constrs))
2666 (if args (setq constructor (car args)))))
2667 ((eq opt :copier)
2668 (if args (setq copier (car args))))
2669 ((eq opt :predicate)
2670 (if args (setq predicate (car args))))
2671 ((eq opt :include)
2672 ;; FIXME: Actually, we can include more than once as long as
2673 ;; we include EIEIO classes rather than cl-structs!
2674 (when include-name (error "Can't :include more than once"))
2675 (setq include-name (car args))
2676 (setq include-descs (mapcar (function
2677 (lambda (x)
2678 (if (consp x) x (list x))))
2679 (cdr args))))
2680 ((eq opt :print-function)
2681 (setq print-func (car args)))
2682 ((eq opt :type)
2683 (setq type (car args))
2684 (unless (memq type '(vector list))
2685 (error "Invalid :type specifier: %s" type)))
2686 ((eq opt :named)
2687 (setq named t))
2688 ((eq opt :initial-offset)
2689 (setq descs (nconc (make-list (car args) '(cl-skip-slot))
2690 descs)))
2692 (error "Structure option %s unrecognized" opt)))))
2693 (unless (or include-name type)
2694 (setq include-name cl--struct-default-parent))
2695 (when include-name (setq include (cl--struct-get-class include-name)))
2696 (if print-func
2697 (setq print-func
2698 `(progn (funcall #',print-func cl-x cl-s cl-n) t))
2699 (or type (and include (not (cl--struct-class-print include)))
2700 (setq print-auto t
2701 print-func (and (or (not (or include type)) (null print-func))
2702 `(progn
2703 (princ ,(format "#S(%s" name) cl-s))))))
2704 (if include
2705 (let* ((inc-type (cl--struct-class-type include))
2706 (old-descs (cl-struct-slot-info include)))
2707 (and type (not (eq inc-type type))
2708 (error ":type disagrees with :include for %s" name))
2709 (while include-descs
2710 (setcar (memq (or (assq (caar include-descs) old-descs)
2711 (error "No slot %s in included struct %s"
2712 (caar include-descs) include))
2713 old-descs)
2714 (pop include-descs)))
2715 (setq descs (append old-descs (delq (assq 'cl-tag-slot descs) descs))
2716 type inc-type
2717 named (if (memq type '(vector list))
2718 (assq 'cl-tag-slot descs)
2719 'true))
2720 (if (cl--struct-class-named include) (setq named t)))
2721 (unless type
2722 (setq named 'true)))
2723 (or named (setq descs (delq (assq 'cl-tag-slot descs) descs)))
2724 (when (and (null predicate) named)
2725 (setq predicate (intern (format "cl--struct-%s-p" name))))
2726 (setq pred-form (and named
2727 (let ((pos (- (length descs)
2728 (length (memq (assq 'cl-tag-slot descs)
2729 descs)))))
2730 (cond
2731 ((null type) ;Record type.
2732 `(memq (type-of cl-x) ,tag-symbol))
2733 ((eq type 'vector)
2734 `(and (vectorp cl-x)
2735 (>= (length cl-x) ,(length descs))
2736 (memq (aref cl-x ,pos) ,tag-symbol)))
2737 ((= pos 0) `(memq (car-safe cl-x) ,tag-symbol))
2738 (t `(and (consp cl-x)
2739 (memq (nth ,pos cl-x) ,tag-symbol))))))
2740 pred-check (and pred-form (> safety 0)
2741 (if (and (eq (cl-caadr pred-form) 'vectorp)
2742 (= safety 1))
2743 (cons 'and (cl-cdddr pred-form))
2744 `(,predicate cl-x))))
2745 (when pred-form
2746 (push `(cl-defsubst ,predicate (cl-x)
2747 (declare (side-effect-free error-free))
2748 ,(if (eq (car pred-form) 'and)
2749 (append pred-form '(t))
2750 `(and ,pred-form t)))
2751 forms)
2752 (push `(put ',name 'cl-deftype-satisfies ',predicate) forms))
2753 (let ((pos 0) (descp descs))
2754 (while descp
2755 (let* ((desc (pop descp))
2756 (slot (pop desc)))
2757 (if (memq slot '(cl-tag-slot cl-skip-slot))
2758 (progn
2759 (push nil slots)
2760 (push (and (eq slot 'cl-tag-slot) `',tag)
2761 defaults))
2762 (if (assq slot descp)
2763 (error "Duplicate slots named %s in %s" slot name))
2764 (let ((accessor (intern (format "%s%s" conc-name slot))))
2765 (push slot slots)
2766 (push (pop desc) defaults)
2767 ;; The arg "cl-x" is referenced by name in eg pred-form
2768 ;; and pred-check, so changing it is not straightforward.
2769 (push `(cl-defsubst ,accessor (cl-x)
2770 ,(format "Access slot \"%s\" of `%s' struct CL-X."
2771 slot struct)
2772 (declare (side-effect-free t))
2773 ,@(and pred-check
2774 (list `(or ,pred-check
2775 (signal 'wrong-type-argument
2776 (list ',name cl-x)))))
2777 ,(if (memq type '(nil vector)) `(aref cl-x ,pos)
2778 (if (= pos 0) '(car cl-x)
2779 `(nth ,pos cl-x))))
2780 forms)
2781 (when (cl-oddp (length desc))
2782 (push
2783 (macroexp--warn-and-return
2784 (format "Missing value for option `%S' of slot `%s' in struct %s!"
2785 (car (last desc)) slot name)
2786 'nil)
2787 forms)
2788 (when (and (keywordp (car defaults))
2789 (not (keywordp (car desc))))
2790 (let ((kw (car defaults)))
2791 (push
2792 (macroexp--warn-and-return
2793 (format " I'll take `%s' to be an option rather than a default value."
2795 'nil)
2796 forms)
2797 (push kw desc)
2798 (setcar defaults nil))))
2799 (if (plist-get desc ':read-only)
2800 (push `(gv-define-expander ,accessor
2801 (lambda (_cl-do _cl-x)
2802 (error "%s is a read-only slot" ',accessor)))
2803 forms)
2804 ;; For normal slots, we don't need to define a setf-expander,
2805 ;; since gv-get can use the compiler macro to get the
2806 ;; same result.
2807 ;; (push `(gv-define-setter ,accessor (cl-val cl-x)
2808 ;; ;; If cl is loaded only for compilation,
2809 ;; ;; the call to cl--struct-setf-expander would
2810 ;; ;; cause a warning because it may not be
2811 ;; ;; defined at run time. Suppress that warning.
2812 ;; (progn
2813 ;; (declare-function
2814 ;; cl--struct-setf-expander "cl-macs"
2815 ;; (x name accessor pred-form pos))
2816 ;; (cl--struct-setf-expander
2817 ;; cl-val cl-x ',name ',accessor
2818 ;; ,(and pred-check `',pred-check)
2819 ;; ,pos)))
2820 ;; forms)
2822 (if print-auto
2823 (nconc print-func
2824 (list `(princ ,(format " %s" slot) cl-s)
2825 `(prin1 (,accessor cl-x) cl-s)))))))
2826 (setq pos (1+ pos))))
2827 (setq slots (nreverse slots)
2828 defaults (nreverse defaults))
2829 (and copier
2830 (push `(defalias ',copier #'copy-sequence)
2831 forms))
2832 (if constructor
2833 (push (list constructor
2834 (cons '&key (delq nil (copy-sequence slots))))
2835 constrs))
2836 (pcase-dolist (`(,cname ,args ,doc) constrs)
2837 (let* ((anames (cl--arglist-args args))
2838 (make (cl-mapcar (function (lambda (s d) (if (memq s anames) s d)))
2839 slots defaults)))
2840 (push `(cl-defsubst ,cname
2841 (&cl-defs (nil ,@descs) ,@args)
2842 ,(if (stringp doc) doc
2843 (format "Constructor for objects of type `%s'." name))
2844 ,@(if (cl--safe-expr-p `(progn ,@(mapcar #'cl-second descs)))
2845 '((declare (side-effect-free t))))
2846 (,(or type #'record) ,@make))
2847 forms)))
2848 (if print-auto (nconc print-func (list '(princ ")" cl-s) t)))
2849 ;; Don't bother adding to cl-custom-print-functions since it's not used
2850 ;; by anything anyway!
2851 ;;(if print-func
2852 ;; (push `(if (boundp 'cl-custom-print-functions)
2853 ;; (push
2854 ;; ;; The auto-generated function does not pay attention to
2855 ;; ;; the depth argument cl-n.
2856 ;; (lambda (cl-x cl-s ,(if print-auto '_cl-n 'cl-n))
2857 ;; (and ,pred-form ,print-func))
2858 ;; cl-custom-print-functions))
2859 ;; forms))
2860 `(progn
2861 (defvar ,tag-symbol)
2862 ,@(nreverse forms)
2863 ;; Call cl-struct-define during compilation as well, so that
2864 ;; a subsequent cl-defstruct in the same file can correctly include this
2865 ;; struct as a parent.
2866 (eval-and-compile
2867 (cl-struct-define ',name ,docstring ',include-name
2868 ',(or type 'record) ,(eq named t) ',descs
2869 ',tag-symbol ',tag ',print-auto))
2870 ',name)))
2872 ;;; Add cl-struct support to pcase
2874 (defun cl--struct-all-parents (class)
2875 (when (cl--struct-class-p class)
2876 (let ((res ())
2877 (classes (list class)))
2878 ;; BFS precedence.
2879 (while (let ((class (pop classes)))
2880 (push class res)
2881 (setq classes
2882 (append classes
2883 (cl--class-parents class)))))
2884 (nreverse res))))
2886 ;;;###autoload
2887 (pcase-defmacro cl-struct (type &rest fields)
2888 "Pcase patterns to match cl-structs.
2889 Elements of FIELDS can be of the form (NAME PAT) in which case the contents of
2890 field NAME is matched against PAT, or they can be of the form NAME which
2891 is a shorthand for (NAME NAME)."
2892 (declare (debug (sexp &rest [&or (sexp pcase-PAT) sexp])))
2893 `(and (pred (pcase--flip cl-typep ',type))
2894 ,@(mapcar
2895 (lambda (field)
2896 (let* ((name (if (consp field) (car field) field))
2897 (pat (if (consp field) (cadr field) field)))
2898 `(app ,(if (eq (cl-struct-sequence-type type) 'list)
2899 `(nth ,(cl-struct-slot-offset type name))
2900 `(pcase--flip aref ,(cl-struct-slot-offset type name)))
2901 ,pat)))
2902 fields)))
2904 (defun cl--defstruct-predicate (type)
2905 (let ((cons (assq (cl-struct-sequence-type type)
2906 `((list . consp)
2907 (vector . vectorp)
2908 (nil . recordp)))))
2909 (if cons
2910 (cdr cons)
2911 'recordp)))
2913 (defun cl--pcase-mutually-exclusive-p (orig pred1 pred2)
2914 "Extra special cases for `cl-typep' predicates."
2915 (let* ((x1 pred1) (x2 pred2)
2917 (and (eq 'pcase--flip (car-safe x1)) (setq x1 (cdr x1))
2918 (eq 'cl-typep (car-safe x1)) (setq x1 (cdr x1))
2919 (null (cdr-safe x1)) (setq x1 (car x1))
2920 (eq 'quote (car-safe x1)) (cadr x1)))
2922 (and (eq 'pcase--flip (car-safe x2)) (setq x2 (cdr x2))
2923 (eq 'cl-typep (car-safe x2)) (setq x2 (cdr x2))
2924 (null (cdr-safe x2)) (setq x2 (car x2))
2925 (eq 'quote (car-safe x2)) (cadr x2))))
2927 (and (symbolp t1) (symbolp t2)
2928 (let ((c1 (cl--find-class t1))
2929 (c2 (cl--find-class t2)))
2930 (and c1 c2
2931 (not (or (memq c1 (cl--struct-all-parents c2))
2932 (memq c2 (cl--struct-all-parents c1)))))))
2933 (let ((c1 (and (symbolp t1) (cl--find-class t1))))
2934 (and c1 (cl--struct-class-p c1)
2935 (funcall orig (cl--defstruct-predicate t1)
2936 pred2)))
2937 (let ((c2 (and (symbolp t2) (cl--find-class t2))))
2938 (and c2 (cl--struct-class-p c2)
2939 (funcall orig pred1
2940 (cl--defstruct-predicate t2))))
2941 (funcall orig pred1 pred2))))
2942 (advice-add 'pcase--mutually-exclusive-p
2943 :around #'cl--pcase-mutually-exclusive-p)
2946 (defun cl-struct-sequence-type (struct-type)
2947 "Return the sequence used to build STRUCT-TYPE.
2948 STRUCT-TYPE is a symbol naming a struct type. Return `record',
2949 `vector`, or `list' if STRUCT-TYPE is a struct type, nil otherwise."
2950 (declare (side-effect-free t) (pure t))
2951 (cl--struct-class-type (cl--struct-get-class struct-type)))
2953 (defun cl-struct-slot-info (struct-type)
2954 "Return a list of slot names of struct STRUCT-TYPE.
2955 Each entry is a list (SLOT-NAME . OPTS), where SLOT-NAME is a
2956 slot name symbol and OPTS is a list of slot options given to
2957 `cl-defstruct'. Dummy slots that represent the struct name and
2958 slots skipped by :initial-offset may appear in the list."
2959 (declare (side-effect-free t) (pure t))
2960 (let* ((class (cl--struct-get-class struct-type))
2961 (slots (cl--struct-class-slots class))
2962 (type (cl--struct-class-type class))
2963 (descs (if type () (list '(cl-tag-slot)))))
2964 (dotimes (i (length slots))
2965 (let ((slot (aref slots i)))
2966 (push `(,(cl--slot-descriptor-name slot)
2967 ,(cl--slot-descriptor-initform slot)
2968 ,@(if (not (eq (cl--slot-descriptor-type slot) t))
2969 `(:type ,(cl--slot-descriptor-type slot)))
2970 ,@(cl--slot-descriptor-props slot))
2971 descs)))
2972 (nreverse descs)))
2974 (define-error 'cl-struct-unknown-slot "struct %S has no slot %S")
2976 (defun cl-struct-slot-offset (struct-type slot-name)
2977 "Return the offset of slot SLOT-NAME in STRUCT-TYPE.
2978 The returned zero-based slot index is relative to the start of
2979 the structure data type and is adjusted for any structure name
2980 and :initial-offset slots. Signal error if struct STRUCT-TYPE
2981 does not contain SLOT-NAME."
2982 (declare (side-effect-free t) (pure t))
2983 (or (gethash slot-name
2984 (cl--class-index-table (cl--struct-get-class struct-type)))
2985 (signal 'cl-struct-unknown-slot (list struct-type slot-name))))
2987 (defvar byte-compile-function-environment)
2988 (defvar byte-compile-macro-environment)
2990 (defun cl--macroexp-fboundp (sym)
2991 "Return non-nil if SYM will be bound when we run the code.
2992 Of course, we really can't know that for sure, so it's just a heuristic."
2993 (or (fboundp sym)
2994 (and (cl--compiling-file)
2995 (or (cdr (assq sym byte-compile-function-environment))
2996 (cdr (assq sym byte-compile-macro-environment))))))
2998 (put 'null 'cl-deftype-satisfies #'null)
2999 (put 'atom 'cl-deftype-satisfies #'atom)
3000 (put 'real 'cl-deftype-satisfies #'numberp)
3001 (put 'fixnum 'cl-deftype-satisfies #'integerp)
3002 (put 'base-char 'cl-deftype-satisfies #'characterp)
3003 (put 'character 'cl-deftype-satisfies #'natnump)
3006 ;;;###autoload
3007 (define-inline cl-typep (val type)
3008 (inline-letevals (val)
3009 (pcase (inline-const-val type)
3010 ((and `(,name . ,args) (guard (get name 'cl-deftype-handler)))
3011 (inline-quote
3012 (cl-typep ,val ',(apply (get name 'cl-deftype-handler) args))))
3013 (`(,(and name (or 'integer 'float 'real 'number))
3014 . ,(or `(,min ,max) pcase--dontcare))
3015 (inline-quote
3016 (and (cl-typep ,val ',name)
3017 ,(if (memq min '(* nil)) t
3018 (if (consp min)
3019 (inline-quote (> ,val ',(car min)))
3020 (inline-quote (>= ,val ',min))))
3021 ,(if (memq max '(* nil)) t
3022 (if (consp max)
3023 (inline-quote (< ,val ',(car max)))
3024 (inline-quote (<= ,val ',max)))))))
3025 (`(not ,type) (inline-quote (not (cl-typep ,val ',type))))
3026 (`(,(and name (or 'and 'or)) . ,types)
3027 (cond
3028 ((null types) (inline-quote ',(eq name 'and)))
3029 ((null (cdr types))
3030 (inline-quote (cl-typep ,val ',(car types))))
3032 (let ((head (car types))
3033 (rest `(,name . ,(cdr types))))
3034 (cond
3035 ((eq name 'and)
3036 (inline-quote (and (cl-typep ,val ',head)
3037 (cl-typep ,val ',rest))))
3039 (inline-quote (or (cl-typep ,val ',head)
3040 (cl-typep ,val ',rest)))))))))
3041 (`(eql ,v) (inline-quote (and (eql ,val ',v) t)))
3042 (`(member . ,args) (inline-quote (and (memql ,val ',args) t)))
3043 (`(satisfies ,pred) (inline-quote (funcall #',pred ,val)))
3044 ((and (pred symbolp) type (guard (get type 'cl-deftype-handler)))
3045 (inline-quote
3046 (cl-typep ,val ',(funcall (get type 'cl-deftype-handler)))))
3047 ((and (pred symbolp) type (guard (get type 'cl-deftype-satisfies)))
3048 (inline-quote (funcall #',(get type 'cl-deftype-satisfies) ,val)))
3049 ((and (or 'nil 't) type) (inline-quote ',type))
3050 ((and (pred symbolp) type)
3051 (let* ((name (symbol-name type))
3052 (namep (intern (concat name "p"))))
3053 (cond
3054 ((cl--macroexp-fboundp namep) (inline-quote (funcall #',namep ,val)))
3055 ((cl--macroexp-fboundp
3056 (setq namep (intern (concat name "-p"))))
3057 (inline-quote (funcall #',namep ,val)))
3058 ((cl--macroexp-fboundp type) (inline-quote (funcall #',type ,val)))
3059 (t (error "Unknown type %S" type)))))
3060 (type (error "Bad type spec: %s" type)))))
3063 ;;;###autoload
3064 (defmacro cl-check-type (form type &optional string)
3065 "Verify that FORM is of type TYPE; signal an error if not.
3066 STRING is an optional description of the desired type."
3067 (declare (debug (place cl-type-spec &optional stringp)))
3068 (and (or (not (cl--compiling-file))
3069 (< cl--optimize-speed 3) (= cl--optimize-safety 3))
3070 (macroexp-let2 macroexp-copyable-p temp form
3071 `(progn (or (cl-typep ,temp ',type)
3072 (signal 'wrong-type-argument
3073 (list ,(or string `',type) ,temp ',form)))
3074 nil))))
3076 ;;;###autoload
3077 (defmacro cl-assert (form &optional show-args string &rest args)
3078 ;; FIXME: This is actually not compatible with Common-Lisp's `assert'.
3079 "Verify that FORM returns non-nil; signal an error if not.
3080 Second arg SHOW-ARGS means to include arguments of FORM in message.
3081 Other args STRING and ARGS... are arguments to be passed to `error'.
3082 They are not evaluated unless the assertion fails. If STRING is
3083 omitted, a default message listing FORM itself is used."
3084 (declare (debug (form &rest form)))
3085 (and (or (not (cl--compiling-file))
3086 (< cl--optimize-speed 3) (= cl--optimize-safety 3))
3087 (let ((sargs (and show-args
3088 (delq nil (mapcar (lambda (x)
3089 (unless (macroexp-const-p x)
3091 (cdr-safe form))))))
3092 `(progn
3093 (or ,form
3094 (cl--assertion-failed
3095 ',form ,@(if (or string sargs args)
3096 `(,string (list ,@sargs) (list ,@args)))))
3097 nil))))
3099 ;;; Compiler macros.
3101 ;;;###autoload
3102 (defmacro cl-define-compiler-macro (func args &rest body)
3103 "Define a compiler-only macro.
3104 This is like `defmacro', but macro expansion occurs only if the call to
3105 FUNC is compiled (i.e., not interpreted). Compiler macros should be used
3106 for optimizing the way calls to FUNC are compiled; the form returned by
3107 BODY should do the same thing as a call to the normal function called
3108 FUNC, though possibly more efficiently. Note that, like regular macros,
3109 compiler macros are expanded repeatedly until no further expansions are
3110 possible. Unlike regular macros, BODY can decide to \"punt\" and leave the
3111 original function call alone by declaring an initial `&whole foo' parameter
3112 and then returning foo."
3113 (declare (debug cl-defmacro) (indent 2))
3114 (let ((p args) (res nil))
3115 (while (consp p) (push (pop p) res))
3116 (setq args (nconc (nreverse res) (and p (list '&rest p)))))
3117 ;; FIXME: The code in bytecomp mishandles top-level expressions that define
3118 ;; uninterned functions. E.g. it would generate code like:
3119 ;; (defalias '#1=#:foo--cmacro #[514 ...])
3120 ;; (put 'foo 'compiler-macro '#:foo--cmacro)
3121 ;; So we circumvent this by using an interned name.
3122 (let ((fname (intern (concat (symbol-name func) "--cmacro"))))
3123 `(eval-and-compile
3124 ;; Name the compiler-macro function, so that `symbol-file' can find it.
3125 (cl-defun ,fname ,(if (memq '&whole args) (delq '&whole args)
3126 (cons '_cl-whole-arg args))
3127 ,@body)
3128 (put ',func 'compiler-macro #',fname))))
3130 ;;;###autoload
3131 (defun cl-compiler-macroexpand (form)
3132 "Like `macroexpand', but for compiler macros.
3133 Expands FORM repeatedly until no further expansion is possible.
3134 Returns FORM unchanged if it has no compiler macro, or if it has a
3135 macro that returns its `&whole' argument."
3136 (while
3137 (let ((func (car-safe form)) (handler nil))
3138 (while (and (symbolp func)
3139 (not (setq handler (get func 'compiler-macro)))
3140 (fboundp func)
3141 (or (not (autoloadp (symbol-function func)))
3142 (autoload-do-load (symbol-function func) func)))
3143 (setq func (symbol-function func)))
3144 (and handler
3145 (not (eq form (setq form (apply handler form (cdr form))))))))
3146 form)
3148 ;; Optimize away unused block-wrappers.
3150 (defvar cl--active-block-names nil)
3152 (cl-define-compiler-macro cl--block-wrapper (cl-form)
3153 (let* ((cl-entry (cons (nth 1 (nth 1 cl-form)) nil))
3154 (cl--active-block-names (cons cl-entry cl--active-block-names))
3155 (cl-body (macroexpand-all ;Performs compiler-macro expansions.
3156 (macroexp-progn (cddr cl-form))
3157 macroexpand-all-environment)))
3158 ;; FIXME: To avoid re-applying macroexpand-all, we'd like to be able
3159 ;; to indicate that this return value is already fully expanded.
3160 (if (cdr cl-entry)
3161 `(catch ,(nth 1 cl-form) ,@(macroexp-unprogn cl-body))
3162 cl-body)))
3164 (cl-define-compiler-macro cl--block-throw (cl-tag cl-value)
3165 (let ((cl-found (assq (nth 1 cl-tag) cl--active-block-names)))
3166 (if cl-found (setcdr cl-found t)))
3167 `(throw ,cl-tag ,cl-value))
3169 ;; Compile-time optimizations for some functions defined in this package.
3171 (defun cl--compiler-macro-member (form a list &rest keys)
3172 (let ((test (and (= (length keys) 2) (eq (car keys) :test)
3173 (cl--const-expr-val (nth 1 keys)))))
3174 (cond ((eq test 'eq) `(memq ,a ,list))
3175 ((eq test 'equal) `(member ,a ,list))
3176 ((or (null keys) (eq test 'eql)) `(memql ,a ,list))
3177 (t form))))
3179 (defun cl--compiler-macro-assoc (form a list &rest keys)
3180 (let ((test (and (= (length keys) 2) (eq (car keys) :test)
3181 (cl--const-expr-val (nth 1 keys)))))
3182 (cond ((eq test 'eq) `(assq ,a ,list))
3183 ((eq test 'equal) `(assoc ,a ,list))
3184 ((and (macroexp-const-p a) (or (null keys) (eq test 'eql)))
3185 (if (floatp (cl--const-expr-val a))
3186 `(assoc ,a ,list) `(assq ,a ,list)))
3187 (t form))))
3189 ;;;###autoload
3190 (defun cl--compiler-macro-adjoin (form a list &rest keys)
3191 (if (memq :key keys) form
3192 (macroexp-let2* macroexp-copyable-p ((va a) (vlist list))
3193 `(if (cl-member ,va ,vlist ,@keys) ,vlist (cons ,va ,vlist)))))
3195 (defun cl--compiler-macro-get (_form sym prop &optional def)
3196 (if def
3197 `(cl-getf (symbol-plist ,sym) ,prop ,def)
3198 `(get ,sym ,prop)))
3200 (dolist (y '(cl-first cl-second cl-third cl-fourth
3201 cl-fifth cl-sixth cl-seventh
3202 cl-eighth cl-ninth cl-tenth
3203 cl-rest cl-endp cl-plusp cl-minusp
3204 cl-caaar cl-caadr cl-cadar
3205 cl-caddr cl-cdaar cl-cdadr
3206 cl-cddar cl-cdddr cl-caaaar
3207 cl-caaadr cl-caadar cl-caaddr
3208 cl-cadaar cl-cadadr cl-caddar
3209 cl-cadddr cl-cdaaar cl-cdaadr
3210 cl-cdadar cl-cdaddr cl-cddaar
3211 cl-cddadr cl-cdddar cl-cddddr))
3212 (put y 'side-effect-free t))
3214 ;;; Things that are inline.
3215 (cl-proclaim '(inline cl-acons cl-map cl-concatenate cl-notany
3216 cl-notevery cl-revappend cl-nreconc gethash))
3218 ;;; Things that are side-effect-free.
3219 (mapc (lambda (x) (function-put x 'side-effect-free t))
3220 '(cl-oddp cl-evenp cl-signum last butlast cl-ldiff cl-pairlis cl-gcd
3221 cl-lcm cl-isqrt cl-floor cl-ceiling cl-truncate cl-round cl-mod cl-rem
3222 cl-subseq cl-list-length cl-get cl-getf))
3224 ;;; Things that are side-effect-and-error-free.
3225 (mapc (lambda (x) (function-put x 'side-effect-free 'error-free))
3226 '(eql cl-list* cl-subst cl-acons cl-equalp
3227 cl-random-state-p copy-tree cl-sublis))
3229 ;;; Types and assertions.
3231 ;;;###autoload
3232 (defmacro cl-deftype (name arglist &rest body)
3233 "Define NAME as a new data type.
3234 The type name can then be used in `cl-typecase', `cl-check-type', etc."
3235 (declare (debug cl-defmacro) (doc-string 3) (indent 2))
3236 `(cl-eval-when (compile load eval)
3237 (put ',name 'cl-deftype-handler
3238 (cl-function (lambda (&cl-defs ('*) ,@arglist) ,@body)))))
3240 (cl-deftype extended-char () `(and character (not base-char)))
3242 ;;; Additional functions that we can now define because we've defined
3243 ;;; `cl-defsubst' and `cl-typep'.
3245 (define-inline cl-struct-slot-value (struct-type slot-name inst)
3246 "Return the value of slot SLOT-NAME in INST of STRUCT-TYPE.
3247 STRUCT and SLOT-NAME are symbols. INST is a structure instance."
3248 (declare (side-effect-free t))
3249 (inline-letevals (struct-type slot-name inst)
3250 (inline-quote
3251 (progn
3252 (unless (cl-typep ,inst ,struct-type)
3253 (signal 'wrong-type-argument (list ,struct-type ,inst)))
3254 ;; We could use `elt', but since the byte compiler will resolve the
3255 ;; branch below at compile time, it's more efficient to use the
3256 ;; type-specific accessor.
3257 (if (eq (cl-struct-sequence-type ,struct-type) 'list)
3258 (nth (cl-struct-slot-offset ,struct-type ,slot-name) ,inst)
3259 (aref ,inst (cl-struct-slot-offset ,struct-type ,slot-name)))))))
3261 (run-hooks 'cl-macs-load-hook)
3263 ;; Local variables:
3264 ;; byte-compile-dynamic: t
3265 ;; generated-autoload-file: "cl-loaddefs.el"
3266 ;; End:
3268 (provide 'cl-macs)
3270 ;;; cl-macs.el ends here