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