Remove lib-src/rcs-checkin
[bpt/emacs.git] / lisp / emacs-lisp / cl.el
CommitLineData
87787319 1;;; cl.el --- Common Lisp extensions for Emacs
fcd73769 2
acaf905b 3;; Copyright (C) 1993, 2001-2012 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."
b1198e17 123 (declare (debug (place &optional form)))
fcd73769
RS
124 (if (symbolp place)
125 (list 'setq place (if x (list '+ place x) (list '1+ place)))
126 (list 'callf '+ place (or x 1))))
127
128(defmacro decf (place &optional x)
64a4c526 129 "Decrement PLACE by X (1 by default).
fcd73769
RS
130PLACE may be a symbol, or any generalized variable allowed by `setf'.
131The return value is the decremented value of PLACE."
b1198e17 132 (declare (debug incf))
fcd73769
RS
133 (if (symbolp place)
134 (list 'setq place (if x (list '- place x) (list '1- place)))
135 (list 'callf '- place (or x 1))))
136
7467c796
GM
137;; Autoloaded, but we haven't loaded cl-loaddefs yet.
138(declare-function cl-do-pop "cl-macs" (place))
139
fcd73769 140(defmacro pop (place)
64a4c526 141 "Remove and return the head of the list stored in PLACE.
fcd73769
RS
142Analogous to (prog1 (car PLACE) (setf PLACE (cdr PLACE))), though more
143careful about evaluating each argument only once and in the right order.
144PLACE may be a symbol, or any generalized variable allowed by `setf'."
b1198e17 145 (declare (debug (place)))
fcd73769
RS
146 (if (symbolp place)
147 (list 'car (list 'prog1 place (list 'setq place (list 'cdr place))))
148 (cl-do-pop place)))
149
150(defmacro push (x place)
64a4c526 151 "Insert X at the head of the list stored in PLACE.
fcd73769
RS
152Analogous to (setf PLACE (cons X PLACE)), though more careful about
153evaluating each argument only once and in the right order. PLACE may
154be a symbol, or any generalized variable allowed by `setf'."
b1198e17 155 (declare (debug (form place)))
fcd73769
RS
156 (if (symbolp place) (list 'setq place (list 'cons x place))
157 (list 'callf2 'cons x place)))
158
159(defmacro pushnew (x place &rest keys)
160 "(pushnew X PLACE): insert X at the head of the list if not already there.
161Like (push X PLACE), except that the list is unmodified if X is `eql' to
162an element already on the list.
708c63a6
JB
163\nKeywords supported: :test :test-not :key
164\n(fn X PLACE [KEYWORD VALUE]...)"
b1198e17
SM
165 (declare (debug
166 (form place &rest
167 &or [[&or ":test" ":test-not" ":key"] function-form]
168 [keywordp form])))
6b34950f
RS
169 (if (symbolp place)
170 (if (null keys)
fcecceea 171 `(let ((x ,x))
ba83908c
SM
172 (if (memql x ,place)
173 ;; This symbol may later on expand to actual code which then
174 ;; trigger warnings like "value unused" since pushnew's return
175 ;; value is rarely used. It should not matter that other
176 ;; warnings may be silenced, since `place' is used earlier and
177 ;; should have triggered them already.
178 (with-no-warnings ,place)
179 (setq ,place (cons x ,place))))
6b34950f 180 (list 'setq place (list* 'adjoin x place keys)))
fcd73769
RS
181 (list* 'callf2 'adjoin x place keys)))
182
183(defun cl-set-elt (seq n val)
184 (if (listp seq) (setcar (nthcdr n seq) val) (aset seq n val)))
185
4ded1ddb 186(defsubst cl-set-nthcdr (n list x)
fcd73769
RS
187 (if (<= n 0) x (setcdr (nthcdr (1- n) list) x) list))
188
189(defun cl-set-buffer-substring (start end val)
190 (save-excursion (delete-region start end)
191 (goto-char start)
192 (insert val)
193 val))
194
195(defun cl-set-substring (str start end val)
196 (if end (if (< end 0) (incf end (length str)))
197 (setq end (length str)))
87dd507f 198 (if (< start 0) (incf start (length str)))
fcd73769
RS
199 (concat (and (> start 0) (substring str 0 start))
200 val
201 (and (< end (length str)) (substring str end))))
202
203
204;;; Control structures.
205
b68f6e48
SM
206;; These macros are so simple and so often-used that it's better to have
207;; them all the time than to load them from cl-macs.el.
fcd73769 208
fcd73769 209(defun cl-map-extents (&rest cl-args)
f67171e6 210 (apply 'cl-map-overlays cl-args))
fcd73769
RS
211
212
213;;; Blocks and exits.
214
215(defalias 'cl-block-wrapper 'identity)
216(defalias 'cl-block-throw 'throw)
217
218
b68f6e48
SM
219;;; Multiple values.
220;; True multiple values are not supported, or even
221;; simulated. Instead, multiple-value-bind and friends simply expect
222;; the target form to return the values as a list.
fcd73769 223
413da451
RS
224(defsubst values (&rest values)
225 "Return multiple values, Common Lisp style.
226The arguments of `values' are the values
227that the containing function should return."
8f7ef366 228 values)
413da451
RS
229
230(defsubst values-list (list)
231 "Return multiple values, Common Lisp style, taken from a list.
232LIST specifies the list of values
233that the containing function should return."
234 list)
235
236(defsubst multiple-value-list (expression)
237 "Return a list of the multiple values produced by EXPRESSION.
238This handles multiple values in Common Lisp style, but it does not
239work right when EXPRESSION calls an ordinary Emacs Lisp function
240that returns just one value."
241 expression)
242
243(defsubst multiple-value-apply (function expression)
244 "Evaluate EXPRESSION to get multiple values and apply FUNCTION to them.
245This handles multiple values in Common Lisp style, but it does not work
246right when EXPRESSION calls an ordinary Emacs Lisp function that returns just
247one value."
248 (apply function expression))
249
a2e74caa
SM
250(defalias 'multiple-value-call 'apply
251 "Apply FUNCTION to ARGUMENTS, taking multiple values into account.
252This implementation only handles the case where there is only one argument.")
f988b541 253
413da451
RS
254(defsubst nth-value (n expression)
255 "Evaluate EXPRESSION to get multiple values and return the Nth one.
256This handles multiple values in Common Lisp style, but it does not work
257right when EXPRESSION calls an ordinary Emacs Lisp function that returns just
258one value."
259 (nth n expression))
fcd73769
RS
260
261;;; Macros.
262
7eb73deb 263(defvar cl-macro-environment)
fcd73769
RS
264(defvar cl-old-macroexpand (prog1 (symbol-function 'macroexpand)
265 (defalias 'macroexpand 'cl-macroexpand)))
266
267(defun cl-macroexpand (cl-macro &optional cl-env)
2a8160e6
RS
268 "Return result of expanding macros at top level of FORM.
269If FORM is not a macro call, it is returned unchanged.
270Otherwise, the macro is expanded and the expansion is considered
271in place of FORM. When a non-macro-call results, it is returned.
272
f15e298c 273The second optional arg ENVIRONMENT specifies an environment of macro
708c63a6
JB
274definitions to shadow the loaded ones for use in file byte-compilation.
275\n(fn FORM &optional ENVIRONMENT)"
fcd73769
RS
276 (let ((cl-macro-environment cl-env))
277 (while (progn (setq cl-macro (funcall cl-old-macroexpand cl-macro cl-env))
278 (and (symbolp cl-macro)
279 (cdr (assq (symbol-name cl-macro) cl-env))))
280 (setq cl-macro (cadr (assq (symbol-name cl-macro) cl-env))))
281 cl-macro))
282
283
284;;; Declarations.
285
286(defvar cl-compiling-file nil)
287(defun cl-compiling-file ()
288 (or cl-compiling-file
7200d79c
SM
289 (and (boundp 'byte-compile--outbuffer)
290 (bufferp (symbol-value 'byte-compile--outbuffer))
291 (equal (buffer-name (symbol-value 'byte-compile--outbuffer))
fcd73769
RS
292 " *Compiler Output*"))))
293
294(defvar cl-proclaims-deferred nil)
295
296(defun proclaim (spec)
297 (if (fboundp 'cl-do-proclaim) (cl-do-proclaim spec t)
298 (push spec cl-proclaims-deferred))
299 nil)
300
301(defmacro declaim (&rest specs)
302 (let ((body (mapcar (function (lambda (x) (list 'proclaim (list 'quote x))))
303 specs)))
304 (if (cl-compiling-file) (list* 'eval-when '(compile load eval) body)
305 (cons 'progn body)))) ; avoid loading cl-macs.el for eval-when
306
307
308;;; Symbols.
309
310(defun cl-random-time ()
311 (let* ((time (copy-sequence (current-time-string))) (i (length time)) (v 0))
312 (while (>= (decf i) 0) (setq v (+ (* v 3) (aref time i))))
313 v))
314
4735906a 315(defvar cl--gensym-counter (* (logand (cl-random-time) 1023) 100))
fcd73769
RS
316
317
318;;; Numbers.
319
708c63a6 320(defun floatp-safe (object)
f4db41fc 321 "Return t if OBJECT is a floating point number.
fcd73769
RS
322On Emacs versions that lack floating-point support, this function
323always returns nil."
708c63a6 324 (and (numberp object) (not (integerp object))))
fcd73769 325
708c63a6 326(defun plusp (number)
f4db41fc 327 "Return t if NUMBER is positive."
708c63a6 328 (> number 0))
fcd73769 329
708c63a6 330(defun minusp (number)
f4db41fc 331 "Return t if NUMBER is negative."
708c63a6 332 (< number 0))
fcd73769 333
708c63a6 334(defun oddp (integer)
f4db41fc 335 "Return t if INTEGER is odd."
708c63a6 336 (eq (logand integer 1) 1))
fcd73769 337
708c63a6 338(defun evenp (integer)
f4db41fc 339 "Return t if INTEGER is even."
708c63a6 340 (eq (logand integer 1) 0))
fcd73769 341
4735906a 342(defvar cl--random-state (vector 'cl-random-state-tag -1 30 (cl-random-time)))
fcd73769 343
416a2c45
GM
344(defconst most-positive-float nil
345 "The largest value that a Lisp float can hold.
346If your system supports infinities, this is the largest finite value.
347For IEEE machines, this is approximately 1.79e+308.
348Call `cl-float-limits' to set this.")
349
350(defconst most-negative-float nil
351 "The largest negative value that a Lisp float can hold.
44b0122f 352This is simply -`most-positive-float'.
416a2c45
GM
353Call `cl-float-limits' to set this.")
354
355(defconst least-positive-float nil
356 "The smallest value greater than zero that a Lisp float can hold.
357For IEEE machines, it is about 4.94e-324 if denormals are supported,
358or 2.22e-308 if they are not.
359Call `cl-float-limits' to set this.")
360
361(defconst least-negative-float nil
362 "The smallest value less than zero that a Lisp float can hold.
363This is simply -`least-positive-float'.
364Call `cl-float-limits' to set this.")
365
366(defconst least-positive-normalized-float nil
367 "The smallest normalized Lisp float greater than zero.
368This is the smallest value for which IEEE denormalization does not lose
369precision. For IEEE machines, this value is about 2.22e-308.
370For machines that do not support the concept of denormalization
371and gradual underflow, this constant equals `least-positive-float'.
372Call `cl-float-limits' to set this.")
373
374(defconst least-negative-normalized-float nil
375 "The smallest normalized Lisp float less than zero.
376This is simply -`least-positive-normalized-float'.
377Call `cl-float-limits' to set this.")
378
379(defconst float-epsilon nil
380 "The smallest positive float that adds to 1.0 to give a distinct value.
381Adding a number less than this to 1.0 returns 1.0 due to roundoff.
382For IEEE machines, epsilon is about 2.22e-16.
383Call `cl-float-limits' to set this.")
384
385(defconst float-negative-epsilon nil
386 "The smallest positive float that subtracts from 1.0 to give a distinct value.
387For IEEE machines, it is about 1.11e-16.
388Call `cl-float-limits' to set this.")
fcd73769
RS
389
390
391;;; Sequence functions.
392
393(defalias 'copy-seq 'copy-sequence)
394
7467c796
GM
395(declare-function cl-mapcar-many "cl-extra" (cl-func cl-seqs))
396
fcd73769
RS
397(defun mapcar* (cl-func cl-x &rest cl-rest)
398 "Apply FUNCTION to each element of SEQ, and make a list of the results.
399If there are several SEQs, FUNCTION is called with that many arguments,
400and mapping stops as soon as the shortest list runs out. With just one
401SEQ, this is like `mapcar'. With several, it is like the Common Lisp
708c63a6
JB
402`mapcar' function extended to arbitrary sequence types.
403\n(fn FUNCTION SEQ...)"
fcd73769
RS
404 (if cl-rest
405 (if (or (cdr cl-rest) (nlistp cl-x) (nlistp (car cl-rest)))
406 (cl-mapcar-many cl-func (cons cl-x cl-rest))
407 (let ((cl-res nil) (cl-y (car cl-rest)))
408 (while (and cl-x cl-y)
409 (push (funcall cl-func (pop cl-x) (pop cl-y)) cl-res))
410 (nreverse cl-res)))
411 (mapcar cl-func cl-x)))
412
686d0681 413(defalias 'svref 'aref)
fcd73769
RS
414
415;;; List functions.
416
417(defalias 'first 'car)
dc79c3ea 418(defalias 'second 'cadr)
fcd73769
RS
419(defalias 'rest 'cdr)
420(defalias 'endp 'null)
421
dc79c3ea 422(defun third (x)
b7a90344
SM
423 "Return the third element of the list X."
424 (car (cdr (cdr x))))
fcd73769 425
dc79c3ea 426(defun fourth (x)
b7a90344
SM
427 "Return the fourth element of the list X."
428 (nth 3 x))
fcd73769 429
dc79c3ea 430(defun fifth (x)
b7a90344
SM
431 "Return the fifth element of the list X."
432 (nth 4 x))
fcd73769 433
dc79c3ea 434(defun sixth (x)
b7a90344
SM
435 "Return the sixth element of the list X."
436 (nth 5 x))
fcd73769 437
dc79c3ea 438(defun seventh (x)
b7a90344
SM
439 "Return the seventh element of the list X."
440 (nth 6 x))
fcd73769 441
dc79c3ea 442(defun eighth (x)
b7a90344
SM
443 "Return the eighth element of the list X."
444 (nth 7 x))
fcd73769 445
dc79c3ea 446(defun ninth (x)
b7a90344
SM
447 "Return the ninth element of the list X."
448 (nth 8 x))
fcd73769 449
dc79c3ea 450(defun tenth (x)
b7a90344
SM
451 "Return the tenth element of the list X."
452 (nth 9 x))
fcd73769 453
fcd73769
RS
454(defun caaar (x)
455 "Return the `car' of the `car' of the `car' of X."
456 (car (car (car x))))
457
458(defun caadr (x)
459 "Return the `car' of the `car' of the `cdr' of X."
460 (car (car (cdr x))))
461
462(defun cadar (x)
463 "Return the `car' of the `cdr' of the `car' of X."
464 (car (cdr (car x))))
465
466(defun caddr (x)
467 "Return the `car' of the `cdr' of the `cdr' of X."
468 (car (cdr (cdr x))))
469
470(defun cdaar (x)
471 "Return the `cdr' of the `car' of the `car' of X."
472 (cdr (car (car x))))
473
474(defun cdadr (x)
475 "Return the `cdr' of the `car' of the `cdr' of X."
476 (cdr (car (cdr x))))
477
478(defun cddar (x)
479 "Return the `cdr' of the `cdr' of the `car' of X."
480 (cdr (cdr (car x))))
481
482(defun cdddr (x)
483 "Return the `cdr' of the `cdr' of the `cdr' of X."
484 (cdr (cdr (cdr x))))
485
486(defun caaaar (x)
487 "Return the `car' of the `car' of the `car' of the `car' of X."
488 (car (car (car (car x)))))
489
490(defun caaadr (x)
491 "Return the `car' of the `car' of the `car' of the `cdr' of X."
492 (car (car (car (cdr x)))))
493
494(defun caadar (x)
495 "Return the `car' of the `car' of the `cdr' of the `car' of X."
496 (car (car (cdr (car x)))))
497
498(defun caaddr (x)
499 "Return the `car' of the `car' of the `cdr' of the `cdr' of X."
500 (car (car (cdr (cdr x)))))
501
502(defun cadaar (x)
503 "Return the `car' of the `cdr' of the `car' of the `car' of X."
504 (car (cdr (car (car x)))))
505
506(defun cadadr (x)
507 "Return the `car' of the `cdr' of the `car' of the `cdr' of X."
508 (car (cdr (car (cdr x)))))
509
510(defun caddar (x)
511 "Return the `car' of the `cdr' of the `cdr' of the `car' of X."
512 (car (cdr (cdr (car x)))))
513
514(defun cadddr (x)
515 "Return the `car' of the `cdr' of the `cdr' of the `cdr' of X."
516 (car (cdr (cdr (cdr x)))))
517
518(defun cdaaar (x)
519 "Return the `cdr' of the `car' of the `car' of the `car' of X."
520 (cdr (car (car (car x)))))
521
522(defun cdaadr (x)
523 "Return the `cdr' of the `car' of the `car' of the `cdr' of X."
524 (cdr (car (car (cdr x)))))
525
526(defun cdadar (x)
527 "Return the `cdr' of the `car' of the `cdr' of the `car' of X."
528 (cdr (car (cdr (car x)))))
529
530(defun cdaddr (x)
531 "Return the `cdr' of the `car' of the `cdr' of the `cdr' of X."
532 (cdr (car (cdr (cdr x)))))
533
534(defun cddaar (x)
535 "Return the `cdr' of the `cdr' of the `car' of the `car' of X."
536 (cdr (cdr (car (car x)))))
537
538(defun cddadr (x)
539 "Return the `cdr' of the `cdr' of the `car' of the `cdr' of X."
540 (cdr (cdr (car (cdr x)))))
541
542(defun cdddar (x)
543 "Return the `cdr' of the `cdr' of the `cdr' of the `car' of X."
544 (cdr (cdr (cdr (car x)))))
545
546(defun cddddr (x)
547 "Return the `cdr' of the `cdr' of the `cdr' of the `cdr' of X."
548 (cdr (cdr (cdr (cdr x)))))
549
f9efebca
RS
550;;(defun last* (x &optional n)
551;; "Returns the last link in the list LIST.
552;;With optional argument N, returns Nth-to-last link (default 1)."
553;; (if n
554;; (let ((m 0) (p x))
555;; (while (consp p) (incf m) (pop p))
556;; (if (<= n 0) p
557;; (if (< n m) (nthcdr (- m n) x) x)))
558;; (while (consp (cdr x)) (pop x))
559;; x))
fcd73769 560
fcd73769 561(defun list* (arg &rest rest) ; See compiler macro in cl-macs.el
708c63a6 562 "Return a new list with specified ARGs as elements, consed to last ARG.
fcd73769 563Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
708c63a6
JB
564`(cons A (cons B (cons C D)))'.
565\n(fn ARG...)"
fcd73769
RS
566 (cond ((not rest) arg)
567 ((not (cdr rest)) (cons arg (car rest)))
568 (t (let* ((n (length rest))
569 (copy (copy-sequence rest))
570 (last (nthcdr (- n 2) copy)))
571 (setcdr last (car (cdr last)))
572 (cons arg copy)))))
573
574(defun ldiff (list sublist)
575 "Return a copy of LIST with the tail SUBLIST removed."
576 (let ((res nil))
577 (while (and (consp list) (not (eq list sublist)))
578 (push (pop list) res))
579 (nreverse res)))
580
6b25a2f5 581(defun copy-list (list)
708c63a6
JB
582 "Return a copy of LIST, which may be a dotted list.
583The elements of LIST are not copied, just the list structure itself."
6b25a2f5
RS
584 (if (consp list)
585 (let ((res nil))
586 (while (consp list) (push (pop list) res))
587 (prog1 (nreverse res) (setcdr res list)))
588 (car list)))
589
fcd73769
RS
590(defun cl-maclisp-member (item list)
591 (while (and list (not (equal item (car list)))) (setq list (cdr list)))
592 list)
593
fcd73769 594(defalias 'cl-member 'memq) ; for compatibility with old CL package
ec302593
GM
595
596;; Autoloaded, but we have not loaded cl-loaddefs yet.
597(declare-function floor* "cl-extra" (x &optional y))
598(declare-function ceiling* "cl-extra" (x &optional y))
599(declare-function truncate* "cl-extra" (x &optional y))
600(declare-function round* "cl-extra" (x &optional y))
601(declare-function mod* "cl-extra" (x y))
602
fcd73769
RS
603(defalias 'cl-floor 'floor*)
604(defalias 'cl-ceiling 'ceiling*)
605(defalias 'cl-truncate 'truncate*)
606(defalias 'cl-round 'round*)
607(defalias 'cl-mod 'mod*)
608
609(defun adjoin (cl-item cl-list &rest cl-keys) ; See compiler macro in cl-macs
610 "Return ITEM consed onto the front of LIST only if it's not already there.
611Otherwise, return LIST unmodified.
708c63a6
JB
612\nKeywords supported: :test :test-not :key
613\n(fn ITEM LIST [KEYWORD VALUE]...)"
fcd73769
RS
614 (cond ((or (equal cl-keys '(:test eq))
615 (and (null cl-keys) (not (numberp cl-item))))
616 (if (memq cl-item cl-list) cl-list (cons cl-item cl-list)))
617 ((or (equal cl-keys '(:test equal)) (null cl-keys))
618 (if (member cl-item cl-list) cl-list (cons cl-item cl-list)))
4735906a 619 (t (apply 'cl--adjoin cl-item cl-list cl-keys))))
fcd73769
RS
620
621(defun subst (cl-new cl-old cl-tree &rest cl-keys)
622 "Substitute NEW for OLD everywhere in TREE (non-destructively).
623Return a copy of TREE with all elements `eql' to OLD replaced by NEW.
708c63a6
JB
624\nKeywords supported: :test :test-not :key
625\n(fn NEW OLD TREE [KEYWORD VALUE]...)"
fcd73769
RS
626 (if (or cl-keys (and (numberp cl-old) (not (integerp cl-old))))
627 (apply 'sublis (list (cons cl-old cl-new)) cl-tree cl-keys)
628 (cl-do-subst cl-new cl-old cl-tree)))
629
630(defun cl-do-subst (cl-new cl-old cl-tree)
631 (cond ((eq cl-tree cl-old) cl-new)
632 ((consp cl-tree)
633 (let ((a (cl-do-subst cl-new cl-old (car cl-tree)))
634 (d (cl-do-subst cl-new cl-old (cdr cl-tree))))
635 (if (and (eq a (car cl-tree)) (eq d (cdr cl-tree)))
636 cl-tree (cons a d))))
637 (t cl-tree)))
638
868904eb
JB
639(defun acons (key value alist)
640 "Add KEY and VALUE to ALIST.
641Return a new list with (cons KEY VALUE) as car and ALIST as cdr."
642 (cons (cons key value) alist))
643
644(defun pairlis (keys values &optional alist)
645 "Make an alist from KEYS and VALUES.
646Return a new alist composed by associating KEYS to corresponding VALUES;
647the process stops as soon as KEYS or VALUES run out.
648If ALIST is non-nil, the new pairs are prepended to it."
649 (nconc (mapcar* 'cons keys values) alist))
fcd73769
RS
650
651
652;;; Miscellaneous.
653
b68f6e48
SM
654;; Autoload the other portions of the package.
655;; We want to replace the basic versions of dolist, dotimes, declare below.
656(fmakunbound 'dolist)
657(fmakunbound 'dotimes)
658(fmakunbound 'declare)
b581bb5c
SM
659;;;###autoload
660(progn
661 ;; Autoload, so autoload.el and font-lock can use it even when CL
662 ;; is not loaded.
663 (put 'defun* 'doc-string-elt 3)
664 (put 'defmacro* 'doc-string-elt 3)
665 (put 'defsubst 'doc-string-elt 3)
666 (put 'defstruct 'doc-string-elt 2))
667
1cd643e7 668(load "cl-loaddefs" nil 'quiet)
fcd73769 669
b68f6e48 670;; This goes here so that cl-macs can find it if it loads right now.
7467c796 671(provide 'cl)
fcd73769 672
b68f6e48 673;; Things to do after byte-compiler is loaded.
fcd73769
RS
674
675(defvar cl-hacked-flag nil)
676(defun cl-hack-byte-compiler ()
7467c796
GM
677 (and (not cl-hacked-flag) (fboundp 'byte-compile-file-form)
678 (progn
679 (setq cl-hacked-flag t) ; Do it first, to prevent recursion.
680 (load "cl-macs" nil t)
681 (run-hooks 'cl-hack-bytecomp-hook))))
fcd73769 682
b68f6e48 683;; Try it now in case the compiler has already been loaded.
fcd73769
RS
684(cl-hack-byte-compiler)
685
b68f6e48 686;; Also make a hook in case compiler is loaded after this file.
0d752cda 687(add-hook 'bytecomp-load-hook 'cl-hack-byte-compiler)
fcd73769
RS
688
689
b68f6e48
SM
690;; The following ensures that packages which expect the old-style cl.el
691;; will be happy with this one.
fcd73769
RS
692
693(provide 'cl)
694
fcd73769
RS
695(run-hooks 'cl-load-hook)
696
87787319
GM
697;; Local variables:
698;; byte-compile-dynamic: t
699;; byte-compile-warnings: (not cl-functions)
700;; End:
701
fcd73769 702;;; cl.el ends here