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