comparison lisp/cl/cl-macs.el @ 0:376386a54a3c r19-14

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