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