1 ;;; cl.el --- Common Lisp extensions for Emacs
3 ;; Copyright (C) 1993, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Dave Gillespie <daveg@synaptics.com>
8 ;; Keywords: extensions
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs 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.
17 ;; GNU Emacs 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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; These are extensions to Emacs Lisp that provide a degree of
28 ;; Common Lisp compatibility, beyond what is already built-in
31 ;; This package was written by Dave Gillespie; it is a complete
32 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
34 ;; Bug reports, comments, and suggestions are welcome!
36 ;; This file contains the portions of the Common Lisp extensions
37 ;; package which should always be present.
42 ;; Once Emacs 19 becomes standard, many things in this package which are
43 ;; messy for reasons of compatibility can be greatly simplified. For now,
44 ;; I prefer to maintain one unified version.
49 ;; Version 2.02 (30 Jul 93):
50 ;; * Added "cl-compat.el" file, extra compatibility with old package.
51 ;; * Added `lexical-let' and `lexical-let*'.
52 ;; * Added `define-modify-macro', `callf', and `callf2'.
53 ;; * Added `ignore-errors'.
54 ;; * Changed `(setf (nthcdr N PLACE) X)' to work when N is zero.
55 ;; * Merged `*gentemp-counter*' into `*gensym-counter*'.
56 ;; * Extended `subseq' to allow negative START and END like `substring'.
57 ;; * Added `in-ref', `across-ref', `elements of-ref' loop clauses.
58 ;; * Added `concat', `vconcat' loop clauses.
59 ;; * Cleaned up a number of compiler warnings.
61 ;; Version 2.01 (7 Jul 93):
62 ;; * Added support for FSF version of Emacs 19.
63 ;; * Added `add-hook' for Emacs 18 users.
64 ;; * Added `defsubst*' and `symbol-macrolet'.
65 ;; * Added `maplist', `mapc', `mapl', `mapcan', `mapcon'.
66 ;; * Added `map', `concatenate', `reduce', `merge'.
67 ;; * Added `revappend', `nreconc', `tailp', `tree-equal'.
68 ;; * Added `assert', `check-type', `typecase', `typep', and `deftype'.
69 ;; * Added destructuring and `&environment' support to `defmacro*'.
70 ;; * Added destructuring to `loop', and added the following clauses:
71 ;; `elements', `frames', `overlays', `intervals', `buffers', `key-seqs'.
72 ;; * Renamed `delete' to `delete*' and `remove' to `remove*'.
73 ;; * Completed support for all keywords in `remove*', `substitute', etc.
74 ;; * Added `most-positive-float' and company.
75 ;; * Fixed hash tables to work with latest Lucid Emacs.
76 ;; * `proclaim' forms are no longer compile-time-evaluating; use `declaim'.
77 ;; * Syntax for `warn' declarations has changed.
78 ;; * Improved implementation of `random*'.
79 ;; * Moved most sequence functions to a new file, cl-seq.el.
80 ;; * Moved `eval-when' into cl-macs.el.
81 ;; * Moved `pushnew' and `adjoin' to cl.el for most common cases.
82 ;; * Moved `provide' forms down to ends of files.
83 ;; * Changed expansion of `pop' to something that compiles to better code.
84 ;; * Changed so that no patch is required for Emacs 19 byte compiler.
85 ;; * Made more things dependent on `optimize' declarations.
86 ;; * Added a partial implementation of struct print functions.
87 ;; * Miscellaneous minor changes.
90 ;; * First public release of this package.
95 (defvar cl-optimize-speed
1)
96 (defvar cl-optimize-safety
1)
100 (defvar custom-print-functions nil
101 "This is a list of functions that format user objects for printing.
102 Each function is called in turn with three arguments: the object, the
103 stream, and the print level (currently ignored). If it is able to
104 print the object it returns true; otherwise it returns nil and the
105 printer proceeds to the next function on the list.
107 This variable is not used at present, but it is defined in hopes that
108 a future Emacs interpreter will be able to use it.")
110 (defun cl-unload-function ()
111 "Stop unloading of the Common Lisp extensions."
112 (message "Cannot unload the feature `cl'")
113 ;; stop standard unloading!
116 ;;; Generalized variables.
117 ;; These macros are defined here so that they
118 ;; can safely be used in .emacs files.
120 (defmacro incf
(place &optional x
)
121 "Increment PLACE by X (1 by default).
122 PLACE may be a symbol, or any generalized variable allowed by `setf'.
123 The return value is the incremented value of PLACE."
125 (list 'setq place
(if x
(list '+ place x
) (list '1+ place
)))
126 (list 'callf
'+ place
(or x
1))))
128 (defmacro decf
(place &optional x
)
129 "Decrement PLACE by X (1 by default).
130 PLACE may be a symbol, or any generalized variable allowed by `setf'.
131 The return value is the decremented value of PLACE."
133 (list 'setq place
(if x
(list '- place x
) (list '1- place
)))
134 (list 'callf
'- place
(or x
1))))
136 (defmacro pop
(place)
137 "Remove and return the head of the list stored in PLACE.
138 Analogous to (prog1 (car PLACE) (setf PLACE (cdr PLACE))), though more
139 careful about evaluating each argument only once and in the right order.
140 PLACE may be a symbol, or any generalized variable allowed by `setf'."
142 (list 'car
(list 'prog1 place
(list 'setq place
(list 'cdr place
))))
145 (defmacro push
(x place
)
146 "Insert X at the head of the list stored in PLACE.
147 Analogous to (setf PLACE (cons X PLACE)), though more careful about
148 evaluating each argument only once and in the right order. PLACE may
149 be a symbol, or any generalized variable allowed by `setf'."
150 (if (symbolp place
) (list 'setq place
(list 'cons x place
))
151 (list 'callf2
'cons x place
)))
153 (defmacro pushnew
(x place
&rest keys
)
154 "(pushnew X PLACE): insert X at the head of the list if not already there.
155 Like (push X PLACE), except that the list is unmodified if X is `eql' to
156 an element already on the list.
157 \nKeywords supported: :test :test-not :key
158 \n(fn X PLACE [KEYWORD VALUE]...)"
162 (if (memql x
,place
) ,place
(setq ,place
(cons x
,place
))))
163 (list 'setq place
(list* 'adjoin x place keys
)))
164 (list* 'callf2
'adjoin x place keys
)))
166 (defun cl-set-elt (seq n val
)
167 (if (listp seq
) (setcar (nthcdr n seq
) val
) (aset seq n val
)))
169 (defsubst cl-set-nthcdr
(n list x
)
170 (if (<= n
0) x
(setcdr (nthcdr (1- n
) list
) x
) list
))
172 (defun cl-set-buffer-substring (start end val
)
173 (save-excursion (delete-region start end
)
178 (defun cl-set-substring (str start end val
)
179 (if end
(if (< end
0) (incf end
(length str
)))
180 (setq end
(length str
)))
181 (if (< start
0) (incf start
(length str
)))
182 (concat (and (> start
0) (substring str
0 start
))
184 (and (< end
(length str
)) (substring str end
))))
187 ;;; Control structures.
189 ;; These macros are so simple and so often-used that it's better to have
190 ;; them all the time than to load them from cl-macs.el.
192 (defun cl-map-extents (&rest cl-args
)
193 (apply 'cl-map-overlays cl-args
))
196 ;;; Blocks and exits.
198 (defalias 'cl-block-wrapper
'identity
)
199 (defalias 'cl-block-throw
'throw
)
203 ;; True multiple values are not supported, or even
204 ;; simulated. Instead, multiple-value-bind and friends simply expect
205 ;; the target form to return the values as a list.
207 (defsubst values
(&rest values
)
208 "Return multiple values, Common Lisp style.
209 The arguments of `values' are the values
210 that the containing function should return."
213 (defsubst values-list
(list)
214 "Return multiple values, Common Lisp style, taken from a list.
215 LIST specifies the list of values
216 that the containing function should return."
219 (defsubst multiple-value-list
(expression)
220 "Return a list of the multiple values produced by EXPRESSION.
221 This handles multiple values in Common Lisp style, but it does not
222 work right when EXPRESSION calls an ordinary Emacs Lisp function
223 that returns just one value."
226 (defsubst multiple-value-apply
(function expression
)
227 "Evaluate EXPRESSION to get multiple values and apply FUNCTION to them.
228 This handles multiple values in Common Lisp style, but it does not work
229 right when EXPRESSION calls an ordinary Emacs Lisp function that returns just
231 (apply function expression
))
233 (defalias 'multiple-value-call
'apply
234 "Apply FUNCTION to ARGUMENTS, taking multiple values into account.
235 This implementation only handles the case where there is only one argument.")
237 (defsubst nth-value
(n expression
)
238 "Evaluate EXPRESSION to get multiple values and return the Nth one.
239 This handles multiple values in Common Lisp style, but it does not work
240 right when EXPRESSION calls an ordinary Emacs Lisp function that returns just
246 (defvar cl-macro-environment nil
)
247 (defvar cl-old-macroexpand
(prog1 (symbol-function 'macroexpand
)
248 (defalias 'macroexpand
'cl-macroexpand
)))
250 (defun cl-macroexpand (cl-macro &optional cl-env
)
251 "Return result of expanding macros at top level of FORM.
252 If FORM is not a macro call, it is returned unchanged.
253 Otherwise, the macro is expanded and the expansion is considered
254 in place of FORM. When a non-macro-call results, it is returned.
256 The second optional arg ENVIRONMENT specifies an environment of macro
257 definitions to shadow the loaded ones for use in file byte-compilation.
258 \n(fn FORM &optional ENVIRONMENT)"
259 (let ((cl-macro-environment cl-env
))
260 (while (progn (setq cl-macro
(funcall cl-old-macroexpand cl-macro cl-env
))
261 (and (symbolp cl-macro
)
262 (cdr (assq (symbol-name cl-macro
) cl-env
))))
263 (setq cl-macro
(cadr (assq (symbol-name cl-macro
) cl-env
))))
269 (defvar cl-compiling-file nil
)
270 (defun cl-compiling-file ()
271 (or cl-compiling-file
272 (and (boundp 'outbuffer
) (bufferp (symbol-value 'outbuffer
))
273 (equal (buffer-name (symbol-value 'outbuffer
))
274 " *Compiler Output*"))))
276 (defvar cl-proclaims-deferred nil
)
278 (defun proclaim (spec)
279 (if (fboundp 'cl-do-proclaim
) (cl-do-proclaim spec t
)
280 (push spec cl-proclaims-deferred
))
283 (defmacro declaim
(&rest specs
)
284 (let ((body (mapcar (function (lambda (x) (list 'proclaim
(list 'quote x
))))
286 (if (cl-compiling-file) (list* 'eval-when
'(compile load eval
) body
)
287 (cons 'progn body
)))) ; avoid loading cl-macs.el for eval-when
292 (defun cl-random-time ()
293 (let* ((time (copy-sequence (current-time-string))) (i (length time
)) (v 0))
294 (while (>= (decf i
) 0) (setq v
(+ (* v
3) (aref time i
))))
297 (defvar *gensym-counter
* (* (logand (cl-random-time) 1023) 100))
302 (defun floatp-safe (object)
303 "Return t if OBJECT is a floating point number.
304 On Emacs versions that lack floating-point support, this function
306 (and (numberp object
) (not (integerp object
))))
308 (defun plusp (number)
309 "Return t if NUMBER is positive."
312 (defun minusp (number)
313 "Return t if NUMBER is negative."
316 (defun oddp (integer)
317 "Return t if INTEGER is odd."
318 (eq (logand integer
1) 1))
320 (defun evenp (integer)
321 "Return t if INTEGER is even."
322 (eq (logand integer
1) 0))
324 (defvar *random-state
* (vector 'cl-random-state-tag -
1 30 (cl-random-time)))
326 ;; The following are actually set by cl-float-limits.
327 (defconst most-positive-float nil
)
328 (defconst most-negative-float nil
)
329 (defconst least-positive-float nil
)
330 (defconst least-negative-float nil
)
331 (defconst least-positive-normalized-float nil
)
332 (defconst least-negative-normalized-float nil
)
333 (defconst float-epsilon nil
)
334 (defconst float-negative-epsilon nil
)
337 ;;; Sequence functions.
339 (defalias 'copy-seq
'copy-sequence
)
341 (defun mapcar* (cl-func cl-x
&rest cl-rest
)
342 "Apply FUNCTION to each element of SEQ, and make a list of the results.
343 If there are several SEQs, FUNCTION is called with that many arguments,
344 and mapping stops as soon as the shortest list runs out. With just one
345 SEQ, this is like `mapcar'. With several, it is like the Common Lisp
346 `mapcar' function extended to arbitrary sequence types.
347 \n(fn FUNCTION SEQ...)"
349 (if (or (cdr cl-rest
) (nlistp cl-x
) (nlistp (car cl-rest
)))
350 (cl-mapcar-many cl-func
(cons cl-x cl-rest
))
351 (let ((cl-res nil
) (cl-y (car cl-rest
)))
352 (while (and cl-x cl-y
)
353 (push (funcall cl-func
(pop cl-x
) (pop cl-y
)) cl-res
))
355 (mapcar cl-func cl-x
)))
357 (defalias 'svref
'aref
)
361 (defalias 'first
'car
)
362 (defalias 'second
'cadr
)
363 (defalias 'rest
'cdr
)
364 (defalias 'endp
'null
)
367 "Return the third element of the list X."
371 "Return the fourth element of the list X."
375 "Return the fifth element of the list X."
379 "Return the sixth element of the list X."
383 "Return the seventh element of the list X."
387 "Return the eighth element of the list X."
391 "Return the ninth element of the list X."
395 "Return the tenth element of the list X."
399 "Return the `car' of the `car' of the `car' of X."
403 "Return the `car' of the `car' of the `cdr' of X."
407 "Return the `car' of the `cdr' of the `car' of X."
411 "Return the `car' of the `cdr' of the `cdr' of X."
415 "Return the `cdr' of the `car' of the `car' of X."
419 "Return the `cdr' of the `car' of the `cdr' of X."
423 "Return the `cdr' of the `cdr' of the `car' of X."
427 "Return the `cdr' of the `cdr' of the `cdr' of X."
431 "Return the `car' of the `car' of the `car' of the `car' of X."
432 (car (car (car (car x
)))))
435 "Return the `car' of the `car' of the `car' of the `cdr' of X."
436 (car (car (car (cdr x
)))))
439 "Return the `car' of the `car' of the `cdr' of the `car' of X."
440 (car (car (cdr (car x
)))))
443 "Return the `car' of the `car' of the `cdr' of the `cdr' of X."
444 (car (car (cdr (cdr x
)))))
447 "Return the `car' of the `cdr' of the `car' of the `car' of X."
448 (car (cdr (car (car x
)))))
451 "Return the `car' of the `cdr' of the `car' of the `cdr' of X."
452 (car (cdr (car (cdr x
)))))
455 "Return the `car' of the `cdr' of the `cdr' of the `car' of X."
456 (car (cdr (cdr (car x
)))))
459 "Return the `car' of the `cdr' of the `cdr' of the `cdr' of X."
460 (car (cdr (cdr (cdr x
)))))
463 "Return the `cdr' of the `car' of the `car' of the `car' of X."
464 (cdr (car (car (car x
)))))
467 "Return the `cdr' of the `car' of the `car' of the `cdr' of X."
468 (cdr (car (car (cdr x
)))))
471 "Return the `cdr' of the `car' of the `cdr' of the `car' of X."
472 (cdr (car (cdr (car x
)))))
475 "Return the `cdr' of the `car' of the `cdr' of the `cdr' of X."
476 (cdr (car (cdr (cdr x
)))))
479 "Return the `cdr' of the `cdr' of the `car' of the `car' of X."
480 (cdr (cdr (car (car x
)))))
483 "Return the `cdr' of the `cdr' of the `car' of the `cdr' of X."
484 (cdr (cdr (car (cdr x
)))))
487 "Return the `cdr' of the `cdr' of the `cdr' of the `car' of X."
488 (cdr (cdr (cdr (car x
)))))
491 "Return the `cdr' of the `cdr' of the `cdr' of the `cdr' of X."
492 (cdr (cdr (cdr (cdr x
)))))
494 ;;(defun last* (x &optional n)
495 ;; "Returns the last link in the list LIST.
496 ;;With optional argument N, returns Nth-to-last link (default 1)."
498 ;; (let ((m 0) (p x))
499 ;; (while (consp p) (incf m) (pop p))
501 ;; (if (< n m) (nthcdr (- m n) x) x)))
502 ;; (while (consp (cdr x)) (pop x))
505 (defun list* (arg &rest rest
) ; See compiler macro in cl-macs.el
506 "Return a new list with specified ARGs as elements, consed to last ARG.
507 Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
508 `(cons A (cons B (cons C D)))'.
510 (cond ((not rest
) arg
)
511 ((not (cdr rest
)) (cons arg
(car rest
)))
512 (t (let* ((n (length rest
))
513 (copy (copy-sequence rest
))
514 (last (nthcdr (- n
2) copy
)))
515 (setcdr last
(car (cdr last
)))
518 (defun ldiff (list sublist
)
519 "Return a copy of LIST with the tail SUBLIST removed."
521 (while (and (consp list
) (not (eq list sublist
)))
522 (push (pop list
) res
))
525 (defun copy-list (list)
526 "Return a copy of LIST, which may be a dotted list.
527 The elements of LIST are not copied, just the list structure itself."
530 (while (consp list
) (push (pop list
) res
))
531 (prog1 (nreverse res
) (setcdr res list
)))
534 (defun cl-maclisp-member (item list
)
535 (while (and list
(not (equal item
(car list
)))) (setq list
(cdr list
)))
538 (defalias 'cl-member
'memq
) ; for compatibility with old CL package
539 (defalias 'cl-floor
'floor
*)
540 (defalias 'cl-ceiling
'ceiling
*)
541 (defalias 'cl-truncate
'truncate
*)
542 (defalias 'cl-round
'round
*)
543 (defalias 'cl-mod
'mod
*)
545 (defun adjoin (cl-item cl-list
&rest cl-keys
) ; See compiler macro in cl-macs
546 "Return ITEM consed onto the front of LIST only if it's not already there.
547 Otherwise, return LIST unmodified.
548 \nKeywords supported: :test :test-not :key
549 \n(fn ITEM LIST [KEYWORD VALUE]...)"
550 (cond ((or (equal cl-keys
'(:test eq
))
551 (and (null cl-keys
) (not (numberp cl-item
))))
552 (if (memq cl-item cl-list
) cl-list
(cons cl-item cl-list
)))
553 ((or (equal cl-keys
'(:test equal
)) (null cl-keys
))
554 (if (member cl-item cl-list
) cl-list
(cons cl-item cl-list
)))
555 (t (apply 'cl-adjoin cl-item cl-list cl-keys
))))
557 (defun subst (cl-new cl-old cl-tree
&rest cl-keys
)
558 "Substitute NEW for OLD everywhere in TREE (non-destructively).
559 Return a copy of TREE with all elements `eql' to OLD replaced by NEW.
560 \nKeywords supported: :test :test-not :key
561 \n(fn NEW OLD TREE [KEYWORD VALUE]...)"
562 (if (or cl-keys
(and (numberp cl-old
) (not (integerp cl-old
))))
563 (apply 'sublis
(list (cons cl-old cl-new
)) cl-tree cl-keys
)
564 (cl-do-subst cl-new cl-old cl-tree
)))
566 (defun cl-do-subst (cl-new cl-old cl-tree
)
567 (cond ((eq cl-tree cl-old
) cl-new
)
569 (let ((a (cl-do-subst cl-new cl-old
(car cl-tree
)))
570 (d (cl-do-subst cl-new cl-old
(cdr cl-tree
))))
571 (if (and (eq a
(car cl-tree
)) (eq d
(cdr cl-tree
)))
572 cl-tree
(cons a d
))))
575 (defun acons (key value alist
)
576 "Add KEY and VALUE to ALIST.
577 Return a new list with (cons KEY VALUE) as car and ALIST as cdr."
578 (cons (cons key value
) alist
))
580 (defun pairlis (keys values
&optional alist
)
581 "Make an alist from KEYS and VALUES.
582 Return a new alist composed by associating KEYS to corresponding VALUES;
583 the process stops as soon as KEYS or VALUES run out.
584 If ALIST is non-nil, the new pairs are prepended to it."
585 (nconc (mapcar* 'cons keys values
) alist
))
590 ;; Define data for indentation and edebug.
592 '(((defun* defmacro
*) 2)
594 (&or symbolp
([&optional
'macro
] 'lambda
(&rest sexp
) &rest form
)))
595 ((eval-when) 1 (sexp &rest form
))
596 ((declare) nil
(&rest sexp
))
597 ((the) 1 (sexp &rest form
))
598 ((case ecase typecase etypecase
) 1 (form &rest
(sexp &rest form
)))
599 ((block return-from
) 1 (sexp &rest form
))
600 ((return) nil
(&optional form
))
601 ((do do
*) 2 ((&rest
&or symbolp
(symbolp &optional form form
))
604 ((do-symbols) 1 ((symbolp form
&optional form form
) &rest form
))
605 ((do-all-symbols) 1 ((symbolp form
&optional form
) &rest form
))
606 ((psetq setf psetf
) nil edebug-setq-form
)
607 ((progv) 2 (&rest form
))
608 ((flet labels macrolet
) 1
609 ((&rest
(sexp sexp
&rest form
)) &rest form
))
610 ((symbol-macrolet lexical-let lexical-let
*) 1
611 ((&rest
&or symbolp
(symbolp form
)) &rest form
))
612 ((multiple-value-bind) 2 ((&rest symbolp
) &rest form
))
613 ((multiple-value-setq) 1 ((&rest symbolp
) &rest form
))
614 ((incf decf remf pushnew shiftf rotatef
) nil
(&rest form
))
615 ((letf letf
*) 1 ((&rest
(&rest form
)) &rest form
))
616 ((callf destructuring-bind
) 2 (sexp form
&rest form
))
617 ((callf2) 3 (sexp form form
&rest form
))
618 ((loop) nil
(&rest
&or symbolp form
))
619 ((ignore-errors) 0 (&rest form
))))
620 (dolist (func (car entry
))
621 (put func
'lisp-indent-function
(nth 1 entry
))
622 (put func
'lisp-indent-hook
(nth 1 entry
))
623 (or (get func
'edebug-form-spec
)
624 (put func
'edebug-form-spec
(nth 2 entry
)))))
626 ;; Autoload the other portions of the package.
627 ;; We want to replace the basic versions of dolist, dotimes, declare below.
628 (fmakunbound 'dolist
)
629 (fmakunbound 'dotimes
)
630 (fmakunbound 'declare
)
631 (load "cl-loaddefs" nil
'quiet
)
633 ;; This goes here so that cl-macs can find it if it loads right now.
634 (provide 'cl-19
) ; usage: (require 'cl-19 "cl")
636 ;; Things to do after byte-compiler is loaded.
637 ;; As a side effect, we cause cl-macs to be loaded when compiling, so
638 ;; that the compiler-macros defined there will be present.
640 (defvar cl-hacked-flag nil
)
641 (defun cl-hack-byte-compiler ()
642 (if (and (not cl-hacked-flag
) (fboundp 'byte-compile-file-form
))
644 (setq cl-hacked-flag t
) ; Do it first, to prevent recursion.
645 (cl-compile-time-init)))) ; In cl-macs.el.
647 ;; Try it now in case the compiler has already been loaded.
648 (cl-hack-byte-compiler)
650 ;; Also make a hook in case compiler is loaded after this file.
651 (add-hook 'bytecomp-load-hook
'cl-hack-byte-compiler
)
654 ;; The following ensures that packages which expect the old-style cl.el
655 ;; will be happy with this one.
659 (run-hooks 'cl-load-hook
)
662 ;; byte-compile-dynamic: t
663 ;; byte-compile-warnings: (not cl-functions)
666 ;; arch-tag: 5f07fa74-f153-4524-9303-21f5be125851