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