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