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