Initial git import
[sxemacs] / lisp / cl.el
1 ;;; cl.el --- Common Lisp extensions for GNU Emacs Lisp
2
3 ;; Copyright (C) 1993, 1997 Free Software Foundation, Inc.
4
5 ;; Author: Dave Gillespie <daveg@synaptics.com>
6 ;; Maintainer: SXEmacs Development Team
7 ;; Version: 2.02
8 ;; Keywords: extensions, dumped, lisp
9
10 ;; This file is part of SXEmacs.
11
12 ;; SXEmacs 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 ;; SXEmacs 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 this program.  If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Synched up with: FSF 19.34.
26
27 ;;; Commentary:
28
29 ;; This file is dumped with SXEmacs.
30
31 ;; These are extensions to Emacs Lisp that provide a degree of
32 ;; Common Lisp compatibility, beyond what is already built-in
33 ;; in Emacs Lisp.
34 ;;
35 ;; This package was written by Dave Gillespie; it is a complete
36 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
37 ;;
38 ;; This package works with Emacs 18, Emacs 19, and XEmacs/Lucid Emacs 19.
39 ;;
40 ;; Bug reports, comments, and suggestions are welcome!
41
42 ;; This file contains the portions of the Common Lisp extensions
43 ;; package which should always be present.
44
45
46 ;;; Future notes:
47
48 ;; Once Emacs 19 becomes standard, many things in this package which are
49 ;; messy for reasons of compatibility can be greatly simplified.  For now,
50 ;; I prefer to maintain one unified version.
51
52
53 ;;; Change Log:
54
55 ;; Version 2.02 (30 Jul 93):
56 ;;  * Added "cl-compat.el" file, extra compatibility with old package.
57 ;;  * Added `lexical-let' and `lexical-let*'.
58 ;;  * Added `define-modify-macro', `callf', and `callf2'.
59 ;;  * Added `ignore-errors'.
60 ;;  * Changed `(setf (nthcdr N PLACE) X)' to work when N is zero.
61 ;;  * Merged `*gentemp-counter*' into `*gensym-counter*'.
62 ;;  * Extended `subseq' to allow negative START and END like `substring'.
63 ;;  * Added `in-ref', `across-ref', `elements of-ref' loop clauses.
64 ;;  * Added `concat', `vconcat' loop clauses.
65 ;;  * Cleaned up a number of compiler warnings.
66
67 ;; Version 2.01 (7 Jul 93):
68 ;;  * Added support for FSF version of Emacs 19.
69 ;;  * Added `add-hook' for Emacs 18 users.
70 ;;  * Added `defsubst*' and `symbol-macrolet'.
71 ;;  * Added `maplist', `mapc', `mapl', `mapcan', `mapcon'.
72 ;;  * Added `map', `concatenate', `reduce', `merge'.
73 ;;  * Added `revappend', `nreconc', `tailp', `tree-equal'.
74 ;;  * Added `assert', `check-type', `typecase', `typep', and `deftype'.
75 ;;  * Added destructuring and `&environment' support to `defmacro*'.
76 ;;  * Added destructuring to `loop', and added the following clauses:
77 ;;      `elements', `frames', `overlays', `intervals', `buffers', `key-seqs'.
78 ;;  * Renamed `delete' to `delete*' and `remove' to `remove*'.
79 ;;  * Completed support for all keywords in `remove*', `substitute', etc.
80 ;;  * Added `most-positive-float' and company.
81 ;;  * Fixed hash tables to work with latest Lucid Emacs.
82 ;;  * `proclaim' forms are no longer compile-time-evaluating; use `declaim'.
83 ;;  * Syntax for `warn' declarations has changed.
84 ;;  * Improved implementation of `random*'.
85 ;;  * Moved most sequence functions to a new file, cl-seq.el.
86 ;;  * Moved `eval-when' into cl-macs.el.
87 ;;  * Moved `pushnew' and `adjoin' to cl.el for most common cases.
88 ;;  * Moved `provide' forms down to ends of files.
89 ;;  * Changed expansion of `pop' to something that compiles to better code.
90 ;;  * Changed so that no patch is required for Emacs 19 byte compiler.
91 ;;  * Made more things dependent on `optimize' declarations.
92 ;;  * Added a partial implementation of struct print functions.
93 ;;  * Miscellaneous minor changes.
94
95 ;; Version 2.00:
96 ;;  * First public release of this package.
97
98
99 ;;; Code:
100
101 (defvar cl-emacs-type (cond ((or (and (fboundp 'epoch::version)
102                                       (symbol-value 'epoch::version))
103                                  (string-lessp emacs-version "19")) 18)
104                             ((string-match "XEmacs" emacs-version)
105                              'lucid)
106                             (t 19)))
107
108 (or (fboundp 'defalias) (fset 'defalias 'fset))
109
110 (defvar cl-optimize-speed 1)
111 (defvar cl-optimize-safety 1)
112
113
114 ;;; Keywords used in this package.
115
116 ;;; XEmacs - keywords are done in Fintern().
117 ;;;
118 ;;; (defconst :test ':test)
119 ;;; (defconst :test-not ':test-not)
120 ;;; (defconst :key ':key)
121 ;;; (defconst :start ':start)
122 ;;; (defconst :start1 ':start1)
123 ;;; (defconst :start2 ':start2)
124 ;;; (defconst :end ':end)
125 ;;; (defconst :end1 ':end1)
126 ;;; (defconst :end2 ':end2)
127 ;;; (defconst :count ':count)
128 ;;; (defconst :initial-value ':initial-value)
129 ;;; (defconst :size ':size)
130 ;;; (defconst :from-end ':from-end)
131 ;;; (defconst :rehash-size ':rehash-size)
132 ;;; (defconst :rehash-threshold ':rehash-threshold)
133 ;;; (defconst :allow-other-keys ':allow-other-keys)
134
135
136 (defvar custom-print-functions nil
137   "This is a list of functions that format user objects for printing.
138 Each function is called in turn with three arguments: the object, the
139 stream, and the print level (currently ignored).  If it is able to
140 print the object it returns true; otherwise it returns nil and the
141 printer proceeds to the next function on the list.")
142
143 (defun print-custom-object (obj &optional stream pl)
144   "Print custom object OBJ into STREAM using print level PL."
145   (let ((cpfs custom-print-functions)
146         (ret nil))
147     (while cpfs
148       (when (funcall (car cpfs) obj stream pl)
149         (setq cpfs nil
150               ret t))
151       (setq cpfs (cdr cpfs)))
152     ret))
153
154 (defun print-object-ignoring-custom-print-functions (obj &optional stream pl)
155   "Print object OBJ not using custom printers."
156   (let ((custom-object-printer nil))
157     (princ obj stream)))
158   
159 ;; Install custom printer
160 (setq custom-object-printer 'print-custom-object)
161
162 ;;; Predicates.
163
164 (defun eql (a b)    ; See compiler macro in cl-macs.el
165   "Return t if the two args are the same Lisp object.
166 Floating-point numbers of equal value are `eql', but they may not be `eq'."
167   (if (floatp a)
168       (equal a b)
169     (eq a b)))
170
171
172 ;;; Generalized variables.  These macros are defined here so that they
173 ;;; can safely be used in .emacs files.
174
175 (defmacro incf (place &optional x)
176   "(incf PLACE [X]): increment PLACE by X (1 by default).
177 PLACE may be a symbol, or any generalized variable allowed by `setf'.
178 The return value is the incremented value of PLACE."
179   (if (symbolp place)
180       (list 'setq place (if x (list '+ place x) (list '1+ place)))
181     ;; XEmacs byte-compiler optimizes (+ FOO 1) to (1+ FOO), so this
182     ;; is OK.
183     (list 'callf '+ place (or x 1))))
184
185 (defmacro decf (place &optional x)
186   "(decf PLACE [X]): decrement PLACE by X (1 by default).
187 PLACE may be a symbol, or any generalized variable allowed by `setf'.
188 The return value is the decremented value of PLACE."
189   (if (symbolp place)
190       (list 'setq place (if x (list '- place x) (list '1- place)))
191     (list 'callf '- place (or x 1))))
192
193 (defmacro pop (place)
194   "(pop PLACE): remove and return the head of the list stored in PLACE.
195 Analogous to (prog1 (car PLACE) (setf PLACE (cdr PLACE))), though more
196 careful about evaluating each argument only once and in the right order.
197 PLACE may be a symbol, or any generalized variable allowed by `setf'."
198   (if (symbolp place)
199       `(car (prog1 ,place (setq ,place (cdr ,place))))
200     (cl-do-pop place)))
201
202 (defmacro push (x place)
203   "(push X PLACE): insert X at the head of the list stored in PLACE.
204 Analogous to (setf PLACE (cons X PLACE)), though more careful about
205 evaluating each argument only once and in the right order.  PLACE may
206 be a symbol, or any generalized variable allowed by `setf'; that is,
207 it does not necessarily have to be a list, though `push' is most often
208 used on lists."
209   (if (symbolp place) `(setq ,place (cons ,x ,place))
210     (list 'callf2 'cons x place)))
211
212 (defmacro pushnew (x place &rest keys)
213   "(pushnew X PLACE): insert X at the head of the list stored in PLACE.
214 Like (push X PLACE), except that the list is unmodified if X is `eql'
215 to an element already on the list.  
216 Keywords supported: :test :test-not :key"
217   (if (symbolp place) (list 'setq place (list* 'adjoin x place keys))
218     (list* 'callf2 'adjoin x place keys)))
219
220 (defun cl-set-elt (seq n val)
221   (if (listp seq) (setcar (nthcdr n seq) val) (aset seq n val)))
222
223 (defun cl-set-nthcdr (n list x)
224   (if (<= n 0) x (setcdr (nthcdr (1- n) list) x) list))
225
226 (defun cl-set-buffer-substring (start end val)
227   (save-excursion (delete-region start end)
228                   (goto-char start)
229                   (insert val)
230                   val))
231
232 (defun cl-set-substring (str start end val)
233   (if end (if (< end 0) (incf end (length str)))
234     (setq end (length str)))
235   (if (< start 0) (incf start (length str)))
236   (concat (and (> start 0) (substring str 0 start))
237           val
238           (and (< end (length str)) (substring str end))))
239
240
241 ;;; Control structures.
242
243 ;; The macros `when' and `unless' are so useful that we want them to
244 ;; ALWAYS be available.  So they've been moved from cl.el to eval.c.
245 ;; Note: FSF Emacs moved them to subr.el in FSF 20.
246
247 (defun cl-map-extents (&rest cl-args)
248   ;; XEmacs: This used to check for overlays first, but that's wrong
249   ;; because of the new compatibility library.  *duh*
250   (cond ((fboundp 'map-extents)
251          (apply 'map-extents cl-args))
252         ((fboundp 'next-overlay-at)
253          (apply 'cl-map-overlays cl-args))))
254
255
256 ;;; Blocks and exits.
257
258 (defalias 'cl-block-wrapper 'identity)
259 (defalias 'cl-block-throw 'throw)
260
261
262 ;;; Multiple values.  True multiple values are not supported, or even
263 ;;; simulated.  Instead, multiple-value-bind and friends simply expect
264 ;;; the target form to return the values as a list.
265
266 (defalias 'values 'list)
267 (defalias 'values-list 'identity)
268 (defalias 'multiple-value-list 'identity)
269 (defalias 'multiple-value-call 'apply)  ; only works for one arg
270 (defalias 'nth-value 'nth)
271
272
273 ;;; Macros.
274
275 (defvar cl-macro-environment nil)
276 ;; XEmacs: we renamed the internal function to macroexpand-internal
277 ;; to avoid doc-file problems.
278 (defvar cl-old-macroexpand (prog1 (symbol-function 'macroexpand-internal)
279                              (defalias 'macroexpand 'cl-macroexpand)))
280
281 (defun cl-macroexpand (cl-macro &optional cl-env)
282   "Return result of expanding macros at top level of FORM.
283 If FORM is not a macro call, it is returned unchanged.
284 Otherwise, the macro is expanded and the expansion is considered
285 in place of FORM.  When a non-macro-call results, it is returned.
286
287 The second optional arg ENVIRONMENT specifies an environment of macro
288 definitions to shadow the loaded ones for use in file byte-compilation."
289   (let ((cl-macro-environment cl-env))
290     (while (progn (setq cl-macro (funcall cl-old-macroexpand cl-macro cl-env))
291                   (and (symbolp cl-macro)
292                        (cdr (assq (symbol-name cl-macro) cl-env))))
293       (setq cl-macro (cadr (assq (symbol-name cl-macro) cl-env))))
294     cl-macro))
295
296
297 ;;; Declarations.
298
299 (defvar cl-compiling-file nil)
300 (defun cl-compiling-file ()
301   (or cl-compiling-file
302       ;; XEmacs change
303 ;      (and (boundp 'outbuffer) (bufferp (symbol-value 'outbuffer))
304 ;          (equal (buffer-name (symbol-value 'outbuffer))
305 ;                 " *Compiler Output*"))
306       (and (boundp 'byte-compile-outbuffer)
307            (bufferp (symbol-value 'byte-compile-outbuffer))
308            (equal (buffer-name (symbol-value 'byte-compile-outbuffer))
309                   " *Compiler Output*"))
310       ))
311
312 (defvar cl-proclaims-deferred nil)
313
314 (defun proclaim (spec)
315   (if (fboundp 'cl-do-proclaim) (cl-do-proclaim spec t)
316     (push spec cl-proclaims-deferred))
317   nil)
318
319 (defmacro declaim (&rest specs)
320   (let ((body (mapcar (function (lambda (x) (list 'proclaim (list 'quote x))))
321                       specs)))
322     (if (cl-compiling-file) (list* 'eval-when '(compile load eval) body)
323       (cons 'progn body))))   ; avoid loading cl-macs.el for eval-when
324
325
326 ;;; Symbols.
327
328 (defun cl-random-time ()
329   (let* ((time (copy-sequence (current-time-string))) (i (length time)) (v 0))
330     (while (>= (decf i) 0) (setq v (+ (* v 3) (aref time i))))
331     v))
332
333 (defvar *gensym-counter* (* (logand (cl-random-time) 1023) 100))
334
335 (defun gensym (&optional arg)
336   "Generate a new uninterned symbol.
337 The name is made by appending a number to a prefix.  If ARG is a string, it
338 is the prefix, otherwise the prefix defaults to \"G\".  If ARG is an integer,
339 the internal counter is reset to that number before creating the name.
340 There is no way to specify both using this function."
341   (let ((prefix (if (stringp arg) arg "G"))
342         (num (if (integerp arg) arg
343                (prog1 *gensym-counter*
344                  (setq *gensym-counter* (1+ *gensym-counter*))))))
345     (make-symbol (format "%s%d" prefix num))))
346
347 (defun gentemp (&optional arg)
348   "Generate a new interned symbol with a unique name.
349 The name is made by appending a number to ARG, default \"G\".
350 If ARG is not a string, it is ignored."
351   (let ((prefix (if (stringp arg) arg "G"))
352         name)
353     (while (intern-soft (setq name (format "%s%d" prefix *gensym-counter*)))
354       (setq *gensym-counter* (1+ *gensym-counter*)))
355     (intern name)))
356
357 ;;; Numbers.
358
359 (defun floatp-safe (object)
360   "Return t if OBJECT is a floating point number."
361   (floatp object))
362
363 (defun plusp (number)
364   "Return t if NUMBER is positive."
365   (> number 0))
366
367 (defun minusp (number)
368   "Return t if NUMBER is negative."
369   (< number 0))
370
371 (unless (featurep 'ent)
372   (defun oddp (integer)
373     "Return t if INTEGER is odd."
374     (eq (logand integer 1) 1))
375
376   (defun evenp (integer)
377     "Return t if INTEGER is even."
378     (eq (logand integer 1) 0)))
379
380 (defun cl-abs (number)
381   "Return the absolute value of NUMBER."
382   (if (>= number 0) number (- number)))
383 (or (fboundp 'abs) (defalias 'abs 'cl-abs))   ; This is built-in to Emacs 19
384
385 (defvar *random-state* (vector 'cl-random-state-tag -1 30 (cl-random-time)))
386
387 ;; Threse constants are defined in C when 'number-types is provided
388 (unless (featurep 'ent)
389 ;;; We use `eval' in case VALBITS differs from compile-time to load-time.
390   (defconst most-positive-fixnum (eval '(lsh -1 -1))
391     "The integer closest in value to positive infinity.")
392   (defconst most-negative-fixnum (eval '(- -1 (lsh -1 -1)))
393     "The integer closest in value to negative infinity."))
394
395 ;;; Sequence functions.
396
397 (defalias 'copy-seq 'copy-sequence)
398
399 (defun mapcar* (cl-func cl-x &rest cl-rest)
400   "Apply FUNCTION to each element of SEQ, and make a list of the results.
401 If there are several SEQs, FUNCTION is called with that many arguments,
402 and mapping stops as soon as the shortest list runs out.  With just one
403 SEQ, this is like `mapcar'.  With several, it is like the Common Lisp
404 `mapcar' function extended to arbitrary sequence types."
405   (if cl-rest
406       (if (or (cdr cl-rest) (nlistp cl-x) (nlistp (car cl-rest)))
407           (cl-mapcar-many cl-func (cons cl-x cl-rest))
408         (let ((cl-res nil) (cl-y (car cl-rest)))
409           (while (and cl-x cl-y)
410             (push (funcall cl-func (pop cl-x) (pop cl-y)) cl-res))
411           (nreverse cl-res)))
412     (mapcar cl-func cl-x)))
413
414
415 ;;; List functions.
416
417 ;; These functions are made known to the byte-compiler by cl-macs.el
418 ;; and turned into efficient car and cdr bytecodes.
419
420 (defalias 'first 'car)
421 (defalias 'rest 'cdr)
422 (defalias 'endp 'null)
423
424 (defun second (x)
425   "Return the second element of the list LIST."
426   (car (cdr x)))
427
428 (defun third (x)
429   "Return the third element of the list LIST."
430   (car (cdr (cdr x))))
431
432 (defun fourth (x)
433   "Return the fourth element of the list LIST."
434   (nth 3 x))
435
436 (defun fifth (x)
437   "Return the fifth element of the list LIST."
438   (nth 4 x))
439
440 (defun sixth (x)
441   "Return the sixth element of the list LIST."
442   (nth 5 x))
443
444 (defun seventh (x)
445   "Return the seventh element of the list LIST."
446   (nth 6 x))
447
448 (defun eighth (x)
449   "Return the eighth element of the list LIST."
450   (nth 7 x))
451
452 (defun ninth (x)
453   "Return the ninth element of the list LIST."
454   (nth 8 x))
455
456 (defun tenth (x)
457   "Return the tenth element of the list LIST."
458   (nth 9 x))
459
460 (defun caar (x)
461   "Return the `car' of the `car' of X."
462   (car (car x)))
463
464 (defun cadr (x)
465   "Return the `car' of the `cdr' of X."
466   (car (cdr x)))
467
468 (defun cdar (x)
469   "Return the `cdr' of the `car' of X."
470   (cdr (car x)))
471
472 (defun cddr (x)
473   "Return the `cdr' of the `cdr' of X."
474   (cdr (cdr x)))
475
476 (defun caaar (x)
477   "Return the `car' of the `car' of the `car' of X."
478   (car (car (car x))))
479
480 (defun caadr (x)
481   "Return the `car' of the `car' of the `cdr' of X."
482   (car (car (cdr x))))
483
484 (defun cadar (x)
485   "Return the `car' of the `cdr' of the `car' of X."
486   (car (cdr (car x))))
487
488 (defun caddr (x)
489   "Return the `car' of the `cdr' of the `cdr' of X."
490   (car (cdr (cdr x))))
491
492 (defun cdaar (x)
493   "Return the `cdr' of the `car' of the `car' of X."
494   (cdr (car (car x))))
495
496 (defun cdadr (x)
497   "Return the `cdr' of the `car' of the `cdr' of X."
498   (cdr (car (cdr x))))
499
500 (defun cddar (x)
501   "Return the `cdr' of the `cdr' of the `car' of X."
502   (cdr (cdr (car x))))
503
504 (defun cdddr (x)
505   "Return the `cdr' of the `cdr' of the `cdr' of X."
506   (cdr (cdr (cdr x))))
507
508 (defun caaaar (x)
509   "Return the `car' of the `car' of the `car' of the `car' of X."
510   (car (car (car (car x)))))
511
512 (defun caaadr (x)
513   "Return the `car' of the `car' of the `car' of the `cdr' of X."
514   (car (car (car (cdr x)))))
515
516 (defun caadar (x)
517   "Return the `car' of the `car' of the `cdr' of the `car' of X."
518   (car (car (cdr (car x)))))
519
520 (defun caaddr (x)
521   "Return the `car' of the `car' of the `cdr' of the `cdr' of X."
522   (car (car (cdr (cdr x)))))
523
524 (defun cadaar (x)
525   "Return the `car' of the `cdr' of the `car' of the `car' of X."
526   (car (cdr (car (car x)))))
527
528 (defun cadadr (x)
529   "Return the `car' of the `cdr' of the `car' of the `cdr' of X."
530   (car (cdr (car (cdr x)))))
531
532 (defun caddar (x)
533   "Return the `car' of the `cdr' of the `cdr' of the `car' of X."
534   (car (cdr (cdr (car x)))))
535
536 (defun cadddr (x)
537   "Return the `car' of the `cdr' of the `cdr' of the `cdr' of X."
538   (car (cdr (cdr (cdr x)))))
539
540 (defun cdaaar (x)
541   "Return the `cdr' of the `car' of the `car' of the `car' of X."
542   (cdr (car (car (car x)))))
543
544 (defun cdaadr (x)
545   "Return the `cdr' of the `car' of the `car' of the `cdr' of X."
546   (cdr (car (car (cdr x)))))
547
548 (defun cdadar (x)
549   "Return the `cdr' of the `car' of the `cdr' of the `car' of X."
550   (cdr (car (cdr (car x)))))
551
552 (defun cdaddr (x)
553   "Return the `cdr' of the `car' of the `cdr' of the `cdr' of X."
554   (cdr (car (cdr (cdr x)))))
555
556 (defun cddaar (x)
557   "Return the `cdr' of the `cdr' of the `car' of the `car' of X."
558   (cdr (cdr (car (car x)))))
559
560 (defun cddadr (x)
561   "Return the `cdr' of the `cdr' of the `car' of the `cdr' of X."
562   (cdr (cdr (car (cdr x)))))
563
564 (defun cdddar (x)
565   "Return the `cdr' of the `cdr' of the `cdr' of the `car' of X."
566   (cdr (cdr (cdr (car x)))))
567
568 (defun cddddr (x)
569   "Return the `cdr' of the `cdr' of the `cdr' of the `cdr' of X."
570   (cdr (cdr (cdr (cdr x)))))
571
572 ;;; `last' is implemented as a C primitive, as of 1998-11
573
574 ;(defun last (x &optional n)
575 ;  "Return the last link in the list LIST.
576 ;With optional argument N, return Nth-to-last link (default 1)."
577 ;  (if n
578 ;      (let ((m 0) (p x))
579 ;       (while (consp p) (incf m) (pop p))
580 ;       (if (<= n 0) p
581 ;         (if (< n m) (nthcdr (- m n) x) x)))
582 ;    (while (consp (cdr x)) (pop x))
583 ;    x))
584
585 ;;; `butlast'  is implemented as a C primitive, as of 1998-11
586 ;;; `nbutlast' is implemented as a C primitive, as of 1998-11
587
588 ;(defun butlast (x &optional n)
589 ;  "Return a copy of LIST with the last N elements removed."
590 ;  (if (and n (<= n 0)) x
591 ;    (nbutlast (copy-sequence x) n)))
592
593 ;(defun nbutlast (x &optional n)
594 ;  "Modify LIST to remove the last N elements."
595 ;  (let ((m (length x)))
596 ;    (or n (setq n 1))
597 ;    (and (< n m)
598 ;        (progn
599 ;          (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
600 ;          x))))
601
602 (defun list* (arg &rest rest)   ; See compiler macro in cl-macs.el
603   "Return a new list with specified args as elements, cons'd to last arg.
604 Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
605 `(cons A (cons B (cons C D)))'."
606   (cond ((not rest) arg)
607         ((not (cdr rest)) (cons arg (car rest)))
608         (t (let* ((n (length rest))
609                   (copy (copy-sequence rest))
610                   (last (nthcdr (- n 2) copy)))
611              (setcdr last (car (cdr last)))
612              (cons arg copy)))))
613
614 (defun ldiff (list sublist)
615   "Return a copy of LIST with the tail SUBLIST removed."
616   (let ((res nil))
617     (while (and (consp list) (not (eq list sublist)))
618       (push (pop list) res))
619     (nreverse res)))
620
621 ;;; `copy-list' is implemented as a C primitive, as of 1998-11
622
623 ;(defun copy-list (list)
624 ;  "Return a copy of a list, which may be a dotted list.
625 ;The elements of the list are not copied, just the list structure itself."
626 ;  (if (consp list)
627 ;      (let ((res nil))
628 ;       (while (consp list) (push (pop list) res))
629 ;       (prog1 (nreverse res) (setcdr res list)))
630 ;    (car list)))
631
632 (defun cl-maclisp-member (item list)
633   (while (and list (not (equal item (car list)))) (setq list (cdr list)))
634   list)
635
636 ;;; Define an Emacs 19-compatible `member' for the benefit of Emacs 18 users.
637 (or (and (fboundp 'member) (subrp (symbol-function 'member)))
638     (defalias 'member 'cl-maclisp-member))
639
640 (defalias 'cl-member 'memq)   ; for compatibility with old CL package
641 (defalias 'cl-floor 'floor*)
642 (defalias 'cl-ceiling 'ceiling*)
643 (defalias 'cl-truncate 'truncate*)
644 (defalias 'cl-round 'round*)
645 (defalias 'cl-mod 'mod*)
646
647 (defun adjoin (cl-item cl-list &rest cl-keys)  ; See compiler macro in cl-macs
648   "Return ITEM consed onto the front of LIST only if it's not already there.
649 Otherwise, return LIST unmodified.
650 Keywords supported:  :test :test-not :key"
651   (cond ((or (equal cl-keys '(:test eq))
652              (and (null cl-keys) (not (numberp cl-item))))
653          (if (memq cl-item cl-list) cl-list (cons cl-item cl-list)))
654         ((or (equal cl-keys '(:test equal)) (null cl-keys))
655          (if (member cl-item cl-list) cl-list (cons cl-item cl-list)))
656         (t (apply 'cl-adjoin cl-item cl-list cl-keys))))
657
658 (defun subst (cl-new cl-old cl-tree &rest cl-keys)
659   "Substitute NEW for OLD everywhere in TREE (non-destructively).
660 Return a copy of TREE with all elements `eql' to OLD replaced by NEW.
661 Keywords supported:  :test :test-not :key"
662   (if (or cl-keys (and (numberp cl-old) (not (integerp cl-old))))
663       (apply 'sublis (list (cons cl-old cl-new)) cl-tree cl-keys)
664     (cl-do-subst cl-new cl-old cl-tree)))
665
666 (defun cl-do-subst (cl-new cl-old cl-tree)
667   (cond ((eq cl-tree cl-old) cl-new)
668         ((consp cl-tree)
669          (let ((a (cl-do-subst cl-new cl-old (car cl-tree)))
670                (d (cl-do-subst cl-new cl-old (cdr cl-tree))))
671            (if (and (eq a (car cl-tree)) (eq d (cdr cl-tree)))
672                cl-tree (cons a d))))
673         (t cl-tree)))
674
675 (defun acons (a b c)
676   "Return a new alist created by adding (KEY . VALUE) to ALIST."
677   (cons (cons a b) c))
678
679 (defun pairlis (a b &optional c) (nconc (mapcar* 'cons a b) c))
680
681
682 ;;; Miscellaneous.
683
684 ;; XEmacs change
685 (define-error 'cl-assertion-failed "Assertion failed")
686
687 ;;; This is defined in Emacs 19; define it here for Emacs 18 users.
688 (defun cl-add-hook (hook func &optional append)
689   "Add to hook variable HOOK the function FUNC.
690 FUNC is not added if it already appears on the list stored in HOOK."
691   (let ((old (and (boundp hook) (symbol-value hook))))
692     (and (listp old) (not (eq (car old) 'lambda))
693          (setq old (list old)))
694     (and (not (member func old))
695          (set hook (if append (nconc old (list func)) (cons func old))))))
696 (or (fboundp 'add-hook) (defalias 'add-hook 'cl-add-hook))
697
698 ;; XEmacs change
699 ;(load "cl-defs")
700
701 ;;; Define data for indentation and edebug.
702 (mapcar
703  #'(lambda (entry)
704      (mapcar
705       #'(lambda (func)
706           (put func 'lisp-indent-function (nth 1 entry))
707           (put func 'lisp-indent-hook (nth 1 entry))
708           (or (get func 'edebug-form-spec)
709               (put func 'edebug-form-spec (nth 2 entry))))
710       (car entry)))
711  '(((defun* defmacro*) defun)
712    ((function*) nil
713     (&or symbolp ([&optional 'macro] 'lambda (&rest sexp) &rest form)))
714    ((eval-when) 1 (sexp &rest form))
715    ((when unless) 1 (&rest form))
716    ((declare) nil (&rest sexp))
717    ((the) 1 (sexp &rest form))
718    ((case ecase typecase etypecase) 1 (form &rest (sexp &rest form)))
719    ((block return-from) 1 (sexp &rest form))
720    ((return) nil (&optional form))
721    ((do do*) 2 ((&rest &or symbolp (symbolp &optional form form))
722                 (form &rest form)
723                 &rest form))
724    ((dolist dotimes) 1 ((symbolp form &rest form) &rest form))
725    ((do-symbols) 1 ((symbolp form &optional form form) &rest form))
726    ((do-all-symbols) 1 ((symbolp form &optional form) &rest form))
727    ((psetq setf psetf) nil edebug-setq-form)
728    ((progv) 2 (&rest form))
729    ((flet labels macrolet) 1
730     ((&rest (sexp sexp &rest form)) &rest form))
731    ((symbol-macrolet lexical-let lexical-let*) 1
732     ((&rest &or symbolp (symbolp form)) &rest form))
733    ((multiple-value-bind) 2 ((&rest symbolp) &rest form))
734    ((multiple-value-setq) 1 ((&rest symbolp) &rest form))
735    ((incf decf remf pop push pushnew shiftf rotatef) nil (&rest form))
736    ((letf letf*) 1 ((&rest (&rest form)) &rest form))
737    ((callf destructuring-bind) 2 (sexp form &rest form))
738    ((callf2) 3 (sexp form form &rest form))
739    ((loop) defun (&rest &or symbolp form))
740    ((ignore-errors) 0 (&rest form))))
741
742
743 ;;; This goes here so that cl-macs can find it if it loads right now.
744 (provide 'cl-19)     ; usage: (require 'cl-19 "cl")
745
746
747 ;;; Things to do after byte-compiler is loaded.
748 ;;; As a side effect, we cause cl-macs to be loaded when compiling, so
749 ;;; that the compiler-macros defined there will be present.
750
751 (defvar cl-hacked-flag nil)
752 (defun cl-hack-byte-compiler ()
753   (if (and (not cl-hacked-flag) (fboundp 'byte-compile-file-form))
754       (progn
755         (when (not (fboundp 'cl-compile-time-init))
756           (load "cl-macs" nil t))
757         (cl-compile-time-init)   ; in cl-macs.el
758         (setq cl-hacked-flag t))))
759
760 ;;; Try it now in case the compiler has already been loaded.
761 (cl-hack-byte-compiler)
762
763 ;;; Also make a hook in case compiler is loaded after this file.
764 ;;; The compiler doesn't call any hooks when it loads or runs, but
765 ;;; we can take advantage of the fact that emacs-lisp-mode will be
766 ;;; called when the compiler reads in the file to be compiled.
767 ;;; BUG: If the first compilation is `byte-compile' rather than
768 ;;; `byte-compile-file', we lose.  Oh, well.
769 (add-hook 'emacs-lisp-mode-hook 'cl-hack-byte-compiler)
770
771
772 ;;; The following ensures that packages which expect the old-style cl.el
773 ;;; will be happy with this one.
774
775 (provide 'cl)
776
777 (provide 'mini-cl)   ; for Epoch
778
779 (run-hooks 'cl-load-hook)
780
781 ;;; cl.el ends here