1 ;;; cl-macs.el --- Common Lisp macros -*- lexical-binding: t; coding: utf-8 -*-
3 ;; Copyright (C) 1993, 2001-2012 Free Software Foundation, Inc.
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
7 ;; Keywords: extensions
10 ;; This file is part of GNU Emacs.
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.
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.
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/>.
27 ;; These are extensions to Emacs Lisp that provide a degree of
28 ;; Common Lisp compatibility, beyond what is already built-in
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.
34 ;; Bug reports, comments, and suggestions are welcome!
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.
41 ;; See cl.el for Change Log.
48 ;; `gv' is required here because cl-macs can be loaded before loaddefs.el.
51 (defmacro cl-pop2
(place)
52 (declare (debug edebug-sexps
))
53 `(prog1 (car (cdr ,place
))
54 (setq ,place
(cdr (cdr ,place
)))))
56 (defvar cl-optimize-safety
)
57 (defvar cl-optimize-speed
)
61 ;;; Some predicates for analyzing Lisp forms.
62 ;; These are used by various
63 ;; macro expanders to optimize the results in certain common cases.
65 (defconst cl--simple-funcs
'(car cdr nth aref elt if and or
+ -
1+ 1- min max
66 car-safe cdr-safe progn prog1 prog2
))
67 (defconst cl--safe-funcs
'(* / % length memq list vector vectorp
70 (defun cl--simple-expr-p (x &optional size
)
71 "Check if no side effects, and executes quickly."
72 (or size
(setq size
10))
73 (if (and (consp x
) (not (memq (car x
) '(quote function cl-function
))))
74 (and (symbolp (car x
))
75 (or (memq (car x
) cl--simple-funcs
)
76 (get (car x
) 'side-effect-free
))
79 (while (and (setq x
(cdr x
))
80 (setq size
(cl--simple-expr-p (car x
) size
))))
81 (and (null x
) (>= size
0) size
)))
82 (and (> size
0) (1- size
))))
84 (defun cl--simple-exprs-p (xs)
85 (while (and xs
(cl--simple-expr-p (car xs
)))
89 (defun cl--safe-expr-p (x)
90 "Check if no side effects."
91 (or (not (and (consp x
) (not (memq (car x
) '(quote function cl-function
)))))
92 (and (symbolp (car x
))
93 (or (memq (car x
) cl--simple-funcs
)
94 (memq (car x
) cl--safe-funcs
)
95 (get (car x
) 'side-effect-free
))
97 (while (and (setq x
(cdr x
)) (cl--safe-expr-p (car x
))))
100 ;;; Check if constant (i.e., no side effects or dependencies).
101 (defun cl--const-expr-p (x)
103 (or (eq (car x
) 'quote
)
104 (and (memq (car x
) '(function cl-function
))
105 (or (symbolp (nth 1 x
))
106 (and (eq (car-safe (nth 1 x
)) 'lambda
) 'func
)))))
107 ((symbolp x
) (and (memq x
'(nil t
)) t
))
110 (defun cl--const-expr-val (x)
111 (and (macroexp-const-p x
) (if (consp x
) (nth 1 x
) x
)))
113 (defun cl-expr-access-order (x v
)
114 ;; This apparently tries to return nil iff the expression X evaluates
115 ;; the variables V in the same order as they appear in V (so as to
116 ;; be able to replace those vars with the expressions they're bound
118 ;; FIXME: This is very naive, it doesn't even check to see if those
119 ;; variables appear more than once.
120 (if (macroexp-const-p x
) v
123 (while (setq x
(cdr x
)) (setq v
(cl-expr-access-order (car x
) v
)))
125 (if (eq x
(car v
)) (cdr v
) '(t)))))
127 (defun cl--expr-contains (x y
)
128 "Count number of times X refers to Y. Return nil for 0 times."
129 ;; FIXME: This is naive, and it will cl-count Y as referred twice in
130 ;; (let ((Y 1)) Y) even though it should be 0. Also it is often called on
131 ;; non-macroexpanded code, so it may also miss some occurrences that would
132 ;; only appear in the expanded code.
133 (cond ((equal y x
) 1)
134 ((and (consp x
) (not (memq (car x
) '(quote function cl-function
))))
137 (setq sum
(+ sum
(or (cl--expr-contains (pop x
) y
) 0))))
138 (setq sum
(+ sum
(or (cl--expr-contains x y
) 0)))
139 (and (> sum
0) sum
)))
142 (defun cl--expr-contains-any (x y
)
143 (while (and y
(not (cl--expr-contains x
(car y
)))) (pop y
))
146 (defun cl--expr-depends-p (x y
)
147 "Check whether X may depend on any of the symbols in Y."
148 (and (not (macroexp-const-p x
))
149 (or (not (cl--safe-expr-p x
)) (cl--expr-contains-any x y
))))
153 (defvar cl--gensym-counter
)
155 (defun cl-gensym (&optional prefix
)
156 "Generate a new uninterned symbol.
157 The name is made by appending a number to PREFIX, default \"G\"."
158 (let ((pfix (if (stringp prefix
) prefix
"G"))
159 (num (if (integerp prefix
) prefix
160 (prog1 cl--gensym-counter
161 (setq cl--gensym-counter
(1+ cl--gensym-counter
))))))
162 (make-symbol (format "%s%d" pfix num
))))
165 (defun cl-gentemp (&optional prefix
)
166 "Generate a new interned symbol with a unique name.
167 The name is made by appending a number to PREFIX, default \"G\"."
168 (let ((pfix (if (stringp prefix
) prefix
"G"))
170 (while (intern-soft (setq name
(format "%s%d" pfix cl--gensym-counter
)))
171 (setq cl--gensym-counter
(1+ cl--gensym-counter
)))
175 ;;; Program structure.
177 (def-edebug-spec cl-declarations
178 (&rest
("cl-declare" &rest sexp
)))
180 (def-edebug-spec cl-declarations-or-string
181 (&or stringp cl-declarations
))
183 (def-edebug-spec cl-lambda-list
185 [&optional
["&optional" cl-
&optional-arg
&rest cl-
&optional-arg
]]
186 [&optional
["&rest" arg
]]
187 [&optional
["&key" [cl-
&key-arg
&rest cl-
&key-arg
]
188 &optional
"&allow-other-keys"]]
189 [&optional
["&aux" &rest
190 &or
(symbolp &optional def-form
) symbolp
]]
193 (def-edebug-spec cl-
&optional-arg
194 (&or
(arg &optional def-form arg
) arg
))
196 (def-edebug-spec cl-
&key-arg
197 (&or
([&or
(symbolp arg
) arg
] &optional def-form arg
) arg
))
199 (defconst cl--lambda-list-keywords
200 '(&optional
&rest
&key
&allow-other-keys
&aux
&whole
&body
&environment
))
202 (defvar cl--bind-block
) (defvar cl--bind-defs
) (defvar cl--bind-enquote
)
203 (defvar cl--bind-inits
) (defvar cl--bind-lets
) (defvar cl--bind-forms
)
205 (defun cl--transform-lambda (form bind-block
)
206 (let* ((args (car form
)) (body (cdr form
)) (orig-args args
)
207 (cl--bind-block bind-block
) (cl--bind-defs nil
) (cl--bind-enquote nil
)
208 (cl--bind-inits nil
) (cl--bind-lets nil
) (cl--bind-forms nil
)
209 (header nil
) (simple-args nil
))
210 (while (or (stringp (car body
))
211 (memq (car-safe (car body
)) '(interactive cl-declare
)))
212 (push (pop body
) header
))
213 (setq args
(if (listp args
) (cl-copy-list args
) (list '&rest args
)))
214 (let ((p (last args
))) (if (cdr p
) (setcdr p
(list '&rest
(cdr p
)))))
215 (if (setq cl--bind-defs
(cadr (memq '&cl-defs args
)))
216 (setq args
(delq '&cl-defs
(delq cl--bind-defs args
))
217 cl--bind-defs
(cadr cl--bind-defs
)))
218 (if (setq cl--bind-enquote
(memq '&cl-quote args
))
219 (setq args
(delq '&cl-quote args
)))
220 (if (memq '&whole args
) (error "&whole not currently implemented"))
221 (let* ((p (memq '&environment args
)) (v (cadr p
))
222 (env-exp 'macroexpand-all-environment
))
223 (if p
(setq args
(nconc (delq (car p
) (delq v args
))
224 (list '&aux
(list v env-exp
))))))
225 (while (and args
(symbolp (car args
))
226 (not (memq (car args
) '(nil &rest
&body
&key
&aux
)))
227 (not (and (eq (car args
) '&optional
)
228 (or cl--bind-defs
(consp (cadr args
))))))
229 (push (pop args
) simple-args
))
230 (or (eq cl--bind-block
'cl-none
)
231 (setq body
(list `(cl-block ,cl--bind-block
,@body
))))
233 (cl-list* nil
(nreverse simple-args
) (nconc (nreverse header
) body
))
234 (if (memq '&optional simple-args
) (push '&optional args
))
235 (cl--do-arglist args nil
(- (length simple-args
)
236 (if (memq '&optional simple-args
) 1 0)))
237 (setq cl--bind-lets
(nreverse cl--bind-lets
))
238 (cl-list* (and cl--bind-inits
`(cl-eval-when (compile load eval
)
239 ,@(nreverse cl--bind-inits
)))
240 (nconc (nreverse simple-args
)
241 (list '&rest
(car (pop cl--bind-lets
))))
242 (nconc (let ((hdr (nreverse header
)))
243 ;; Macro expansion can take place in the middle of
244 ;; apparently harmless computation, so it should not
245 ;; touch the match-data.
248 (cons (help-add-fundoc-usage
249 (if (stringp (car hdr
)) (pop hdr
))
252 (cl--make-usage-args orig-args
))))
254 (list `(let* ,cl--bind-lets
255 ,@(nreverse cl--bind-forms
)
259 (defmacro cl-defun
(name args
&rest body
)
260 "Define NAME as a function.
261 Like normal `defun', except ARGLIST allows full Common Lisp conventions,
262 and BODY is implicitly surrounded by (cl-block NAME ...).
264 \(fn NAME ARGLIST [DOCSTRING] BODY...)"
266 ;; Same as defun but use cl-lambda-list.
267 (&define
[&or name
("setf" :name setf name
)]
269 cl-declarations-or-string
270 [&optional
("interactive" interactive
)]
274 (let* ((res (cl--transform-lambda (cons args body
) name
))
275 (form `(defun ,name
,@(cdr res
))))
276 (if (car res
) `(progn ,(car res
) ,form
) form
)))
278 ;; The lambda list for macros is different from that of normal lambdas.
279 ;; Note that &environment is only allowed as first or last items in the
282 (def-edebug-spec cl-macro-list
283 (([&optional
"&environment" arg
]
285 [&optional
["&optional" &rest
286 &or
(cl-macro-arg &optional def-form cl-macro-arg
) arg
]]
287 [&optional
[[&or
"&rest" "&body"] cl-macro-arg
]]
288 [&optional
["&key" [&rest
289 [&or
([&or
(symbolp cl-macro-arg
) arg
]
290 &optional def-form cl-macro-arg
)
292 &optional
"&allow-other-keys"]]
293 [&optional
["&aux" &rest
294 &or
(symbolp &optional def-form
) symbolp
]]
295 [&optional
"&environment" arg
]
298 (def-edebug-spec cl-macro-arg
299 (&or arg cl-macro-list1
))
301 (def-edebug-spec cl-macro-list1
302 (([&optional
"&whole" arg
] ;; only allowed at lower levels
304 [&optional
["&optional" &rest
305 &or
(cl-macro-arg &optional def-form cl-macro-arg
) arg
]]
306 [&optional
[[&or
"&rest" "&body"] cl-macro-arg
]]
307 [&optional
["&key" [&rest
308 [&or
([&or
(symbolp cl-macro-arg
) arg
]
309 &optional def-form cl-macro-arg
)
311 &optional
"&allow-other-keys"]]
312 [&optional
["&aux" &rest
313 &or
(symbolp &optional def-form
) symbolp
]]
317 (defmacro cl-defmacro
(name args
&rest body
)
318 "Define NAME as a macro.
319 Like normal `defmacro', except ARGLIST allows full Common Lisp conventions,
320 and BODY is implicitly surrounded by (cl-block NAME ...).
322 \(fn NAME ARGLIST [DOCSTRING] BODY...)"
324 (&define name cl-macro-list cl-declarations-or-string def-body
))
327 (let* ((res (cl--transform-lambda (cons args body
) name
))
328 (form `(defmacro ,name
,@(cdr res
))))
329 (if (car res
) `(progn ,(car res
) ,form
) form
)))
331 (def-edebug-spec cl-lambda-expr
332 (&define
("lambda" cl-lambda-list
333 ;;cl-declarations-or-string
334 ;;[&optional ("interactive" interactive)]
337 ;; Redefine function-form to also match cl-function
338 (def-edebug-spec function-form
339 ;; form at the end could also handle "function",
340 ;; but recognize it specially to avoid wrapping function forms.
341 (&or
([&or
"quote" "function"] &or symbolp lambda-expr
)
342 ("cl-function" cl-function
)
346 (defmacro cl-function
(func)
347 "Introduce a function.
348 Like normal `function', except that if argument is a lambda form,
349 its argument list allows full Common Lisp conventions."
350 (declare (debug (&or symbolp cl-lambda-expr
)))
351 (if (eq (car-safe func
) 'lambda
)
352 (let* ((res (cl--transform-lambda (cdr func
) 'cl-none
))
353 (form `(function (lambda .
,(cdr res
)))))
354 (if (car res
) `(progn ,(car res
) ,form
) form
))
357 (declare-function help-add-fundoc-usage
"help-fns" (docstring arglist
))
359 (defun cl--make-usage-var (x)
360 "X can be a var or a (destructuring) lambda-list."
362 ((symbolp x
) (make-symbol (upcase (symbol-name x
))))
363 ((consp x
) (cl--make-usage-args x
))
366 (defun cl--make-usage-args (arglist)
367 ;; `orig-args' can contain &cl-defs (an internal
368 ;; CL thingy I don't understand), so remove it.
369 (let ((x (memq '&cl-defs arglist
)))
370 (when x
(setq arglist
(delq (car x
) (remq (cadr x
) arglist
)))))
375 (if (eq ?\
& (aref (symbol-name x
) 0))
377 (make-symbol (upcase (symbol-name x
)))))
379 ((memq state
'(nil &rest
)) (cl--make-usage-args x
))
380 (t ;(VAR INITFORM SVAR) or ((KEYWORD VAR) INITFORM SVAR).
382 (if (and (consp (car x
)) (eq state
'&key
))
383 (list (caar x
) (cl--make-usage-var (nth 1 (car x
))))
384 (cl--make-usage-var (car x
)))
386 (cl--make-usage-args (nthcdr 2 x
)) ;SVAR.
390 (defun cl--do-arglist (args expr
&optional num
) ; uses bind-*
392 (if (or (memq args cl--lambda-list-keywords
) (not (symbolp args
)))
393 (error "Invalid argument name: %s" args
)
394 (push (list args expr
) cl--bind-lets
))
395 (setq args
(cl-copy-list args
))
396 (let ((p (last args
))) (if (cdr p
) (setcdr p
(list '&rest
(cdr p
)))))
397 (let ((p (memq '&body args
))) (if p
(setcar p
'&rest
)))
398 (if (memq '&environment args
) (error "&environment used incorrectly"))
399 (let ((save-args args
)
400 (restarg (memq '&rest args
))
401 (safety (if (cl--compiling-file) cl-optimize-safety
3))
403 (laterarg nil
) (exactarg nil
) minarg
)
404 (or num
(setq num
0))
405 (if (listp (cadr restarg
))
406 (setq restarg
(make-symbol "--cl-rest--"))
407 (setq restarg
(cadr restarg
)))
408 (push (list restarg expr
) cl--bind-lets
)
409 (if (eq (car args
) '&whole
)
410 (push (list (cl-pop2 args
) restarg
) cl--bind-lets
))
412 (setq minarg restarg
)
413 (while (and p
(not (memq (car p
) cl--lambda-list-keywords
)))
414 (or (eq p args
) (setq minarg
(list 'cdr minarg
)))
416 (if (memq (car p
) '(nil &aux
))
417 (setq minarg
`(= (length ,restarg
)
418 ,(length (cl-ldiff args p
)))
419 exactarg
(not (eq args p
)))))
420 (while (and args
(not (memq (car args
) cl--lambda-list-keywords
)))
421 (let ((poparg (list (if (or (cdr args
) (not exactarg
)) 'pop
'car
)
425 (if (or laterarg
(= safety
0)) poparg
427 (signal 'wrong-number-of-arguments
428 (list ,(and (not (eq cl--bind-block
'cl-none
))
430 (length ,restarg
)))))))
431 (setq num
(1+ num
) laterarg t
))
432 (while (and (eq (car args
) '&optional
) (pop args
))
433 (while (and args
(not (memq (car args
) cl--lambda-list-keywords
)))
434 (let ((arg (pop args
)))
435 (or (consp arg
) (setq arg
(list arg
)))
436 (if (cddr arg
) (cl--do-arglist (nth 2 arg
) `(and ,restarg t
)))
437 (let ((def (if (cdr arg
) (nth 1 arg
)
438 (or (car cl--bind-defs
)
439 (nth 1 (assq (car arg
) cl--bind-defs
)))))
440 (poparg `(pop ,restarg
)))
441 (and def cl--bind-enquote
(setq def
`',def
))
442 (cl--do-arglist (car arg
)
443 (if def
`(if ,restarg
,poparg
,def
) poparg
))
444 (setq num
(1+ num
))))))
445 (if (eq (car args
) '&rest
)
446 (let ((arg (cl-pop2 args
)))
447 (if (consp arg
) (cl--do-arglist arg restarg
)))
448 (or (eq (car args
) '&key
) (= safety
0) exactarg
450 (signal 'wrong-number-of-arguments
452 ,(and (not (eq cl--bind-block
'cl-none
))
454 (+ ,num
(length ,restarg
)))))
456 (while (and (eq (car args
) '&key
) (pop args
))
457 (while (and args
(not (memq (car args
) cl--lambda-list-keywords
)))
458 (let ((arg (pop args
)))
459 (or (consp arg
) (setq arg
(list arg
)))
460 (let* ((karg (if (consp (car arg
)) (caar arg
)
461 (intern (format ":%s" (car arg
)))))
462 (varg (if (consp (car arg
)) (cl-cadar arg
) (car arg
)))
463 (def (if (cdr arg
) (cadr arg
)
464 (or (car cl--bind-defs
) (cadr (assq varg cl--bind-defs
)))))
465 (look `(memq ',karg
,restarg
)))
466 (and def cl--bind-enquote
(setq def
`',def
))
468 (let* ((temp (or (nth 2 arg
) (make-symbol "--cl-var--")))
469 (val `(car (cdr ,temp
))))
470 (cl--do-arglist temp look
)
473 (prog1 ,val
(setq ,temp t
))
477 `(car (cdr ,(if (null def
)
480 ,(if (eq (cl--const-expr-p def
) t
)
481 `'(nil ,(cl--const-expr-val def
))
482 `(list nil
,def
))))))))
484 (setq keys
(nreverse keys
))
485 (or (and (eq (car args
) '&allow-other-keys
) (pop args
))
486 (null keys
) (= safety
0)
487 (let* ((var (make-symbol "--cl-keys--"))
488 (allow '(:allow-other-keys
))
491 ((memq (car ,var
) ',(append keys allow
))
492 (setq ,var
(cdr (cdr ,var
))))
493 ((car (cdr (memq (quote ,@allow
) ,restarg
)))
497 ,(format "Keyword argument %%s not one of %s"
500 (push `(let ((,var
,restarg
)) ,check
) cl--bind-forms
)))
501 (while (and (eq (car args
) '&aux
) (pop args
))
502 (while (and args
(not (memq (car args
) cl--lambda-list-keywords
)))
503 (if (consp (car args
))
504 (if (and cl--bind-enquote
(cl-cadar args
))
505 (cl--do-arglist (caar args
)
506 `',(cadr (pop args
)))
507 (cl--do-arglist (caar args
) (cadr (pop args
))))
508 (cl--do-arglist (pop args
) nil
))))
509 (if args
(error "Malformed argument list %s" save-args
)))))
511 (defun cl--arglist-args (args)
512 (if (nlistp args
) (list args
)
513 (let ((res nil
) (kind nil
) arg
)
515 (setq arg
(pop args
))
516 (if (memq arg cl--lambda-list-keywords
) (setq kind arg
)
517 (if (eq arg
'&cl-defs
) (pop args
)
518 (and (consp arg
) kind
(setq arg
(car arg
)))
519 (and (consp arg
) (cdr arg
) (eq kind
'&key
) (setq arg
(cadr arg
)))
520 (setq res
(nconc res
(cl--arglist-args arg
))))))
521 (nconc res
(and args
(list args
))))))
524 (defmacro cl-destructuring-bind
(args expr
&rest body
)
526 (debug (&define cl-macro-list def-form cl-declarations def-body
)))
527 (let* ((cl--bind-lets nil
) (cl--bind-forms nil
) (cl--bind-inits nil
)
528 (cl--bind-defs nil
) (cl--bind-block 'cl-none
) (cl--bind-enquote nil
))
529 (cl--do-arglist (or args
'(&aux
)) expr
)
530 (append '(progn) cl--bind-inits
531 (list `(let* ,(nreverse cl--bind-lets
)
532 ,@(nreverse cl--bind-forms
) ,@body
)))))
535 ;;; The `cl-eval-when' form.
537 (defvar cl-not-toplevel nil
)
540 (defmacro cl-eval-when
(when &rest body
)
541 "Control when BODY is evaluated.
542 If `compile' is in WHEN, BODY is evaluated when compiled at top-level.
543 If `load' is in WHEN, BODY is evaluated when loaded after top-level compile.
544 If `eval' is in WHEN, BODY is evaluated when interpreted or at non-top-level.
546 \(fn (WHEN...) BODY...)"
547 (declare (indent 1) (debug ((&rest
&or
"compile" "load" "eval") body
)))
548 (if (and (fboundp 'cl--compiling-file
) (cl--compiling-file)
549 (not cl-not-toplevel
) (not (boundp 'for-effect
))) ; horrible kludge
550 (let ((comp (or (memq 'compile when
) (memq :compile-toplevel when
)))
552 (if (or (memq 'load when
) (memq :load-toplevel when
))
553 (if comp
(cons 'progn
(mapcar 'cl--compile-time-too body
))
554 `(if nil nil
,@body
))
555 (progn (if comp
(eval (cons 'progn body
))) nil
)))
556 (and (or (memq 'eval when
) (memq :execute when
))
557 (cons 'progn body
))))
559 (defun cl--compile-time-too (form)
560 (or (and (symbolp (car-safe form
)) (get (car-safe form
) 'byte-hunk-handler
))
561 (setq form
(macroexpand
562 form
(cons '(cl-eval-when) byte-compile-macro-environment
))))
563 (cond ((eq (car-safe form
) 'progn
)
564 (cons 'progn
(mapcar 'cl--compile-time-too
(cdr form
))))
565 ((eq (car-safe form
) 'cl-eval-when
)
566 (let ((when (nth 1 form
)))
567 (if (or (memq 'eval when
) (memq :execute when
))
568 `(cl-eval-when (compile ,@when
) ,@(cddr form
))
570 (t (eval form
) form
)))
573 (defmacro cl-load-time-value
(form &optional _read-only
)
574 "Like `progn', but evaluates the body at load time.
575 The result of the body appears to the compiler as a quoted constant."
576 (declare (debug (form &optional sexp
)))
577 (if (cl--compiling-file)
578 (let* ((temp (cl-gentemp "--cl-load-time--"))
579 (set `(set ',temp
,form
)))
580 (if (and (fboundp 'byte-compile-file-form-defmumble
)
581 (boundp 'this-kind
) (boundp 'that-one
))
582 (fset 'byte-compile-file-form
584 (fset 'byte-compile-file-form
585 ',(symbol-function 'byte-compile-file-form
))
586 (byte-compile-file-form ',set
)
587 (byte-compile-file-form form
)))
588 (print set
(symbol-value 'byte-compile--outbuffer
)))
589 `(symbol-value ',temp
))
593 ;;; Conditional control structures.
596 (defmacro cl-case
(expr &rest clauses
)
597 "Eval EXPR and choose among clauses on that value.
598 Each clause looks like (KEYLIST BODY...). EXPR is evaluated and compared
599 against each key in each KEYLIST; the corresponding BODY is evaluated.
600 If no clause succeeds, cl-case returns nil. A single atom may be used in
601 place of a KEYLIST of one atom. A KEYLIST of t or `otherwise' is
602 allowed only in the final clause, and matches if no other keys match.
603 Key values are compared by `eql'.
604 \n(fn EXPR (KEYLIST BODY...)...)"
605 (declare (indent 1) (debug (form &rest
(sexp body
))))
606 (let* ((temp (if (cl--simple-expr-p expr
3) expr
(make-symbol "--cl-var--")))
613 (cons (cond ((memq (car c
) '(t otherwise
)) t
)
614 ((eq (car c
) 'cl--ecase-error-flag
)
615 `(error "cl-ecase failed: %s, %s"
616 ,temp
',(reverse head-list
)))
618 (setq head-list
(append (car c
) head-list
))
619 `(cl-member ,temp
',(car c
)))
621 (if (memq (car c
) head-list
)
622 (error "Duplicate key in case: %s"
624 (push (car c
) head-list
)
625 `(eql ,temp
',(car c
))))
626 (or (cdr c
) '(nil)))))
628 (if (eq temp expr
) body
629 `(let ((,temp
,expr
)) ,body
))))
632 (defmacro cl-ecase
(expr &rest clauses
)
633 "Like `cl-case', but error if no cl-case fits.
634 `otherwise'-clauses are not allowed.
635 \n(fn EXPR (KEYLIST BODY...)...)"
636 (declare (indent 1) (debug cl-case
))
637 `(cl-case ,expr
,@clauses
(cl--ecase-error-flag)))
640 (defmacro cl-typecase
(expr &rest clauses
)
641 "Evals EXPR, chooses among clauses on that value.
642 Each clause looks like (TYPE BODY...). EXPR is evaluated and, if it
643 satisfies TYPE, the corresponding BODY is evaluated. If no clause succeeds,
644 cl-typecase returns nil. A TYPE of t or `otherwise' is allowed only in the
645 final clause, and matches if no other keys match.
646 \n(fn EXPR (TYPE BODY...)...)"
648 (debug (form &rest
([&or cl-type-spec
"otherwise"] body
))))
649 (let* ((temp (if (cl--simple-expr-p expr
3) expr
(make-symbol "--cl-var--")))
656 (cons (cond ((eq (car c
) 'otherwise
) t
)
657 ((eq (car c
) 'cl--ecase-error-flag
)
658 `(error "cl-etypecase failed: %s, %s"
659 ,temp
',(reverse type-list
)))
661 (push (car c
) type-list
)
662 (cl--make-type-test temp
(car c
))))
663 (or (cdr c
) '(nil)))))
665 (if (eq temp expr
) body
666 `(let ((,temp
,expr
)) ,body
))))
669 (defmacro cl-etypecase
(expr &rest clauses
)
670 "Like `cl-typecase', but error if no case fits.
671 `otherwise'-clauses are not allowed.
672 \n(fn EXPR (TYPE BODY...)...)"
673 (declare (indent 1) (debug cl-typecase
))
674 `(cl-typecase ,expr
,@clauses
(cl--ecase-error-flag)))
677 ;;; Blocks and exits.
680 (defmacro cl-block
(name &rest body
)
681 "Define a lexically-scoped block named NAME.
682 NAME may be any symbol. Code inside the BODY forms can call `cl-return-from'
683 to jump prematurely out of the block. This differs from `catch' and `throw'
684 in two respects: First, the NAME is an unevaluated symbol rather than a
685 quoted symbol or other form; and second, NAME is lexically rather than
686 dynamically scoped: Only references to it within BODY will work. These
687 references may appear inside macro expansions, but not inside functions
689 (declare (indent 1) (debug (symbolp body
)))
690 (if (cl--safe-expr-p `(progn ,@body
)) `(progn ,@body
)
692 (catch ',(intern (format "--cl-block-%s--" name
))
696 (defmacro cl-return
(&optional result
)
697 "Return from the block named nil.
698 This is equivalent to `(cl-return-from nil RESULT)'."
699 (declare (debug (&optional form
)))
700 `(cl-return-from nil
,result
))
703 (defmacro cl-return-from
(name &optional result
)
704 "Return from the block named NAME.
705 This jumps out to the innermost enclosing `(cl-block NAME ...)' form,
706 returning RESULT from that form (or nil if RESULT is omitted).
707 This is compatible with Common Lisp, but note that `defun' and
708 `defmacro' do not create implicit blocks as they do in Common Lisp."
709 (declare (indent 1) (debug (symbolp &optional form
)))
710 (let ((name2 (intern (format "--cl-block-%s--" name
))))
711 `(cl--block-throw ',name2
,result
)))
714 ;;; The "cl-loop" macro.
716 (defvar cl--loop-args
) (defvar cl--loop-accum-var
) (defvar cl--loop-accum-vars
)
717 (defvar cl--loop-bindings
) (defvar cl--loop-body
) (defvar cl--loop-destr-temps
)
718 (defvar cl--loop-finally
) (defvar cl--loop-finish-flag
)
719 (defvar cl--loop-first-flag
)
720 (defvar cl--loop-initially
) (defvar cl--loop-map-form
) (defvar cl--loop-name
)
721 (defvar cl--loop-result
) (defvar cl--loop-result-explicit
)
722 (defvar cl--loop-result-var
) (defvar cl--loop-steps
) (defvar cl--loop-symbol-macs
)
725 (defmacro cl-loop
(&rest loop-args
)
726 "The Common Lisp `cl-loop' macro.
728 for VAR from/upfrom/downfrom NUM to/upto/downto/above/below NUM by NUM,
729 for VAR in LIST by FUNC, for VAR on LIST by FUNC, for VAR = INIT then EXPR,
730 for VAR across ARRAY, repeat NUM, with VAR = INIT, while COND, until COND,
731 always COND, never COND, thereis COND, collect EXPR into VAR,
732 append EXPR into VAR, nconc EXPR into VAR, sum EXPR into VAR,
733 count EXPR into VAR, maximize EXPR into VAR, minimize EXPR into VAR,
734 if COND CLAUSE [and CLAUSE]... else CLAUSE [and CLAUSE...],
735 unless COND CLAUSE [and CLAUSE]... else CLAUSE [and CLAUSE...],
736 do EXPRS..., initially EXPRS..., finally EXPRS..., return EXPR,
737 finally return EXPR, named NAME.
740 (declare (debug (&rest
&or symbolp form
)))
741 (if (not (memq t
(mapcar 'symbolp
(delq nil
(delq t
(cl-copy-list loop-args
))))))
742 `(cl-block nil
(while t
,@loop-args
))
743 (let ((cl--loop-args loop-args
) (cl--loop-name nil
) (cl--loop-bindings nil
)
744 (cl--loop-body nil
) (cl--loop-steps nil
)
745 (cl--loop-result nil
) (cl--loop-result-explicit nil
)
746 (cl--loop-result-var nil
) (cl--loop-finish-flag nil
)
747 (cl--loop-accum-var nil
) (cl--loop-accum-vars nil
)
748 (cl--loop-initially nil
) (cl--loop-finally nil
)
749 (cl--loop-map-form nil
) (cl--loop-first-flag nil
)
750 (cl--loop-destr-temps nil
) (cl--loop-symbol-macs nil
))
751 (setq cl--loop-args
(append cl--loop-args
'(cl-end-loop)))
752 (while (not (eq (car cl--loop-args
) 'cl-end-loop
)) (cl-parse-loop-clause))
753 (if cl--loop-finish-flag
754 (push `((,cl--loop-finish-flag t
)) cl--loop-bindings
))
755 (if cl--loop-first-flag
756 (progn (push `((,cl--loop-first-flag t
)) cl--loop-bindings
)
757 (push `(setq ,cl--loop-first-flag nil
) cl--loop-steps
)))
758 (let* ((epilogue (nconc (nreverse cl--loop-finally
)
759 (list (or cl--loop-result-explicit cl--loop-result
))))
760 (ands (cl--loop-build-ands (nreverse cl--loop-body
)))
761 (while-body (nconc (cadr ands
) (nreverse cl--loop-steps
)))
763 (nreverse cl--loop-initially
)
764 (list (if cl--loop-map-form
765 `(cl-block --cl-finish--
767 (if (eq (car ands
) t
) while-body
768 (cons `(or ,(car ands
)
769 (cl-return-from --cl-finish--
772 '--cl-map cl--loop-map-form
))
773 `(while ,(car ands
) ,@while-body
)))
774 (if cl--loop-finish-flag
775 (if (equal epilogue
'(nil)) (list cl--loop-result-var
)
776 `((if ,cl--loop-finish-flag
777 (progn ,@epilogue
) ,cl--loop-result-var
)))
779 (if cl--loop-result-var
(push (list cl--loop-result-var
) cl--loop-bindings
))
780 (while cl--loop-bindings
781 (if (cdar cl--loop-bindings
)
782 (setq body
(list (cl--loop-let (pop cl--loop-bindings
) body t
)))
784 (while (and cl--loop-bindings
785 (not (cdar cl--loop-bindings
)))
786 (push (car (pop cl--loop-bindings
)) lets
))
787 (setq body
(list (cl--loop-let lets body nil
))))))
788 (if cl--loop-symbol-macs
789 (setq body
(list `(cl-symbol-macrolet ,cl--loop-symbol-macs
,@body
))))
790 `(cl-block ,cl--loop-name
,@body
)))))
792 ;; Below is a complete spec for cl-loop, in several parts that correspond
793 ;; to the syntax given in CLtL2. The specs do more than specify where
794 ;; the forms are; it also specifies, as much as Edebug allows, all the
795 ;; syntactically valid cl-loop clauses. The disadvantage of this
796 ;; completeness is rigidity, but the "for ... being" clause allows
797 ;; arbitrary extensions of the form: [symbolp &rest &or symbolp form].
799 ;; (def-edebug-spec cl-loop
800 ;; ([&optional ["named" symbolp]]
806 ;; loop-initial-final]
807 ;; [&rest loop-clause]
810 ;; (def-edebug-spec loop-with
813 ;; [&optional ["=" form]]
814 ;; &rest ["and" loop-var
816 ;; [&optional ["=" form]]]))
818 ;; (def-edebug-spec loop-for-as
819 ;; ([&or "for" "as"] loop-for-as-subclause
820 ;; &rest ["and" loop-for-as-subclause]))
822 ;; (def-edebug-spec loop-for-as-subclause
826 ;; [[&or "in" "on" "in-ref" "across-ref"]
827 ;; form &optional ["by" function-form]]
829 ;; ["=" form &optional ["then" form]]
832 ;; [&or "the" "each"]
834 ;; [[&or "element" "elements"]
835 ;; [&or "of" "in" "of-ref"] form
836 ;; &optional "using" ["index" symbolp]];; is this right?
837 ;; [[&or "hash-key" "hash-keys"
838 ;; "hash-value" "hash-values"]
840 ;; hash-table-p &optional ["using" ([&or "hash-value" "hash-values"
841 ;; "hash-key" "hash-keys"] sexp)]]
843 ;; [[&or "symbol" "present-symbol" "external-symbol"
844 ;; "symbols" "present-symbols" "external-symbols"]
845 ;; [&or "in" "of"] package-p]
847 ;; ;; Extensions for Emacs Lisp, including Lucid Emacs.
848 ;; [[&or "frame" "frames"
849 ;; "screen" "screens"
850 ;; "buffer" "buffers"]]
852 ;; [[&or "window" "windows"]
853 ;; [&or "of" "in"] form]
855 ;; [[&or "overlay" "overlays"
856 ;; "extent" "extents"]
857 ;; [&or "of" "in"] form
858 ;; &optional [[&or "from" "to"] form]]
860 ;; [[&or "interval" "intervals"]
861 ;; [&or "in" "of"] form
862 ;; &optional [[&or "from" "to"] form]
863 ;; ["property" form]]
865 ;; [[&or "key-code" "key-codes"
866 ;; "key-seq" "key-seqs"
867 ;; "key-binding" "key-bindings"]
868 ;; [&or "in" "of"] form
869 ;; &optional ["using" ([&or "key-code" "key-codes"
870 ;; "key-seq" "key-seqs"
871 ;; "key-binding" "key-bindings"]
873 ;; ;; For arbitrary extensions, recognize anything else.
874 ;; [symbolp &rest &or symbolp form]
877 ;; ;; arithmetic - must be last since all parts are optional.
878 ;; [[&optional [[&or "from" "downfrom" "upfrom"] form]]
879 ;; [&optional [[&or "to" "downto" "upto" "below" "above"] form]]
880 ;; [&optional ["by" form]]
883 ;; (def-edebug-spec loop-initial-final
885 ;; ;; [&optional &or "do" "doing"] ;; CLtL2 doesn't allow this.
886 ;; &rest loop-non-atomic-expr]
888 ;; [[&optional &or "do" "doing"] &rest loop-non-atomic-expr]
889 ;; ["return" form]]))
891 ;; (def-edebug-spec loop-and-clause
892 ;; (loop-clause &rest ["and" loop-clause]))
894 ;; (def-edebug-spec loop-clause
896 ;; [[&or "while" "until" "always" "never" "thereis"] form]
898 ;; [[&or "collect" "collecting"
899 ;; "append" "appending"
900 ;; "nconc" "nconcing"
901 ;; "concat" "vconcat"] form
902 ;; [&optional ["into" loop-var]]]
904 ;; [[&or "count" "counting"
906 ;; "maximize" "maximizing"
907 ;; "minimize" "minimizing"] form
908 ;; [&optional ["into" loop-var]]
911 ;; [[&or "if" "when" "unless"]
912 ;; form loop-and-clause
913 ;; [&optional ["else" loop-and-clause]]
914 ;; [&optional "end"]]
916 ;; [[&or "do" "doing"] &rest loop-non-atomic-expr]
919 ;; loop-initial-final
922 ;; (def-edebug-spec loop-non-atomic-expr
923 ;; ([¬ atom] form))
925 ;; (def-edebug-spec loop-var
926 ;; ;; The symbolp must be last alternative to recognize e.g. (a b . c)
928 ;; ;; (loop-var . [&or nil loop-var])
929 ;; ;; (symbolp . [&or nil loop-var])
930 ;; ;; (symbolp . loop-var)
931 ;; ;; (symbolp . (symbolp . [&or nil loop-var]))
932 ;; ;; (symbolp . (symbolp . loop-var))
933 ;; ;; (symbolp . (symbolp . symbolp)) == (symbolp symbolp . symbolp)
934 ;; (&or (loop-var . [&or nil loop-var]) [gate symbolp]))
936 ;; (def-edebug-spec loop-type-spec
937 ;; (&optional ["of-type" loop-d-type-spec]))
939 ;; (def-edebug-spec loop-d-type-spec
940 ;; (&or (loop-d-type-spec . [&or nil loop-d-type-spec]) cl-type-spec))
944 (defun cl-parse-loop-clause () ; uses loop-*
945 (let ((word (pop cl--loop-args
))
946 (hash-types '(hash-key hash-keys hash-value hash-values
))
947 (key-types '(key-code key-codes key-seq key-seqs
948 key-binding key-bindings
)))
951 ((null cl--loop-args
)
952 (error "Malformed `cl-loop' macro"))
955 (setq cl--loop-name
(pop cl--loop-args
)))
957 ((eq word
'initially
)
958 (if (memq (car cl--loop-args
) '(do doing
)) (pop cl--loop-args
))
959 (or (consp (car cl--loop-args
)) (error "Syntax error on `initially' clause"))
960 (while (consp (car cl--loop-args
))
961 (push (pop cl--loop-args
) cl--loop-initially
)))
964 (if (eq (car cl--loop-args
) 'return
)
965 (setq cl--loop-result-explicit
(or (cl-pop2 cl--loop-args
) '(quote nil
)))
966 (if (memq (car cl--loop-args
) '(do doing
)) (pop cl--loop-args
))
967 (or (consp (car cl--loop-args
)) (error "Syntax error on `finally' clause"))
968 (if (and (eq (caar cl--loop-args
) 'return
) (null cl--loop-name
))
969 (setq cl--loop-result-explicit
(or (nth 1 (pop cl--loop-args
)) '(quote nil
)))
970 (while (consp (car cl--loop-args
))
971 (push (pop cl--loop-args
) cl--loop-finally
)))))
973 ((memq word
'(for as
))
974 (let ((loop-for-bindings nil
) (loop-for-sets nil
) (loop-for-steps nil
)
977 ;; Use `cl-gensym' rather than `make-symbol'. It's important that
978 ;; (not (eq (symbol-name var1) (symbol-name var2))) because
979 ;; these vars get added to the macro-environment.
980 (let ((var (or (pop cl--loop-args
) (cl-gensym "--cl-var--"))))
981 (setq word
(pop cl--loop-args
))
982 (if (eq word
'being
) (setq word
(pop cl--loop-args
)))
983 (if (memq word
'(the each
)) (setq word
(pop cl--loop-args
)))
984 (if (memq word
'(buffer buffers
))
985 (setq word
'in cl--loop-args
(cons '(buffer-list) cl--loop-args
)))
988 ((memq word
'(from downfrom upfrom to downto upto
990 (push word cl--loop-args
)
991 (if (memq (car cl--loop-args
) '(downto above
))
992 (error "Must specify `from' value for downward cl-loop"))
993 (let* ((down (or (eq (car cl--loop-args
) 'downfrom
)
994 (memq (cl-caddr cl--loop-args
) '(downto above
))))
995 (excl (or (memq (car cl--loop-args
) '(above below
))
996 (memq (cl-caddr cl--loop-args
) '(above below
))))
997 (start (and (memq (car cl--loop-args
) '(from upfrom downfrom
))
998 (cl-pop2 cl--loop-args
)))
999 (end (and (memq (car cl--loop-args
)
1000 '(to upto downto above below
))
1001 (cl-pop2 cl--loop-args
)))
1002 (step (and (eq (car cl--loop-args
) 'by
) (cl-pop2 cl--loop-args
)))
1003 (end-var (and (not (macroexp-const-p end
))
1004 (make-symbol "--cl-var--")))
1005 (step-var (and (not (macroexp-const-p step
))
1006 (make-symbol "--cl-var--"))))
1007 (and step
(numberp step
) (<= step
0)
1008 (error "Loop `by' value is not positive: %s" step
))
1009 (push (list var
(or start
0)) loop-for-bindings
)
1010 (if end-var
(push (list end-var end
) loop-for-bindings
))
1011 (if step-var
(push (list step-var step
)
1015 (if down
(if excl
'> '>=) (if excl
'< '<=))
1016 var
(or end-var end
)) cl--loop-body
))
1017 (push (list var
(list (if down
'-
'+) var
1018 (or step-var step
1)))
1021 ((memq word
'(in in-ref on
))
1022 (let* ((on (eq word
'on
))
1023 (temp (if (and on
(symbolp var
))
1024 var
(make-symbol "--cl-var--"))))
1025 (push (list temp
(pop cl--loop-args
)) loop-for-bindings
)
1026 (push `(consp ,temp
) cl--loop-body
)
1027 (if (eq word
'in-ref
)
1028 (push (list var
`(car ,temp
)) cl--loop-symbol-macs
)
1031 (push (list var nil
) loop-for-bindings
)
1032 (push (list var
(if on temp
`(car ,temp
)))
1035 (if (eq (car cl--loop-args
) 'by
)
1036 (let ((step (cl-pop2 cl--loop-args
)))
1037 (if (and (memq (car-safe step
)
1040 (symbolp (nth 1 step
)))
1041 (list (nth 1 step
) temp
)
1042 `(funcall ,step
,temp
)))
1047 (let* ((start (pop cl--loop-args
))
1048 (then (if (eq (car cl--loop-args
) 'then
) (cl-pop2 cl--loop-args
) start
)))
1049 (push (list var nil
) loop-for-bindings
)
1050 (if (or ands
(eq (car cl--loop-args
) 'and
))
1053 (if ,(or cl--loop-first-flag
1054 (setq cl--loop-first-flag
1055 (make-symbol "--cl-var--")))
1058 (push (list var then
) loop-for-steps
))
1060 (if (eq start then
) start
1061 `(if ,(or cl--loop-first-flag
1062 (setq cl--loop-first-flag
1063 (make-symbol "--cl-var--")))
1067 ((memq word
'(across across-ref
))
1068 (let ((temp-vec (make-symbol "--cl-vec--"))
1069 (temp-idx (make-symbol "--cl-idx--")))
1070 (push (list temp-vec
(pop cl--loop-args
)) loop-for-bindings
)
1071 (push (list temp-idx -
1) loop-for-bindings
)
1072 (push `(< (setq ,temp-idx
(1+ ,temp-idx
))
1073 (length ,temp-vec
)) cl--loop-body
)
1074 (if (eq word
'across-ref
)
1075 (push (list var
`(aref ,temp-vec
,temp-idx
))
1076 cl--loop-symbol-macs
)
1077 (push (list var nil
) loop-for-bindings
)
1078 (push (list var
`(aref ,temp-vec
,temp-idx
))
1081 ((memq word
'(element elements
))
1082 (let ((ref (or (memq (car cl--loop-args
) '(in-ref of-ref
))
1083 (and (not (memq (car cl--loop-args
) '(in of
)))
1084 (error "Expected `of'"))))
1085 (seq (cl-pop2 cl--loop-args
))
1086 (temp-seq (make-symbol "--cl-seq--"))
1087 (temp-idx (if (eq (car cl--loop-args
) 'using
)
1088 (if (and (= (length (cadr cl--loop-args
)) 2)
1089 (eq (cl-caadr cl--loop-args
) 'index
))
1090 (cadr (cl-pop2 cl--loop-args
))
1091 (error "Bad `using' clause"))
1092 (make-symbol "--cl-idx--"))))
1093 (push (list temp-seq seq
) loop-for-bindings
)
1094 (push (list temp-idx
0) loop-for-bindings
)
1096 (let ((temp-len (make-symbol "--cl-len--")))
1097 (push (list temp-len
`(length ,temp-seq
))
1099 (push (list var
`(elt ,temp-seq
,temp-idx
))
1100 cl--loop-symbol-macs
)
1101 (push `(< ,temp-idx
,temp-len
) cl--loop-body
))
1102 (push (list var nil
) loop-for-bindings
)
1103 (push `(and ,temp-seq
1104 (or (consp ,temp-seq
)
1105 (< ,temp-idx
(length ,temp-seq
))))
1107 (push (list var
`(if (consp ,temp-seq
)
1109 (aref ,temp-seq
,temp-idx
)))
1111 (push (list temp-idx
`(1+ ,temp-idx
))
1114 ((memq word hash-types
)
1115 (or (memq (car cl--loop-args
) '(in of
)) (error "Expected `of'"))
1116 (let* ((table (cl-pop2 cl--loop-args
))
1117 (other (if (eq (car cl--loop-args
) 'using
)
1118 (if (and (= (length (cadr cl--loop-args
)) 2)
1119 (memq (cl-caadr cl--loop-args
) hash-types
)
1120 (not (eq (cl-caadr cl--loop-args
) word
)))
1121 (cadr (cl-pop2 cl--loop-args
))
1122 (error "Bad `using' clause"))
1123 (make-symbol "--cl-var--"))))
1124 (if (memq word
'(hash-value hash-values
))
1125 (setq var
(prog1 other
(setq other var
))))
1126 (setq cl--loop-map-form
1127 `(maphash (lambda (,var
,other
) . --cl-map
) ,table
))))
1129 ((memq word
'(symbol present-symbol external-symbol
1130 symbols present-symbols external-symbols
))
1131 (let ((ob (and (memq (car cl--loop-args
) '(in of
)) (cl-pop2 cl--loop-args
))))
1132 (setq cl--loop-map-form
1133 `(mapatoms (lambda (,var
) . --cl-map
) ,ob
))))
1135 ((memq word
'(overlay overlays extent extents
))
1136 (let ((buf nil
) (from nil
) (to nil
))
1137 (while (memq (car cl--loop-args
) '(in of from to
))
1138 (cond ((eq (car cl--loop-args
) 'from
) (setq from
(cl-pop2 cl--loop-args
)))
1139 ((eq (car cl--loop-args
) 'to
) (setq to
(cl-pop2 cl--loop-args
)))
1140 (t (setq buf
(cl-pop2 cl--loop-args
)))))
1141 (setq cl--loop-map-form
1143 (lambda (,var
,(make-symbol "--cl-var--"))
1144 (progn . --cl-map
) nil
)
1147 ((memq word
'(interval intervals
))
1148 (let ((buf nil
) (prop nil
) (from nil
) (to nil
)
1149 (var1 (make-symbol "--cl-var1--"))
1150 (var2 (make-symbol "--cl-var2--")))
1151 (while (memq (car cl--loop-args
) '(in of property from to
))
1152 (cond ((eq (car cl--loop-args
) 'from
) (setq from
(cl-pop2 cl--loop-args
)))
1153 ((eq (car cl--loop-args
) 'to
) (setq to
(cl-pop2 cl--loop-args
)))
1154 ((eq (car cl--loop-args
) 'property
)
1155 (setq prop
(cl-pop2 cl--loop-args
)))
1156 (t (setq buf
(cl-pop2 cl--loop-args
)))))
1157 (if (and (consp var
) (symbolp (car var
)) (symbolp (cdr var
)))
1158 (setq var1
(car var
) var2
(cdr var
))
1159 (push (list var
`(cons ,var1
,var2
)) loop-for-sets
))
1160 (setq cl--loop-map-form
1162 (lambda (,var1
,var2
) . --cl-map
)
1163 ,buf
,prop
,from
,to
))))
1165 ((memq word key-types
)
1166 (or (memq (car cl--loop-args
) '(in of
)) (error "Expected `of'"))
1167 (let ((cl-map (cl-pop2 cl--loop-args
))
1168 (other (if (eq (car cl--loop-args
) 'using
)
1169 (if (and (= (length (cadr cl--loop-args
)) 2)
1170 (memq (cl-caadr cl--loop-args
) key-types
)
1171 (not (eq (cl-caadr cl--loop-args
) word
)))
1172 (cadr (cl-pop2 cl--loop-args
))
1173 (error "Bad `using' clause"))
1174 (make-symbol "--cl-var--"))))
1175 (if (memq word
'(key-binding key-bindings
))
1176 (setq var
(prog1 other
(setq other var
))))
1177 (setq cl--loop-map-form
1178 `(,(if (memq word
'(key-seq key-seqs
))
1179 'cl--map-keymap-recursively
'map-keymap
)
1180 (lambda (,var
,other
) . --cl-map
) ,cl-map
))))
1182 ((memq word
'(frame frames screen screens
))
1183 (let ((temp (make-symbol "--cl-var--")))
1184 (push (list var
'(selected-frame))
1186 (push (list temp nil
) loop-for-bindings
)
1187 (push `(prog1 (not (eq ,var
,temp
))
1188 (or ,temp
(setq ,temp
,var
)))
1190 (push (list var
`(next-frame ,var
))
1193 ((memq word
'(window windows
))
1194 (let ((scr (and (memq (car cl--loop-args
) '(in of
)) (cl-pop2 cl--loop-args
)))
1195 (temp (make-symbol "--cl-var--"))
1196 (minip (make-symbol "--cl-minip--")))
1197 (push (list var
(if scr
1198 `(frame-selected-window ,scr
)
1199 '(selected-window)))
1201 ;; If we started in the minibuffer, we need to
1202 ;; ensure that next-window will bring us back there
1203 ;; at some point. (Bug#7492).
1204 ;; (Consider using walk-windows instead of cl-loop if
1205 ;; you care about such things.)
1206 (push (list minip
`(minibufferp (window-buffer ,var
)))
1208 (push (list temp nil
) loop-for-bindings
)
1209 (push `(prog1 (not (eq ,var
,temp
))
1210 (or ,temp
(setq ,temp
,var
)))
1212 (push (list var
`(next-window ,var
,minip
))
1216 (let ((handler (and (symbolp word
)
1217 (get word
'cl--loop-for-handler
))))
1219 (funcall handler var
)
1220 (error "Expected a `for' preposition, found %s" word
)))))
1221 (eq (car cl--loop-args
) 'and
))
1223 (pop cl--loop-args
))
1224 (if (and ands loop-for-bindings
)
1225 (push (nreverse loop-for-bindings
) cl--loop-bindings
)
1226 (setq cl--loop-bindings
(nconc (mapcar 'list loop-for-bindings
)
1227 cl--loop-bindings
)))
1230 ,(cl--loop-let (nreverse loop-for-sets
) 'setq ands
)
1233 (push (cons (if ands
'cl-psetq
'setq
)
1234 (apply 'append
(nreverse loop-for-steps
)))
1238 (let ((temp (make-symbol "--cl-var--")))
1239 (push (list (list temp
(pop cl--loop-args
))) cl--loop-bindings
)
1240 (push `(>= (setq ,temp
(1- ,temp
)) 0) cl--loop-body
)))
1242 ((memq word
'(collect collecting
))
1243 (let ((what (pop cl--loop-args
))
1244 (var (cl--loop-handle-accum nil
'nreverse
)))
1245 (if (eq var cl--loop-accum-var
)
1246 (push `(progn (push ,what
,var
) t
) cl--loop-body
)
1248 (setq ,var
(nconc ,var
(list ,what
)))
1249 t
) cl--loop-body
))))
1251 ((memq word
'(nconc nconcing append appending
))
1252 (let ((what (pop cl--loop-args
))
1253 (var (cl--loop-handle-accum nil
'nreverse
)))
1256 ,(if (eq var cl--loop-accum-var
)
1258 (,(if (memq word
'(nconc nconcing
))
1259 #'nreverse
#'reverse
)
1262 `(,(if (memq word
'(nconc nconcing
))
1264 ,var
,what
))) t
) cl--loop-body
)))
1266 ((memq word
'(concat concating
))
1267 (let ((what (pop cl--loop-args
))
1268 (var (cl--loop-handle-accum "")))
1269 (push `(progn (cl-callf concat
,var
,what
) t
) cl--loop-body
)))
1271 ((memq word
'(vconcat vconcating
))
1272 (let ((what (pop cl--loop-args
))
1273 (var (cl--loop-handle-accum [])))
1274 (push `(progn (cl-callf vconcat
,var
,what
) t
) cl--loop-body
)))
1276 ((memq word
'(sum summing
))
1277 (let ((what (pop cl--loop-args
))
1278 (var (cl--loop-handle-accum 0)))
1279 (push `(progn (cl-incf ,var
,what
) t
) cl--loop-body
)))
1281 ((memq word
'(count counting
))
1282 (let ((what (pop cl--loop-args
))
1283 (var (cl--loop-handle-accum 0)))
1284 (push `(progn (if ,what
(cl-incf ,var
)) t
) cl--loop-body
)))
1286 ((memq word
'(minimize minimizing maximize maximizing
))
1287 (let* ((what (pop cl--loop-args
))
1288 (temp (if (cl--simple-expr-p what
) what
(make-symbol "--cl-var--")))
1289 (var (cl--loop-handle-accum nil
))
1290 (func (intern (substring (symbol-name word
) 0 3)))
1291 (set `(setq ,var
(if ,var
(,func
,var
,temp
) ,temp
))))
1292 (push `(progn ,(if (eq temp what
) set
1293 `(let ((,temp
,what
)) ,set
))
1297 (let ((bindings nil
))
1298 (while (progn (push (list (pop cl--loop-args
)
1299 (and (eq (car cl--loop-args
) '=) (cl-pop2 cl--loop-args
)))
1301 (eq (car cl--loop-args
) 'and
))
1302 (pop cl--loop-args
))
1303 (push (nreverse bindings
) cl--loop-bindings
)))
1306 (push (pop cl--loop-args
) cl--loop-body
))
1309 (push `(not ,(pop cl--loop-args
)) cl--loop-body
))
1312 (or cl--loop-finish-flag
(setq cl--loop-finish-flag
(make-symbol "--cl-flag--")))
1313 (push `(setq ,cl--loop-finish-flag
,(pop cl--loop-args
)) cl--loop-body
)
1314 (setq cl--loop-result t
))
1317 (or cl--loop-finish-flag
(setq cl--loop-finish-flag
(make-symbol "--cl-flag--")))
1318 (push `(setq ,cl--loop-finish-flag
(not ,(pop cl--loop-args
)))
1320 (setq cl--loop-result t
))
1323 (or cl--loop-finish-flag
(setq cl--loop-finish-flag
(make-symbol "--cl-flag--")))
1324 (or cl--loop-result-var
(setq cl--loop-result-var
(make-symbol "--cl-var--")))
1325 (push `(setq ,cl--loop-finish-flag
1326 (not (setq ,cl--loop-result-var
,(pop cl--loop-args
))))
1329 ((memq word
'(if when unless
))
1330 (let* ((cond (pop cl--loop-args
))
1331 (then (let ((cl--loop-body nil
))
1332 (cl-parse-loop-clause)
1333 (cl--loop-build-ands (nreverse cl--loop-body
))))
1334 (else (let ((cl--loop-body nil
))
1335 (if (eq (car cl--loop-args
) 'else
)
1336 (progn (pop cl--loop-args
) (cl-parse-loop-clause)))
1337 (cl--loop-build-ands (nreverse cl--loop-body
))))
1338 (simple (and (eq (car then
) t
) (eq (car else
) t
))))
1339 (if (eq (car cl--loop-args
) 'end
) (pop cl--loop-args
))
1340 (if (eq word
'unless
) (setq then
(prog1 else
(setq else then
))))
1341 (let ((form (cons (if simple
(cons 'progn
(nth 1 then
)) (nth 2 then
))
1342 (if simple
(nth 1 else
) (list (nth 2 else
))))))
1343 (if (cl--expr-contains form
'it
)
1344 (let ((temp (make-symbol "--cl-var--")))
1345 (push (list temp
) cl--loop-bindings
)
1346 (setq form
`(if (setq ,temp
,cond
)
1347 ,@(cl-subst temp
'it form
))))
1348 (setq form
`(if ,cond
,@form
)))
1349 (push (if simple
`(progn ,form t
) form
) cl--loop-body
))))
1351 ((memq word
'(do doing
))
1353 (or (consp (car cl--loop-args
)) (error "Syntax error on `do' clause"))
1354 (while (consp (car cl--loop-args
)) (push (pop cl--loop-args
) body
))
1355 (push (cons 'progn
(nreverse (cons t body
))) cl--loop-body
)))
1358 (or cl--loop-finish-flag
(setq cl--loop-finish-flag
(make-symbol "--cl-var--")))
1359 (or cl--loop-result-var
(setq cl--loop-result-var
(make-symbol "--cl-var--")))
1360 (push `(setq ,cl--loop-result-var
,(pop cl--loop-args
)
1361 ,cl--loop-finish-flag nil
) cl--loop-body
))
1364 (let ((handler (and (symbolp word
) (get word
'cl--loop-handler
))))
1365 (or handler
(error "Expected a cl-loop keyword, found %s" word
))
1366 (funcall handler
))))
1367 (if (eq (car cl--loop-args
) 'and
)
1368 (progn (pop cl--loop-args
) (cl-parse-loop-clause)))))
1370 (defun cl--loop-let (specs body par
) ; uses loop-*
1371 (let ((p specs
) (temps nil
) (new nil
))
1372 (while (and p
(or (symbolp (car-safe (car p
))) (null (cl-cadar p
))))
1376 (setq par nil p specs
)
1378 (or (macroexp-const-p (cl-cadar p
))
1379 (let ((temp (make-symbol "--cl-var--")))
1380 (push (list temp
(cl-cadar p
)) temps
)
1381 (setcar (cdar p
) temp
)))
1384 (if (and (consp (car specs
)) (listp (caar specs
)))
1385 (let* ((spec (caar specs
)) (nspecs nil
)
1386 (expr (cadr (pop specs
)))
1387 (temp (cdr (or (assq spec cl--loop-destr-temps
)
1388 (car (push (cons spec
(or (last spec
0)
1389 (make-symbol "--cl-var--")))
1390 cl--loop-destr-temps
))))))
1391 (push (list temp expr
) new
)
1393 (push (list (pop spec
)
1394 (and expr
(list (if spec
'pop
'car
) temp
)))
1396 (setq specs
(nconc (nreverse nspecs
) specs
)))
1397 (push (pop specs
) new
)))
1399 (let ((set (cons (if par
'cl-psetq
'setq
) (apply 'nconc
(nreverse new
)))))
1400 (if temps
`(let* ,(nreverse temps
) ,set
) set
))
1401 `(,(if par
'let
'let
*)
1402 ,(nconc (nreverse temps
) (nreverse new
)) ,@body
))))
1404 (defun cl--loop-handle-accum (def &optional func
) ; uses loop-*
1405 (if (eq (car cl--loop-args
) 'into
)
1406 (let ((var (cl-pop2 cl--loop-args
)))
1407 (or (memq var cl--loop-accum-vars
)
1408 (progn (push (list (list var def
)) cl--loop-bindings
)
1409 (push var cl--loop-accum-vars
)))
1411 (or cl--loop-accum-var
1413 (push (list (list (setq cl--loop-accum-var
(make-symbol "--cl-var--")) def
))
1415 (setq cl--loop-result
(if func
(list func cl--loop-accum-var
)
1416 cl--loop-accum-var
))
1417 cl--loop-accum-var
))))
1419 (defun cl--loop-build-ands (clauses)
1423 (if (and (eq (car-safe (car clauses
)) 'progn
)
1424 (eq (car (last (car clauses
))) t
))
1426 (setq clauses
(cons (nconc (butlast (car clauses
))
1427 (if (eq (car-safe (cadr clauses
))
1430 (list (cadr clauses
))))
1432 (setq body
(cdr (butlast (pop clauses
)))))
1433 (push (pop clauses
) ands
)))
1434 (setq ands
(or (nreverse ands
) (list t
)))
1435 (list (if (cdr ands
) (cons 'and ands
) (car ands
))
1437 (let ((full (if body
1438 (append ands
(list (cons 'progn
(append body
'(t)))))
1440 (if (cdr full
) (cons 'and full
) (car full
))))))
1443 ;;; Other iteration control structures.
1446 (defmacro cl-do
(steps endtest
&rest body
)
1447 "The Common Lisp `cl-do' loop.
1449 \(fn ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)"
1452 ((&rest
&or symbolp
(symbolp &optional form form
))
1454 cl-declarations body
)))
1455 (cl-expand-do-loop steps endtest body nil
))
1458 (defmacro cl-do
* (steps endtest
&rest body
)
1459 "The Common Lisp `cl-do*' loop.
1461 \(fn ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)"
1462 (declare (indent 2) (debug cl-do
))
1463 (cl-expand-do-loop steps endtest body t
))
1465 (defun cl-expand-do-loop (steps endtest body star
)
1467 (,(if star
'let
* 'let
)
1468 ,(mapcar (lambda (c) (if (consp c
) (list (car c
) (nth 1 c
)) c
))
1470 (while (not ,(car endtest
))
1472 ,@(let ((sets (mapcar (lambda (c)
1473 (and (consp c
) (cdr (cdr c
))
1474 (list (car c
) (nth 2 c
))))
1476 (setq sets
(delq nil sets
))
1478 (list (cons (if (or star
(not (cdr sets
)))
1480 (apply 'append sets
))))))
1481 ,@(or (cdr endtest
) '(nil)))))
1484 (defmacro cl-dolist
(spec &rest body
)
1486 Evaluate BODY with VAR bound to each `car' from LIST, in turn.
1487 Then evaluate RESULT to get return value, default nil.
1488 An implicit nil block is established around the loop.
1490 \(fn (VAR LIST [RESULT]) BODY...)"
1491 (declare (debug ((symbolp form
&optional form
) cl-declarations body
)))
1492 (let ((temp (make-symbol "--cl-dolist-temp--")))
1493 ;; FIXME: Copy&pasted from subr.el.
1495 ;; This is not a reliable test, but it does not matter because both
1496 ;; semantics are acceptable, tho one is slightly faster with dynamic
1497 ;; scoping and the other is slightly faster (and has cleaner semantics)
1498 ;; with lexical scoping.
1499 ,(if lexical-binding
1500 `(let ((,temp
,(nth 1 spec
)))
1502 (let ((,(car spec
) (car ,temp
)))
1504 (setq ,temp
(cdr ,temp
))))
1505 ,@(if (cdr (cdr spec
))
1506 ;; FIXME: This let often leads to "unused var" warnings.
1507 `((let ((,(car spec
) nil
)) ,@(cdr (cdr spec
))))))
1508 `(let ((,temp
,(nth 1 spec
))
1511 (setq ,(car spec
) (car ,temp
))
1513 (setq ,temp
(cdr ,temp
)))
1514 ,@(if (cdr (cdr spec
))
1515 `((setq ,(car spec
) nil
) ,@(cddr spec
))))))))
1518 (defmacro cl-dotimes
(spec &rest body
)
1519 "Loop a certain number of times.
1520 Evaluate BODY with VAR bound to successive integers from 0, inclusive,
1521 to COUNT, exclusive. Then evaluate RESULT to get return value, default
1524 \(fn (VAR COUNT [RESULT]) BODY...)"
1525 (declare (debug cl-dolist
))
1526 (let ((temp (make-symbol "--cl-dotimes-temp--"))
1528 ;; FIXME: Copy&pasted from subr.el.
1530 ;; This is not a reliable test, but it does not matter because both
1531 ;; semantics are acceptable, tho one is slightly faster with dynamic
1532 ;; scoping and the other has cleaner semantics.
1533 ,(if lexical-binding
1534 (let ((counter '--dotimes-counter--
))
1537 (while (< ,counter
,temp
)
1538 (let ((,(car spec
) ,counter
))
1540 (setq ,counter
(1+ ,counter
)))
1542 ;; FIXME: This let often leads to "unused var" warnings.
1543 `((let ((,(car spec
) ,counter
)) ,@(cddr spec
))))))
1546 (while (< ,(car spec
) ,temp
)
1548 (cl-incf ,(car spec
)))
1549 ,@(cdr (cdr spec
)))))))
1552 (defmacro cl-do-symbols
(spec &rest body
)
1553 "Loop over all symbols.
1554 Evaluate BODY with VAR bound to each interned symbol, or to each symbol
1557 \(fn (VAR [OBARRAY [RESULT]]) BODY...)"
1559 (debug ((symbolp &optional form form
) cl-declarations body
)))
1560 ;; Apparently this doesn't have an implicit block.
1563 (mapatoms #'(lambda (,(car spec
)) ,@body
)
1564 ,@(and (cadr spec
) (list (cadr spec
))))
1568 (defmacro cl-do-all-symbols
(spec &rest body
)
1569 (declare (indent 1) (debug ((symbolp &optional form
) cl-declarations body
)))
1570 `(cl-do-symbols (,(car spec
) nil
,(cadr spec
)) ,@body
))
1576 (defmacro cl-psetq
(&rest args
)
1577 "Set SYMs to the values VALs in parallel.
1578 This is like `setq', except that all VAL forms are evaluated (in order)
1579 before assigning any symbols SYM to the corresponding values.
1581 \(fn SYM VAL SYM VAL ...)"
1582 (declare (debug setq
))
1583 (cons 'cl-psetf args
))
1586 ;;; Binding control structures.
1589 (defmacro cl-progv
(symbols values
&rest body
)
1590 "Bind SYMBOLS to VALUES dynamically in BODY.
1591 The forms SYMBOLS and VALUES are evaluated, and must evaluate to lists.
1592 Each symbol in the first list is bound to the corresponding value in the
1593 second list (or made unbound if VALUES is shorter than SYMBOLS); then the
1594 BODY forms are executed and their result is returned. This is much like
1595 a `let' form, except that the list of symbols can be computed at run-time."
1596 (declare (indent 2) (debug (form form body
)))
1597 `(let ((cl--progv-save nil
))
1599 (progn (cl--progv-before ,symbols
,values
) ,@body
)
1600 (cl--progv-after))))
1602 (defvar cl--labels-convert-cache nil
)
1604 (defun cl--labels-convert (f)
1605 "Special macro-expander to rename (function F) references in `cl-labels'."
1607 ;; ¡¡Big Ugly Hack!! We can't use a compiler-macro because those are checked
1608 ;; *after* handling `function', but we want to stop macroexpansion from
1609 ;; being applied infinitely, so we use a cache to return the exact `form'
1610 ;; being expanded even though we don't receive it.
1611 ((eq f
(car cl--labels-convert-cache
)) (cdr cl--labels-convert-cache
))
1613 (let ((found (assq f macroexpand-all-environment
)))
1614 (if (and found
(ignore-errors
1615 (eq (cadr (cl-caddr found
)) 'cl-labels-args
)))
1616 (cadr (cl-caddr (cl-cadddr found
)))
1617 (let ((res `(function ,f
)))
1618 (setq cl--labels-convert-cache
(cons f res
))
1621 ;;; This should really have some way to shadow 'byte-compile properties, etc.
1623 (defmacro cl-flet
(bindings &rest body
)
1624 "Make temporary function definitions.
1625 Like `cl-labels' but the definitions are not recursive.
1627 \(fn ((FUNC ARGLIST BODY...) ...) FORM...)"
1628 (declare (indent 1) (debug ((&rest
(cl-defun)) cl-declarations body
)))
1629 (let ((binds ()) (newenv macroexpand-all-environment
))
1630 (dolist (binding bindings
)
1631 (let ((var (make-symbol (format "--cl-%s--" (car binding
)))))
1632 (push (list var
`(cl-function (lambda .
,(cdr binding
)))) binds
)
1633 (push (cons (car binding
)
1634 `(lambda (&rest cl-labels-args
)
1635 (cl-list* 'funcall
',var
1638 `(let ,(nreverse binds
)
1642 ;; Don't override lexical-let's macro-expander.
1643 (if (assq 'function newenv
) newenv
1644 (cons (cons 'function
#'cl--labels-convert
) newenv
)))))))
1647 (defmacro cl-labels
(bindings &rest body
)
1648 "Make temporary function bindings.
1649 The bindings can be recursive. Assumes the use of `lexical-binding'.
1651 \(fn ((FUNC ARGLIST BODY...) ...) FORM...)"
1652 (declare (indent 1) (debug cl-flet
))
1653 (let ((binds ()) (newenv macroexpand-all-environment
))
1654 (dolist (binding bindings
)
1655 (let ((var (make-symbol (format "--cl-%s--" (car binding
)))))
1656 (push (list var
`(cl-function (lambda .
,(cdr binding
)))) binds
)
1657 (push (cons (car binding
)
1658 `(lambda (&rest cl-labels-args
)
1659 (cl-list* 'funcall
',var
1662 (macroexpand-all `(letrec ,(nreverse binds
) ,@body
)
1663 ;; Don't override lexical-let's macro-expander.
1664 (if (assq 'function newenv
) newenv
1665 (cons (cons 'function
#'cl--labels-convert
) newenv
)))))
1667 ;; The following ought to have a better definition for use with newer
1670 (defmacro cl-macrolet
(bindings &rest body
)
1671 "Make temporary macro definitions.
1672 This is like `cl-flet', but for macros instead of functions.
1674 \(fn ((NAME ARGLIST BODY...) ...) FORM...)"
1677 ((&rest
(&define name
(&rest arg
) cl-declarations-or-string
1679 cl-declarations body
)))
1681 `(cl-macrolet (,(car bindings
)) (cl-macrolet ,(cdr bindings
) ,@body
))
1682 (if (null bindings
) (cons 'progn body
)
1683 (let* ((name (caar bindings
))
1684 (res (cl--transform-lambda (cdar bindings
) name
)))
1686 (macroexpand-all (cons 'progn body
)
1687 (cons (cons name
`(lambda ,@(cdr res
)))
1688 macroexpand-all-environment
))))))
1690 (defconst cl--old-macroexpand
1691 (if (and (boundp 'cl--old-macroexpand
)
1692 (eq (symbol-function 'macroexpand
)
1693 #'cl--sm-macroexpand
))
1695 (symbol-function 'macroexpand
)))
1697 (defun cl--sm-macroexpand (cl-macro &optional cl-env
)
1698 "Special macro expander used inside `cl-symbol-macrolet'.
1699 This function replaces `macroexpand' during macro expansion
1700 of `cl-symbol-macrolet', and does the same thing as `macroexpand'
1701 except that it additionally expands symbol macros."
1702 (let ((macroexpand-all-environment cl-env
))
1705 (setq cl-macro
(funcall cl--old-macroexpand cl-macro cl-env
))
1708 ;; Perform symbol-macro expansion.
1709 (when (cdr (assq (symbol-name cl-macro
) cl-env
))
1710 (setq cl-macro
(cadr (assq (symbol-name cl-macro
) cl-env
)))))
1711 ((eq 'setq
(car-safe cl-macro
))
1712 ;; Convert setq to setf if required by symbol-macro expansion.
1713 (let* ((args (mapcar (lambda (f) (cl--sm-macroexpand f cl-env
))
1716 (while (and p
(symbolp (car p
))) (setq p
(cddr p
)))
1717 (if p
(setq cl-macro
(cons 'setf args
))
1718 (setq cl-macro
(cons 'setq args
))
1719 ;; Don't loop further.
1724 (defmacro cl-symbol-macrolet
(bindings &rest body
)
1725 "Make symbol macro definitions.
1726 Within the body FORMs, references to the variable NAME will be replaced
1727 by EXPANSION, and (setq NAME ...) will act like (setf EXPANSION ...).
1729 \(fn ((NAME EXPANSION) ...) FORM...)"
1730 (declare (indent 1) (debug ((&rest
(symbol sexp
)) cl-declarations body
)))
1733 `(cl-symbol-macrolet (,(car bindings
))
1734 (cl-symbol-macrolet ,(cdr bindings
) ,@body
)))
1735 ((null bindings
) (macroexp-progn body
))
1737 (let ((previous-macroexpand (symbol-function 'macroexpand
)))
1740 (fset 'macroexpand
#'cl--sm-macroexpand
)
1741 ;; FIXME: For N bindings, this will traverse `body' N times!
1742 (macroexpand-all (cons 'progn body
)
1743 (cons (list (symbol-name (caar bindings
))
1744 (cl-cadar bindings
))
1745 macroexpand-all-environment
)))
1746 (fset 'macroexpand previous-macroexpand
))))))
1748 ;;; Multiple values.
1751 (defmacro cl-multiple-value-bind
(vars form
&rest body
)
1752 "Collect multiple return values.
1753 FORM must return a list; the BODY is then executed with the first N elements
1754 of this list bound (`let'-style) to each of the symbols SYM in turn. This
1755 is analogous to the Common Lisp `cl-multiple-value-bind' macro, using lists to
1756 simulate true multiple return values. For compatibility, (cl-values A B C) is
1757 a synonym for (list A B C).
1759 \(fn (SYM...) FORM BODY)"
1760 (declare (indent 2) (debug ((&rest symbolp
) form body
)))
1761 (let ((temp (make-symbol "--cl-var--")) (n -
1))
1762 `(let* ((,temp
,form
)
1763 ,@(mapcar (lambda (v)
1764 (list v
`(nth ,(setq n
(1+ n
)) ,temp
)))
1769 (defmacro cl-multiple-value-setq
(vars form
)
1770 "Collect multiple return values.
1771 FORM must return a list; the first N elements of this list are stored in
1772 each of the symbols SYM in turn. This is analogous to the Common Lisp
1773 `cl-multiple-value-setq' macro, using lists to simulate true multiple return
1774 values. For compatibility, (cl-values A B C) is a synonym for (list A B C).
1776 \(fn (SYM...) FORM)"
1777 (declare (indent 1) (debug ((&rest symbolp
) form
)))
1778 (cond ((null vars
) `(progn ,form nil
))
1779 ((null (cdr vars
)) `(setq ,(car vars
) (car ,form
)))
1781 (let* ((temp (make-symbol "--cl-var--")) (n 0))
1782 `(let ((,temp
,form
))
1783 (prog1 (setq ,(pop vars
) (car ,temp
))
1784 (setq ,@(apply #'nconc
1786 (list v
`(nth ,(setq n
(1+ n
))
1794 (defmacro cl-locally
(&rest body
)
1798 (defmacro cl-the
(_type form
)
1799 (declare (indent 1) (debug (cl-type-spec form
)))
1802 (defvar cl-proclaim-history t
) ; for future compilers
1803 (defvar cl-declare-stack t
) ; for future compilers
1805 (defun cl-do-proclaim (spec hist
)
1806 (and hist
(listp cl-proclaim-history
) (push spec cl-proclaim-history
))
1807 (cond ((eq (car-safe spec
) 'special
)
1808 (if (boundp 'byte-compile-bound-variables
)
1809 (setq byte-compile-bound-variables
1810 (append (cdr spec
) byte-compile-bound-variables
))))
1812 ((eq (car-safe spec
) 'inline
)
1813 (while (setq spec
(cdr spec
))
1814 (or (memq (get (car spec
) 'byte-optimizer
)
1815 '(nil byte-compile-inline-expand
))
1816 (error "%s already has a byte-optimizer, can't make it inline"
1818 (put (car spec
) 'byte-optimizer
'byte-compile-inline-expand
)))
1820 ((eq (car-safe spec
) 'notinline
)
1821 (while (setq spec
(cdr spec
))
1822 (if (eq (get (car spec
) 'byte-optimizer
)
1823 'byte-compile-inline-expand
)
1824 (put (car spec
) 'byte-optimizer nil
))))
1826 ((eq (car-safe spec
) 'optimize
)
1827 (let ((speed (assq (nth 1 (assq 'speed
(cdr spec
)))
1828 '((0 nil
) (1 t
) (2 t
) (3 t
))))
1829 (safety (assq (nth 1 (assq 'safety
(cdr spec
)))
1830 '((0 t
) (1 t
) (2 t
) (3 nil
)))))
1831 (if speed
(setq cl-optimize-speed
(car speed
)
1832 byte-optimize
(nth 1 speed
)))
1833 (if safety
(setq cl-optimize-safety
(car safety
)
1834 byte-compile-delete-errors
(nth 1 safety
)))))
1836 ((and (eq (car-safe spec
) 'warn
) (boundp 'byte-compile-warnings
))
1837 (while (setq spec
(cdr spec
))
1838 (if (consp (car spec
))
1839 (if (eq (cl-cadar spec
) 0)
1840 (byte-compile-disable-warning (caar spec
))
1841 (byte-compile-enable-warning (caar spec
)))))))
1844 ;;; Process any proclamations made before cl-macs was loaded.
1845 (defvar cl-proclaims-deferred
)
1846 (let ((p (reverse cl-proclaims-deferred
)))
1847 (while p
(cl-do-proclaim (pop p
) t
))
1848 (setq cl-proclaims-deferred nil
))
1851 (defmacro cl-declare
(&rest specs
)
1852 "Declare SPECS about the current function while compiling.
1855 \(cl-declare (warn 0))
1857 will turn off byte-compile warnings in the function.
1858 See Info node `(cl)Declarations' for details."
1859 (if (cl--compiling-file)
1861 (if (listp cl-declare-stack
) (push (car specs
) cl-declare-stack
))
1862 (cl-do-proclaim (pop specs
) nil
)))
1867 ;;; Generalized variables.
1869 ;;; Some standard place types from Common Lisp.
1870 (gv-define-setter cl-get
(store x y
&optional d
) `(put ,x
,y
,store
))
1871 (gv-define-setter cl-subseq
(new seq start
&optional end
)
1872 `(progn (cl-replace ,seq
,new
:start1
,start
:end1
,end
) ,new
))
1874 ;;; Various car/cdr aliases. Note that `cadr' is handled specially.
1875 (gv-define-setter cl-fourth
(store x
) `(setcar (cl-cdddr ,x
) ,store
))
1876 (gv-define-setter cl-fifth
(store x
) `(setcar (nthcdr 4 ,x
) ,store
))
1877 (gv-define-setter cl-sixth
(store x
) `(setcar (nthcdr 5 ,x
) ,store
))
1878 (gv-define-setter cl-seventh
(store x
) `(setcar (nthcdr 6 ,x
) ,store
))
1879 (gv-define-setter cl-eighth
(store x
) `(setcar (nthcdr 7 ,x
) ,store
))
1880 (gv-define-setter cl-ninth
(store x
) `(setcar (nthcdr 8 ,x
) ,store
))
1881 (gv-define-setter cl-tenth
(store x
) `(setcar (nthcdr 9 ,x
) ,store
))
1883 ;;; Some more Emacs-related place types.
1884 (gv-define-simple-setter buffer-file-name set-visited-file-name t
)
1885 (gv-define-setter buffer-modified-p
(flag &optional buf
)
1886 `(with-current-buffer ,buf
1887 (set-buffer-modified-p ,flag
)))
1888 (gv-define-simple-setter buffer-name rename-buffer t
)
1889 (gv-define-setter buffer-string
(store)
1890 `(progn (erase-buffer) (insert ,store
)))
1891 (gv-define-simple-setter buffer-substring cl--set-buffer-substring
)
1892 (gv-define-simple-setter current-buffer set-buffer
)
1893 (gv-define-simple-setter current-case-table set-case-table
)
1894 (gv-define-simple-setter current-column move-to-column t
)
1895 (gv-define-simple-setter current-global-map use-global-map t
)
1896 (gv-define-setter current-input-mode
(store)
1897 `(progn (apply #'set-input-mode
,store
) ,store
))
1898 (gv-define-simple-setter current-local-map use-local-map t
)
1899 (gv-define-simple-setter current-window-configuration set-window-configuration t
)
1900 (gv-define-simple-setter default-file-modes set-default-file-modes t
)
1901 (gv-define-simple-setter documentation-property put
)
1902 (gv-define-setter face-background
(x f
&optional s
) `(set-face-background ,f
,x
,s
))
1903 (gv-define-setter face-background-pixmap
(x f
&optional s
)
1904 `(set-face-background-pixmap ,f
,x
,s
))
1905 (gv-define-setter face-font
(x f
&optional s
) `(set-face-font ,f
,x
,s
))
1906 (gv-define-setter face-foreground
(x f
&optional s
) `(set-face-foreground ,f
,x
,s
))
1907 (gv-define-setter face-underline-p
(x f
&optional s
)
1908 `(set-face-underline-p ,f
,x
,s
))
1909 (gv-define-simple-setter file-modes set-file-modes t
)
1910 (gv-define-simple-setter frame-height set-screen-height t
)
1911 (gv-define-simple-setter frame-parameters modify-frame-parameters t
)
1912 (gv-define-simple-setter frame-visible-p cl--set-frame-visible-p
)
1913 (gv-define-simple-setter frame-width set-screen-width t
)
1914 (gv-define-simple-setter getenv setenv t
)
1915 (gv-define-simple-setter get-register set-register
)
1916 (gv-define-simple-setter global-key-binding global-set-key
)
1917 (gv-define-simple-setter local-key-binding local-set-key
)
1918 (gv-define-simple-setter mark set-mark t
)
1919 (gv-define-simple-setter mark-marker set-mark t
)
1920 (gv-define-simple-setter marker-position set-marker t
)
1921 (gv-define-setter mouse-position
(store scr
)
1922 `(set-mouse-position ,scr
(car ,store
) (cadr ,store
)
1924 (gv-define-simple-setter point goto-char
)
1925 (gv-define-simple-setter point-marker goto-char t
)
1926 (gv-define-setter point-max
(store)
1927 `(progn (narrow-to-region (point-min) ,store
) ,store
))
1928 (gv-define-setter point-min
(store)
1929 `(progn (narrow-to-region ,store
(point-max)) ,store
))
1930 (gv-define-setter read-mouse-position
(store scr
)
1931 `(set-mouse-position ,scr
(car ,store
) (cdr ,store
)))
1932 (gv-define-simple-setter screen-height set-screen-height t
)
1933 (gv-define-simple-setter screen-width set-screen-width t
)
1934 (gv-define-simple-setter selected-window select-window
)
1935 (gv-define-simple-setter selected-screen select-screen
)
1936 (gv-define-simple-setter selected-frame select-frame
)
1937 (gv-define-simple-setter standard-case-table set-standard-case-table
)
1938 (gv-define-simple-setter syntax-table set-syntax-table
)
1939 (gv-define-simple-setter visited-file-modtime set-visited-file-modtime t
)
1940 (gv-define-setter window-height
(store)
1941 `(progn (enlarge-window (- ,store
(window-height))) ,store
))
1942 (gv-define-setter window-width
(store)
1943 `(progn (enlarge-window (- ,store
(window-width)) t
) ,store
))
1944 (gv-define-simple-setter x-get-secondary-selection x-own-secondary-selection t
)
1945 (gv-define-simple-setter x-get-selection x-own-selection t
)
1947 ;;; More complex setf-methods.
1949 ;; This is a hack that allows (setf (eq a 7) B) to mean either
1950 ;; (setq a 7) or (setq a nil) depending on whether B is nil or not.
1951 ;; This is useful when you have control over the PLACE but not over
1952 ;; the VALUE, as is the case in define-minor-mode's :variable.
1953 ;; It turned out that :variable needed more flexibility anyway, so
1954 ;; this doesn't seem too useful now.
1955 (gv-define-expander eq
1956 (lambda (do place val
)
1957 (gv-letplace (getter setter
) place
1958 (macroexp-let2 nil val val
1959 (funcall do
`(eq ,getter
,val
)
1962 (,v
,(funcall setter val
))
1963 ((eq ,getter
,val
) ,(funcall setter
`(not ,val
))))))))))
1965 (gv-define-expander nthcdr
1966 (lambda (do n place
)
1967 (macroexp-let2 nil idx n
1968 (gv-letplace (getter setter
) place
1969 (funcall do
`(nthcdr ,idx
,getter
)
1970 (lambda (v) `(if (<= ,idx
0) ,(funcall setter v
)
1971 (setcdr (nthcdr (1- ,idx
) ,getter
) ,v
))))))))
1973 (gv-define-expander cl-getf
1974 (lambda (do place tag
&optional def
)
1975 (gv-letplace (getter setter
) place
1976 (macroexp-let2 nil k tag
1977 (macroexp-let2 nil d def
1978 (funcall do
`(cl-getf ,getter
,k
,d
)
1979 (lambda (v) (funcall setter
`(cl--set-getf ,getter
,k
,v
)))))))))
1981 (gv-define-expander substring
1982 (lambda (do place from
&optional to
)
1983 (gv-letplace (getter setter
) place
1984 (macroexp-let2 nil start from
1985 (macroexp-let2 nil end to
1986 (funcall do
`(substring ,getter
,start
,end
)
1988 (funcall setter
`(cl--set-substring
1989 ,getter
,start
,end
,v
)))))))))
1991 ;;; The standard modify macros.
1993 ;; `setf' is now part of core Elisp, defined in gv.el.
1996 (defmacro cl-psetf
(&rest args
)
1997 "Set PLACEs to the values VALs in parallel.
1998 This is like `setf', except that all VAL forms are evaluated (in order)
1999 before assigning any PLACEs to the corresponding values.
2001 \(fn PLACE VAL PLACE VAL ...)"
2002 (declare (debug setf
))
2003 (let ((p args
) (simple t
) (vars nil
))
2005 (if (or (not (symbolp (car p
))) (cl--expr-depends-p (nth 1 p
) vars
))
2007 (if (memq (car p
) vars
)
2008 (error "Destination duplicated in psetf: %s" (car p
)))
2010 (or p
(error "Odd number of arguments to cl-psetf"))
2013 `(progn (setf ,@args
) nil
)
2014 (setq args
(reverse args
))
2015 (let ((expr `(setf ,(cadr args
) ,(car args
))))
2016 (while (setq args
(cddr args
))
2017 (setq expr
`(setf ,(cadr args
) (prog1 ,(car args
) ,expr
))))
2018 `(progn ,expr nil
)))))
2021 (defmacro cl-remf
(place tag
)
2022 "Remove TAG from property list PLACE.
2023 PLACE may be a symbol, or any generalized variable allowed by `setf'.
2024 The form returns true if TAG was found and removed, nil otherwise."
2025 (declare (debug (place form
)))
2026 (gv-letplace (tval setter
) place
2027 (macroexp-let2 macroexp-copyable-p ttag tag
2028 `(if (eq ,ttag
(car ,tval
))
2029 (progn ,(funcall setter
`(cddr ,tval
))
2031 (cl--do-remf ,tval
,ttag
)))))
2034 (defmacro cl-shiftf
(place &rest args
)
2035 "Shift left among PLACEs.
2036 Example: (cl-shiftf A B C) sets A to B, B to C, and returns the old A.
2037 Each PLACE may be a symbol, or any generalized variable allowed by `setf'.
2040 (declare (debug (&rest place
)))
2043 ((symbolp place
) `(prog1 ,place
(setq ,place
(cl-shiftf ,@args
))))
2045 (gv-letplace (getter setter
) place
2047 ,(funcall setter
`(cl-shiftf ,@args
)))))))
2050 (defmacro cl-rotatef
(&rest args
)
2051 "Rotate left among PLACEs.
2052 Example: (cl-rotatef A B C) sets A to B, B to C, and C to A. It returns nil.
2053 Each PLACE may be a symbol, or any generalized variable allowed by `setf'.
2056 (declare (debug (&rest place
)))
2057 (if (not (memq nil
(mapcar 'symbolp args
)))
2062 (setq sets
(nconc sets
(list (pop args
) (car args
)))))
2063 `(cl-psetf ,@sets
,(car args
) ,first
)))
2064 (let* ((places (reverse args
))
2065 (temp (make-symbol "--cl-rotatef--"))
2069 (gv-letplace (getter setter
) (pop places
)
2070 `(prog1 ,getter
,(funcall setter form
)))))
2071 (gv-letplace (getter setter
) (car places
)
2072 (macroexp-let* `((,temp
,getter
))
2073 `(progn ,(funcall setter form
) nil
))))))
2076 (defmacro cl-callf
(func place
&rest args
)
2077 "Set PLACE to (FUNC PLACE ARGS...).
2078 FUNC should be an unquoted function name. PLACE may be a symbol,
2079 or any generalized variable allowed by `setf'."
2080 (declare (indent 2) (debug (cl-function place
&rest form
)))
2081 (gv-letplace (getter setter
) place
2082 (let* ((rargs (cons getter args
)))
2084 (if (symbolp func
) (cons func rargs
)
2085 `(funcall #',func
,@rargs
))))))
2088 (defmacro cl-callf2
(func arg1 place
&rest args
)
2089 "Set PLACE to (FUNC ARG1 PLACE ARGS...).
2090 Like `cl-callf', but PLACE is the second argument of FUNC, not the first.
2092 \(fn FUNC ARG1 PLACE ARGS...)"
2093 (declare (indent 3) (debug (cl-function form place
&rest form
)))
2094 (if (and (cl--safe-expr-p arg1
) (cl--simple-expr-p place
) (symbolp func
))
2095 `(setf ,place
(,func
,arg1
,place
,@args
))
2096 (macroexp-let2 nil a1 arg1
2097 (gv-letplace (getter setter
) place
2098 (let* ((rargs (cl-list* a1 getter args
)))
2100 (if (symbolp func
) (cons func rargs
)
2101 `(funcall #',func
,@rargs
))))))))
2106 (defmacro cl-defstruct
(struct &rest descs
)
2107 "Define a struct type.
2108 This macro defines a new data type called NAME that stores data
2109 in SLOTs. It defines a `make-NAME' constructor, a `copy-NAME'
2110 copier, a `NAME-p' predicate, and slot accessors named `NAME-SLOT'.
2111 You can use the accessors to set the corresponding slots, via `setf'.
2113 NAME may instead take the form (NAME OPTIONS...), where each
2114 OPTION is either a single keyword or (KEYWORD VALUE).
2115 See Info node `(cl)Structures' for a list of valid keywords.
2117 Each SLOT may instead take the form (SLOT SLOT-OPTS...), where
2118 SLOT-OPTS are keyword-value pairs for that slot. Currently, only
2119 one keyword is supported, `:read-only'. If this has a non-nil
2120 value, that slot cannot be set via `setf'.
2122 \(fn NAME SLOTS...)"
2123 (declare (doc-string 2)
2125 (&define
;Makes top-level form not be wrapped.
2129 (&or
[":conc-name" symbolp
]
2130 [":constructor" symbolp
&optional cl-lambda-list
]
2132 [":predicate" symbolp
]
2133 [":include" symbolp
&rest sexp
] ;; Not finished.
2134 ;; The following are not supported.
2135 ;; [":print-function" ...]
2137 ;; [":initial-offset" ...]
2140 ;; All the above is for the following def-form.
2141 &rest
&or symbolp
(symbolp def-form
2142 &optional
":read-only" sexp
))))
2143 (let* ((name (if (consp struct
) (car struct
) struct
))
2144 (opts (cdr-safe struct
))
2147 (conc-name (concat (symbol-name name
) "-"))
2148 (constructor (intern (format "make-%s" name
)))
2150 (copier (intern (format "copy-%s" name
)))
2151 (predicate (intern (format "%s-p" name
)))
2152 (print-func nil
) (print-auto nil
)
2153 (safety (if (cl--compiling-file) cl-optimize-safety
3))
2155 (tag (intern (format "cl-struct-%s" name
)))
2156 (tag-symbol (intern (format "cl-struct-%s-tags" name
)))
2162 pred-form pred-check
)
2163 (if (stringp (car descs
))
2164 (push `(put ',name
'structure-documentation
2165 ,(pop descs
)) forms
))
2166 (setq descs
(cons '(cl-tag-slot)
2167 (mapcar (function (lambda (x) (if (consp x
) x
(list x
))))
2170 (let ((opt (if (consp (car opts
)) (caar opts
) (car opts
)))
2171 (args (cdr-safe (pop opts
))))
2172 (cond ((eq opt
:conc-name
)
2174 (setq conc-name
(if (car args
)
2175 (symbol-name (car args
)) ""))))
2176 ((eq opt
:constructor
)
2179 ;; If this defines a constructor of the same name as
2180 ;; the default one, don't define the default.
2181 (if (eq (car args
) constructor
)
2182 (setq constructor nil
))
2183 (push args constrs
))
2184 (if args
(setq constructor
(car args
)))))
2186 (if args
(setq copier
(car args
))))
2187 ((eq opt
:predicate
)
2188 (if args
(setq predicate
(car args
))))
2190 (setq include
(car args
)
2191 include-descs
(mapcar (function
2193 (if (consp x
) x
(list x
))))
2195 ((eq opt
:print-function
)
2196 (setq print-func
(car args
)))
2198 (setq type
(car args
)))
2201 ((eq opt
:initial-offset
)
2202 (setq descs
(nconc (make-list (car args
) '(cl-skip-slot))
2205 (error "Slot option %s unrecognized" opt
)))))
2208 `(progn (funcall #',print-func cl-x cl-s cl-n
) t
))
2209 (or type
(and include
(not (get include
'cl-struct-print
)))
2211 print-func
(and (or (not (or include type
)) (null print-func
))
2213 (princ ,(format "#S(%s" name
) cl-s
))))))
2215 (let ((inc-type (get include
'cl-struct-type
))
2216 (old-descs (get include
'cl-struct-slots
)))
2217 (or inc-type
(error "%s is not a struct name" include
))
2218 (and type
(not (eq (car inc-type
) type
))
2219 (error ":type disagrees with :include for %s" name
))
2220 (while include-descs
2221 (setcar (memq (or (assq (caar include-descs
) old-descs
)
2222 (error "No slot %s in included struct %s"
2223 (caar include-descs
) include
))
2225 (pop include-descs
)))
2226 (setq descs
(append old-descs
(delq (assq 'cl-tag-slot descs
) descs
))
2228 named
(assq 'cl-tag-slot descs
))
2229 (if (cadr inc-type
) (setq tag name named t
))
2230 (let ((incl include
))
2232 (push `(cl-pushnew ',tag
2233 ,(intern (format "cl-struct-%s-tags" incl
)))
2235 (setq incl
(get incl
'cl-struct-include
)))))
2238 (or (memq type
'(vector list
))
2239 (error "Invalid :type specifier: %s" type
))
2240 (if named
(setq tag name
)))
2241 (setq type
'vector named
'true
)))
2242 (or named
(setq descs
(delq (assq 'cl-tag-slot descs
) descs
)))
2243 (push `(defvar ,tag-symbol
) forms
)
2244 (setq pred-form
(and named
2245 (let ((pos (- (length descs
)
2246 (length (memq (assq 'cl-tag-slot descs
)
2248 (if (eq type
'vector
)
2249 `(and (vectorp cl-x
)
2250 (>= (length cl-x
) ,(length descs
))
2251 (memq (aref cl-x
,pos
) ,tag-symbol
))
2253 `(memq (car-safe cl-x
) ,tag-symbol
)
2255 (memq (nth ,pos cl-x
) ,tag-symbol
))))))
2256 pred-check
(and pred-form
(> safety
0)
2257 (if (and (eq (cl-caadr pred-form
) 'vectorp
)
2259 (cons 'and
(cl-cdddr pred-form
)) pred-form
)))
2260 (let ((pos 0) (descp descs
))
2262 (let* ((desc (pop descp
))
2264 (if (memq slot
'(cl-tag-slot cl-skip-slot
))
2267 (push (and (eq slot
'cl-tag-slot
) `',tag
)
2269 (if (assq slot descp
)
2270 (error "Duplicate slots named %s in %s" slot name
))
2271 (let ((accessor (intern (format "%s%s" conc-name slot
))))
2273 (push (nth 1 desc
) defaults
)
2274 (push `(cl-defsubst ,accessor
(cl-x)
2276 (list `(or ,pred-check
2277 (error "%s accessing a non-%s"
2278 ',accessor
',name
))))
2279 ,(if (eq type
'vector
) `(aref cl-x
,pos
)
2280 (if (= pos
0) '(car cl-x
)
2281 `(nth ,pos cl-x
)))) forms
)
2282 (push (cons accessor t
) side-eff
)
2283 ;; Don't bother defining a setf-expander, since gv-get can use
2284 ;; the compiler macro to get the same result.
2285 ;;(push `(gv-define-setter ,accessor (cl-val cl-x)
2286 ;; ,(if (cadr (memq :read-only (cddr desc)))
2287 ;; `(progn (ignore cl-x cl-val)
2288 ;; (error "%s is a read-only slot"
2290 ;; ;; If cl is loaded only for compilation,
2291 ;; ;; the call to cl--struct-setf-expander would
2292 ;; ;; cause a warning because it may not be
2293 ;; ;; defined at run time. Suppress that warning.
2295 ;; (declare-function
2296 ;; cl--struct-setf-expander "cl-macs"
2297 ;; (x name accessor pred-form pos))
2298 ;; (cl--struct-setf-expander
2299 ;; cl-val cl-x ',name ',accessor
2300 ;; ,(and pred-check `',pred-check)
2305 (list `(princ ,(format " %s" slot
) cl-s
)
2306 `(prin1 (,accessor cl-x
) cl-s
)))))))
2307 (setq pos
(1+ pos
))))
2308 (setq slots
(nreverse slots
)
2309 defaults
(nreverse defaults
))
2310 (and predicate pred-form
2311 (progn (push `(cl-defsubst ,predicate
(cl-x)
2312 ,(if (eq (car pred-form
) 'and
)
2313 (append pred-form
'(t))
2314 `(and ,pred-form t
))) forms
)
2315 (push (cons predicate
'error-free
) side-eff
)))
2317 (progn (push `(defun ,copier
(x) (copy-sequence x
)) forms
)
2318 (push (cons copier t
) side-eff
)))
2320 (push (list constructor
2321 (cons '&key
(delq nil
(copy-sequence slots
))))
2324 (let* ((name (caar constrs
))
2325 (args (cadr (pop constrs
)))
2326 (anames (cl--arglist-args args
))
2327 (make (cl-mapcar (function (lambda (s d
) (if (memq s anames
) s d
)))
2329 (push `(cl-defsubst ,name
2330 (&cl-defs
'(nil ,@descs
) ,@args
)
2331 (,type
,@make
)) forms
)
2332 (if (cl--safe-expr-p `(progn ,@(mapcar #'cl-second descs
)))
2333 (push (cons name t
) side-eff
))))
2334 (if print-auto
(nconc print-func
(list '(princ ")" cl-s
) t
)))
2335 ;; Don't bother adding to cl-custom-print-functions since it's not used
2336 ;; by anything anyway!
2338 ;; (push `(if (boundp 'cl-custom-print-functions)
2340 ;; ;; The auto-generated function does not pay attention to
2341 ;; ;; the depth argument cl-n.
2342 ;; (lambda (cl-x cl-s ,(if print-auto '_cl-n 'cl-n))
2343 ;; (and ,pred-form ,print-func))
2344 ;; cl-custom-print-functions))
2346 (push `(setq ,tag-symbol
(list ',tag
)) forms
)
2347 (push `(cl-eval-when (compile load eval
)
2348 (put ',name
'cl-struct-slots
',descs
)
2349 (put ',name
'cl-struct-type
',(list type
(eq named t
)))
2350 (put ',name
'cl-struct-include
',include
)
2351 (put ',name
'cl-struct-print
,print-auto
)
2352 ,@(mapcar (lambda (x)
2353 `(put ',(car x
) 'side-effect-free
',(cdr x
)))
2356 `(progn ,@(nreverse (cons `',name forms
)))))
2358 ;;; Types and assertions.
2361 (defmacro cl-deftype
(name arglist
&rest body
)
2362 "Define NAME as a new data type.
2363 The type name can then be used in `cl-typecase', `cl-check-type', etc."
2364 (declare (debug cl-defmacro
) (doc-string 3))
2365 `(cl-eval-when (compile load eval
)
2366 (put ',name
'cl-deftype-handler
2367 (cl-function (lambda (&cl-defs
'('*) ,@arglist
) ,@body
)))))
2369 (defun cl--make-type-test (val type
)
2371 (cond ((get type
'cl-deftype-handler
)
2372 (cl--make-type-test val
(funcall (get type
'cl-deftype-handler
))))
2373 ((memq type
'(nil t
)) type
)
2374 ((eq type
'null
) `(null ,val
))
2375 ((eq type
'atom
) `(atom ,val
))
2376 ((eq type
'float
) `(cl-floatp-safe ,val
))
2377 ((eq type
'real
) `(numberp ,val
))
2378 ((eq type
'fixnum
) `(integerp ,val
))
2379 ;; FIXME: Should `character' accept things like ?\C-\M-a ? --Stef
2380 ((memq type
'(character string-char
)) `(characterp ,val
))
2382 (let* ((name (symbol-name type
))
2383 (namep (intern (concat name
"p"))))
2384 (if (fboundp namep
) (list namep val
)
2385 (list (intern (concat name
"-p")) val
)))))
2386 (cond ((get (car type
) 'cl-deftype-handler
)
2387 (cl--make-type-test val
(apply (get (car type
) 'cl-deftype-handler
)
2389 ((memq (car type
) '(integer float real number
))
2390 (delq t
`(and ,(cl--make-type-test val
(car type
))
2391 ,(if (memq (cadr type
) '(* nil
)) t
2392 (if (consp (cadr type
)) `(> ,val
,(cl-caadr type
))
2393 `(>= ,val
,(cadr type
))))
2394 ,(if (memq (cl-caddr type
) '(* nil
)) t
2395 (if (consp (cl-caddr type
)) `(< ,val
,(cl-caaddr type
))
2396 `(<= ,val
,(cl-caddr type
)))))))
2397 ((memq (car type
) '(and or not
))
2399 (mapcar (function (lambda (x) (cl--make-type-test val x
)))
2401 ((memq (car type
) '(member cl-member
))
2402 `(and (cl-member ,val
',(cdr type
)) t
))
2403 ((eq (car type
) 'satisfies
) (list (cadr type
) val
))
2404 (t (error "Bad type spec: %s" type
)))))
2408 (defun cl-typep (object type
) ; See compiler macro below.
2409 "Check that OBJECT is of type TYPE.
2410 TYPE is a Common Lisp-style type specifier."
2411 (let ((cl--object object
)) ;; Yuck!!
2412 (eval (cl--make-type-test 'cl--object type
))))
2415 (defmacro cl-check-type
(form type
&optional string
)
2416 "Verify that FORM is of type TYPE; signal an error if not.
2417 STRING is an optional description of the desired type."
2418 (declare (debug (place cl-type-spec
&optional stringp
)))
2419 (and (or (not (cl--compiling-file))
2420 (< cl-optimize-speed
3) (= cl-optimize-safety
3))
2421 (let* ((temp (if (cl--simple-expr-p form
3)
2422 form
(make-symbol "--cl-var--")))
2423 (body `(or ,(cl--make-type-test temp type
)
2424 (signal 'wrong-type-argument
2425 (list ,(or string
`',type
)
2427 (if (eq temp form
) `(progn ,body nil
)
2428 `(let ((,temp
,form
)) ,body nil
)))))
2431 (defmacro cl-assert
(form &optional show-args string
&rest args
)
2432 "Verify that FORM returns non-nil; signal an error if not.
2433 Second arg SHOW-ARGS means to include arguments of FORM in message.
2434 Other args STRING and ARGS... are arguments to be passed to `error'.
2435 They are not evaluated unless the assertion fails. If STRING is
2436 omitted, a default message listing FORM itself is used."
2437 (declare (debug (form &rest form
)))
2438 (and (or (not (cl--compiling-file))
2439 (< cl-optimize-speed
3) (= cl-optimize-safety
3))
2440 (let ((sargs (and show-args
2441 (delq nil
(mapcar (lambda (x)
2442 (unless (macroexp-const-p x
)
2448 `(error ,string
,@sargs
,@args
)
2449 `(signal 'cl-assertion-failed
2450 (list ',form
,@sargs
))))
2453 ;;; Compiler macros.
2456 (defmacro cl-define-compiler-macro
(func args
&rest body
)
2457 "Define a compiler-only macro.
2458 This is like `defmacro', but macro expansion occurs only if the call to
2459 FUNC is compiled (i.e., not interpreted). Compiler macros should be used
2460 for optimizing the way calls to FUNC are compiled; the form returned by
2461 BODY should do the same thing as a call to the normal function called
2462 FUNC, though possibly more efficiently. Note that, like regular macros,
2463 compiler macros are expanded repeatedly until no further expansions are
2464 possible. Unlike regular macros, BODY can decide to \"punt\" and leave the
2465 original function call alone by declaring an initial `&whole foo' parameter
2466 and then returning foo."
2467 (declare (debug cl-defmacro
))
2468 (let ((p args
) (res nil
))
2469 (while (consp p
) (push (pop p
) res
))
2470 (setq args
(nconc (nreverse res
) (and p
(list '&rest p
)))))
2471 `(cl-eval-when (compile load eval
)
2472 (put ',func
'compiler-macro
2473 (cl-function (lambda ,(if (memq '&whole args
) (delq '&whole args
)
2474 (cons '_cl-whole-arg args
))
2476 ;; This is so that describe-function can locate
2477 ;; the macro definition.
2478 (let ((file ,(or buffer-file-name
2479 (and (boundp 'byte-compile-current-file
)
2480 (stringp byte-compile-current-file
)
2481 byte-compile-current-file
))))
2482 (if file
(put ',func
'compiler-macro-file
2483 (purecopy (file-name-nondirectory file
)))))))
2486 (defun cl-compiler-macroexpand (form)
2488 (let ((func (car-safe form
)) (handler nil
))
2489 (while (and (symbolp func
)
2490 (not (setq handler
(get func
'compiler-macro
)))
2492 (or (not (eq (car-safe (symbol-function func
)) 'autoload
))
2493 (load (nth 1 (symbol-function func
)))))
2494 (setq func
(symbol-function func
)))
2496 (not (eq form
(setq form
(apply handler form
(cdr form
))))))))
2499 ;; Optimize away unused block-wrappers.
2501 (defvar cl--active-block-names nil
)
2503 (cl-define-compiler-macro cl--block-wrapper
(cl-form)
2504 (let* ((cl-entry (cons (nth 1 (nth 1 cl-form
)) nil
))
2505 (cl--active-block-names (cons cl-entry cl--active-block-names
))
2506 (cl-body (macroexpand-all ;Performs compiler-macro expansions.
2507 (cons 'progn
(cddr cl-form
))
2508 macroexpand-all-environment
)))
2509 ;; FIXME: To avoid re-applying macroexpand-all, we'd like to be able
2510 ;; to indicate that this return value is already fully expanded.
2512 `(catch ,(nth 1 cl-form
) ,@(cdr cl-body
))
2515 (cl-define-compiler-macro cl--block-throw
(cl-tag cl-value
)
2516 (let ((cl-found (assq (nth 1 cl-tag
) cl--active-block-names
)))
2517 (if cl-found
(setcdr cl-found t
)))
2518 `(throw ,cl-tag
,cl-value
))
2521 (defmacro cl-defsubst
(name args
&rest body
)
2522 "Define NAME as a function.
2523 Like `defun', except the function is automatically declared `inline',
2524 ARGLIST allows full Common Lisp conventions, and BODY is implicitly
2525 surrounded by (cl-block NAME ...).
2527 \(fn NAME ARGLIST [DOCSTRING] BODY...)"
2528 (declare (debug cl-defun
) (indent 2))
2529 (let* ((argns (cl--arglist-args args
)) (p argns
)
2530 (pbody (cons 'progn body
))
2531 (unsafe (not (cl--safe-expr-p pbody
))))
2532 (while (and p
(eq (cl--expr-contains args
(car p
)) 1)) (pop p
))
2534 ,(if p nil
; give up if defaults refer to earlier args
2535 `(cl-define-compiler-macro ,name
2536 ,(if (memq '&key args
)
2537 `(&whole cl-whole
&cl-quote
,@args
)
2538 (cons '&cl-quote args
))
2539 (cl--defsubst-expand
2540 ',argns
'(cl-block ,name
,@body
)
2541 ;; We used to pass `simple' as
2542 ;; (not (or unsafe (cl-expr-access-order pbody argns)))
2543 ;; But this is much too simplistic since it
2544 ;; does not pay attention to the argvs (and
2545 ;; cl-expr-access-order itself is also too naive).
2547 ,(and (memq '&key args
) 'cl-whole
) ,unsafe
,@argns
)))
2548 (cl-defun ,name
,args
,@body
))))
2550 (defun cl--defsubst-expand (argns body simple whole unsafe
&rest argvs
)
2551 (if (and whole
(not (cl--safe-expr-p (cons 'progn argvs
)))) whole
2552 (if (cl--simple-exprs-p argvs
) (setq simple t
))
2555 (cl-mapcar (lambda (argn argv
)
2556 (if (or simple
(macroexp-const-p argv
))
2557 (progn (push (cons argn argv
) substs
)
2558 (and unsafe
(list argn argv
)))
2561 ;; FIXME: `sublis/subst' will happily substitute the symbol
2562 ;; `argn' in places where it's not used as a reference
2564 ;; FIXME: `sublis/subst' will happily copy `argv' to a different
2565 ;; scope, leading to name capture.
2566 (setq body
(cond ((null substs
) body
)
2567 ((null (cdr substs
))
2568 (cl-subst (cdar substs
) (caar substs
) body
))
2569 (t (cl-sublis substs body
))))
2570 (if lets
`(let ,lets
,body
) body
))))
2573 ;; Compile-time optimizations for some functions defined in this package.
2574 ;; Note that cl.el arranges to force cl-macs to be loaded at compile-time,
2575 ;; mainly to make sure these macros will be present.
2577 (defun cl--compiler-macro-member (form a list
&rest keys
)
2578 (let ((test (and (= (length keys
) 2) (eq (car keys
) :test
)
2579 (cl--const-expr-val (nth 1 keys
)))))
2580 (cond ((eq test
'eq
) `(memq ,a
,list
))
2581 ((eq test
'equal
) `(member ,a
,list
))
2582 ((or (null keys
) (eq test
'eql
)) `(memql ,a
,list
))
2585 (defun cl--compiler-macro-assoc (form a list
&rest keys
)
2586 (let ((test (and (= (length keys
) 2) (eq (car keys
) :test
)
2587 (cl--const-expr-val (nth 1 keys
)))))
2588 (cond ((eq test
'eq
) `(assq ,a
,list
))
2589 ((eq test
'equal
) `(assoc ,a
,list
))
2590 ((and (macroexp-const-p a
) (or (null keys
) (eq test
'eql
)))
2591 (if (cl-floatp-safe (cl--const-expr-val a
))
2592 `(assoc ,a
,list
) `(assq ,a
,list
)))
2596 (defun cl--compiler-macro-adjoin (form a list
&rest keys
)
2597 (if (and (cl--simple-expr-p a
) (cl--simple-expr-p list
)
2598 (not (memq :key keys
)))
2599 `(if (cl-member ,a
,list
,@keys
) ,list
(cons ,a
,list
))
2603 (defun cl--compiler-macro-list* (_form arg
&rest others
)
2604 (let* ((args (reverse (cons arg others
)))
2606 (while (setq args
(cdr args
))
2607 (setq form
`(cons ,(car args
) ,form
)))
2610 (defun cl--compiler-macro-get (_form sym prop
&optional def
)
2612 `(cl-getf (symbol-plist ,sym
) ,prop
,def
)
2615 (cl-define-compiler-macro cl-typep
(&whole form val type
)
2616 (if (macroexp-const-p type
)
2617 (macroexp-let2 macroexp-copyable-p temp val
2618 (cl--make-type-test temp
(cl--const-expr-val type
)))
2622 (defun cl--compiler-macro-cXXr (form x
)
2623 (let* ((head (car form
))
2624 (n (symbol-name (car form
)))
2625 (i (- (length n
) 2)))
2626 (if (not (string-match "c[ad]+r\\'" n
))
2627 (if (and (fboundp head
) (symbolp (symbol-function head
)))
2628 (cl--compiler-macro-cXXr (cons (symbol-function head
) (cdr form
))
2630 (error "Compiler macro for cXXr applied to non-cXXr form"))
2631 (while (> i
(match-beginning 0))
2632 (setq x
(list (if (eq (aref n i
) ?a
) 'car
'cdr
) x
))
2636 (dolist (y '(cl-first cl-second cl-third cl-fourth
2637 cl-fifth cl-sixth cl-seventh
2638 cl-eighth cl-ninth cl-tenth
2639 cl-rest cl-endp cl-plusp cl-minusp
2640 cl-caaar cl-caadr cl-cadar
2641 cl-caddr cl-cdaar cl-cdadr
2642 cl-cddar cl-cdddr cl-caaaar
2643 cl-caaadr cl-caadar cl-caaddr
2644 cl-cadaar cl-cadadr cl-caddar
2645 cl-cadddr cl-cdaaar cl-cdaadr
2646 cl-cdadar cl-cdaddr cl-cddaar
2647 cl-cddadr cl-cdddar cl-cddddr
))
2648 (put y
'side-effect-free t
))
2650 ;;; Things that are inline.
2651 (cl-proclaim '(inline cl-floatp-safe cl-acons cl-map cl-concatenate cl-notany
2652 cl-notevery cl--set-elt cl-revappend cl-nreconc gethash
))
2654 ;;; Things that are side-effect-free.
2655 (mapc (lambda (x) (put x
'side-effect-free t
))
2656 '(cl-oddp cl-evenp cl-signum last butlast cl-ldiff cl-pairlis cl-gcd cl-lcm
2657 cl-isqrt cl-floor cl-ceiling cl-truncate cl-round cl-mod cl-rem cl-subseq
2658 cl-list-length cl-get cl-getf
))
2660 ;;; Things that are side-effect-and-error-free.
2661 (mapc (lambda (x) (put x
'side-effect-free
'error-free
))
2662 '(eql cl-floatp-safe cl-list
* cl-subst cl-acons cl-equalp cl-random-state-p
2663 copy-tree cl-sublis
))
2666 (run-hooks 'cl-macs-load-hook
)
2669 ;; byte-compile-dynamic: t
2670 ;; byte-compile-warnings: (not cl-functions)
2671 ;; generated-autoload-file: "cl-loaddefs.el"
2676 ;;; cl-macs.el ends here