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