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