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