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