1 ;;; cl-macs.el --- Common Lisp macros
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 (defmacro cl-pop2
(place)
49 (declare (debug edebug-sexps
))
50 `(prog1 (car (cdr ,place
))
51 (setq ,place
(cdr (cdr ,place
)))))
53 (defvar cl-optimize-safety
)
54 (defvar cl-optimize-speed
)
57 ;; This kludge allows macros which use cl-transform-function-property
58 ;; to be called at compile-time.
61 (or (fboundp 'cl-transform-function-property
)
62 (defun cl-transform-function-property (n p f
)
63 `(put ',n
',p
#'(lambda .
,f
)))))
67 (defvar cl-old-bc-file-form nil
)
69 ;;; Some predicates for analyzing Lisp forms. These are used by various
70 ;;; macro expanders to optimize the results in certain common cases.
72 (defconst cl-simple-funcs
'(car cdr nth aref elt if and or
+ -
1+ 1- min max
73 car-safe cdr-safe progn prog1 prog2
))
74 (defconst cl-safe-funcs
'(* / % length memq list vector vectorp
77 ;;; Check if no side effects, and executes quickly.
78 (defun cl-simple-expr-p (x &optional size
)
79 (or size
(setq size
10))
80 (if (and (consp x
) (not (memq (car x
) '(quote function cl-function
))))
81 (and (symbolp (car x
))
82 (or (memq (car x
) cl-simple-funcs
)
83 (get (car x
) 'side-effect-free
))
86 (while (and (setq x
(cdr x
))
87 (setq size
(cl-simple-expr-p (car x
) size
))))
88 (and (null x
) (>= size
0) size
)))
89 (and (> size
0) (1- size
))))
91 (defun cl-simple-exprs-p (xs)
92 (while (and xs
(cl-simple-expr-p (car xs
)))
96 ;;; Check if no side effects.
97 (defun cl-safe-expr-p (x)
98 (or (not (and (consp x
) (not (memq (car x
) '(quote function cl-function
)))))
99 (and (symbolp (car x
))
100 (or (memq (car x
) cl-simple-funcs
)
101 (memq (car x
) cl-safe-funcs
)
102 (get (car x
) 'side-effect-free
))
104 (while (and (setq x
(cdr x
)) (cl-safe-expr-p (car x
))))
107 ;;; Check if constant (i.e., no side effects or dependencies).
108 (defun cl-const-expr-p (x)
110 (or (eq (car x
) 'quote
)
111 (and (memq (car x
) '(function cl-function
))
112 (or (symbolp (nth 1 x
))
113 (and (eq (car-safe (nth 1 x
)) 'lambda
) 'func
)))))
114 ((symbolp x
) (and (memq x
'(nil t
)) t
))
117 (defun cl-const-exprs-p (xs)
118 (while (and xs
(cl-const-expr-p (car xs
)))
122 (defun cl-const-expr-val (x)
123 (and (eq (cl-const-expr-p x
) t
) (if (consp x
) (nth 1 x
) x
)))
125 (defun cl-expr-access-order (x v
)
126 ;; This apparently tries to return nil iff the expression X evaluates
127 ;; the variables V in the same order as they appear in V (so as to
128 ;; be able to replace those vars with the expressions they're bound
130 ;; FIXME: This is very naive, it doesn't even check to see if those
131 ;; variables appear more than once.
132 (if (cl-const-expr-p x
) v
135 (while (setq x
(cdr x
)) (setq v
(cl-expr-access-order (car x
) v
)))
137 (if (eq x
(car v
)) (cdr v
) '(t)))))
139 ;;; Count number of times X refers to Y. Return nil for 0 times.
140 (defun cl-expr-contains (x y
)
141 ;; FIXME: This is naive, and it will cl-count Y as referred twice in
142 ;; (let ((Y 1)) Y) even though it should be 0. Also it is often called on
143 ;; non-macroexpanded code, so it may also miss some occurrences that would
144 ;; only appear in the expanded code.
145 (cond ((equal y x
) 1)
146 ((and (consp x
) (not (memq (car x
) '(quote function cl-function
))))
149 (setq sum
(+ sum
(or (cl-expr-contains (pop x
) y
) 0))))
150 (setq sum
(+ sum
(or (cl-expr-contains x y
) 0)))
151 (and (> sum
0) sum
)))
154 (defun cl-expr-contains-any (x y
)
155 (while (and y
(not (cl-expr-contains x
(car y
)))) (pop y
))
158 ;;; Check whether X may depend on any of the symbols in Y.
159 (defun cl-expr-depends-p (x y
)
160 (and (not (cl-const-expr-p x
))
161 (or (not (cl-safe-expr-p x
)) (cl-expr-contains-any x y
))))
165 (defvar cl--gensym-counter
)
167 (defun cl-gensym (&optional prefix
)
168 "Generate a new uninterned symbol.
169 The name is made by appending a number to PREFIX, default \"G\"."
170 (let ((pfix (if (stringp prefix
) prefix
"G"))
171 (num (if (integerp prefix
) prefix
172 (prog1 cl--gensym-counter
173 (setq cl--gensym-counter
(1+ cl--gensym-counter
))))))
174 (make-symbol (format "%s%d" pfix num
))))
177 (defun cl-gentemp (&optional prefix
)
178 "Generate a new interned symbol with a unique name.
179 The name is made by appending a number to PREFIX, default \"G\"."
180 (let ((pfix (if (stringp prefix
) prefix
"G"))
182 (while (intern-soft (setq name
(format "%s%d" pfix cl--gensym-counter
)))
183 (setq cl--gensym-counter
(1+ cl--gensym-counter
)))
187 ;;; Program structure.
189 (def-edebug-spec cl-declarations
190 (&rest
("cl-declare" &rest sexp
)))
192 (def-edebug-spec cl-declarations-or-string
193 (&or stringp cl-declarations
))
195 (def-edebug-spec cl-lambda-list
197 [&optional
["&optional" cl-
&optional-arg
&rest cl-
&optional-arg
]]
198 [&optional
["&rest" arg
]]
199 [&optional
["&key" [cl-
&key-arg
&rest cl-
&key-arg
]
200 &optional
"&allow-other-keys"]]
201 [&optional
["&aux" &rest
202 &or
(symbolp &optional def-form
) symbolp
]]
205 (def-edebug-spec cl-
&optional-arg
206 (&or
(arg &optional def-form arg
) arg
))
208 (def-edebug-spec cl-
&key-arg
209 (&or
([&or
(symbolp arg
) arg
] &optional def-form arg
) arg
))
212 (defmacro cl-defun
(name args
&rest body
)
213 "Define NAME as a function.
214 Like normal `defun', except ARGLIST allows full Common Lisp conventions,
215 and BODY is implicitly surrounded by (cl-block NAME ...).
217 \(fn NAME ARGLIST [DOCSTRING] BODY...)"
219 ;; Same as defun but use cl-lambda-list.
220 (&define
[&or name
("cl-setf" :name cl-setf name
)]
222 cl-declarations-or-string
223 [&optional
("interactive" interactive
)]
227 (let* ((res (cl-transform-lambda (cons args body
) name
))
228 (form `(defun ,name
,@(cdr res
))))
229 (if (car res
) `(progn ,(car res
) ,form
) form
)))
231 ;; The lambda list for macros is different from that of normal lambdas.
232 ;; Note that &environment is only allowed as first or last items in the
235 (def-edebug-spec cl-macro-list
236 (([&optional
"&environment" arg
]
238 [&optional
["&optional" &rest
239 &or
(cl-macro-arg &optional def-form cl-macro-arg
) arg
]]
240 [&optional
[[&or
"&rest" "&body"] cl-macro-arg
]]
241 [&optional
["&key" [&rest
242 [&or
([&or
(symbolp cl-macro-arg
) arg
]
243 &optional def-form cl-macro-arg
)
245 &optional
"&allow-other-keys"]]
246 [&optional
["&aux" &rest
247 &or
(symbolp &optional def-form
) symbolp
]]
248 [&optional
"&environment" arg
]
251 (def-edebug-spec cl-macro-arg
252 (&or arg cl-macro-list1
))
254 (def-edebug-spec cl-macro-list1
255 (([&optional
"&whole" arg
] ;; only allowed at lower levels
257 [&optional
["&optional" &rest
258 &or
(cl-macro-arg &optional def-form cl-macro-arg
) arg
]]
259 [&optional
[[&or
"&rest" "&body"] cl-macro-arg
]]
260 [&optional
["&key" [&rest
261 [&or
([&or
(symbolp cl-macro-arg
) arg
]
262 &optional def-form cl-macro-arg
)
264 &optional
"&allow-other-keys"]]
265 [&optional
["&aux" &rest
266 &or
(symbolp &optional def-form
) symbolp
]]
270 (defmacro cl-defmacro
(name args
&rest body
)
271 "Define NAME as a macro.
272 Like normal `defmacro', except ARGLIST allows full Common Lisp conventions,
273 and BODY is implicitly surrounded by (cl-block NAME ...).
275 \(fn NAME ARGLIST [DOCSTRING] BODY...)"
277 (&define name cl-macro-list cl-declarations-or-string def-body
))
280 (let* ((res (cl-transform-lambda (cons args body
) name
))
281 (form `(defmacro ,name
,@(cdr res
))))
282 (if (car res
) `(progn ,(car res
) ,form
) form
)))
284 (def-edebug-spec cl-lambda-expr
285 (&define
("lambda" cl-lambda-list
286 ;;cl-declarations-or-string
287 ;;[&optional ("interactive" interactive)]
290 ;; Redefine function-form to also match cl-function
291 (def-edebug-spec function-form
292 ;; form at the end could also handle "function",
293 ;; but recognize it specially to avoid wrapping function forms.
294 (&or
([&or
"quote" "function"] &or symbolp lambda-expr
)
295 ("cl-function" cl-function
)
299 (defmacro cl-function
(func)
300 "Introduce a function.
301 Like normal `function', except that if argument is a lambda form,
302 its argument list allows full Common Lisp conventions."
303 (declare (debug (&or symbolp cl-lambda-expr
)))
304 (if (eq (car-safe func
) 'lambda
)
305 (let* ((res (cl-transform-lambda (cdr func
) 'cl-none
))
306 (form `(function (lambda .
,(cdr res
)))))
307 (if (car res
) `(progn ,(car res
) ,form
) form
))
310 (defun cl-transform-function-property (func prop form
)
311 (let ((res (cl-transform-lambda form func
)))
312 `(progn ,@(cdr (cdr (car res
)))
313 (put ',func
',prop
#'(lambda .
,(cdr res
))))))
315 (defconst cl-lambda-list-keywords
316 '(&optional
&rest
&key
&allow-other-keys
&aux
&whole
&body
&environment
))
318 (defvar cl-macro-environment nil
319 "Keep the list of currently active macros.
320 It is a list of elements of the form either:
321 - (SYMBOL . FUNCTION) where FUNCTION is the macro expansion function.
322 - (SYMBOL-NAME . EXPANSION) where SYMBOL-NAME is the name of a symbol macro.")
323 (defvar cl-bind-block
) (defvar cl-bind-defs
) (defvar cl-bind-enquote
)
324 (defvar cl-bind-inits
) (defvar cl-bind-lets
) (defvar cl-bind-forms
)
326 (declare-function help-add-fundoc-usage
"help-fns" (docstring arglist
))
328 (defun cl--make-usage-var (x)
329 "X can be a var or a (destructuring) lambda-list."
331 ((symbolp x
) (make-symbol (upcase (symbol-name x
))))
332 ((consp x
) (cl--make-usage-args x
))
335 (defun cl--make-usage-args (arglist)
336 ;; `orig-args' can contain &cl-defs (an internal
337 ;; CL thingy I don't understand), so remove it.
338 (let ((x (memq '&cl-defs arglist
)))
339 (when x
(setq arglist
(delq (car x
) (remq (cadr x
) arglist
)))))
344 (if (eq ?\
& (aref (symbol-name x
) 0))
346 (make-symbol (upcase (symbol-name x
)))))
348 ((memq state
'(nil &rest
)) (cl--make-usage-args x
))
349 (t ;(VAR INITFORM SVAR) or ((KEYWORD VAR) INITFORM SVAR).
351 (if (and (consp (car x
)) (eq state
'&key
))
352 (list (caar x
) (cl--make-usage-var (nth 1 (car x
))))
353 (cl--make-usage-var (car x
)))
355 (cl--make-usage-args (nthcdr 2 x
)) ;SVAR.
359 (defun cl-transform-lambda (form cl-bind-block
)
360 (let* ((args (car form
)) (body (cdr form
)) (orig-args args
)
361 (cl-bind-defs nil
) (cl-bind-enquote nil
)
362 (cl-bind-inits nil
) (cl-bind-lets nil
) (cl-bind-forms nil
)
363 (header nil
) (simple-args nil
))
364 (while (or (stringp (car body
))
365 (memq (car-safe (car body
)) '(interactive cl-declare
)))
366 (push (pop body
) header
))
367 (setq args
(if (listp args
) (cl-copy-list args
) (list '&rest args
)))
368 (let ((p (last args
))) (if (cdr p
) (setcdr p
(list '&rest
(cdr p
)))))
369 (if (setq cl-bind-defs
(cadr (memq '&cl-defs args
)))
370 (setq args
(delq '&cl-defs
(delq cl-bind-defs args
))
371 cl-bind-defs
(cadr cl-bind-defs
)))
372 (if (setq cl-bind-enquote
(memq '&cl-quote args
))
373 (setq args
(delq '&cl-quote args
)))
374 (if (memq '&whole args
) (error "&whole not currently implemented"))
375 (let* ((p (memq '&environment args
)) (v (cadr p
)))
376 (if p
(setq args
(nconc (delq (car p
) (delq v args
))
377 (list '&aux
(list v
'cl-macro-environment
))))))
378 (while (and args
(symbolp (car args
))
379 (not (memq (car args
) '(nil &rest
&body
&key
&aux
)))
380 (not (and (eq (car args
) '&optional
)
381 (or cl-bind-defs
(consp (cadr args
))))))
382 (push (pop args
) simple-args
))
383 (or (eq cl-bind-block
'cl-none
)
384 (setq body
(list `(cl-block ,cl-bind-block
,@body
))))
386 (cl-list* nil
(nreverse simple-args
) (nconc (nreverse header
) body
))
387 (if (memq '&optional simple-args
) (push '&optional args
))
388 (cl-do-arglist args nil
(- (length simple-args
)
389 (if (memq '&optional simple-args
) 1 0)))
390 (setq cl-bind-lets
(nreverse cl-bind-lets
))
391 (cl-list* (and cl-bind-inits
`(cl-eval-when (compile load eval
)
392 ,@(nreverse cl-bind-inits
)))
393 (nconc (nreverse simple-args
)
394 (list '&rest
(car (pop cl-bind-lets
))))
395 (nconc (let ((hdr (nreverse header
)))
396 ;; Macro expansion can take place in the middle of
397 ;; apparently harmless computation, so it should not
398 ;; touch the match-data.
401 (cons (help-add-fundoc-usage
402 (if (stringp (car hdr
)) (pop hdr
))
405 (cl--make-usage-args orig-args
))))
407 (list `(let* ,cl-bind-lets
408 ,@(nreverse cl-bind-forms
)
411 (defun cl-do-arglist (args expr
&optional num
) ; uses bind-*
413 (if (or (memq args cl-lambda-list-keywords
) (not (symbolp args
)))
414 (error "Invalid argument name: %s" args
)
415 (push (list args expr
) cl-bind-lets
))
416 (setq args
(cl-copy-list args
))
417 (let ((p (last args
))) (if (cdr p
) (setcdr p
(list '&rest
(cdr p
)))))
418 (let ((p (memq '&body args
))) (if p
(setcar p
'&rest
)))
419 (if (memq '&environment args
) (error "&environment used incorrectly"))
420 (let ((save-args args
)
421 (restarg (memq '&rest args
))
422 (safety (if (cl-compiling-file) cl-optimize-safety
3))
424 (laterarg nil
) (exactarg nil
) minarg
)
425 (or num
(setq num
0))
426 (if (listp (cadr restarg
))
427 (setq restarg
(make-symbol "--cl-rest--"))
428 (setq restarg
(cadr restarg
)))
429 (push (list restarg expr
) cl-bind-lets
)
430 (if (eq (car args
) '&whole
)
431 (push (list (cl-pop2 args
) restarg
) cl-bind-lets
))
433 (setq minarg restarg
)
434 (while (and p
(not (memq (car p
) cl-lambda-list-keywords
)))
435 (or (eq p args
) (setq minarg
(list 'cdr minarg
)))
437 (if (memq (car p
) '(nil &aux
))
438 (setq minarg
`(= (length ,restarg
)
439 ,(length (cl-ldiff args p
)))
440 exactarg
(not (eq args p
)))))
441 (while (and args
(not (memq (car args
) cl-lambda-list-keywords
)))
442 (let ((poparg (list (if (or (cdr args
) (not exactarg
)) 'pop
'car
)
446 (if (or laterarg
(= safety
0)) poparg
448 (signal 'wrong-number-of-arguments
449 (list ,(and (not (eq cl-bind-block
'cl-none
))
451 (length ,restarg
)))))))
452 (setq num
(1+ num
) laterarg t
))
453 (while (and (eq (car args
) '&optional
) (pop args
))
454 (while (and args
(not (memq (car args
) cl-lambda-list-keywords
)))
455 (let ((arg (pop args
)))
456 (or (consp arg
) (setq arg
(list arg
)))
457 (if (cddr arg
) (cl-do-arglist (nth 2 arg
) `(and ,restarg t
)))
458 (let ((def (if (cdr arg
) (nth 1 arg
)
459 (or (car cl-bind-defs
)
460 (nth 1 (assq (car arg
) cl-bind-defs
)))))
461 (poparg `(pop ,restarg
)))
462 (and def cl-bind-enquote
(setq def
`',def
))
463 (cl-do-arglist (car arg
)
464 (if def
`(if ,restarg
,poparg
,def
) poparg
))
465 (setq num
(1+ num
))))))
466 (if (eq (car args
) '&rest
)
467 (let ((arg (cl-pop2 args
)))
468 (if (consp arg
) (cl-do-arglist arg restarg
)))
469 (or (eq (car args
) '&key
) (= safety
0) exactarg
471 (signal 'wrong-number-of-arguments
473 ,(and (not (eq cl-bind-block
'cl-none
))
475 (+ ,num
(length ,restarg
)))))
477 (while (and (eq (car args
) '&key
) (pop args
))
478 (while (and args
(not (memq (car args
) cl-lambda-list-keywords
)))
479 (let ((arg (pop args
)))
480 (or (consp arg
) (setq arg
(list arg
)))
481 (let* ((karg (if (consp (car arg
)) (caar arg
)
482 (intern (format ":%s" (car arg
)))))
483 (varg (if (consp (car arg
)) (cl-cadar arg
) (car arg
)))
484 (def (if (cdr arg
) (cadr arg
)
485 (or (car cl-bind-defs
) (cadr (assq varg cl-bind-defs
)))))
486 (look `(memq ',karg
,restarg
)))
487 (and def cl-bind-enquote
(setq def
`',def
))
489 (let* ((temp (or (nth 2 arg
) (make-symbol "--cl-var--")))
490 (val `(car (cdr ,temp
))))
491 (cl-do-arglist temp look
)
494 (prog1 ,val
(setq ,temp t
))
498 `(car (cdr ,(if (null def
)
501 ,(if (eq (cl-const-expr-p def
) t
)
502 `'(nil ,(cl-const-expr-val def
))
503 `(list nil
,def
))))))))
505 (setq keys
(nreverse keys
))
506 (or (and (eq (car args
) '&allow-other-keys
) (pop args
))
507 (null keys
) (= safety
0)
508 (let* ((var (make-symbol "--cl-keys--"))
509 (allow '(:allow-other-keys
))
512 ((memq (car ,var
) ',(append keys allow
))
513 (setq ,var
(cdr (cdr ,var
))))
514 ((car (cdr (memq (quote ,@allow
) ,restarg
)))
518 ,(format "Keyword argument %%s not one of %s"
521 (push `(let ((,var
,restarg
)) ,check
) cl-bind-forms
)))
522 (while (and (eq (car args
) '&aux
) (pop args
))
523 (while (and args
(not (memq (car args
) cl-lambda-list-keywords
)))
524 (if (consp (car args
))
525 (if (and cl-bind-enquote
(cl-cadar args
))
526 (cl-do-arglist (caar args
)
527 `',(cadr (pop args
)))
528 (cl-do-arglist (caar args
) (cadr (pop args
))))
529 (cl-do-arglist (pop args
) nil
))))
530 (if args
(error "Malformed argument list %s" save-args
)))))
532 (defun cl-arglist-args (args)
533 (if (nlistp args
) (list args
)
534 (let ((res nil
) (kind nil
) arg
)
536 (setq arg
(pop args
))
537 (if (memq arg cl-lambda-list-keywords
) (setq kind arg
)
538 (if (eq arg
'&cl-defs
) (pop args
)
539 (and (consp arg
) kind
(setq arg
(car arg
)))
540 (and (consp arg
) (cdr arg
) (eq kind
'&key
) (setq arg
(cadr arg
)))
541 (setq res
(nconc res
(cl-arglist-args arg
))))))
542 (nconc res
(and args
(list args
))))))
545 (defmacro cl-destructuring-bind
(args expr
&rest body
)
547 (debug (&define cl-macro-list def-form cl-declarations def-body
)))
548 (let* ((cl-bind-lets nil
) (cl-bind-forms nil
) (cl-bind-inits nil
)
549 (cl-bind-defs nil
) (cl-bind-block 'cl-none
) (cl-bind-enquote nil
))
550 (cl-do-arglist (or args
'(&aux
)) expr
)
551 (append '(progn) cl-bind-inits
552 (list `(let* ,(nreverse cl-bind-lets
)
553 ,@(nreverse cl-bind-forms
) ,@body
)))))
556 ;;; The `cl-eval-when' form.
558 (defvar cl-not-toplevel nil
)
561 (defmacro cl-eval-when
(when &rest body
)
562 "Control when BODY is evaluated.
563 If `compile' is in WHEN, BODY is evaluated when compiled at top-level.
564 If `load' is in WHEN, BODY is evaluated when loaded after top-level compile.
565 If `eval' is in WHEN, BODY is evaluated when interpreted or at non-top-level.
567 \(fn (WHEN...) BODY...)"
568 (declare (indent 1) (debug ((&rest
&or
"compile" "load" "eval") body
)))
569 (if (and (fboundp 'cl-compiling-file
) (cl-compiling-file)
570 (not cl-not-toplevel
) (not (boundp 'for-effect
))) ; horrible kludge
571 (let ((comp (or (memq 'compile when
) (memq :compile-toplevel when
)))
573 (if (or (memq 'load when
) (memq :load-toplevel when
))
574 (if comp
(cons 'progn
(mapcar 'cl-compile-time-too body
))
575 `(if nil nil
,@body
))
576 (progn (if comp
(eval (cons 'progn body
))) nil
)))
577 (and (or (memq 'eval when
) (memq :execute when
))
578 (cons 'progn body
))))
580 (defun cl-compile-time-too (form)
581 (or (and (symbolp (car-safe form
)) (get (car-safe form
) 'byte-hunk-handler
))
582 (setq form
(macroexpand
583 form
(cons '(cl-eval-when) byte-compile-macro-environment
))))
584 (cond ((eq (car-safe form
) 'progn
)
585 (cons 'progn
(mapcar 'cl-compile-time-too
(cdr form
))))
586 ((eq (car-safe form
) 'cl-eval-when
)
587 (let ((when (nth 1 form
)))
588 (if (or (memq 'eval when
) (memq :execute when
))
589 `(cl-eval-when (compile ,@when
) ,@(cddr form
))
591 (t (eval form
) form
)))
594 (defmacro cl-load-time-value
(form &optional read-only
)
595 "Like `progn', but evaluates the body at load time.
596 The result of the body appears to the compiler as a quoted constant."
597 (declare (debug (form &optional sexp
)))
598 (if (cl-compiling-file)
599 (let* ((temp (cl-gentemp "--cl-load-time--"))
600 (set `(set ',temp
,form
)))
601 (if (and (fboundp 'byte-compile-file-form-defmumble
)
602 (boundp 'this-kind
) (boundp 'that-one
))
603 (fset 'byte-compile-file-form
605 (fset 'byte-compile-file-form
606 ',(symbol-function 'byte-compile-file-form
))
607 (byte-compile-file-form ',set
)
608 (byte-compile-file-form form
)))
609 (print set
(symbol-value 'byte-compile--outbuffer
)))
610 `(symbol-value ',temp
))
614 ;;; Conditional control structures.
617 (defmacro cl-case
(expr &rest clauses
)
618 "Eval EXPR and choose among clauses on that value.
619 Each clause looks like (KEYLIST BODY...). EXPR is evaluated and compared
620 against each key in each KEYLIST; the corresponding BODY is evaluated.
621 If no clause succeeds, cl-case returns nil. A single atom may be used in
622 place of a KEYLIST of one atom. A KEYLIST of t or `otherwise' is
623 allowed only in the final clause, and matches if no other keys match.
624 Key values are compared by `eql'.
625 \n(fn EXPR (KEYLIST BODY...)...)"
626 (declare (indent 1) (debug (form &rest
(sexp body
))))
627 (let* ((temp (if (cl-simple-expr-p expr
3) expr
(make-symbol "--cl-var--")))
634 (cons (cond ((memq (car c
) '(t otherwise
)) t
)
635 ((eq (car c
) 'cl--ecase-error-flag
)
636 `(error "cl-ecase failed: %s, %s"
637 ,temp
',(reverse head-list
)))
639 (setq head-list
(append (car c
) head-list
))
640 `(cl-member ,temp
',(car c
)))
642 (if (memq (car c
) head-list
)
643 (error "Duplicate key in case: %s"
645 (push (car c
) head-list
)
646 `(eql ,temp
',(car c
))))
647 (or (cdr c
) '(nil)))))
649 (if (eq temp expr
) body
650 `(let ((,temp
,expr
)) ,body
))))
653 (defmacro cl-ecase
(expr &rest clauses
)
654 "Like `cl-case', but error if no cl-case fits.
655 `otherwise'-clauses are not allowed.
656 \n(fn EXPR (KEYLIST BODY...)...)"
657 (declare (indent 1) (debug cl-case
))
658 `(cl-case ,expr
,@clauses
(cl--ecase-error-flag)))
661 (defmacro cl-typecase
(expr &rest clauses
)
662 "Evals EXPR, chooses among clauses on that value.
663 Each clause looks like (TYPE BODY...). EXPR is evaluated and, if it
664 satisfies TYPE, the corresponding BODY is evaluated. If no clause succeeds,
665 cl-typecase returns nil. A TYPE of t or `otherwise' is allowed only in the
666 final clause, and matches if no other keys match.
667 \n(fn EXPR (TYPE BODY...)...)"
669 (debug (form &rest
([&or cl-type-spec
"otherwise"] body
))))
670 (let* ((temp (if (cl-simple-expr-p expr
3) expr
(make-symbol "--cl-var--")))
677 (cons (cond ((eq (car c
) 'otherwise
) t
)
678 ((eq (car c
) 'cl--ecase-error-flag
)
679 `(error "cl-etypecase failed: %s, %s"
680 ,temp
',(reverse type-list
)))
682 (push (car c
) type-list
)
683 (cl-make-type-test temp
(car c
))))
684 (or (cdr c
) '(nil)))))
686 (if (eq temp expr
) body
687 `(let ((,temp
,expr
)) ,body
))))
690 (defmacro cl-etypecase
(expr &rest clauses
)
691 "Like `cl-typecase', but error if no case fits.
692 `otherwise'-clauses are not allowed.
693 \n(fn EXPR (TYPE BODY...)...)"
694 (declare (indent 1) (debug cl-typecase
))
695 `(cl-typecase ,expr
,@clauses
(cl--ecase-error-flag)))
698 ;;; Blocks and exits.
701 (defmacro cl-block
(name &rest body
)
702 "Define a lexically-scoped block named NAME.
703 NAME may be any symbol. Code inside the BODY forms can call `cl-return-from'
704 to jump prematurely out of the block. This differs from `catch' and `throw'
705 in two respects: First, the NAME is an unevaluated symbol rather than a
706 quoted symbol or other form; and second, NAME is lexically rather than
707 dynamically scoped: Only references to it within BODY will work. These
708 references may appear inside macro expansions, but not inside functions
710 (declare (indent 1) (debug (symbolp body
)))
711 (if (cl-safe-expr-p `(progn ,@body
)) `(progn ,@body
)
713 (catch ',(intern (format "--cl-block-%s--" name
))
717 (defmacro cl-return
(&optional result
)
718 "Return from the block named nil.
719 This is equivalent to `(cl-return-from nil RESULT)'."
720 (declare (debug (&optional form
)))
721 `(cl-return-from nil
,result
))
724 (defmacro cl-return-from
(name &optional result
)
725 "Return from the block named NAME.
726 This jumps out to the innermost enclosing `(cl-block NAME ...)' form,
727 returning RESULT from that form (or nil if RESULT is omitted).
728 This is compatible with Common Lisp, but note that `defun' and
729 `defmacro' do not create implicit blocks as they do in Common Lisp."
730 (declare (indent 1) (debug (symbolp &optional form
)))
731 (let ((name2 (intern (format "--cl-block-%s--" name
))))
732 `(cl-block-throw ',name2
,result
)))
735 ;;; The "cl-loop" macro.
737 (defvar cl-loop-args
) (defvar cl-loop-accum-var
) (defvar cl-loop-accum-vars
)
738 (defvar cl-loop-bindings
) (defvar cl-loop-body
) (defvar cl-loop-destr-temps
)
739 (defvar cl-loop-finally
) (defvar cl-loop-finish-flag
)
740 (defvar cl-loop-first-flag
)
741 (defvar cl-loop-initially
) (defvar cl-loop-map-form
) (defvar cl-loop-name
)
742 (defvar cl-loop-result
) (defvar cl-loop-result-explicit
)
743 (defvar cl-loop-result-var
) (defvar cl-loop-steps
) (defvar cl-loop-symbol-macs
)
746 (defmacro cl-loop
(&rest cl-loop-args
)
747 "The Common Lisp `cl-loop' macro.
749 for VAR from/upfrom/downfrom NUM to/upto/downto/above/below NUM by NUM,
750 for VAR in LIST by FUNC, for VAR on LIST by FUNC, for VAR = INIT then EXPR,
751 for VAR across ARRAY, repeat NUM, with VAR = INIT, while COND, until COND,
752 always COND, never COND, thereis COND, collect EXPR into VAR,
753 append EXPR into VAR, nconc EXPR into VAR, sum EXPR into VAR,
754 count EXPR into VAR, maximize EXPR into VAR, minimize EXPR into VAR,
755 if COND CLAUSE [and CLAUSE]... else CLAUSE [and CLAUSE...],
756 unless COND CLAUSE [and CLAUSE]... else CLAUSE [and CLAUSE...],
757 do EXPRS..., initially EXPRS..., finally EXPRS..., return EXPR,
758 finally return EXPR, named NAME.
761 (declare (debug (&rest
&or symbolp form
)))
762 (if (not (memq t
(mapcar 'symbolp
(delq nil
(delq t
(cl-copy-list cl-loop-args
))))))
763 `(cl-block nil
(while t
,@cl-loop-args
))
764 (let ((cl-loop-name nil
) (cl-loop-bindings nil
)
765 (cl-loop-body nil
) (cl-loop-steps nil
)
766 (cl-loop-result nil
) (cl-loop-result-explicit nil
)
767 (cl-loop-result-var nil
) (cl-loop-finish-flag nil
)
768 (cl-loop-accum-var nil
) (cl-loop-accum-vars nil
)
769 (cl-loop-initially nil
) (cl-loop-finally nil
)
770 (cl-loop-map-form nil
) (cl-loop-first-flag nil
)
771 (cl-loop-destr-temps nil
) (cl-loop-symbol-macs nil
))
772 (setq cl-loop-args
(append cl-loop-args
'(cl-end-loop)))
773 (while (not (eq (car cl-loop-args
) 'cl-end-loop
)) (cl-parse-loop-clause))
774 (if cl-loop-finish-flag
775 (push `((,cl-loop-finish-flag t
)) cl-loop-bindings
))
776 (if cl-loop-first-flag
777 (progn (push `((,cl-loop-first-flag t
)) cl-loop-bindings
)
778 (push `(setq ,cl-loop-first-flag nil
) cl-loop-steps
)))
779 (let* ((epilogue (nconc (nreverse cl-loop-finally
)
780 (list (or cl-loop-result-explicit cl-loop-result
))))
781 (ands (cl-loop-build-ands (nreverse cl-loop-body
)))
782 (while-body (nconc (cadr ands
) (nreverse cl-loop-steps
)))
784 (nreverse cl-loop-initially
)
785 (list (if cl-loop-map-form
786 `(cl-block --cl-finish--
788 (if (eq (car ands
) t
) while-body
789 (cons `(or ,(car ands
)
790 (cl-return-from --cl-finish--
793 '--cl-map cl-loop-map-form
))
794 `(while ,(car ands
) ,@while-body
)))
795 (if cl-loop-finish-flag
796 (if (equal epilogue
'(nil)) (list cl-loop-result-var
)
797 `((if ,cl-loop-finish-flag
798 (progn ,@epilogue
) ,cl-loop-result-var
)))
800 (if cl-loop-result-var
(push (list cl-loop-result-var
) cl-loop-bindings
))
801 (while cl-loop-bindings
802 (if (cdar cl-loop-bindings
)
803 (setq body
(list (cl-loop-let (pop cl-loop-bindings
) body t
)))
805 (while (and cl-loop-bindings
806 (not (cdar cl-loop-bindings
)))
807 (push (car (pop cl-loop-bindings
)) lets
))
808 (setq body
(list (cl-loop-let lets body nil
))))))
809 (if cl-loop-symbol-macs
810 (setq body
(list `(cl-symbol-macrolet ,cl-loop-symbol-macs
,@body
))))
811 `(cl-block ,cl-loop-name
,@body
)))))
813 ;; Below is a complete spec for cl-loop, in several parts that correspond
814 ;; to the syntax given in CLtL2. The specs do more than specify where
815 ;; the forms are; it also specifies, as much as Edebug allows, all the
816 ;; syntactically valid cl-loop clauses. The disadvantage of this
817 ;; completeness is rigidity, but the "for ... being" clause allows
818 ;; arbitrary extensions of the form: [symbolp &rest &or symbolp form].
820 ;; (def-edebug-spec cl-loop
821 ;; ([&optional ["named" symbolp]]
827 ;; loop-initial-final]
828 ;; [&rest loop-clause]
831 ;; (def-edebug-spec loop-with
834 ;; [&optional ["=" form]]
835 ;; &rest ["and" loop-var
837 ;; [&optional ["=" form]]]))
839 ;; (def-edebug-spec loop-for-as
840 ;; ([&or "for" "as"] loop-for-as-subclause
841 ;; &rest ["and" loop-for-as-subclause]))
843 ;; (def-edebug-spec loop-for-as-subclause
847 ;; [[&or "in" "on" "in-ref" "across-ref"]
848 ;; form &optional ["by" function-form]]
850 ;; ["=" form &optional ["then" form]]
853 ;; [&or "the" "each"]
855 ;; [[&or "element" "elements"]
856 ;; [&or "of" "in" "of-ref"] form
857 ;; &optional "using" ["index" symbolp]];; is this right?
858 ;; [[&or "hash-key" "hash-keys"
859 ;; "hash-value" "hash-values"]
861 ;; hash-table-p &optional ["using" ([&or "hash-value" "hash-values"
862 ;; "hash-key" "hash-keys"] sexp)]]
864 ;; [[&or "symbol" "present-symbol" "external-symbol"
865 ;; "symbols" "present-symbols" "external-symbols"]
866 ;; [&or "in" "of"] package-p]
868 ;; ;; Extensions for Emacs Lisp, including Lucid Emacs.
869 ;; [[&or "frame" "frames"
870 ;; "screen" "screens"
871 ;; "buffer" "buffers"]]
873 ;; [[&or "window" "windows"]
874 ;; [&or "of" "in"] form]
876 ;; [[&or "overlay" "overlays"
877 ;; "extent" "extents"]
878 ;; [&or "of" "in"] form
879 ;; &optional [[&or "from" "to"] form]]
881 ;; [[&or "interval" "intervals"]
882 ;; [&or "in" "of"] form
883 ;; &optional [[&or "from" "to"] form]
884 ;; ["property" form]]
886 ;; [[&or "key-code" "key-codes"
887 ;; "key-seq" "key-seqs"
888 ;; "key-binding" "key-bindings"]
889 ;; [&or "in" "of"] form
890 ;; &optional ["using" ([&or "key-code" "key-codes"
891 ;; "key-seq" "key-seqs"
892 ;; "key-binding" "key-bindings"]
894 ;; ;; For arbitrary extensions, recognize anything else.
895 ;; [symbolp &rest &or symbolp form]
898 ;; ;; arithmetic - must be last since all parts are optional.
899 ;; [[&optional [[&or "from" "downfrom" "upfrom"] form]]
900 ;; [&optional [[&or "to" "downto" "upto" "below" "above"] form]]
901 ;; [&optional ["by" form]]
904 ;; (def-edebug-spec loop-initial-final
906 ;; ;; [&optional &or "do" "doing"] ;; CLtL2 doesn't allow this.
907 ;; &rest loop-non-atomic-expr]
909 ;; [[&optional &or "do" "doing"] &rest loop-non-atomic-expr]
910 ;; ["return" form]]))
912 ;; (def-edebug-spec loop-and-clause
913 ;; (loop-clause &rest ["and" loop-clause]))
915 ;; (def-edebug-spec loop-clause
917 ;; [[&or "while" "until" "always" "never" "thereis"] form]
919 ;; [[&or "collect" "collecting"
920 ;; "append" "appending"
921 ;; "nconc" "nconcing"
922 ;; "concat" "vconcat"] form
923 ;; [&optional ["into" loop-var]]]
925 ;; [[&or "count" "counting"
927 ;; "maximize" "maximizing"
928 ;; "minimize" "minimizing"] form
929 ;; [&optional ["into" loop-var]]
932 ;; [[&or "if" "when" "unless"]
933 ;; form loop-and-clause
934 ;; [&optional ["else" loop-and-clause]]
935 ;; [&optional "end"]]
937 ;; [[&or "do" "doing"] &rest loop-non-atomic-expr]
940 ;; loop-initial-final
943 ;; (def-edebug-spec loop-non-atomic-expr
944 ;; ([¬ atom] form))
946 ;; (def-edebug-spec loop-var
947 ;; ;; The symbolp must be last alternative to recognize e.g. (a b . c)
949 ;; ;; (loop-var . [&or nil loop-var])
950 ;; ;; (symbolp . [&or nil loop-var])
951 ;; ;; (symbolp . loop-var)
952 ;; ;; (symbolp . (symbolp . [&or nil loop-var]))
953 ;; ;; (symbolp . (symbolp . loop-var))
954 ;; ;; (symbolp . (symbolp . symbolp)) == (symbolp symbolp . symbolp)
955 ;; (&or (loop-var . [&or nil loop-var]) [gate symbolp]))
957 ;; (def-edebug-spec loop-type-spec
958 ;; (&optional ["of-type" loop-d-type-spec]))
960 ;; (def-edebug-spec loop-d-type-spec
961 ;; (&or (loop-d-type-spec . [&or nil loop-d-type-spec]) cl-type-spec))
965 (defun cl-parse-loop-clause () ; uses loop-*
966 (let ((word (pop cl-loop-args
))
967 (hash-types '(hash-key hash-keys hash-value hash-values
))
968 (key-types '(key-code key-codes key-seq key-seqs
969 key-binding key-bindings
)))
973 (error "Malformed `cl-loop' macro"))
976 (setq cl-loop-name
(pop cl-loop-args
)))
978 ((eq word
'initially
)
979 (if (memq (car cl-loop-args
) '(do doing
)) (pop cl-loop-args
))
980 (or (consp (car cl-loop-args
)) (error "Syntax error on `initially' clause"))
981 (while (consp (car cl-loop-args
))
982 (push (pop cl-loop-args
) cl-loop-initially
)))
985 (if (eq (car cl-loop-args
) 'return
)
986 (setq cl-loop-result-explicit
(or (cl-pop2 cl-loop-args
) '(quote nil
)))
987 (if (memq (car cl-loop-args
) '(do doing
)) (pop cl-loop-args
))
988 (or (consp (car cl-loop-args
)) (error "Syntax error on `finally' clause"))
989 (if (and (eq (caar cl-loop-args
) 'return
) (null cl-loop-name
))
990 (setq cl-loop-result-explicit
(or (nth 1 (pop cl-loop-args
)) '(quote nil
)))
991 (while (consp (car cl-loop-args
))
992 (push (pop cl-loop-args
) cl-loop-finally
)))))
994 ((memq word
'(for as
))
995 (let ((loop-for-bindings nil
) (loop-for-sets nil
) (loop-for-steps nil
)
998 ;; Use `cl-gensym' rather than `make-symbol'. It's important that
999 ;; (not (eq (symbol-name var1) (symbol-name var2))) because
1000 ;; these vars get added to the cl-macro-environment.
1001 (let ((var (or (pop cl-loop-args
) (cl-gensym "--cl-var--"))))
1002 (setq word
(pop cl-loop-args
))
1003 (if (eq word
'being
) (setq word
(pop cl-loop-args
)))
1004 (if (memq word
'(the each
)) (setq word
(pop cl-loop-args
)))
1005 (if (memq word
'(buffer buffers
))
1006 (setq word
'in cl-loop-args
(cons '(buffer-list) cl-loop-args
)))
1009 ((memq word
'(from downfrom upfrom to downto upto
1011 (push word cl-loop-args
)
1012 (if (memq (car cl-loop-args
) '(downto above
))
1013 (error "Must specify `from' value for downward cl-loop"))
1014 (let* ((down (or (eq (car cl-loop-args
) 'downfrom
)
1015 (memq (cl-caddr cl-loop-args
) '(downto above
))))
1016 (excl (or (memq (car cl-loop-args
) '(above below
))
1017 (memq (cl-caddr cl-loop-args
) '(above below
))))
1018 (start (and (memq (car cl-loop-args
) '(from upfrom downfrom
))
1019 (cl-pop2 cl-loop-args
)))
1020 (end (and (memq (car cl-loop-args
)
1021 '(to upto downto above below
))
1022 (cl-pop2 cl-loop-args
)))
1023 (step (and (eq (car cl-loop-args
) 'by
) (cl-pop2 cl-loop-args
)))
1024 (end-var (and (not (cl-const-expr-p end
))
1025 (make-symbol "--cl-var--")))
1026 (step-var (and (not (cl-const-expr-p step
))
1027 (make-symbol "--cl-var--"))))
1028 (and step
(numberp step
) (<= step
0)
1029 (error "Loop `by' value is not positive: %s" step
))
1030 (push (list var
(or start
0)) loop-for-bindings
)
1031 (if end-var
(push (list end-var end
) loop-for-bindings
))
1032 (if step-var
(push (list step-var step
)
1036 (if down
(if excl
'> '>=) (if excl
'< '<=))
1037 var
(or end-var end
)) cl-loop-body
))
1038 (push (list var
(list (if down
'-
'+) var
1039 (or step-var step
1)))
1042 ((memq word
'(in in-ref on
))
1043 (let* ((on (eq word
'on
))
1044 (temp (if (and on
(symbolp var
))
1045 var
(make-symbol "--cl-var--"))))
1046 (push (list temp
(pop cl-loop-args
)) loop-for-bindings
)
1047 (push `(consp ,temp
) cl-loop-body
)
1048 (if (eq word
'in-ref
)
1049 (push (list var
`(car ,temp
)) cl-loop-symbol-macs
)
1052 (push (list var nil
) loop-for-bindings
)
1053 (push (list var
(if on temp
`(car ,temp
)))
1056 (if (eq (car cl-loop-args
) 'by
)
1057 (let ((step (cl-pop2 cl-loop-args
)))
1058 (if (and (memq (car-safe step
)
1061 (symbolp (nth 1 step
)))
1062 (list (nth 1 step
) temp
)
1063 `(funcall ,step
,temp
)))
1068 (let* ((start (pop cl-loop-args
))
1069 (then (if (eq (car cl-loop-args
) 'then
) (cl-pop2 cl-loop-args
) start
)))
1070 (push (list var nil
) loop-for-bindings
)
1071 (if (or ands
(eq (car cl-loop-args
) 'and
))
1074 (if ,(or cl-loop-first-flag
1075 (setq cl-loop-first-flag
1076 (make-symbol "--cl-var--")))
1079 (push (list var then
) loop-for-steps
))
1081 (if (eq start then
) start
1082 `(if ,(or cl-loop-first-flag
1083 (setq cl-loop-first-flag
1084 (make-symbol "--cl-var--")))
1088 ((memq word
'(across across-ref
))
1089 (let ((temp-vec (make-symbol "--cl-vec--"))
1090 (temp-idx (make-symbol "--cl-idx--")))
1091 (push (list temp-vec
(pop cl-loop-args
)) loop-for-bindings
)
1092 (push (list temp-idx -
1) loop-for-bindings
)
1093 (push `(< (setq ,temp-idx
(1+ ,temp-idx
))
1094 (length ,temp-vec
)) cl-loop-body
)
1095 (if (eq word
'across-ref
)
1096 (push (list var
`(aref ,temp-vec
,temp-idx
))
1097 cl-loop-symbol-macs
)
1098 (push (list var nil
) loop-for-bindings
)
1099 (push (list var
`(aref ,temp-vec
,temp-idx
))
1102 ((memq word
'(element elements
))
1103 (let ((ref (or (memq (car cl-loop-args
) '(in-ref of-ref
))
1104 (and (not (memq (car cl-loop-args
) '(in of
)))
1105 (error "Expected `of'"))))
1106 (seq (cl-pop2 cl-loop-args
))
1107 (temp-seq (make-symbol "--cl-seq--"))
1108 (temp-idx (if (eq (car cl-loop-args
) 'using
)
1109 (if (and (= (length (cadr cl-loop-args
)) 2)
1110 (eq (cl-caadr cl-loop-args
) 'index
))
1111 (cadr (cl-pop2 cl-loop-args
))
1112 (error "Bad `using' clause"))
1113 (make-symbol "--cl-idx--"))))
1114 (push (list temp-seq seq
) loop-for-bindings
)
1115 (push (list temp-idx
0) loop-for-bindings
)
1117 (let ((temp-len (make-symbol "--cl-len--")))
1118 (push (list temp-len
`(length ,temp-seq
))
1120 (push (list var
`(elt ,temp-seq temp-idx
))
1121 cl-loop-symbol-macs
)
1122 (push `(< ,temp-idx
,temp-len
) cl-loop-body
))
1123 (push (list var nil
) loop-for-bindings
)
1124 (push `(and ,temp-seq
1125 (or (consp ,temp-seq
)
1126 (< ,temp-idx
(length ,temp-seq
))))
1128 (push (list var
`(if (consp ,temp-seq
)
1130 (aref ,temp-seq
,temp-idx
)))
1132 (push (list temp-idx
`(1+ ,temp-idx
))
1135 ((memq word hash-types
)
1136 (or (memq (car cl-loop-args
) '(in of
)) (error "Expected `of'"))
1137 (let* ((table (cl-pop2 cl-loop-args
))
1138 (other (if (eq (car cl-loop-args
) 'using
)
1139 (if (and (= (length (cadr cl-loop-args
)) 2)
1140 (memq (cl-caadr cl-loop-args
) hash-types
)
1141 (not (eq (cl-caadr cl-loop-args
) word
)))
1142 (cadr (cl-pop2 cl-loop-args
))
1143 (error "Bad `using' clause"))
1144 (make-symbol "--cl-var--"))))
1145 (if (memq word
'(hash-value hash-values
))
1146 (setq var
(prog1 other
(setq other var
))))
1147 (setq cl-loop-map-form
1148 `(maphash (lambda (,var
,other
) . --cl-map
) ,table
))))
1150 ((memq word
'(symbol present-symbol external-symbol
1151 symbols present-symbols external-symbols
))
1152 (let ((ob (and (memq (car cl-loop-args
) '(in of
)) (cl-pop2 cl-loop-args
))))
1153 (setq cl-loop-map-form
1154 `(mapatoms (lambda (,var
) . --cl-map
) ,ob
))))
1156 ((memq word
'(overlay overlays extent extents
))
1157 (let ((buf nil
) (from nil
) (to nil
))
1158 (while (memq (car cl-loop-args
) '(in of from to
))
1159 (cond ((eq (car cl-loop-args
) 'from
) (setq from
(cl-pop2 cl-loop-args
)))
1160 ((eq (car cl-loop-args
) 'to
) (setq to
(cl-pop2 cl-loop-args
)))
1161 (t (setq buf
(cl-pop2 cl-loop-args
)))))
1162 (setq cl-loop-map-form
1164 (lambda (,var
,(make-symbol "--cl-var--"))
1165 (progn . --cl-map
) nil
)
1168 ((memq word
'(interval intervals
))
1169 (let ((buf nil
) (prop nil
) (from nil
) (to nil
)
1170 (var1 (make-symbol "--cl-var1--"))
1171 (var2 (make-symbol "--cl-var2--")))
1172 (while (memq (car cl-loop-args
) '(in of property from to
))
1173 (cond ((eq (car cl-loop-args
) 'from
) (setq from
(cl-pop2 cl-loop-args
)))
1174 ((eq (car cl-loop-args
) 'to
) (setq to
(cl-pop2 cl-loop-args
)))
1175 ((eq (car cl-loop-args
) 'property
)
1176 (setq prop
(cl-pop2 cl-loop-args
)))
1177 (t (setq buf
(cl-pop2 cl-loop-args
)))))
1178 (if (and (consp var
) (symbolp (car var
)) (symbolp (cdr var
)))
1179 (setq var1
(car var
) var2
(cdr var
))
1180 (push (list var
`(cons ,var1
,var2
)) loop-for-sets
))
1181 (setq cl-loop-map-form
1183 (lambda (,var1
,var2
) . --cl-map
)
1184 ,buf
,prop
,from
,to
))))
1186 ((memq word key-types
)
1187 (or (memq (car cl-loop-args
) '(in of
)) (error "Expected `of'"))
1188 (let ((cl-map (cl-pop2 cl-loop-args
))
1189 (other (if (eq (car cl-loop-args
) 'using
)
1190 (if (and (= (length (cadr cl-loop-args
)) 2)
1191 (memq (cl-caadr cl-loop-args
) key-types
)
1192 (not (eq (cl-caadr cl-loop-args
) word
)))
1193 (cadr (cl-pop2 cl-loop-args
))
1194 (error "Bad `using' clause"))
1195 (make-symbol "--cl-var--"))))
1196 (if (memq word
'(key-binding key-bindings
))
1197 (setq var
(prog1 other
(setq other var
))))
1198 (setq cl-loop-map-form
1199 `(,(if (memq word
'(key-seq key-seqs
))
1200 'cl-map-keymap-recursively
'map-keymap
)
1201 (lambda (,var
,other
) . --cl-map
) ,cl-map
))))
1203 ((memq word
'(frame frames screen screens
))
1204 (let ((temp (make-symbol "--cl-var--")))
1205 (push (list var
'(selected-frame))
1207 (push (list temp nil
) loop-for-bindings
)
1208 (push `(prog1 (not (eq ,var
,temp
))
1209 (or ,temp
(setq ,temp
,var
)))
1211 (push (list var
`(next-frame ,var
))
1214 ((memq word
'(window windows
))
1215 (let ((scr (and (memq (car cl-loop-args
) '(in of
)) (cl-pop2 cl-loop-args
)))
1216 (temp (make-symbol "--cl-var--"))
1217 (minip (make-symbol "--cl-minip--")))
1218 (push (list var
(if scr
1219 `(frame-selected-window ,scr
)
1220 '(selected-window)))
1222 ;; If we started in the minibuffer, we need to
1223 ;; ensure that next-window will bring us back there
1224 ;; at some point. (Bug#7492).
1225 ;; (Consider using walk-windows instead of cl-loop if
1226 ;; you care about such things.)
1227 (push (list minip
`(minibufferp (window-buffer ,var
)))
1229 (push (list temp nil
) loop-for-bindings
)
1230 (push `(prog1 (not (eq ,var
,temp
))
1231 (or ,temp
(setq ,temp
,var
)))
1233 (push (list var
`(next-window ,var
,minip
))
1237 (let ((handler (and (symbolp word
)
1238 (get word
'cl-loop-for-handler
))))
1240 (funcall handler var
)
1241 (error "Expected a `for' preposition, found %s" word
)))))
1242 (eq (car cl-loop-args
) 'and
))
1245 (if (and ands loop-for-bindings
)
1246 (push (nreverse loop-for-bindings
) cl-loop-bindings
)
1247 (setq cl-loop-bindings
(nconc (mapcar 'list loop-for-bindings
)
1251 ,(cl-loop-let (nreverse loop-for-sets
) 'setq ands
)
1254 (push (cons (if ands
'cl-psetq
'setq
)
1255 (apply 'append
(nreverse loop-for-steps
)))
1259 (let ((temp (make-symbol "--cl-var--")))
1260 (push (list (list temp
(pop cl-loop-args
))) cl-loop-bindings
)
1261 (push `(>= (setq ,temp
(1- ,temp
)) 0) cl-loop-body
)))
1263 ((memq word
'(collect collecting
))
1264 (let ((what (pop cl-loop-args
))
1265 (var (cl-loop-handle-accum nil
'nreverse
)))
1266 (if (eq var cl-loop-accum-var
)
1267 (push `(progn (push ,what
,var
) t
) cl-loop-body
)
1269 (setq ,var
(nconc ,var
(list ,what
)))
1272 ((memq word
'(nconc nconcing append appending
))
1273 (let ((what (pop cl-loop-args
))
1274 (var (cl-loop-handle-accum nil
'nreverse
)))
1277 ,(if (eq var cl-loop-accum-var
)
1279 (,(if (memq word
'(nconc nconcing
))
1280 #'nreverse
#'reverse
)
1283 `(,(if (memq word
'(nconc nconcing
))
1285 ,var
,what
))) t
) cl-loop-body
)))
1287 ((memq word
'(concat concating
))
1288 (let ((what (pop cl-loop-args
))
1289 (var (cl-loop-handle-accum "")))
1290 (push `(progn (cl-callf concat
,var
,what
) t
) cl-loop-body
)))
1292 ((memq word
'(vconcat vconcating
))
1293 (let ((what (pop cl-loop-args
))
1294 (var (cl-loop-handle-accum [])))
1295 (push `(progn (cl-callf vconcat
,var
,what
) t
) cl-loop-body
)))
1297 ((memq word
'(sum summing
))
1298 (let ((what (pop cl-loop-args
))
1299 (var (cl-loop-handle-accum 0)))
1300 (push `(progn (cl-incf ,var
,what
) t
) cl-loop-body
)))
1302 ((memq word
'(count counting
))
1303 (let ((what (pop cl-loop-args
))
1304 (var (cl-loop-handle-accum 0)))
1305 (push `(progn (if ,what
(cl-incf ,var
)) t
) cl-loop-body
)))
1307 ((memq word
'(minimize minimizing maximize maximizing
))
1308 (let* ((what (pop cl-loop-args
))
1309 (temp (if (cl-simple-expr-p what
) what
(make-symbol "--cl-var--")))
1310 (var (cl-loop-handle-accum nil
))
1311 (func (intern (substring (symbol-name word
) 0 3)))
1312 (set `(setq ,var
(if ,var
(,func
,var
,temp
) ,temp
))))
1313 (push `(progn ,(if (eq temp what
) set
1314 `(let ((,temp
,what
)) ,set
))
1318 (let ((bindings nil
))
1319 (while (progn (push (list (pop cl-loop-args
)
1320 (and (eq (car cl-loop-args
) '=) (cl-pop2 cl-loop-args
)))
1322 (eq (car cl-loop-args
) 'and
))
1324 (push (nreverse bindings
) cl-loop-bindings
)))
1327 (push (pop cl-loop-args
) cl-loop-body
))
1330 (push `(not ,(pop cl-loop-args
)) cl-loop-body
))
1333 (or cl-loop-finish-flag
(setq cl-loop-finish-flag
(make-symbol "--cl-flag--")))
1334 (push `(setq ,cl-loop-finish-flag
,(pop cl-loop-args
)) cl-loop-body
)
1335 (setq cl-loop-result t
))
1338 (or cl-loop-finish-flag
(setq cl-loop-finish-flag
(make-symbol "--cl-flag--")))
1339 (push `(setq ,cl-loop-finish-flag
(not ,(pop cl-loop-args
)))
1341 (setq cl-loop-result t
))
1344 (or cl-loop-finish-flag
(setq cl-loop-finish-flag
(make-symbol "--cl-flag--")))
1345 (or cl-loop-result-var
(setq cl-loop-result-var
(make-symbol "--cl-var--")))
1346 (push `(setq ,cl-loop-finish-flag
1347 (not (setq ,cl-loop-result-var
,(pop cl-loop-args
))))
1350 ((memq word
'(if when unless
))
1351 (let* ((cond (pop cl-loop-args
))
1352 (then (let ((cl-loop-body nil
))
1353 (cl-parse-loop-clause)
1354 (cl-loop-build-ands (nreverse cl-loop-body
))))
1355 (else (let ((cl-loop-body nil
))
1356 (if (eq (car cl-loop-args
) 'else
)
1357 (progn (pop cl-loop-args
) (cl-parse-loop-clause)))
1358 (cl-loop-build-ands (nreverse cl-loop-body
))))
1359 (simple (and (eq (car then
) t
) (eq (car else
) t
))))
1360 (if (eq (car cl-loop-args
) 'end
) (pop cl-loop-args
))
1361 (if (eq word
'unless
) (setq then
(prog1 else
(setq else then
))))
1362 (let ((form (cons (if simple
(cons 'progn
(nth 1 then
)) (nth 2 then
))
1363 (if simple
(nth 1 else
) (list (nth 2 else
))))))
1364 (if (cl-expr-contains form
'it
)
1365 (let ((temp (make-symbol "--cl-var--")))
1366 (push (list temp
) cl-loop-bindings
)
1367 (setq form
`(if (setq ,temp
,cond
)
1368 ,@(cl-subst temp
'it form
))))
1369 (setq form
`(if ,cond
,@form
)))
1370 (push (if simple
`(progn ,form t
) form
) cl-loop-body
))))
1372 ((memq word
'(do doing
))
1374 (or (consp (car cl-loop-args
)) (error "Syntax error on `do' clause"))
1375 (while (consp (car cl-loop-args
)) (push (pop cl-loop-args
) body
))
1376 (push (cons 'progn
(nreverse (cons t body
))) cl-loop-body
)))
1379 (or cl-loop-finish-flag
(setq cl-loop-finish-flag
(make-symbol "--cl-var--")))
1380 (or cl-loop-result-var
(setq cl-loop-result-var
(make-symbol "--cl-var--")))
1381 (push `(setq ,cl-loop-result-var
,(pop cl-loop-args
)
1382 ,cl-loop-finish-flag nil
) cl-loop-body
))
1385 (let ((handler (and (symbolp word
) (get word
'cl-loop-handler
))))
1386 (or handler
(error "Expected a cl-loop keyword, found %s" word
))
1387 (funcall handler
))))
1388 (if (eq (car cl-loop-args
) 'and
)
1389 (progn (pop cl-loop-args
) (cl-parse-loop-clause)))))
1391 (defun cl-loop-let (specs body par
) ; uses loop-*
1392 (let ((p specs
) (temps nil
) (new nil
))
1393 (while (and p
(or (symbolp (car-safe (car p
))) (null (cl-cadar p
))))
1397 (setq par nil p specs
)
1399 (or (cl-const-expr-p (cl-cadar p
))
1400 (let ((temp (make-symbol "--cl-var--")))
1401 (push (list temp
(cl-cadar p
)) temps
)
1402 (setcar (cdar p
) temp
)))
1405 (if (and (consp (car specs
)) (listp (caar specs
)))
1406 (let* ((spec (caar specs
)) (nspecs nil
)
1407 (expr (cadr (pop specs
)))
1408 (temp (cdr (or (assq spec cl-loop-destr-temps
)
1409 (car (push (cons spec
(or (last spec
0)
1410 (make-symbol "--cl-var--")))
1411 cl-loop-destr-temps
))))))
1412 (push (list temp expr
) new
)
1414 (push (list (pop spec
)
1415 (and expr
(list (if spec
'pop
'car
) temp
)))
1417 (setq specs
(nconc (nreverse nspecs
) specs
)))
1418 (push (pop specs
) new
)))
1420 (let ((set (cons (if par
'cl-psetq
'setq
) (apply 'nconc
(nreverse new
)))))
1421 (if temps
`(let* ,(nreverse temps
) ,set
) set
))
1422 `(,(if par
'let
'let
*)
1423 ,(nconc (nreverse temps
) (nreverse new
)) ,@body
))))
1425 (defun cl-loop-handle-accum (def &optional func
) ; uses loop-*
1426 (if (eq (car cl-loop-args
) 'into
)
1427 (let ((var (cl-pop2 cl-loop-args
)))
1428 (or (memq var cl-loop-accum-vars
)
1429 (progn (push (list (list var def
)) cl-loop-bindings
)
1430 (push var cl-loop-accum-vars
)))
1432 (or cl-loop-accum-var
1434 (push (list (list (setq cl-loop-accum-var
(make-symbol "--cl-var--")) def
))
1436 (setq cl-loop-result
(if func
(list func cl-loop-accum-var
)
1438 cl-loop-accum-var
))))
1440 (defun cl-loop-build-ands (clauses)
1444 (if (and (eq (car-safe (car clauses
)) 'progn
)
1445 (eq (car (last (car clauses
))) t
))
1447 (setq clauses
(cons (nconc (butlast (car clauses
))
1448 (if (eq (car-safe (cadr clauses
))
1451 (list (cadr clauses
))))
1453 (setq body
(cdr (butlast (pop clauses
)))))
1454 (push (pop clauses
) ands
)))
1455 (setq ands
(or (nreverse ands
) (list t
)))
1456 (list (if (cdr ands
) (cons 'and ands
) (car ands
))
1458 (let ((full (if body
1459 (append ands
(list (cons 'progn
(append body
'(t)))))
1461 (if (cdr full
) (cons 'and full
) (car full
))))))
1464 ;;; Other iteration control structures.
1467 (defmacro cl-do
(steps endtest
&rest body
)
1468 "The Common Lisp `cl-do' loop.
1470 \(fn ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)"
1473 ((&rest
&or symbolp
(symbolp &optional form form
))
1475 cl-declarations body
)))
1476 (cl-expand-do-loop steps endtest body nil
))
1479 (defmacro cl-do
* (steps endtest
&rest body
)
1480 "The Common Lisp `cl-do*' loop.
1482 \(fn ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)"
1483 (declare (indent 2) (debug cl-do
))
1484 (cl-expand-do-loop steps endtest body t
))
1486 (defun cl-expand-do-loop (steps endtest body star
)
1488 (,(if star
'let
* 'let
)
1489 ,(mapcar (lambda (c) (if (consp c
) (list (car c
) (nth 1 c
)) c
))
1491 (while (not ,(car endtest
))
1493 ,@(let ((sets (mapcar (lambda (c)
1494 (and (consp c
) (cdr (cdr c
))
1495 (list (car c
) (nth 2 c
))))
1497 (setq sets
(delq nil sets
))
1499 (list (cons (if (or star
(not (cdr sets
)))
1501 (apply 'append sets
))))))
1502 ,@(or (cdr endtest
) '(nil)))))
1505 (defmacro cl-dolist
(spec &rest body
)
1507 Evaluate BODY with VAR bound to each `car' from LIST, in turn.
1508 Then evaluate RESULT to get return value, default nil.
1509 An implicit nil block is established around the loop.
1511 \(fn (VAR LIST [RESULT]) BODY...)"
1512 (declare (debug ((symbolp form
&optional form
) cl-declarations body
)))
1513 (let ((temp (make-symbol "--cl-dolist-temp--")))
1514 ;; FIXME: Copy&pasted from subr.el.
1516 ;; This is not a reliable test, but it does not matter because both
1517 ;; semantics are acceptable, tho one is slightly faster with dynamic
1518 ;; scoping and the other is slightly faster (and has cleaner semantics)
1519 ;; with lexical scoping.
1520 ,(if lexical-binding
1521 `(let ((,temp
,(nth 1 spec
)))
1523 (let ((,(car spec
) (car ,temp
)))
1525 (setq ,temp
(cdr ,temp
))))
1526 ,@(if (cdr (cdr spec
))
1527 ;; FIXME: This let often leads to "unused var" warnings.
1528 `((let ((,(car spec
) nil
)) ,@(cdr (cdr spec
))))))
1529 `(let ((,temp
,(nth 1 spec
))
1532 (setq ,(car spec
) (car ,temp
))
1534 (setq ,temp
(cdr ,temp
)))
1535 ,@(if (cdr (cdr spec
))
1536 `((setq ,(car spec
) nil
) ,@(cddr spec
))))))))
1539 (defmacro cl-dotimes
(spec &rest body
)
1540 "Loop a certain number of times.
1541 Evaluate BODY with VAR bound to successive integers from 0, inclusive,
1542 to COUNT, exclusive. Then evaluate RESULT to get return value, default
1545 \(fn (VAR COUNT [RESULT]) BODY...)"
1546 (declare (debug cl-dolist
))
1547 (let ((temp (make-symbol "--cl-dotimes-temp--"))
1549 ;; FIXME: Copy&pasted from subr.el.
1551 ;; This is not a reliable test, but it does not matter because both
1552 ;; semantics are acceptable, tho one is slightly faster with dynamic
1553 ;; scoping and the other has cleaner semantics.
1554 ,(if lexical-binding
1555 (let ((counter '--dotimes-counter--
))
1558 (while (< ,counter
,temp
)
1559 (let ((,(car spec
) ,counter
))
1561 (setq ,counter
(1+ ,counter
)))
1563 ;; FIXME: This let often leads to "unused var" warnings.
1564 `((let ((,(car spec
) ,counter
)) ,@(cddr spec
))))))
1567 (while (< ,(car spec
) ,temp
)
1569 (cl-incf ,(car spec
)))
1570 ,@(cdr (cdr spec
)))))))
1573 (defmacro cl-do-symbols
(spec &rest body
)
1574 "Loop over all symbols.
1575 Evaluate BODY with VAR bound to each interned symbol, or to each symbol
1578 \(fn (VAR [OBARRAY [RESULT]]) BODY...)"
1580 (debug ((symbolp &optional form form
) cl-declarations body
)))
1581 ;; Apparently this doesn't have an implicit block.
1584 (mapatoms #'(lambda (,(car spec
)) ,@body
)
1585 ,@(and (cadr spec
) (list (cadr spec
))))
1589 (defmacro cl-do-all-symbols
(spec &rest body
)
1590 (declare (indent 1) (debug ((symbolp &optional form
) cl-declarations body
)))
1591 `(cl-do-symbols (,(car spec
) nil
,(cadr spec
)) ,@body
))
1597 (defmacro cl-psetq
(&rest args
)
1598 "Set SYMs to the values VALs in parallel.
1599 This is like `setq', except that all VAL forms are evaluated (in order)
1600 before assigning any symbols SYM to the corresponding values.
1602 \(fn SYM VAL SYM VAL ...)"
1603 (declare (debug setq
))
1604 (cons 'cl-psetf args
))
1607 ;;; Binding control structures.
1610 (defmacro cl-progv
(symbols values
&rest body
)
1611 "Bind SYMBOLS to VALUES dynamically in BODY.
1612 The forms SYMBOLS and VALUES are evaluated, and must evaluate to lists.
1613 Each symbol in the first list is bound to the corresponding value in the
1614 second list (or made unbound if VALUES is shorter than SYMBOLS); then the
1615 BODY forms are executed and their result is returned. This is much like
1616 a `let' form, except that the list of symbols can be computed at run-time."
1617 (declare (indent 2) (debug (form form body
)))
1618 `(let ((cl-progv-save nil
))
1620 (progn (cl-progv-before ,symbols
,values
) ,@body
)
1623 ;;; This should really have some way to shadow 'byte-compile properties, etc.
1625 (defmacro cl-flet
(bindings &rest body
)
1626 "Make temporary function definitions.
1627 This is an analogue of `let' that operates on the function cell of FUNC
1628 rather than its value cell. The FORMs are evaluated with the specified
1629 function definitions in place, then the definitions are undone (the FUNCs
1630 go back to their previous definitions, or lack thereof).
1632 \(fn ((FUNC ARGLIST BODY...) ...) FORM...)"
1633 (declare (indent 1) (debug ((&rest
(cl-defun)) cl-declarations body
)))
1636 (if (or (and (fboundp (car x
))
1637 (eq (car-safe (symbol-function (car x
))) 'macro
))
1638 (cdr (assq (car x
) cl-macro-environment
)))
1639 (error "Use `cl-labels', not `cl-flet', to rebind macro names"))
1640 (let ((func `(cl-function
1642 (cl-block ,(car x
) ,@(cddr x
))))))
1643 (when (cl-compiling-file)
1644 ;; Bug#411. It would be nice to fix this.
1645 (and (get (car x
) 'byte-compile
)
1646 (error "Byte-compiling a redefinition of `%s' \
1647 will not work - use `cl-labels' instead" (symbol-name (car x
))))
1648 ;; FIXME This affects the rest of the file, when it
1649 ;; should be restricted to the cl-flet body.
1650 (and (boundp 'byte-compile-function-environment
)
1651 (push (cons (car x
) (eval func
))
1652 byte-compile-function-environment
)))
1653 (list `(symbol-function ',(car x
)) func
)))
1658 (defmacro cl-labels
(bindings &rest body
)
1659 "Make temporary function bindings.
1660 This is like `cl-flet', except the bindings are lexical instead of dynamic.
1661 Unlike `cl-flet', this macro is fully compliant with the Common Lisp standard.
1663 \(fn ((FUNC ARGLIST BODY...) ...) FORM...)"
1664 (declare (indent 1) (debug cl-flet
))
1665 (let ((vars nil
) (sets nil
) (cl-macro-environment cl-macro-environment
))
1667 ;; Use `cl-gensym' rather than `make-symbol'. It's important that
1668 ;; (not (eq (symbol-name var1) (symbol-name var2))) because these
1669 ;; vars get added to the cl-macro-environment.
1670 (let ((var (cl-gensym "--cl-var--")))
1672 (push `(cl-function (lambda .
,(cdar bindings
))) sets
)
1674 (push (list (car (pop bindings
)) 'lambda
'(&rest cl-labels-args
)
1675 `(cl-list* 'funcall
',var
1677 cl-macro-environment
)))
1678 (cl-macroexpand-all `(cl-lexical-let ,vars
(setq ,@sets
) ,@body
)
1679 cl-macro-environment
)))
1681 ;; The following ought to have a better definition for use with newer
1684 (defmacro cl-macrolet
(bindings &rest body
)
1685 "Make temporary macro definitions.
1686 This is like `cl-flet', but for macros instead of functions.
1688 \(fn ((NAME ARGLIST BODY...) ...) FORM...)"
1691 ((&rest
(&define name
(&rest arg
) cl-declarations-or-string
1693 cl-declarations body
)))
1695 `(cl-macrolet (,(car bindings
)) (cl-macrolet ,(cdr bindings
) ,@body
))
1696 (if (null bindings
) (cons 'progn body
)
1697 (let* ((name (caar bindings
))
1698 (res (cl-transform-lambda (cdar bindings
) name
)))
1700 (cl-macroexpand-all (cons 'progn body
)
1701 (cons (cl-list* name
'lambda
(cdr res
))
1702 cl-macro-environment
))))))
1705 (defmacro cl-symbol-macrolet
(bindings &rest body
)
1706 "Make symbol macro definitions.
1707 Within the body FORMs, references to the variable NAME will be replaced
1708 by EXPANSION, and (setq NAME ...) will act like (cl-setf EXPANSION ...).
1710 \(fn ((NAME EXPANSION) ...) FORM...)"
1711 (declare (indent 1) (debug ((&rest
(symbol sexp
)) cl-declarations body
)))
1713 `(cl-symbol-macrolet (,(car bindings
))
1714 (cl-symbol-macrolet ,(cdr bindings
) ,@body
))
1715 (if (null bindings
) (cons 'progn body
)
1716 (cl-macroexpand-all (cons 'progn body
)
1717 (cons (list (symbol-name (caar bindings
))
1718 (cl-cadar bindings
))
1719 cl-macro-environment
)))))
1721 (defvar cl-closure-vars nil
)
1723 (defmacro cl-lexical-let
(bindings &rest body
)
1724 "Like `let', but lexically scoped.
1725 The main visible difference is that lambdas inside BODY will create
1726 lexical closures as in Common Lisp.
1727 \n(fn BINDINGS BODY)"
1728 (declare (indent 1) (debug let
))
1729 (let* ((cl-closure-vars cl-closure-vars
)
1730 (vars (mapcar (function
1732 (or (consp x
) (setq x
(list x
)))
1733 (push (make-symbol (format "--cl-%s--" (car x
)))
1735 (set (car cl-closure-vars
) [bad-lexical-ref
])
1736 (list (car x
) (cadr x
) (car cl-closure-vars
))))
1741 (nconc (mapcar (function (lambda (x)
1742 (list (symbol-name (car x
))
1743 `(symbol-value ,(cl-caddr x
))
1745 (list '(defun . cl-defun-expander
))
1746 cl-macro-environment
))))
1747 (if (not (get (car (last cl-closure-vars
)) 'used
))
1748 ;; Turn (let ((foo (cl-gensym)))
1749 ;; (set foo <val>) ...(symbol-value foo)...)
1750 ;; into (let ((foo <val>)) ...(symbol-value 'foo)...).
1751 ;; This is good because it's more efficient but it only works with
1752 ;; dynamic scoping, since with lexical scoping we'd need
1753 ;; (let ((foo <val>)) ...foo...).
1755 ,@(mapcar (lambda (x) `(defvar ,(cl-caddr x
))) vars
)
1756 (let ,(mapcar (lambda (x) (list (cl-caddr x
) (cadr x
))) vars
)
1757 ,(cl-sublis (mapcar (lambda (x)
1762 `(let ,(mapcar (lambda (x)
1764 `(make-symbol ,(format "--%s--" (car x
)))))
1766 (cl-setf ,@(apply #'append
1768 (list `(symbol-value ,(cl-caddr x
)) (cadr x
)))
1773 (defmacro cl-lexical-let
* (bindings &rest body
)
1774 "Like `let*', but lexically scoped.
1775 The main visible difference is that lambdas inside BODY, and in
1776 successive bindings within BINDINGS, will create lexical closures
1777 as in Common Lisp. This is similar to the behavior of `let*' in
1779 \n(fn BINDINGS BODY)"
1780 (declare (indent 1) (debug let
))
1781 (if (null bindings
) (cons 'progn body
)
1782 (setq bindings
(reverse bindings
))
1784 (setq body
(list `(cl-lexical-let (,(pop bindings
)) ,@body
))))
1787 (defun cl-defun-expander (func &rest rest
)
1789 (defalias ',func
#'(lambda ,@rest
))
1793 ;;; Multiple values.
1796 (defmacro cl-multiple-value-bind
(vars form
&rest body
)
1797 "Collect multiple return values.
1798 FORM must return a list; the BODY is then executed with the first N elements
1799 of this list bound (`let'-style) to each of the symbols SYM in turn. This
1800 is analogous to the Common Lisp `cl-multiple-value-bind' macro, using lists to
1801 simulate true multiple return values. For compatibility, (cl-values A B C) is
1802 a synonym for (list A B C).
1804 \(fn (SYM...) FORM BODY)"
1805 (declare (indent 2) (debug ((&rest symbolp
) form body
)))
1806 (let ((temp (make-symbol "--cl-var--")) (n -
1))
1807 `(let* ((,temp
,form
)
1808 ,@(mapcar (lambda (v)
1809 (list v
`(nth ,(setq n
(1+ n
)) ,temp
)))
1814 (defmacro cl-multiple-value-setq
(vars form
)
1815 "Collect multiple return values.
1816 FORM must return a list; the first N elements of this list are stored in
1817 each of the symbols SYM in turn. This is analogous to the Common Lisp
1818 `cl-multiple-value-setq' macro, using lists to simulate true multiple return
1819 values. For compatibility, (cl-values A B C) is a synonym for (list A B C).
1821 \(fn (SYM...) FORM)"
1822 (declare (indent 1) (debug ((&rest symbolp
) form
)))
1823 (cond ((null vars
) `(progn ,form nil
))
1824 ((null (cdr vars
)) `(setq ,(car vars
) (car ,form
)))
1826 (let* ((temp (make-symbol "--cl-var--")) (n 0))
1827 `(let ((,temp
,form
))
1828 (prog1 (setq ,(pop vars
) (car ,temp
))
1829 (setq ,@(apply #'nconc
1831 (list v
`(nth ,(setq n
(1+ n
))
1839 (defmacro cl-locally
(&rest body
)
1843 (defmacro cl-the
(type form
)
1844 (declare (indent 1) (debug (cl-type-spec form
)))
1847 (defvar cl-proclaim-history t
) ; for future compilers
1848 (defvar cl-declare-stack t
) ; for future compilers
1850 (defun cl-do-proclaim (spec hist
)
1851 (and hist
(listp cl-proclaim-history
) (push spec cl-proclaim-history
))
1852 (cond ((eq (car-safe spec
) 'special
)
1853 (if (boundp 'byte-compile-bound-variables
)
1854 (setq byte-compile-bound-variables
1855 (append (cdr spec
) byte-compile-bound-variables
))))
1857 ((eq (car-safe spec
) 'inline
)
1858 (while (setq spec
(cdr spec
))
1859 (or (memq (get (car spec
) 'byte-optimizer
)
1860 '(nil byte-compile-inline-expand
))
1861 (error "%s already has a byte-optimizer, can't make it inline"
1863 (put (car spec
) 'byte-optimizer
'byte-compile-inline-expand
)))
1865 ((eq (car-safe spec
) 'notinline
)
1866 (while (setq spec
(cdr spec
))
1867 (if (eq (get (car spec
) 'byte-optimizer
)
1868 'byte-compile-inline-expand
)
1869 (put (car spec
) 'byte-optimizer nil
))))
1871 ((eq (car-safe spec
) 'optimize
)
1872 (let ((speed (assq (nth 1 (assq 'speed
(cdr spec
)))
1873 '((0 nil
) (1 t
) (2 t
) (3 t
))))
1874 (safety (assq (nth 1 (assq 'safety
(cdr spec
)))
1875 '((0 t
) (1 t
) (2 t
) (3 nil
)))))
1876 (if speed
(setq cl-optimize-speed
(car speed
)
1877 byte-optimize
(nth 1 speed
)))
1878 (if safety
(setq cl-optimize-safety
(car safety
)
1879 byte-compile-delete-errors
(nth 1 safety
)))))
1881 ((and (eq (car-safe spec
) 'warn
) (boundp 'byte-compile-warnings
))
1882 (while (setq spec
(cdr spec
))
1883 (if (consp (car spec
))
1884 (if (eq (cl-cadar spec
) 0)
1885 (byte-compile-disable-warning (caar spec
))
1886 (byte-compile-enable-warning (caar spec
)))))))
1889 ;;; Process any proclamations made before cl-macs was loaded.
1890 (defvar cl-proclaims-deferred
)
1891 (let ((p (reverse cl-proclaims-deferred
)))
1892 (while p
(cl-do-proclaim (pop p
) t
))
1893 (setq cl-proclaims-deferred nil
))
1896 (defmacro cl-declare
(&rest specs
)
1897 "Declare SPECS about the current function while compiling.
1900 \(cl-declare (warn 0))
1902 will turn off byte-compile warnings in the function.
1903 See Info node `(cl)Declarations' for details."
1904 (if (cl-compiling-file)
1906 (if (listp cl-declare-stack
) (push (car specs
) cl-declare-stack
))
1907 (cl-do-proclaim (pop specs
) nil
)))
1912 ;;; Generalized variables.
1915 (defmacro cl-define-setf-method
(func args
&rest body
)
1916 "Define a `cl-setf' method.
1917 This method shows how to handle `cl-setf's to places of the form (NAME ARGS...).
1918 The argument forms ARGS are bound according to ARGLIST, as if NAME were
1919 going to be expanded as a macro, then the BODY forms are executed and must
1920 return a list of five elements: a temporary-variables list, a value-forms
1921 list, a store-variables list (of length one), a store-form, and an access-
1922 form. See `cl-defsetf' for a simpler way to define most setf-methods.
1924 \(fn NAME ARGLIST BODY...)"
1926 (&define name cl-lambda-list cl-declarations-or-string def-body
)))
1927 `(cl-eval-when (compile load eval
)
1928 ,@(if (stringp (car body
))
1929 (list `(put ',func
'setf-documentation
,(pop body
))))
1930 ,(cl-transform-function-property
1931 func
'setf-method
(cons args body
))))
1932 (defalias 'cl-define-setf-expander
'cl-define-setf-method
)
1935 (defmacro cl-defsetf
(func arg1
&rest args
)
1936 "Define a `cl-setf' method.
1937 This macro is an easy-to-use substitute for `cl-define-setf-method' that works
1938 well for simple place forms. In the simple `cl-defsetf' form, `cl-setf's of
1939 the form (cl-setf (NAME ARGS...) VAL) are transformed to function or macro
1940 calls of the form (FUNC ARGS... VAL). Example:
1942 (cl-defsetf aref aset)
1944 Alternate form: (cl-defsetf NAME ARGLIST (STORE) BODY...).
1945 Here, the above `cl-setf' call is expanded by binding the argument forms ARGS
1946 according to ARGLIST, binding the value form VAL to STORE, then executing
1947 BODY, which must return a Lisp form that does the necessary `cl-setf' operation.
1948 Actually, ARGLIST and STORE may be bound to temporary variables which are
1949 introduced automatically to preserve proper execution order of the arguments.
1952 (cl-defsetf nth (n x) (v) `(setcar (nthcdr ,n ,x) ,v))
1954 \(fn NAME [FUNC | ARGLIST (STORE) BODY...])"
1957 [&or
[symbolp
&optional stringp
]
1958 [cl-lambda-list
(symbolp)]]
1959 cl-declarations-or-string def-body
)))
1960 (if (and (listp arg1
) (consp args
))
1961 (let* ((largs nil
) (largsr nil
)
1962 (temps nil
) (tempsr nil
)
1963 (restarg nil
) (rest-temps nil
)
1964 (store-var (car (prog1 (car args
) (setq args
(cdr args
)))))
1965 (store-temp (intern (format "--%s--temp--" store-var
)))
1966 (lets1 nil
) (lets2 nil
)
1967 (docstr nil
) (p arg1
))
1968 (if (stringp (car args
))
1969 (setq docstr
(prog1 (car args
) (setq args
(cdr args
)))))
1970 (while (and p
(not (eq (car p
) '&aux
)))
1971 (if (eq (car p
) '&rest
)
1972 (setq p
(cdr p
) restarg
(car p
))
1973 (or (memq (car p
) '(&optional
&key
&allow-other-keys
))
1974 (setq largs
(cons (if (consp (car p
)) (car (car p
)) (car p
))
1976 temps
(cons (intern (format "--%s--temp--" (car largs
)))
1979 (setq largs
(nreverse largs
) temps
(nreverse temps
))
1981 (setq largsr
(append largs
(list restarg
))
1982 rest-temps
(intern (format "--%s--temp--" restarg
))
1983 tempsr
(append temps
(list rest-temps
)))
1984 (setq largsr largs tempsr temps
))
1985 (let ((p1 largs
) (p2 temps
))
1987 (setq lets1
(cons `(,(car p2
)
1988 (make-symbol ,(format "--cl-%s--" (car p1
))))
1990 lets2
(cons (list (car p1
) (car p2
)) lets2
)
1991 p1
(cdr p1
) p2
(cdr p2
))))
1992 (if restarg
(setq lets2
(cons (list restarg rest-temps
) lets2
)))
1993 `(cl-define-setf-method ,func
,arg1
1994 ,@(and docstr
(list docstr
))
1998 (make-symbol ,(format "--cl-%s--" store-var
)))
2001 (mapcar (lambda (_) (make-symbol "--cl-var--"))
2006 (,(if restarg
'cl-list
* 'list
) ,@tempsr
)
2007 (,(if restarg
'cl-list
* 'list
) ,@largsr
)
2011 (cons (list store-var store-temp
)
2014 (,(if restarg
'cl-list
* 'list
)
2015 ,@(cons `',func tempsr
))))))
2016 `(cl-defsetf ,func
(&rest args
) (store)
2017 ,(let ((call `(cons ',arg1
2018 (append args
(list store
)))))
2020 `(list 'progn
,call store
)
2023 ;;; Some standard place types from Common Lisp.
2024 (cl-defsetf aref aset
)
2025 (cl-defsetf car setcar
)
2026 (cl-defsetf cdr setcdr
)
2027 (cl-defsetf caar
(x) (val) `(setcar (car ,x
) ,val
))
2028 (cl-defsetf cadr
(x) (val) `(setcar (cdr ,x
) ,val
))
2029 (cl-defsetf cdar
(x) (val) `(setcdr (car ,x
) ,val
))
2030 (cl-defsetf cddr
(x) (val) `(setcdr (cdr ,x
) ,val
))
2031 (cl-defsetf elt
(seq n
) (store)
2032 `(if (listp ,seq
) (setcar (nthcdr ,n
,seq
) ,store
)
2033 (aset ,seq
,n
,store
)))
2034 (cl-defsetf get put
)
2035 (cl-defsetf cl-get
(x y
&optional d
) (store) `(put ,x
,y
,store
))
2036 (cl-defsetf gethash
(x h
&optional d
) (store) `(puthash ,x
,store
,h
))
2037 (cl-defsetf nth
(n x
) (store) `(setcar (nthcdr ,n
,x
) ,store
))
2038 (cl-defsetf cl-subseq
(seq start
&optional end
) (new)
2039 `(progn (cl-replace ,seq
,new
:start1
,start
:end1
,end
) ,new
))
2040 (cl-defsetf symbol-function fset
)
2041 (cl-defsetf symbol-plist setplist
)
2042 (cl-defsetf symbol-value set
)
2044 ;;; Various car/cdr aliases. Note that `cadr' is handled specially.
2045 (cl-defsetf cl-first setcar
)
2046 (cl-defsetf cl-second
(x) (store) `(setcar (cdr ,x
) ,store
))
2047 (cl-defsetf cl-third
(x) (store) `(setcar (cddr ,x
) ,store
))
2048 (cl-defsetf cl-fourth
(x) (store) `(setcar (cl-cdddr ,x
) ,store
))
2049 (cl-defsetf cl-fifth
(x) (store) `(setcar (nthcdr 4 ,x
) ,store
))
2050 (cl-defsetf cl-sixth
(x) (store) `(setcar (nthcdr 5 ,x
) ,store
))
2051 (cl-defsetf cl-seventh
(x) (store) `(setcar (nthcdr 6 ,x
) ,store
))
2052 (cl-defsetf cl-eighth
(x) (store) `(setcar (nthcdr 7 ,x
) ,store
))
2053 (cl-defsetf cl-ninth
(x) (store) `(setcar (nthcdr 8 ,x
) ,store
))
2054 (cl-defsetf cl-tenth
(x) (store) `(setcar (nthcdr 9 ,x
) ,store
))
2055 (cl-defsetf cl-rest setcdr
)
2057 ;;; Some more Emacs-related place types.
2058 (cl-defsetf buffer-file-name set-visited-file-name t
)
2059 (cl-defsetf buffer-modified-p
(&optional buf
) (flag)
2060 `(with-current-buffer ,buf
2061 (set-buffer-modified-p ,flag
)))
2062 (cl-defsetf buffer-name rename-buffer t
)
2063 (cl-defsetf buffer-string
() (store)
2064 `(progn (erase-buffer) (insert ,store
)))
2065 (cl-defsetf buffer-substring cl-set-buffer-substring
)
2066 (cl-defsetf current-buffer set-buffer
)
2067 (cl-defsetf current-case-table set-case-table
)
2068 (cl-defsetf current-column move-to-column t
)
2069 (cl-defsetf current-global-map use-global-map t
)
2070 (cl-defsetf current-input-mode
() (store)
2071 `(progn (apply #'set-input-mode
,store
) ,store
))
2072 (cl-defsetf current-local-map use-local-map t
)
2073 (cl-defsetf current-window-configuration set-window-configuration t
)
2074 (cl-defsetf default-file-modes set-default-file-modes t
)
2075 (cl-defsetf default-value set-default
)
2076 (cl-defsetf documentation-property put
)
2077 (cl-defsetf face-background
(f &optional s
) (x) `(set-face-background ,f
,x
,s
))
2078 (cl-defsetf face-background-pixmap
(f &optional s
) (x)
2079 `(set-face-background-pixmap ,f
,x
,s
))
2080 (cl-defsetf face-font
(f &optional s
) (x) `(set-face-font ,f
,x
,s
))
2081 (cl-defsetf face-foreground
(f &optional s
) (x) `(set-face-foreground ,f
,x
,s
))
2082 (cl-defsetf face-underline-p
(f &optional s
) (x)
2083 `(set-face-underline-p ,f
,x
,s
))
2084 (cl-defsetf file-modes set-file-modes t
)
2085 (cl-defsetf frame-height set-screen-height t
)
2086 (cl-defsetf frame-parameters modify-frame-parameters t
)
2087 (cl-defsetf frame-visible-p cl-set-frame-visible-p
)
2088 (cl-defsetf frame-width set-screen-width t
)
2089 (cl-defsetf frame-parameter set-frame-parameter t
)
2090 (cl-defsetf terminal-parameter set-terminal-parameter
)
2091 (cl-defsetf getenv setenv t
)
2092 (cl-defsetf get-register set-register
)
2093 (cl-defsetf global-key-binding global-set-key
)
2094 (cl-defsetf keymap-parent set-keymap-parent
)
2095 (cl-defsetf local-key-binding local-set-key
)
2096 (cl-defsetf mark set-mark t
)
2097 (cl-defsetf mark-marker set-mark t
)
2098 (cl-defsetf marker-position set-marker t
)
2099 (cl-defsetf match-data set-match-data t
)
2100 (cl-defsetf mouse-position
(scr) (store)
2101 `(set-mouse-position ,scr
(car ,store
) (cadr ,store
)
2103 (cl-defsetf overlay-get overlay-put
)
2104 (cl-defsetf overlay-start
(ov) (store)
2105 `(progn (move-overlay ,ov
,store
(overlay-end ,ov
)) ,store
))
2106 (cl-defsetf overlay-end
(ov) (store)
2107 `(progn (move-overlay ,ov
(overlay-start ,ov
) ,store
) ,store
))
2108 (cl-defsetf point goto-char
)
2109 (cl-defsetf point-marker goto-char t
)
2110 (cl-defsetf point-max
() (store)
2111 `(progn (narrow-to-region (point-min) ,store
) ,store
))
2112 (cl-defsetf point-min
() (store)
2113 `(progn (narrow-to-region ,store
(point-max)) ,store
))
2114 (cl-defsetf process-buffer set-process-buffer
)
2115 (cl-defsetf process-filter set-process-filter
)
2116 (cl-defsetf process-sentinel set-process-sentinel
)
2117 (cl-defsetf process-get process-put
)
2118 (cl-defsetf read-mouse-position
(scr) (store)
2119 `(set-mouse-position ,scr
(car ,store
) (cdr ,store
)))
2120 (cl-defsetf screen-height set-screen-height t
)
2121 (cl-defsetf screen-width set-screen-width t
)
2122 (cl-defsetf selected-window select-window
)
2123 (cl-defsetf selected-screen select-screen
)
2124 (cl-defsetf selected-frame select-frame
)
2125 (cl-defsetf standard-case-table set-standard-case-table
)
2126 (cl-defsetf syntax-table set-syntax-table
)
2127 (cl-defsetf visited-file-modtime set-visited-file-modtime t
)
2128 (cl-defsetf window-buffer set-window-buffer t
)
2129 (cl-defsetf window-display-table set-window-display-table t
)
2130 (cl-defsetf window-dedicated-p set-window-dedicated-p t
)
2131 (cl-defsetf window-height
() (store)
2132 `(progn (enlarge-window (- ,store
(window-height))) ,store
))
2133 (cl-defsetf window-hscroll set-window-hscroll
)
2134 (cl-defsetf window-parameter set-window-parameter
)
2135 (cl-defsetf window-point set-window-point
)
2136 (cl-defsetf window-start set-window-start
)
2137 (cl-defsetf window-width
() (store)
2138 `(progn (enlarge-window (- ,store
(window-width)) t
) ,store
))
2139 (cl-defsetf x-get-secondary-selection x-own-secondary-selection t
)
2140 (cl-defsetf x-get-selection x-own-selection t
)
2142 ;; This is a hack that allows (cl-setf (eq a 7) B) to mean either
2143 ;; (setq a 7) or (setq a nil) depending on whether B is nil or not.
2144 ;; This is useful when you have control over the PLACE but not over
2145 ;; the VALUE, as is the case in define-minor-mode's :variable.
2146 (cl-define-setf-method eq
(place val
)
2147 (let ((method (cl-get-setf-method place cl-macro-environment
))
2148 (val-temp (make-symbol "--eq-val--"))
2149 (store-temp (make-symbol "--eq-store--")))
2150 (list (append (nth 0 method
) (list val-temp
))
2151 (append (nth 1 method
) (list val
))
2153 `(let ((,(car (nth 2 method
))
2154 (if ,store-temp
,val-temp
(not ,val-temp
))))
2155 ,(nth 3 method
) ,store-temp
)
2156 `(eq ,(nth 4 method
) ,val-temp
))))
2158 ;;; More complex setf-methods.
2159 ;; These should take &environment arguments, but since full arglists aren't
2160 ;; available while compiling cl-macs, we fake it by referring to the global
2161 ;; variable cl-macro-environment directly.
2163 (cl-define-setf-method apply
(func arg1
&rest rest
)
2164 (or (and (memq (car-safe func
) '(quote function cl-function
))
2165 (symbolp (car-safe (cdr-safe func
))))
2166 (error "First arg to apply in cl-setf is not (function SYM): %s" func
))
2167 (let* ((form (cons (nth 1 func
) (cons arg1 rest
)))
2168 (method (cl-get-setf-method form cl-macro-environment
)))
2169 (list (car method
) (nth 1 method
) (nth 2 method
)
2170 (cl-setf-make-apply (nth 3 method
) (cadr func
) (car method
))
2171 (cl-setf-make-apply (nth 4 method
) (cadr func
) (car method
)))))
2173 (defun cl-setf-make-apply (form func temps
)
2174 (if (eq (car form
) 'progn
)
2175 `(progn ,(cl-setf-make-apply (cadr form
) func temps
) ,@(cddr form
))
2176 (or (equal (last form
) (last temps
))
2177 (error "%s is not suitable for use with setf-of-apply" func
))
2178 `(apply ',(car form
) ,@(cdr form
))))
2180 (cl-define-setf-method nthcdr
(n place
)
2181 (let ((method (cl-get-setf-method place cl-macro-environment
))
2182 (n-temp (make-symbol "--cl-nthcdr-n--"))
2183 (store-temp (make-symbol "--cl-nthcdr-store--")))
2184 (list (cons n-temp
(car method
))
2185 (cons n
(nth 1 method
))
2187 `(let ((,(car (nth 2 method
))
2188 (cl-set-nthcdr ,n-temp
,(nth 4 method
)
2190 ,(nth 3 method
) ,store-temp
)
2191 `(nthcdr ,n-temp
,(nth 4 method
)))))
2193 (cl-define-setf-method cl-getf
(place tag
&optional def
)
2194 (let ((method (cl-get-setf-method place cl-macro-environment
))
2195 (tag-temp (make-symbol "--cl-getf-tag--"))
2196 (def-temp (make-symbol "--cl-getf-def--"))
2197 (store-temp (make-symbol "--cl-getf-store--")))
2198 (list (append (car method
) (list tag-temp def-temp
))
2199 (append (nth 1 method
) (list tag def
))
2201 `(let ((,(car (nth 2 method
))
2202 (cl-set-getf ,(nth 4 method
) ,tag-temp
,store-temp
)))
2203 ,(nth 3 method
) ,store-temp
)
2204 `(cl-getf ,(nth 4 method
) ,tag-temp
,def-temp
))))
2206 (cl-define-setf-method substring
(place from
&optional to
)
2207 (let ((method (cl-get-setf-method place cl-macro-environment
))
2208 (from-temp (make-symbol "--cl-substring-from--"))
2209 (to-temp (make-symbol "--cl-substring-to--"))
2210 (store-temp (make-symbol "--cl-substring-store--")))
2211 (list (append (car method
) (list from-temp to-temp
))
2212 (append (nth 1 method
) (list from to
))
2214 `(let ((,(car (nth 2 method
))
2215 (cl-set-substring ,(nth 4 method
)
2216 ,from-temp
,to-temp
,store-temp
)))
2217 ,(nth 3 method
) ,store-temp
)
2218 `(substring ,(nth 4 method
) ,from-temp
,to-temp
))))
2220 ;;; Getting and optimizing setf-methods.
2222 (defun cl-get-setf-method (place &optional env
)
2223 "Return a list of five values describing the setf-method for PLACE.
2224 PLACE may be any Lisp form which can appear as the PLACE argument to
2225 a macro like `cl-setf' or `cl-incf'."
2227 (let ((temp (make-symbol "--cl-setf--")))
2228 (list nil nil
(list temp
) `(setq ,place
,temp
) place
))
2229 (or (and (symbolp (car place
))
2230 (let* ((func (car place
))
2231 (name (symbol-name func
))
2232 (method (get func
'setf-method
))
2233 (case-fold-search nil
))
2235 (let ((cl-macro-environment env
))
2236 (setq method
(apply method
(cdr place
))))
2237 (if (and (consp method
) (= (length method
) 5))
2239 (error "Setf-method for %s returns malformed method"
2241 (and (string-match-p "\\`c[ad][ad][ad]?[ad]?r\\'" name
)
2242 (cl-get-setf-method (cl-compiler-macroexpand place
)))
2243 (and (eq func
'edebug-after
)
2244 (cl-get-setf-method (nth (1- (length place
)) place
)
2246 (if (eq place
(setq place
(macroexpand place env
)))
2247 (if (and (symbolp (car place
)) (fboundp (car place
))
2248 (symbolp (symbol-function (car place
))))
2249 (cl-get-setf-method (cons (symbol-function (car place
))
2251 (error "No setf-method known for %s" (car place
)))
2252 (cl-get-setf-method place env
)))))
2254 (defun cl-setf-do-modify (place opt-expr
)
2255 (let* ((method (cl-get-setf-method place cl-macro-environment
))
2256 (temps (car method
)) (values (nth 1 method
))
2257 (lets nil
) (subs nil
)
2258 (optimize (and (not (eq opt-expr
'no-opt
))
2259 (or (and (not (eq opt-expr
'unsafe
))
2260 (cl-safe-expr-p opt-expr
))
2261 (cl-setf-simple-store-p (car (nth 2 method
))
2263 (simple (and optimize
(consp place
) (cl-simple-exprs-p (cdr place
)))))
2265 (if (or simple
(cl-const-expr-p (car values
)))
2266 (push (cons (pop temps
) (pop values
)) subs
)
2267 (push (list (pop temps
) (pop values
)) lets
)))
2268 (list (nreverse lets
)
2269 (cons (car (nth 2 method
)) (cl-sublis subs
(nth 3 method
)))
2270 (cl-sublis subs
(nth 4 method
)))))
2272 (defun cl-setf-do-store (spec val
)
2273 (let ((sym (car spec
))
2275 (if (or (cl-const-expr-p val
)
2276 (and (cl-simple-expr-p val
) (eq (cl-expr-contains form sym
) 1))
2277 (cl-setf-simple-store-p sym form
))
2278 (cl-subst val sym form
)
2279 `(let ((,sym
,val
)) ,form
))))
2281 (defun cl-setf-simple-store-p (sym form
)
2282 (and (consp form
) (eq (cl-expr-contains form sym
) 1)
2283 (eq (nth (1- (length form
)) form
) sym
)
2284 (symbolp (car form
)) (fboundp (car form
))
2285 (not (eq (car-safe (symbol-function (car form
))) 'macro
))))
2287 ;;; The standard modify macros.
2289 (defmacro cl-setf
(&rest args
)
2290 "Set each PLACE to the value of its VAL.
2291 This is a generalized version of `setq'; the PLACEs may be symbolic
2292 references such as (car x) or (aref x i), as well as plain symbols.
2293 For example, (cl-setf (cl-cadar x) y) is equivalent to (setcar (cdar x) y).
2294 The return value is the last VAL in the list.
2296 \(fn PLACE VAL PLACE VAL ...)"
2297 (declare (debug (&rest
[place form
])))
2298 (if (cdr (cdr args
))
2300 (while args
(push `(cl-setf ,(pop args
) ,(pop args
)) sets
))
2301 (cons 'progn
(nreverse sets
)))
2302 (if (symbolp (car args
))
2303 (and args
(cons 'setq args
))
2304 (let* ((method (cl-setf-do-modify (car args
) (nth 1 args
)))
2305 (store (cl-setf-do-store (nth 1 method
) (nth 1 args
))))
2306 (if (car method
) `(let* ,(car method
) ,store
) store
)))))
2309 (defmacro cl-psetf
(&rest args
)
2310 "Set PLACEs to the values VALs in parallel.
2311 This is like `cl-setf', except that all VAL forms are evaluated (in order)
2312 before assigning any PLACEs to the corresponding values.
2314 \(fn PLACE VAL PLACE VAL ...)"
2315 (declare (debug cl-setf
))
2316 (let ((p args
) (simple t
) (vars nil
))
2318 (if (or (not (symbolp (car p
))) (cl-expr-depends-p (nth 1 p
) vars
))
2320 (if (memq (car p
) vars
)
2321 (error "Destination duplicated in psetf: %s" (car p
)))
2323 (or p
(error "Odd number of arguments to cl-psetf"))
2326 `(progn (cl-setf ,@args
) nil
)
2327 (setq args
(reverse args
))
2328 (let ((expr `(cl-setf ,(cadr args
) ,(car args
))))
2329 (while (setq args
(cddr args
))
2330 (setq expr
`(cl-setf ,(cadr args
) (prog1 ,(car args
) ,expr
))))
2331 `(progn ,expr nil
)))))
2334 (defun cl-do-pop (place)
2335 (if (cl-simple-expr-p place
)
2336 `(prog1 (car ,place
) (cl-setf ,place
(cdr ,place
)))
2337 (let* ((method (cl-setf-do-modify place t
))
2338 (temp (make-symbol "--cl-pop--")))
2339 `(let* (,@(car method
)
2340 (,temp
,(nth 2 method
)))
2342 ,(cl-setf-do-store (nth 1 method
) `(cdr ,temp
)))))))
2345 (defmacro cl-remf
(place tag
)
2346 "Remove TAG from property list PLACE.
2347 PLACE may be a symbol, or any generalized variable allowed by `cl-setf'.
2348 The form returns true if TAG was found and removed, nil otherwise."
2349 (declare (debug (place form
)))
2350 (let* ((method (cl-setf-do-modify place t
))
2351 (tag-temp (and (not (cl-const-expr-p tag
)) (make-symbol "--cl-remf-tag--")))
2352 (val-temp (and (not (cl-simple-expr-p place
))
2353 (make-symbol "--cl-remf-place--")))
2354 (ttag (or tag-temp tag
))
2355 (tval (or val-temp
(nth 2 method
))))
2356 `(let* (,@(car method
)
2357 ,@(and val-temp
`((,val-temp
,(nth 2 method
))))
2358 ,@(and tag-temp
`((,tag-temp
,tag
))))
2359 (if (eq ,ttag
(car ,tval
))
2360 (progn ,(cl-setf-do-store (nth 1 method
) `(cddr ,tval
))
2362 `(cl-do-remf ,tval
,ttag
)))))
2365 (defmacro cl-shiftf
(place &rest args
)
2366 "Shift left among PLACEs.
2367 Example: (cl-shiftf A B C) sets A to B, B to C, and returns the old A.
2368 Each PLACE may be a symbol, or any generalized variable allowed by `cl-setf'.
2371 (declare (debug (&rest place
)))
2374 ((symbolp place
) `(prog1 ,place
(setq ,place
(cl-shiftf ,@args
))))
2376 (let ((method (cl-setf-do-modify place
'unsafe
)))
2377 `(let* ,(car method
)
2378 (prog1 ,(nth 2 method
)
2379 ,(cl-setf-do-store (nth 1 method
) `(cl-shiftf ,@args
))))))))
2382 (defmacro cl-rotatef
(&rest args
)
2383 "Rotate left among PLACEs.
2384 Example: (cl-rotatef A B C) sets A to B, B to C, and C to A. It returns nil.
2385 Each PLACE may be a symbol, or any generalized variable allowed by `cl-setf'.
2388 (declare (debug (&rest place
)))
2389 (if (not (memq nil
(mapcar 'symbolp args
)))
2394 (setq sets
(nconc sets
(list (pop args
) (car args
)))))
2395 `(cl-psetf ,@sets
,(car args
) ,first
)))
2396 (let* ((places (reverse args
))
2397 (temp (make-symbol "--cl-rotatef--"))
2400 (let ((method (cl-setf-do-modify (pop places
) 'unsafe
)))
2401 (setq form
`(let* ,(car method
)
2402 (prog1 ,(nth 2 method
)
2403 ,(cl-setf-do-store (nth 1 method
) form
))))))
2404 (let ((method (cl-setf-do-modify (car places
) 'unsafe
)))
2405 `(let* (,@(car method
) (,temp
,(nth 2 method
)))
2406 ,(cl-setf-do-store (nth 1 method
) form
) nil
)))))
2409 (defmacro cl-letf
(bindings &rest body
)
2410 "Temporarily bind to PLACEs.
2411 This is the analogue of `let', but with generalized variables (in the
2412 sense of `cl-setf') for the PLACEs. Each PLACE is set to the corresponding
2413 VALUE, then the BODY forms are executed. On exit, either normally or
2414 because of a `throw' or error, the PLACEs are set back to their original
2415 values. Note that this macro is *not* available in Common Lisp.
2416 As a special case, if `(PLACE)' is used instead of `(PLACE VALUE)',
2417 the PLACE is not modified before executing BODY.
2419 \(fn ((PLACE VALUE) ...) BODY...)"
2420 (declare (indent 1) (debug ((&rest
(gate place
&optional form
)) body
)))
2421 (if (and (not (cdr bindings
)) (cdar bindings
) (symbolp (caar bindings
)))
2422 `(let ,bindings
,@body
)
2423 (let ((lets nil
) (sets nil
)
2424 (unsets nil
) (rev (reverse bindings
)))
2426 (let* ((place (if (symbolp (caar rev
))
2427 `(symbol-value ',(caar rev
))
2429 (value (cl-cadar rev
))
2430 (method (cl-setf-do-modify place
'no-opt
))
2431 (save (make-symbol "--cl-letf-save--"))
2432 (bound (and (memq (car place
) '(symbol-value symbol-function
))
2433 (make-symbol "--cl-letf-bound--")))
2434 (temp (and (not (cl-const-expr-p value
)) (cdr bindings
)
2435 (make-symbol "--cl-letf-val--"))))
2436 (setq lets
(nconc (car method
)
2439 (list (if (eq (car place
)
2442 (nth 1 (nth 2 method
))))
2443 (list save
`(and ,bound
2445 (list (list save
(nth 2 method
))))
2446 (and temp
(list (list temp value
)))
2451 ,@(if (cdr (car rev
))
2452 (cons (cl-setf-do-store (nth 1 method
)
2458 ,(cl-setf-do-store (nth 1 method
) save
)
2459 (,(if (eq (car place
) 'symbol-value
)
2460 #'makunbound
#'fmakunbound
)
2461 ,(nth 1 (nth 2 method
))))
2462 (cl-setf-do-store (nth 1 method
) save
))))
2464 `(let* ,lets
,@body
))))
2468 (defmacro cl-letf
* (bindings &rest body
)
2469 "Temporarily bind to PLACEs.
2470 This is the analogue of `let*', but with generalized variables (in the
2471 sense of `cl-setf') for the PLACEs. Each PLACE is set to the corresponding
2472 VALUE, then the BODY forms are executed. On exit, either normally or
2473 because of a `throw' or error, the PLACEs are set back to their original
2474 values. Note that this macro is *not* available in Common Lisp.
2475 As a special case, if `(PLACE)' is used instead of `(PLACE VALUE)',
2476 the PLACE is not modified before executing BODY.
2478 \(fn ((PLACE VALUE) ...) BODY...)"
2479 (declare (indent 1) (debug cl-letf
))
2482 (setq bindings
(reverse bindings
))
2484 (setq body
(list `(cl-letf (,(pop bindings
)) ,@body
))))
2488 (defmacro cl-callf
(func place
&rest args
)
2489 "Set PLACE to (FUNC PLACE ARGS...).
2490 FUNC should be an unquoted function name. PLACE may be a symbol,
2491 or any generalized variable allowed by `cl-setf'.
2493 \(fn FUNC PLACE ARGS...)"
2494 (declare (indent 2) (debug (cl-function place
&rest form
)))
2495 (let* ((method (cl-setf-do-modify place
(cons 'list args
)))
2496 (rargs (cons (nth 2 method
) args
)))
2497 `(let* ,(car method
)
2498 ,(cl-setf-do-store (nth 1 method
)
2499 (if (symbolp func
) (cons func rargs
)
2500 `(funcall #',func
,@rargs
))))))
2503 (defmacro cl-callf2
(func arg1 place
&rest args
)
2504 "Set PLACE to (FUNC ARG1 PLACE ARGS...).
2505 Like `cl-callf', but PLACE is the second argument of FUNC, not the first.
2507 \(fn FUNC ARG1 PLACE ARGS...)"
2508 (declare (indent 3) (debug (cl-function form place
&rest form
)))
2509 (if (and (cl-safe-expr-p arg1
) (cl-simple-expr-p place
) (symbolp func
))
2510 `(cl-setf ,place
(,func
,arg1
,place
,@args
))
2511 (let* ((method (cl-setf-do-modify place
(cons 'list args
)))
2512 (temp (and (not (cl-const-expr-p arg1
)) (make-symbol "--cl-arg1--")))
2513 (rargs (cl-list* (or temp arg1
) (nth 2 method
) args
)))
2514 `(let* (,@(and temp
(list (list temp arg1
))) ,@(car method
))
2515 ,(cl-setf-do-store (nth 1 method
)
2516 (if (symbolp func
) (cons func rargs
)
2517 `(funcall #',func
,@rargs
)))))))
2520 (defmacro cl-define-modify-macro
(name arglist func
&optional doc
)
2521 "Define a `cl-setf'-like modify macro.
2522 If NAME is called, it combines its PLACE argument with the other arguments
2523 from ARGLIST using FUNC: (cl-define-modify-macro cl-incf (&optional (n 1)) +)"
2525 (&define name cl-lambda-list
;; should exclude &key
2526 symbolp
&optional stringp
)))
2527 (if (memq '&key arglist
) (error "&key not allowed in cl-define-modify-macro"))
2528 (let ((place (make-symbol "--cl-place--")))
2529 `(cl-defmacro ,name
(,place
,@arglist
)
2531 (,(if (memq '&rest arglist
) #'cl-list
* #'list
)
2532 #'cl-callf
',func
,place
2533 ,@(cl-arglist-args arglist
)))))
2539 (defmacro cl-defstruct
(struct &rest descs
)
2540 "Define a struct type.
2541 This macro defines a new data type called NAME that stores data
2542 in SLOTs. It defines a `make-NAME' constructor, a `copy-NAME'
2543 copier, a `NAME-p' predicate, and slot accessors named `NAME-SLOT'.
2544 You can use the accessors to set the corresponding slots, via `cl-setf'.
2546 NAME may instead take the form (NAME OPTIONS...), where each
2547 OPTION is either a single keyword or (KEYWORD VALUE).
2548 See Info node `(cl)Structures' for a list of valid keywords.
2550 Each SLOT may instead take the form (SLOT SLOT-OPTS...), where
2551 SLOT-OPTS are keyword-value pairs for that slot. Currently, only
2552 one keyword is supported, `:read-only'. If this has a non-nil
2553 value, that slot cannot be set via `cl-setf'.
2555 \(fn NAME SLOTS...)"
2556 (declare (doc-string 2)
2558 (&define
;Makes top-level form not be wrapped.
2562 (&or
[":conc-name" symbolp
]
2563 [":constructor" symbolp
&optional cl-lambda-list
]
2565 [":predicate" symbolp
]
2566 [":include" symbolp
&rest sexp
] ;; Not finished.
2567 ;; The following are not supported.
2568 ;; [":print-function" ...]
2570 ;; [":initial-offset" ...]
2573 ;; All the above is for the following def-form.
2574 &rest
&or symbolp
(symbolp def-form
2575 &optional
":read-only" sexp
))))
2576 (let* ((name (if (consp struct
) (car struct
) struct
))
2577 (opts (cdr-safe struct
))
2580 (conc-name (concat (symbol-name name
) "-"))
2581 (constructor (intern (format "make-%s" name
)))
2583 (copier (intern (format "copy-%s" name
)))
2584 (predicate (intern (format "%s-p" name
)))
2585 (print-func nil
) (print-auto nil
)
2586 (safety (if (cl-compiling-file) cl-optimize-safety
3))
2588 (tag (intern (format "cl-struct-%s" name
)))
2589 (tag-symbol (intern (format "cl-struct-%s-tags" name
)))
2595 pred-form pred-check
)
2596 (if (stringp (car descs
))
2597 (push `(put ',name
'structure-documentation
2598 ,(pop descs
)) forms
))
2599 (setq descs
(cons '(cl-tag-slot)
2600 (mapcar (function (lambda (x) (if (consp x
) x
(list x
))))
2603 (let ((opt (if (consp (car opts
)) (caar opts
) (car opts
)))
2604 (args (cdr-safe (pop opts
))))
2605 (cond ((eq opt
:conc-name
)
2607 (setq conc-name
(if (car args
)
2608 (symbol-name (car args
)) ""))))
2609 ((eq opt
:constructor
)
2612 ;; If this defines a constructor of the same name as
2613 ;; the default one, don't define the default.
2614 (if (eq (car args
) constructor
)
2615 (setq constructor nil
))
2616 (push args constrs
))
2617 (if args
(setq constructor
(car args
)))))
2619 (if args
(setq copier
(car args
))))
2620 ((eq opt
:predicate
)
2621 (if args
(setq predicate
(car args
))))
2623 (setq include
(car args
)
2624 include-descs
(mapcar (function
2626 (if (consp x
) x
(list x
))))
2628 ((eq opt
:print-function
)
2629 (setq print-func
(car args
)))
2631 (setq type
(car args
)))
2634 ((eq opt
:initial-offset
)
2635 (setq descs
(nconc (make-list (car args
) '(cl-skip-slot))
2638 (error "Slot option %s unrecognized" opt
)))))
2641 `(progn (funcall #',print-func cl-x cl-s cl-n
) t
))
2642 (or type
(and include
(not (get include
'cl-struct-print
)))
2644 print-func
(and (or (not (or include type
)) (null print-func
))
2646 (princ ,(format "#S(%s" name
) cl-s
))))))
2648 (let ((inc-type (get include
'cl-struct-type
))
2649 (old-descs (get include
'cl-struct-slots
)))
2650 (or inc-type
(error "%s is not a struct name" include
))
2651 (and type
(not (eq (car inc-type
) type
))
2652 (error ":type disagrees with :include for %s" name
))
2653 (while include-descs
2654 (setcar (memq (or (assq (caar include-descs
) old-descs
)
2655 (error "No slot %s in included struct %s"
2656 (caar include-descs
) include
))
2658 (pop include-descs
)))
2659 (setq descs
(append old-descs
(delq (assq 'cl-tag-slot descs
) descs
))
2661 named
(assq 'cl-tag-slot descs
))
2662 (if (cadr inc-type
) (setq tag name named t
))
2663 (let ((incl include
))
2665 (push `(cl-pushnew ',tag
2666 ,(intern (format "cl-struct-%s-tags" incl
)))
2668 (setq incl
(get incl
'cl-struct-include
)))))
2671 (or (memq type
'(vector list
))
2672 (error "Invalid :type specifier: %s" type
))
2673 (if named
(setq tag name
)))
2674 (setq type
'vector named
'true
)))
2675 (or named
(setq descs
(delq (assq 'cl-tag-slot descs
) descs
)))
2676 (push `(defvar ,tag-symbol
) forms
)
2677 (setq pred-form
(and named
2678 (let ((pos (- (length descs
)
2679 (length (memq (assq 'cl-tag-slot descs
)
2681 (if (eq type
'vector
)
2682 `(and (vectorp cl-x
)
2683 (>= (length cl-x
) ,(length descs
))
2684 (memq (aref cl-x
,pos
) ,tag-symbol
))
2686 `(memq (car-safe cl-x
) ,tag-symbol
)
2688 (memq (nth ,pos cl-x
) ,tag-symbol
))))))
2689 pred-check
(and pred-form
(> safety
0)
2690 (if (and (eq (cl-caadr pred-form
) 'vectorp
)
2692 (cons 'and
(cl-cdddr pred-form
)) pred-form
)))
2693 (let ((pos 0) (descp descs
))
2695 (let* ((desc (pop descp
))
2697 (if (memq slot
'(cl-tag-slot cl-skip-slot
))
2700 (push (and (eq slot
'cl-tag-slot
) `',tag
)
2702 (if (assq slot descp
)
2703 (error "Duplicate slots named %s in %s" slot name
))
2704 (let ((accessor (intern (format "%s%s" conc-name slot
))))
2706 (push (nth 1 desc
) defaults
)
2708 'cl-defsubst accessor
'(cl-x)
2711 (list `(or ,pred-check
2712 (error "%s accessing a non-%s"
2713 ',accessor
',name
))))
2714 (list (if (eq type
'vector
) `(aref cl-x
,pos
)
2715 (if (= pos
0) '(car cl-x
)
2716 `(nth ,pos cl-x
)))))) forms
)
2717 (push (cons accessor t
) side-eff
)
2718 (push `(cl-define-setf-method ,accessor
(cl-x)
2719 ,(if (cadr (memq :read-only
(cddr desc
)))
2720 `(progn (ignore cl-x
)
2721 (error "%s is a read-only slot"
2723 ;; If cl is loaded only for compilation,
2724 ;; the call to cl-struct-setf-expander would
2725 ;; cause a warning because it may not be
2726 ;; defined at run time. Suppress that warning.
2729 cl-struct-setf-expander
"cl-macs"
2730 (x name accessor pred-form pos
))
2731 (cl-struct-setf-expander
2732 cl-x
',name
',accessor
2733 ,(and pred-check
`',pred-check
)
2738 (list `(princ ,(format " %s" slot
) cl-s
)
2739 `(prin1 (,accessor cl-x
) cl-s
)))))))
2740 (setq pos
(1+ pos
))))
2741 (setq slots
(nreverse slots
)
2742 defaults
(nreverse defaults
))
2743 (and predicate pred-form
2744 (progn (push `(cl-defsubst ,predicate
(cl-x)
2745 ,(if (eq (car pred-form
) 'and
)
2746 (append pred-form
'(t))
2747 `(and ,pred-form t
))) forms
)
2748 (push (cons predicate
'error-free
) side-eff
)))
2750 (progn (push `(defun ,copier
(x) (copy-sequence x
)) forms
)
2751 (push (cons copier t
) side-eff
)))
2753 (push (list constructor
2754 (cons '&key
(delq nil
(copy-sequence slots
))))
2757 (let* ((name (caar constrs
))
2758 (args (cadr (pop constrs
)))
2759 (anames (cl-arglist-args args
))
2760 (make (cl-mapcar (function (lambda (s d
) (if (memq s anames
) s d
)))
2762 (push `(cl-defsubst ,name
2763 (&cl-defs
'(nil ,@descs
) ,@args
)
2764 (,type
,@make
)) forms
)
2765 (if (cl-safe-expr-p `(progn ,@(mapcar #'cl-second descs
)))
2766 (push (cons name t
) side-eff
))))
2767 (if print-auto
(nconc print-func
(list '(princ ")" cl-s
) t
)))
2770 ;; The auto-generated function does not pay attention to
2771 ;; the depth argument cl-n.
2772 (lambda (cl-x cl-s
,(if print-auto
'_cl-n
'cl-n
))
2773 (and ,pred-form
,print-func
))
2774 cl-custom-print-functions
)
2776 (push `(setq ,tag-symbol
(list ',tag
)) forms
)
2777 (push `(cl-eval-when (compile load eval
)
2778 (put ',name
'cl-struct-slots
',descs
)
2779 (put ',name
'cl-struct-type
',(list type
(eq named t
)))
2780 (put ',name
'cl-struct-include
',include
)
2781 (put ',name
'cl-struct-print
,print-auto
)
2782 ,@(mapcar (lambda (x)
2783 `(put ',(car x
) 'side-effect-free
',(cdr x
)))
2786 `(progn ,@(nreverse (cons `',name forms
)))))
2789 (defun cl-struct-setf-expander (x name accessor pred-form pos
)
2790 (let* ((temp (make-symbol "--cl-x--")) (store (make-symbol "--cl-store--")))
2791 (list (list temp
) (list x
) (list store
)
2794 (list `(or ,(cl-subst temp
'cl-x pred-form
)
2796 "%s storing a non-%s"
2798 ,(if (eq (car (get name
'cl-struct-type
)) 'vector
)
2799 `(aset ,temp
,pos
,store
)
2803 (while (>= (setq pos
(1- pos
)) 0)
2804 (setq xx
`(cdr ,xx
)))
2806 `(nthcdr ,pos
,temp
))
2808 (list accessor temp
))))
2811 ;;; Types and assertions.
2814 (defmacro cl-deftype
(name arglist
&rest body
)
2815 "Define NAME as a new data type.
2816 The type name can then be used in `cl-typecase', `cl-check-type', etc."
2817 (declare (debug cl-defmacro
) (doc-string 3))
2818 `(cl-eval-when (compile load eval
)
2819 ,(cl-transform-function-property
2820 name
'cl-deftype-handler
(cons `(&cl-defs
'('*) ,@arglist
) body
))))
2822 (defun cl-make-type-test (val type
)
2824 (cond ((get type
'cl-deftype-handler
)
2825 (cl-make-type-test val
(funcall (get type
'cl-deftype-handler
))))
2826 ((memq type
'(nil t
)) type
)
2827 ((eq type
'null
) `(null ,val
))
2828 ((eq type
'atom
) `(atom ,val
))
2829 ((eq type
'float
) `(cl-floatp-safe ,val
))
2830 ((eq type
'real
) `(numberp ,val
))
2831 ((eq type
'fixnum
) `(integerp ,val
))
2832 ;; FIXME: Should `character' accept things like ?\C-\M-a ? --Stef
2833 ((memq type
'(character string-char
)) `(characterp ,val
))
2835 (let* ((name (symbol-name type
))
2836 (namep (intern (concat name
"p"))))
2837 (if (fboundp namep
) (list namep val
)
2838 (list (intern (concat name
"-p")) val
)))))
2839 (cond ((get (car type
) 'cl-deftype-handler
)
2840 (cl-make-type-test val
(apply (get (car type
) 'cl-deftype-handler
)
2842 ((memq (car type
) '(integer float real number
))
2843 (delq t
`(and ,(cl-make-type-test val
(car type
))
2844 ,(if (memq (cadr type
) '(* nil
)) t
2845 (if (consp (cadr type
)) `(> ,val
,(cl-caadr type
))
2846 `(>= ,val
,(cadr type
))))
2847 ,(if (memq (cl-caddr type
) '(* nil
)) t
2848 (if (consp (cl-caddr type
)) `(< ,val
,(cl-caaddr type
))
2849 `(<= ,val
,(cl-caddr type
)))))))
2850 ((memq (car type
) '(and or not
))
2852 (mapcar (function (lambda (x) (cl-make-type-test val x
)))
2854 ((memq (car type
) '(member cl-member
))
2855 `(and (cl-member ,val
',(cdr type
)) t
))
2856 ((eq (car type
) 'satisfies
) (list (cadr type
) val
))
2857 (t (error "Bad type spec: %s" type
)))))
2860 (defun cl-typep (object type
) ; See compiler macro below.
2861 "Check that OBJECT is of type TYPE.
2862 TYPE is a Common Lisp-style type specifier."
2863 (eval (cl-make-type-test 'object type
)))
2866 (defmacro cl-check-type
(form type
&optional string
)
2867 "Verify that FORM is of type TYPE; signal an error if not.
2868 STRING is an optional description of the desired type."
2869 (declare (debug (place cl-type-spec
&optional stringp
)))
2870 (and (or (not (cl-compiling-file))
2871 (< cl-optimize-speed
3) (= cl-optimize-safety
3))
2872 (let* ((temp (if (cl-simple-expr-p form
3)
2873 form
(make-symbol "--cl-var--")))
2874 (body `(or ,(cl-make-type-test temp type
)
2875 (signal 'wrong-type-argument
2876 (list ,(or string
`',type
)
2878 (if (eq temp form
) `(progn ,body nil
)
2879 `(let ((,temp
,form
)) ,body nil
)))))
2882 (defmacro cl-assert
(form &optional show-args string
&rest args
)
2883 "Verify that FORM returns non-nil; signal an error if not.
2884 Second arg SHOW-ARGS means to include arguments of FORM in message.
2885 Other args STRING and ARGS... are arguments to be passed to `error'.
2886 They are not evaluated unless the assertion fails. If STRING is
2887 omitted, a default message listing FORM itself is used."
2888 (declare (debug (form &rest form
)))
2889 (and (or (not (cl-compiling-file))
2890 (< cl-optimize-speed
3) (= cl-optimize-safety
3))
2891 (let ((sargs (and show-args
2894 (unless (cl-const-expr-p x
)
2900 `(error ,string
,@sargs
,@args
)
2901 `(signal 'cl-assertion-failed
2902 (list ',form
,@sargs
))))
2905 ;;; Compiler macros.
2908 (defmacro cl-define-compiler-macro
(func args
&rest body
)
2909 "Define a compiler-only macro.
2910 This is like `defmacro', but macro expansion occurs only if the call to
2911 FUNC is compiled (i.e., not interpreted). Compiler macros should be used
2912 for optimizing the way calls to FUNC are compiled; the form returned by
2913 BODY should do the same thing as a call to the normal function called
2914 FUNC, though possibly more efficiently. Note that, like regular macros,
2915 compiler macros are expanded repeatedly until no further expansions are
2916 possible. Unlike regular macros, BODY can decide to \"punt\" and leave the
2917 original function call alone by declaring an initial `&whole foo' parameter
2918 and then returning foo."
2919 (declare (debug cl-defmacro
))
2920 (let ((p args
) (res nil
))
2921 (while (consp p
) (push (pop p
) res
))
2922 (setq args
(nconc (nreverse res
) (and p
(list '&rest p
)))))
2923 `(cl-eval-when (compile load eval
)
2924 ,(cl-transform-function-property
2925 func
'compiler-macro
2926 (cons (if (memq '&whole args
) (delq '&whole args
)
2927 (cons '_cl-whole-arg args
)) body
))
2928 ;; This is so that describe-function can locate
2929 ;; the macro definition.
2930 (let ((file ,(or buffer-file-name
2931 (and (boundp 'byte-compile-current-file
)
2932 (stringp byte-compile-current-file
)
2933 byte-compile-current-file
))))
2934 (if file
(put ',func
'compiler-macro-file
2935 (purecopy (file-name-nondirectory file
)))))))
2938 (defun cl-compiler-macroexpand (form)
2940 (let ((func (car-safe form
)) (handler nil
))
2941 (while (and (symbolp func
)
2942 (not (setq handler
(get func
'compiler-macro
)))
2944 (or (not (eq (car-safe (symbol-function func
)) 'autoload
))
2945 (load (nth 1 (symbol-function func
)))))
2946 (setq func
(symbol-function func
)))
2948 (not (eq form
(setq form
(apply handler form
(cdr form
))))))))
2951 (defun cl-byte-compile-compiler-macro (form)
2952 (if (eq form
(setq form
(cl-compiler-macroexpand form
)))
2953 (byte-compile-normal-call form
)
2954 (byte-compile-form form
)))
2956 ;; Optimize away unused block-wrappers.
2958 (defvar cl-active-block-names nil
)
2960 (cl-define-compiler-macro cl-block-wrapper
(cl-form)
2961 (let* ((cl-entry (cons (nth 1 (nth 1 cl-form
)) nil
))
2962 (cl-active-block-names (cons cl-entry cl-active-block-names
))
2963 (cl-body (macroexpand-all ;Performs compiler-macro expansions.
2964 (cons 'progn
(cddr cl-form
))
2965 macroexpand-all-environment
)))
2966 ;; FIXME: To avoid re-applying macroexpand-all, we'd like to be able
2967 ;; to indicate that this return value is already fully expanded.
2969 `(catch ,(nth 1 cl-form
) ,@(cdr cl-body
))
2972 (cl-define-compiler-macro cl-block-throw
(cl-tag cl-value
)
2973 (let ((cl-found (assq (nth 1 cl-tag
) cl-active-block-names
)))
2974 (if cl-found
(setcdr cl-found t
)))
2975 `(throw ,cl-tag
,cl-value
))
2978 (defmacro cl-defsubst
(name args
&rest body
)
2979 "Define NAME as a function.
2980 Like `defun', except the function is automatically declared `inline',
2981 ARGLIST allows full Common Lisp conventions, and BODY is implicitly
2982 surrounded by (cl-block NAME ...).
2984 \(fn NAME ARGLIST [DOCSTRING] BODY...)"
2985 (declare (debug cl-defun
))
2986 (let* ((argns (cl-arglist-args args
)) (p argns
)
2987 (pbody (cons 'progn body
))
2988 (unsafe (not (cl-safe-expr-p pbody
))))
2989 (while (and p
(eq (cl-expr-contains args
(car p
)) 1)) (pop p
))
2991 ,(if p nil
; give up if defaults refer to earlier args
2992 `(cl-define-compiler-macro ,name
2993 ,(if (memq '&key args
)
2994 `(&whole cl-whole
&cl-quote
,@args
)
2995 (cons '&cl-quote args
))
2997 ',argns
'(cl-block ,name
,@body
)
2998 ;; We used to pass `simple' as
2999 ;; (not (or unsafe (cl-expr-access-order pbody argns)))
3000 ;; But this is much too simplistic since it
3001 ;; does not pay attention to the argvs (and
3002 ;; cl-expr-access-order itself is also too naive).
3004 ,(and (memq '&key args
) 'cl-whole
) ,unsafe
,@argns
)))
3005 (cl-defun ,name
,args
,@body
))))
3007 (defun cl-defsubst-expand (argns body simple whole unsafe
&rest argvs
)
3008 (if (and whole
(not (cl-safe-expr-p (cons 'progn argvs
)))) whole
3009 (if (cl-simple-exprs-p argvs
) (setq simple t
))
3012 (cl-mapcar (lambda (argn argv
)
3013 (if (or simple
(cl-const-expr-p argv
))
3014 (progn (push (cons argn argv
) substs
)
3015 (and unsafe
(list argn argv
)))
3018 ;; FIXME: `sublis/subst' will happily substitute the symbol
3019 ;; `argn' in places where it's not used as a reference
3021 ;; FIXME: `sublis/subst' will happily copy `argv' to a different
3022 ;; scope, leading to name capture.
3023 (setq body
(cond ((null substs
) body
)
3024 ((null (cdr substs
))
3025 (cl-subst (cdar substs
) (caar substs
) body
))
3026 (t (cl-sublis substs body
))))
3027 (if lets
`(let ,lets
,body
) body
))))
3030 ;; Compile-time optimizations for some functions defined in this package.
3031 ;; Note that cl.el arranges to force cl-macs to be loaded at compile-time,
3032 ;; mainly to make sure these macros will be present.
3034 (put 'eql
'byte-compile nil
)
3035 (cl-define-compiler-macro eql
(&whole form a b
)
3036 (cond ((eq (cl-const-expr-p a
) t
)
3037 (let ((val (cl-const-expr-val a
)))
3038 (if (and (numberp val
) (not (integerp val
)))
3041 ((eq (cl-const-expr-p b
) t
)
3042 (let ((val (cl-const-expr-val b
)))
3043 (if (and (numberp val
) (not (integerp val
)))
3046 ((cl-simple-expr-p a
5)
3050 ((and (cl-safe-expr-p a
)
3051 (cl-simple-expr-p b
5))
3057 (cl-define-compiler-macro cl-member
(&whole form a list
&rest keys
)
3058 (let ((test (and (= (length keys
) 2) (eq (car keys
) :test
)
3059 (cl-const-expr-val (nth 1 keys
)))))
3060 (cond ((eq test
'eq
) `(memq ,a
,list
))
3061 ((eq test
'equal
) `(member ,a
,list
))
3062 ((or (null keys
) (eq test
'eql
)) `(memql ,a
,list
))
3065 (cl-define-compiler-macro cl-assoc
(&whole form a list
&rest keys
)
3066 (let ((test (and (= (length keys
) 2) (eq (car keys
) :test
)
3067 (cl-const-expr-val (nth 1 keys
)))))
3068 (cond ((eq test
'eq
) `(assq ,a
,list
))
3069 ((eq test
'equal
) `(assoc ,a
,list
))
3070 ((and (eq (cl-const-expr-p a
) t
) (or (null keys
) (eq test
'eql
)))
3071 (if (cl-floatp-safe (cl-const-expr-val a
))
3072 `(assoc ,a
,list
) `(assq ,a
,list
)))
3075 (cl-define-compiler-macro cl-adjoin
(&whole form a list
&rest keys
)
3076 (if (and (cl-simple-expr-p a
) (cl-simple-expr-p list
)
3077 (not (memq :key keys
)))
3078 `(if (cl-member ,a
,list
,@keys
) ,list
(cons ,a
,list
))
3081 (cl-define-compiler-macro cl-list
* (arg &rest others
)
3082 (let* ((args (reverse (cons arg others
)))
3084 (while (setq args
(cdr args
))
3085 (setq form
`(cons ,(car args
) ,form
)))
3088 (cl-define-compiler-macro cl-get
(sym prop
&optional def
)
3090 `(cl-getf (symbol-plist ,sym
) ,prop
,def
)
3093 (cl-define-compiler-macro cl-typep
(&whole form val type
)
3094 (if (cl-const-expr-p type
)
3095 (let ((res (cl-make-type-test val
(cl-const-expr-val type
))))
3096 (if (or (memq (cl-expr-contains res val
) '(nil 1))
3097 (cl-simple-expr-p val
)) res
3098 (let ((temp (make-symbol "--cl-var--")))
3099 `(let ((,temp
,val
)) ,(cl-subst temp val res
)))))
3104 (put (car y
) 'side-effect-free t
)
3105 (put (car y
) 'compiler-macro
3107 ,(if (symbolp (cadr y
))
3109 (list ',(cl-caddr y
) x
))
3110 (cons 'list
(cdr y
))))))
3111 '((cl-first 'car x
) (cl-second 'cadr x
) (cl-third 'cl-caddr x
) (cl-fourth 'cl-cadddr x
)
3112 (cl-fifth 'nth
4 x
) (cl-sixth 'nth
5 x
) (cl-seventh 'nth
6 x
)
3113 (cl-eighth 'nth
7 x
) (cl-ninth 'nth
8 x
) (cl-tenth 'nth
9 x
)
3114 (cl-rest 'cdr x
) (cl-endp 'null x
) (cl-plusp '> x
0) (cl-minusp '< x
0)
3115 (cl-caaar car caar
) (cl-caadr car cadr
) (cl-cadar car cdar
)
3116 (cl-caddr car cddr
) (cl-cdaar cdr caar
) (cl-cdadr cdr cadr
)
3117 (cl-cddar cdr cdar
) (cl-cdddr cdr cddr
) (cl-caaaar car cl-caaar
)
3118 (cl-caaadr car cl-caadr
) (cl-caadar car cl-cadar
) (cl-caaddr car cl-caddr
)
3119 (cl-cadaar car cl-cdaar
) (cl-cadadr car cl-cdadr
) (cl-caddar car cl-cddar
)
3120 (cl-cadddr car cl-cdddr
) (cl-cdaaar cdr cl-caaar
) (cl-cdaadr cdr cl-caadr
)
3121 (cl-cdadar cdr cl-cadar
) (cl-cdaddr cdr cl-caddr
) (cl-cddaar cdr cl-cdaar
)
3122 (cl-cddadr cdr cl-cdadr
) (cl-cdddar cdr cl-cddar
) (cl-cddddr cdr cl-cdddr
) ))
3124 ;;; Things that are inline.
3125 (cl-proclaim '(inline cl-floatp-safe cl-acons cl-map cl-concatenate cl-notany cl-notevery
3126 cl-set-elt cl-revappend cl-nreconc gethash
))
3128 ;;; Things that are side-effect-free.
3129 (mapc (lambda (x) (put x
'side-effect-free t
))
3130 '(cl-oddp cl-evenp cl-signum last butlast cl-ldiff cl-pairlis cl-gcd cl-lcm
3131 cl-isqrt cl-floor cl-ceiling cl-truncate cl-round cl-mod cl-rem cl-subseq
3132 cl-list-length cl-get cl-getf
))
3134 ;;; Things that are side-effect-and-error-free.
3135 (mapc (lambda (x) (put x
'side-effect-free
'error-free
))
3136 '(eql cl-floatp-safe cl-list
* cl-subst cl-acons cl-equalp cl-random-state-p
3137 copy-tree cl-sublis
))
3140 (run-hooks 'cl-macs-load-hook
)
3143 ;; byte-compile-dynamic: t
3144 ;; byte-compile-warnings: (not cl-functions)
3145 ;; generated-autoload-file: "cl-loaddefs.el"
3148 ;;; cl-macs.el ends here