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