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