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