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