Merge: Import crypto/md5 and stdint modules from gnulib.
[bpt/emacs.git] / lisp / emacs-lisp / cl.el
CommitLineData
87787319 1;;; cl.el --- Common Lisp extensions for Emacs
fcd73769 2
73b0cd50 3;; Copyright (C) 1993, 2001-2011 Free Software Foundation, Inc.
fcd73769
RS
4
5;; Author: Dave Gillespie <daveg@synaptics.com>
6;; Version: 2.02
7;; Keywords: extensions
8
9;; This file is part of GNU Emacs.
10
d6cba7ae 11;; GNU Emacs is free software: you can redistribute it and/or modify
fcd73769 12;; it under the terms of the GNU General Public License as published by
d6cba7ae
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
fcd73769
RS
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
d6cba7ae 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
fcd73769 23
07b3798c 24;;; Commentary:
fcd73769
RS
25
26;; These are extensions to Emacs Lisp that provide a degree of
27;; Common Lisp compatibility, beyond what is already built-in
28;; in Emacs Lisp.
29;;
30;; This package was written by Dave Gillespie; it is a complete
31;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
32;;
fcd73769
RS
33;; Bug reports, comments, and suggestions are welcome!
34
35;; This file contains the portions of the Common Lisp extensions
36;; package which should always be present.
37
38
07b3798c 39;;; Future notes:
fcd73769
RS
40
41;; Once Emacs 19 becomes standard, many things in this package which are
42;; messy for reasons of compatibility can be greatly simplified. For now,
43;; I prefer to maintain one unified version.
44
45
07b3798c 46;;; Change Log:
fcd73769
RS
47
48;; Version 2.02 (30 Jul 93):
49;; * Added "cl-compat.el" file, extra compatibility with old package.
50;; * Added `lexical-let' and `lexical-let*'.
51;; * Added `define-modify-macro', `callf', and `callf2'.
52;; * Added `ignore-errors'.
53;; * Changed `(setf (nthcdr N PLACE) X)' to work when N is zero.
54;; * Merged `*gentemp-counter*' into `*gensym-counter*'.
55;; * Extended `subseq' to allow negative START and END like `substring'.
56;; * Added `in-ref', `across-ref', `elements of-ref' loop clauses.
57;; * Added `concat', `vconcat' loop clauses.
58;; * Cleaned up a number of compiler warnings.
59
60;; Version 2.01 (7 Jul 93):
61;; * Added support for FSF version of Emacs 19.
62;; * Added `add-hook' for Emacs 18 users.
63;; * Added `defsubst*' and `symbol-macrolet'.
64;; * Added `maplist', `mapc', `mapl', `mapcan', `mapcon'.
65;; * Added `map', `concatenate', `reduce', `merge'.
66;; * Added `revappend', `nreconc', `tailp', `tree-equal'.
67;; * Added `assert', `check-type', `typecase', `typep', and `deftype'.
68;; * Added destructuring and `&environment' support to `defmacro*'.
69;; * Added destructuring to `loop', and added the following clauses:
70;; `elements', `frames', `overlays', `intervals', `buffers', `key-seqs'.
71;; * Renamed `delete' to `delete*' and `remove' to `remove*'.
72;; * Completed support for all keywords in `remove*', `substitute', etc.
73;; * Added `most-positive-float' and company.
74;; * Fixed hash tables to work with latest Lucid Emacs.
75;; * `proclaim' forms are no longer compile-time-evaluating; use `declaim'.
76;; * Syntax for `warn' declarations has changed.
77;; * Improved implementation of `random*'.
78;; * Moved most sequence functions to a new file, cl-seq.el.
79;; * Moved `eval-when' into cl-macs.el.
80;; * Moved `pushnew' and `adjoin' to cl.el for most common cases.
81;; * Moved `provide' forms down to ends of files.
82;; * Changed expansion of `pop' to something that compiles to better code.
83;; * Changed so that no patch is required for Emacs 19 byte compiler.
84;; * Made more things dependent on `optimize' declarations.
85;; * Added a partial implementation of struct print functions.
86;; * Miscellaneous minor changes.
87
88;; Version 2.00:
89;; * First public release of this package.
90
91
07b3798c 92;;; Code:
fcd73769 93
fcd73769
RS
94(defvar cl-optimize-speed 1)
95(defvar cl-optimize-safety 1)
96
97
7dcf6269 98;;;###autoload
fcd73769
RS
99(defvar custom-print-functions nil
100 "This is a list of functions that format user objects for printing.
101Each function is called in turn with three arguments: the object, the
102stream, and the print level (currently ignored). If it is able to
103print the object it returns true; otherwise it returns nil and the
104printer proceeds to the next function on the list.
105
106This variable is not used at present, but it is defined in hopes that
107a future Emacs interpreter will be able to use it.")
108
66eed827
JB
109(defun cl-unload-function ()
110 "Stop unloading of the Common Lisp extensions."
111 (message "Cannot unload the feature `cl'")
112 ;; stop standard unloading!
113 t)
fcd73769 114
b68f6e48
SM
115;;; Generalized variables.
116;; These macros are defined here so that they
117;; can safely be used in .emacs files.
fcd73769
RS
118
119(defmacro incf (place &optional x)
64a4c526 120 "Increment PLACE by X (1 by default).
fcd73769
RS
121PLACE may be a symbol, or any generalized variable allowed by `setf'.
122The return value is the incremented value of PLACE."
123 (if (symbolp place)
124 (list 'setq place (if x (list '+ place x) (list '1+ place)))
125 (list 'callf '+ place (or x 1))))
126
127(defmacro decf (place &optional x)
64a4c526 128 "Decrement PLACE by X (1 by default).
fcd73769
RS
129PLACE may be a symbol, or any generalized variable allowed by `setf'.
130The return value is the decremented value of PLACE."
131 (if (symbolp place)
132 (list 'setq place (if x (list '- place x) (list '1- place)))
133 (list 'callf '- place (or x 1))))
134
7467c796
GM
135;; Autoloaded, but we haven't loaded cl-loaddefs yet.
136(declare-function cl-do-pop "cl-macs" (place))
137
fcd73769 138(defmacro pop (place)
64a4c526 139 "Remove and return the head of the list stored in PLACE.
fcd73769
RS
140Analogous to (prog1 (car PLACE) (setf PLACE (cdr PLACE))), though more
141careful about evaluating each argument only once and in the right order.
142PLACE may be a symbol, or any generalized variable allowed by `setf'."
143 (if (symbolp place)
144 (list 'car (list 'prog1 place (list 'setq place (list 'cdr place))))
145 (cl-do-pop place)))
146
147(defmacro push (x place)
64a4c526 148 "Insert X at the head of the list stored in PLACE.
fcd73769
RS
149Analogous to (setf PLACE (cons X PLACE)), though more careful about
150evaluating each argument only once and in the right order. PLACE may
151be a symbol, or any generalized variable allowed by `setf'."
152 (if (symbolp place) (list 'setq place (list 'cons x place))
153 (list 'callf2 'cons x place)))
154
155(defmacro pushnew (x place &rest keys)
156 "(pushnew X PLACE): insert X at the head of the list if not already there.
157Like (push X PLACE), except that the list is unmodified if X is `eql' to
158an element already on the list.
708c63a6
JB
159\nKeywords supported: :test :test-not :key
160\n(fn X PLACE [KEYWORD VALUE]...)"
6b34950f
RS
161 (if (symbolp place)
162 (if (null keys)
fcecceea
KS
163 `(let ((x ,x))
164 (if (memql x ,place) ,place (setq ,place (cons x ,place))))
6b34950f 165 (list 'setq place (list* 'adjoin x place keys)))
fcd73769
RS
166 (list* 'callf2 'adjoin x place keys)))
167
168(defun cl-set-elt (seq n val)
169 (if (listp seq) (setcar (nthcdr n seq) val) (aset seq n val)))
170
4ded1ddb 171(defsubst cl-set-nthcdr (n list x)
fcd73769
RS
172 (if (<= n 0) x (setcdr (nthcdr (1- n) list) x) list))
173
174(defun cl-set-buffer-substring (start end val)
175 (save-excursion (delete-region start end)
176 (goto-char start)
177 (insert val)
178 val))
179
180(defun cl-set-substring (str start end val)
181 (if end (if (< end 0) (incf end (length str)))
182 (setq end (length str)))
87dd507f 183 (if (< start 0) (incf start (length str)))
fcd73769
RS
184 (concat (and (> start 0) (substring str 0 start))
185 val
186 (and (< end (length str)) (substring str end))))
187
188
189;;; Control structures.
190
b68f6e48
SM
191;; These macros are so simple and so often-used that it's better to have
192;; them all the time than to load them from cl-macs.el.
fcd73769 193
fcd73769 194(defun cl-map-extents (&rest cl-args)
f67171e6 195 (apply 'cl-map-overlays cl-args))
fcd73769
RS
196
197
198;;; Blocks and exits.
199
200(defalias 'cl-block-wrapper 'identity)
201(defalias 'cl-block-throw 'throw)
202
203
b68f6e48
SM
204;;; Multiple values.
205;; True multiple values are not supported, or even
206;; simulated. Instead, multiple-value-bind and friends simply expect
207;; the target form to return the values as a list.
fcd73769 208
413da451
RS
209(defsubst values (&rest values)
210 "Return multiple values, Common Lisp style.
211The arguments of `values' are the values
212that the containing function should return."
8f7ef366 213 values)
413da451
RS
214
215(defsubst values-list (list)
216 "Return multiple values, Common Lisp style, taken from a list.
217LIST specifies the list of values
218that the containing function should return."
219 list)
220
221(defsubst multiple-value-list (expression)
222 "Return a list of the multiple values produced by EXPRESSION.
223This handles multiple values in Common Lisp style, but it does not
224work right when EXPRESSION calls an ordinary Emacs Lisp function
225that returns just one value."
226 expression)
227
228(defsubst multiple-value-apply (function expression)
229 "Evaluate EXPRESSION to get multiple values and apply FUNCTION to them.
230This handles multiple values in Common Lisp style, but it does not work
231right when EXPRESSION calls an ordinary Emacs Lisp function that returns just
232one value."
233 (apply function expression))
234
a2e74caa
SM
235(defalias 'multiple-value-call 'apply
236 "Apply FUNCTION to ARGUMENTS, taking multiple values into account.
237This implementation only handles the case where there is only one argument.")
f988b541 238
413da451
RS
239(defsubst nth-value (n expression)
240 "Evaluate EXPRESSION to get multiple values and return the Nth one.
241This handles multiple values in Common Lisp style, but it does not work
242right when EXPRESSION calls an ordinary Emacs Lisp function that returns just
243one value."
244 (nth n expression))
fcd73769
RS
245
246;;; Macros.
247
7eb73deb 248(defvar cl-macro-environment)
fcd73769
RS
249(defvar cl-old-macroexpand (prog1 (symbol-function 'macroexpand)
250 (defalias 'macroexpand 'cl-macroexpand)))
251
252(defun cl-macroexpand (cl-macro &optional cl-env)
2a8160e6
RS
253 "Return result of expanding macros at top level of FORM.
254If FORM is not a macro call, it is returned unchanged.
255Otherwise, the macro is expanded and the expansion is considered
256in place of FORM. When a non-macro-call results, it is returned.
257
f15e298c 258The second optional arg ENVIRONMENT specifies an environment of macro
708c63a6
JB
259definitions to shadow the loaded ones for use in file byte-compilation.
260\n(fn FORM &optional ENVIRONMENT)"
fcd73769
RS
261 (let ((cl-macro-environment cl-env))
262 (while (progn (setq cl-macro (funcall cl-old-macroexpand cl-macro cl-env))
263 (and (symbolp cl-macro)
264 (cdr (assq (symbol-name cl-macro) cl-env))))
265 (setq cl-macro (cadr (assq (symbol-name cl-macro) cl-env))))
266 cl-macro))
267
268
269;;; Declarations.
270
271(defvar cl-compiling-file nil)
272(defun cl-compiling-file ()
273 (or cl-compiling-file
4b949062
GM
274 (and (boundp 'bytecomp-outbuffer)
275 (bufferp (symbol-value 'bytecomp-outbuffer))
276 (equal (buffer-name (symbol-value 'bytecomp-outbuffer))
fcd73769
RS
277 " *Compiler Output*"))))
278
279(defvar cl-proclaims-deferred nil)
280
281(defun proclaim (spec)
282 (if (fboundp 'cl-do-proclaim) (cl-do-proclaim spec t)
283 (push spec cl-proclaims-deferred))
284 nil)
285
286(defmacro declaim (&rest specs)
287 (let ((body (mapcar (function (lambda (x) (list 'proclaim (list 'quote x))))
288 specs)))
289 (if (cl-compiling-file) (list* 'eval-when '(compile load eval) body)
290 (cons 'progn body)))) ; avoid loading cl-macs.el for eval-when
291
292
293;;; Symbols.
294
295(defun cl-random-time ()
296 (let* ((time (copy-sequence (current-time-string))) (i (length time)) (v 0))
297 (while (>= (decf i) 0) (setq v (+ (* v 3) (aref time i))))
298 v))
299
300(defvar *gensym-counter* (* (logand (cl-random-time) 1023) 100))
301
302
303;;; Numbers.
304
708c63a6 305(defun floatp-safe (object)
f4db41fc 306 "Return t if OBJECT is a floating point number.
fcd73769
RS
307On Emacs versions that lack floating-point support, this function
308always returns nil."
708c63a6 309 (and (numberp object) (not (integerp object))))
fcd73769 310
708c63a6 311(defun plusp (number)
f4db41fc 312 "Return t if NUMBER is positive."
708c63a6 313 (> number 0))
fcd73769 314
708c63a6 315(defun minusp (number)
f4db41fc 316 "Return t if NUMBER is negative."
708c63a6 317 (< number 0))
fcd73769 318
708c63a6 319(defun oddp (integer)
f4db41fc 320 "Return t if INTEGER is odd."
708c63a6 321 (eq (logand integer 1) 1))
fcd73769 322
708c63a6 323(defun evenp (integer)
f4db41fc 324 "Return t if INTEGER is even."
708c63a6 325 (eq (logand integer 1) 0))
fcd73769 326
fcd73769
RS
327(defvar *random-state* (vector 'cl-random-state-tag -1 30 (cl-random-time)))
328
b68f6e48 329;; The following are actually set by cl-float-limits.
fcd73769
RS
330(defconst most-positive-float nil)
331(defconst most-negative-float nil)
332(defconst least-positive-float nil)
333(defconst least-negative-float nil)
334(defconst least-positive-normalized-float nil)
335(defconst least-negative-normalized-float nil)
336(defconst float-epsilon nil)
337(defconst float-negative-epsilon nil)
338
339
340;;; Sequence functions.
341
342(defalias 'copy-seq 'copy-sequence)
343
7467c796
GM
344(declare-function cl-mapcar-many "cl-extra" (cl-func cl-seqs))
345
fcd73769
RS
346(defun mapcar* (cl-func cl-x &rest cl-rest)
347 "Apply FUNCTION to each element of SEQ, and make a list of the results.
348If there are several SEQs, FUNCTION is called with that many arguments,
349and mapping stops as soon as the shortest list runs out. With just one
350SEQ, this is like `mapcar'. With several, it is like the Common Lisp
708c63a6
JB
351`mapcar' function extended to arbitrary sequence types.
352\n(fn FUNCTION SEQ...)"
fcd73769
RS
353 (if cl-rest
354 (if (or (cdr cl-rest) (nlistp cl-x) (nlistp (car cl-rest)))
355 (cl-mapcar-many cl-func (cons cl-x cl-rest))
356 (let ((cl-res nil) (cl-y (car cl-rest)))
357 (while (and cl-x cl-y)
358 (push (funcall cl-func (pop cl-x) (pop cl-y)) cl-res))
359 (nreverse cl-res)))
360 (mapcar cl-func cl-x)))
361
686d0681 362(defalias 'svref 'aref)
fcd73769
RS
363
364;;; List functions.
365
366(defalias 'first 'car)
dc79c3ea 367(defalias 'second 'cadr)
fcd73769
RS
368(defalias 'rest 'cdr)
369(defalias 'endp 'null)
370
dc79c3ea 371(defun third (x)
b7a90344
SM
372 "Return the third element of the list X."
373 (car (cdr (cdr x))))
fcd73769 374
dc79c3ea 375(defun fourth (x)
b7a90344
SM
376 "Return the fourth element of the list X."
377 (nth 3 x))
fcd73769 378
dc79c3ea 379(defun fifth (x)
b7a90344
SM
380 "Return the fifth element of the list X."
381 (nth 4 x))
fcd73769 382
dc79c3ea 383(defun sixth (x)
b7a90344
SM
384 "Return the sixth element of the list X."
385 (nth 5 x))
fcd73769 386
dc79c3ea 387(defun seventh (x)
b7a90344
SM
388 "Return the seventh element of the list X."
389 (nth 6 x))
fcd73769 390
dc79c3ea 391(defun eighth (x)
b7a90344
SM
392 "Return the eighth element of the list X."
393 (nth 7 x))
fcd73769 394
dc79c3ea 395(defun ninth (x)
b7a90344
SM
396 "Return the ninth element of the list X."
397 (nth 8 x))
fcd73769 398
dc79c3ea 399(defun tenth (x)
b7a90344
SM
400 "Return the tenth element of the list X."
401 (nth 9 x))
fcd73769 402
fcd73769
RS
403(defun caaar (x)
404 "Return the `car' of the `car' of the `car' of X."
405 (car (car (car x))))
406
407(defun caadr (x)
408 "Return the `car' of the `car' of the `cdr' of X."
409 (car (car (cdr x))))
410
411(defun cadar (x)
412 "Return the `car' of the `cdr' of the `car' of X."
413 (car (cdr (car x))))
414
415(defun caddr (x)
416 "Return the `car' of the `cdr' of the `cdr' of X."
417 (car (cdr (cdr x))))
418
419(defun cdaar (x)
420 "Return the `cdr' of the `car' of the `car' of X."
421 (cdr (car (car x))))
422
423(defun cdadr (x)
424 "Return the `cdr' of the `car' of the `cdr' of X."
425 (cdr (car (cdr x))))
426
427(defun cddar (x)
428 "Return the `cdr' of the `cdr' of the `car' of X."
429 (cdr (cdr (car x))))
430
431(defun cdddr (x)
432 "Return the `cdr' of the `cdr' of the `cdr' of X."
433 (cdr (cdr (cdr x))))
434
435(defun caaaar (x)
436 "Return the `car' of the `car' of the `car' of the `car' of X."
437 (car (car (car (car x)))))
438
439(defun caaadr (x)
440 "Return the `car' of the `car' of the `car' of the `cdr' of X."
441 (car (car (car (cdr x)))))
442
443(defun caadar (x)
444 "Return the `car' of the `car' of the `cdr' of the `car' of X."
445 (car (car (cdr (car x)))))
446
447(defun caaddr (x)
448 "Return the `car' of the `car' of the `cdr' of the `cdr' of X."
449 (car (car (cdr (cdr x)))))
450
451(defun cadaar (x)
452 "Return the `car' of the `cdr' of the `car' of the `car' of X."
453 (car (cdr (car (car x)))))
454
455(defun cadadr (x)
456 "Return the `car' of the `cdr' of the `car' of the `cdr' of X."
457 (car (cdr (car (cdr x)))))
458
459(defun caddar (x)
460 "Return the `car' of the `cdr' of the `cdr' of the `car' of X."
461 (car (cdr (cdr (car x)))))
462
463(defun cadddr (x)
464 "Return the `car' of the `cdr' of the `cdr' of the `cdr' of X."
465 (car (cdr (cdr (cdr x)))))
466
467(defun cdaaar (x)
468 "Return the `cdr' of the `car' of the `car' of the `car' of X."
469 (cdr (car (car (car x)))))
470
471(defun cdaadr (x)
472 "Return the `cdr' of the `car' of the `car' of the `cdr' of X."
473 (cdr (car (car (cdr x)))))
474
475(defun cdadar (x)
476 "Return the `cdr' of the `car' of the `cdr' of the `car' of X."
477 (cdr (car (cdr (car x)))))
478
479(defun cdaddr (x)
480 "Return the `cdr' of the `car' of the `cdr' of the `cdr' of X."
481 (cdr (car (cdr (cdr x)))))
482
483(defun cddaar (x)
484 "Return the `cdr' of the `cdr' of the `car' of the `car' of X."
485 (cdr (cdr (car (car x)))))
486
487(defun cddadr (x)
488 "Return the `cdr' of the `cdr' of the `car' of the `cdr' of X."
489 (cdr (cdr (car (cdr x)))))
490
491(defun cdddar (x)
492 "Return the `cdr' of the `cdr' of the `cdr' of the `car' of X."
493 (cdr (cdr (cdr (car x)))))
494
495(defun cddddr (x)
496 "Return the `cdr' of the `cdr' of the `cdr' of the `cdr' of X."
497 (cdr (cdr (cdr (cdr x)))))
498
f9efebca
RS
499;;(defun last* (x &optional n)
500;; "Returns the last link in the list LIST.
501;;With optional argument N, returns Nth-to-last link (default 1)."
502;; (if n
503;; (let ((m 0) (p x))
504;; (while (consp p) (incf m) (pop p))
505;; (if (<= n 0) p
506;; (if (< n m) (nthcdr (- m n) x) x)))
507;; (while (consp (cdr x)) (pop x))
508;; x))
fcd73769 509
fcd73769 510(defun list* (arg &rest rest) ; See compiler macro in cl-macs.el
708c63a6 511 "Return a new list with specified ARGs as elements, consed to last ARG.
fcd73769 512Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
708c63a6
JB
513`(cons A (cons B (cons C D)))'.
514\n(fn ARG...)"
fcd73769
RS
515 (cond ((not rest) arg)
516 ((not (cdr rest)) (cons arg (car rest)))
517 (t (let* ((n (length rest))
518 (copy (copy-sequence rest))
519 (last (nthcdr (- n 2) copy)))
520 (setcdr last (car (cdr last)))
521 (cons arg copy)))))
522
523(defun ldiff (list sublist)
524 "Return a copy of LIST with the tail SUBLIST removed."
525 (let ((res nil))
526 (while (and (consp list) (not (eq list sublist)))
527 (push (pop list) res))
528 (nreverse res)))
529
6b25a2f5 530(defun copy-list (list)
708c63a6
JB
531 "Return a copy of LIST, which may be a dotted list.
532The elements of LIST are not copied, just the list structure itself."
6b25a2f5
RS
533 (if (consp list)
534 (let ((res nil))
535 (while (consp list) (push (pop list) res))
536 (prog1 (nreverse res) (setcdr res list)))
537 (car list)))
538
fcd73769
RS
539(defun cl-maclisp-member (item list)
540 (while (and list (not (equal item (car list)))) (setq list (cdr list)))
541 list)
542
fcd73769 543(defalias 'cl-member 'memq) ; for compatibility with old CL package
ec302593
GM
544
545;; Autoloaded, but we have not loaded cl-loaddefs yet.
546(declare-function floor* "cl-extra" (x &optional y))
547(declare-function ceiling* "cl-extra" (x &optional y))
548(declare-function truncate* "cl-extra" (x &optional y))
549(declare-function round* "cl-extra" (x &optional y))
550(declare-function mod* "cl-extra" (x y))
551
fcd73769
RS
552(defalias 'cl-floor 'floor*)
553(defalias 'cl-ceiling 'ceiling*)
554(defalias 'cl-truncate 'truncate*)
555(defalias 'cl-round 'round*)
556(defalias 'cl-mod 'mod*)
557
558(defun adjoin (cl-item cl-list &rest cl-keys) ; See compiler macro in cl-macs
559 "Return ITEM consed onto the front of LIST only if it's not already there.
560Otherwise, return LIST unmodified.
708c63a6
JB
561\nKeywords supported: :test :test-not :key
562\n(fn ITEM LIST [KEYWORD VALUE]...)"
fcd73769
RS
563 (cond ((or (equal cl-keys '(:test eq))
564 (and (null cl-keys) (not (numberp cl-item))))
565 (if (memq cl-item cl-list) cl-list (cons cl-item cl-list)))
566 ((or (equal cl-keys '(:test equal)) (null cl-keys))
567 (if (member cl-item cl-list) cl-list (cons cl-item cl-list)))
568 (t (apply 'cl-adjoin cl-item cl-list cl-keys))))
569
570(defun subst (cl-new cl-old cl-tree &rest cl-keys)
571 "Substitute NEW for OLD everywhere in TREE (non-destructively).
572Return a copy of TREE with all elements `eql' to OLD replaced by NEW.
708c63a6
JB
573\nKeywords supported: :test :test-not :key
574\n(fn NEW OLD TREE [KEYWORD VALUE]...)"
fcd73769
RS
575 (if (or cl-keys (and (numberp cl-old) (not (integerp cl-old))))
576 (apply 'sublis (list (cons cl-old cl-new)) cl-tree cl-keys)
577 (cl-do-subst cl-new cl-old cl-tree)))
578
579(defun cl-do-subst (cl-new cl-old cl-tree)
580 (cond ((eq cl-tree cl-old) cl-new)
581 ((consp cl-tree)
582 (let ((a (cl-do-subst cl-new cl-old (car cl-tree)))
583 (d (cl-do-subst cl-new cl-old (cdr cl-tree))))
584 (if (and (eq a (car cl-tree)) (eq d (cdr cl-tree)))
585 cl-tree (cons a d))))
586 (t cl-tree)))
587
868904eb
JB
588(defun acons (key value alist)
589 "Add KEY and VALUE to ALIST.
590Return a new list with (cons KEY VALUE) as car and ALIST as cdr."
591 (cons (cons key value) alist))
592
593(defun pairlis (keys values &optional alist)
594 "Make an alist from KEYS and VALUES.
595Return a new alist composed by associating KEYS to corresponding VALUES;
596the process stops as soon as KEYS or VALUES run out.
597If ALIST is non-nil, the new pairs are prepended to it."
598 (nconc (mapcar* 'cons keys values) alist))
fcd73769
RS
599
600
601;;; Miscellaneous.
602
b68f6e48 603;; Define data for indentation and edebug.
dbc65a5e
SM
604(dolist (entry
605 '(((defun* defmacro*) 2)
606 ((function*) nil
607 (&or symbolp ([&optional 'macro] 'lambda (&rest sexp) &rest form)))
608 ((eval-when) 1 (sexp &rest form))
609 ((declare) nil (&rest sexp))
610 ((the) 1 (sexp &rest form))
611 ((case ecase typecase etypecase) 1 (form &rest (sexp &rest form)))
612 ((block return-from) 1 (sexp &rest form))
613 ((return) nil (&optional form))
614 ((do do*) 2 ((&rest &or symbolp (symbolp &optional form form))
615 (form &rest form)
616 &rest form))
617 ((do-symbols) 1 ((symbolp form &optional form form) &rest form))
618 ((do-all-symbols) 1 ((symbolp form &optional form) &rest form))
619 ((psetq setf psetf) nil edebug-setq-form)
620 ((progv) 2 (&rest form))
621 ((flet labels macrolet) 1
622 ((&rest (sexp sexp &rest form)) &rest form))
623 ((symbol-macrolet lexical-let lexical-let*) 1
624 ((&rest &or symbolp (symbolp form)) &rest form))
625 ((multiple-value-bind) 2 ((&rest symbolp) &rest form))
626 ((multiple-value-setq) 1 ((&rest symbolp) &rest form))
627 ((incf decf remf pushnew shiftf rotatef) nil (&rest form))
628 ((letf letf*) 1 ((&rest (&rest form)) &rest form))
629 ((callf destructuring-bind) 2 (sexp form &rest form))
630 ((callf2) 3 (sexp form form &rest form))
631 ((loop) nil (&rest &or symbolp form))
632 ((ignore-errors) 0 (&rest form))))
633 (dolist (func (car entry))
634 (put func 'lisp-indent-function (nth 1 entry))
635 (put func 'lisp-indent-hook (nth 1 entry))
636 (or (get func 'edebug-form-spec)
637 (put func 'edebug-form-spec (nth 2 entry)))))
fcd73769 638
b68f6e48
SM
639;; Autoload the other portions of the package.
640;; We want to replace the basic versions of dolist, dotimes, declare below.
641(fmakunbound 'dolist)
642(fmakunbound 'dotimes)
643(fmakunbound 'declare)
1cd643e7 644(load "cl-loaddefs" nil 'quiet)
fcd73769 645
b68f6e48 646;; This goes here so that cl-macs can find it if it loads right now.
7467c796 647(provide 'cl)
fcd73769 648
b68f6e48 649;; Things to do after byte-compiler is loaded.
fcd73769
RS
650
651(defvar cl-hacked-flag nil)
652(defun cl-hack-byte-compiler ()
7467c796
GM
653 (and (not cl-hacked-flag) (fboundp 'byte-compile-file-form)
654 (progn
655 (setq cl-hacked-flag t) ; Do it first, to prevent recursion.
656 (load "cl-macs" nil t)
657 (run-hooks 'cl-hack-bytecomp-hook))))
fcd73769 658
b68f6e48 659;; Try it now in case the compiler has already been loaded.
fcd73769
RS
660(cl-hack-byte-compiler)
661
b68f6e48 662;; Also make a hook in case compiler is loaded after this file.
0d752cda 663(add-hook 'bytecomp-load-hook 'cl-hack-byte-compiler)
fcd73769
RS
664
665
b68f6e48
SM
666;; The following ensures that packages which expect the old-style cl.el
667;; will be happy with this one.
fcd73769
RS
668
669(provide 'cl)
670
fcd73769
RS
671(run-hooks 'cl-load-hook)
672
87787319
GM
673;; Local variables:
674;; byte-compile-dynamic: t
675;; byte-compile-warnings: (not cl-functions)
676;; End:
677
fcd73769 678;;; cl.el ends here