* lisp/emacs-lisp/cl-macs.el (cl-do-all-symbols): Add doc string.
[emacs.git] / lisp / emacs-lisp / cl-macs.el
blobe7a66ad1b1ae07395818ad379bb1bb967fb38f09
1 ;;; cl-macs.el --- Common Lisp macros -*- lexical-binding: t; coding: utf-8 -*-
3 ;; Copyright (C) 1993, 2001-2012 Free Software Foundation, Inc.
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
6 ;; Version: 2.02
7 ;; Keywords: extensions
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; These are extensions to Emacs Lisp that provide a degree of
28 ;; Common Lisp compatibility, beyond what is already built-in
29 ;; in Emacs Lisp.
31 ;; This package was written by Dave Gillespie; it is a complete
32 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
34 ;; Bug reports, comments, and suggestions are welcome!
36 ;; This file contains the portions of the Common Lisp extensions
37 ;; package which should be autoloaded, but need only be present
38 ;; if the compiler or interpreter is used---this file is not
39 ;; necessary for executing compiled code.
41 ;; See cl.el for Change Log.
44 ;;; Code:
46 (require 'cl-lib)
47 (require 'macroexp)
48 ;; `gv' is required here because cl-macs can be loaded before loaddefs.el.
49 (require 'gv)
51 (defmacro cl-pop2 (place)
52 (declare (debug edebug-sexps))
53 `(prog1 (car (cdr ,place))
54 (setq ,place (cdr (cdr ,place)))))
56 (defvar cl-optimize-safety)
57 (defvar cl-optimize-speed)
59 ;;; Initialization.
61 ;; Place compiler macros at the beginning, otherwise uses of the corresponding
62 ;; functions can lead to recursive-loads that prevent the calls from
63 ;; being optimized.
65 ;;;###autoload
66 (defun cl--compiler-macro-list* (_form arg &rest others)
67 (let* ((args (reverse (cons arg others)))
68 (form (car args)))
69 (while (setq args (cdr args))
70 (setq form `(cons ,(car args) ,form)))
71 form))
73 ;;;###autoload
74 (defun cl--compiler-macro-cXXr (form x)
75 (let* ((head (car form))
76 (n (symbol-name (car form)))
77 (i (- (length n) 2)))
78 (if (not (string-match "c[ad]+r\\'" n))
79 (if (and (fboundp head) (symbolp (symbol-function head)))
80 (cl--compiler-macro-cXXr (cons (symbol-function head) (cdr form))
82 (error "Compiler macro for cXXr applied to non-cXXr form"))
83 (while (> i (match-beginning 0))
84 (setq x (list (if (eq (aref n i) ?a) 'car 'cdr) x))
85 (setq i (1- i)))
86 x)))
88 ;;; Some predicates for analyzing Lisp forms.
89 ;; These are used by various
90 ;; macro expanders to optimize the results in certain common cases.
92 (defconst cl--simple-funcs '(car cdr nth aref elt if and or + - 1+ 1- min max
93 car-safe cdr-safe progn prog1 prog2))
94 (defconst cl--safe-funcs '(* / % length memq list vector vectorp
95 < > <= >= = error))
97 (defun cl--simple-expr-p (x &optional size)
98 "Check if no side effects, and executes quickly."
99 (or size (setq size 10))
100 (if (and (consp x) (not (memq (car x) '(quote function cl-function))))
101 (and (symbolp (car x))
102 (or (memq (car x) cl--simple-funcs)
103 (get (car x) 'side-effect-free))
104 (progn
105 (setq size (1- size))
106 (while (and (setq x (cdr x))
107 (setq size (cl--simple-expr-p (car x) size))))
108 (and (null x) (>= size 0) size)))
109 (and (> size 0) (1- size))))
111 (defun cl--simple-exprs-p (xs)
112 (while (and xs (cl--simple-expr-p (car xs)))
113 (setq xs (cdr xs)))
114 (not xs))
116 (defun cl--safe-expr-p (x)
117 "Check if no side effects."
118 (or (not (and (consp x) (not (memq (car x) '(quote function cl-function)))))
119 (and (symbolp (car x))
120 (or (memq (car x) cl--simple-funcs)
121 (memq (car x) cl--safe-funcs)
122 (get (car x) 'side-effect-free))
123 (progn
124 (while (and (setq x (cdr x)) (cl--safe-expr-p (car x))))
125 (null x)))))
127 ;;; Check if constant (i.e., no side effects or dependencies).
128 (defun cl--const-expr-p (x)
129 (cond ((consp x)
130 (or (eq (car x) 'quote)
131 (and (memq (car x) '(function cl-function))
132 (or (symbolp (nth 1 x))
133 (and (eq (car-safe (nth 1 x)) 'lambda) 'func)))))
134 ((symbolp x) (and (memq x '(nil t)) t))
135 (t t)))
137 (defun cl--const-expr-val (x)
138 (and (macroexp-const-p x) (if (consp x) (nth 1 x) x)))
140 (defun cl--expr-contains (x y)
141 "Count number of times X refers to Y. Return nil for 0 times."
142 ;; FIXME: This is naive, and it will cl-count Y as referred twice in
143 ;; (let ((Y 1)) Y) even though it should be 0. Also it is often called on
144 ;; non-macroexpanded code, so it may also miss some occurrences that would
145 ;; only appear in the expanded code.
146 (cond ((equal y x) 1)
147 ((and (consp x) (not (memq (car x) '(quote function cl-function))))
148 (let ((sum 0))
149 (while (consp x)
150 (setq sum (+ sum (or (cl--expr-contains (pop x) y) 0))))
151 (setq sum (+ sum (or (cl--expr-contains x y) 0)))
152 (and (> sum 0) sum)))
153 (t nil)))
155 (defun cl--expr-contains-any (x y)
156 (while (and y (not (cl--expr-contains x (car y)))) (pop y))
159 (defun cl--expr-depends-p (x y)
160 "Check whether X may depend on any of the symbols in Y."
161 (and (not (macroexp-const-p x))
162 (or (not (cl--safe-expr-p x)) (cl--expr-contains-any x y))))
164 ;;; Symbols.
166 (defvar cl--gensym-counter)
167 ;;;###autoload
168 (defun cl-gensym (&optional prefix)
169 "Generate a new uninterned symbol.
170 The name is made by appending a number to PREFIX, default \"G\"."
171 (let ((pfix (if (stringp prefix) prefix "G"))
172 (num (if (integerp prefix) prefix
173 (prog1 cl--gensym-counter
174 (setq cl--gensym-counter (1+ cl--gensym-counter))))))
175 (make-symbol (format "%s%d" pfix num))))
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 \"G\"."
181 (let ((pfix (if (stringp prefix) prefix "G"))
182 name)
183 (while (intern-soft (setq name (format "%s%d" pfix cl--gensym-counter)))
184 (setq cl--gensym-counter (1+ cl--gensym-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 stringp cl-declarations))
196 (def-edebug-spec cl-lambda-list
197 (([&rest arg]
198 [&optional ["&optional" cl-&optional-arg &rest cl-&optional-arg]]
199 [&optional ["&rest" arg]]
200 [&optional ["&key" [cl-&key-arg &rest cl-&key-arg]
201 &optional "&allow-other-keys"]]
202 [&optional ["&aux" &rest
203 &or (symbolp &optional def-form) symbolp]]
206 (def-edebug-spec cl-&optional-arg
207 (&or (arg &optional def-form arg) arg))
209 (def-edebug-spec cl-&key-arg
210 (&or ([&or (symbolp arg) arg] &optional def-form arg) arg))
212 (defconst cl--lambda-list-keywords
213 '(&optional &rest &key &allow-other-keys &aux &whole &body &environment))
215 (defvar cl--bind-block) (defvar cl--bind-defs) (defvar cl--bind-enquote)
216 (defvar cl--bind-inits) (defvar cl--bind-lets) (defvar cl--bind-forms)
218 (defun cl--transform-lambda (form bind-block)
219 (let* ((args (car form)) (body (cdr form)) (orig-args args)
220 (cl--bind-block bind-block) (cl--bind-defs nil) (cl--bind-enquote nil)
221 (cl--bind-inits nil) (cl--bind-lets nil) (cl--bind-forms nil)
222 (header nil) (simple-args nil))
223 (while (or (stringp (car body))
224 (memq (car-safe (car body)) '(interactive cl-declare)))
225 (push (pop body) header))
226 (setq args (if (listp args) (cl-copy-list args) (list '&rest args)))
227 (let ((p (last args))) (if (cdr p) (setcdr p (list '&rest (cdr p)))))
228 (if (setq cl--bind-defs (cadr (memq '&cl-defs args)))
229 (setq args (delq '&cl-defs (delq cl--bind-defs args))
230 cl--bind-defs (cadr cl--bind-defs)))
231 (if (setq cl--bind-enquote (memq '&cl-quote args))
232 (setq args (delq '&cl-quote args)))
233 (if (memq '&whole args) (error "&whole not currently implemented"))
234 (let* ((p (memq '&environment args)) (v (cadr p))
235 (env-exp 'macroexpand-all-environment))
236 (if p (setq args (nconc (delq (car p) (delq v args))
237 (list '&aux (list v env-exp))))))
238 (while (and args (symbolp (car args))
239 (not (memq (car args) '(nil &rest &body &key &aux)))
240 (not (and (eq (car args) '&optional)
241 (or cl--bind-defs (consp (cadr args))))))
242 (push (pop args) simple-args))
243 (or (eq cl--bind-block 'cl-none)
244 (setq body (list `(cl-block ,cl--bind-block ,@body))))
245 (if (null args)
246 (cl-list* nil (nreverse simple-args) (nconc (nreverse header) body))
247 (if (memq '&optional simple-args) (push '&optional args))
248 (cl--do-arglist args nil (- (length simple-args)
249 (if (memq '&optional simple-args) 1 0)))
250 (setq cl--bind-lets (nreverse cl--bind-lets))
251 (cl-list* (and cl--bind-inits `(cl-eval-when (compile load eval)
252 ,@(nreverse cl--bind-inits)))
253 (nconc (nreverse simple-args)
254 (list '&rest (car (pop cl--bind-lets))))
255 (nconc (let ((hdr (nreverse header)))
256 ;; Macro expansion can take place in the middle of
257 ;; apparently harmless computation, so it should not
258 ;; touch the match-data.
259 (save-match-data
260 (require 'help-fns)
261 (cons (help-add-fundoc-usage
262 (if (stringp (car hdr)) (pop hdr))
263 (format "%S"
264 (cons 'fn
265 (cl--make-usage-args orig-args))))
266 hdr)))
267 (list `(let* ,cl--bind-lets
268 ,@(nreverse cl--bind-forms)
269 ,@body)))))))
271 ;;;###autoload
272 (defmacro cl-defun (name args &rest body)
273 "Define NAME as a function.
274 Like normal `defun', except ARGLIST allows full Common Lisp conventions,
275 and BODY is implicitly surrounded by (cl-block NAME ...).
277 \(fn NAME ARGLIST [DOCSTRING] BODY...)"
278 (declare (debug
279 ;; Same as defun but use cl-lambda-list.
280 (&define [&or name ("setf" :name setf name)]
281 cl-lambda-list
282 cl-declarations-or-string
283 [&optional ("interactive" interactive)]
284 def-body))
285 (doc-string 3)
286 (indent 2))
287 (let* ((res (cl--transform-lambda (cons args body) name))
288 (form `(defun ,name ,@(cdr res))))
289 (if (car res) `(progn ,(car res) ,form) form)))
291 ;; The lambda list for macros is different from that of normal lambdas.
292 ;; Note that &environment is only allowed as first or last items in the
293 ;; top level list.
295 (def-edebug-spec cl-macro-list
296 (([&optional "&environment" arg]
297 [&rest cl-macro-arg]
298 [&optional ["&optional" &rest
299 &or (cl-macro-arg &optional def-form cl-macro-arg) arg]]
300 [&optional [[&or "&rest" "&body"] cl-macro-arg]]
301 [&optional ["&key" [&rest
302 [&or ([&or (symbolp cl-macro-arg) arg]
303 &optional def-form cl-macro-arg)
304 arg]]
305 &optional "&allow-other-keys"]]
306 [&optional ["&aux" &rest
307 &or (symbolp &optional def-form) symbolp]]
308 [&optional "&environment" arg]
311 (def-edebug-spec cl-macro-arg
312 (&or arg cl-macro-list1))
314 (def-edebug-spec cl-macro-list1
315 (([&optional "&whole" arg] ;; only allowed at lower levels
316 [&rest cl-macro-arg]
317 [&optional ["&optional" &rest
318 &or (cl-macro-arg &optional def-form cl-macro-arg) arg]]
319 [&optional [[&or "&rest" "&body"] cl-macro-arg]]
320 [&optional ["&key" [&rest
321 [&or ([&or (symbolp cl-macro-arg) arg]
322 &optional def-form cl-macro-arg)
323 arg]]
324 &optional "&allow-other-keys"]]
325 [&optional ["&aux" &rest
326 &or (symbolp &optional def-form) symbolp]]
327 . [&or arg nil])))
329 ;;;###autoload
330 (defmacro cl-defmacro (name args &rest body)
331 "Define NAME as a macro.
332 Like normal `defmacro', except ARGLIST allows full Common Lisp conventions,
333 and BODY is implicitly surrounded by (cl-block NAME ...).
335 \(fn NAME ARGLIST [DOCSTRING] BODY...)"
336 (declare (debug
337 (&define name cl-macro-list cl-declarations-or-string def-body))
338 (doc-string 3)
339 (indent 2))
340 (let* ((res (cl--transform-lambda (cons args body) name))
341 (form `(defmacro ,name ,@(cdr res))))
342 (if (car res) `(progn ,(car res) ,form) form)))
344 (def-edebug-spec cl-lambda-expr
345 (&define ("lambda" cl-lambda-list
346 ;;cl-declarations-or-string
347 ;;[&optional ("interactive" interactive)]
348 def-body)))
350 ;; Redefine function-form to also match cl-function
351 (def-edebug-spec function-form
352 ;; form at the end could also handle "function",
353 ;; but recognize it specially to avoid wrapping function forms.
354 (&or ([&or "quote" "function"] &or symbolp lambda-expr)
355 ("cl-function" cl-function)
356 form))
358 ;;;###autoload
359 (defmacro cl-function (func)
360 "Introduce a function.
361 Like normal `function', except that if argument is a lambda form,
362 its argument list allows full Common Lisp conventions."
363 (declare (debug (&or symbolp cl-lambda-expr)))
364 (if (eq (car-safe func) 'lambda)
365 (let* ((res (cl--transform-lambda (cdr func) 'cl-none))
366 (form `(function (lambda . ,(cdr res)))))
367 (if (car res) `(progn ,(car res) ,form) form))
368 `(function ,func)))
370 (declare-function help-add-fundoc-usage "help-fns" (docstring arglist))
372 (defun cl--make-usage-var (x)
373 "X can be a var or a (destructuring) lambda-list."
374 (cond
375 ((symbolp x) (make-symbol (upcase (symbol-name x))))
376 ((consp x) (cl--make-usage-args x))
377 (t x)))
379 (defun cl--make-usage-args (arglist)
380 (if (cdr-safe (last arglist)) ;Not a proper list.
381 (let* ((last (last arglist))
382 (tail (cdr last)))
383 (unwind-protect
384 (progn
385 (setcdr last nil)
386 (nconc (cl--make-usage-args arglist) (cl--make-usage-var tail)))
387 (setcdr last tail)))
388 ;; `orig-args' can contain &cl-defs (an internal
389 ;; CL thingy I don't understand), so remove it.
390 (let ((x (memq '&cl-defs arglist)))
391 (when x (setq arglist (delq (car x) (remq (cadr x) arglist)))))
392 (let ((state nil))
393 (mapcar (lambda (x)
394 (cond
395 ((symbolp x)
396 (let ((first (aref (symbol-name x) 0)))
397 (if (eq ?\& first)
398 (setq state x)
399 ;; Strip a leading underscore, since it only
400 ;; means that this argument is unused.
401 (make-symbol (upcase (if (eq ?_ first)
402 (substring (symbol-name x) 1)
403 (symbol-name x)))))))
404 ((not (consp x)) x)
405 ((memq state '(nil &rest)) (cl--make-usage-args x))
406 (t ;(VAR INITFORM SVAR) or ((KEYWORD VAR) INITFORM SVAR).
407 (cl-list*
408 (if (and (consp (car x)) (eq state '&key))
409 (list (caar x) (cl--make-usage-var (nth 1 (car x))))
410 (cl--make-usage-var (car x)))
411 (nth 1 x) ;INITFORM.
412 (cl--make-usage-args (nthcdr 2 x)) ;SVAR.
413 ))))
414 arglist))))
416 (defun cl--do-arglist (args expr &optional num) ; uses bind-*
417 (if (nlistp args)
418 (if (or (memq args cl--lambda-list-keywords) (not (symbolp args)))
419 (error "Invalid argument name: %s" args)
420 (push (list args expr) cl--bind-lets))
421 (setq args (cl-copy-list args))
422 (let ((p (last args))) (if (cdr p) (setcdr p (list '&rest (cdr p)))))
423 (let ((p (memq '&body args))) (if p (setcar p '&rest)))
424 (if (memq '&environment args) (error "&environment used incorrectly"))
425 (let ((save-args args)
426 (restarg (memq '&rest args))
427 (safety (if (cl--compiling-file) cl-optimize-safety 3))
428 (keys nil)
429 (laterarg nil) (exactarg nil) minarg)
430 (or num (setq num 0))
431 (if (listp (cadr restarg))
432 (setq restarg (make-symbol "--cl-rest--"))
433 (setq restarg (cadr restarg)))
434 (push (list restarg expr) cl--bind-lets)
435 (if (eq (car args) '&whole)
436 (push (list (cl-pop2 args) restarg) cl--bind-lets))
437 (let ((p args))
438 (setq minarg restarg)
439 (while (and p (not (memq (car p) cl--lambda-list-keywords)))
440 (or (eq p args) (setq minarg (list 'cdr minarg)))
441 (setq p (cdr p)))
442 (if (memq (car p) '(nil &aux))
443 (setq minarg `(= (length ,restarg)
444 ,(length (cl-ldiff args p)))
445 exactarg (not (eq args p)))))
446 (while (and args (not (memq (car args) cl--lambda-list-keywords)))
447 (let ((poparg (list (if (or (cdr args) (not exactarg)) 'pop 'car)
448 restarg)))
449 (cl--do-arglist
450 (pop args)
451 (if (or laterarg (= safety 0)) poparg
452 `(if ,minarg ,poparg
453 (signal 'wrong-number-of-arguments
454 (list ,(and (not (eq cl--bind-block 'cl-none))
455 `',cl--bind-block)
456 (length ,restarg)))))))
457 (setq num (1+ num) laterarg t))
458 (while (and (eq (car args) '&optional) (pop args))
459 (while (and args (not (memq (car args) cl--lambda-list-keywords)))
460 (let ((arg (pop args)))
461 (or (consp arg) (setq arg (list arg)))
462 (if (cddr arg) (cl--do-arglist (nth 2 arg) `(and ,restarg t)))
463 (let ((def (if (cdr arg) (nth 1 arg)
464 (or (car cl--bind-defs)
465 (nth 1 (assq (car arg) cl--bind-defs)))))
466 (poparg `(pop ,restarg)))
467 (and def cl--bind-enquote (setq def `',def))
468 (cl--do-arglist (car arg)
469 (if def `(if ,restarg ,poparg ,def) poparg))
470 (setq num (1+ num))))))
471 (if (eq (car args) '&rest)
472 (let ((arg (cl-pop2 args)))
473 (if (consp arg) (cl--do-arglist arg restarg)))
474 (or (eq (car args) '&key) (= safety 0) exactarg
475 (push `(if ,restarg
476 (signal 'wrong-number-of-arguments
477 (list
478 ,(and (not (eq cl--bind-block 'cl-none))
479 `',cl--bind-block)
480 (+ ,num (length ,restarg)))))
481 cl--bind-forms)))
482 (while (and (eq (car args) '&key) (pop args))
483 (while (and args (not (memq (car args) cl--lambda-list-keywords)))
484 (let ((arg (pop args)))
485 (or (consp arg) (setq arg (list arg)))
486 (let* ((karg (if (consp (car arg)) (caar arg)
487 (let ((name (symbol-name (car arg))))
488 ;; Strip a leading underscore, since it only
489 ;; means that this argument is unused, but
490 ;; shouldn't affect the key's name (bug#12367).
491 (if (eq ?_ (aref name 0))
492 (setq name (substring name 1)))
493 (intern (format ":%s" name)))))
494 (varg (if (consp (car arg)) (cl-cadar arg) (car arg)))
495 (def (if (cdr arg) (cadr arg)
496 (or (car cl--bind-defs) (cadr (assq varg cl--bind-defs)))))
497 (look `(memq ',karg ,restarg)))
498 (and def cl--bind-enquote (setq def `',def))
499 (if (cddr arg)
500 (let* ((temp (or (nth 2 arg) (make-symbol "--cl-var--")))
501 (val `(car (cdr ,temp))))
502 (cl--do-arglist temp look)
503 (cl--do-arglist varg
504 `(if ,temp
505 (prog1 ,val (setq ,temp t))
506 ,def)))
507 (cl--do-arglist
508 varg
509 `(car (cdr ,(if (null def)
510 look
511 `(or ,look
512 ,(if (eq (cl--const-expr-p def) t)
513 `'(nil ,(cl--const-expr-val def))
514 `(list nil ,def))))))))
515 (push karg keys)))))
516 (setq keys (nreverse keys))
517 (or (and (eq (car args) '&allow-other-keys) (pop args))
518 (null keys) (= safety 0)
519 (let* ((var (make-symbol "--cl-keys--"))
520 (allow '(:allow-other-keys))
521 (check `(while ,var
522 (cond
523 ((memq (car ,var) ',(append keys allow))
524 (setq ,var (cdr (cdr ,var))))
525 ((car (cdr (memq (quote ,@allow) ,restarg)))
526 (setq ,var nil))
528 (error
529 ,(format "Keyword argument %%s not one of %s"
530 keys)
531 (car ,var)))))))
532 (push `(let ((,var ,restarg)) ,check) cl--bind-forms)))
533 (while (and (eq (car args) '&aux) (pop args))
534 (while (and args (not (memq (car args) cl--lambda-list-keywords)))
535 (if (consp (car args))
536 (if (and cl--bind-enquote (cl-cadar args))
537 (cl--do-arglist (caar args)
538 `',(cadr (pop args)))
539 (cl--do-arglist (caar args) (cadr (pop args))))
540 (cl--do-arglist (pop args) nil))))
541 (if args (error "Malformed argument list %s" save-args)))))
543 (defun cl--arglist-args (args)
544 (if (nlistp args) (list args)
545 (let ((res nil) (kind nil) arg)
546 (while (consp args)
547 (setq arg (pop args))
548 (if (memq arg cl--lambda-list-keywords) (setq kind arg)
549 (if (eq arg '&cl-defs) (pop args)
550 (and (consp arg) kind (setq arg (car arg)))
551 (and (consp arg) (cdr arg) (eq kind '&key) (setq arg (cadr arg)))
552 (setq res (nconc res (cl--arglist-args arg))))))
553 (nconc res (and args (list args))))))
555 ;;;###autoload
556 (defmacro cl-destructuring-bind (args expr &rest body)
557 (declare (indent 2)
558 (debug (&define cl-macro-list def-form cl-declarations def-body)))
559 (let* ((cl--bind-lets nil) (cl--bind-forms nil) (cl--bind-inits nil)
560 (cl--bind-defs nil) (cl--bind-block 'cl-none) (cl--bind-enquote nil))
561 (cl--do-arglist (or args '(&aux)) expr)
562 (append '(progn) cl--bind-inits
563 (list `(let* ,(nreverse cl--bind-lets)
564 ,@(nreverse cl--bind-forms) ,@body)))))
567 ;;; The `cl-eval-when' form.
569 (defvar cl-not-toplevel nil)
571 ;;;###autoload
572 (defmacro cl-eval-when (when &rest body)
573 "Control when BODY is evaluated.
574 If `compile' is in WHEN, BODY is evaluated when compiled at top-level.
575 If `load' is in WHEN, BODY is evaluated when loaded after top-level compile.
576 If `eval' is in WHEN, BODY is evaluated when interpreted or at non-top-level.
578 \(fn (WHEN...) BODY...)"
579 (declare (indent 1) (debug ((&rest &or "compile" "load" "eval") body)))
580 (if (and (fboundp 'cl--compiling-file) (cl--compiling-file)
581 (not cl-not-toplevel) (not (boundp 'for-effect))) ; horrible kludge
582 (let ((comp (or (memq 'compile when) (memq :compile-toplevel when)))
583 (cl-not-toplevel t))
584 (if (or (memq 'load when) (memq :load-toplevel when))
585 (if comp (cons 'progn (mapcar 'cl--compile-time-too body))
586 `(if nil nil ,@body))
587 (progn (if comp (eval (cons 'progn body))) nil)))
588 (and (or (memq 'eval when) (memq :execute when))
589 (cons 'progn body))))
591 (defun cl--compile-time-too (form)
592 (or (and (symbolp (car-safe form)) (get (car-safe form) 'byte-hunk-handler))
593 (setq form (macroexpand
594 form (cons '(cl-eval-when) byte-compile-macro-environment))))
595 (cond ((eq (car-safe form) 'progn)
596 (cons 'progn (mapcar 'cl--compile-time-too (cdr form))))
597 ((eq (car-safe form) 'cl-eval-when)
598 (let ((when (nth 1 form)))
599 (if (or (memq 'eval when) (memq :execute when))
600 `(cl-eval-when (compile ,@when) ,@(cddr form))
601 form)))
602 (t (eval form) form)))
604 ;;;###autoload
605 (defmacro cl-load-time-value (form &optional _read-only)
606 "Like `progn', but evaluates the body at load time.
607 The result of the body appears to the compiler as a quoted constant."
608 (declare (debug (form &optional sexp)))
609 (if (cl--compiling-file)
610 (let* ((temp (cl-gentemp "--cl-load-time--"))
611 (set `(set ',temp ,form)))
612 (if (and (fboundp 'byte-compile-file-form-defmumble)
613 (boundp 'this-kind) (boundp 'that-one))
614 (fset 'byte-compile-file-form
615 `(lambda (form)
616 (fset 'byte-compile-file-form
617 ',(symbol-function 'byte-compile-file-form))
618 (byte-compile-file-form ',set)
619 (byte-compile-file-form form)))
620 (print set (symbol-value 'byte-compile--outbuffer)))
621 `(symbol-value ',temp))
622 `',(eval form)))
625 ;;; Conditional control structures.
627 ;;;###autoload
628 (defmacro cl-case (expr &rest clauses)
629 "Eval EXPR and choose among clauses on that value.
630 Each clause looks like (KEYLIST BODY...). EXPR is evaluated and compared
631 against each key in each KEYLIST; the corresponding BODY is evaluated.
632 If no clause succeeds, cl-case returns nil. A single atom may be used in
633 place of a KEYLIST of one atom. A KEYLIST of t or `otherwise' is
634 allowed only in the final clause, and matches if no other keys match.
635 Key values are compared by `eql'.
636 \n(fn EXPR (KEYLIST BODY...)...)"
637 (declare (indent 1) (debug (form &rest (sexp body))))
638 (let* ((temp (if (cl--simple-expr-p expr 3) expr (make-symbol "--cl-var--")))
639 (head-list nil)
640 (body (cons
641 'cond
642 (mapcar
643 (function
644 (lambda (c)
645 (cons (cond ((memq (car c) '(t otherwise)) t)
646 ((eq (car c) 'cl--ecase-error-flag)
647 `(error "cl-ecase failed: %s, %s"
648 ,temp ',(reverse head-list)))
649 ((listp (car c))
650 (setq head-list (append (car c) head-list))
651 `(cl-member ,temp ',(car c)))
653 (if (memq (car c) head-list)
654 (error "Duplicate key in case: %s"
655 (car c)))
656 (push (car c) head-list)
657 `(eql ,temp ',(car c))))
658 (or (cdr c) '(nil)))))
659 clauses))))
660 (if (eq temp expr) body
661 `(let ((,temp ,expr)) ,body))))
663 ;;;###autoload
664 (defmacro cl-ecase (expr &rest clauses)
665 "Like `cl-case', but error if no case fits.
666 `otherwise'-clauses are not allowed.
667 \n(fn EXPR (KEYLIST BODY...)...)"
668 (declare (indent 1) (debug cl-case))
669 `(cl-case ,expr ,@clauses (cl--ecase-error-flag)))
671 ;;;###autoload
672 (defmacro cl-typecase (expr &rest clauses)
673 "Evals EXPR, chooses among clauses on that value.
674 Each clause looks like (TYPE BODY...). EXPR is evaluated and, if it
675 satisfies TYPE, the corresponding BODY is evaluated. If no clause succeeds,
676 cl-typecase returns nil. A TYPE of t or `otherwise' is allowed only in the
677 final clause, and matches if no other keys match.
678 \n(fn EXPR (TYPE BODY...)...)"
679 (declare (indent 1)
680 (debug (form &rest ([&or cl-type-spec "otherwise"] body))))
681 (let* ((temp (if (cl--simple-expr-p expr 3) expr (make-symbol "--cl-var--")))
682 (type-list nil)
683 (body (cons
684 'cond
685 (mapcar
686 (function
687 (lambda (c)
688 (cons (cond ((eq (car c) 'otherwise) t)
689 ((eq (car c) 'cl--ecase-error-flag)
690 `(error "cl-etypecase failed: %s, %s"
691 ,temp ',(reverse type-list)))
693 (push (car c) type-list)
694 (cl--make-type-test temp (car c))))
695 (or (cdr c) '(nil)))))
696 clauses))))
697 (if (eq temp expr) body
698 `(let ((,temp ,expr)) ,body))))
700 ;;;###autoload
701 (defmacro cl-etypecase (expr &rest clauses)
702 "Like `cl-typecase', but error if no case fits.
703 `otherwise'-clauses are not allowed.
704 \n(fn EXPR (TYPE BODY...)...)"
705 (declare (indent 1) (debug cl-typecase))
706 `(cl-typecase ,expr ,@clauses (cl--ecase-error-flag)))
709 ;;; Blocks and exits.
711 ;;;###autoload
712 (defmacro cl-block (name &rest body)
713 "Define a lexically-scoped block named NAME.
714 NAME may be any symbol. Code inside the BODY forms can call `cl-return-from'
715 to jump prematurely out of the block. This differs from `catch' and `throw'
716 in two respects: First, the NAME is an unevaluated symbol rather than a
717 quoted symbol or other form; and second, NAME is lexically rather than
718 dynamically scoped: Only references to it within BODY will work. These
719 references may appear inside macro expansions, but not inside functions
720 called from BODY."
721 (declare (indent 1) (debug (symbolp body)))
722 (if (cl--safe-expr-p `(progn ,@body)) `(progn ,@body)
723 `(cl--block-wrapper
724 (catch ',(intern (format "--cl-block-%s--" name))
725 ,@body))))
727 ;;;###autoload
728 (defmacro cl-return (&optional result)
729 "Return from the block named nil.
730 This is equivalent to `(cl-return-from nil RESULT)'."
731 (declare (debug (&optional form)))
732 `(cl-return-from nil ,result))
734 ;;;###autoload
735 (defmacro cl-return-from (name &optional result)
736 "Return from the block named NAME.
737 This jumps out to the innermost enclosing `(cl-block NAME ...)' form,
738 returning RESULT from that form (or nil if RESULT is omitted).
739 This is compatible with Common Lisp, but note that `defun' and
740 `defmacro' do not create implicit blocks as they do in Common Lisp."
741 (declare (indent 1) (debug (symbolp &optional form)))
742 (let ((name2 (intern (format "--cl-block-%s--" name))))
743 `(cl--block-throw ',name2 ,result)))
746 ;;; The "cl-loop" macro.
748 (defvar cl--loop-args) (defvar cl--loop-accum-var) (defvar cl--loop-accum-vars)
749 (defvar cl--loop-bindings) (defvar cl--loop-body) (defvar cl--loop-destr-temps)
750 (defvar cl--loop-finally) (defvar cl--loop-finish-flag)
751 (defvar cl--loop-first-flag)
752 (defvar cl--loop-initially) (defvar cl--loop-map-form) (defvar cl--loop-name)
753 (defvar cl--loop-result) (defvar cl--loop-result-explicit)
754 (defvar cl--loop-result-var) (defvar cl--loop-steps) (defvar cl--loop-symbol-macs)
756 ;;;###autoload
757 (defmacro cl-loop (&rest loop-args)
758 "The Common Lisp `cl-loop' macro.
759 Valid clauses are:
760 for VAR from/upfrom/downfrom NUM to/upto/downto/above/below NUM by NUM,
761 for VAR in LIST by FUNC, for VAR on LIST by FUNC, for VAR = INIT then EXPR,
762 for VAR across ARRAY, repeat NUM, with VAR = INIT, while COND, until COND,
763 always COND, never COND, thereis COND, collect EXPR into VAR,
764 append EXPR into VAR, nconc EXPR into VAR, sum EXPR into VAR,
765 count EXPR into VAR, maximize EXPR into VAR, minimize EXPR into VAR,
766 if COND CLAUSE [and CLAUSE]... else CLAUSE [and CLAUSE...],
767 unless COND CLAUSE [and CLAUSE]... else CLAUSE [and CLAUSE...],
768 do EXPRS..., initially EXPRS..., finally EXPRS..., return EXPR,
769 finally return EXPR, named NAME.
771 \(fn CLAUSE...)"
772 (declare (debug (&rest &or
773 ;; These are usually followed by a symbol, but it can
774 ;; actually be any destructuring-bind pattern, which
775 ;; would erroneously match `form'.
776 [[&or "for" "as" "with" "and"] sexp]
777 ;; These are followed by expressions which could
778 ;; erroneously match `symbolp'.
779 [[&or "from" "upfrom" "downfrom" "to" "upto" "downto"
780 "above" "below" "by" "in" "on" "=" "across"
781 "repeat" "while" "until" "always" "never"
782 "thereis" "collect" "append" "nconc" "sum"
783 "count" "maximize" "minimize" "if" "unless"
784 "return"] form]
785 ;; Simple default, which covers 99% of the cases.
786 symbolp form)))
787 (if (not (memq t (mapcar 'symbolp (delq nil (delq t (cl-copy-list loop-args))))))
788 `(cl-block nil (while t ,@loop-args))
789 (let ((cl--loop-args loop-args) (cl--loop-name nil) (cl--loop-bindings nil)
790 (cl--loop-body nil) (cl--loop-steps nil)
791 (cl--loop-result nil) (cl--loop-result-explicit nil)
792 (cl--loop-result-var nil) (cl--loop-finish-flag nil)
793 (cl--loop-accum-var nil) (cl--loop-accum-vars nil)
794 (cl--loop-initially nil) (cl--loop-finally nil)
795 (cl--loop-map-form nil) (cl--loop-first-flag nil)
796 (cl--loop-destr-temps nil) (cl--loop-symbol-macs nil))
797 (setq cl--loop-args (append cl--loop-args '(cl-end-loop)))
798 (while (not (eq (car cl--loop-args) 'cl-end-loop)) (cl-parse-loop-clause))
799 (if cl--loop-finish-flag
800 (push `((,cl--loop-finish-flag t)) cl--loop-bindings))
801 (if cl--loop-first-flag
802 (progn (push `((,cl--loop-first-flag t)) cl--loop-bindings)
803 (push `(setq ,cl--loop-first-flag nil) cl--loop-steps)))
804 (let* ((epilogue (nconc (nreverse cl--loop-finally)
805 (list (or cl--loop-result-explicit cl--loop-result))))
806 (ands (cl--loop-build-ands (nreverse cl--loop-body)))
807 (while-body (nconc (cadr ands) (nreverse cl--loop-steps)))
808 (body (append
809 (nreverse cl--loop-initially)
810 (list (if cl--loop-map-form
811 `(cl-block --cl-finish--
812 ,(cl-subst
813 (if (eq (car ands) t) while-body
814 (cons `(or ,(car ands)
815 (cl-return-from --cl-finish--
816 nil))
817 while-body))
818 '--cl-map cl--loop-map-form))
819 `(while ,(car ands) ,@while-body)))
820 (if cl--loop-finish-flag
821 (if (equal epilogue '(nil)) (list cl--loop-result-var)
822 `((if ,cl--loop-finish-flag
823 (progn ,@epilogue) ,cl--loop-result-var)))
824 epilogue))))
825 (if cl--loop-result-var (push (list cl--loop-result-var) cl--loop-bindings))
826 (while cl--loop-bindings
827 (if (cdar cl--loop-bindings)
828 (setq body (list (cl--loop-let (pop cl--loop-bindings) body t)))
829 (let ((lets nil))
830 (while (and cl--loop-bindings
831 (not (cdar cl--loop-bindings)))
832 (push (car (pop cl--loop-bindings)) lets))
833 (setq body (list (cl--loop-let lets body nil))))))
834 (if cl--loop-symbol-macs
835 (setq body (list `(cl-symbol-macrolet ,cl--loop-symbol-macs ,@body))))
836 `(cl-block ,cl--loop-name ,@body)))))
838 ;; Below is a complete spec for cl-loop, in several parts that correspond
839 ;; to the syntax given in CLtL2. The specs do more than specify where
840 ;; the forms are; it also specifies, as much as Edebug allows, all the
841 ;; syntactically valid cl-loop clauses. The disadvantage of this
842 ;; completeness is rigidity, but the "for ... being" clause allows
843 ;; arbitrary extensions of the form: [symbolp &rest &or symbolp form].
845 ;; (def-edebug-spec cl-loop
846 ;; ([&optional ["named" symbolp]]
847 ;; [&rest
848 ;; &or
849 ;; ["repeat" form]
850 ;; loop-for-as
851 ;; loop-with
852 ;; loop-initial-final]
853 ;; [&rest loop-clause]
854 ;; ))
856 ;; (def-edebug-spec loop-with
857 ;; ("with" loop-var
858 ;; loop-type-spec
859 ;; [&optional ["=" form]]
860 ;; &rest ["and" loop-var
861 ;; loop-type-spec
862 ;; [&optional ["=" form]]]))
864 ;; (def-edebug-spec loop-for-as
865 ;; ([&or "for" "as"] loop-for-as-subclause
866 ;; &rest ["and" loop-for-as-subclause]))
868 ;; (def-edebug-spec loop-for-as-subclause
869 ;; (loop-var
870 ;; loop-type-spec
871 ;; &or
872 ;; [[&or "in" "on" "in-ref" "across-ref"]
873 ;; form &optional ["by" function-form]]
875 ;; ["=" form &optional ["then" form]]
876 ;; ["across" form]
877 ;; ["being"
878 ;; [&or "the" "each"]
879 ;; &or
880 ;; [[&or "element" "elements"]
881 ;; [&or "of" "in" "of-ref"] form
882 ;; &optional "using" ["index" symbolp]];; is this right?
883 ;; [[&or "hash-key" "hash-keys"
884 ;; "hash-value" "hash-values"]
885 ;; [&or "of" "in"]
886 ;; hash-table-p &optional ["using" ([&or "hash-value" "hash-values"
887 ;; "hash-key" "hash-keys"] sexp)]]
889 ;; [[&or "symbol" "present-symbol" "external-symbol"
890 ;; "symbols" "present-symbols" "external-symbols"]
891 ;; [&or "in" "of"] package-p]
893 ;; ;; Extensions for Emacs Lisp, including Lucid Emacs.
894 ;; [[&or "frame" "frames"
895 ;; "screen" "screens"
896 ;; "buffer" "buffers"]]
898 ;; [[&or "window" "windows"]
899 ;; [&or "of" "in"] form]
901 ;; [[&or "overlay" "overlays"
902 ;; "extent" "extents"]
903 ;; [&or "of" "in"] form
904 ;; &optional [[&or "from" "to"] form]]
906 ;; [[&or "interval" "intervals"]
907 ;; [&or "in" "of"] form
908 ;; &optional [[&or "from" "to"] form]
909 ;; ["property" form]]
911 ;; [[&or "key-code" "key-codes"
912 ;; "key-seq" "key-seqs"
913 ;; "key-binding" "key-bindings"]
914 ;; [&or "in" "of"] form
915 ;; &optional ["using" ([&or "key-code" "key-codes"
916 ;; "key-seq" "key-seqs"
917 ;; "key-binding" "key-bindings"]
918 ;; sexp)]]
919 ;; ;; For arbitrary extensions, recognize anything else.
920 ;; [symbolp &rest &or symbolp form]
921 ;; ]
923 ;; ;; arithmetic - must be last since all parts are optional.
924 ;; [[&optional [[&or "from" "downfrom" "upfrom"] form]]
925 ;; [&optional [[&or "to" "downto" "upto" "below" "above"] form]]
926 ;; [&optional ["by" form]]
927 ;; ]))
929 ;; (def-edebug-spec loop-initial-final
930 ;; (&or ["initially"
931 ;; ;; [&optional &or "do" "doing"] ;; CLtL2 doesn't allow this.
932 ;; &rest loop-non-atomic-expr]
933 ;; ["finally" &or
934 ;; [[&optional &or "do" "doing"] &rest loop-non-atomic-expr]
935 ;; ["return" form]]))
937 ;; (def-edebug-spec loop-and-clause
938 ;; (loop-clause &rest ["and" loop-clause]))
940 ;; (def-edebug-spec loop-clause
941 ;; (&or
942 ;; [[&or "while" "until" "always" "never" "thereis"] form]
944 ;; [[&or "collect" "collecting"
945 ;; "append" "appending"
946 ;; "nconc" "nconcing"
947 ;; "concat" "vconcat"] form
948 ;; [&optional ["into" loop-var]]]
950 ;; [[&or "count" "counting"
951 ;; "sum" "summing"
952 ;; "maximize" "maximizing"
953 ;; "minimize" "minimizing"] form
954 ;; [&optional ["into" loop-var]]
955 ;; loop-type-spec]
957 ;; [[&or "if" "when" "unless"]
958 ;; form loop-and-clause
959 ;; [&optional ["else" loop-and-clause]]
960 ;; [&optional "end"]]
962 ;; [[&or "do" "doing"] &rest loop-non-atomic-expr]
964 ;; ["return" form]
965 ;; loop-initial-final
966 ;; ))
968 ;; (def-edebug-spec loop-non-atomic-expr
969 ;; ([&not atom] form))
971 ;; (def-edebug-spec loop-var
972 ;; ;; The symbolp must be last alternative to recognize e.g. (a b . c)
973 ;; ;; loop-var =>
974 ;; ;; (loop-var . [&or nil loop-var])
975 ;; ;; (symbolp . [&or nil loop-var])
976 ;; ;; (symbolp . loop-var)
977 ;; ;; (symbolp . (symbolp . [&or nil loop-var]))
978 ;; ;; (symbolp . (symbolp . loop-var))
979 ;; ;; (symbolp . (symbolp . symbolp)) == (symbolp symbolp . symbolp)
980 ;; (&or (loop-var . [&or nil loop-var]) [gate symbolp]))
982 ;; (def-edebug-spec loop-type-spec
983 ;; (&optional ["of-type" loop-d-type-spec]))
985 ;; (def-edebug-spec loop-d-type-spec
986 ;; (&or (loop-d-type-spec . [&or nil loop-d-type-spec]) cl-type-spec))
990 (defun cl-parse-loop-clause () ; uses loop-*
991 (let ((word (pop cl--loop-args))
992 (hash-types '(hash-key hash-keys hash-value hash-values))
993 (key-types '(key-code key-codes key-seq key-seqs
994 key-binding key-bindings)))
995 (cond
997 ((null cl--loop-args)
998 (error "Malformed `cl-loop' macro"))
1000 ((eq word 'named)
1001 (setq cl--loop-name (pop cl--loop-args)))
1003 ((eq word 'initially)
1004 (if (memq (car cl--loop-args) '(do doing)) (pop cl--loop-args))
1005 (or (consp (car cl--loop-args)) (error "Syntax error on `initially' clause"))
1006 (while (consp (car cl--loop-args))
1007 (push (pop cl--loop-args) cl--loop-initially)))
1009 ((eq word 'finally)
1010 (if (eq (car cl--loop-args) 'return)
1011 (setq cl--loop-result-explicit (or (cl-pop2 cl--loop-args) '(quote nil)))
1012 (if (memq (car cl--loop-args) '(do doing)) (pop cl--loop-args))
1013 (or (consp (car cl--loop-args)) (error "Syntax error on `finally' clause"))
1014 (if (and (eq (caar cl--loop-args) 'return) (null cl--loop-name))
1015 (setq cl--loop-result-explicit (or (nth 1 (pop cl--loop-args)) '(quote nil)))
1016 (while (consp (car cl--loop-args))
1017 (push (pop cl--loop-args) cl--loop-finally)))))
1019 ((memq word '(for as))
1020 (let ((loop-for-bindings nil) (loop-for-sets nil) (loop-for-steps nil)
1021 (ands nil))
1022 (while
1023 ;; Use `cl-gensym' rather than `make-symbol'. It's important that
1024 ;; (not (eq (symbol-name var1) (symbol-name var2))) because
1025 ;; these vars get added to the macro-environment.
1026 (let ((var (or (pop cl--loop-args) (cl-gensym "--cl-var--"))))
1027 (setq word (pop cl--loop-args))
1028 (if (eq word 'being) (setq word (pop cl--loop-args)))
1029 (if (memq word '(the each)) (setq word (pop cl--loop-args)))
1030 (if (memq word '(buffer buffers))
1031 (setq word 'in cl--loop-args (cons '(buffer-list) cl--loop-args)))
1032 (cond
1034 ((memq word '(from downfrom upfrom to downto upto
1035 above below by))
1036 (push word cl--loop-args)
1037 (if (memq (car cl--loop-args) '(downto above))
1038 (error "Must specify `from' value for downward cl-loop"))
1039 (let* ((down (or (eq (car cl--loop-args) 'downfrom)
1040 (memq (cl-caddr cl--loop-args) '(downto above))))
1041 (excl (or (memq (car cl--loop-args) '(above below))
1042 (memq (cl-caddr cl--loop-args) '(above below))))
1043 (start (and (memq (car cl--loop-args) '(from upfrom downfrom))
1044 (cl-pop2 cl--loop-args)))
1045 (end (and (memq (car cl--loop-args)
1046 '(to upto downto above below))
1047 (cl-pop2 cl--loop-args)))
1048 (step (and (eq (car cl--loop-args) 'by) (cl-pop2 cl--loop-args)))
1049 (end-var (and (not (macroexp-const-p end))
1050 (make-symbol "--cl-var--")))
1051 (step-var (and (not (macroexp-const-p step))
1052 (make-symbol "--cl-var--"))))
1053 (and step (numberp step) (<= step 0)
1054 (error "Loop `by' value is not positive: %s" step))
1055 (push (list var (or start 0)) loop-for-bindings)
1056 (if end-var (push (list end-var end) loop-for-bindings))
1057 (if step-var (push (list step-var step)
1058 loop-for-bindings))
1059 (if end
1060 (push (list
1061 (if down (if excl '> '>=) (if excl '< '<=))
1062 var (or end-var end)) cl--loop-body))
1063 (push (list var (list (if down '- '+) var
1064 (or step-var step 1)))
1065 loop-for-steps)))
1067 ((memq word '(in in-ref on))
1068 (let* ((on (eq word 'on))
1069 (temp (if (and on (symbolp var))
1070 var (make-symbol "--cl-var--"))))
1071 (push (list temp (pop cl--loop-args)) loop-for-bindings)
1072 (push `(consp ,temp) cl--loop-body)
1073 (if (eq word 'in-ref)
1074 (push (list var `(car ,temp)) cl--loop-symbol-macs)
1075 (or (eq temp var)
1076 (progn
1077 (push (list var nil) loop-for-bindings)
1078 (push (list var (if on temp `(car ,temp)))
1079 loop-for-sets))))
1080 (push (list temp
1081 (if (eq (car cl--loop-args) 'by)
1082 (let ((step (cl-pop2 cl--loop-args)))
1083 (if (and (memq (car-safe step)
1084 '(quote function
1085 cl-function))
1086 (symbolp (nth 1 step)))
1087 (list (nth 1 step) temp)
1088 `(funcall ,step ,temp)))
1089 `(cdr ,temp)))
1090 loop-for-steps)))
1092 ((eq word '=)
1093 (let* ((start (pop cl--loop-args))
1094 (then (if (eq (car cl--loop-args) 'then) (cl-pop2 cl--loop-args) start)))
1095 (push (list var nil) loop-for-bindings)
1096 (if (or ands (eq (car cl--loop-args) 'and))
1097 (progn
1098 (push `(,var
1099 (if ,(or cl--loop-first-flag
1100 (setq cl--loop-first-flag
1101 (make-symbol "--cl-var--")))
1102 ,start ,var))
1103 loop-for-sets)
1104 (push (list var then) loop-for-steps))
1105 (push (list var
1106 (if (eq start then) start
1107 `(if ,(or cl--loop-first-flag
1108 (setq cl--loop-first-flag
1109 (make-symbol "--cl-var--")))
1110 ,start ,then)))
1111 loop-for-sets))))
1113 ((memq word '(across across-ref))
1114 (let ((temp-vec (make-symbol "--cl-vec--"))
1115 (temp-idx (make-symbol "--cl-idx--")))
1116 (push (list temp-vec (pop cl--loop-args)) loop-for-bindings)
1117 (push (list temp-idx -1) loop-for-bindings)
1118 (push `(< (setq ,temp-idx (1+ ,temp-idx))
1119 (length ,temp-vec)) cl--loop-body)
1120 (if (eq word 'across-ref)
1121 (push (list var `(aref ,temp-vec ,temp-idx))
1122 cl--loop-symbol-macs)
1123 (push (list var nil) loop-for-bindings)
1124 (push (list var `(aref ,temp-vec ,temp-idx))
1125 loop-for-sets))))
1127 ((memq word '(element elements))
1128 (let ((ref (or (memq (car cl--loop-args) '(in-ref of-ref))
1129 (and (not (memq (car cl--loop-args) '(in of)))
1130 (error "Expected `of'"))))
1131 (seq (cl-pop2 cl--loop-args))
1132 (temp-seq (make-symbol "--cl-seq--"))
1133 (temp-idx (if (eq (car cl--loop-args) 'using)
1134 (if (and (= (length (cadr cl--loop-args)) 2)
1135 (eq (cl-caadr cl--loop-args) 'index))
1136 (cadr (cl-pop2 cl--loop-args))
1137 (error "Bad `using' clause"))
1138 (make-symbol "--cl-idx--"))))
1139 (push (list temp-seq seq) loop-for-bindings)
1140 (push (list temp-idx 0) loop-for-bindings)
1141 (if ref
1142 (let ((temp-len (make-symbol "--cl-len--")))
1143 (push (list temp-len `(length ,temp-seq))
1144 loop-for-bindings)
1145 (push (list var `(elt ,temp-seq ,temp-idx))
1146 cl--loop-symbol-macs)
1147 (push `(< ,temp-idx ,temp-len) cl--loop-body))
1148 (push (list var nil) loop-for-bindings)
1149 (push `(and ,temp-seq
1150 (or (consp ,temp-seq)
1151 (< ,temp-idx (length ,temp-seq))))
1152 cl--loop-body)
1153 (push (list var `(if (consp ,temp-seq)
1154 (pop ,temp-seq)
1155 (aref ,temp-seq ,temp-idx)))
1156 loop-for-sets))
1157 (push (list temp-idx `(1+ ,temp-idx))
1158 loop-for-steps)))
1160 ((memq word hash-types)
1161 (or (memq (car cl--loop-args) '(in of)) (error "Expected `of'"))
1162 (let* ((table (cl-pop2 cl--loop-args))
1163 (other (if (eq (car cl--loop-args) 'using)
1164 (if (and (= (length (cadr cl--loop-args)) 2)
1165 (memq (cl-caadr cl--loop-args) hash-types)
1166 (not (eq (cl-caadr cl--loop-args) word)))
1167 (cadr (cl-pop2 cl--loop-args))
1168 (error "Bad `using' clause"))
1169 (make-symbol "--cl-var--"))))
1170 (if (memq word '(hash-value hash-values))
1171 (setq var (prog1 other (setq other var))))
1172 (setq cl--loop-map-form
1173 `(maphash (lambda (,var ,other) . --cl-map) ,table))))
1175 ((memq word '(symbol present-symbol external-symbol
1176 symbols present-symbols external-symbols))
1177 (let ((ob (and (memq (car cl--loop-args) '(in of)) (cl-pop2 cl--loop-args))))
1178 (setq cl--loop-map-form
1179 `(mapatoms (lambda (,var) . --cl-map) ,ob))))
1181 ((memq word '(overlay overlays extent extents))
1182 (let ((buf nil) (from nil) (to nil))
1183 (while (memq (car cl--loop-args) '(in of from to))
1184 (cond ((eq (car cl--loop-args) 'from) (setq from (cl-pop2 cl--loop-args)))
1185 ((eq (car cl--loop-args) 'to) (setq to (cl-pop2 cl--loop-args)))
1186 (t (setq buf (cl-pop2 cl--loop-args)))))
1187 (setq cl--loop-map-form
1188 `(cl--map-overlays
1189 (lambda (,var ,(make-symbol "--cl-var--"))
1190 (progn . --cl-map) nil)
1191 ,buf ,from ,to))))
1193 ((memq word '(interval intervals))
1194 (let ((buf nil) (prop nil) (from nil) (to nil)
1195 (var1 (make-symbol "--cl-var1--"))
1196 (var2 (make-symbol "--cl-var2--")))
1197 (while (memq (car cl--loop-args) '(in of property from to))
1198 (cond ((eq (car cl--loop-args) 'from) (setq from (cl-pop2 cl--loop-args)))
1199 ((eq (car cl--loop-args) 'to) (setq to (cl-pop2 cl--loop-args)))
1200 ((eq (car cl--loop-args) 'property)
1201 (setq prop (cl-pop2 cl--loop-args)))
1202 (t (setq buf (cl-pop2 cl--loop-args)))))
1203 (if (and (consp var) (symbolp (car var)) (symbolp (cdr var)))
1204 (setq var1 (car var) var2 (cdr var))
1205 (push (list var `(cons ,var1 ,var2)) loop-for-sets))
1206 (setq cl--loop-map-form
1207 `(cl--map-intervals
1208 (lambda (,var1 ,var2) . --cl-map)
1209 ,buf ,prop ,from ,to))))
1211 ((memq word key-types)
1212 (or (memq (car cl--loop-args) '(in of)) (error "Expected `of'"))
1213 (let ((cl-map (cl-pop2 cl--loop-args))
1214 (other (if (eq (car cl--loop-args) 'using)
1215 (if (and (= (length (cadr cl--loop-args)) 2)
1216 (memq (cl-caadr cl--loop-args) key-types)
1217 (not (eq (cl-caadr cl--loop-args) word)))
1218 (cadr (cl-pop2 cl--loop-args))
1219 (error "Bad `using' clause"))
1220 (make-symbol "--cl-var--"))))
1221 (if (memq word '(key-binding key-bindings))
1222 (setq var (prog1 other (setq other var))))
1223 (setq cl--loop-map-form
1224 `(,(if (memq word '(key-seq key-seqs))
1225 'cl--map-keymap-recursively 'map-keymap)
1226 (lambda (,var ,other) . --cl-map) ,cl-map))))
1228 ((memq word '(frame frames screen screens))
1229 (let ((temp (make-symbol "--cl-var--")))
1230 (push (list var '(selected-frame))
1231 loop-for-bindings)
1232 (push (list temp nil) loop-for-bindings)
1233 (push `(prog1 (not (eq ,var ,temp))
1234 (or ,temp (setq ,temp ,var)))
1235 cl--loop-body)
1236 (push (list var `(next-frame ,var))
1237 loop-for-steps)))
1239 ((memq word '(window windows))
1240 (let ((scr (and (memq (car cl--loop-args) '(in of)) (cl-pop2 cl--loop-args)))
1241 (temp (make-symbol "--cl-var--"))
1242 (minip (make-symbol "--cl-minip--")))
1243 (push (list var (if scr
1244 `(frame-selected-window ,scr)
1245 '(selected-window)))
1246 loop-for-bindings)
1247 ;; If we started in the minibuffer, we need to
1248 ;; ensure that next-window will bring us back there
1249 ;; at some point. (Bug#7492).
1250 ;; (Consider using walk-windows instead of cl-loop if
1251 ;; you care about such things.)
1252 (push (list minip `(minibufferp (window-buffer ,var)))
1253 loop-for-bindings)
1254 (push (list temp nil) loop-for-bindings)
1255 (push `(prog1 (not (eq ,var ,temp))
1256 (or ,temp (setq ,temp ,var)))
1257 cl--loop-body)
1258 (push (list var `(next-window ,var ,minip))
1259 loop-for-steps)))
1262 (let ((handler (and (symbolp word)
1263 (get word 'cl--loop-for-handler))))
1264 (if handler
1265 (funcall handler var)
1266 (error "Expected a `for' preposition, found %s" word)))))
1267 (eq (car cl--loop-args) 'and))
1268 (setq ands t)
1269 (pop cl--loop-args))
1270 (if (and ands loop-for-bindings)
1271 (push (nreverse loop-for-bindings) cl--loop-bindings)
1272 (setq cl--loop-bindings (nconc (mapcar 'list loop-for-bindings)
1273 cl--loop-bindings)))
1274 (if loop-for-sets
1275 (push `(progn
1276 ,(cl--loop-let (nreverse loop-for-sets) 'setq ands)
1277 t) cl--loop-body))
1278 (if loop-for-steps
1279 (push (cons (if ands 'cl-psetq 'setq)
1280 (apply 'append (nreverse loop-for-steps)))
1281 cl--loop-steps))))
1283 ((eq word 'repeat)
1284 (let ((temp (make-symbol "--cl-var--")))
1285 (push (list (list temp (pop cl--loop-args))) cl--loop-bindings)
1286 (push `(>= (setq ,temp (1- ,temp)) 0) cl--loop-body)))
1288 ((memq word '(collect collecting))
1289 (let ((what (pop cl--loop-args))
1290 (var (cl--loop-handle-accum nil 'nreverse)))
1291 (if (eq var cl--loop-accum-var)
1292 (push `(progn (push ,what ,var) t) cl--loop-body)
1293 (push `(progn
1294 (setq ,var (nconc ,var (list ,what)))
1295 t) cl--loop-body))))
1297 ((memq word '(nconc nconcing append appending))
1298 (let ((what (pop cl--loop-args))
1299 (var (cl--loop-handle-accum nil 'nreverse)))
1300 (push `(progn
1301 (setq ,var
1302 ,(if (eq var cl--loop-accum-var)
1303 `(nconc
1304 (,(if (memq word '(nconc nconcing))
1305 #'nreverse #'reverse)
1306 ,what)
1307 ,var)
1308 `(,(if (memq word '(nconc nconcing))
1309 #'nconc #'append)
1310 ,var ,what))) t) cl--loop-body)))
1312 ((memq word '(concat concating))
1313 (let ((what (pop cl--loop-args))
1314 (var (cl--loop-handle-accum "")))
1315 (push `(progn (cl-callf concat ,var ,what) t) cl--loop-body)))
1317 ((memq word '(vconcat vconcating))
1318 (let ((what (pop cl--loop-args))
1319 (var (cl--loop-handle-accum [])))
1320 (push `(progn (cl-callf vconcat ,var ,what) t) cl--loop-body)))
1322 ((memq word '(sum summing))
1323 (let ((what (pop cl--loop-args))
1324 (var (cl--loop-handle-accum 0)))
1325 (push `(progn (cl-incf ,var ,what) t) cl--loop-body)))
1327 ((memq word '(count counting))
1328 (let ((what (pop cl--loop-args))
1329 (var (cl--loop-handle-accum 0)))
1330 (push `(progn (if ,what (cl-incf ,var)) t) cl--loop-body)))
1332 ((memq word '(minimize minimizing maximize maximizing))
1333 (let* ((what (pop cl--loop-args))
1334 (temp (if (cl--simple-expr-p what) what (make-symbol "--cl-var--")))
1335 (var (cl--loop-handle-accum nil))
1336 (func (intern (substring (symbol-name word) 0 3)))
1337 (set `(setq ,var (if ,var (,func ,var ,temp) ,temp))))
1338 (push `(progn ,(if (eq temp what) set
1339 `(let ((,temp ,what)) ,set))
1340 t) cl--loop-body)))
1342 ((eq word 'with)
1343 (let ((bindings nil))
1344 (while (progn (push (list (pop cl--loop-args)
1345 (and (eq (car cl--loop-args) '=) (cl-pop2 cl--loop-args)))
1346 bindings)
1347 (eq (car cl--loop-args) 'and))
1348 (pop cl--loop-args))
1349 (push (nreverse bindings) cl--loop-bindings)))
1351 ((eq word 'while)
1352 (push (pop cl--loop-args) cl--loop-body))
1354 ((eq word 'until)
1355 (push `(not ,(pop cl--loop-args)) cl--loop-body))
1357 ((eq word 'always)
1358 (or cl--loop-finish-flag (setq cl--loop-finish-flag (make-symbol "--cl-flag--")))
1359 (push `(setq ,cl--loop-finish-flag ,(pop cl--loop-args)) cl--loop-body)
1360 (setq cl--loop-result t))
1362 ((eq word 'never)
1363 (or cl--loop-finish-flag (setq cl--loop-finish-flag (make-symbol "--cl-flag--")))
1364 (push `(setq ,cl--loop-finish-flag (not ,(pop cl--loop-args)))
1365 cl--loop-body)
1366 (setq cl--loop-result t))
1368 ((eq word 'thereis)
1369 (or cl--loop-finish-flag (setq cl--loop-finish-flag (make-symbol "--cl-flag--")))
1370 (or cl--loop-result-var (setq cl--loop-result-var (make-symbol "--cl-var--")))
1371 (push `(setq ,cl--loop-finish-flag
1372 (not (setq ,cl--loop-result-var ,(pop cl--loop-args))))
1373 cl--loop-body))
1375 ((memq word '(if when unless))
1376 (let* ((cond (pop cl--loop-args))
1377 (then (let ((cl--loop-body nil))
1378 (cl-parse-loop-clause)
1379 (cl--loop-build-ands (nreverse cl--loop-body))))
1380 (else (let ((cl--loop-body nil))
1381 (if (eq (car cl--loop-args) 'else)
1382 (progn (pop cl--loop-args) (cl-parse-loop-clause)))
1383 (cl--loop-build-ands (nreverse cl--loop-body))))
1384 (simple (and (eq (car then) t) (eq (car else) t))))
1385 (if (eq (car cl--loop-args) 'end) (pop cl--loop-args))
1386 (if (eq word 'unless) (setq then (prog1 else (setq else then))))
1387 (let ((form (cons (if simple (cons 'progn (nth 1 then)) (nth 2 then))
1388 (if simple (nth 1 else) (list (nth 2 else))))))
1389 (if (cl--expr-contains form 'it)
1390 (let ((temp (make-symbol "--cl-var--")))
1391 (push (list temp) cl--loop-bindings)
1392 (setq form `(if (setq ,temp ,cond)
1393 ,@(cl-subst temp 'it form))))
1394 (setq form `(if ,cond ,@form)))
1395 (push (if simple `(progn ,form t) form) cl--loop-body))))
1397 ((memq word '(do doing))
1398 (let ((body nil))
1399 (or (consp (car cl--loop-args)) (error "Syntax error on `do' clause"))
1400 (while (consp (car cl--loop-args)) (push (pop cl--loop-args) body))
1401 (push (cons 'progn (nreverse (cons t body))) cl--loop-body)))
1403 ((eq word 'return)
1404 (or cl--loop-finish-flag (setq cl--loop-finish-flag (make-symbol "--cl-var--")))
1405 (or cl--loop-result-var (setq cl--loop-result-var (make-symbol "--cl-var--")))
1406 (push `(setq ,cl--loop-result-var ,(pop cl--loop-args)
1407 ,cl--loop-finish-flag nil) cl--loop-body))
1410 (let ((handler (and (symbolp word) (get word 'cl--loop-handler))))
1411 (or handler (error "Expected a cl-loop keyword, found %s" word))
1412 (funcall handler))))
1413 (if (eq (car cl--loop-args) 'and)
1414 (progn (pop cl--loop-args) (cl-parse-loop-clause)))))
1416 (defun cl--loop-let (specs body par) ; uses loop-*
1417 (let ((p specs) (temps nil) (new nil))
1418 (while (and p (or (symbolp (car-safe (car p))) (null (cl-cadar p))))
1419 (setq p (cdr p)))
1420 (and par p
1421 (progn
1422 (setq par nil p specs)
1423 (while p
1424 (or (macroexp-const-p (cl-cadar p))
1425 (let ((temp (make-symbol "--cl-var--")))
1426 (push (list temp (cl-cadar p)) temps)
1427 (setcar (cdar p) temp)))
1428 (setq p (cdr p)))))
1429 (while specs
1430 (if (and (consp (car specs)) (listp (caar specs)))
1431 (let* ((spec (caar specs)) (nspecs nil)
1432 (expr (cadr (pop specs)))
1433 (temp (cdr (or (assq spec cl--loop-destr-temps)
1434 (car (push (cons spec (or (last spec 0)
1435 (make-symbol "--cl-var--")))
1436 cl--loop-destr-temps))))))
1437 (push (list temp expr) new)
1438 (while (consp spec)
1439 (push (list (pop spec)
1440 (and expr (list (if spec 'pop 'car) temp)))
1441 nspecs))
1442 (setq specs (nconc (nreverse nspecs) specs)))
1443 (push (pop specs) new)))
1444 (if (eq body 'setq)
1445 (let ((set (cons (if par 'cl-psetq 'setq) (apply 'nconc (nreverse new)))))
1446 (if temps `(let* ,(nreverse temps) ,set) set))
1447 `(,(if par 'let 'let*)
1448 ,(nconc (nreverse temps) (nreverse new)) ,@body))))
1450 (defun cl--loop-handle-accum (def &optional func) ; uses loop-*
1451 (if (eq (car cl--loop-args) 'into)
1452 (let ((var (cl-pop2 cl--loop-args)))
1453 (or (memq var cl--loop-accum-vars)
1454 (progn (push (list (list var def)) cl--loop-bindings)
1455 (push var cl--loop-accum-vars)))
1456 var)
1457 (or cl--loop-accum-var
1458 (progn
1459 (push (list (list (setq cl--loop-accum-var (make-symbol "--cl-var--")) def))
1460 cl--loop-bindings)
1461 (setq cl--loop-result (if func (list func cl--loop-accum-var)
1462 cl--loop-accum-var))
1463 cl--loop-accum-var))))
1465 (defun cl--loop-build-ands (clauses)
1466 "Return various representations of (and . CLAUSES).
1467 CLAUSES is a list of Elisp expressions, where clauses of the form
1468 \(progn E1 E2 E3 .. t) are the focus of particular optimizations.
1469 The return value has shape (COND BODY COMBO)
1470 such that COMBO is equivalent to (and . CLAUSES)."
1471 (let ((ands nil)
1472 (body nil))
1473 ;; Look through `clauses', trying to optimize (progn ,@A t) (progn ,@B) ,@C
1474 ;; into (progn ,@A ,@B) ,@C.
1475 (while clauses
1476 (if (and (eq (car-safe (car clauses)) 'progn)
1477 (eq (car (last (car clauses))) t))
1478 (if (cdr clauses)
1479 (setq clauses (cons (nconc (butlast (car clauses))
1480 (if (eq (car-safe (cadr clauses))
1481 'progn)
1482 (cl-cdadr clauses)
1483 (list (cadr clauses))))
1484 (cddr clauses)))
1485 ;; A final (progn ,@A t) is moved outside of the `and'.
1486 (setq body (cdr (butlast (pop clauses)))))
1487 (push (pop clauses) ands)))
1488 (setq ands (or (nreverse ands) (list t)))
1489 (list (if (cdr ands) (cons 'and ands) (car ands))
1490 body
1491 (let ((full (if body
1492 (append ands (list (cons 'progn (append body '(t)))))
1493 ands)))
1494 (if (cdr full) (cons 'and full) (car full))))))
1497 ;;; Other iteration control structures.
1499 ;;;###autoload
1500 (defmacro cl-do (steps endtest &rest body)
1501 "The Common Lisp `cl-do' loop.
1503 \(fn ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)"
1504 (declare (indent 2)
1505 (debug
1506 ((&rest &or symbolp (symbolp &optional form form))
1507 (form body)
1508 cl-declarations body)))
1509 (cl-expand-do-loop steps endtest body nil))
1511 ;;;###autoload
1512 (defmacro cl-do* (steps endtest &rest body)
1513 "The Common Lisp `cl-do*' loop.
1515 \(fn ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)"
1516 (declare (indent 2) (debug cl-do))
1517 (cl-expand-do-loop steps endtest body t))
1519 (defun cl-expand-do-loop (steps endtest body star)
1520 `(cl-block nil
1521 (,(if star 'let* 'let)
1522 ,(mapcar (lambda (c) (if (consp c) (list (car c) (nth 1 c)) c))
1523 steps)
1524 (while (not ,(car endtest))
1525 ,@body
1526 ,@(let ((sets (mapcar (lambda (c)
1527 (and (consp c) (cdr (cdr c))
1528 (list (car c) (nth 2 c))))
1529 steps)))
1530 (setq sets (delq nil sets))
1531 (and sets
1532 (list (cons (if (or star (not (cdr sets)))
1533 'setq 'cl-psetq)
1534 (apply 'append sets))))))
1535 ,@(or (cdr endtest) '(nil)))))
1537 ;;;###autoload
1538 (defmacro cl-dolist (spec &rest body)
1539 "Loop over a list.
1540 Evaluate BODY with VAR bound to each `car' from LIST, in turn.
1541 Then evaluate RESULT to get return value, default nil.
1542 An implicit nil block is established around the loop.
1544 \(fn (VAR LIST [RESULT]) BODY...)"
1545 (declare (debug ((symbolp form &optional form) cl-declarations body))
1546 (indent 1))
1547 `(cl-block nil
1548 (,(if (eq 'cl-dolist (symbol-function 'dolist)) 'cl--dolist 'dolist)
1549 ,spec ,@body)))
1551 ;;;###autoload
1552 (defmacro cl-dotimes (spec &rest body)
1553 "Loop a certain number of times.
1554 Evaluate BODY with VAR bound to successive integers from 0, inclusive,
1555 to COUNT, exclusive. Then evaluate RESULT to get return value, default
1556 nil.
1558 \(fn (VAR COUNT [RESULT]) BODY...)"
1559 (declare (debug cl-dolist) (indent 1))
1560 `(cl-block nil
1561 (,(if (eq 'cl-dotimes (symbol-function 'dotimes)) 'cl--dotimes 'dotimes)
1562 ,spec ,@body)))
1564 ;;;###autoload
1565 (defmacro cl-do-symbols (spec &rest body)
1566 "Loop over all symbols.
1567 Evaluate BODY with VAR bound to each interned symbol, or to each symbol
1568 from OBARRAY.
1570 \(fn (VAR [OBARRAY [RESULT]]) BODY...)"
1571 (declare (indent 1)
1572 (debug ((symbolp &optional form form) cl-declarations body)))
1573 ;; Apparently this doesn't have an implicit block.
1574 `(cl-block nil
1575 (let (,(car spec))
1576 (mapatoms #'(lambda (,(car spec)) ,@body)
1577 ,@(and (cadr spec) (list (cadr spec))))
1578 ,(cl-caddr spec))))
1580 ;;;###autoload
1581 (defmacro cl-do-all-symbols (spec &rest body)
1582 "Like `cl-do-symbols', but use the default obarray.
1584 \(fn (VAR [RESULT]) BODY...)"
1585 (declare (indent 1) (debug ((symbolp &optional form) cl-declarations body)))
1586 `(cl-do-symbols (,(car spec) nil ,(cadr spec)) ,@body))
1589 ;;; Assignments.
1591 ;;;###autoload
1592 (defmacro cl-psetq (&rest args)
1593 "Set SYMs to the values VALs in parallel.
1594 This is like `setq', except that all VAL forms are evaluated (in order)
1595 before assigning any symbols SYM to the corresponding values.
1597 \(fn SYM VAL SYM VAL ...)"
1598 (declare (debug setq))
1599 (cons 'cl-psetf args))
1602 ;;; Binding control structures.
1604 ;;;###autoload
1605 (defmacro cl-progv (symbols values &rest body)
1606 "Bind SYMBOLS to VALUES dynamically in BODY.
1607 The forms SYMBOLS and VALUES are evaluated, and must evaluate to lists.
1608 Each symbol in the first list is bound to the corresponding value in the
1609 second list (or to nil if VALUES is shorter than SYMBOLS); then the
1610 BODY forms are executed and their result is returned. This is much like
1611 a `let' form, except that the list of symbols can be computed at run-time."
1612 (declare (indent 2) (debug (form form body)))
1613 (let ((bodyfun (make-symbol "cl--progv-body"))
1614 (binds (make-symbol "binds"))
1615 (syms (make-symbol "syms"))
1616 (vals (make-symbol "vals")))
1617 `(progn
1618 (defvar ,bodyfun)
1619 (let* ((,syms ,symbols)
1620 (,vals ,values)
1621 (,bodyfun (lambda () ,@body))
1622 (,binds ()))
1623 (while ,syms
1624 (push (list (pop ,syms) (list 'quote (pop ,vals))) ,binds))
1625 (eval (list 'let ,binds '(funcall ,bodyfun)))))))
1627 (defvar cl--labels-convert-cache nil)
1629 (defun cl--labels-convert (f)
1630 "Special macro-expander to rename (function F) references in `cl-labels'."
1631 (cond
1632 ;; ¡¡Big Ugly Hack!! We can't use a compiler-macro because those are checked
1633 ;; *after* handling `function', but we want to stop macroexpansion from
1634 ;; being applied infinitely, so we use a cache to return the exact `form'
1635 ;; being expanded even though we don't receive it.
1636 ((eq f (car cl--labels-convert-cache)) (cdr cl--labels-convert-cache))
1638 (let ((found (assq f macroexpand-all-environment)))
1639 (if (and found (ignore-errors
1640 (eq (cadr (cl-caddr found)) 'cl-labels-args)))
1641 (cadr (cl-caddr (cl-cadddr found)))
1642 (let ((res `(function ,f)))
1643 (setq cl--labels-convert-cache (cons f res))
1644 res))))))
1646 ;;;###autoload
1647 (defmacro cl-flet (bindings &rest body)
1648 "Make temporary function definitions.
1649 Like `cl-labels' but the definitions are not recursive.
1651 \(fn ((FUNC ARGLIST BODY...) ...) FORM...)"
1652 (declare (indent 1) (debug ((&rest (cl-defun)) cl-declarations body)))
1653 (let ((binds ()) (newenv macroexpand-all-environment))
1654 (dolist (binding bindings)
1655 (let ((var (make-symbol (format "--cl-%s--" (car binding)))))
1656 (push (list var `(cl-function (lambda . ,(cdr binding)))) binds)
1657 (push (cons (car binding)
1658 `(lambda (&rest cl-labels-args)
1659 (cl-list* 'funcall ',var
1660 cl-labels-args)))
1661 newenv)))
1662 `(let ,(nreverse binds)
1663 ,@(macroexp-unprogn
1664 (macroexpand-all
1665 `(progn ,@body)
1666 ;; Don't override lexical-let's macro-expander.
1667 (if (assq 'function newenv) newenv
1668 (cons (cons 'function #'cl--labels-convert) newenv)))))))
1670 ;;;###autoload
1671 (defmacro cl-flet* (bindings &rest body)
1672 "Make temporary function definitions.
1673 Like `cl-flet' but the definitions can refer to previous ones.
1675 \(fn ((FUNC ARGLIST BODY...) ...) FORM...)"
1676 (declare (indent 1) (debug cl-flet))
1677 (cond
1678 ((null bindings) (macroexp-progn body))
1679 ((null (cdr bindings)) `(cl-flet ,bindings ,@body))
1680 (t `(cl-flet (,(pop bindings)) (cl-flet* ,bindings ,@body)))))
1682 ;;;###autoload
1683 (defmacro cl-labels (bindings &rest body)
1684 "Make temporary function bindings.
1685 The bindings can be recursive and the scoping is lexical, but capturing them
1686 in closures will only work if `lexical-binding' is in use.
1688 \(fn ((FUNC ARGLIST BODY...) ...) FORM...)"
1689 (declare (indent 1) (debug cl-flet))
1690 (let ((binds ()) (newenv macroexpand-all-environment))
1691 (dolist (binding bindings)
1692 (let ((var (make-symbol (format "--cl-%s--" (car binding)))))
1693 (push (list var `(cl-function (lambda . ,(cdr binding)))) binds)
1694 (push (cons (car binding)
1695 `(lambda (&rest cl-labels-args)
1696 (cl-list* 'funcall ',var
1697 cl-labels-args)))
1698 newenv)))
1699 (macroexpand-all `(letrec ,(nreverse binds) ,@body)
1700 ;; Don't override lexical-let's macro-expander.
1701 (if (assq 'function newenv) newenv
1702 (cons (cons 'function #'cl--labels-convert) newenv)))))
1704 ;; The following ought to have a better definition for use with newer
1705 ;; byte compilers.
1706 ;;;###autoload
1707 (defmacro cl-macrolet (bindings &rest body)
1708 "Make temporary macro definitions.
1709 This is like `cl-flet', but for macros instead of functions.
1711 \(fn ((NAME ARGLIST BODY...) ...) FORM...)"
1712 (declare (indent 1)
1713 (debug
1714 ((&rest (&define name (&rest arg) cl-declarations-or-string
1715 def-body))
1716 cl-declarations body)))
1717 (if (cdr bindings)
1718 `(cl-macrolet (,(car bindings)) (cl-macrolet ,(cdr bindings) ,@body))
1719 (if (null bindings) (cons 'progn body)
1720 (let* ((name (caar bindings))
1721 (res (cl--transform-lambda (cdar bindings) name)))
1722 (eval (car res))
1723 (macroexpand-all (cons 'progn body)
1724 (cons (cons name `(lambda ,@(cdr res)))
1725 macroexpand-all-environment))))))
1727 (defconst cl--old-macroexpand
1728 (if (and (boundp 'cl--old-macroexpand)
1729 (eq (symbol-function 'macroexpand)
1730 #'cl--sm-macroexpand))
1731 cl--old-macroexpand
1732 (symbol-function 'macroexpand)))
1734 (defun cl--sm-macroexpand (exp &optional env)
1735 "Special macro expander used inside `cl-symbol-macrolet'.
1736 This function replaces `macroexpand' during macro expansion
1737 of `cl-symbol-macrolet', and does the same thing as `macroexpand'
1738 except that it additionally expands symbol macros."
1739 (let ((macroexpand-all-environment env))
1740 (while
1741 (progn
1742 (setq exp (funcall cl--old-macroexpand exp env))
1743 (pcase exp
1744 ((pred symbolp)
1745 ;; Perform symbol-macro expansion.
1746 (when (cdr (assq (symbol-name exp) env))
1747 (setq exp (cadr (assq (symbol-name exp) env)))))
1748 (`(setq . ,_)
1749 ;; Convert setq to setf if required by symbol-macro expansion.
1750 (let* ((args (mapcar (lambda (f) (cl--sm-macroexpand f env))
1751 (cdr exp)))
1752 (p args))
1753 (while (and p (symbolp (car p))) (setq p (cddr p)))
1754 (if p (setq exp (cons 'setf args))
1755 (setq exp (cons 'setq args))
1756 ;; Don't loop further.
1757 nil)))
1758 (`(,(or `let `let*) . ,(or `(,bindings . ,body) dontcare))
1759 ;; CL's symbol-macrolet treats re-bindings as candidates for
1760 ;; expansion (turning the let into a letf if needed), contrary to
1761 ;; Common-Lisp where such re-bindings hide the symbol-macro.
1762 (let ((letf nil) (found nil) (nbs ()))
1763 (dolist (binding bindings)
1764 (let* ((var (if (symbolp binding) binding (car binding)))
1765 (sm (assq (symbol-name var) env)))
1766 (push (if (not (cdr sm))
1767 binding
1768 (let ((nexp (cadr sm)))
1769 (setq found t)
1770 (unless (symbolp nexp) (setq letf t))
1771 (cons nexp (cdr-safe binding))))
1772 nbs)))
1773 (when found
1774 (setq exp `(,(if letf
1775 (if (eq (car exp) 'let) 'cl-letf 'cl-letf*)
1776 (car exp))
1777 ,(nreverse nbs)
1778 ,@body)))))
1779 ;; FIXME: The behavior of CL made sense in a dynamically scoped
1780 ;; language, but for lexical scoping, Common-Lisp's behavior might
1781 ;; make more sense (and indeed, CL behaves like Common-Lisp w.r.t
1782 ;; lexical-let), so maybe we should adjust the behavior based on
1783 ;; the use of lexical-binding.
1784 ;; (`(,(or `let `let*) . ,(or `(,bindings . ,body) dontcare))
1785 ;; (let ((nbs ()) (found nil))
1786 ;; (dolist (binding bindings)
1787 ;; (let* ((var (if (symbolp binding) binding (car binding)))
1788 ;; (name (symbol-name var))
1789 ;; (val (and found (consp binding) (eq 'let* (car exp))
1790 ;; (list (macroexpand-all (cadr binding)
1791 ;; env)))))
1792 ;; (push (if (assq name env)
1793 ;; ;; This binding should hide its symbol-macro,
1794 ;; ;; but given the way macroexpand-all works, we
1795 ;; ;; can't prevent application of `env' to the
1796 ;; ;; sub-expressions, so we need to α-rename this
1797 ;; ;; variable instead.
1798 ;; (let ((nvar (make-symbol
1799 ;; (copy-sequence name))))
1800 ;; (setq found t)
1801 ;; (push (list name nvar) env)
1802 ;; (cons nvar (or val (cdr-safe binding))))
1803 ;; (if val (cons var val) binding))
1804 ;; nbs)))
1805 ;; (when found
1806 ;; (setq exp `(,(car exp)
1807 ;; ,(nreverse nbs)
1808 ;; ,@(macroexp-unprogn
1809 ;; (macroexpand-all (macroexp-progn body)
1810 ;; env)))))
1811 ;; nil))
1813 exp))
1815 ;;;###autoload
1816 (defmacro cl-symbol-macrolet (bindings &rest body)
1817 "Make symbol macro definitions.
1818 Within the body FORMs, references to the variable NAME will be replaced
1819 by EXPANSION, and (setq NAME ...) will act like (setf EXPANSION ...).
1821 \(fn ((NAME EXPANSION) ...) FORM...)"
1822 (declare (indent 1) (debug ((&rest (symbol sexp)) cl-declarations body)))
1823 (cond
1824 ((cdr bindings)
1825 `(cl-symbol-macrolet (,(car bindings))
1826 (cl-symbol-macrolet ,(cdr bindings) ,@body)))
1827 ((null bindings) (macroexp-progn body))
1829 (let ((previous-macroexpand (symbol-function 'macroexpand)))
1830 (unwind-protect
1831 (progn
1832 (fset 'macroexpand #'cl--sm-macroexpand)
1833 ;; FIXME: For N bindings, this will traverse `body' N times!
1834 (macroexpand-all (cons 'progn body)
1835 (cons (list (symbol-name (caar bindings))
1836 (cl-cadar bindings))
1837 macroexpand-all-environment)))
1838 (fset 'macroexpand previous-macroexpand))))))
1840 ;;; Multiple values.
1842 ;;;###autoload
1843 (defmacro cl-multiple-value-bind (vars form &rest body)
1844 "Collect multiple return values.
1845 FORM must return a list; the BODY is then executed with the first N elements
1846 of this list bound (`let'-style) to each of the symbols SYM in turn. This
1847 is analogous to the Common Lisp `cl-multiple-value-bind' macro, using lists to
1848 simulate true multiple return values. For compatibility, (cl-values A B C) is
1849 a synonym for (list A B C).
1851 \(fn (SYM...) FORM BODY)"
1852 (declare (indent 2) (debug ((&rest symbolp) form body)))
1853 (let ((temp (make-symbol "--cl-var--")) (n -1))
1854 `(let* ((,temp ,form)
1855 ,@(mapcar (lambda (v)
1856 (list v `(nth ,(setq n (1+ n)) ,temp)))
1857 vars))
1858 ,@body)))
1860 ;;;###autoload
1861 (defmacro cl-multiple-value-setq (vars form)
1862 "Collect multiple return values.
1863 FORM must return a list; the first N elements of this list are stored in
1864 each of the symbols SYM in turn. This is analogous to the Common Lisp
1865 `cl-multiple-value-setq' macro, using lists to simulate true multiple return
1866 values. For compatibility, (cl-values A B C) is a synonym for (list A B C).
1868 \(fn (SYM...) FORM)"
1869 (declare (indent 1) (debug ((&rest symbolp) form)))
1870 (cond ((null vars) `(progn ,form nil))
1871 ((null (cdr vars)) `(setq ,(car vars) (car ,form)))
1873 (let* ((temp (make-symbol "--cl-var--")) (n 0))
1874 `(let ((,temp ,form))
1875 (prog1 (setq ,(pop vars) (car ,temp))
1876 (setq ,@(apply #'nconc
1877 (mapcar (lambda (v)
1878 (list v `(nth ,(setq n (1+ n))
1879 ,temp)))
1880 vars)))))))))
1883 ;;; Declarations.
1885 ;;;###autoload
1886 (defmacro cl-locally (&rest body)
1887 (declare (debug t))
1888 (cons 'progn body))
1889 ;;;###autoload
1890 (defmacro cl-the (_type form)
1891 (declare (indent 1) (debug (cl-type-spec form)))
1892 form)
1894 (defvar cl-proclaim-history t) ; for future compilers
1895 (defvar cl-declare-stack t) ; for future compilers
1897 (defun cl-do-proclaim (spec hist)
1898 (and hist (listp cl-proclaim-history) (push spec cl-proclaim-history))
1899 (cond ((eq (car-safe spec) 'special)
1900 (if (boundp 'byte-compile-bound-variables)
1901 (setq byte-compile-bound-variables
1902 (append (cdr spec) byte-compile-bound-variables))))
1904 ((eq (car-safe spec) 'inline)
1905 (while (setq spec (cdr spec))
1906 (or (memq (get (car spec) 'byte-optimizer)
1907 '(nil byte-compile-inline-expand))
1908 (error "%s already has a byte-optimizer, can't make it inline"
1909 (car spec)))
1910 (put (car spec) 'byte-optimizer 'byte-compile-inline-expand)))
1912 ((eq (car-safe spec) 'notinline)
1913 (while (setq spec (cdr spec))
1914 (if (eq (get (car spec) 'byte-optimizer)
1915 'byte-compile-inline-expand)
1916 (put (car spec) 'byte-optimizer nil))))
1918 ((eq (car-safe spec) 'optimize)
1919 (let ((speed (assq (nth 1 (assq 'speed (cdr spec)))
1920 '((0 nil) (1 t) (2 t) (3 t))))
1921 (safety (assq (nth 1 (assq 'safety (cdr spec)))
1922 '((0 t) (1 t) (2 t) (3 nil)))))
1923 (if speed (setq cl-optimize-speed (car speed)
1924 byte-optimize (nth 1 speed)))
1925 (if safety (setq cl-optimize-safety (car safety)
1926 byte-compile-delete-errors (nth 1 safety)))))
1928 ((and (eq (car-safe spec) 'warn) (boundp 'byte-compile-warnings))
1929 (while (setq spec (cdr spec))
1930 (if (consp (car spec))
1931 (if (eq (cl-cadar spec) 0)
1932 (byte-compile-disable-warning (caar spec))
1933 (byte-compile-enable-warning (caar spec)))))))
1934 nil)
1936 ;;; Process any proclamations made before cl-macs was loaded.
1937 (defvar cl-proclaims-deferred)
1938 (let ((p (reverse cl-proclaims-deferred)))
1939 (while p (cl-do-proclaim (pop p) t))
1940 (setq cl-proclaims-deferred nil))
1942 ;;;###autoload
1943 (defmacro cl-declare (&rest specs)
1944 "Declare SPECS about the current function while compiling.
1945 For instance
1947 \(cl-declare (warn 0))
1949 will turn off byte-compile warnings in the function.
1950 See Info node `(cl)Declarations' for details."
1951 (if (cl--compiling-file)
1952 (while specs
1953 (if (listp cl-declare-stack) (push (car specs) cl-declare-stack))
1954 (cl-do-proclaim (pop specs) nil)))
1955 nil)
1957 ;;; The standard modify macros.
1959 ;; `setf' is now part of core Elisp, defined in gv.el.
1961 ;;;###autoload
1962 (defmacro cl-psetf (&rest args)
1963 "Set PLACEs to the values VALs in parallel.
1964 This is like `setf', except that all VAL forms are evaluated (in order)
1965 before assigning any PLACEs to the corresponding values.
1967 \(fn PLACE VAL PLACE VAL ...)"
1968 (declare (debug setf))
1969 (let ((p args) (simple t) (vars nil))
1970 (while p
1971 (if (or (not (symbolp (car p))) (cl--expr-depends-p (nth 1 p) vars))
1972 (setq simple nil))
1973 (if (memq (car p) vars)
1974 (error "Destination duplicated in psetf: %s" (car p)))
1975 (push (pop p) vars)
1976 (or p (error "Odd number of arguments to cl-psetf"))
1977 (pop p))
1978 (if simple
1979 `(progn (setq ,@args) nil)
1980 (setq args (reverse args))
1981 (let ((expr `(setf ,(cadr args) ,(car args))))
1982 (while (setq args (cddr args))
1983 (setq expr `(setf ,(cadr args) (prog1 ,(car args) ,expr))))
1984 `(progn ,expr nil)))))
1986 ;;;###autoload
1987 (defmacro cl-remf (place tag)
1988 "Remove TAG from property list PLACE.
1989 PLACE may be a symbol, or any generalized variable allowed by `setf'.
1990 The form returns true if TAG was found and removed, nil otherwise."
1991 (declare (debug (place form)))
1992 (gv-letplace (tval setter) place
1993 (macroexp-let2 macroexp-copyable-p ttag tag
1994 `(if (eq ,ttag (car ,tval))
1995 (progn ,(funcall setter `(cddr ,tval))
1997 (cl--do-remf ,tval ,ttag)))))
1999 ;;;###autoload
2000 (defmacro cl-shiftf (place &rest args)
2001 "Shift left among PLACEs.
2002 Example: (cl-shiftf A B C) sets A to B, B to C, and returns the old A.
2003 Each PLACE may be a symbol, or any generalized variable allowed by `setf'.
2005 \(fn PLACE... VAL)"
2006 (declare (debug (&rest place)))
2007 (cond
2008 ((null args) place)
2009 ((symbolp place) `(prog1 ,place (setq ,place (cl-shiftf ,@args))))
2011 (gv-letplace (getter setter) place
2012 `(prog1 ,getter
2013 ,(funcall setter `(cl-shiftf ,@args)))))))
2015 ;;;###autoload
2016 (defmacro cl-rotatef (&rest args)
2017 "Rotate left among PLACEs.
2018 Example: (cl-rotatef A B C) sets A to B, B to C, and C to A. It returns nil.
2019 Each PLACE may be a symbol, or any generalized variable allowed by `setf'.
2021 \(fn PLACE...)"
2022 (declare (debug (&rest place)))
2023 (if (not (memq nil (mapcar 'symbolp args)))
2024 (and (cdr args)
2025 (let ((sets nil)
2026 (first (car args)))
2027 (while (cdr args)
2028 (setq sets (nconc sets (list (pop args) (car args)))))
2029 `(cl-psetf ,@sets ,(car args) ,first)))
2030 (let* ((places (reverse args))
2031 (temp (make-symbol "--cl-rotatef--"))
2032 (form temp))
2033 (while (cdr places)
2034 (setq form
2035 (gv-letplace (getter setter) (pop places)
2036 `(prog1 ,getter ,(funcall setter form)))))
2037 (gv-letplace (getter setter) (car places)
2038 (macroexp-let* `((,temp ,getter))
2039 `(progn ,(funcall setter form) nil))))))
2041 ;; FIXME: `letf' is unsatisfactory because it does not really "restore" the
2042 ;; previous state. If the getter/setter loses information, that info is
2043 ;; not recovered.
2045 (defun cl--letf (bindings simplebinds binds body)
2046 ;; It's not quite clear what the semantics of cl-letf should be.
2047 ;; E.g. in (cl-letf ((PLACE1 VAL1) (PLACE2 VAL2)) BODY), while it's clear
2048 ;; that the actual assignments ("bindings") should only happen after
2049 ;; evaluating VAL1 and VAL2, it's not clear when the sub-expressions of
2050 ;; PLACE1 and PLACE2 should be evaluated. Should we have
2051 ;; PLACE1; VAL1; PLACE2; VAL2; bind1; bind2
2052 ;; or
2053 ;; VAL1; VAL2; PLACE1; PLACE2; bind1; bind2
2054 ;; or
2055 ;; VAL1; VAL2; PLACE1; bind1; PLACE2; bind2
2056 ;; Common-Lisp's `psetf' does the first, so we'll do the same.
2057 (if (null bindings)
2058 (if (and (null binds) (null simplebinds)) (macroexp-progn body)
2059 `(let* (,@(mapcar (lambda (x)
2060 (pcase-let ((`(,vold ,getter ,_setter ,_vnew) x))
2061 (list vold getter)))
2062 binds)
2063 ,@simplebinds)
2064 (unwind-protect
2065 ,(macroexp-progn
2066 (append
2067 (delq nil
2068 (mapcar (lambda (x)
2069 (pcase x
2070 ;; If there's no vnew, do nothing.
2071 (`(,_vold ,_getter ,setter ,vnew)
2072 (funcall setter vnew))))
2073 binds))
2074 body))
2075 ,@(mapcar (lambda (x)
2076 (pcase-let ((`(,vold ,_getter ,setter ,_vnew) x))
2077 (funcall setter vold)))
2078 binds))))
2079 (let ((binding (car bindings)))
2080 (gv-letplace (getter setter) (car binding)
2081 (macroexp-let2 nil vnew (cadr binding)
2082 (if (symbolp (car binding))
2083 ;; Special-case for simple variables.
2084 (cl--letf (cdr bindings)
2085 (cons `(,getter ,(if (cdr binding) vnew getter))
2086 simplebinds)
2087 binds body)
2088 (cl--letf (cdr bindings) simplebinds
2089 (cons `(,(make-symbol "old") ,getter ,setter
2090 ,@(if (cdr binding) (list vnew)))
2091 binds)
2092 body)))))))
2094 ;;;###autoload
2095 (defmacro cl-letf (bindings &rest body)
2096 "Temporarily bind to PLACEs.
2097 This is the analogue of `let', but with generalized variables (in the
2098 sense of `setf') for the PLACEs. Each PLACE is set to the corresponding
2099 VALUE, then the BODY forms are executed. On exit, either normally or
2100 because of a `throw' or error, the PLACEs are set back to their original
2101 values. Note that this macro is *not* available in Common Lisp.
2102 As a special case, if `(PLACE)' is used instead of `(PLACE VALUE)',
2103 the PLACE is not modified before executing BODY.
2105 \(fn ((PLACE VALUE) ...) BODY...)"
2106 (declare (indent 1) (debug ((&rest (gate gv-place &optional form)) body)))
2107 (if (and (not (cdr bindings)) (cdar bindings) (symbolp (caar bindings)))
2108 `(let ,bindings ,@body)
2109 (cl--letf bindings () () body)))
2111 ;;;###autoload
2112 (defmacro cl-letf* (bindings &rest body)
2113 "Temporarily bind to PLACEs.
2114 Like `cl-letf' but where the bindings are performed one at a time,
2115 rather than all at the end (i.e. like `let*' rather than like `let')."
2116 (declare (indent 1) (debug cl-letf))
2117 (dolist (binding (reverse bindings))
2118 (setq body (list `(cl-letf (,binding) ,@body))))
2119 (macroexp-progn body))
2121 ;;;###autoload
2122 (defmacro cl-callf (func place &rest args)
2123 "Set PLACE to (FUNC PLACE ARGS...).
2124 FUNC should be an unquoted function name. PLACE may be a symbol,
2125 or any generalized variable allowed by `setf'."
2126 (declare (indent 2) (debug (cl-function place &rest form)))
2127 (gv-letplace (getter setter) place
2128 (let* ((rargs (cons getter args)))
2129 (funcall setter
2130 (if (symbolp func) (cons func rargs)
2131 `(funcall #',func ,@rargs))))))
2133 ;;;###autoload
2134 (defmacro cl-callf2 (func arg1 place &rest args)
2135 "Set PLACE to (FUNC ARG1 PLACE ARGS...).
2136 Like `cl-callf', but PLACE is the second argument of FUNC, not the first.
2138 \(fn FUNC ARG1 PLACE ARGS...)"
2139 (declare (indent 3) (debug (cl-function form place &rest form)))
2140 (if (and (cl--safe-expr-p arg1) (cl--simple-expr-p place) (symbolp func))
2141 `(setf ,place (,func ,arg1 ,place ,@args))
2142 (macroexp-let2 nil a1 arg1
2143 (gv-letplace (getter setter) place
2144 (let* ((rargs (cl-list* a1 getter args)))
2145 (funcall setter
2146 (if (symbolp func) (cons func rargs)
2147 `(funcall #',func ,@rargs))))))))
2149 ;;; Structures.
2151 ;;;###autoload
2152 (defmacro cl-defstruct (struct &rest descs)
2153 "Define a struct type.
2154 This macro defines a new data type called NAME that stores data
2155 in SLOTs. It defines a `make-NAME' constructor, a `copy-NAME'
2156 copier, a `NAME-p' predicate, and slot accessors named `NAME-SLOT'.
2157 You can use the accessors to set the corresponding slots, via `setf'.
2159 NAME may instead take the form (NAME OPTIONS...), where each
2160 OPTION is either a single keyword or (KEYWORD VALUE) where
2161 KEYWORD can be one of :conc-name, :constructor, :copier, :predicate,
2162 :type, :named, :initial-offset, :print-function, or :include.
2164 Each SLOT may instead take the form (SLOT SLOT-OPTS...), where
2165 SLOT-OPTS are keyword-value pairs for that slot. Currently, only
2166 one keyword is supported, `:read-only'. If this has a non-nil
2167 value, that slot cannot be set via `setf'.
2169 \(fn NAME SLOTS...)"
2170 (declare (doc-string 2) (indent 1)
2171 (debug
2172 (&define ;Makes top-level form not be wrapped.
2173 [&or symbolp
2174 (gate
2175 symbolp &rest
2176 (&or [":conc-name" symbolp]
2177 [":constructor" symbolp &optional cl-lambda-list]
2178 [":copier" symbolp]
2179 [":predicate" symbolp]
2180 [":include" symbolp &rest sexp] ;; Not finished.
2181 ;; The following are not supported.
2182 ;; [":print-function" ...]
2183 ;; [":type" ...]
2184 ;; [":initial-offset" ...]
2186 [&optional stringp]
2187 ;; All the above is for the following def-form.
2188 &rest &or symbolp (symbolp def-form
2189 &optional ":read-only" sexp))))
2190 (let* ((name (if (consp struct) (car struct) struct))
2191 (opts (cdr-safe struct))
2192 (slots nil)
2193 (defaults nil)
2194 (conc-name (concat (symbol-name name) "-"))
2195 (constructor (intern (format "make-%s" name)))
2196 (constrs nil)
2197 (copier (intern (format "copy-%s" name)))
2198 (predicate (intern (format "%s-p" name)))
2199 (print-func nil) (print-auto nil)
2200 (safety (if (cl--compiling-file) cl-optimize-safety 3))
2201 (include nil)
2202 (tag (intern (format "cl-struct-%s" name)))
2203 (tag-symbol (intern (format "cl-struct-%s-tags" name)))
2204 (include-descs nil)
2205 (side-eff nil)
2206 (type nil)
2207 (named nil)
2208 (forms nil)
2209 pred-form pred-check)
2210 (if (stringp (car descs))
2211 (push `(put ',name 'structure-documentation
2212 ,(pop descs)) forms))
2213 (setq descs (cons '(cl-tag-slot)
2214 (mapcar (function (lambda (x) (if (consp x) x (list x))))
2215 descs)))
2216 (while opts
2217 (let ((opt (if (consp (car opts)) (caar opts) (car opts)))
2218 (args (cdr-safe (pop opts))))
2219 (cond ((eq opt :conc-name)
2220 (if args
2221 (setq conc-name (if (car args)
2222 (symbol-name (car args)) ""))))
2223 ((eq opt :constructor)
2224 (if (cdr args)
2225 (progn
2226 ;; If this defines a constructor of the same name as
2227 ;; the default one, don't define the default.
2228 (if (eq (car args) constructor)
2229 (setq constructor nil))
2230 (push args constrs))
2231 (if args (setq constructor (car args)))))
2232 ((eq opt :copier)
2233 (if args (setq copier (car args))))
2234 ((eq opt :predicate)
2235 (if args (setq predicate (car args))))
2236 ((eq opt :include)
2237 (setq include (car args)
2238 include-descs (mapcar (function
2239 (lambda (x)
2240 (if (consp x) x (list x))))
2241 (cdr args))))
2242 ((eq opt :print-function)
2243 (setq print-func (car args)))
2244 ((eq opt :type)
2245 (setq type (car args)))
2246 ((eq opt :named)
2247 (setq named t))
2248 ((eq opt :initial-offset)
2249 (setq descs (nconc (make-list (car args) '(cl-skip-slot))
2250 descs)))
2252 (error "Slot option %s unrecognized" opt)))))
2253 (if print-func
2254 (setq print-func
2255 `(progn (funcall #',print-func cl-x cl-s cl-n) t))
2256 (or type (and include (not (get include 'cl-struct-print)))
2257 (setq print-auto t
2258 print-func (and (or (not (or include type)) (null print-func))
2259 `(progn
2260 (princ ,(format "#S(%s" name) cl-s))))))
2261 (if include
2262 (let ((inc-type (get include 'cl-struct-type))
2263 (old-descs (get include 'cl-struct-slots)))
2264 (or inc-type (error "%s is not a struct name" include))
2265 (and type (not (eq (car inc-type) type))
2266 (error ":type disagrees with :include for %s" name))
2267 (while include-descs
2268 (setcar (memq (or (assq (caar include-descs) old-descs)
2269 (error "No slot %s in included struct %s"
2270 (caar include-descs) include))
2271 old-descs)
2272 (pop include-descs)))
2273 (setq descs (append old-descs (delq (assq 'cl-tag-slot descs) descs))
2274 type (car inc-type)
2275 named (assq 'cl-tag-slot descs))
2276 (if (cadr inc-type) (setq tag name named t))
2277 (let ((incl include))
2278 (while incl
2279 (push `(cl-pushnew ',tag
2280 ,(intern (format "cl-struct-%s-tags" incl)))
2281 forms)
2282 (setq incl (get incl 'cl-struct-include)))))
2283 (if type
2284 (progn
2285 (or (memq type '(vector list))
2286 (error "Invalid :type specifier: %s" type))
2287 (if named (setq tag name)))
2288 (setq type 'vector named 'true)))
2289 (or named (setq descs (delq (assq 'cl-tag-slot descs) descs)))
2290 (push `(defvar ,tag-symbol) forms)
2291 (setq pred-form (and named
2292 (let ((pos (- (length descs)
2293 (length (memq (assq 'cl-tag-slot descs)
2294 descs)))))
2295 (if (eq type 'vector)
2296 `(and (vectorp cl-x)
2297 (>= (length cl-x) ,(length descs))
2298 (memq (aref cl-x ,pos) ,tag-symbol))
2299 (if (= pos 0)
2300 `(memq (car-safe cl-x) ,tag-symbol)
2301 `(and (consp cl-x)
2302 (memq (nth ,pos cl-x) ,tag-symbol))))))
2303 pred-check (and pred-form (> safety 0)
2304 (if (and (eq (cl-caadr pred-form) 'vectorp)
2305 (= safety 1))
2306 (cons 'and (cl-cdddr pred-form)) pred-form)))
2307 (let ((pos 0) (descp descs))
2308 (while descp
2309 (let* ((desc (pop descp))
2310 (slot (car desc)))
2311 (if (memq slot '(cl-tag-slot cl-skip-slot))
2312 (progn
2313 (push nil slots)
2314 (push (and (eq slot 'cl-tag-slot) `',tag)
2315 defaults))
2316 (if (assq slot descp)
2317 (error "Duplicate slots named %s in %s" slot name))
2318 (let ((accessor (intern (format "%s%s" conc-name slot))))
2319 (push slot slots)
2320 (push (nth 1 desc) defaults)
2321 (push `(cl-defsubst ,accessor (cl-x)
2322 ,@(and pred-check
2323 (list `(or ,pred-check
2324 (error "%s accessing a non-%s"
2325 ',accessor ',name))))
2326 ,(if (eq type 'vector) `(aref cl-x ,pos)
2327 (if (= pos 0) '(car cl-x)
2328 `(nth ,pos cl-x)))) forms)
2329 (push (cons accessor t) side-eff)
2330 (if (cadr (memq :read-only (cddr desc)))
2331 (push `(gv-define-expander ,accessor
2332 (lambda (_cl-do _cl-x)
2333 (error "%s is a read-only slot" ',accessor)))
2334 forms)
2335 ;; For normal slots, we don't need to define a setf-expander,
2336 ;; since gv-get can use the compiler macro to get the
2337 ;; same result.
2338 ;; (push `(gv-define-setter ,accessor (cl-val cl-x)
2339 ;; ;; If cl is loaded only for compilation,
2340 ;; ;; the call to cl--struct-setf-expander would
2341 ;; ;; cause a warning because it may not be
2342 ;; ;; defined at run time. Suppress that warning.
2343 ;; (progn
2344 ;; (declare-function
2345 ;; cl--struct-setf-expander "cl-macs"
2346 ;; (x name accessor pred-form pos))
2347 ;; (cl--struct-setf-expander
2348 ;; cl-val cl-x ',name ',accessor
2349 ;; ,(and pred-check `',pred-check)
2350 ;; ,pos)))
2351 ;; forms)
2353 (if print-auto
2354 (nconc print-func
2355 (list `(princ ,(format " %s" slot) cl-s)
2356 `(prin1 (,accessor cl-x) cl-s)))))))
2357 (setq pos (1+ pos))))
2358 (setq slots (nreverse slots)
2359 defaults (nreverse defaults))
2360 (and predicate pred-form
2361 (progn (push `(cl-defsubst ,predicate (cl-x)
2362 ,(if (eq (car pred-form) 'and)
2363 (append pred-form '(t))
2364 `(and ,pred-form t))) forms)
2365 (push (cons predicate 'error-free) side-eff)))
2366 (and copier
2367 (progn (push `(defun ,copier (x) (copy-sequence x)) forms)
2368 (push (cons copier t) side-eff)))
2369 (if constructor
2370 (push (list constructor
2371 (cons '&key (delq nil (copy-sequence slots))))
2372 constrs))
2373 (while constrs
2374 (let* ((name (caar constrs))
2375 (args (cadr (pop constrs)))
2376 (anames (cl--arglist-args args))
2377 (make (cl-mapcar (function (lambda (s d) (if (memq s anames) s d)))
2378 slots defaults)))
2379 (push `(cl-defsubst ,name
2380 (&cl-defs '(nil ,@descs) ,@args)
2381 (,type ,@make)) forms)
2382 (if (cl--safe-expr-p `(progn ,@(mapcar #'cl-second descs)))
2383 (push (cons name t) side-eff))))
2384 (if print-auto (nconc print-func (list '(princ ")" cl-s) t)))
2385 ;; Don't bother adding to cl-custom-print-functions since it's not used
2386 ;; by anything anyway!
2387 ;;(if print-func
2388 ;; (push `(if (boundp 'cl-custom-print-functions)
2389 ;; (push
2390 ;; ;; The auto-generated function does not pay attention to
2391 ;; ;; the depth argument cl-n.
2392 ;; (lambda (cl-x cl-s ,(if print-auto '_cl-n 'cl-n))
2393 ;; (and ,pred-form ,print-func))
2394 ;; cl-custom-print-functions))
2395 ;; forms))
2396 (push `(setq ,tag-symbol (list ',tag)) forms)
2397 (push `(cl-eval-when (compile load eval)
2398 (put ',name 'cl-struct-slots ',descs)
2399 (put ',name 'cl-struct-type ',(list type (eq named t)))
2400 (put ',name 'cl-struct-include ',include)
2401 (put ',name 'cl-struct-print ,print-auto)
2402 ,@(mapcar (lambda (x)
2403 `(put ',(car x) 'side-effect-free ',(cdr x)))
2404 side-eff))
2405 forms)
2406 `(progn ,@(nreverse (cons `',name forms)))))
2408 ;;; Types and assertions.
2410 ;;;###autoload
2411 (defmacro cl-deftype (name arglist &rest body)
2412 "Define NAME as a new data type.
2413 The type name can then be used in `cl-typecase', `cl-check-type', etc."
2414 (declare (debug cl-defmacro) (doc-string 3))
2415 `(cl-eval-when (compile load eval)
2416 (put ',name 'cl-deftype-handler
2417 (cl-function (lambda (&cl-defs '('*) ,@arglist) ,@body)))))
2419 (defun cl--make-type-test (val type)
2420 (if (symbolp type)
2421 (cond ((get type 'cl-deftype-handler)
2422 (cl--make-type-test val (funcall (get type 'cl-deftype-handler))))
2423 ((memq type '(nil t)) type)
2424 ((eq type 'null) `(null ,val))
2425 ((eq type 'atom) `(atom ,val))
2426 ((eq type 'float) `(cl-floatp-safe ,val))
2427 ((eq type 'real) `(numberp ,val))
2428 ((eq type 'fixnum) `(integerp ,val))
2429 ;; FIXME: Should `character' accept things like ?\C-\M-a ? --Stef
2430 ((memq type '(character string-char)) `(characterp ,val))
2432 (let* ((name (symbol-name type))
2433 (namep (intern (concat name "p"))))
2434 (if (fboundp namep) (list namep val)
2435 (list (intern (concat name "-p")) val)))))
2436 (cond ((get (car type) 'cl-deftype-handler)
2437 (cl--make-type-test val (apply (get (car type) 'cl-deftype-handler)
2438 (cdr type))))
2439 ((memq (car type) '(integer float real number))
2440 (delq t `(and ,(cl--make-type-test val (car type))
2441 ,(if (memq (cadr type) '(* nil)) t
2442 (if (consp (cadr type)) `(> ,val ,(cl-caadr type))
2443 `(>= ,val ,(cadr type))))
2444 ,(if (memq (cl-caddr type) '(* nil)) t
2445 (if (consp (cl-caddr type)) `(< ,val ,(cl-caaddr type))
2446 `(<= ,val ,(cl-caddr type)))))))
2447 ((memq (car type) '(and or not))
2448 (cons (car type)
2449 (mapcar (function (lambda (x) (cl--make-type-test val x)))
2450 (cdr type))))
2451 ((memq (car type) '(member cl-member))
2452 `(and (cl-member ,val ',(cdr type)) t))
2453 ((eq (car type) 'satisfies) (list (cadr type) val))
2454 (t (error "Bad type spec: %s" type)))))
2456 (defvar cl--object)
2457 ;;;###autoload
2458 (defun cl-typep (object type) ; See compiler macro below.
2459 "Check that OBJECT is of type TYPE.
2460 TYPE is a Common Lisp-style type specifier."
2461 (let ((cl--object object)) ;; Yuck!!
2462 (eval (cl--make-type-test 'cl--object type))))
2464 ;;;###autoload
2465 (defmacro cl-check-type (form type &optional string)
2466 "Verify that FORM is of type TYPE; signal an error if not.
2467 STRING is an optional description of the desired type."
2468 (declare (debug (place cl-type-spec &optional stringp)))
2469 (and (or (not (cl--compiling-file))
2470 (< cl-optimize-speed 3) (= cl-optimize-safety 3))
2471 (let* ((temp (if (cl--simple-expr-p form 3)
2472 form (make-symbol "--cl-var--")))
2473 (body `(or ,(cl--make-type-test temp type)
2474 (signal 'wrong-type-argument
2475 (list ,(or string `',type)
2476 ,temp ',form)))))
2477 (if (eq temp form) `(progn ,body nil)
2478 `(let ((,temp ,form)) ,body nil)))))
2480 ;;;###autoload
2481 (defmacro cl-assert (form &optional show-args string &rest args)
2482 ;; FIXME: This is actually not compatible with Common-Lisp's `assert'.
2483 "Verify that FORM returns non-nil; signal an error if not.
2484 Second arg SHOW-ARGS means to include arguments of FORM in message.
2485 Other args STRING and ARGS... are arguments to be passed to `error'.
2486 They are not evaluated unless the assertion fails. If STRING is
2487 omitted, a default message listing FORM itself is used."
2488 (declare (debug (form &rest form)))
2489 (and (or (not (cl--compiling-file))
2490 (< cl-optimize-speed 3) (= cl-optimize-safety 3))
2491 (let ((sargs (and show-args
2492 (delq nil (mapcar (lambda (x)
2493 (unless (macroexp-const-p x)
2495 (cdr form))))))
2496 `(progn
2497 (or ,form
2498 ,(if string
2499 `(error ,string ,@sargs ,@args)
2500 `(signal 'cl-assertion-failed
2501 (list ',form ,@sargs))))
2502 nil))))
2504 ;;; Compiler macros.
2506 ;;;###autoload
2507 (defmacro cl-define-compiler-macro (func args &rest body)
2508 "Define a compiler-only macro.
2509 This is like `defmacro', but macro expansion occurs only if the call to
2510 FUNC is compiled (i.e., not interpreted). Compiler macros should be used
2511 for optimizing the way calls to FUNC are compiled; the form returned by
2512 BODY should do the same thing as a call to the normal function called
2513 FUNC, though possibly more efficiently. Note that, like regular macros,
2514 compiler macros are expanded repeatedly until no further expansions are
2515 possible. Unlike regular macros, BODY can decide to \"punt\" and leave the
2516 original function call alone by declaring an initial `&whole foo' parameter
2517 and then returning foo."
2518 (declare (debug cl-defmacro))
2519 (let ((p args) (res nil))
2520 (while (consp p) (push (pop p) res))
2521 (setq args (nconc (nreverse res) (and p (list '&rest p)))))
2522 `(cl-eval-when (compile load eval)
2523 (put ',func 'compiler-macro
2524 (cl-function (lambda ,(if (memq '&whole args) (delq '&whole args)
2525 (cons '_cl-whole-arg args))
2526 ,@body)))
2527 ;; This is so that describe-function can locate
2528 ;; the macro definition.
2529 (let ((file ,(or buffer-file-name
2530 (and (boundp 'byte-compile-current-file)
2531 (stringp byte-compile-current-file)
2532 byte-compile-current-file))))
2533 (if file (put ',func 'compiler-macro-file
2534 (purecopy (file-name-nondirectory file)))))))
2536 ;;;###autoload
2537 (defun cl-compiler-macroexpand (form)
2538 (while
2539 (let ((func (car-safe form)) (handler nil))
2540 (while (and (symbolp func)
2541 (not (setq handler (get func 'compiler-macro)))
2542 (fboundp func)
2543 (or (not (autoloadp (symbol-function func)))
2544 (autoload-do-load (symbol-function func) func)))
2545 (setq func (symbol-function func)))
2546 (and handler
2547 (not (eq form (setq form (apply handler form (cdr form))))))))
2548 form)
2550 ;; Optimize away unused block-wrappers.
2552 (defvar cl--active-block-names nil)
2554 (cl-define-compiler-macro cl--block-wrapper (cl-form)
2555 (let* ((cl-entry (cons (nth 1 (nth 1 cl-form)) nil))
2556 (cl--active-block-names (cons cl-entry cl--active-block-names))
2557 (cl-body (macroexpand-all ;Performs compiler-macro expansions.
2558 (cons 'progn (cddr cl-form))
2559 macroexpand-all-environment)))
2560 ;; FIXME: To avoid re-applying macroexpand-all, we'd like to be able
2561 ;; to indicate that this return value is already fully expanded.
2562 (if (cdr cl-entry)
2563 `(catch ,(nth 1 cl-form) ,@(cdr cl-body))
2564 cl-body)))
2566 (cl-define-compiler-macro cl--block-throw (cl-tag cl-value)
2567 (let ((cl-found (assq (nth 1 cl-tag) cl--active-block-names)))
2568 (if cl-found (setcdr cl-found t)))
2569 `(throw ,cl-tag ,cl-value))
2571 ;;;###autoload
2572 (defmacro cl-defsubst (name args &rest body)
2573 "Define NAME as a function.
2574 Like `defun', except the function is automatically declared `inline',
2575 ARGLIST allows full Common Lisp conventions, and BODY is implicitly
2576 surrounded by (cl-block NAME ...).
2578 \(fn NAME ARGLIST [DOCSTRING] BODY...)"
2579 (declare (debug cl-defun) (indent 2))
2580 (let* ((argns (cl--arglist-args args)) (p argns)
2581 (pbody (cons 'progn body))
2582 (unsafe (not (cl--safe-expr-p pbody))))
2583 (while (and p (eq (cl--expr-contains args (car p)) 1)) (pop p))
2584 `(progn
2585 ,(if p nil ; give up if defaults refer to earlier args
2586 `(cl-define-compiler-macro ,name
2587 ,(if (memq '&key args)
2588 `(&whole cl-whole &cl-quote ,@args)
2589 (cons '&cl-quote args))
2590 (cl--defsubst-expand
2591 ',argns '(cl-block ,name ,@body)
2592 ;; We used to pass `simple' as
2593 ;; (not (or unsafe (cl-expr-access-order pbody argns)))
2594 ;; But this is much too simplistic since it
2595 ;; does not pay attention to the argvs (and
2596 ;; cl-expr-access-order itself is also too naive).
2598 ,(and (memq '&key args) 'cl-whole) ,unsafe ,@argns)))
2599 (cl-defun ,name ,args ,@body))))
2601 (defun cl--defsubst-expand (argns body simple whole unsafe &rest argvs)
2602 (if (and whole (not (cl--safe-expr-p (cons 'progn argvs)))) whole
2603 (if (cl--simple-exprs-p argvs) (setq simple t))
2604 (let* ((substs ())
2605 (lets (delq nil
2606 (cl-mapcar (lambda (argn argv)
2607 (if (or simple (macroexp-const-p argv))
2608 (progn (push (cons argn argv) substs)
2609 (and unsafe (list argn argv)))
2610 (list argn argv)))
2611 argns argvs))))
2612 ;; FIXME: `sublis/subst' will happily substitute the symbol
2613 ;; `argn' in places where it's not used as a reference
2614 ;; to a variable.
2615 ;; FIXME: `sublis/subst' will happily copy `argv' to a different
2616 ;; scope, leading to name capture.
2617 (setq body (cond ((null substs) body)
2618 ((null (cdr substs))
2619 (cl-subst (cdar substs) (caar substs) body))
2620 (t (cl-sublis substs body))))
2621 (if lets `(let ,lets ,body) body))))
2624 ;; Compile-time optimizations for some functions defined in this package.
2626 (defun cl--compiler-macro-member (form a list &rest keys)
2627 (let ((test (and (= (length keys) 2) (eq (car keys) :test)
2628 (cl--const-expr-val (nth 1 keys)))))
2629 (cond ((eq test 'eq) `(memq ,a ,list))
2630 ((eq test 'equal) `(member ,a ,list))
2631 ((or (null keys) (eq test 'eql)) `(memql ,a ,list))
2632 (t form))))
2634 (defun cl--compiler-macro-assoc (form a list &rest keys)
2635 (let ((test (and (= (length keys) 2) (eq (car keys) :test)
2636 (cl--const-expr-val (nth 1 keys)))))
2637 (cond ((eq test 'eq) `(assq ,a ,list))
2638 ((eq test 'equal) `(assoc ,a ,list))
2639 ((and (macroexp-const-p a) (or (null keys) (eq test 'eql)))
2640 (if (cl-floatp-safe (cl--const-expr-val a))
2641 `(assoc ,a ,list) `(assq ,a ,list)))
2642 (t form))))
2644 ;;;###autoload
2645 (defun cl--compiler-macro-adjoin (form a list &rest keys)
2646 (if (and (cl--simple-expr-p a) (cl--simple-expr-p list)
2647 (not (memq :key keys)))
2648 `(if (cl-member ,a ,list ,@keys) ,list (cons ,a ,list))
2649 form))
2651 (defun cl--compiler-macro-get (_form sym prop &optional def)
2652 (if def
2653 `(cl-getf (symbol-plist ,sym) ,prop ,def)
2654 `(get ,sym ,prop)))
2656 (cl-define-compiler-macro cl-typep (&whole form val type)
2657 (if (macroexp-const-p type)
2658 (macroexp-let2 macroexp-copyable-p temp val
2659 (cl--make-type-test temp (cl--const-expr-val type)))
2660 form))
2662 (dolist (y '(cl-first cl-second cl-third cl-fourth
2663 cl-fifth cl-sixth cl-seventh
2664 cl-eighth cl-ninth cl-tenth
2665 cl-rest cl-endp cl-plusp cl-minusp
2666 cl-caaar cl-caadr cl-cadar
2667 cl-caddr cl-cdaar cl-cdadr
2668 cl-cddar cl-cdddr cl-caaaar
2669 cl-caaadr cl-caadar cl-caaddr
2670 cl-cadaar cl-cadadr cl-caddar
2671 cl-cadddr cl-cdaaar cl-cdaadr
2672 cl-cdadar cl-cdaddr cl-cddaar
2673 cl-cddadr cl-cdddar cl-cddddr))
2674 (put y 'side-effect-free t))
2676 ;;; Things that are inline.
2677 (cl-proclaim '(inline cl-floatp-safe cl-acons cl-map cl-concatenate cl-notany
2678 cl-notevery cl--set-elt cl-revappend cl-nreconc gethash))
2680 ;;; Things that are side-effect-free.
2681 (mapc (lambda (x) (put x 'side-effect-free t))
2682 '(cl-oddp cl-evenp cl-signum last butlast cl-ldiff cl-pairlis cl-gcd cl-lcm
2683 cl-isqrt cl-floor cl-ceiling cl-truncate cl-round cl-mod cl-rem cl-subseq
2684 cl-list-length cl-get cl-getf))
2686 ;;; Things that are side-effect-and-error-free.
2687 (mapc (lambda (x) (put x 'side-effect-free 'error-free))
2688 '(eql cl-floatp-safe cl-list* cl-subst cl-acons cl-equalp cl-random-state-p
2689 copy-tree cl-sublis))
2692 (run-hooks 'cl-macs-load-hook)
2694 ;; Local variables:
2695 ;; byte-compile-dynamic: t
2696 ;; generated-autoload-file: "cl-loaddefs.el"
2697 ;; End:
2699 (provide 'cl-macs)
2701 ;;; cl-macs.el ends here