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