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