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