Commit | Line | Data |
---|---|---|
d9857e53 | 1 | ;;; cl-macs.el --- Common Lisp macros -*- lexical-binding: t; coding: utf-8 -*- |
fcd73769 | 2 | |
ab422c4d | 3 | ;; Copyright (C) 1993, 2001-2013 Free Software Foundation, Inc. |
fcd73769 RS |
4 | |
5 | ;; Author: Dave Gillespie <daveg@synaptics.com> | |
12059709 | 6 | ;; Old-Version: 2.02 |
fcd73769 | 7 | ;; Keywords: extensions |
bd78fa1d | 8 | ;; Package: emacs |
fcd73769 RS |
9 | |
10 | ;; This file is part of GNU Emacs. | |
11 | ||
d6cba7ae | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
fcd73769 | 13 | ;; it under the terms of the GNU General Public License as published by |
d6cba7ae GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
fcd73769 RS |
16 | |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
d6cba7ae | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
fcd73769 | 24 | |
07b3798c | 25 | ;;; Commentary: |
fcd73769 RS |
26 | |
27 | ;; These are extensions to Emacs Lisp that provide a degree of | |
28 | ;; Common Lisp compatibility, beyond what is already built-in | |
29 | ;; in Emacs Lisp. | |
30 | ;; | |
31 | ;; This package was written by Dave Gillespie; it is a complete | |
32 | ;; rewrite of Cesar Quiroz's original cl.el package of December 1986. | |
33 | ;; | |
fcd73769 RS |
34 | ;; Bug reports, comments, and suggestions are welcome! |
35 | ||
36 | ;; This file contains the portions of the Common Lisp extensions | |
37 | ;; package which should be autoloaded, but need only be present | |
38 | ;; if the compiler or interpreter is used---this file is not | |
39 | ;; necessary for executing compiled code. | |
40 | ||
41 | ;; See cl.el for Change Log. | |
42 | ||
43 | ||
07b3798c | 44 | ;;; Code: |
fcd73769 | 45 | |
7c1898a7 | 46 | (require 'cl-lib) |
4dd1c416 | 47 | (require 'macroexp) |
2ee3d7f0 SM |
48 | ;; `gv' is required here because cl-macs can be loaded before loaddefs.el. |
49 | (require 'gv) | |
fcd73769 | 50 | |
338bfefa | 51 | (defmacro cl--pop2 (place) |
7c1898a7 | 52 | (declare (debug edebug-sexps)) |
03fef3e6 SM |
53 | `(prog1 (car (cdr ,place)) |
54 | (setq ,place (cdr (cdr ,place))))) | |
fcd73769 | 55 | |
338bfefa SM |
56 | (defvar cl--optimize-safety) |
57 | (defvar cl--optimize-speed) | |
fcd73769 | 58 | |
fcd73769 RS |
59 | ;;; Initialization. |
60 | ||
972debf2 SM |
61 | ;; Place compiler macros at the beginning, otherwise uses of the corresponding |
62 | ;; functions can lead to recursive-loads that prevent the calls from | |
63 | ;; being optimized. | |
64 | ||
65 | ;;;###autoload | |
66 | (defun cl--compiler-macro-list* (_form arg &rest others) | |
67 | (let* ((args (reverse (cons arg others))) | |
68 | (form (car args))) | |
69 | (while (setq args (cdr args)) | |
70 | (setq form `(cons ,(car args) ,form))) | |
71 | form)) | |
72 | ||
73 | ;;;###autoload | |
74 | (defun cl--compiler-macro-cXXr (form x) | |
75 | (let* ((head (car form)) | |
76 | (n (symbol-name (car form))) | |
77 | (i (- (length n) 2))) | |
78 | (if (not (string-match "c[ad]+r\\'" n)) | |
79 | (if (and (fboundp head) (symbolp (symbol-function head))) | |
80 | (cl--compiler-macro-cXXr (cons (symbol-function head) (cdr form)) | |
81 | x) | |
82 | (error "Compiler macro for cXXr applied to non-cXXr form")) | |
83 | (while (> i (match-beginning 0)) | |
84 | (setq x (list (if (eq (aref n i) ?a) 'car 'cdr) x)) | |
85 | (setq i (1- i))) | |
86 | x))) | |
87 | ||
4dd1c416 SM |
88 | ;;; Some predicates for analyzing Lisp forms. |
89 | ;; These are used by various | |
90 | ;; macro expanders to optimize the results in certain common cases. | |
fcd73769 | 91 | |
4dd1c416 | 92 | (defconst cl--simple-funcs '(car cdr nth aref elt if and or + - 1+ 1- min max |
b7b95a1e | 93 | car-safe cdr-safe progn prog1 prog2)) |
4dd1c416 | 94 | (defconst cl--safe-funcs '(* / % length memq list vector vectorp |
b7b95a1e DL |
95 | < > <= >= = error)) |
96 | ||
4dd1c416 SM |
97 | (defun cl--simple-expr-p (x &optional size) |
98 | "Check if no side effects, and executes quickly." | |
b7b95a1e | 99 | (or size (setq size 10)) |
7c1898a7 | 100 | (if (and (consp x) (not (memq (car x) '(quote function cl-function)))) |
b7b95a1e | 101 | (and (symbolp (car x)) |
4dd1c416 | 102 | (or (memq (car x) cl--simple-funcs) |
b7b95a1e DL |
103 | (get (car x) 'side-effect-free)) |
104 | (progn | |
105 | (setq size (1- size)) | |
106 | (while (and (setq x (cdr x)) | |
4dd1c416 | 107 | (setq size (cl--simple-expr-p (car x) size)))) |
b7b95a1e DL |
108 | (and (null x) (>= size 0) size))) |
109 | (and (> size 0) (1- size)))) | |
110 | ||
4dd1c416 SM |
111 | (defun cl--simple-exprs-p (xs) |
112 | (while (and xs (cl--simple-expr-p (car xs))) | |
b7b95a1e DL |
113 | (setq xs (cdr xs))) |
114 | (not xs)) | |
115 | ||
4dd1c416 SM |
116 | (defun cl--safe-expr-p (x) |
117 | "Check if no side effects." | |
7c1898a7 | 118 | (or (not (and (consp x) (not (memq (car x) '(quote function cl-function))))) |
b7b95a1e | 119 | (and (symbolp (car x)) |
4dd1c416 SM |
120 | (or (memq (car x) cl--simple-funcs) |
121 | (memq (car x) cl--safe-funcs) | |
b7b95a1e DL |
122 | (get (car x) 'side-effect-free)) |
123 | (progn | |
4dd1c416 | 124 | (while (and (setq x (cdr x)) (cl--safe-expr-p (car x)))) |
b7b95a1e DL |
125 | (null x))))) |
126 | ||
127 | ;;; Check if constant (i.e., no side effects or dependencies). | |
4dd1c416 | 128 | (defun cl--const-expr-p (x) |
b7b95a1e DL |
129 | (cond ((consp x) |
130 | (or (eq (car x) 'quote) | |
7c1898a7 | 131 | (and (memq (car x) '(function cl-function)) |
b7b95a1e DL |
132 | (or (symbolp (nth 1 x)) |
133 | (and (eq (car-safe (nth 1 x)) 'lambda) 'func))))) | |
134 | ((symbolp x) (and (memq x '(nil t)) t)) | |
135 | (t t))) | |
136 | ||
4dd1c416 SM |
137 | (defun cl--const-expr-val (x) |
138 | (and (macroexp-const-p x) (if (consp x) (nth 1 x) x))) | |
b7b95a1e | 139 | |
4dd1c416 SM |
140 | (defun cl--expr-contains (x y) |
141 | "Count number of times X refers to Y. Return nil for 0 times." | |
7c1898a7 | 142 | ;; FIXME: This is naive, and it will cl-count Y as referred twice in |
e129292c CS |
143 | ;; (let ((Y 1)) Y) even though it should be 0. Also it is often called on |
144 | ;; non-macroexpanded code, so it may also miss some occurrences that would | |
145 | ;; only appear in the expanded code. | |
b7b95a1e | 146 | (cond ((equal y x) 1) |
7c1898a7 | 147 | ((and (consp x) (not (memq (car x) '(quote function cl-function)))) |
b7b95a1e | 148 | (let ((sum 0)) |
e129292c | 149 | (while (consp x) |
4dd1c416 SM |
150 | (setq sum (+ sum (or (cl--expr-contains (pop x) y) 0)))) |
151 | (setq sum (+ sum (or (cl--expr-contains x y) 0))) | |
b7b95a1e DL |
152 | (and (> sum 0) sum))) |
153 | (t nil))) | |
154 | ||
4dd1c416 SM |
155 | (defun cl--expr-contains-any (x y) |
156 | (while (and y (not (cl--expr-contains x (car y)))) (pop y)) | |
b7b95a1e DL |
157 | y) |
158 | ||
4dd1c416 SM |
159 | (defun cl--expr-depends-p (x y) |
160 | "Check whether X may depend on any of the symbols in Y." | |
161 | (and (not (macroexp-const-p x)) | |
162 | (or (not (cl--safe-expr-p x)) (cl--expr-contains-any x y)))) | |
b7b95a1e | 163 | |
fcd73769 RS |
164 | ;;; Symbols. |
165 | ||
4735906a | 166 | (defvar cl--gensym-counter) |
ebacfcc6 | 167 | ;;;###autoload |
7c1898a7 | 168 | (defun cl-gensym (&optional prefix) |
fcd73769 RS |
169 | "Generate a new uninterned symbol. |
170 | The name is made by appending a number to PREFIX, default \"G\"." | |
a766dfa1 JB |
171 | (let ((pfix (if (stringp prefix) prefix "G")) |
172 | (num (if (integerp prefix) prefix | |
4735906a SM |
173 | (prog1 cl--gensym-counter |
174 | (setq cl--gensym-counter (1+ cl--gensym-counter)))))) | |
a766dfa1 | 175 | (make-symbol (format "%s%d" pfix num)))) |
fcd73769 | 176 | |
ebacfcc6 | 177 | ;;;###autoload |
7c1898a7 | 178 | (defun cl-gentemp (&optional prefix) |
fcd73769 RS |
179 | "Generate a new interned symbol with a unique name. |
180 | The name is made by appending a number to PREFIX, default \"G\"." | |
a766dfa1 | 181 | (let ((pfix (if (stringp prefix) prefix "G")) |
fcd73769 | 182 | name) |
4735906a SM |
183 | (while (intern-soft (setq name (format "%s%d" pfix cl--gensym-counter))) |
184 | (setq cl--gensym-counter (1+ cl--gensym-counter))) | |
fcd73769 RS |
185 | (intern name))) |
186 | ||
187 | ||
188 | ;;; Program structure. | |
189 | ||
b1198e17 | 190 | (def-edebug-spec cl-declarations |
7c1898a7 | 191 | (&rest ("cl-declare" &rest sexp))) |
b1198e17 SM |
192 | |
193 | (def-edebug-spec cl-declarations-or-string | |
194 | (&or stringp cl-declarations)) | |
195 | ||
196 | (def-edebug-spec cl-lambda-list | |
197 | (([&rest arg] | |
198 | [&optional ["&optional" cl-&optional-arg &rest cl-&optional-arg]] | |
199 | [&optional ["&rest" arg]] | |
200 | [&optional ["&key" [cl-&key-arg &rest cl-&key-arg] | |
201 | &optional "&allow-other-keys"]] | |
202 | [&optional ["&aux" &rest | |
203 | &or (symbolp &optional def-form) symbolp]] | |
204 | ))) | |
205 | ||
206 | (def-edebug-spec cl-&optional-arg | |
207 | (&or (arg &optional def-form arg) arg)) | |
208 | ||
209 | (def-edebug-spec cl-&key-arg | |
210 | (&or ([&or (symbolp arg) arg] &optional def-form arg) arg)) | |
211 | ||
bb3faf5b SM |
212 | (defconst cl--lambda-list-keywords |
213 | '(&optional &rest &key &allow-other-keys &aux &whole &body &environment)) | |
214 | ||
215 | (defvar cl--bind-block) (defvar cl--bind-defs) (defvar cl--bind-enquote) | |
216 | (defvar cl--bind-inits) (defvar cl--bind-lets) (defvar cl--bind-forms) | |
217 | ||
218 | (defun cl--transform-lambda (form bind-block) | |
35a10516 SM |
219 | "Transform a function form FORM of name BIND-BLOCK. |
220 | BIND-BLOCK is the name of the symbol to which the function will be bound, | |
221 | and which will be used for the name of the `cl-block' surrounding the | |
222 | function's body. | |
223 | FORM is of the form (ARGS . BODY)." | |
bb3faf5b SM |
224 | (let* ((args (car form)) (body (cdr form)) (orig-args args) |
225 | (cl--bind-block bind-block) (cl--bind-defs nil) (cl--bind-enquote nil) | |
226 | (cl--bind-inits nil) (cl--bind-lets nil) (cl--bind-forms nil) | |
227 | (header nil) (simple-args nil)) | |
228 | (while (or (stringp (car body)) | |
35a10516 | 229 | (memq (car-safe (car body)) '(interactive declare cl-declare))) |
bb3faf5b SM |
230 | (push (pop body) header)) |
231 | (setq args (if (listp args) (cl-copy-list args) (list '&rest args))) | |
232 | (let ((p (last args))) (if (cdr p) (setcdr p (list '&rest (cdr p))))) | |
233 | (if (setq cl--bind-defs (cadr (memq '&cl-defs args))) | |
234 | (setq args (delq '&cl-defs (delq cl--bind-defs args)) | |
235 | cl--bind-defs (cadr cl--bind-defs))) | |
236 | (if (setq cl--bind-enquote (memq '&cl-quote args)) | |
237 | (setq args (delq '&cl-quote args))) | |
238 | (if (memq '&whole args) (error "&whole not currently implemented")) | |
239 | (let* ((p (memq '&environment args)) (v (cadr p)) | |
240 | (env-exp 'macroexpand-all-environment)) | |
241 | (if p (setq args (nconc (delq (car p) (delq v args)) | |
242 | (list '&aux (list v env-exp)))))) | |
243 | (while (and args (symbolp (car args)) | |
244 | (not (memq (car args) '(nil &rest &body &key &aux))) | |
245 | (not (and (eq (car args) '&optional) | |
246 | (or cl--bind-defs (consp (cadr args)))))) | |
247 | (push (pop args) simple-args)) | |
248 | (or (eq cl--bind-block 'cl-none) | |
249 | (setq body (list `(cl-block ,cl--bind-block ,@body)))) | |
250 | (if (null args) | |
251 | (cl-list* nil (nreverse simple-args) (nconc (nreverse header) body)) | |
252 | (if (memq '&optional simple-args) (push '&optional args)) | |
253 | (cl--do-arglist args nil (- (length simple-args) | |
254 | (if (memq '&optional simple-args) 1 0))) | |
255 | (setq cl--bind-lets (nreverse cl--bind-lets)) | |
256 | (cl-list* (and cl--bind-inits `(cl-eval-when (compile load eval) | |
257 | ,@(nreverse cl--bind-inits))) | |
258 | (nconc (nreverse simple-args) | |
259 | (list '&rest (car (pop cl--bind-lets)))) | |
260 | (nconc (let ((hdr (nreverse header))) | |
261 | ;; Macro expansion can take place in the middle of | |
262 | ;; apparently harmless computation, so it should not | |
263 | ;; touch the match-data. | |
264 | (save-match-data | |
265 | (require 'help-fns) | |
266 | (cons (help-add-fundoc-usage | |
267 | (if (stringp (car hdr)) (pop hdr)) | |
589fc479 SM |
268 | ;; Be careful with make-symbol and (back)quote, |
269 | ;; see bug#12884. | |
270 | (let ((print-gensym nil) (print-quoted t)) | |
271 | (format "%S" (cons 'fn (cl--make-usage-args | |
272 | orig-args))))) | |
bb3faf5b SM |
273 | hdr))) |
274 | (list `(let* ,cl--bind-lets | |
275 | ,@(nreverse cl--bind-forms) | |
276 | ,@body))))))) | |
277 | ||
ebacfcc6 | 278 | ;;;###autoload |
7c1898a7 | 279 | (defmacro cl-defun (name args &rest body) |
69d8fb1e | 280 | "Define NAME as a function. |
fcd73769 | 281 | Like normal `defun', except ARGLIST allows full Common Lisp conventions, |
7c1898a7 | 282 | and BODY is implicitly surrounded by (cl-block NAME ...). |
69d8fb1e SM |
283 | |
284 | \(fn NAME ARGLIST [DOCSTRING] BODY...)" | |
b1198e17 SM |
285 | (declare (debug |
286 | ;; Same as defun but use cl-lambda-list. | |
2ee3d7f0 | 287 | (&define [&or name ("setf" :name setf name)] |
b1198e17 SM |
288 | cl-lambda-list |
289 | cl-declarations-or-string | |
290 | [&optional ("interactive" interactive)] | |
70b8ef8f | 291 | def-body)) |
b581bb5c | 292 | (doc-string 3) |
70b8ef8f | 293 | (indent 2)) |
4dd1c416 | 294 | (let* ((res (cl--transform-lambda (cons args body) name)) |
03fef3e6 SM |
295 | (form `(defun ,name ,@(cdr res)))) |
296 | (if (car res) `(progn ,(car res) ,form) form))) | |
fcd73769 | 297 | |
b1198e17 SM |
298 | ;; The lambda list for macros is different from that of normal lambdas. |
299 | ;; Note that &environment is only allowed as first or last items in the | |
300 | ;; top level list. | |
301 | ||
302 | (def-edebug-spec cl-macro-list | |
303 | (([&optional "&environment" arg] | |
304 | [&rest cl-macro-arg] | |
305 | [&optional ["&optional" &rest | |
306 | &or (cl-macro-arg &optional def-form cl-macro-arg) arg]] | |
307 | [&optional [[&or "&rest" "&body"] cl-macro-arg]] | |
308 | [&optional ["&key" [&rest | |
309 | [&or ([&or (symbolp cl-macro-arg) arg] | |
310 | &optional def-form cl-macro-arg) | |
311 | arg]] | |
312 | &optional "&allow-other-keys"]] | |
313 | [&optional ["&aux" &rest | |
314 | &or (symbolp &optional def-form) symbolp]] | |
315 | [&optional "&environment" arg] | |
316 | ))) | |
317 | ||
318 | (def-edebug-spec cl-macro-arg | |
319 | (&or arg cl-macro-list1)) | |
320 | ||
321 | (def-edebug-spec cl-macro-list1 | |
322 | (([&optional "&whole" arg] ;; only allowed at lower levels | |
323 | [&rest cl-macro-arg] | |
324 | [&optional ["&optional" &rest | |
325 | &or (cl-macro-arg &optional def-form cl-macro-arg) arg]] | |
326 | [&optional [[&or "&rest" "&body"] cl-macro-arg]] | |
327 | [&optional ["&key" [&rest | |
328 | [&or ([&or (symbolp cl-macro-arg) arg] | |
329 | &optional def-form cl-macro-arg) | |
330 | arg]] | |
331 | &optional "&allow-other-keys"]] | |
332 | [&optional ["&aux" &rest | |
333 | &or (symbolp &optional def-form) symbolp]] | |
334 | . [&or arg nil]))) | |
335 | ||
ebacfcc6 | 336 | ;;;###autoload |
7c1898a7 | 337 | (defmacro cl-defmacro (name args &rest body) |
69d8fb1e | 338 | "Define NAME as a macro. |
fcd73769 | 339 | Like normal `defmacro', except ARGLIST allows full Common Lisp conventions, |
7c1898a7 | 340 | and BODY is implicitly surrounded by (cl-block NAME ...). |
69d8fb1e SM |
341 | |
342 | \(fn NAME ARGLIST [DOCSTRING] BODY...)" | |
b1198e17 | 343 | (declare (debug |
70b8ef8f | 344 | (&define name cl-macro-list cl-declarations-or-string def-body)) |
b581bb5c | 345 | (doc-string 3) |
70b8ef8f | 346 | (indent 2)) |
4dd1c416 | 347 | (let* ((res (cl--transform-lambda (cons args body) name)) |
03fef3e6 SM |
348 | (form `(defmacro ,name ,@(cdr res)))) |
349 | (if (car res) `(progn ,(car res) ,form) form))) | |
fcd73769 | 350 | |
b1198e17 SM |
351 | (def-edebug-spec cl-lambda-expr |
352 | (&define ("lambda" cl-lambda-list | |
353 | ;;cl-declarations-or-string | |
354 | ;;[&optional ("interactive" interactive)] | |
355 | def-body))) | |
356 | ||
7c1898a7 | 357 | ;; Redefine function-form to also match cl-function |
b1198e17 SM |
358 | (def-edebug-spec function-form |
359 | ;; form at the end could also handle "function", | |
360 | ;; but recognize it specially to avoid wrapping function forms. | |
361 | (&or ([&or "quote" "function"] &or symbolp lambda-expr) | |
7c1898a7 | 362 | ("cl-function" cl-function) |
b1198e17 SM |
363 | form)) |
364 | ||
ebacfcc6 | 365 | ;;;###autoload |
7c1898a7 | 366 | (defmacro cl-function (func) |
64a4c526 | 367 | "Introduce a function. |
3187ba1c JB |
368 | Like normal `function', except that if argument is a lambda form, |
369 | its argument list allows full Common Lisp conventions." | |
b1198e17 | 370 | (declare (debug (&or symbolp cl-lambda-expr))) |
fcd73769 | 371 | (if (eq (car-safe func) 'lambda) |
4dd1c416 | 372 | (let* ((res (cl--transform-lambda (cdr func) 'cl-none)) |
03fef3e6 SM |
373 | (form `(function (lambda . ,(cdr res))))) |
374 | (if (car res) `(progn ,(car res) ,form) form)) | |
375 | `(function ,func))) | |
fcd73769 | 376 | |
b4587710 JB |
377 | (declare-function help-add-fundoc-usage "help-fns" (docstring arglist)) |
378 | ||
673e08bb SM |
379 | (defun cl--make-usage-var (x) |
380 | "X can be a var or a (destructuring) lambda-list." | |
381 | (cond | |
382 | ((symbolp x) (make-symbol (upcase (symbol-name x)))) | |
383 | ((consp x) (cl--make-usage-args x)) | |
384 | (t x))) | |
385 | ||
386 | (defun cl--make-usage-args (arglist) | |
b68581e2 SM |
387 | (if (cdr-safe (last arglist)) ;Not a proper list. |
388 | (let* ((last (last arglist)) | |
389 | (tail (cdr last))) | |
390 | (unwind-protect | |
391 | (progn | |
392 | (setcdr last nil) | |
393 | (nconc (cl--make-usage-args arglist) (cl--make-usage-var tail))) | |
394 | (setcdr last tail))) | |
395 | ;; `orig-args' can contain &cl-defs (an internal | |
396 | ;; CL thingy I don't understand), so remove it. | |
397 | (let ((x (memq '&cl-defs arglist))) | |
398 | (when x (setq arglist (delq (car x) (remq (cadr x) arglist))))) | |
399 | (let ((state nil)) | |
400 | (mapcar (lambda (x) | |
401 | (cond | |
402 | ((symbolp x) | |
d458ef98 SM |
403 | (let ((first (aref (symbol-name x) 0))) |
404 | (if (eq ?\& first) | |
405 | (setq state x) | |
406 | ;; Strip a leading underscore, since it only | |
407 | ;; means that this argument is unused. | |
408 | (make-symbol (upcase (if (eq ?_ first) | |
409 | (substring (symbol-name x) 1) | |
410 | (symbol-name x))))))) | |
b68581e2 SM |
411 | ((not (consp x)) x) |
412 | ((memq state '(nil &rest)) (cl--make-usage-args x)) | |
413 | (t ;(VAR INITFORM SVAR) or ((KEYWORD VAR) INITFORM SVAR). | |
414 | (cl-list* | |
415 | (if (and (consp (car x)) (eq state '&key)) | |
416 | (list (caar x) (cl--make-usage-var (nth 1 (car x)))) | |
417 | (cl--make-usage-var (car x))) | |
418 | (nth 1 x) ;INITFORM. | |
419 | (cl--make-usage-args (nthcdr 2 x)) ;SVAR. | |
420 | )))) | |
421 | arglist)))) | |
673e08bb | 422 | |
4dd1c416 | 423 | (defun cl--do-arglist (args expr &optional num) ; uses bind-* |
fcd73769 | 424 | (if (nlistp args) |
bb3faf5b | 425 | (if (or (memq args cl--lambda-list-keywords) (not (symbolp args))) |
fcd73769 | 426 | (error "Invalid argument name: %s" args) |
513749ee | 427 | (push (list args expr) cl--bind-lets)) |
7c1898a7 | 428 | (setq args (cl-copy-list args)) |
fcd73769 RS |
429 | (let ((p (last args))) (if (cdr p) (setcdr p (list '&rest (cdr p))))) |
430 | (let ((p (memq '&body args))) (if p (setcar p '&rest))) | |
431 | (if (memq '&environment args) (error "&environment used incorrectly")) | |
432 | (let ((save-args args) | |
433 | (restarg (memq '&rest args)) | |
338bfefa | 434 | (safety (if (cl--compiling-file) cl--optimize-safety 3)) |
fcd73769 RS |
435 | (keys nil) |
436 | (laterarg nil) (exactarg nil) minarg) | |
437 | (or num (setq num 0)) | |
438 | (if (listp (cadr restarg)) | |
e542ea4b | 439 | (setq restarg (make-symbol "--cl-rest--")) |
fcd73769 | 440 | (setq restarg (cadr restarg))) |
513749ee | 441 | (push (list restarg expr) cl--bind-lets) |
fcd73769 | 442 | (if (eq (car args) '&whole) |
338bfefa | 443 | (push (list (cl--pop2 args) restarg) cl--bind-lets)) |
fcd73769 RS |
444 | (let ((p args)) |
445 | (setq minarg restarg) | |
bb3faf5b | 446 | (while (and p (not (memq (car p) cl--lambda-list-keywords))) |
fcd73769 RS |
447 | (or (eq p args) (setq minarg (list 'cdr minarg))) |
448 | (setq p (cdr p))) | |
449 | (if (memq (car p) '(nil &aux)) | |
03fef3e6 | 450 | (setq minarg `(= (length ,restarg) |
7c1898a7 | 451 | ,(length (cl-ldiff args p))) |
fcd73769 | 452 | exactarg (not (eq args p))))) |
bb3faf5b | 453 | (while (and args (not (memq (car args) cl--lambda-list-keywords))) |
fcd73769 RS |
454 | (let ((poparg (list (if (or (cdr args) (not exactarg)) 'pop 'car) |
455 | restarg))) | |
4dd1c416 | 456 | (cl--do-arglist |
69d8fb1e | 457 | (pop args) |
fcd73769 | 458 | (if (or laterarg (= safety 0)) poparg |
03fef3e6 SM |
459 | `(if ,minarg ,poparg |
460 | (signal 'wrong-number-of-arguments | |
513749ee SM |
461 | (list ,(and (not (eq cl--bind-block 'cl-none)) |
462 | `',cl--bind-block) | |
03fef3e6 | 463 | (length ,restarg))))))) |
fcd73769 | 464 | (setq num (1+ num) laterarg t)) |
69d8fb1e | 465 | (while (and (eq (car args) '&optional) (pop args)) |
bb3faf5b | 466 | (while (and args (not (memq (car args) cl--lambda-list-keywords))) |
69d8fb1e | 467 | (let ((arg (pop args))) |
fcd73769 | 468 | (or (consp arg) (setq arg (list arg))) |
4dd1c416 | 469 | (if (cddr arg) (cl--do-arglist (nth 2 arg) `(and ,restarg t))) |
fcd73769 | 470 | (let ((def (if (cdr arg) (nth 1 arg) |
513749ee SM |
471 | (or (car cl--bind-defs) |
472 | (nth 1 (assq (car arg) cl--bind-defs))))) | |
03fef3e6 | 473 | (poparg `(pop ,restarg))) |
513749ee | 474 | (and def cl--bind-enquote (setq def `',def)) |
4dd1c416 | 475 | (cl--do-arglist (car arg) |
03fef3e6 | 476 | (if def `(if ,restarg ,poparg ,def) poparg)) |
fcd73769 RS |
477 | (setq num (1+ num)))))) |
478 | (if (eq (car args) '&rest) | |
338bfefa | 479 | (let ((arg (cl--pop2 args))) |
4dd1c416 | 480 | (if (consp arg) (cl--do-arglist arg restarg))) |
fcd73769 | 481 | (or (eq (car args) '&key) (= safety 0) exactarg |
03fef3e6 SM |
482 | (push `(if ,restarg |
483 | (signal 'wrong-number-of-arguments | |
484 | (list | |
513749ee SM |
485 | ,(and (not (eq cl--bind-block 'cl-none)) |
486 | `',cl--bind-block) | |
03fef3e6 | 487 | (+ ,num (length ,restarg))))) |
513749ee | 488 | cl--bind-forms))) |
69d8fb1e | 489 | (while (and (eq (car args) '&key) (pop args)) |
bb3faf5b | 490 | (while (and args (not (memq (car args) cl--lambda-list-keywords))) |
69d8fb1e | 491 | (let ((arg (pop args))) |
dd441b46 | 492 | (or (consp arg) (setq arg (list arg))) |
fcd73769 | 493 | (let* ((karg (if (consp (car arg)) (caar arg) |
d458ef98 SM |
494 | (let ((name (symbol-name (car arg)))) |
495 | ;; Strip a leading underscore, since it only | |
496 | ;; means that this argument is unused, but | |
497 | ;; shouldn't affect the key's name (bug#12367). | |
498 | (if (eq ?_ (aref name 0)) | |
499 | (setq name (substring name 1))) | |
500 | (intern (format ":%s" name))))) | |
7c1898a7 | 501 | (varg (if (consp (car arg)) (cl-cadar arg) (car arg))) |
fcd73769 | 502 | (def (if (cdr arg) (cadr arg) |
513749ee | 503 | (or (car cl--bind-defs) (cadr (assq varg cl--bind-defs))))) |
03fef3e6 | 504 | (look `(memq ',karg ,restarg))) |
513749ee | 505 | (and def cl--bind-enquote (setq def `',def)) |
fcd73769 | 506 | (if (cddr arg) |
e542ea4b | 507 | (let* ((temp (or (nth 2 arg) (make-symbol "--cl-var--"))) |
03fef3e6 | 508 | (val `(car (cdr ,temp)))) |
4dd1c416 SM |
509 | (cl--do-arglist temp look) |
510 | (cl--do-arglist varg | |
03fef3e6 SM |
511 | `(if ,temp |
512 | (prog1 ,val (setq ,temp t)) | |
513 | ,def))) | |
4dd1c416 | 514 | (cl--do-arglist |
fcd73769 | 515 | varg |
03fef3e6 | 516 | `(car (cdr ,(if (null def) |
fcd73769 | 517 | look |
03fef3e6 | 518 | `(or ,look |
4dd1c416 SM |
519 | ,(if (eq (cl--const-expr-p def) t) |
520 | `'(nil ,(cl--const-expr-val def)) | |
03fef3e6 | 521 | `(list nil ,def)))))))) |
69d8fb1e | 522 | (push karg keys))))) |
fcd73769 | 523 | (setq keys (nreverse keys)) |
69d8fb1e | 524 | (or (and (eq (car args) '&allow-other-keys) (pop args)) |
fcd73769 | 525 | (null keys) (= safety 0) |
e542ea4b | 526 | (let* ((var (make-symbol "--cl-keys--")) |
fcd73769 | 527 | (allow '(:allow-other-keys)) |
03fef3e6 SM |
528 | (check `(while ,var |
529 | (cond | |
530 | ((memq (car ,var) ',(append keys allow)) | |
531 | (setq ,var (cdr (cdr ,var)))) | |
532 | ((car (cdr (memq (quote ,@allow) ,restarg))) | |
533 | (setq ,var nil)) | |
534 | (t | |
535 | (error | |
536 | ,(format "Keyword argument %%s not one of %s" | |
537 | keys) | |
538 | (car ,var))))))) | |
513749ee | 539 | (push `(let ((,var ,restarg)) ,check) cl--bind-forms))) |
69d8fb1e | 540 | (while (and (eq (car args) '&aux) (pop args)) |
bb3faf5b | 541 | (while (and args (not (memq (car args) cl--lambda-list-keywords))) |
fcd73769 | 542 | (if (consp (car args)) |
513749ee | 543 | (if (and cl--bind-enquote (cl-cadar args)) |
4dd1c416 | 544 | (cl--do-arglist (caar args) |
03fef3e6 | 545 | `',(cadr (pop args))) |
4dd1c416 SM |
546 | (cl--do-arglist (caar args) (cadr (pop args)))) |
547 | (cl--do-arglist (pop args) nil)))) | |
fcd73769 RS |
548 | (if args (error "Malformed argument list %s" save-args))))) |
549 | ||
4dd1c416 | 550 | (defun cl--arglist-args (args) |
fcd73769 RS |
551 | (if (nlistp args) (list args) |
552 | (let ((res nil) (kind nil) arg) | |
553 | (while (consp args) | |
69d8fb1e | 554 | (setq arg (pop args)) |
bb3faf5b | 555 | (if (memq arg cl--lambda-list-keywords) (setq kind arg) |
69d8fb1e | 556 | (if (eq arg '&cl-defs) (pop args) |
fcd73769 RS |
557 | (and (consp arg) kind (setq arg (car arg))) |
558 | (and (consp arg) (cdr arg) (eq kind '&key) (setq arg (cadr arg))) | |
4dd1c416 | 559 | (setq res (nconc res (cl--arglist-args arg)))))) |
fcd73769 RS |
560 | (nconc res (and args (list args)))))) |
561 | ||
ebacfcc6 | 562 | ;;;###autoload |
7c1898a7 | 563 | (defmacro cl-destructuring-bind (args expr &rest body) |
5593ed90 | 564 | "Bind the variables in ARGS to the result of EXPR and execute BODY." |
70b8ef8f SM |
565 | (declare (indent 2) |
566 | (debug (&define cl-macro-list def-form cl-declarations def-body))) | |
513749ee SM |
567 | (let* ((cl--bind-lets nil) (cl--bind-forms nil) (cl--bind-inits nil) |
568 | (cl--bind-defs nil) (cl--bind-block 'cl-none) (cl--bind-enquote nil)) | |
4dd1c416 | 569 | (cl--do-arglist (or args '(&aux)) expr) |
513749ee SM |
570 | (append '(progn) cl--bind-inits |
571 | (list `(let* ,(nreverse cl--bind-lets) | |
572 | ,@(nreverse cl--bind-forms) ,@body))))) | |
fcd73769 RS |
573 | |
574 | ||
7c1898a7 | 575 | ;;; The `cl-eval-when' form. |
fcd73769 | 576 | |
338bfefa | 577 | (defvar cl--not-toplevel nil) |
fcd73769 | 578 | |
ebacfcc6 | 579 | ;;;###autoload |
7c1898a7 | 580 | (defmacro cl-eval-when (when &rest body) |
69d8fb1e | 581 | "Control when BODY is evaluated. |
fcd73769 RS |
582 | If `compile' is in WHEN, BODY is evaluated when compiled at top-level. |
583 | If `load' is in WHEN, BODY is evaluated when loaded after top-level compile. | |
69d8fb1e SM |
584 | If `eval' is in WHEN, BODY is evaluated when interpreted or at non-top-level. |
585 | ||
586 | \(fn (WHEN...) BODY...)" | |
cf4e5178 | 587 | (declare (indent 1) (debug (sexp body))) |
bb3faf5b | 588 | (if (and (fboundp 'cl--compiling-file) (cl--compiling-file) |
338bfefa | 589 | (not cl--not-toplevel) (not (boundp 'for-effect))) ;Horrible kludge. |
64a4c526 | 590 | (let ((comp (or (memq 'compile when) (memq :compile-toplevel when))) |
338bfefa | 591 | (cl--not-toplevel t)) |
64a4c526 | 592 | (if (or (memq 'load when) (memq :load-toplevel when)) |
4dd1c416 | 593 | (if comp (cons 'progn (mapcar 'cl--compile-time-too body)) |
03fef3e6 | 594 | `(if nil nil ,@body)) |
fcd73769 | 595 | (progn (if comp (eval (cons 'progn body))) nil))) |
64a4c526 | 596 | (and (or (memq 'eval when) (memq :execute when)) |
fcd73769 RS |
597 | (cons 'progn body)))) |
598 | ||
4dd1c416 | 599 | (defun cl--compile-time-too (form) |
fcd73769 RS |
600 | (or (and (symbolp (car-safe form)) (get (car-safe form) 'byte-hunk-handler)) |
601 | (setq form (macroexpand | |
7c1898a7 | 602 | form (cons '(cl-eval-when) byte-compile-macro-environment)))) |
fcd73769 | 603 | (cond ((eq (car-safe form) 'progn) |
4dd1c416 | 604 | (cons 'progn (mapcar 'cl--compile-time-too (cdr form)))) |
7c1898a7 | 605 | ((eq (car-safe form) 'cl-eval-when) |
fcd73769 | 606 | (let ((when (nth 1 form))) |
64a4c526 | 607 | (if (or (memq 'eval when) (memq :execute when)) |
7c1898a7 | 608 | `(cl-eval-when (compile ,@when) ,@(cddr form)) |
fcd73769 RS |
609 | form))) |
610 | (t (eval form) form))) | |
611 | ||
ebacfcc6 | 612 | ;;;###autoload |
513749ee | 613 | (defmacro cl-load-time-value (form &optional _read-only) |
fcd73769 RS |
614 | "Like `progn', but evaluates the body at load time. |
615 | The result of the body appears to the compiler as a quoted constant." | |
b1198e17 | 616 | (declare (debug (form &optional sexp))) |
bb3faf5b | 617 | (if (cl--compiling-file) |
7c1898a7 | 618 | (let* ((temp (cl-gentemp "--cl-load-time--")) |
03fef3e6 | 619 | (set `(set ',temp ,form))) |
fcd73769 RS |
620 | (if (and (fboundp 'byte-compile-file-form-defmumble) |
621 | (boundp 'this-kind) (boundp 'that-one)) | |
622 | (fset 'byte-compile-file-form | |
03fef3e6 SM |
623 | `(lambda (form) |
624 | (fset 'byte-compile-file-form | |
625 | ',(symbol-function 'byte-compile-file-form)) | |
626 | (byte-compile-file-form ',set) | |
627 | (byte-compile-file-form form))) | |
7200d79c | 628 | (print set (symbol-value 'byte-compile--outbuffer))) |
03fef3e6 SM |
629 | `(symbol-value ',temp)) |
630 | `',(eval form))) | |
fcd73769 RS |
631 | |
632 | ||
633 | ;;; Conditional control structures. | |
634 | ||
ebacfcc6 | 635 | ;;;###autoload |
7c1898a7 | 636 | (defmacro cl-case (expr &rest clauses) |
3187ba1c | 637 | "Eval EXPR and choose among clauses on that value. |
fcd73769 RS |
638 | Each clause looks like (KEYLIST BODY...). EXPR is evaluated and compared |
639 | against each key in each KEYLIST; the corresponding BODY is evaluated. | |
7c1898a7 | 640 | If no clause succeeds, cl-case returns nil. A single atom may be used in |
a8ea72a0 | 641 | place of a KEYLIST of one atom. A KEYLIST of t or `otherwise' is |
fcd73769 | 642 | allowed only in the final clause, and matches if no other keys match. |
3187ba1c JB |
643 | Key values are compared by `eql'. |
644 | \n(fn EXPR (KEYLIST BODY...)...)" | |
70b8ef8f | 645 | (declare (indent 1) (debug (form &rest (sexp body)))) |
4dd1c416 | 646 | (let* ((temp (if (cl--simple-expr-p expr 3) expr (make-symbol "--cl-var--"))) |
fcd73769 RS |
647 | (head-list nil) |
648 | (body (cons | |
649 | 'cond | |
650 | (mapcar | |
651 | (function | |
652 | (lambda (c) | |
653 | (cons (cond ((memq (car c) '(t otherwise)) t) | |
7c1898a7 SM |
654 | ((eq (car c) 'cl--ecase-error-flag) |
655 | `(error "cl-ecase failed: %s, %s" | |
03fef3e6 | 656 | ,temp ',(reverse head-list))) |
fcd73769 RS |
657 | ((listp (car c)) |
658 | (setq head-list (append (car c) head-list)) | |
7c1898a7 | 659 | `(cl-member ,temp ',(car c))) |
fcd73769 RS |
660 | (t |
661 | (if (memq (car c) head-list) | |
662 | (error "Duplicate key in case: %s" | |
663 | (car c))) | |
69d8fb1e | 664 | (push (car c) head-list) |
03fef3e6 | 665 | `(eql ,temp ',(car c)))) |
fcd73769 RS |
666 | (or (cdr c) '(nil))))) |
667 | clauses)))) | |
668 | (if (eq temp expr) body | |
03fef3e6 | 669 | `(let ((,temp ,expr)) ,body)))) |
fcd73769 | 670 | |
ebacfcc6 | 671 | ;;;###autoload |
7c1898a7 | 672 | (defmacro cl-ecase (expr &rest clauses) |
a464a6c7 | 673 | "Like `cl-case', but error if no case fits. |
3187ba1c JB |
674 | `otherwise'-clauses are not allowed. |
675 | \n(fn EXPR (KEYLIST BODY...)...)" | |
7c1898a7 SM |
676 | (declare (indent 1) (debug cl-case)) |
677 | `(cl-case ,expr ,@clauses (cl--ecase-error-flag))) | |
fcd73769 | 678 | |
ebacfcc6 | 679 | ;;;###autoload |
7c1898a7 | 680 | (defmacro cl-typecase (expr &rest clauses) |
3187ba1c | 681 | "Evals EXPR, chooses among clauses on that value. |
fcd73769 RS |
682 | Each clause looks like (TYPE BODY...). EXPR is evaluated and, if it |
683 | satisfies TYPE, the corresponding BODY is evaluated. If no clause succeeds, | |
7c1898a7 | 684 | cl-typecase returns nil. A TYPE of t or `otherwise' is allowed only in the |
3187ba1c JB |
685 | final clause, and matches if no other keys match. |
686 | \n(fn EXPR (TYPE BODY...)...)" | |
70b8ef8f SM |
687 | (declare (indent 1) |
688 | (debug (form &rest ([&or cl-type-spec "otherwise"] body)))) | |
4dd1c416 | 689 | (let* ((temp (if (cl--simple-expr-p expr 3) expr (make-symbol "--cl-var--"))) |
fcd73769 RS |
690 | (type-list nil) |
691 | (body (cons | |
692 | 'cond | |
693 | (mapcar | |
694 | (function | |
695 | (lambda (c) | |
696 | (cons (cond ((eq (car c) 'otherwise) t) | |
7c1898a7 SM |
697 | ((eq (car c) 'cl--ecase-error-flag) |
698 | `(error "cl-etypecase failed: %s, %s" | |
03fef3e6 | 699 | ,temp ',(reverse type-list))) |
fcd73769 | 700 | (t |
69d8fb1e | 701 | (push (car c) type-list) |
4dd1c416 | 702 | (cl--make-type-test temp (car c)))) |
fcd73769 RS |
703 | (or (cdr c) '(nil))))) |
704 | clauses)))) | |
705 | (if (eq temp expr) body | |
03fef3e6 | 706 | `(let ((,temp ,expr)) ,body)))) |
fcd73769 | 707 | |
ebacfcc6 | 708 | ;;;###autoload |
7c1898a7 SM |
709 | (defmacro cl-etypecase (expr &rest clauses) |
710 | "Like `cl-typecase', but error if no case fits. | |
3187ba1c JB |
711 | `otherwise'-clauses are not allowed. |
712 | \n(fn EXPR (TYPE BODY...)...)" | |
7c1898a7 SM |
713 | (declare (indent 1) (debug cl-typecase)) |
714 | `(cl-typecase ,expr ,@clauses (cl--ecase-error-flag))) | |
fcd73769 RS |
715 | |
716 | ||
717 | ;;; Blocks and exits. | |
718 | ||
ebacfcc6 | 719 | ;;;###autoload |
7c1898a7 | 720 | (defmacro cl-block (name &rest body) |
64a4c526 | 721 | "Define a lexically-scoped block named NAME. |
7c1898a7 | 722 | NAME may be any symbol. Code inside the BODY forms can call `cl-return-from' |
fcd73769 RS |
723 | to jump prematurely out of the block. This differs from `catch' and `throw' |
724 | in two respects: First, the NAME is an unevaluated symbol rather than a | |
725 | quoted symbol or other form; and second, NAME is lexically rather than | |
726 | dynamically scoped: Only references to it within BODY will work. These | |
727 | references may appear inside macro expansions, but not inside functions | |
728 | called from BODY." | |
70b8ef8f | 729 | (declare (indent 1) (debug (symbolp body))) |
4dd1c416 | 730 | (if (cl--safe-expr-p `(progn ,@body)) `(progn ,@body) |
bb3faf5b | 731 | `(cl--block-wrapper |
03fef3e6 SM |
732 | (catch ',(intern (format "--cl-block-%s--" name)) |
733 | ,@body)))) | |
fcd73769 | 734 | |
ebacfcc6 | 735 | ;;;###autoload |
7c1898a7 | 736 | (defmacro cl-return (&optional result) |
64a4c526 | 737 | "Return from the block named nil. |
7c1898a7 | 738 | This is equivalent to `(cl-return-from nil RESULT)'." |
b1198e17 | 739 | (declare (debug (&optional form))) |
7c1898a7 | 740 | `(cl-return-from nil ,result)) |
fcd73769 | 741 | |
ebacfcc6 | 742 | ;;;###autoload |
7c1898a7 | 743 | (defmacro cl-return-from (name &optional result) |
64a4c526 | 744 | "Return from the block named NAME. |
7c1898a7 | 745 | This jumps out to the innermost enclosing `(cl-block NAME ...)' form, |
fcd73769 RS |
746 | returning RESULT from that form (or nil if RESULT is omitted). |
747 | This is compatible with Common Lisp, but note that `defun' and | |
748 | `defmacro' do not create implicit blocks as they do in Common Lisp." | |
70b8ef8f | 749 | (declare (indent 1) (debug (symbolp &optional form))) |
fcd73769 | 750 | (let ((name2 (intern (format "--cl-block-%s--" name)))) |
bb3faf5b | 751 | `(cl--block-throw ',name2 ,result))) |
fcd73769 RS |
752 | |
753 | ||
7c1898a7 | 754 | ;;; The "cl-loop" macro. |
fcd73769 | 755 | |
4dd1c416 SM |
756 | (defvar cl--loop-args) (defvar cl--loop-accum-var) (defvar cl--loop-accum-vars) |
757 | (defvar cl--loop-bindings) (defvar cl--loop-body) (defvar cl--loop-destr-temps) | |
758 | (defvar cl--loop-finally) (defvar cl--loop-finish-flag) | |
759 | (defvar cl--loop-first-flag) | |
760 | (defvar cl--loop-initially) (defvar cl--loop-map-form) (defvar cl--loop-name) | |
761 | (defvar cl--loop-result) (defvar cl--loop-result-explicit) | |
338bfefa SM |
762 | (defvar cl--loop-result-var) (defvar cl--loop-steps) |
763 | (defvar cl--loop-symbol-macs) | |
fcd73769 | 764 | |
ebacfcc6 | 765 | ;;;###autoload |
513749ee | 766 | (defmacro cl-loop (&rest loop-args) |
662b0ee6 | 767 | "The Common Lisp `loop' macro. |
d5837773 GM |
768 | Valid clauses include: |
769 | For clauses: | |
770 | for VAR from/upfrom/downfrom EXPR1 to/upto/downto/above/below EXPR2 by EXPR3 | |
771 | for VAR = EXPR1 then EXPR2 | |
772 | for VAR in/on/in-ref LIST by FUNC | |
773 | for VAR across/across-ref ARRAY | |
774 | for VAR being: | |
775 | the elements of/of-ref SEQUENCE [using (index VAR2)] | |
776 | the symbols [of OBARRAY] | |
777 | the hash-keys/hash-values of HASH-TABLE [using (hash-values/hash-keys V2)] | |
778 | the key-codes/key-bindings/key-seqs of KEYMAP [using (key-bindings VAR2)] | |
779 | the overlays/intervals [of BUFFER] [from POS1] [to POS2] | |
780 | the frames/buffers | |
781 | the windows [of FRAME] | |
782 | Iteration clauses: | |
783 | repeat INTEGER | |
784 | while/until/always/never/thereis CONDITION | |
785 | Accumulation clauses: | |
786 | collect/append/nconc/concat/vconcat/count/sum/maximize/minimize FORM | |
787 | [into VAR] | |
788 | Miscellaneous clauses: | |
789 | with VAR = INIT | |
790 | if/when/unless COND CLAUSE [and CLAUSE]... else CLAUSE [and CLAUSE...] | |
791 | named NAME | |
792 | initially/finally [do] EXPRS... | |
793 | do EXPRS... | |
794 | [finally] return EXPR | |
795 | ||
796 | For more details, see Info node `(cl)Loop Facility'. | |
69d8fb1e SM |
797 | |
798 | \(fn CLAUSE...)" | |
d301b413 SM |
799 | (declare (debug (&rest &or |
800 | ;; These are usually followed by a symbol, but it can | |
801 | ;; actually be any destructuring-bind pattern, which | |
802 | ;; would erroneously match `form'. | |
803 | [[&or "for" "as" "with" "and"] sexp] | |
804 | ;; These are followed by expressions which could | |
805 | ;; erroneously match `symbolp'. | |
806 | [[&or "from" "upfrom" "downfrom" "to" "upto" "downto" | |
807 | "above" "below" "by" "in" "on" "=" "across" | |
808 | "repeat" "while" "until" "always" "never" | |
809 | "thereis" "collect" "append" "nconc" "sum" | |
810 | "count" "maximize" "minimize" "if" "unless" | |
811 | "return"] form] | |
812 | ;; Simple default, which covers 99% of the cases. | |
813 | symbolp form))) | |
338bfefa SM |
814 | (if (not (memq t (mapcar #'symbolp |
815 | (delq nil (delq t (cl-copy-list loop-args)))))) | |
513749ee SM |
816 | `(cl-block nil (while t ,@loop-args)) |
817 | (let ((cl--loop-args loop-args) (cl--loop-name nil) (cl--loop-bindings nil) | |
4dd1c416 SM |
818 | (cl--loop-body nil) (cl--loop-steps nil) |
819 | (cl--loop-result nil) (cl--loop-result-explicit nil) | |
820 | (cl--loop-result-var nil) (cl--loop-finish-flag nil) | |
821 | (cl--loop-accum-var nil) (cl--loop-accum-vars nil) | |
822 | (cl--loop-initially nil) (cl--loop-finally nil) | |
823 | (cl--loop-map-form nil) (cl--loop-first-flag nil) | |
824 | (cl--loop-destr-temps nil) (cl--loop-symbol-macs nil)) | |
825 | (setq cl--loop-args (append cl--loop-args '(cl-end-loop))) | |
338bfefa SM |
826 | (while (not (eq (car cl--loop-args) 'cl-end-loop)) |
827 | (cl--parse-loop-clause)) | |
4dd1c416 SM |
828 | (if cl--loop-finish-flag |
829 | (push `((,cl--loop-finish-flag t)) cl--loop-bindings)) | |
830 | (if cl--loop-first-flag | |
831 | (progn (push `((,cl--loop-first-flag t)) cl--loop-bindings) | |
832 | (push `(setq ,cl--loop-first-flag nil) cl--loop-steps))) | |
833 | (let* ((epilogue (nconc (nreverse cl--loop-finally) | |
338bfefa SM |
834 | (list (or cl--loop-result-explicit |
835 | cl--loop-result)))) | |
4dd1c416 SM |
836 | (ands (cl--loop-build-ands (nreverse cl--loop-body))) |
837 | (while-body (nconc (cadr ands) (nreverse cl--loop-steps))) | |
fcd73769 | 838 | (body (append |
4dd1c416 SM |
839 | (nreverse cl--loop-initially) |
840 | (list (if cl--loop-map-form | |
7c1898a7 SM |
841 | `(cl-block --cl-finish-- |
842 | ,(cl-subst | |
03fef3e6 SM |
843 | (if (eq (car ands) t) while-body |
844 | (cons `(or ,(car ands) | |
7c1898a7 | 845 | (cl-return-from --cl-finish-- |
03fef3e6 SM |
846 | nil)) |
847 | while-body)) | |
4dd1c416 | 848 | '--cl-map cl--loop-map-form)) |
03fef3e6 | 849 | `(while ,(car ands) ,@while-body))) |
4dd1c416 SM |
850 | (if cl--loop-finish-flag |
851 | (if (equal epilogue '(nil)) (list cl--loop-result-var) | |
852 | `((if ,cl--loop-finish-flag | |
853 | (progn ,@epilogue) ,cl--loop-result-var))) | |
fcd73769 | 854 | epilogue)))) |
338bfefa SM |
855 | (if cl--loop-result-var |
856 | (push (list cl--loop-result-var) cl--loop-bindings)) | |
4dd1c416 SM |
857 | (while cl--loop-bindings |
858 | (if (cdar cl--loop-bindings) | |
859 | (setq body (list (cl--loop-let (pop cl--loop-bindings) body t))) | |
fcd73769 | 860 | (let ((lets nil)) |
4dd1c416 SM |
861 | (while (and cl--loop-bindings |
862 | (not (cdar cl--loop-bindings))) | |
863 | (push (car (pop cl--loop-bindings)) lets)) | |
864 | (setq body (list (cl--loop-let lets body nil)))))) | |
865 | (if cl--loop-symbol-macs | |
338bfefa SM |
866 | (setq body |
867 | (list `(cl-symbol-macrolet ,cl--loop-symbol-macs ,@body)))) | |
4dd1c416 | 868 | `(cl-block ,cl--loop-name ,@body))))) |
fcd73769 | 869 | |
7c1898a7 | 870 | ;; Below is a complete spec for cl-loop, in several parts that correspond |
b1198e17 SM |
871 | ;; to the syntax given in CLtL2. The specs do more than specify where |
872 | ;; the forms are; it also specifies, as much as Edebug allows, all the | |
7c1898a7 | 873 | ;; syntactically valid cl-loop clauses. The disadvantage of this |
b1198e17 SM |
874 | ;; completeness is rigidity, but the "for ... being" clause allows |
875 | ;; arbitrary extensions of the form: [symbolp &rest &or symbolp form]. | |
876 | ||
7c1898a7 | 877 | ;; (def-edebug-spec cl-loop |
b1198e17 SM |
878 | ;; ([&optional ["named" symbolp]] |
879 | ;; [&rest | |
880 | ;; &or | |
881 | ;; ["repeat" form] | |
882 | ;; loop-for-as | |
883 | ;; loop-with | |
884 | ;; loop-initial-final] | |
885 | ;; [&rest loop-clause] | |
886 | ;; )) | |
887 | ||
888 | ;; (def-edebug-spec loop-with | |
889 | ;; ("with" loop-var | |
890 | ;; loop-type-spec | |
891 | ;; [&optional ["=" form]] | |
892 | ;; &rest ["and" loop-var | |
893 | ;; loop-type-spec | |
894 | ;; [&optional ["=" form]]])) | |
895 | ||
896 | ;; (def-edebug-spec loop-for-as | |
897 | ;; ([&or "for" "as"] loop-for-as-subclause | |
898 | ;; &rest ["and" loop-for-as-subclause])) | |
899 | ||
900 | ;; (def-edebug-spec loop-for-as-subclause | |
901 | ;; (loop-var | |
902 | ;; loop-type-spec | |
903 | ;; &or | |
904 | ;; [[&or "in" "on" "in-ref" "across-ref"] | |
905 | ;; form &optional ["by" function-form]] | |
906 | ||
907 | ;; ["=" form &optional ["then" form]] | |
908 | ;; ["across" form] | |
909 | ;; ["being" | |
910 | ;; [&or "the" "each"] | |
911 | ;; &or | |
912 | ;; [[&or "element" "elements"] | |
913 | ;; [&or "of" "in" "of-ref"] form | |
914 | ;; &optional "using" ["index" symbolp]];; is this right? | |
915 | ;; [[&or "hash-key" "hash-keys" | |
916 | ;; "hash-value" "hash-values"] | |
917 | ;; [&or "of" "in"] | |
918 | ;; hash-table-p &optional ["using" ([&or "hash-value" "hash-values" | |
919 | ;; "hash-key" "hash-keys"] sexp)]] | |
920 | ||
921 | ;; [[&or "symbol" "present-symbol" "external-symbol" | |
922 | ;; "symbols" "present-symbols" "external-symbols"] | |
923 | ;; [&or "in" "of"] package-p] | |
924 | ||
925 | ;; ;; Extensions for Emacs Lisp, including Lucid Emacs. | |
926 | ;; [[&or "frame" "frames" | |
927 | ;; "screen" "screens" | |
928 | ;; "buffer" "buffers"]] | |
929 | ||
930 | ;; [[&or "window" "windows"] | |
931 | ;; [&or "of" "in"] form] | |
932 | ||
933 | ;; [[&or "overlay" "overlays" | |
934 | ;; "extent" "extents"] | |
935 | ;; [&or "of" "in"] form | |
936 | ;; &optional [[&or "from" "to"] form]] | |
937 | ||
938 | ;; [[&or "interval" "intervals"] | |
939 | ;; [&or "in" "of"] form | |
940 | ;; &optional [[&or "from" "to"] form] | |
941 | ;; ["property" form]] | |
942 | ||
943 | ;; [[&or "key-code" "key-codes" | |
944 | ;; "key-seq" "key-seqs" | |
945 | ;; "key-binding" "key-bindings"] | |
946 | ;; [&or "in" "of"] form | |
947 | ;; &optional ["using" ([&or "key-code" "key-codes" | |
948 | ;; "key-seq" "key-seqs" | |
949 | ;; "key-binding" "key-bindings"] | |
950 | ;; sexp)]] | |
951 | ;; ;; For arbitrary extensions, recognize anything else. | |
952 | ;; [symbolp &rest &or symbolp form] | |
953 | ;; ] | |
954 | ||
955 | ;; ;; arithmetic - must be last since all parts are optional. | |
956 | ;; [[&optional [[&or "from" "downfrom" "upfrom"] form]] | |
957 | ;; [&optional [[&or "to" "downto" "upto" "below" "above"] form]] | |
958 | ;; [&optional ["by" form]] | |
959 | ;; ])) | |
960 | ||
961 | ;; (def-edebug-spec loop-initial-final | |
962 | ;; (&or ["initially" | |
963 | ;; ;; [&optional &or "do" "doing"] ;; CLtL2 doesn't allow this. | |
964 | ;; &rest loop-non-atomic-expr] | |
965 | ;; ["finally" &or | |
966 | ;; [[&optional &or "do" "doing"] &rest loop-non-atomic-expr] | |
967 | ;; ["return" form]])) | |
968 | ||
969 | ;; (def-edebug-spec loop-and-clause | |
970 | ;; (loop-clause &rest ["and" loop-clause])) | |
971 | ||
972 | ;; (def-edebug-spec loop-clause | |
973 | ;; (&or | |
974 | ;; [[&or "while" "until" "always" "never" "thereis"] form] | |
975 | ||
976 | ;; [[&or "collect" "collecting" | |
977 | ;; "append" "appending" | |
978 | ;; "nconc" "nconcing" | |
979 | ;; "concat" "vconcat"] form | |
980 | ;; [&optional ["into" loop-var]]] | |
981 | ||
982 | ;; [[&or "count" "counting" | |
983 | ;; "sum" "summing" | |
984 | ;; "maximize" "maximizing" | |
985 | ;; "minimize" "minimizing"] form | |
986 | ;; [&optional ["into" loop-var]] | |
987 | ;; loop-type-spec] | |
988 | ||
989 | ;; [[&or "if" "when" "unless"] | |
990 | ;; form loop-and-clause | |
991 | ;; [&optional ["else" loop-and-clause]] | |
992 | ;; [&optional "end"]] | |
993 | ||
994 | ;; [[&or "do" "doing"] &rest loop-non-atomic-expr] | |
995 | ||
996 | ;; ["return" form] | |
997 | ;; loop-initial-final | |
998 | ;; )) | |
999 | ||
1000 | ;; (def-edebug-spec loop-non-atomic-expr | |
1001 | ;; ([¬ atom] form)) | |
1002 | ||
1003 | ;; (def-edebug-spec loop-var | |
1004 | ;; ;; The symbolp must be last alternative to recognize e.g. (a b . c) | |
1005 | ;; ;; loop-var => | |
1006 | ;; ;; (loop-var . [&or nil loop-var]) | |
1007 | ;; ;; (symbolp . [&or nil loop-var]) | |
1008 | ;; ;; (symbolp . loop-var) | |
1009 | ;; ;; (symbolp . (symbolp . [&or nil loop-var])) | |
1010 | ;; ;; (symbolp . (symbolp . loop-var)) | |
1011 | ;; ;; (symbolp . (symbolp . symbolp)) == (symbolp symbolp . symbolp) | |
1012 | ;; (&or (loop-var . [&or nil loop-var]) [gate symbolp])) | |
1013 | ||
1014 | ;; (def-edebug-spec loop-type-spec | |
1015 | ;; (&optional ["of-type" loop-d-type-spec])) | |
1016 | ||
1017 | ;; (def-edebug-spec loop-d-type-spec | |
1018 | ;; (&or (loop-d-type-spec . [&or nil loop-d-type-spec]) cl-type-spec)) | |
1019 | ||
1020 | ||
1021 | ||
338bfefa | 1022 | (defun cl--parse-loop-clause () ; uses loop-* |
4dd1c416 | 1023 | (let ((word (pop cl--loop-args)) |
fcd73769 RS |
1024 | (hash-types '(hash-key hash-keys hash-value hash-values)) |
1025 | (key-types '(key-code key-codes key-seq key-seqs | |
1026 | key-binding key-bindings))) | |
1027 | (cond | |
1028 | ||
4dd1c416 | 1029 | ((null cl--loop-args) |
7c1898a7 | 1030 | (error "Malformed `cl-loop' macro")) |
fcd73769 RS |
1031 | |
1032 | ((eq word 'named) | |
4dd1c416 | 1033 | (setq cl--loop-name (pop cl--loop-args))) |
fcd73769 RS |
1034 | |
1035 | ((eq word 'initially) | |
4dd1c416 | 1036 | (if (memq (car cl--loop-args) '(do doing)) (pop cl--loop-args)) |
338bfefa SM |
1037 | (or (consp (car cl--loop-args)) |
1038 | (error "Syntax error on `initially' clause")) | |
4dd1c416 SM |
1039 | (while (consp (car cl--loop-args)) |
1040 | (push (pop cl--loop-args) cl--loop-initially))) | |
fcd73769 RS |
1041 | |
1042 | ((eq word 'finally) | |
4dd1c416 | 1043 | (if (eq (car cl--loop-args) 'return) |
338bfefa SM |
1044 | (setq cl--loop-result-explicit |
1045 | (or (cl--pop2 cl--loop-args) '(quote nil))) | |
4dd1c416 | 1046 | (if (memq (car cl--loop-args) '(do doing)) (pop cl--loop-args)) |
338bfefa SM |
1047 | (or (consp (car cl--loop-args)) |
1048 | (error "Syntax error on `finally' clause")) | |
4dd1c416 | 1049 | (if (and (eq (caar cl--loop-args) 'return) (null cl--loop-name)) |
338bfefa SM |
1050 | (setq cl--loop-result-explicit |
1051 | (or (nth 1 (pop cl--loop-args)) '(quote nil))) | |
4dd1c416 SM |
1052 | (while (consp (car cl--loop-args)) |
1053 | (push (pop cl--loop-args) cl--loop-finally))))) | |
fcd73769 RS |
1054 | |
1055 | ((memq word '(for as)) | |
1056 | (let ((loop-for-bindings nil) (loop-for-sets nil) (loop-for-steps nil) | |
1057 | (ands nil)) | |
1058 | (while | |
7c1898a7 | 1059 | ;; Use `cl-gensym' rather than `make-symbol'. It's important that |
443b961a | 1060 | ;; (not (eq (symbol-name var1) (symbol-name var2))) because |
4dd1c416 SM |
1061 | ;; these vars get added to the macro-environment. |
1062 | (let ((var (or (pop cl--loop-args) (cl-gensym "--cl-var--")))) | |
1063 | (setq word (pop cl--loop-args)) | |
1064 | (if (eq word 'being) (setq word (pop cl--loop-args))) | |
1065 | (if (memq word '(the each)) (setq word (pop cl--loop-args))) | |
fcd73769 | 1066 | (if (memq word '(buffer buffers)) |
338bfefa SM |
1067 | (setq word 'in |
1068 | cl--loop-args (cons '(buffer-list) cl--loop-args))) | |
fcd73769 RS |
1069 | (cond |
1070 | ||
1071 | ((memq word '(from downfrom upfrom to downto upto | |
1072 | above below by)) | |
4dd1c416 SM |
1073 | (push word cl--loop-args) |
1074 | (if (memq (car cl--loop-args) '(downto above)) | |
7c1898a7 | 1075 | (error "Must specify `from' value for downward cl-loop")) |
4dd1c416 | 1076 | (let* ((down (or (eq (car cl--loop-args) 'downfrom) |
338bfefa SM |
1077 | (memq (cl-caddr cl--loop-args) |
1078 | '(downto above)))) | |
4dd1c416 | 1079 | (excl (or (memq (car cl--loop-args) '(above below)) |
338bfefa SM |
1080 | (memq (cl-caddr cl--loop-args) |
1081 | '(above below)))) | |
1082 | (start (and (memq (car cl--loop-args) | |
1083 | '(from upfrom downfrom)) | |
1084 | (cl--pop2 cl--loop-args))) | |
4dd1c416 | 1085 | (end (and (memq (car cl--loop-args) |
fcd73769 | 1086 | '(to upto downto above below)) |
338bfefa SM |
1087 | (cl--pop2 cl--loop-args))) |
1088 | (step (and (eq (car cl--loop-args) 'by) | |
1089 | (cl--pop2 cl--loop-args))) | |
4dd1c416 | 1090 | (end-var (and (not (macroexp-const-p end)) |
e542ea4b | 1091 | (make-symbol "--cl-var--"))) |
4dd1c416 | 1092 | (step-var (and (not (macroexp-const-p step)) |
e542ea4b | 1093 | (make-symbol "--cl-var--")))) |
fcd73769 RS |
1094 | (and step (numberp step) (<= step 0) |
1095 | (error "Loop `by' value is not positive: %s" step)) | |
69d8fb1e SM |
1096 | (push (list var (or start 0)) loop-for-bindings) |
1097 | (if end-var (push (list end-var end) loop-for-bindings)) | |
1098 | (if step-var (push (list step-var step) | |
e542ea4b | 1099 | loop-for-bindings)) |
fcd73769 | 1100 | (if end |
69d8fb1e | 1101 | (push (list |
e542ea4b | 1102 | (if down (if excl '> '>=) (if excl '< '<=)) |
4dd1c416 | 1103 | var (or end-var end)) cl--loop-body)) |
69d8fb1e | 1104 | (push (list var (list (if down '- '+) var |
e542ea4b SM |
1105 | (or step-var step 1))) |
1106 | loop-for-steps))) | |
fcd73769 RS |
1107 | |
1108 | ((memq word '(in in-ref on)) | |
1109 | (let* ((on (eq word 'on)) | |
e542ea4b SM |
1110 | (temp (if (and on (symbolp var)) |
1111 | var (make-symbol "--cl-var--")))) | |
4dd1c416 SM |
1112 | (push (list temp (pop cl--loop-args)) loop-for-bindings) |
1113 | (push `(consp ,temp) cl--loop-body) | |
fcd73769 | 1114 | (if (eq word 'in-ref) |
4dd1c416 | 1115 | (push (list var `(car ,temp)) cl--loop-symbol-macs) |
fcd73769 RS |
1116 | (or (eq temp var) |
1117 | (progn | |
69d8fb1e | 1118 | (push (list var nil) loop-for-bindings) |
03fef3e6 | 1119 | (push (list var (if on temp `(car ,temp))) |
e542ea4b | 1120 | loop-for-sets)))) |
69d8fb1e | 1121 | (push (list temp |
4dd1c416 | 1122 | (if (eq (car cl--loop-args) 'by) |
338bfefa | 1123 | (let ((step (cl--pop2 cl--loop-args))) |
e542ea4b SM |
1124 | (if (and (memq (car-safe step) |
1125 | '(quote function | |
7c1898a7 | 1126 | cl-function)) |
e542ea4b SM |
1127 | (symbolp (nth 1 step))) |
1128 | (list (nth 1 step) temp) | |
03fef3e6 SM |
1129 | `(funcall ,step ,temp))) |
1130 | `(cdr ,temp))) | |
e542ea4b | 1131 | loop-for-steps))) |
fcd73769 RS |
1132 | |
1133 | ((eq word '=) | |
4dd1c416 | 1134 | (let* ((start (pop cl--loop-args)) |
338bfefa SM |
1135 | (then (if (eq (car cl--loop-args) 'then) |
1136 | (cl--pop2 cl--loop-args) start))) | |
69d8fb1e | 1137 | (push (list var nil) loop-for-bindings) |
4dd1c416 | 1138 | (if (or ands (eq (car cl--loop-args) 'and)) |
fcd73769 | 1139 | (progn |
e542ea4b | 1140 | (push `(,var |
4dd1c416 SM |
1141 | (if ,(or cl--loop-first-flag |
1142 | (setq cl--loop-first-flag | |
e542ea4b SM |
1143 | (make-symbol "--cl-var--"))) |
1144 | ,start ,var)) | |
1145 | loop-for-sets) | |
69d8fb1e SM |
1146 | (push (list var then) loop-for-steps)) |
1147 | (push (list var | |
e542ea4b | 1148 | (if (eq start then) start |
4dd1c416 SM |
1149 | `(if ,(or cl--loop-first-flag |
1150 | (setq cl--loop-first-flag | |
e542ea4b SM |
1151 | (make-symbol "--cl-var--"))) |
1152 | ,start ,then))) | |
1153 | loop-for-sets)))) | |
fcd73769 RS |
1154 | |
1155 | ((memq word '(across across-ref)) | |
e542ea4b SM |
1156 | (let ((temp-vec (make-symbol "--cl-vec--")) |
1157 | (temp-idx (make-symbol "--cl-idx--"))) | |
4dd1c416 | 1158 | (push (list temp-vec (pop cl--loop-args)) loop-for-bindings) |
69d8fb1e | 1159 | (push (list temp-idx -1) loop-for-bindings) |
03fef3e6 | 1160 | (push `(< (setq ,temp-idx (1+ ,temp-idx)) |
4dd1c416 | 1161 | (length ,temp-vec)) cl--loop-body) |
fcd73769 | 1162 | (if (eq word 'across-ref) |
03fef3e6 | 1163 | (push (list var `(aref ,temp-vec ,temp-idx)) |
4dd1c416 | 1164 | cl--loop-symbol-macs) |
69d8fb1e | 1165 | (push (list var nil) loop-for-bindings) |
03fef3e6 | 1166 | (push (list var `(aref ,temp-vec ,temp-idx)) |
e542ea4b | 1167 | loop-for-sets)))) |
fcd73769 RS |
1168 | |
1169 | ((memq word '(element elements)) | |
4dd1c416 SM |
1170 | (let ((ref (or (memq (car cl--loop-args) '(in-ref of-ref)) |
1171 | (and (not (memq (car cl--loop-args) '(in of))) | |
fcd73769 | 1172 | (error "Expected `of'")))) |
338bfefa | 1173 | (seq (cl--pop2 cl--loop-args)) |
e542ea4b | 1174 | (temp-seq (make-symbol "--cl-seq--")) |
338bfefa SM |
1175 | (temp-idx |
1176 | (if (eq (car cl--loop-args) 'using) | |
1177 | (if (and (= (length (cadr cl--loop-args)) 2) | |
1178 | (eq (cl-caadr cl--loop-args) 'index)) | |
1179 | (cadr (cl--pop2 cl--loop-args)) | |
1180 | (error "Bad `using' clause")) | |
1181 | (make-symbol "--cl-idx--")))) | |
69d8fb1e SM |
1182 | (push (list temp-seq seq) loop-for-bindings) |
1183 | (push (list temp-idx 0) loop-for-bindings) | |
fcd73769 | 1184 | (if ref |
e542ea4b | 1185 | (let ((temp-len (make-symbol "--cl-len--"))) |
03fef3e6 | 1186 | (push (list temp-len `(length ,temp-seq)) |
e542ea4b | 1187 | loop-for-bindings) |
2eb87922 | 1188 | (push (list var `(elt ,temp-seq ,temp-idx)) |
4dd1c416 SM |
1189 | cl--loop-symbol-macs) |
1190 | (push `(< ,temp-idx ,temp-len) cl--loop-body)) | |
69d8fb1e | 1191 | (push (list var nil) loop-for-bindings) |
03fef3e6 SM |
1192 | (push `(and ,temp-seq |
1193 | (or (consp ,temp-seq) | |
1194 | (< ,temp-idx (length ,temp-seq)))) | |
4dd1c416 | 1195 | cl--loop-body) |
03fef3e6 SM |
1196 | (push (list var `(if (consp ,temp-seq) |
1197 | (pop ,temp-seq) | |
1198 | (aref ,temp-seq ,temp-idx))) | |
e542ea4b | 1199 | loop-for-sets)) |
03fef3e6 | 1200 | (push (list temp-idx `(1+ ,temp-idx)) |
e542ea4b | 1201 | loop-for-steps))) |
fcd73769 RS |
1202 | |
1203 | ((memq word hash-types) | |
338bfefa SM |
1204 | (or (memq (car cl--loop-args) '(in of)) |
1205 | (error "Expected `of'")) | |
1206 | (let* ((table (cl--pop2 cl--loop-args)) | |
1207 | (other | |
1208 | (if (eq (car cl--loop-args) 'using) | |
1209 | (if (and (= (length (cadr cl--loop-args)) 2) | |
1210 | (memq (cl-caadr cl--loop-args) hash-types) | |
1211 | (not (eq (cl-caadr cl--loop-args) word))) | |
1212 | (cadr (cl--pop2 cl--loop-args)) | |
1213 | (error "Bad `using' clause")) | |
1214 | (make-symbol "--cl-var--")))) | |
fcd73769 RS |
1215 | (if (memq word '(hash-value hash-values)) |
1216 | (setq var (prog1 other (setq other var)))) | |
4dd1c416 | 1217 | (setq cl--loop-map-form |
e542ea4b | 1218 | `(maphash (lambda (,var ,other) . --cl-map) ,table)))) |
fcd73769 RS |
1219 | |
1220 | ((memq word '(symbol present-symbol external-symbol | |
1221 | symbols present-symbols external-symbols)) | |
338bfefa SM |
1222 | (let ((ob (and (memq (car cl--loop-args) '(in of)) |
1223 | (cl--pop2 cl--loop-args)))) | |
4dd1c416 | 1224 | (setq cl--loop-map-form |
e542ea4b | 1225 | `(mapatoms (lambda (,var) . --cl-map) ,ob)))) |
fcd73769 RS |
1226 | |
1227 | ((memq word '(overlay overlays extent extents)) | |
1228 | (let ((buf nil) (from nil) (to nil)) | |
4dd1c416 | 1229 | (while (memq (car cl--loop-args) '(in of from to)) |
338bfefa SM |
1230 | (cond ((eq (car cl--loop-args) 'from) |
1231 | (setq from (cl--pop2 cl--loop-args))) | |
1232 | ((eq (car cl--loop-args) 'to) | |
1233 | (setq to (cl--pop2 cl--loop-args))) | |
1234 | (t (setq buf (cl--pop2 cl--loop-args))))) | |
4dd1c416 | 1235 | (setq cl--loop-map-form |
bb3faf5b | 1236 | `(cl--map-overlays |
e542ea4b SM |
1237 | (lambda (,var ,(make-symbol "--cl-var--")) |
1238 | (progn . --cl-map) nil) | |
1239 | ,buf ,from ,to)))) | |
fcd73769 RS |
1240 | |
1241 | ((memq word '(interval intervals)) | |
1242 | (let ((buf nil) (prop nil) (from nil) (to nil) | |
e542ea4b SM |
1243 | (var1 (make-symbol "--cl-var1--")) |
1244 | (var2 (make-symbol "--cl-var2--"))) | |
4dd1c416 | 1245 | (while (memq (car cl--loop-args) '(in of property from to)) |
338bfefa SM |
1246 | (cond ((eq (car cl--loop-args) 'from) |
1247 | (setq from (cl--pop2 cl--loop-args))) | |
1248 | ((eq (car cl--loop-args) 'to) | |
1249 | (setq to (cl--pop2 cl--loop-args))) | |
4dd1c416 | 1250 | ((eq (car cl--loop-args) 'property) |
338bfefa SM |
1251 | (setq prop (cl--pop2 cl--loop-args))) |
1252 | (t (setq buf (cl--pop2 cl--loop-args))))) | |
fcd73769 RS |
1253 | (if (and (consp var) (symbolp (car var)) (symbolp (cdr var))) |
1254 | (setq var1 (car var) var2 (cdr var)) | |
03fef3e6 | 1255 | (push (list var `(cons ,var1 ,var2)) loop-for-sets)) |
4dd1c416 | 1256 | (setq cl--loop-map-form |
bb3faf5b | 1257 | `(cl--map-intervals |
e542ea4b SM |
1258 | (lambda (,var1 ,var2) . --cl-map) |
1259 | ,buf ,prop ,from ,to)))) | |
fcd73769 RS |
1260 | |
1261 | ((memq word key-types) | |
338bfefa SM |
1262 | (or (memq (car cl--loop-args) '(in of)) |
1263 | (error "Expected `of'")) | |
1264 | (let ((cl-map (cl--pop2 cl--loop-args)) | |
1265 | (other | |
1266 | (if (eq (car cl--loop-args) 'using) | |
1267 | (if (and (= (length (cadr cl--loop-args)) 2) | |
1268 | (memq (cl-caadr cl--loop-args) key-types) | |
1269 | (not (eq (cl-caadr cl--loop-args) word))) | |
1270 | (cadr (cl--pop2 cl--loop-args)) | |
1271 | (error "Bad `using' clause")) | |
1272 | (make-symbol "--cl-var--")))) | |
fcd73769 RS |
1273 | (if (memq word '(key-binding key-bindings)) |
1274 | (setq var (prog1 other (setq other var)))) | |
4dd1c416 | 1275 | (setq cl--loop-map-form |
e542ea4b | 1276 | `(,(if (memq word '(key-seq key-seqs)) |
bb3faf5b | 1277 | 'cl--map-keymap-recursively 'map-keymap) |
7c1898a7 | 1278 | (lambda (,var ,other) . --cl-map) ,cl-map)))) |
fcd73769 RS |
1279 | |
1280 | ((memq word '(frame frames screen screens)) | |
e542ea4b | 1281 | (let ((temp (make-symbol "--cl-var--"))) |
69d8fb1e | 1282 | (push (list var '(selected-frame)) |
e542ea4b | 1283 | loop-for-bindings) |
69d8fb1e | 1284 | (push (list temp nil) loop-for-bindings) |
03fef3e6 SM |
1285 | (push `(prog1 (not (eq ,var ,temp)) |
1286 | (or ,temp (setq ,temp ,var))) | |
4dd1c416 | 1287 | cl--loop-body) |
03fef3e6 | 1288 | (push (list var `(next-frame ,var)) |
e542ea4b | 1289 | loop-for-steps))) |
fcd73769 RS |
1290 | |
1291 | ((memq word '(window windows)) | |
338bfefa SM |
1292 | (let ((scr (and (memq (car cl--loop-args) '(in of)) |
1293 | (cl--pop2 cl--loop-args))) | |
05907bb3 GM |
1294 | (temp (make-symbol "--cl-var--")) |
1295 | (minip (make-symbol "--cl-minip--"))) | |
69d8fb1e | 1296 | (push (list var (if scr |
03fef3e6 | 1297 | `(frame-selected-window ,scr) |
e542ea4b SM |
1298 | '(selected-window))) |
1299 | loop-for-bindings) | |
05907bb3 GM |
1300 | ;; If we started in the minibuffer, we need to |
1301 | ;; ensure that next-window will bring us back there | |
1302 | ;; at some point. (Bug#7492). | |
7c1898a7 | 1303 | ;; (Consider using walk-windows instead of cl-loop if |
05907bb3 GM |
1304 | ;; you care about such things.) |
1305 | (push (list minip `(minibufferp (window-buffer ,var))) | |
1306 | loop-for-bindings) | |
69d8fb1e | 1307 | (push (list temp nil) loop-for-bindings) |
03fef3e6 SM |
1308 | (push `(prog1 (not (eq ,var ,temp)) |
1309 | (or ,temp (setq ,temp ,var))) | |
4dd1c416 | 1310 | cl--loop-body) |
03fef3e6 | 1311 | (push (list var `(next-window ,var ,minip)) |
05907bb3 | 1312 | loop-for-steps))) |
fcd73769 RS |
1313 | |
1314 | (t | |
49138400 | 1315 | ;; This is an advertised interface: (info "(cl)Other Clauses"). |
fcd73769 | 1316 | (let ((handler (and (symbolp word) |
49138400 | 1317 | (get word 'cl-loop-for-handler)))) |
fcd73769 RS |
1318 | (if handler |
1319 | (funcall handler var) | |
1320 | (error "Expected a `for' preposition, found %s" word))))) | |
4dd1c416 | 1321 | (eq (car cl--loop-args) 'and)) |
fcd73769 | 1322 | (setq ands t) |
4dd1c416 | 1323 | (pop cl--loop-args)) |
fcd73769 | 1324 | (if (and ands loop-for-bindings) |
4dd1c416 SM |
1325 | (push (nreverse loop-for-bindings) cl--loop-bindings) |
1326 | (setq cl--loop-bindings (nconc (mapcar 'list loop-for-bindings) | |
1327 | cl--loop-bindings))) | |
fcd73769 | 1328 | (if loop-for-sets |
03fef3e6 | 1329 | (push `(progn |
4dd1c416 SM |
1330 | ,(cl--loop-let (nreverse loop-for-sets) 'setq ands) |
1331 | t) cl--loop-body)) | |
fcd73769 | 1332 | (if loop-for-steps |
7c1898a7 | 1333 | (push (cons (if ands 'cl-psetq 'setq) |
e542ea4b | 1334 | (apply 'append (nreverse loop-for-steps))) |
4dd1c416 | 1335 | cl--loop-steps)))) |
fcd73769 RS |
1336 | |
1337 | ((eq word 'repeat) | |
e542ea4b | 1338 | (let ((temp (make-symbol "--cl-var--"))) |
4dd1c416 SM |
1339 | (push (list (list temp (pop cl--loop-args))) cl--loop-bindings) |
1340 | (push `(>= (setq ,temp (1- ,temp)) 0) cl--loop-body))) | |
fcd73769 | 1341 | |
ae1aa776 | 1342 | ((memq word '(collect collecting)) |
4dd1c416 SM |
1343 | (let ((what (pop cl--loop-args)) |
1344 | (var (cl--loop-handle-accum nil 'nreverse))) | |
1345 | (if (eq var cl--loop-accum-var) | |
1346 | (push `(progn (push ,what ,var) t) cl--loop-body) | |
03fef3e6 SM |
1347 | (push `(progn |
1348 | (setq ,var (nconc ,var (list ,what))) | |
4dd1c416 | 1349 | t) cl--loop-body)))) |
fcd73769 RS |
1350 | |
1351 | ((memq word '(nconc nconcing append appending)) | |
4dd1c416 SM |
1352 | (let ((what (pop cl--loop-args)) |
1353 | (var (cl--loop-handle-accum nil 'nreverse))) | |
03fef3e6 SM |
1354 | (push `(progn |
1355 | (setq ,var | |
4dd1c416 | 1356 | ,(if (eq var cl--loop-accum-var) |
03fef3e6 SM |
1357 | `(nconc |
1358 | (,(if (memq word '(nconc nconcing)) | |
1359 | #'nreverse #'reverse) | |
1360 | ,what) | |
1361 | ,var) | |
1362 | `(,(if (memq word '(nconc nconcing)) | |
1363 | #'nconc #'append) | |
4dd1c416 | 1364 | ,var ,what))) t) cl--loop-body))) |
fcd73769 RS |
1365 | |
1366 | ((memq word '(concat concating)) | |
4dd1c416 SM |
1367 | (let ((what (pop cl--loop-args)) |
1368 | (var (cl--loop-handle-accum ""))) | |
1369 | (push `(progn (cl-callf concat ,var ,what) t) cl--loop-body))) | |
fcd73769 RS |
1370 | |
1371 | ((memq word '(vconcat vconcating)) | |
4dd1c416 SM |
1372 | (let ((what (pop cl--loop-args)) |
1373 | (var (cl--loop-handle-accum []))) | |
1374 | (push `(progn (cl-callf vconcat ,var ,what) t) cl--loop-body))) | |
fcd73769 RS |
1375 | |
1376 | ((memq word '(sum summing)) | |
4dd1c416 SM |
1377 | (let ((what (pop cl--loop-args)) |
1378 | (var (cl--loop-handle-accum 0))) | |
1379 | (push `(progn (cl-incf ,var ,what) t) cl--loop-body))) | |
fcd73769 RS |
1380 | |
1381 | ((memq word '(count counting)) | |
4dd1c416 SM |
1382 | (let ((what (pop cl--loop-args)) |
1383 | (var (cl--loop-handle-accum 0))) | |
1384 | (push `(progn (if ,what (cl-incf ,var)) t) cl--loop-body))) | |
fcd73769 RS |
1385 | |
1386 | ((memq word '(minimize minimizing maximize maximizing)) | |
4dd1c416 | 1387 | (let* ((what (pop cl--loop-args)) |
338bfefa SM |
1388 | (temp (if (cl--simple-expr-p what) what |
1389 | (make-symbol "--cl-var--"))) | |
4dd1c416 | 1390 | (var (cl--loop-handle-accum nil)) |
fcd73769 | 1391 | (func (intern (substring (symbol-name word) 0 3))) |
03fef3e6 SM |
1392 | (set `(setq ,var (if ,var (,func ,var ,temp) ,temp)))) |
1393 | (push `(progn ,(if (eq temp what) set | |
1394 | `(let ((,temp ,what)) ,set)) | |
4dd1c416 | 1395 | t) cl--loop-body))) |
fcd73769 RS |
1396 | |
1397 | ((eq word 'with) | |
1398 | (let ((bindings nil)) | |
4dd1c416 | 1399 | (while (progn (push (list (pop cl--loop-args) |
338bfefa SM |
1400 | (and (eq (car cl--loop-args) '=) |
1401 | (cl--pop2 cl--loop-args))) | |
e542ea4b | 1402 | bindings) |
4dd1c416 SM |
1403 | (eq (car cl--loop-args) 'and)) |
1404 | (pop cl--loop-args)) | |
1405 | (push (nreverse bindings) cl--loop-bindings))) | |
fcd73769 RS |
1406 | |
1407 | ((eq word 'while) | |
4dd1c416 | 1408 | (push (pop cl--loop-args) cl--loop-body)) |
fcd73769 RS |
1409 | |
1410 | ((eq word 'until) | |
4dd1c416 | 1411 | (push `(not ,(pop cl--loop-args)) cl--loop-body)) |
fcd73769 RS |
1412 | |
1413 | ((eq word 'always) | |
338bfefa SM |
1414 | (or cl--loop-finish-flag |
1415 | (setq cl--loop-finish-flag (make-symbol "--cl-flag--"))) | |
4dd1c416 SM |
1416 | (push `(setq ,cl--loop-finish-flag ,(pop cl--loop-args)) cl--loop-body) |
1417 | (setq cl--loop-result t)) | |
fcd73769 RS |
1418 | |
1419 | ((eq word 'never) | |
338bfefa SM |
1420 | (or cl--loop-finish-flag |
1421 | (setq cl--loop-finish-flag (make-symbol "--cl-flag--"))) | |
4dd1c416 SM |
1422 | (push `(setq ,cl--loop-finish-flag (not ,(pop cl--loop-args))) |
1423 | cl--loop-body) | |
1424 | (setq cl--loop-result t)) | |
fcd73769 RS |
1425 | |
1426 | ((eq word 'thereis) | |
338bfefa SM |
1427 | (or cl--loop-finish-flag |
1428 | (setq cl--loop-finish-flag (make-symbol "--cl-flag--"))) | |
1429 | (or cl--loop-result-var | |
1430 | (setq cl--loop-result-var (make-symbol "--cl-var--"))) | |
4dd1c416 SM |
1431 | (push `(setq ,cl--loop-finish-flag |
1432 | (not (setq ,cl--loop-result-var ,(pop cl--loop-args)))) | |
1433 | cl--loop-body)) | |
fcd73769 RS |
1434 | |
1435 | ((memq word '(if when unless)) | |
4dd1c416 SM |
1436 | (let* ((cond (pop cl--loop-args)) |
1437 | (then (let ((cl--loop-body nil)) | |
338bfefa | 1438 | (cl--parse-loop-clause) |
4dd1c416 SM |
1439 | (cl--loop-build-ands (nreverse cl--loop-body)))) |
1440 | (else (let ((cl--loop-body nil)) | |
1441 | (if (eq (car cl--loop-args) 'else) | |
338bfefa | 1442 | (progn (pop cl--loop-args) (cl--parse-loop-clause))) |
4dd1c416 | 1443 | (cl--loop-build-ands (nreverse cl--loop-body)))) |
fcd73769 | 1444 | (simple (and (eq (car then) t) (eq (car else) t)))) |
4dd1c416 | 1445 | (if (eq (car cl--loop-args) 'end) (pop cl--loop-args)) |
fcd73769 RS |
1446 | (if (eq word 'unless) (setq then (prog1 else (setq else then)))) |
1447 | (let ((form (cons (if simple (cons 'progn (nth 1 then)) (nth 2 then)) | |
1448 | (if simple (nth 1 else) (list (nth 2 else)))))) | |
4dd1c416 | 1449 | (if (cl--expr-contains form 'it) |
e542ea4b | 1450 | (let ((temp (make-symbol "--cl-var--"))) |
4dd1c416 | 1451 | (push (list temp) cl--loop-bindings) |
03fef3e6 | 1452 | (setq form `(if (setq ,temp ,cond) |
7c1898a7 | 1453 | ,@(cl-subst temp 'it form)))) |
03fef3e6 | 1454 | (setq form `(if ,cond ,@form))) |
4dd1c416 | 1455 | (push (if simple `(progn ,form t) form) cl--loop-body)))) |
fcd73769 RS |
1456 | |
1457 | ((memq word '(do doing)) | |
1458 | (let ((body nil)) | |
4dd1c416 SM |
1459 | (or (consp (car cl--loop-args)) (error "Syntax error on `do' clause")) |
1460 | (while (consp (car cl--loop-args)) (push (pop cl--loop-args) body)) | |
1461 | (push (cons 'progn (nreverse (cons t body))) cl--loop-body))) | |
fcd73769 RS |
1462 | |
1463 | ((eq word 'return) | |
338bfefa SM |
1464 | (or cl--loop-finish-flag |
1465 | (setq cl--loop-finish-flag (make-symbol "--cl-var--"))) | |
1466 | (or cl--loop-result-var | |
1467 | (setq cl--loop-result-var (make-symbol "--cl-var--"))) | |
4dd1c416 SM |
1468 | (push `(setq ,cl--loop-result-var ,(pop cl--loop-args) |
1469 | ,cl--loop-finish-flag nil) cl--loop-body)) | |
fcd73769 RS |
1470 | |
1471 | (t | |
49138400 GM |
1472 | ;; This is an advertised interface: (info "(cl)Other Clauses"). |
1473 | (let ((handler (and (symbolp word) (get word 'cl-loop-handler)))) | |
7c1898a7 | 1474 | (or handler (error "Expected a cl-loop keyword, found %s" word)) |
fcd73769 | 1475 | (funcall handler)))) |
4dd1c416 | 1476 | (if (eq (car cl--loop-args) 'and) |
338bfefa | 1477 | (progn (pop cl--loop-args) (cl--parse-loop-clause))))) |
fcd73769 | 1478 | |
4dd1c416 | 1479 | (defun cl--loop-let (specs body par) ; uses loop-* |
fcd73769 | 1480 | (let ((p specs) (temps nil) (new nil)) |
7c1898a7 | 1481 | (while (and p (or (symbolp (car-safe (car p))) (null (cl-cadar p)))) |
fcd73769 RS |
1482 | (setq p (cdr p))) |
1483 | (and par p | |
1484 | (progn | |
1485 | (setq par nil p specs) | |
1486 | (while p | |
4dd1c416 | 1487 | (or (macroexp-const-p (cl-cadar p)) |
e542ea4b | 1488 | (let ((temp (make-symbol "--cl-var--"))) |
7c1898a7 | 1489 | (push (list temp (cl-cadar p)) temps) |
fcd73769 RS |
1490 | (setcar (cdar p) temp))) |
1491 | (setq p (cdr p))))) | |
1492 | (while specs | |
1493 | (if (and (consp (car specs)) (listp (caar specs))) | |
1494 | (let* ((spec (caar specs)) (nspecs nil) | |
69d8fb1e | 1495 | (expr (cadr (pop specs))) |
338bfefa SM |
1496 | (temp |
1497 | (cdr (or (assq spec cl--loop-destr-temps) | |
1498 | (car (push (cons spec | |
1499 | (or (last spec 0) | |
1500 | (make-symbol "--cl-var--"))) | |
1501 | cl--loop-destr-temps)))))) | |
69d8fb1e | 1502 | (push (list temp expr) new) |
fcd73769 | 1503 | (while (consp spec) |
69d8fb1e | 1504 | (push (list (pop spec) |
fcd73769 RS |
1505 | (and expr (list (if spec 'pop 'car) temp))) |
1506 | nspecs)) | |
1507 | (setq specs (nconc (nreverse nspecs) specs))) | |
69d8fb1e | 1508 | (push (pop specs) new))) |
fcd73769 | 1509 | (if (eq body 'setq) |
338bfefa SM |
1510 | (let ((set (cons (if par 'cl-psetq 'setq) |
1511 | (apply 'nconc (nreverse new))))) | |
03fef3e6 SM |
1512 | (if temps `(let* ,(nreverse temps) ,set) set)) |
1513 | `(,(if par 'let 'let*) | |
1514 | ,(nconc (nreverse temps) (nreverse new)) ,@body)))) | |
fcd73769 | 1515 | |
338bfefa | 1516 | (defun cl--loop-handle-accum (def &optional func) ; uses loop-* |
4dd1c416 | 1517 | (if (eq (car cl--loop-args) 'into) |
338bfefa | 1518 | (let ((var (cl--pop2 cl--loop-args))) |
4dd1c416 SM |
1519 | (or (memq var cl--loop-accum-vars) |
1520 | (progn (push (list (list var def)) cl--loop-bindings) | |
1521 | (push var cl--loop-accum-vars))) | |
fcd73769 | 1522 | var) |
4dd1c416 | 1523 | (or cl--loop-accum-var |
fcd73769 | 1524 | (progn |
338bfefa SM |
1525 | (push (list (list |
1526 | (setq cl--loop-accum-var (make-symbol "--cl-var--")) | |
1527 | def)) | |
1528 | cl--loop-bindings) | |
4dd1c416 | 1529 | (setq cl--loop-result (if func (list func cl--loop-accum-var) |
338bfefa | 1530 | cl--loop-accum-var)) |
4dd1c416 | 1531 | cl--loop-accum-var)))) |
fcd73769 | 1532 | |
4dd1c416 | 1533 | (defun cl--loop-build-ands (clauses) |
9b851e25 SM |
1534 | "Return various representations of (and . CLAUSES). |
1535 | CLAUSES is a list of Elisp expressions, where clauses of the form | |
1536 | \(progn E1 E2 E3 .. t) are the focus of particular optimizations. | |
1537 | The return value has shape (COND BODY COMBO) | |
1538 | such that COMBO is equivalent to (and . CLAUSES)." | |
fcd73769 RS |
1539 | (let ((ands nil) |
1540 | (body nil)) | |
9b851e25 SM |
1541 | ;; Look through `clauses', trying to optimize (progn ,@A t) (progn ,@B) ,@C |
1542 | ;; into (progn ,@A ,@B) ,@C. | |
fcd73769 RS |
1543 | (while clauses |
1544 | (if (and (eq (car-safe (car clauses)) 'progn) | |
1545 | (eq (car (last (car clauses))) t)) | |
1546 | (if (cdr clauses) | |
1547 | (setq clauses (cons (nconc (butlast (car clauses)) | |
1548 | (if (eq (car-safe (cadr clauses)) | |
1549 | 'progn) | |
7c1898a7 | 1550 | (cl-cdadr clauses) |
fcd73769 RS |
1551 | (list (cadr clauses)))) |
1552 | (cddr clauses))) | |
9b851e25 | 1553 | ;; A final (progn ,@A t) is moved outside of the `and'. |
69d8fb1e SM |
1554 | (setq body (cdr (butlast (pop clauses))))) |
1555 | (push (pop clauses) ands))) | |
fcd73769 RS |
1556 | (setq ands (or (nreverse ands) (list t))) |
1557 | (list (if (cdr ands) (cons 'and ands) (car ands)) | |
1558 | body | |
1559 | (let ((full (if body | |
1560 | (append ands (list (cons 'progn (append body '(t))))) | |
1561 | ands))) | |
1562 | (if (cdr full) (cons 'and full) (car full)))))) | |
1563 | ||
1564 | ||
1565 | ;;; Other iteration control structures. | |
1566 | ||
ebacfcc6 | 1567 | ;;;###autoload |
7c1898a7 | 1568 | (defmacro cl-do (steps endtest &rest body) |
662b0ee6 | 1569 | "The Common Lisp `do' loop. |
a766dfa1 JB |
1570 | |
1571 | \(fn ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)" | |
70b8ef8f SM |
1572 | (declare (indent 2) |
1573 | (debug | |
b1198e17 SM |
1574 | ((&rest &or symbolp (symbolp &optional form form)) |
1575 | (form body) | |
1576 | cl-declarations body))) | |
338bfefa | 1577 | (cl--expand-do-loop steps endtest body nil)) |
fcd73769 | 1578 | |
ebacfcc6 | 1579 | ;;;###autoload |
7c1898a7 | 1580 | (defmacro cl-do* (steps endtest &rest body) |
662b0ee6 | 1581 | "The Common Lisp `do*' loop. |
a766dfa1 JB |
1582 | |
1583 | \(fn ((VAR INIT [STEP])...) (END-TEST [RESULT...]) BODY...)" | |
7c1898a7 | 1584 | (declare (indent 2) (debug cl-do)) |
338bfefa | 1585 | (cl--expand-do-loop steps endtest body t)) |
fcd73769 | 1586 | |
338bfefa | 1587 | (defun cl--expand-do-loop (steps endtest body star) |
7c1898a7 | 1588 | `(cl-block nil |
03fef3e6 SM |
1589 | (,(if star 'let* 'let) |
1590 | ,(mapcar (lambda (c) (if (consp c) (list (car c) (nth 1 c)) c)) | |
1591 | steps) | |
1592 | (while (not ,(car endtest)) | |
1593 | ,@body | |
1594 | ,@(let ((sets (mapcar (lambda (c) | |
1595 | (and (consp c) (cdr (cdr c)) | |
1596 | (list (car c) (nth 2 c)))) | |
1597 | steps))) | |
1598 | (setq sets (delq nil sets)) | |
1599 | (and sets | |
1600 | (list (cons (if (or star (not (cdr sets))) | |
7c1898a7 | 1601 | 'setq 'cl-psetq) |
03fef3e6 SM |
1602 | (apply 'append sets)))))) |
1603 | ,@(or (cdr endtest) '(nil))))) | |
fcd73769 | 1604 | |
ebacfcc6 | 1605 | ;;;###autoload |
7c1898a7 | 1606 | (defmacro cl-dolist (spec &rest body) |
69d8fb1e | 1607 | "Loop over a list. |
63744c0f | 1608 | Evaluate BODY with VAR bound to each `car' from LIST, in turn. |
69d8fb1e | 1609 | Then evaluate RESULT to get return value, default nil. |
ce887515 | 1610 | An implicit nil block is established around the loop. |
69d8fb1e SM |
1611 | |
1612 | \(fn (VAR LIST [RESULT]) BODY...)" | |
a464a6c7 SM |
1613 | (declare (debug ((symbolp form &optional form) cl-declarations body)) |
1614 | (indent 1)) | |
413d4689 SM |
1615 | (let ((loop `(dolist ,spec ,@body))) |
1616 | (if (advice-member-p #'cl--wrap-in-nil-block 'dolist) | |
1617 | loop `(cl-block nil ,loop)))) | |
63744c0f | 1618 | |
ebacfcc6 | 1619 | ;;;###autoload |
7c1898a7 | 1620 | (defmacro cl-dotimes (spec &rest body) |
69d8fb1e | 1621 | "Loop a certain number of times. |
63744c0f DL |
1622 | Evaluate BODY with VAR bound to successive integers from 0, inclusive, |
1623 | to COUNT, exclusive. Then evaluate RESULT to get return value, default | |
69d8fb1e SM |
1624 | nil. |
1625 | ||
1626 | \(fn (VAR COUNT [RESULT]) BODY...)" | |
a464a6c7 | 1627 | (declare (debug cl-dolist) (indent 1)) |
413d4689 SM |
1628 | (let ((loop `(dotimes ,spec ,@body))) |
1629 | (if (advice-member-p #'cl--wrap-in-nil-block 'dotimes) | |
1630 | loop `(cl-block nil ,loop)))) | |
63744c0f | 1631 | |
4611a3cc SM |
1632 | (defvar cl--tagbody-alist nil) |
1633 | ||
1634 | ;;;###autoload | |
1635 | (defmacro cl-tagbody (&rest labels-or-stmts) | |
1636 | "Execute statements while providing for control transfers to labels. | |
1637 | Each element of LABELS-OR-STMTS can be either a label (integer or symbol) | |
1638 | or a `cons' cell, in which case it's taken to be a statement. | |
1639 | This distinction is made before performing macroexpansion. | |
1640 | Statements are executed in sequence left to right, discarding any return value, | |
1641 | stopping only when reaching the end of LABELS-OR-STMTS. | |
1642 | Any statement can transfer control at any time to the statements that follow | |
1643 | one of the labels with the special form (go LABEL). | |
1644 | Labels have lexical scope and dynamic extent." | |
1645 | (let ((blocks '()) | |
1646 | (first-label (if (consp (car labels-or-stmts)) | |
1647 | 'cl--preamble (pop labels-or-stmts)))) | |
1648 | (let ((block (list first-label))) | |
1649 | (dolist (label-or-stmt labels-or-stmts) | |
1650 | (if (consp label-or-stmt) (push label-or-stmt block) | |
1651 | ;; Add a "go to next block" to implement the fallthrough. | |
1652 | (unless (eq 'go (car-safe (car-safe block))) | |
1653 | (push `(go ,label-or-stmt) block)) | |
1654 | (push (nreverse block) blocks) | |
1655 | (setq block (list label-or-stmt)))) | |
1656 | (unless (eq 'go (car-safe (car-safe block))) | |
1657 | (push `(go cl--exit) block)) | |
1658 | (push (nreverse block) blocks)) | |
1659 | (let ((catch-tag (make-symbol "cl--tagbody-tag"))) | |
1660 | (push (cons 'cl--exit catch-tag) cl--tagbody-alist) | |
1661 | (dolist (block blocks) | |
1662 | (push (cons (car block) catch-tag) cl--tagbody-alist)) | |
1663 | (macroexpand-all | |
1664 | `(let ((next-label ',first-label)) | |
1665 | (while | |
1666 | (not (eq (setq next-label | |
1667 | (catch ',catch-tag | |
1668 | (cl-case next-label | |
1669 | ,@blocks))) | |
1670 | 'cl--exit)))) | |
1671 | `((go . ,(lambda (label) | |
1672 | (let ((catch-tag (cdr (assq label cl--tagbody-alist)))) | |
1673 | (unless catch-tag | |
1674 | (error "Unknown cl-tagbody go label `%S'" label)) | |
1675 | `(throw ',catch-tag ',label)))) | |
1676 | ,@macroexpand-all-environment))))) | |
1677 | ||
ebacfcc6 | 1678 | ;;;###autoload |
7c1898a7 | 1679 | (defmacro cl-do-symbols (spec &rest body) |
69d8fb1e | 1680 | "Loop over all symbols. |
fcd73769 | 1681 | Evaluate BODY with VAR bound to each interned symbol, or to each symbol |
69d8fb1e SM |
1682 | from OBARRAY. |
1683 | ||
1684 | \(fn (VAR [OBARRAY [RESULT]]) BODY...)" | |
70b8ef8f SM |
1685 | (declare (indent 1) |
1686 | (debug ((symbolp &optional form form) cl-declarations body))) | |
fcd73769 | 1687 | ;; Apparently this doesn't have an implicit block. |
7c1898a7 | 1688 | `(cl-block nil |
03fef3e6 SM |
1689 | (let (,(car spec)) |
1690 | (mapatoms #'(lambda (,(car spec)) ,@body) | |
1691 | ,@(and (cadr spec) (list (cadr spec)))) | |
7c1898a7 | 1692 | ,(cl-caddr spec)))) |
fcd73769 | 1693 | |
ebacfcc6 | 1694 | ;;;###autoload |
7c1898a7 | 1695 | (defmacro cl-do-all-symbols (spec &rest body) |
5c677d44 GM |
1696 | "Like `cl-do-symbols', but use the default obarray. |
1697 | ||
1698 | \(fn (VAR [RESULT]) BODY...)" | |
70b8ef8f | 1699 | (declare (indent 1) (debug ((symbolp &optional form) cl-declarations body))) |
7c1898a7 | 1700 | `(cl-do-symbols (,(car spec) nil ,(cadr spec)) ,@body)) |
fcd73769 RS |
1701 | |
1702 | ||
1703 | ;;; Assignments. | |
1704 | ||
ebacfcc6 | 1705 | ;;;###autoload |
7c1898a7 | 1706 | (defmacro cl-psetq (&rest args) |
69d8fb1e | 1707 | "Set SYMs to the values VALs in parallel. |
fcd73769 | 1708 | This is like `setq', except that all VAL forms are evaluated (in order) |
69d8fb1e SM |
1709 | before assigning any symbols SYM to the corresponding values. |
1710 | ||
1711 | \(fn SYM VAL SYM VAL ...)" | |
b1198e17 | 1712 | (declare (debug setq)) |
7c1898a7 | 1713 | (cons 'cl-psetf args)) |
fcd73769 RS |
1714 | |
1715 | ||
1716 | ;;; Binding control structures. | |
1717 | ||
ebacfcc6 | 1718 | ;;;###autoload |
7c1898a7 | 1719 | (defmacro cl-progv (symbols values &rest body) |
64a4c526 | 1720 | "Bind SYMBOLS to VALUES dynamically in BODY. |
fcd73769 | 1721 | The forms SYMBOLS and VALUES are evaluated, and must evaluate to lists. |
3187ba1c | 1722 | Each symbol in the first list is bound to the corresponding value in the |
a05cb6e3 | 1723 | second list (or to nil if VALUES is shorter than SYMBOLS); then the |
fcd73769 RS |
1724 | BODY forms are executed and their result is returned. This is much like |
1725 | a `let' form, except that the list of symbols can be computed at run-time." | |
70b8ef8f | 1726 | (declare (indent 2) (debug (form form body))) |
338bfefa | 1727 | (let ((bodyfun (make-symbol "body")) |
a464a6c7 SM |
1728 | (binds (make-symbol "binds")) |
1729 | (syms (make-symbol "syms")) | |
1730 | (vals (make-symbol "vals"))) | |
1731 | `(progn | |
a464a6c7 SM |
1732 | (let* ((,syms ,symbols) |
1733 | (,vals ,values) | |
1734 | (,bodyfun (lambda () ,@body)) | |
1735 | (,binds ())) | |
1736 | (while ,syms | |
1737 | (push (list (pop ,syms) (list 'quote (pop ,vals))) ,binds)) | |
338bfefa | 1738 | (eval (list 'let ,binds (list 'funcall (list 'quote ,bodyfun)))))))) |
fcd73769 | 1739 | |
de7e2b36 SM |
1740 | (defvar cl--labels-convert-cache nil) |
1741 | ||
1742 | (defun cl--labels-convert (f) | |
1743 | "Special macro-expander to rename (function F) references in `cl-labels'." | |
1744 | (cond | |
1745 | ;; ¡¡Big Ugly Hack!! We can't use a compiler-macro because those are checked | |
1746 | ;; *after* handling `function', but we want to stop macroexpansion from | |
1747 | ;; being applied infinitely, so we use a cache to return the exact `form' | |
1748 | ;; being expanded even though we don't receive it. | |
1749 | ((eq f (car cl--labels-convert-cache)) (cdr cl--labels-convert-cache)) | |
1750 | (t | |
1751 | (let ((found (assq f macroexpand-all-environment))) | |
1752 | (if (and found (ignore-errors | |
1753 | (eq (cadr (cl-caddr found)) 'cl-labels-args))) | |
1754 | (cadr (cl-caddr (cl-cadddr found))) | |
1755 | (let ((res `(function ,f))) | |
1756 | (setq cl--labels-convert-cache (cons f res)) | |
1757 | res)))))) | |
1758 | ||
ebacfcc6 | 1759 | ;;;###autoload |
7c1898a7 | 1760 | (defmacro cl-flet (bindings &rest body) |
eb31fa40 | 1761 | "Make local function definitions. |
de7e2b36 | 1762 | Like `cl-labels' but the definitions are not recursive. |
69d8fb1e SM |
1763 | |
1764 | \(fn ((FUNC ARGLIST BODY...) ...) FORM...)" | |
7c1898a7 | 1765 | (declare (indent 1) (debug ((&rest (cl-defun)) cl-declarations body))) |
de7e2b36 SM |
1766 | (let ((binds ()) (newenv macroexpand-all-environment)) |
1767 | (dolist (binding bindings) | |
1768 | (let ((var (make-symbol (format "--cl-%s--" (car binding))))) | |
1769 | (push (list var `(cl-function (lambda . ,(cdr binding)))) binds) | |
1770 | (push (cons (car binding) | |
1771 | `(lambda (&rest cl-labels-args) | |
1772 | (cl-list* 'funcall ',var | |
1773 | cl-labels-args))) | |
1774 | newenv))) | |
1775 | `(let ,(nreverse binds) | |
1776 | ,@(macroexp-unprogn | |
1777 | (macroexpand-all | |
1778 | `(progn ,@body) | |
1779 | ;; Don't override lexical-let's macro-expander. | |
1780 | (if (assq 'function newenv) newenv | |
1781 | (cons (cons 'function #'cl--labels-convert) newenv))))))) | |
fcd73769 | 1782 | |
d5c6faf9 SM |
1783 | ;;;###autoload |
1784 | (defmacro cl-flet* (bindings &rest body) | |
eb31fa40 | 1785 | "Make local function definitions. |
d5c6faf9 SM |
1786 | Like `cl-flet' but the definitions can refer to previous ones. |
1787 | ||
1788 | \(fn ((FUNC ARGLIST BODY...) ...) FORM...)" | |
a464a6c7 | 1789 | (declare (indent 1) (debug cl-flet)) |
d5c6faf9 SM |
1790 | (cond |
1791 | ((null bindings) (macroexp-progn body)) | |
1792 | ((null (cdr bindings)) `(cl-flet ,bindings ,@body)) | |
1793 | (t `(cl-flet (,(pop bindings)) (cl-flet* ,bindings ,@body))))) | |
1794 | ||
ebacfcc6 | 1795 | ;;;###autoload |
7c1898a7 | 1796 | (defmacro cl-labels (bindings &rest body) |
3187ba1c | 1797 | "Make temporary function bindings. |
a464a6c7 SM |
1798 | The bindings can be recursive and the scoping is lexical, but capturing them |
1799 | in closures will only work if `lexical-binding' is in use. | |
69d8fb1e SM |
1800 | |
1801 | \(fn ((FUNC ARGLIST BODY...) ...) FORM...)" | |
7c1898a7 | 1802 | (declare (indent 1) (debug cl-flet)) |
de7e2b36 SM |
1803 | (let ((binds ()) (newenv macroexpand-all-environment)) |
1804 | (dolist (binding bindings) | |
1805 | (let ((var (make-symbol (format "--cl-%s--" (car binding))))) | |
1806 | (push (list var `(cl-function (lambda . ,(cdr binding)))) binds) | |
1807 | (push (cons (car binding) | |
4dd1c416 SM |
1808 | `(lambda (&rest cl-labels-args) |
1809 | (cl-list* 'funcall ',var | |
1810 | cl-labels-args))) | |
6fa6c4ae | 1811 | newenv))) |
de7e2b36 SM |
1812 | (macroexpand-all `(letrec ,(nreverse binds) ,@body) |
1813 | ;; Don't override lexical-let's macro-expander. | |
1814 | (if (assq 'function newenv) newenv | |
1815 | (cons (cons 'function #'cl--labels-convert) newenv))))) | |
fcd73769 RS |
1816 | |
1817 | ;; The following ought to have a better definition for use with newer | |
1818 | ;; byte compilers. | |
ebacfcc6 | 1819 | ;;;###autoload |
7c1898a7 | 1820 | (defmacro cl-macrolet (bindings &rest body) |
4342e957 | 1821 | "Make temporary macro definitions. |
7c1898a7 | 1822 | This is like `cl-flet', but for macros instead of functions. |
69d8fb1e SM |
1823 | |
1824 | \(fn ((NAME ARGLIST BODY...) ...) FORM...)" | |
70b8ef8f SM |
1825 | (declare (indent 1) |
1826 | (debug | |
b1198e17 SM |
1827 | ((&rest (&define name (&rest arg) cl-declarations-or-string |
1828 | def-body)) | |
1829 | cl-declarations body))) | |
fcd73769 | 1830 | (if (cdr bindings) |
7c1898a7 | 1831 | `(cl-macrolet (,(car bindings)) (cl-macrolet ,(cdr bindings) ,@body)) |
fcd73769 RS |
1832 | (if (null bindings) (cons 'progn body) |
1833 | (let* ((name (caar bindings)) | |
4dd1c416 | 1834 | (res (cl--transform-lambda (cdar bindings) name))) |
fcd73769 | 1835 | (eval (car res)) |
6fa6c4ae SM |
1836 | (macroexpand-all (cons 'progn body) |
1837 | (cons (cons name `(lambda ,@(cdr res))) | |
1838 | macroexpand-all-environment)))))) | |
1839 | ||
1840 | (defconst cl--old-macroexpand | |
1841 | (if (and (boundp 'cl--old-macroexpand) | |
1842 | (eq (symbol-function 'macroexpand) | |
1843 | #'cl--sm-macroexpand)) | |
1844 | cl--old-macroexpand | |
1845 | (symbol-function 'macroexpand))) | |
1846 | ||
ea376861 | 1847 | (defun cl--sm-macroexpand (exp &optional env) |
6fa6c4ae SM |
1848 | "Special macro expander used inside `cl-symbol-macrolet'. |
1849 | This function replaces `macroexpand' during macro expansion | |
1850 | of `cl-symbol-macrolet', and does the same thing as `macroexpand' | |
1851 | except that it additionally expands symbol macros." | |
ea376861 | 1852 | (let ((macroexpand-all-environment env)) |
6fa6c4ae SM |
1853 | (while |
1854 | (progn | |
ea376861 SM |
1855 | (setq exp (funcall cl--old-macroexpand exp env)) |
1856 | (pcase exp | |
1857 | ((pred symbolp) | |
1858 | ;; Perform symbol-macro expansion. | |
1859 | (when (cdr (assq (symbol-name exp) env)) | |
1860 | (setq exp (cadr (assq (symbol-name exp) env))))) | |
1861 | (`(setq . ,_) | |
1862 | ;; Convert setq to setf if required by symbol-macro expansion. | |
1863 | (let* ((args (mapcar (lambda (f) (cl--sm-macroexpand f env)) | |
1864 | (cdr exp))) | |
1865 | (p args)) | |
1866 | (while (and p (symbolp (car p))) (setq p (cddr p))) | |
1867 | (if p (setq exp (cons 'setf args)) | |
1868 | (setq exp (cons 'setq args)) | |
1869 | ;; Don't loop further. | |
1870 | nil))) | |
1871 | (`(,(or `let `let*) . ,(or `(,bindings . ,body) dontcare)) | |
1872 | ;; CL's symbol-macrolet treats re-bindings as candidates for | |
1873 | ;; expansion (turning the let into a letf if needed), contrary to | |
1874 | ;; Common-Lisp where such re-bindings hide the symbol-macro. | |
1875 | (let ((letf nil) (found nil) (nbs ())) | |
1876 | (dolist (binding bindings) | |
1877 | (let* ((var (if (symbolp binding) binding (car binding))) | |
1878 | (sm (assq (symbol-name var) env))) | |
1879 | (push (if (not (cdr sm)) | |
1880 | binding | |
1881 | (let ((nexp (cadr sm))) | |
1882 | (setq found t) | |
1883 | (unless (symbolp nexp) (setq letf t)) | |
1884 | (cons nexp (cdr-safe binding)))) | |
1885 | nbs))) | |
1886 | (when found | |
1887 | (setq exp `(,(if letf | |
1888 | (if (eq (car exp) 'let) 'cl-letf 'cl-letf*) | |
1889 | (car exp)) | |
1890 | ,(nreverse nbs) | |
1891 | ,@body))))) | |
1892 | ;; FIXME: The behavior of CL made sense in a dynamically scoped | |
1893 | ;; language, but for lexical scoping, Common-Lisp's behavior might | |
1894 | ;; make more sense (and indeed, CL behaves like Common-Lisp w.r.t | |
1895 | ;; lexical-let), so maybe we should adjust the behavior based on | |
1896 | ;; the use of lexical-binding. | |
1897 | ;; (`(,(or `let `let*) . ,(or `(,bindings . ,body) dontcare)) | |
1898 | ;; (let ((nbs ()) (found nil)) | |
1899 | ;; (dolist (binding bindings) | |
1900 | ;; (let* ((var (if (symbolp binding) binding (car binding))) | |
1901 | ;; (name (symbol-name var)) | |
1902 | ;; (val (and found (consp binding) (eq 'let* (car exp)) | |
1903 | ;; (list (macroexpand-all (cadr binding) | |
1904 | ;; env))))) | |
1905 | ;; (push (if (assq name env) | |
1906 | ;; ;; This binding should hide its symbol-macro, | |
1907 | ;; ;; but given the way macroexpand-all works, we | |
1908 | ;; ;; can't prevent application of `env' to the | |
1909 | ;; ;; sub-expressions, so we need to α-rename this | |
1910 | ;; ;; variable instead. | |
1911 | ;; (let ((nvar (make-symbol | |
1912 | ;; (copy-sequence name)))) | |
1913 | ;; (setq found t) | |
1914 | ;; (push (list name nvar) env) | |
1915 | ;; (cons nvar (or val (cdr-safe binding)))) | |
1916 | ;; (if val (cons var val) binding)) | |
1917 | ;; nbs))) | |
1918 | ;; (when found | |
1919 | ;; (setq exp `(,(car exp) | |
1920 | ;; ,(nreverse nbs) | |
1921 | ;; ,@(macroexp-unprogn | |
1922 | ;; (macroexpand-all (macroexp-progn body) | |
1923 | ;; env))))) | |
1924 | ;; nil)) | |
1925 | ))) | |
1926 | exp)) | |
fcd73769 | 1927 | |
ebacfcc6 | 1928 | ;;;###autoload |
7c1898a7 | 1929 | (defmacro cl-symbol-macrolet (bindings &rest body) |
4342e957 | 1930 | "Make symbol macro definitions. |
fcd73769 | 1931 | Within the body FORMs, references to the variable NAME will be replaced |
2ee3d7f0 | 1932 | by EXPANSION, and (setq NAME ...) will act like (setf EXPANSION ...). |
69d8fb1e SM |
1933 | |
1934 | \(fn ((NAME EXPANSION) ...) FORM...)" | |
70b8ef8f | 1935 | (declare (indent 1) (debug ((&rest (symbol sexp)) cl-declarations body))) |
6fa6c4ae SM |
1936 | (cond |
1937 | ((cdr bindings) | |
36cec983 | 1938 | `(cl-symbol-macrolet (,(car bindings)) |
6fa6c4ae SM |
1939 | (cl-symbol-macrolet ,(cdr bindings) ,@body))) |
1940 | ((null bindings) (macroexp-progn body)) | |
1941 | (t | |
1942 | (let ((previous-macroexpand (symbol-function 'macroexpand))) | |
1943 | (unwind-protect | |
1944 | (progn | |
1945 | (fset 'macroexpand #'cl--sm-macroexpand) | |
1946 | ;; FIXME: For N bindings, this will traverse `body' N times! | |
1947 | (macroexpand-all (cons 'progn body) | |
36cec983 SM |
1948 | (cons (list (symbol-name (caar bindings)) |
1949 | (cl-cadar bindings)) | |
6fa6c4ae SM |
1950 | macroexpand-all-environment))) |
1951 | (fset 'macroexpand previous-macroexpand)))))) | |
fcd73769 | 1952 | |
fcd73769 RS |
1953 | ;;; Multiple values. |
1954 | ||
ebacfcc6 | 1955 | ;;;###autoload |
7c1898a7 | 1956 | (defmacro cl-multiple-value-bind (vars form &rest body) |
69d8fb1e | 1957 | "Collect multiple return values. |
fcd73769 RS |
1958 | FORM must return a list; the BODY is then executed with the first N elements |
1959 | of this list bound (`let'-style) to each of the symbols SYM in turn. This | |
1048af7c | 1960 | is analogous to the Common Lisp `multiple-value-bind' macro, using lists to |
7c1898a7 | 1961 | simulate true multiple return values. For compatibility, (cl-values A B C) is |
69d8fb1e SM |
1962 | a synonym for (list A B C). |
1963 | ||
3187ba1c | 1964 | \(fn (SYM...) FORM BODY)" |
70b8ef8f | 1965 | (declare (indent 2) (debug ((&rest symbolp) form body))) |
e542ea4b | 1966 | (let ((temp (make-symbol "--cl-var--")) (n -1)) |
03fef3e6 SM |
1967 | `(let* ((,temp ,form) |
1968 | ,@(mapcar (lambda (v) | |
1969 | (list v `(nth ,(setq n (1+ n)) ,temp))) | |
1970 | vars)) | |
1971 | ,@body))) | |
fcd73769 | 1972 | |
ebacfcc6 | 1973 | ;;;###autoload |
7c1898a7 | 1974 | (defmacro cl-multiple-value-setq (vars form) |
69d8fb1e | 1975 | "Collect multiple return values. |
fcd73769 RS |
1976 | FORM must return a list; the first N elements of this list are stored in |
1977 | each of the symbols SYM in turn. This is analogous to the Common Lisp | |
1048af7c | 1978 | `multiple-value-setq' macro, using lists to simulate true multiple return |
7c1898a7 | 1979 | values. For compatibility, (cl-values A B C) is a synonym for (list A B C). |
69d8fb1e | 1980 | |
3187ba1c | 1981 | \(fn (SYM...) FORM)" |
70b8ef8f | 1982 | (declare (indent 1) (debug ((&rest symbolp) form))) |
03fef3e6 SM |
1983 | (cond ((null vars) `(progn ,form nil)) |
1984 | ((null (cdr vars)) `(setq ,(car vars) (car ,form))) | |
fcd73769 | 1985 | (t |
e542ea4b | 1986 | (let* ((temp (make-symbol "--cl-var--")) (n 0)) |
03fef3e6 SM |
1987 | `(let ((,temp ,form)) |
1988 | (prog1 (setq ,(pop vars) (car ,temp)) | |
1989 | (setq ,@(apply #'nconc | |
1990 | (mapcar (lambda (v) | |
1991 | (list v `(nth ,(setq n (1+ n)) | |
1992 | ,temp))) | |
1993 | vars))))))))) | |
fcd73769 RS |
1994 | |
1995 | ||
1996 | ;;; Declarations. | |
1997 | ||
ebacfcc6 | 1998 | ;;;###autoload |
7c1898a7 | 1999 | (defmacro cl-locally (&rest body) |
5593ed90 | 2000 | "Equivalent to `progn'." |
b1198e17 SM |
2001 | (declare (debug t)) |
2002 | (cons 'progn body)) | |
ebacfcc6 | 2003 | ;;;###autoload |
513749ee | 2004 | (defmacro cl-the (_type form) |
1048af7c | 2005 | "At present this ignores TYPE and is simply equivalent to FORM." |
70b8ef8f | 2006 | (declare (indent 1) (debug (cl-type-spec form))) |
b1198e17 | 2007 | form) |
fcd73769 | 2008 | |
338bfefa SM |
2009 | (defvar cl--proclaim-history t) ; for future compilers |
2010 | (defvar cl--declare-stack t) ; for future compilers | |
fcd73769 | 2011 | |
338bfefa SM |
2012 | (defun cl--do-proclaim (spec hist) |
2013 | (and hist (listp cl--proclaim-history) (push spec cl--proclaim-history)) | |
fcd73769 RS |
2014 | (cond ((eq (car-safe spec) 'special) |
2015 | (if (boundp 'byte-compile-bound-variables) | |
2016 | (setq byte-compile-bound-variables | |
2017 | (append (cdr spec) byte-compile-bound-variables)))) | |
2018 | ||
2019 | ((eq (car-safe spec) 'inline) | |
2020 | (while (setq spec (cdr spec)) | |
2021 | (or (memq (get (car spec) 'byte-optimizer) | |
2022 | '(nil byte-compile-inline-expand)) | |
2023 | (error "%s already has a byte-optimizer, can't make it inline" | |
2024 | (car spec))) | |
2025 | (put (car spec) 'byte-optimizer 'byte-compile-inline-expand))) | |
2026 | ||
2027 | ((eq (car-safe spec) 'notinline) | |
2028 | (while (setq spec (cdr spec)) | |
2029 | (if (eq (get (car spec) 'byte-optimizer) | |
2030 | 'byte-compile-inline-expand) | |
2031 | (put (car spec) 'byte-optimizer nil)))) | |
2032 | ||
2033 | ((eq (car-safe spec) 'optimize) | |
2034 | (let ((speed (assq (nth 1 (assq 'speed (cdr spec))) | |
2035 | '((0 nil) (1 t) (2 t) (3 t)))) | |
2036 | (safety (assq (nth 1 (assq 'safety (cdr spec))) | |
2037 | '((0 t) (1 t) (2 t) (3 nil))))) | |
338bfefa | 2038 | (if speed (setq cl--optimize-speed (car speed) |
fcd73769 | 2039 | byte-optimize (nth 1 speed))) |
338bfefa | 2040 | (if safety (setq cl--optimize-safety (car safety) |
fcd73769 RS |
2041 | byte-compile-delete-errors (nth 1 safety))))) |
2042 | ||
2043 | ((and (eq (car-safe spec) 'warn) (boundp 'byte-compile-warnings)) | |
fcd73769 RS |
2044 | (while (setq spec (cdr spec)) |
2045 | (if (consp (car spec)) | |
7c1898a7 | 2046 | (if (eq (cl-cadar spec) 0) |
bc8ce89b GM |
2047 | (byte-compile-disable-warning (caar spec)) |
2048 | (byte-compile-enable-warning (caar spec))))))) | |
fcd73769 RS |
2049 | nil) |
2050 | ||
2051 | ;;; Process any proclamations made before cl-macs was loaded. | |
338bfefa SM |
2052 | (defvar cl--proclaims-deferred) |
2053 | (let ((p (reverse cl--proclaims-deferred))) | |
2054 | (while p (cl--do-proclaim (pop p) t)) | |
2055 | (setq cl--proclaims-deferred nil)) | |
fcd73769 | 2056 | |
ebacfcc6 | 2057 | ;;;###autoload |
7c1898a7 | 2058 | (defmacro cl-declare (&rest specs) |
c39da690 | 2059 | "Declare SPECS about the current function while compiling. |
4bf0979f LMI |
2060 | For instance |
2061 | ||
1048af7c | 2062 | (cl-declare (warn 0)) |
4bf0979f | 2063 | |
dbc44fcd LMI |
2064 | will turn off byte-compile warnings in the function. |
2065 | See Info node `(cl)Declarations' for details." | |
bb3faf5b | 2066 | (if (cl--compiling-file) |
fcd73769 | 2067 | (while specs |
338bfefa SM |
2068 | (if (listp cl--declare-stack) (push (car specs) cl--declare-stack)) |
2069 | (cl--do-proclaim (pop specs) nil))) | |
fcd73769 RS |
2070 | nil) |
2071 | ||
fcd73769 | 2072 | ;;; The standard modify macros. |
69d8fb1e | 2073 | |
2ee3d7f0 | 2074 | ;; `setf' is now part of core Elisp, defined in gv.el. |
fcd73769 | 2075 | |
ebacfcc6 | 2076 | ;;;###autoload |
7c1898a7 | 2077 | (defmacro cl-psetf (&rest args) |
69d8fb1e | 2078 | "Set PLACEs to the values VALs in parallel. |
2ee3d7f0 | 2079 | This is like `setf', except that all VAL forms are evaluated (in order) |
69d8fb1e SM |
2080 | before assigning any PLACEs to the corresponding values. |
2081 | ||
2082 | \(fn PLACE VAL PLACE VAL ...)" | |
2ee3d7f0 | 2083 | (declare (debug setf)) |
fcd73769 RS |
2084 | (let ((p args) (simple t) (vars nil)) |
2085 | (while p | |
4dd1c416 | 2086 | (if (or (not (symbolp (car p))) (cl--expr-depends-p (nth 1 p) vars)) |
fcd73769 RS |
2087 | (setq simple nil)) |
2088 | (if (memq (car p) vars) | |
2089 | (error "Destination duplicated in psetf: %s" (car p))) | |
69d8fb1e | 2090 | (push (pop p) vars) |
7c1898a7 | 2091 | (or p (error "Odd number of arguments to cl-psetf")) |
69d8fb1e | 2092 | (pop p)) |
fcd73769 | 2093 | (if simple |
972debf2 | 2094 | `(progn (setq ,@args) nil) |
fcd73769 | 2095 | (setq args (reverse args)) |
2ee3d7f0 | 2096 | (let ((expr `(setf ,(cadr args) ,(car args)))) |
fcd73769 | 2097 | (while (setq args (cddr args)) |
2ee3d7f0 | 2098 | (setq expr `(setf ,(cadr args) (prog1 ,(car args) ,expr)))) |
03fef3e6 | 2099 | `(progn ,expr nil))))) |
fcd73769 | 2100 | |
ebacfcc6 | 2101 | ;;;###autoload |
7c1898a7 | 2102 | (defmacro cl-remf (place tag) |
64a4c526 | 2103 | "Remove TAG from property list PLACE. |
2ee3d7f0 | 2104 | PLACE may be a symbol, or any generalized variable allowed by `setf'. |
fcd73769 | 2105 | The form returns true if TAG was found and removed, nil otherwise." |
b1198e17 | 2106 | (declare (debug (place form))) |
2ee3d7f0 SM |
2107 | (gv-letplace (tval setter) place |
2108 | (macroexp-let2 macroexp-copyable-p ttag tag | |
2109 | `(if (eq ,ttag (car ,tval)) | |
2110 | (progn ,(funcall setter `(cddr ,tval)) | |
03fef3e6 | 2111 | t) |
ad4d226c | 2112 | (cl--do-remf ,tval ,ttag))))) |
fcd73769 | 2113 | |
ebacfcc6 | 2114 | ;;;###autoload |
7c1898a7 | 2115 | (defmacro cl-shiftf (place &rest args) |
69d8fb1e | 2116 | "Shift left among PLACEs. |
7c1898a7 | 2117 | Example: (cl-shiftf A B C) sets A to B, B to C, and returns the old A. |
2ee3d7f0 | 2118 | Each PLACE may be a symbol, or any generalized variable allowed by `setf'. |
69d8fb1e | 2119 | |
3187ba1c | 2120 | \(fn PLACE... VAL)" |
b1198e17 | 2121 | (declare (debug (&rest place))) |
2fa5eef4 SM |
2122 | (cond |
2123 | ((null args) place) | |
7c1898a7 | 2124 | ((symbolp place) `(prog1 ,place (setq ,place (cl-shiftf ,@args)))) |
2fa5eef4 | 2125 | (t |
2ee3d7f0 SM |
2126 | (gv-letplace (getter setter) place |
2127 | `(prog1 ,getter | |
2128 | ,(funcall setter `(cl-shiftf ,@args))))))) | |
fcd73769 | 2129 | |
ebacfcc6 | 2130 | ;;;###autoload |
7c1898a7 | 2131 | (defmacro cl-rotatef (&rest args) |
69d8fb1e | 2132 | "Rotate left among PLACEs. |
7c1898a7 | 2133 | Example: (cl-rotatef A B C) sets A to B, B to C, and C to A. It returns nil. |
2ee3d7f0 | 2134 | Each PLACE may be a symbol, or any generalized variable allowed by `setf'. |
69d8fb1e SM |
2135 | |
2136 | \(fn PLACE...)" | |
b1198e17 | 2137 | (declare (debug (&rest place))) |
fcd73769 RS |
2138 | (if (not (memq nil (mapcar 'symbolp args))) |
2139 | (and (cdr args) | |
2140 | (let ((sets nil) | |
2141 | (first (car args))) | |
2142 | (while (cdr args) | |
69d8fb1e | 2143 | (setq sets (nconc sets (list (pop args) (car args))))) |
7c1898a7 | 2144 | `(cl-psetf ,@sets ,(car args) ,first))) |
fcd73769 | 2145 | (let* ((places (reverse args)) |
e542ea4b | 2146 | (temp (make-symbol "--cl-rotatef--")) |
fcd73769 RS |
2147 | (form temp)) |
2148 | (while (cdr places) | |
2ee3d7f0 SM |
2149 | (setq form |
2150 | (gv-letplace (getter setter) (pop places) | |
2151 | `(prog1 ,getter ,(funcall setter form))))) | |
2152 | (gv-letplace (getter setter) (car places) | |
2153 | (macroexp-let* `((,temp ,getter)) | |
2154 | `(progn ,(funcall setter form) nil)))))) | |
fcd73769 | 2155 | |
a464a6c7 SM |
2156 | ;; FIXME: `letf' is unsatisfactory because it does not really "restore" the |
2157 | ;; previous state. If the getter/setter loses information, that info is | |
2158 | ;; not recovered. | |
2159 | ||
2160 | (defun cl--letf (bindings simplebinds binds body) | |
2161 | ;; It's not quite clear what the semantics of cl-letf should be. | |
2162 | ;; E.g. in (cl-letf ((PLACE1 VAL1) (PLACE2 VAL2)) BODY), while it's clear | |
2163 | ;; that the actual assignments ("bindings") should only happen after | |
2164 | ;; evaluating VAL1 and VAL2, it's not clear when the sub-expressions of | |
2165 | ;; PLACE1 and PLACE2 should be evaluated. Should we have | |
2166 | ;; PLACE1; VAL1; PLACE2; VAL2; bind1; bind2 | |
2167 | ;; or | |
2168 | ;; VAL1; VAL2; PLACE1; PLACE2; bind1; bind2 | |
2169 | ;; or | |
2170 | ;; VAL1; VAL2; PLACE1; bind1; PLACE2; bind2 | |
2171 | ;; Common-Lisp's `psetf' does the first, so we'll do the same. | |
2172 | (if (null bindings) | |
2173 | (if (and (null binds) (null simplebinds)) (macroexp-progn body) | |
2174 | `(let* (,@(mapcar (lambda (x) | |
2175 | (pcase-let ((`(,vold ,getter ,_setter ,_vnew) x)) | |
2176 | (list vold getter))) | |
2177 | binds) | |
2178 | ,@simplebinds) | |
2179 | (unwind-protect | |
2180 | ,(macroexp-progn | |
2181 | (append | |
2182 | (delq nil | |
2183 | (mapcar (lambda (x) | |
2184 | (pcase x | |
2185 | ;; If there's no vnew, do nothing. | |
2186 | (`(,_vold ,_getter ,setter ,vnew) | |
2187 | (funcall setter vnew)))) | |
2188 | binds)) | |
2189 | body)) | |
2190 | ,@(mapcar (lambda (x) | |
2191 | (pcase-let ((`(,vold ,_getter ,setter ,_vnew) x)) | |
2192 | (funcall setter vold))) | |
2193 | binds)))) | |
2194 | (let ((binding (car bindings))) | |
2195 | (gv-letplace (getter setter) (car binding) | |
2196 | (macroexp-let2 nil vnew (cadr binding) | |
2197 | (if (symbolp (car binding)) | |
2198 | ;; Special-case for simple variables. | |
2199 | (cl--letf (cdr bindings) | |
2200 | (cons `(,getter ,(if (cdr binding) vnew getter)) | |
2201 | simplebinds) | |
2202 | binds body) | |
2203 | (cl--letf (cdr bindings) simplebinds | |
2204 | (cons `(,(make-symbol "old") ,getter ,setter | |
2205 | ,@(if (cdr binding) (list vnew))) | |
2206 | binds) | |
2207 | body))))))) | |
2208 | ||
2209 | ;;;###autoload | |
2210 | (defmacro cl-letf (bindings &rest body) | |
2211 | "Temporarily bind to PLACEs. | |
2212 | This is the analogue of `let', but with generalized variables (in the | |
2213 | sense of `setf') for the PLACEs. Each PLACE is set to the corresponding | |
2214 | VALUE, then the BODY forms are executed. On exit, either normally or | |
2215 | because of a `throw' or error, the PLACEs are set back to their original | |
2216 | values. Note that this macro is *not* available in Common Lisp. | |
2217 | As a special case, if `(PLACE)' is used instead of `(PLACE VALUE)', | |
2218 | the PLACE is not modified before executing BODY. | |
2219 | ||
2220 | \(fn ((PLACE VALUE) ...) BODY...)" | |
2221 | (declare (indent 1) (debug ((&rest (gate gv-place &optional form)) body))) | |
2222 | (if (and (not (cdr bindings)) (cdar bindings) (symbolp (caar bindings))) | |
2223 | `(let ,bindings ,@body) | |
2224 | (cl--letf bindings () () body))) | |
2225 | ||
2226 | ;;;###autoload | |
2227 | (defmacro cl-letf* (bindings &rest body) | |
2228 | "Temporarily bind to PLACEs. | |
2229 | Like `cl-letf' but where the bindings are performed one at a time, | |
2230 | rather than all at the end (i.e. like `let*' rather than like `let')." | |
2231 | (declare (indent 1) (debug cl-letf)) | |
2232 | (dolist (binding (reverse bindings)) | |
2233 | (setq body (list `(cl-letf (,binding) ,@body)))) | |
2234 | (macroexp-progn body)) | |
2235 | ||
ebacfcc6 | 2236 | ;;;###autoload |
7c1898a7 | 2237 | (defmacro cl-callf (func place &rest args) |
69d8fb1e | 2238 | "Set PLACE to (FUNC PLACE ARGS...). |
fcd73769 | 2239 | FUNC should be an unquoted function name. PLACE may be a symbol, |
2ee3d7f0 | 2240 | or any generalized variable allowed by `setf'." |
7c1898a7 | 2241 | (declare (indent 2) (debug (cl-function place &rest form))) |
2ee3d7f0 SM |
2242 | (gv-letplace (getter setter) place |
2243 | (let* ((rargs (cons getter args))) | |
2244 | (funcall setter | |
2245 | (if (symbolp func) (cons func rargs) | |
2246 | `(funcall #',func ,@rargs)))))) | |
fcd73769 | 2247 | |
ebacfcc6 | 2248 | ;;;###autoload |
7c1898a7 | 2249 | (defmacro cl-callf2 (func arg1 place &rest args) |
69d8fb1e | 2250 | "Set PLACE to (FUNC ARG1 PLACE ARGS...). |
7c1898a7 | 2251 | Like `cl-callf', but PLACE is the second argument of FUNC, not the first. |
69d8fb1e SM |
2252 | |
2253 | \(fn FUNC ARG1 PLACE ARGS...)" | |
7c1898a7 | 2254 | (declare (indent 3) (debug (cl-function form place &rest form))) |
4dd1c416 | 2255 | (if (and (cl--safe-expr-p arg1) (cl--simple-expr-p place) (symbolp func)) |
2ee3d7f0 SM |
2256 | `(setf ,place (,func ,arg1 ,place ,@args)) |
2257 | (macroexp-let2 nil a1 arg1 | |
2258 | (gv-letplace (getter setter) place | |
2259 | (let* ((rargs (cl-list* a1 getter args))) | |
2260 | (funcall setter | |
2261 | (if (symbolp func) (cons func rargs) | |
2262 | `(funcall #',func ,@rargs)))))))) | |
fcd73769 RS |
2263 | |
2264 | ;;; Structures. | |
2265 | ||
ebacfcc6 | 2266 | ;;;###autoload |
7c1898a7 | 2267 | (defmacro cl-defstruct (struct &rest descs) |
69d8fb1e | 2268 | "Define a struct type. |
c7dc1ac1 CY |
2269 | This macro defines a new data type called NAME that stores data |
2270 | in SLOTs. It defines a `make-NAME' constructor, a `copy-NAME' | |
2271 | copier, a `NAME-p' predicate, and slot accessors named `NAME-SLOT'. | |
2ee3d7f0 | 2272 | You can use the accessors to set the corresponding slots, via `setf'. |
69d8fb1e | 2273 | |
c7dc1ac1 | 2274 | NAME may instead take the form (NAME OPTIONS...), where each |
685f87b0 SM |
2275 | OPTION is either a single keyword or (KEYWORD VALUE) where |
2276 | KEYWORD can be one of :conc-name, :constructor, :copier, :predicate, | |
2277 | :type, :named, :initial-offset, :print-function, or :include. | |
c7dc1ac1 | 2278 | |
cf4e5178 SM |
2279 | Each SLOT may instead take the form (SNAME SDEFAULT SOPTIONS...), where |
2280 | SDEFAULT is the default value of that slot and SOPTIONS are keyword-value | |
2281 | pairs for that slot. | |
1048af7c JB |
2282 | Currently, only one keyword is supported, `:read-only'. If this has a |
2283 | non-nil value, that slot cannot be set via `setf'. | |
c7dc1ac1 CY |
2284 | |
2285 | \(fn NAME SLOTS...)" | |
972debf2 | 2286 | (declare (doc-string 2) (indent 1) |
b581bb5c | 2287 | (debug |
b1198e17 SM |
2288 | (&define ;Makes top-level form not be wrapped. |
2289 | [&or symbolp | |
2290 | (gate | |
2291 | symbolp &rest | |
2292 | (&or [":conc-name" symbolp] | |
2293 | [":constructor" symbolp &optional cl-lambda-list] | |
2294 | [":copier" symbolp] | |
2295 | [":predicate" symbolp] | |
2296 | [":include" symbolp &rest sexp] ;; Not finished. | |
2297 | ;; The following are not supported. | |
2298 | ;; [":print-function" ...] | |
2299 | ;; [":type" ...] | |
2300 | ;; [":initial-offset" ...] | |
2301 | ))] | |
2302 | [&optional stringp] | |
2303 | ;; All the above is for the following def-form. | |
2304 | &rest &or symbolp (symbolp def-form | |
2305 | &optional ":read-only" sexp)))) | |
fcd73769 RS |
2306 | (let* ((name (if (consp struct) (car struct) struct)) |
2307 | (opts (cdr-safe struct)) | |
2308 | (slots nil) | |
2309 | (defaults nil) | |
2310 | (conc-name (concat (symbol-name name) "-")) | |
2311 | (constructor (intern (format "make-%s" name))) | |
2312 | (constrs nil) | |
2313 | (copier (intern (format "copy-%s" name))) | |
2314 | (predicate (intern (format "%s-p" name))) | |
2315 | (print-func nil) (print-auto nil) | |
338bfefa | 2316 | (safety (if (cl--compiling-file) cl--optimize-safety 3)) |
fcd73769 RS |
2317 | (include nil) |
2318 | (tag (intern (format "cl-struct-%s" name))) | |
2319 | (tag-symbol (intern (format "cl-struct-%s-tags" name))) | |
2320 | (include-descs nil) | |
fcd73769 RS |
2321 | (side-eff nil) |
2322 | (type nil) | |
2323 | (named nil) | |
2324 | (forms nil) | |
2325 | pred-form pred-check) | |
2326 | (if (stringp (car descs)) | |
03fef3e6 SM |
2327 | (push `(put ',name 'structure-documentation |
2328 | ,(pop descs)) forms)) | |
fcd73769 RS |
2329 | (setq descs (cons '(cl-tag-slot) |
2330 | (mapcar (function (lambda (x) (if (consp x) x (list x)))) | |
2331 | descs))) | |
2332 | (while opts | |
2333 | (let ((opt (if (consp (car opts)) (caar opts) (car opts))) | |
69d8fb1e | 2334 | (args (cdr-safe (pop opts)))) |
64a4c526 | 2335 | (cond ((eq opt :conc-name) |
fcd73769 RS |
2336 | (if args |
2337 | (setq conc-name (if (car args) | |
2338 | (symbol-name (car args)) "")))) | |
64a4c526 | 2339 | ((eq opt :constructor) |
fcd73769 | 2340 | (if (cdr args) |
40e8a6c3 SM |
2341 | (progn |
2342 | ;; If this defines a constructor of the same name as | |
2343 | ;; the default one, don't define the default. | |
2344 | (if (eq (car args) constructor) | |
2345 | (setq constructor nil)) | |
2346 | (push args constrs)) | |
fcd73769 | 2347 | (if args (setq constructor (car args))))) |
64a4c526 | 2348 | ((eq opt :copier) |
fcd73769 | 2349 | (if args (setq copier (car args)))) |
64a4c526 | 2350 | ((eq opt :predicate) |
fcd73769 | 2351 | (if args (setq predicate (car args)))) |
64a4c526 | 2352 | ((eq opt :include) |
fcd73769 RS |
2353 | (setq include (car args) |
2354 | include-descs (mapcar (function | |
2355 | (lambda (x) | |
2356 | (if (consp x) x (list x)))) | |
36f0f2b1 | 2357 | (cdr args)))) |
64a4c526 | 2358 | ((eq opt :print-function) |
fcd73769 | 2359 | (setq print-func (car args))) |
64a4c526 | 2360 | ((eq opt :type) |
fcd73769 | 2361 | (setq type (car args))) |
64a4c526 | 2362 | ((eq opt :named) |
fcd73769 | 2363 | (setq named t)) |
64a4c526 | 2364 | ((eq opt :initial-offset) |
fcd73769 RS |
2365 | (setq descs (nconc (make-list (car args) '(cl-skip-slot)) |
2366 | descs))) | |
2367 | (t | |
2368 | (error "Slot option %s unrecognized" opt))))) | |
2369 | (if print-func | |
03fef3e6 SM |
2370 | (setq print-func |
2371 | `(progn (funcall #',print-func cl-x cl-s cl-n) t)) | |
fcd73769 RS |
2372 | (or type (and include (not (get include 'cl-struct-print))) |
2373 | (setq print-auto t | |
2374 | print-func (and (or (not (or include type)) (null print-func)) | |
03fef3e6 SM |
2375 | `(progn |
2376 | (princ ,(format "#S(%s" name) cl-s)))))) | |
fcd73769 RS |
2377 | (if include |
2378 | (let ((inc-type (get include 'cl-struct-type)) | |
2379 | (old-descs (get include 'cl-struct-slots))) | |
2380 | (or inc-type (error "%s is not a struct name" include)) | |
2381 | (and type (not (eq (car inc-type) type)) | |
2382 | (error ":type disagrees with :include for %s" name)) | |
2383 | (while include-descs | |
2384 | (setcar (memq (or (assq (caar include-descs) old-descs) | |
2385 | (error "No slot %s in included struct %s" | |
2386 | (caar include-descs) include)) | |
2387 | old-descs) | |
69d8fb1e | 2388 | (pop include-descs))) |
fcd73769 RS |
2389 | (setq descs (append old-descs (delq (assq 'cl-tag-slot descs) descs)) |
2390 | type (car inc-type) | |
2391 | named (assq 'cl-tag-slot descs)) | |
2392 | (if (cadr inc-type) (setq tag name named t)) | |
36f0f2b1 RS |
2393 | (let ((incl include)) |
2394 | (while incl | |
7c1898a7 | 2395 | (push `(cl-pushnew ',tag |
03fef3e6 SM |
2396 | ,(intern (format "cl-struct-%s-tags" incl))) |
2397 | forms) | |
36f0f2b1 | 2398 | (setq incl (get incl 'cl-struct-include))))) |
fcd73769 RS |
2399 | (if type |
2400 | (progn | |
2401 | (or (memq type '(vector list)) | |
4920bd1e | 2402 | (error "Invalid :type specifier: %s" type)) |
fcd73769 RS |
2403 | (if named (setq tag name))) |
2404 | (setq type 'vector named 'true))) | |
2405 | (or named (setq descs (delq (assq 'cl-tag-slot descs) descs))) | |
03fef3e6 | 2406 | (push `(defvar ,tag-symbol) forms) |
fcd73769 RS |
2407 | (setq pred-form (and named |
2408 | (let ((pos (- (length descs) | |
2409 | (length (memq (assq 'cl-tag-slot descs) | |
2410 | descs))))) | |
2411 | (if (eq type 'vector) | |
03fef3e6 SM |
2412 | `(and (vectorp cl-x) |
2413 | (>= (length cl-x) ,(length descs)) | |
2414 | (memq (aref cl-x ,pos) ,tag-symbol)) | |
fcd73769 | 2415 | (if (= pos 0) |
03fef3e6 SM |
2416 | `(memq (car-safe cl-x) ,tag-symbol) |
2417 | `(and (consp cl-x) | |
2418 | (memq (nth ,pos cl-x) ,tag-symbol)))))) | |
fcd73769 | 2419 | pred-check (and pred-form (> safety 0) |
7c1898a7 | 2420 | (if (and (eq (cl-caadr pred-form) 'vectorp) |
fcd73769 | 2421 | (= safety 1)) |
7c1898a7 | 2422 | (cons 'and (cl-cdddr pred-form)) pred-form))) |
fcd73769 RS |
2423 | (let ((pos 0) (descp descs)) |
2424 | (while descp | |
69d8fb1e | 2425 | (let* ((desc (pop descp)) |
fcd73769 RS |
2426 | (slot (car desc))) |
2427 | (if (memq slot '(cl-tag-slot cl-skip-slot)) | |
2428 | (progn | |
69d8fb1e | 2429 | (push nil slots) |
03fef3e6 | 2430 | (push (and (eq slot 'cl-tag-slot) `',tag) |
fcd73769 RS |
2431 | defaults)) |
2432 | (if (assq slot descp) | |
2433 | (error "Duplicate slots named %s in %s" slot name)) | |
2434 | (let ((accessor (intern (format "%s%s" conc-name slot)))) | |
69d8fb1e SM |
2435 | (push slot slots) |
2436 | (push (nth 1 desc) defaults) | |
2ee3d7f0 SM |
2437 | (push `(cl-defsubst ,accessor (cl-x) |
2438 | ,@(and pred-check | |
03fef3e6 SM |
2439 | (list `(or ,pred-check |
2440 | (error "%s accessing a non-%s" | |
2441 | ',accessor ',name)))) | |
2ee3d7f0 SM |
2442 | ,(if (eq type 'vector) `(aref cl-x ,pos) |
2443 | (if (= pos 0) '(car cl-x) | |
2444 | `(nth ,pos cl-x)))) forms) | |
69d8fb1e | 2445 | (push (cons accessor t) side-eff) |
94c66ce5 SM |
2446 | (if (cadr (memq :read-only (cddr desc))) |
2447 | (push `(gv-define-expander ,accessor | |
2448 | (lambda (_cl-do _cl-x) | |
2449 | (error "%s is a read-only slot" ',accessor))) | |
2450 | forms) | |
2451 | ;; For normal slots, we don't need to define a setf-expander, | |
2452 | ;; since gv-get can use the compiler macro to get the | |
2453 | ;; same result. | |
2454 | ;; (push `(gv-define-setter ,accessor (cl-val cl-x) | |
2455 | ;; ;; If cl is loaded only for compilation, | |
2456 | ;; ;; the call to cl--struct-setf-expander would | |
2457 | ;; ;; cause a warning because it may not be | |
2458 | ;; ;; defined at run time. Suppress that warning. | |
2459 | ;; (progn | |
2460 | ;; (declare-function | |
2461 | ;; cl--struct-setf-expander "cl-macs" | |
2462 | ;; (x name accessor pred-form pos)) | |
2463 | ;; (cl--struct-setf-expander | |
2464 | ;; cl-val cl-x ',name ',accessor | |
2465 | ;; ,(and pred-check `',pred-check) | |
2466 | ;; ,pos))) | |
2467 | ;; forms) | |
2468 | ) | |
fcd73769 RS |
2469 | (if print-auto |
2470 | (nconc print-func | |
03fef3e6 SM |
2471 | (list `(princ ,(format " %s" slot) cl-s) |
2472 | `(prin1 (,accessor cl-x) cl-s))))))) | |
fcd73769 RS |
2473 | (setq pos (1+ pos)))) |
2474 | (setq slots (nreverse slots) | |
2475 | defaults (nreverse defaults)) | |
2476 | (and predicate pred-form | |
7c1898a7 | 2477 | (progn (push `(cl-defsubst ,predicate (cl-x) |
03fef3e6 SM |
2478 | ,(if (eq (car pred-form) 'and) |
2479 | (append pred-form '(t)) | |
2480 | `(and ,pred-form t))) forms) | |
69d8fb1e | 2481 | (push (cons predicate 'error-free) side-eff))) |
fcd73769 | 2482 | (and copier |
03fef3e6 | 2483 | (progn (push `(defun ,copier (x) (copy-sequence x)) forms) |
69d8fb1e | 2484 | (push (cons copier t) side-eff))) |
fcd73769 | 2485 | (if constructor |
69d8fb1e | 2486 | (push (list constructor |
fcd73769 RS |
2487 | (cons '&key (delq nil (copy-sequence slots)))) |
2488 | constrs)) | |
2489 | (while constrs | |
2490 | (let* ((name (caar constrs)) | |
69d8fb1e | 2491 | (args (cadr (pop constrs))) |
4dd1c416 | 2492 | (anames (cl--arglist-args args)) |
7c1898a7 | 2493 | (make (cl-mapcar (function (lambda (s d) (if (memq s anames) s d))) |
fcd73769 | 2494 | slots defaults))) |
7c1898a7 | 2495 | (push `(cl-defsubst ,name |
03fef3e6 SM |
2496 | (&cl-defs '(nil ,@descs) ,@args) |
2497 | (,type ,@make)) forms) | |
4dd1c416 | 2498 | (if (cl--safe-expr-p `(progn ,@(mapcar #'cl-second descs))) |
69d8fb1e | 2499 | (push (cons name t) side-eff)))) |
fcd73769 | 2500 | (if print-auto (nconc print-func (list '(princ ")" cl-s) t))) |
5a315f9c SM |
2501 | ;; Don't bother adding to cl-custom-print-functions since it's not used |
2502 | ;; by anything anyway! | |
2503 | ;;(if print-func | |
2504 | ;; (push `(if (boundp 'cl-custom-print-functions) | |
2505 | ;; (push | |
2506 | ;; ;; The auto-generated function does not pay attention to | |
2507 | ;; ;; the depth argument cl-n. | |
2508 | ;; (lambda (cl-x cl-s ,(if print-auto '_cl-n 'cl-n)) | |
2509 | ;; (and ,pred-form ,print-func)) | |
2510 | ;; cl-custom-print-functions)) | |
2511 | ;; forms)) | |
03fef3e6 | 2512 | (push `(setq ,tag-symbol (list ',tag)) forms) |
7c1898a7 | 2513 | (push `(cl-eval-when (compile load eval) |
03fef3e6 SM |
2514 | (put ',name 'cl-struct-slots ',descs) |
2515 | (put ',name 'cl-struct-type ',(list type (eq named t))) | |
2516 | (put ',name 'cl-struct-include ',include) | |
2517 | (put ',name 'cl-struct-print ,print-auto) | |
2518 | ,@(mapcar (lambda (x) | |
2519 | `(put ',(car x) 'side-effect-free ',(cdr x))) | |
2520 | side-eff)) | |
2521 | forms) | |
2522 | `(progn ,@(nreverse (cons `',name forms))))) | |
fcd73769 | 2523 | |
fcd73769 RS |
2524 | ;;; Types and assertions. |
2525 | ||
15120dec | 2526 | ;;;###autoload |
7c1898a7 | 2527 | (defmacro cl-deftype (name arglist &rest body) |
64a4c526 | 2528 | "Define NAME as a new data type. |
7c1898a7 SM |
2529 | The type name can then be used in `cl-typecase', `cl-check-type', etc." |
2530 | (declare (debug cl-defmacro) (doc-string 3)) | |
2531 | `(cl-eval-when (compile load eval) | |
4302f5ba SM |
2532 | (put ',name 'cl-deftype-handler |
2533 | (cl-function (lambda (&cl-defs '('*) ,@arglist) ,@body))))) | |
fcd73769 | 2534 | |
4dd1c416 | 2535 | (defun cl--make-type-test (val type) |
fcd73769 RS |
2536 | (if (symbolp type) |
2537 | (cond ((get type 'cl-deftype-handler) | |
4dd1c416 | 2538 | (cl--make-type-test val (funcall (get type 'cl-deftype-handler)))) |
fcd73769 | 2539 | ((memq type '(nil t)) type) |
e0b16322 | 2540 | ((eq type 'null) `(null ,val)) |
578f8106 | 2541 | ((eq type 'atom) `(atom ,val)) |
1dfcc79e | 2542 | ((eq type 'float) `(floatp ,val)) |
e0b16322 SM |
2543 | ((eq type 'real) `(numberp ,val)) |
2544 | ((eq type 'fixnum) `(integerp ,val)) | |
7c1898a7 | 2545 | ;; FIXME: Should `character' accept things like ?\C-\M-a ? --Stef |
31ff0ac1 | 2546 | ((memq type '(character string-char)) `(characterp ,val)) |
fcd73769 RS |
2547 | (t |
2548 | (let* ((name (symbol-name type)) | |
2549 | (namep (intern (concat name "p")))) | |
2550 | (if (fboundp namep) (list namep val) | |
2551 | (list (intern (concat name "-p")) val))))) | |
2552 | (cond ((get (car type) 'cl-deftype-handler) | |
4dd1c416 | 2553 | (cl--make-type-test val (apply (get (car type) 'cl-deftype-handler) |
fcd73769 | 2554 | (cdr type)))) |
e0b16322 | 2555 | ((memq (car type) '(integer float real number)) |
4dd1c416 | 2556 | (delq t `(and ,(cl--make-type-test val (car type)) |
03fef3e6 | 2557 | ,(if (memq (cadr type) '(* nil)) t |
7c1898a7 | 2558 | (if (consp (cadr type)) `(> ,val ,(cl-caadr type)) |
03fef3e6 | 2559 | `(>= ,val ,(cadr type)))) |
7c1898a7 | 2560 | ,(if (memq (cl-caddr type) '(* nil)) t |
338bfefa SM |
2561 | (if (consp (cl-caddr type)) |
2562 | `(< ,val ,(cl-caaddr type)) | |
7c1898a7 | 2563 | `(<= ,val ,(cl-caddr type))))))) |
e0b16322 | 2564 | ((memq (car type) '(and or not)) |
fcd73769 | 2565 | (cons (car type) |
4dd1c416 | 2566 | (mapcar (function (lambda (x) (cl--make-type-test val x))) |
fcd73769 | 2567 | (cdr type)))) |
7c1898a7 SM |
2568 | ((memq (car type) '(member cl-member)) |
2569 | `(and (cl-member ,val ',(cdr type)) t)) | |
e0b16322 | 2570 | ((eq (car type) 'satisfies) (list (cadr type) val)) |
fcd73769 RS |
2571 | (t (error "Bad type spec: %s" type))))) |
2572 | ||
513749ee | 2573 | (defvar cl--object) |
ebacfcc6 | 2574 | ;;;###autoload |
7c1898a7 | 2575 | (defun cl-typep (object type) ; See compiler macro below. |
fcd73769 RS |
2576 | "Check that OBJECT is of type TYPE. |
2577 | TYPE is a Common Lisp-style type specifier." | |
cf4e5178 | 2578 | (declare (compiler-macro cl--compiler-macro-typep)) |
513749ee SM |
2579 | (let ((cl--object object)) ;; Yuck!! |
2580 | (eval (cl--make-type-test 'cl--object type)))) | |
fcd73769 | 2581 | |
cf4e5178 SM |
2582 | (defun cl--compiler-macro-typep (form val type) |
2583 | (if (macroexp-const-p type) | |
2584 | (macroexp-let2 macroexp-copyable-p temp val | |
2585 | (cl--make-type-test temp (cl--const-expr-val type))) | |
2586 | form)) | |
2587 | ||
ebacfcc6 | 2588 | ;;;###autoload |
7c1898a7 | 2589 | (defmacro cl-check-type (form type &optional string) |
fcd73769 RS |
2590 | "Verify that FORM is of type TYPE; signal an error if not. |
2591 | STRING is an optional description of the desired type." | |
b1198e17 | 2592 | (declare (debug (place cl-type-spec &optional stringp))) |
bb3faf5b | 2593 | (and (or (not (cl--compiling-file)) |
338bfefa | 2594 | (< cl--optimize-speed 3) (= cl--optimize-safety 3)) |
4dd1c416 | 2595 | (let* ((temp (if (cl--simple-expr-p form 3) |
e542ea4b | 2596 | form (make-symbol "--cl-var--"))) |
4dd1c416 | 2597 | (body `(or ,(cl--make-type-test temp type) |
03fef3e6 SM |
2598 | (signal 'wrong-type-argument |
2599 | (list ,(or string `',type) | |
2600 | ,temp ',form))))) | |
2601 | (if (eq temp form) `(progn ,body nil) | |
2602 | `(let ((,temp ,form)) ,body nil))))) | |
fcd73769 | 2603 | |
ebacfcc6 | 2604 | ;;;###autoload |
7c1898a7 | 2605 | (defmacro cl-assert (form &optional show-args string &rest args) |
d5c6faf9 | 2606 | ;; FIXME: This is actually not compatible with Common-Lisp's `assert'. |
fcd73769 RS |
2607 | "Verify that FORM returns non-nil; signal an error if not. |
2608 | Second arg SHOW-ARGS means to include arguments of FORM in message. | |
2609 | Other args STRING and ARGS... are arguments to be passed to `error'. | |
2610 | They are not evaluated unless the assertion fails. If STRING is | |
2611 | omitted, a default message listing FORM itself is used." | |
b1198e17 | 2612 | (declare (debug (form &rest form))) |
bb3faf5b | 2613 | (and (or (not (cl--compiling-file)) |
338bfefa | 2614 | (< cl--optimize-speed 3) (= cl--optimize-safety 3)) |
2b88d62a | 2615 | (let ((sargs (and show-args |
4dd1c416 SM |
2616 | (delq nil (mapcar (lambda (x) |
2617 | (unless (macroexp-const-p x) | |
2618 | x)) | |
2619 | (cdr form)))))) | |
03fef3e6 SM |
2620 | `(progn |
2621 | (or ,form | |
2622 | ,(if string | |
2623 | `(error ,string ,@sargs ,@args) | |
2624 | `(signal 'cl-assertion-failed | |
2625 | (list ',form ,@sargs)))) | |
2626 | nil)))) | |
fcd73769 | 2627 | |
fcd73769 RS |
2628 | ;;; Compiler macros. |
2629 | ||
ebacfcc6 | 2630 | ;;;###autoload |
7c1898a7 | 2631 | (defmacro cl-define-compiler-macro (func args &rest body) |
64a4c526 | 2632 | "Define a compiler-only macro. |
fcd73769 RS |
2633 | This is like `defmacro', but macro expansion occurs only if the call to |
2634 | FUNC is compiled (i.e., not interpreted). Compiler macros should be used | |
2635 | for optimizing the way calls to FUNC are compiled; the form returned by | |
2636 | BODY should do the same thing as a call to the normal function called | |
2637 | FUNC, though possibly more efficiently. Note that, like regular macros, | |
2638 | compiler macros are expanded repeatedly until no further expansions are | |
2639 | possible. Unlike regular macros, BODY can decide to \"punt\" and leave the | |
2640 | original function call alone by declaring an initial `&whole foo' parameter | |
2641 | and then returning foo." | |
7c1898a7 | 2642 | (declare (debug cl-defmacro)) |
16c9c10f | 2643 | (let ((p args) (res nil)) |
69d8fb1e | 2644 | (while (consp p) (push (pop p) res)) |
16c9c10f | 2645 | (setq args (nconc (nreverse res) (and p (list '&rest p))))) |
cf4e5178 SM |
2646 | (let ((fname (make-symbol (concat (symbol-name func) "--cmacro")))) |
2647 | `(eval-and-compile | |
2648 | ;; Name the compiler-macro function, so that `symbol-file' can find it. | |
2649 | (cl-defun ,fname ,(if (memq '&whole args) (delq '&whole args) | |
2650 | (cons '_cl-whole-arg args)) | |
2651 | ,@body) | |
2652 | (put ',func 'compiler-macro #',fname)))) | |
fcd73769 | 2653 | |
ebacfcc6 | 2654 | ;;;###autoload |
7c1898a7 | 2655 | (defun cl-compiler-macroexpand (form) |
5593ed90 GM |
2656 | "Like `macroexpand', but for compiler macros. |
2657 | Expands FORM repeatedly until no further expansion is possible. | |
2658 | Returns FORM unchanged if it has no compiler macro, or if it has a | |
2659 | macro that returns its `&whole' argument." | |
fcd73769 RS |
2660 | (while |
2661 | (let ((func (car-safe form)) (handler nil)) | |
2662 | (while (and (symbolp func) | |
57a7d507 | 2663 | (not (setq handler (get func 'compiler-macro))) |
fcd73769 | 2664 | (fboundp func) |
7abaf5cc SM |
2665 | (or (not (autoloadp (symbol-function func))) |
2666 | (autoload-do-load (symbol-function func) func))) | |
fcd73769 RS |
2667 | (setq func (symbol-function func))) |
2668 | (and handler | |
2669 | (not (eq form (setq form (apply handler form (cdr form)))))))) | |
2670 | form) | |
2671 | ||
414dbb00 SM |
2672 | ;; Optimize away unused block-wrappers. |
2673 | ||
4dd1c416 | 2674 | (defvar cl--active-block-names nil) |
414dbb00 | 2675 | |
bb3faf5b | 2676 | (cl-define-compiler-macro cl--block-wrapper (cl-form) |
414dbb00 | 2677 | (let* ((cl-entry (cons (nth 1 (nth 1 cl-form)) nil)) |
4dd1c416 | 2678 | (cl--active-block-names (cons cl-entry cl--active-block-names)) |
414dbb00 SM |
2679 | (cl-body (macroexpand-all ;Performs compiler-macro expansions. |
2680 | (cons 'progn (cddr cl-form)) | |
2681 | macroexpand-all-environment))) | |
2682 | ;; FIXME: To avoid re-applying macroexpand-all, we'd like to be able | |
2683 | ;; to indicate that this return value is already fully expanded. | |
2684 | (if (cdr cl-entry) | |
a08a25d7 | 2685 | `(catch ,(nth 1 cl-form) ,@(cdr cl-body)) |
414dbb00 SM |
2686 | cl-body))) |
2687 | ||
bb3faf5b | 2688 | (cl-define-compiler-macro cl--block-throw (cl-tag cl-value) |
4dd1c416 | 2689 | (let ((cl-found (assq (nth 1 cl-tag) cl--active-block-names))) |
414dbb00 SM |
2690 | (if cl-found (setcdr cl-found t))) |
2691 | `(throw ,cl-tag ,cl-value)) | |
2692 | ||
eb123b12 | 2693 | ;;;###autoload |
7c1898a7 | 2694 | (defmacro cl-defsubst (name args &rest body) |
69d8fb1e | 2695 | "Define NAME as a function. |
fcd73769 RS |
2696 | Like `defun', except the function is automatically declared `inline', |
2697 | ARGLIST allows full Common Lisp conventions, and BODY is implicitly | |
7c1898a7 | 2698 | surrounded by (cl-block NAME ...). |
69d8fb1e SM |
2699 | |
2700 | \(fn NAME ARGLIST [DOCSTRING] BODY...)" | |
2ee3d7f0 | 2701 | (declare (debug cl-defun) (indent 2)) |
4dd1c416 | 2702 | (let* ((argns (cl--arglist-args args)) (p argns) |
fcd73769 | 2703 | (pbody (cons 'progn body)) |
4dd1c416 SM |
2704 | (unsafe (not (cl--safe-expr-p pbody)))) |
2705 | (while (and p (eq (cl--expr-contains args (car p)) 1)) (pop p)) | |
03fef3e6 SM |
2706 | `(progn |
2707 | ,(if p nil ; give up if defaults refer to earlier args | |
7c1898a7 | 2708 | `(cl-define-compiler-macro ,name |
03fef3e6 SM |
2709 | ,(if (memq '&key args) |
2710 | `(&whole cl-whole &cl-quote ,@args) | |
2711 | (cons '&cl-quote args)) | |
bb3faf5b | 2712 | (cl--defsubst-expand |
7c1898a7 | 2713 | ',argns '(cl-block ,name ,@body) |
03fef3e6 SM |
2714 | ;; We used to pass `simple' as |
2715 | ;; (not (or unsafe (cl-expr-access-order pbody argns))) | |
2716 | ;; But this is much too simplistic since it | |
2717 | ;; does not pay attention to the argvs (and | |
2718 | ;; cl-expr-access-order itself is also too naive). | |
2719 | nil | |
2720 | ,(and (memq '&key args) 'cl-whole) ,unsafe ,@argns))) | |
7c1898a7 | 2721 | (cl-defun ,name ,args ,@body)))) |
fcd73769 | 2722 | |
bb3faf5b | 2723 | (defun cl--defsubst-expand (argns body simple whole unsafe &rest argvs) |
4dd1c416 SM |
2724 | (if (and whole (not (cl--safe-expr-p (cons 'progn argvs)))) whole |
2725 | (if (cl--simple-exprs-p argvs) (setq simple t)) | |
e754e83b SM |
2726 | (let* ((substs ()) |
2727 | (lets (delq nil | |
7c1898a7 | 2728 | (cl-mapcar (lambda (argn argv) |
4dd1c416 | 2729 | (if (or simple (macroexp-const-p argv)) |
7c1898a7 SM |
2730 | (progn (push (cons argn argv) substs) |
2731 | (and unsafe (list argn argv))) | |
2732 | (list argn argv))) | |
2733 | argns argvs)))) | |
e754e83b SM |
2734 | ;; FIXME: `sublis/subst' will happily substitute the symbol |
2735 | ;; `argn' in places where it's not used as a reference | |
2736 | ;; to a variable. | |
2737 | ;; FIXME: `sublis/subst' will happily copy `argv' to a different | |
2738 | ;; scope, leading to name capture. | |
2739 | (setq body (cond ((null substs) body) | |
2740 | ((null (cdr substs)) | |
7c1898a7 | 2741 | (cl-subst (cdar substs) (caar substs) body)) |
aff6371e | 2742 | (t (cl--sublis substs body)))) |
03fef3e6 | 2743 | (if lets `(let ,lets ,body) body)))) |
fcd73769 | 2744 | |
aff6371e SM |
2745 | (defun cl--sublis (alist tree) |
2746 | "Perform substitutions indicated by ALIST in TREE (non-destructively)." | |
2747 | (let ((x (assq tree alist))) | |
2748 | (cond | |
2749 | (x (cdr x)) | |
2750 | ((consp tree) | |
2751 | (cons (cl--sublis alist (car tree)) (cl--sublis alist (cdr tree)))) | |
2752 | (t tree)))) | |
fcd73769 | 2753 | |
9b77469a | 2754 | ;; Compile-time optimizations for some functions defined in this package. |
fcd73769 | 2755 | |
d9857e53 | 2756 | (defun cl--compiler-macro-member (form a list &rest keys) |
64a4c526 | 2757 | (let ((test (and (= (length keys) 2) (eq (car keys) :test) |
4dd1c416 | 2758 | (cl--const-expr-val (nth 1 keys))))) |
03fef3e6 SM |
2759 | (cond ((eq test 'eq) `(memq ,a ,list)) |
2760 | ((eq test 'equal) `(member ,a ,list)) | |
2761 | ((or (null keys) (eq test 'eql)) `(memql ,a ,list)) | |
fcd73769 RS |
2762 | (t form)))) |
2763 | ||
d9857e53 | 2764 | (defun cl--compiler-macro-assoc (form a list &rest keys) |
64a4c526 | 2765 | (let ((test (and (= (length keys) 2) (eq (car keys) :test) |
4dd1c416 | 2766 | (cl--const-expr-val (nth 1 keys))))) |
03fef3e6 SM |
2767 | (cond ((eq test 'eq) `(assq ,a ,list)) |
2768 | ((eq test 'equal) `(assoc ,a ,list)) | |
4dd1c416 | 2769 | ((and (macroexp-const-p a) (or (null keys) (eq test 'eql))) |
1dfcc79e | 2770 | (if (floatp (cl--const-expr-val a)) |
03fef3e6 | 2771 | `(assoc ,a ,list) `(assq ,a ,list))) |
fcd73769 RS |
2772 | (t form)))) |
2773 | ||
71adb94b | 2774 | ;;;###autoload |
d9857e53 | 2775 | (defun cl--compiler-macro-adjoin (form a list &rest keys) |
208d0342 SM |
2776 | (if (memq :key keys) form |
2777 | (macroexp-let2 macroexp-copyable-p va a | |
2778 | (macroexp-let2 macroexp-copyable-p vlist list | |
2779 | `(if (cl-member ,va ,vlist ,@keys) ,vlist (cons ,va ,vlist)))))) | |
fcd73769 | 2780 | |
d9857e53 | 2781 | (defun cl--compiler-macro-get (_form sym prop &optional def) |
fcd73769 | 2782 | (if def |
7c1898a7 | 2783 | `(cl-getf (symbol-plist ,sym) ,prop ,def) |
03fef3e6 | 2784 | `(get ,sym ,prop))) |
fcd73769 | 2785 | |
71adb94b SM |
2786 | (dolist (y '(cl-first cl-second cl-third cl-fourth |
2787 | cl-fifth cl-sixth cl-seventh | |
2788 | cl-eighth cl-ninth cl-tenth | |
2789 | cl-rest cl-endp cl-plusp cl-minusp | |
2790 | cl-caaar cl-caadr cl-cadar | |
2791 | cl-caddr cl-cdaar cl-cdadr | |
2792 | cl-cddar cl-cdddr cl-caaaar | |
2793 | cl-caaadr cl-caadar cl-caaddr | |
2794 | cl-cadaar cl-cadadr cl-caddar | |
2795 | cl-cadddr cl-cdaaar cl-cdaadr | |
2796 | cl-cdadar cl-cdaddr cl-cddaar | |
2797 | cl-cddadr cl-cdddar cl-cddddr)) | |
2798 | (put y 'side-effect-free t)) | |
fcd73769 RS |
2799 | |
2800 | ;;; Things that are inline. | |
1dfcc79e | 2801 | (cl-proclaim '(inline cl-acons cl-map cl-concatenate cl-notany |
2ee3d7f0 | 2802 | cl-notevery cl--set-elt cl-revappend cl-nreconc gethash)) |
fcd73769 RS |
2803 | |
2804 | ;;; Things that are side-effect-free. | |
e542ea4b | 2805 | (mapc (lambda (x) (put x 'side-effect-free t)) |
338bfefa SM |
2806 | '(cl-oddp cl-evenp cl-signum last butlast cl-ldiff cl-pairlis cl-gcd |
2807 | cl-lcm cl-isqrt cl-floor cl-ceiling cl-truncate cl-round cl-mod cl-rem | |
2808 | cl-subseq cl-list-length cl-get cl-getf)) | |
fcd73769 RS |
2809 | |
2810 | ;;; Things that are side-effect-and-error-free. | |
e542ea4b | 2811 | (mapc (lambda (x) (put x 'side-effect-free 'error-free)) |
1dfcc79e | 2812 | '(eql cl-list* cl-subst cl-acons cl-equalp |
338bfefa | 2813 | cl-random-state-p copy-tree cl-sublis)) |
fcd73769 RS |
2814 | |
2815 | ||
2816 | (run-hooks 'cl-macs-load-hook) | |
2817 | ||
ebacfcc6 | 2818 | ;; Local variables: |
bc8ce89b | 2819 | ;; byte-compile-dynamic: t |
ebacfcc6 SM |
2820 | ;; generated-autoload-file: "cl-loaddefs.el" |
2821 | ;; End: | |
b69a3374 | 2822 | |
de7e2b36 SM |
2823 | (provide 'cl-macs) |
2824 | ||
fcd73769 | 2825 | ;;; cl-macs.el ends here |