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