]> code.delx.au - gnu-emacs/blobdiff - lisp/emacs-lisp/cl.el
(lisp-fill-paragraph): Fix backslashes.
[gnu-emacs] / lisp / emacs-lisp / cl.el
index 3d0a3c496026af32cb08c1e49113474a121677cb..b098a467f9f518219a82a768da6314674c9fb4be 100644 (file)
@@ -1,4 +1,4 @@
-;; cl.el --- Common Lisp extensions for GNU Emacs Lisp
+;;; cl.el --- Common Lisp extensions for Emacs -*-byte-compile-dynamic: t;-*-
 
 ;; Copyright (C) 1993 Free Software Foundation, Inc.
 
@@ -10,7 +10,7 @@
 
 ;; GNU Emacs is free software; you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 1, or (at your option)
+;; the Free Software Foundation; either version 2, or (at your option)
 ;; any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
@@ -19,8 +19,9 @@
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING.  If not, write to
-;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+;; Boston, MA 02111-1307, USA.
 
 ;;; Commentary:
 
@@ -31,8 +32,6 @@
 ;; This package was written by Dave Gillespie; it is a complete
 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986.
 ;;
-;; This package works with Emacs 18, Emacs 19, and Lucid Emacs 19.
-;;
 ;; Bug reports, comments, and suggestions are welcome!
 
 ;; This file contains the portions of the Common Lisp extensions
 
 ;;; Code:
 
-(defvar cl-emacs-type (cond ((or (and (fboundp 'epoch::version)
-                                     (symbol-value 'epoch::version))
-                                (string-lessp emacs-version "19")) 18)
-                           ((string-match "Lucid" emacs-version) 'lucid)
-                           (t 19)))
-
-(or (fboundp 'defalias) (fset 'defalias 'fset))
-
 (defvar cl-optimize-speed 1)
 (defvar cl-optimize-safety 1)
 
 
-;;; Keywords used in this package.
-
-(defconst :test ':test)
-(defconst :test-not ':test-not)
-(defconst :key ':key)
-(defconst :start ':start)
-(defconst :start1 ':start1)
-(defconst :start2 ':start2)
-(defconst :end ':end)
-(defconst :end1 ':end1)
-(defconst :end2 ':end2)
-(defconst :count ':count)
-(defconst :initial-value ':initial-value)
-(defconst :size ':size)
-(defconst :from-end ':from-end)
-(defconst :rehash-size ':rehash-size)
-(defconst :rehash-threshold ':rehash-threshold)
-(defconst :allow-other-keys ':allow-other-keys)
-
-
+;;;###autoload
 (defvar custom-print-functions nil
   "This is a list of functions that format user objects for printing.
 Each function is called in turn with three arguments: the object, the
@@ -136,6 +108,10 @@ printer proceeds to the next function on the list.
 This variable is not used at present, but it is defined in hopes that
 a future Emacs interpreter will be able to use it.")
 
+(defvar cl-unload-hook '(cl-cannot-unload)
+  "Prevent unloading the feature `cl', since it does not work.")
+(defun cl-cannot-unload ()
+  (error "Cannot unload the feature `cl'"))
 
 ;;; Predicates.
 
@@ -151,7 +127,7 @@ Floating-point numbers of equal value are `eql', but they may not be `eq'."
 ;;; can safely be used in .emacs files.
 
 (defmacro incf (place &optional x)
-  "(incf PLACE [X]): increment PLACE by X (1 by default).
+  "Increment PLACE by X (1 by default).
 PLACE may be a symbol, or any generalized variable allowed by `setf'.
 The return value is the incremented value of PLACE."
   (if (symbolp place)
@@ -159,7 +135,7 @@ The return value is the incremented value of PLACE."
     (list 'callf '+ place (or x 1))))
 
 (defmacro decf (place &optional x)
-  "(decf PLACE [X]): decrement PLACE by X (1 by default).
+  "Decrement PLACE by X (1 by default).
 PLACE may be a symbol, or any generalized variable allowed by `setf'.
 The return value is the decremented value of PLACE."
   (if (symbolp place)
@@ -167,7 +143,7 @@ The return value is the decremented value of PLACE."
     (list 'callf '- place (or x 1))))
 
 (defmacro pop (place)
-  "(pop PLACE): remove and return the head of the list stored in PLACE.
+  "Remove and return the head of the list stored in PLACE.
 Analogous to (prog1 (car PLACE) (setf PLACE (cdr PLACE))), though more
 careful about evaluating each argument only once and in the right order.
 PLACE may be a symbol, or any generalized variable allowed by `setf'."
@@ -176,7 +152,7 @@ PLACE may be a symbol, or any generalized variable allowed by `setf'."
     (cl-do-pop place)))
 
 (defmacro push (x place)
-  "(push X PLACE): insert X at the head of the list stored in PLACE.
+  "Insert X at the head of the list stored in PLACE.
 Analogous to (setf PLACE (cons X PLACE)), though more careful about
 evaluating each argument only once and in the right order.  PLACE may
 be a symbol, or any generalized variable allowed by `setf'."
@@ -206,7 +182,7 @@ Keywords supported:  :test :test-not :key"
 (defun cl-set-substring (str start end val)
   (if end (if (< end 0) (incf end (length str)))
     (setq end (length str)))
-  (if (< start 0) (incf start str))
+  (if (< start 0) (incf start (length str)))
   (concat (and (> start 0) (substring str 0 start))
          val
          (and (< end (length str)) (substring str end))))
@@ -217,17 +193,8 @@ Keywords supported:  :test :test-not :key"
 ;;; These macros are so simple and so often-used that it's better to have
 ;;; them all the time than to load them from cl-macs.el.
 
-(defmacro when (cond &rest body)
-  "(when COND BODY...): if COND yields non-nil, do BODY, else return nil."
-  (list 'if cond (cons 'progn body)))
-
-(defmacro unless (cond &rest body)
-  "(unless COND BODY...): if COND yields nil, do BODY, else return nil."
-  (cons 'if (cons cond (cons nil body))))
-
 (defun cl-map-extents (&rest cl-args)
-  (if (fboundp 'next-overlay-at) (apply 'cl-map-overlays cl-args)
-    (if (fboundp 'map-extents) (apply 'map-extents cl-args))))
+  (apply 'cl-map-overlays cl-args))
 
 
 ;;; Blocks and exits.
@@ -240,12 +207,42 @@ Keywords supported:  :test :test-not :key"
 ;;; simulated.  Instead, multiple-value-bind and friends simply expect
 ;;; the target form to return the values as a list.
 
-(defalias 'values 'list)
-(defalias 'values-list 'identity)
-(defalias 'multiple-value-list 'identity)
-(defalias 'multiple-value-call 'apply)  ; only works for one arg
-(defalias 'nth-value 'nth)
+(defsubst values (&rest values)
+  "Return multiple values, Common Lisp style.
+The arguments of `values' are the values
+that the containing function should return."
+  values)
 
+(defsubst values-list (list)
+  "Return multiple values, Common Lisp style, taken from a list.
+LIST specifies the list of values
+that the containing function should return."
+  list)
+
+(defsubst multiple-value-list (expression)
+  "Return a list of the multiple values produced by EXPRESSION.
+This handles multiple values in Common Lisp style, but it does not
+work right when EXPRESSION calls an ordinary Emacs Lisp function
+that returns just one value."
+  expression)
+
+(defsubst multiple-value-apply (function expression)
+  "Evaluate EXPRESSION to get multiple values and apply FUNCTION to them.
+This handles multiple values in Common Lisp style, but it does not work
+right when EXPRESSION calls an ordinary Emacs Lisp function that returns just
+one value."
+  (apply function expression))
+
+(defalias 'multiple-value-call 'apply
+  "Apply FUNCTION to ARGUMENTS, taking multiple values into account.
+This implementation only handles the case where there is only one argument.")
+
+(defsubst nth-value (n expression)
+  "Evaluate EXPRESSION to get multiple values and return the Nth one.
+This handles multiple values in Common Lisp style, but it does not work
+right when EXPRESSION calls an ordinary Emacs Lisp function that returns just
+one value."
+  (nth n expression))
 
 ;;; Macros.
 
@@ -259,7 +256,7 @@ If FORM is not a macro call, it is returned unchanged.
 Otherwise, the macro is expanded and the expansion is considered
 in place of FORM.  When a non-macro-call results, it is returned.
 
-The second optional arg ENVIRONMENT species an environment of macro
+The second optional arg ENVIRONMENT specifies an environment of macro
 definitions to shadow the loaded ones for use in file byte-compilation."
   (let ((cl-macro-environment cl-env))
     (while (progn (setq cl-macro (funcall cl-old-macroexpand cl-macro cl-env))
@@ -326,17 +323,8 @@ always returns nil."
   "T if INTEGER is even."
   (eq (logand x 1) 0))
 
-(defun cl-abs (x)
-  "Return the absolute value of ARG."
-  (if (>= x 0) x (- x)))
-(or (fboundp 'abs) (defalias 'abs 'cl-abs))   ; This is built-in to Emacs 19
-
 (defvar *random-state* (vector 'cl-random-state-tag -1 30 (cl-random-time)))
 
-;;; We use `eval' in case VALBITS differs from compile-time to load-time.
-(defconst most-positive-fixnum (eval '(lsh -1 -1)))
-(defconst most-negative-fixnum (eval '(- -1 (lsh -1 -1))))
-
 ;;; The following are actually set by cl-float-limits.
 (defconst most-positive-float nil)
 (defconst most-negative-float nil)
@@ -367,65 +355,47 @@ SEQ, this is like `mapcar'.  With several, it is like the Common Lisp
          (nreverse cl-res)))
     (mapcar cl-func cl-x)))
 
+(defalias 'svref 'aref)
 
 ;;; List functions.
 
 (defalias 'first 'car)
+(defalias 'second 'cadr)
 (defalias 'rest 'cdr)
 (defalias 'endp 'null)
 
-(defun second (x)
-  "Return the second element of the list LIST."
-  (car (cdr x)))
-
 (defun third (x)
-  "Return the third element of the list LIST."
+  "Return the third element of the list X."
   (car (cdr (cdr x))))
 
 (defun fourth (x)
-  "Return the fourth element of the list LIST."
+  "Return the fourth element of the list X."
   (nth 3 x))
 
 (defun fifth (x)
-  "Return the fifth element of the list LIST."
+  "Return the fifth element of the list X."
   (nth 4 x))
 
 (defun sixth (x)
-  "Return the sixth element of the list LIST."
+  "Return the sixth element of the list X."
   (nth 5 x))
 
 (defun seventh (x)
-  "Return the seventh element of the list LIST."
+  "Return the seventh element of the list X."
   (nth 6 x))
 
 (defun eighth (x)
-  "Return the eighth element of the list LIST."
+  "Return the eighth element of the list X."
   (nth 7 x))
 
 (defun ninth (x)
-  "Return the ninth element of the list LIST."
+  "Return the ninth element of the list X."
   (nth 8 x))
 
 (defun tenth (x)
-  "Return the tenth element of the list LIST."
+  "Return the tenth element of the list X."
   (nth 9 x))
 
-(defun caar (x)
-  "Return the `car' of the `car' of X."
-  (car (car x)))
-
-(defun cadr (x)
-  "Return the `car' of the `cdr' of X."
-  (car (cdr x)))
-
-(defun cdar (x)
-  "Return the `cdr' of the `car' of X."
-  (cdr (car x)))
-
-(defun cddr (x)
-  "Return the `cdr' of the `cdr' of X."
-  (cdr (cdr x)))
-
 (defun caaar (x)
   "Return the `car' of the `car' of the `car' of X."
   (car (car (car x))))
@@ -522,30 +492,16 @@ SEQ, this is like `mapcar'.  With several, it is like the Common Lisp
   "Return the `cdr' of the `cdr' of the `cdr' of the `cdr' of X."
   (cdr (cdr (cdr (cdr x)))))
 
-(defun last (x &optional n)
-  "Returns the last link in the list LIST.
-With optional argument N, returns Nth-to-last link (default 1)."
-  (if n
-      (let ((m 0) (p x))
-       (while (consp p) (incf m) (pop p))
-       (if (<= n 0) p
-         (if (< n m) (nthcdr (- m n) x) x)))
-    (while (consp (cdr x)) (pop x))
-    x))
-
-(defun butlast (x &optional n)
-  "Returns a copy of LIST with the last N elements removed."
-  (if (and n (<= n 0)) x
-    (nbutlast (copy-sequence x) n)))
-
-(defun nbutlast (x &optional n)
-  "Modifies LIST to remove the last N elements."
-  (let ((m (length x)))
-    (or n (setq n 1))
-    (and (< n m)
-        (progn
-          (if (> n 0) (setcdr (nthcdr (- (1- m) n) x) nil))
-          x))))
+;;(defun last* (x &optional n)
+;;  "Returns the last link in the list LIST.
+;;With optional argument N, returns Nth-to-last link (default 1)."
+;;  (if n
+;;      (let ((m 0) (p x))
+;;     (while (consp p) (incf m) (pop p))
+;;     (if (<= n 0) p
+;;       (if (< n m) (nthcdr (- m n) x) x)))
+;;    (while (consp (cdr x)) (pop x))
+;;    x))
 
 (defun list* (arg &rest rest)   ; See compiler macro in cl-macs.el
   "Return a new list with specified args as elements, cons'd to last arg.
@@ -579,10 +535,6 @@ The elements of the list are not copied, just the list structure itself."
   (while (and list (not (equal item (car list)))) (setq list (cdr list)))
   list)
 
-;;; Define an Emacs 19-compatible `member' for the benefit of Emacs 18 users.
-(or (and (fboundp 'member) (subrp (symbol-function 'member)))
-    (defalias 'member 'cl-maclisp-member))
-
 (defalias 'cl-member 'memq)   ; for compatibility with old CL package
 (defalias 'cl-floor 'floor*)
 (defalias 'cl-ceiling 'ceiling*)
@@ -627,46 +579,42 @@ Keywords supported:  :test :test-not :key"
 (put 'cl-assertion-failed 'error-conditions '(error))
 (put 'cl-assertion-failed 'error-message "Assertion failed")
 
-;;; This is defined in Emacs 19; define it here for Emacs 18 users.
-(defun cl-add-hook (hook func &optional append)
-  "Add to hook variable HOOK the function FUNC.
-FUNC is not added if it already appears on the list stored in HOOK."
-  (let ((old (and (boundp hook) (symbol-value hook))))
-    (and (listp old) (not (eq (car old) 'lambda))
-        (setq old (list old)))
-    (and (not (member func old))
-        (set hook (if append (nconc old (list func)) (cons func old))))))
-(or (fboundp 'add-hook) (defalias 'add-hook 'cl-add-hook))
-
+(defvar cl-fake-autoloads nil
+  "Non-nil means don't make CL functions autoload.")
 
 ;;; Autoload the other portions of the package.
+;; We want to replace the basic versions of dolist, dotimes, declare below.
+(fmakunbound 'dolist)
+(fmakunbound 'dotimes)
+(fmakunbound 'declare)
 (mapcar (function
         (lambda (set)
-          (mapcar (function
-                   (lambda (func)
-                     (autoload func (car set) nil nil (nth 1 set))))
-                  (cddr set))))
+          (let ((file (if cl-fake-autoloads "<none>" (car set))))
+            (mapcar (function
+                     (lambda (func)
+                       (autoload func (car set) nil nil (nth 1 set))))
+                    (cddr set)))))
        '(("cl-extra" nil
           coerce equalp cl-map-keymap maplist mapc mapl mapcan mapcon
           cl-map-keymap cl-map-keymap-recursively cl-map-intervals
           cl-map-overlays cl-set-frame-visible-p cl-float-limits
-          gcd lcm isqrt expt floor* ceiling* truncate* round*
+          gcd lcm isqrt floor* ceiling* truncate* round*
           mod* rem* signum random* make-random-state random-state-p
           subseq concatenate cl-mapcar-many map some every notany
           notevery revappend nreconc list-length tailp copy-tree get* getf
-          cl-set-getf cl-do-remf remprop make-hash-table cl-hash-lookup
-          gethash cl-puthash remhash clrhash maphash hash-table-p
-          hash-table-count cl-progv-before cl-prettyexpand
+          cl-set-getf cl-do-remf remprop cl-make-hash-table cl-hash-lookup
+          cl-gethash cl-puthash cl-remhash cl-clrhash cl-maphash cl-hash-table-p
+          cl-hash-table-count cl-progv-before cl-prettyexpand
           cl-macroexpand-all)
          ("cl-seq" nil
-          reduce fill replace remq remove remove* remove-if remove-if-not
-          delete delete* delete-if delete-if-not remove-duplicates
+          reduce fill replace remove* remove-if remove-if-not
+          delete* delete-if delete-if-not remove-duplicates
           delete-duplicates substitute substitute-if substitute-if-not
           nsubstitute nsubstitute-if nsubstitute-if-not find find-if
           find-if-not position position-if position-if-not count count-if
           count-if-not mismatch search sort* stable-sort merge member*
           member-if member-if-not cl-adjoin assoc* assoc-if assoc-if-not
-          rassoc* rassoc rassoc-if rassoc-if-not union nunion intersection
+          rassoc* rassoc-if rassoc-if-not union nunion intersection
           nintersection set-difference nset-difference set-exclusive-or
           nset-exclusive-or subsetp subst-if subst-if-not nsubst nsubst-if
           nsubst-if-not sublis nsublis tree-equal)
@@ -675,7 +623,7 @@ FUNC is not added if it already appears on the list stored in HOOK."
           cl-struct-setf-expander compiler-macroexpand cl-compile-time-init)
          ("cl-macs" t
           defun* defmacro* function* destructuring-bind eval-when
-          eval-when-compile load-time-value case ecase typecase etypecase
+          load-time-value case ecase typecase etypecase
           block return return-from loop do do* dolist dotimes do-symbols
           do-all-symbols psetq progv flet labels macrolet symbol-macrolet
           lexical-let lexical-let* multiple-value-bind multiple-value-setq
@@ -697,7 +645,6 @@ FUNC is not added if it already appears on the list stored in HOOK."
          ((function*) nil
           (&or symbolp ([&optional 'macro] 'lambda (&rest sexp) &rest form)))
          ((eval-when) 1 (sexp &rest form))
-         ((when unless) 1 (&rest form))
          ((declare) nil (&rest sexp))
          ((the) 1 (sexp &rest form))
          ((case ecase typecase etypecase) 1 (form &rest (sexp &rest form)))
@@ -706,7 +653,6 @@ FUNC is not added if it already appears on the list stored in HOOK."
          ((do do*) 2 ((&rest &or symbolp (symbolp &optional form form))
                       (form &rest form)
                       &rest form))
-         ((dolist dotimes) 1 ((symbolp form &rest form) &rest form))
          ((do-symbols) 1 ((symbolp form &optional form form) &rest form))
          ((do-all-symbols) 1 ((symbolp form &optional form) &rest form))
          ((psetq setf psetf) nil edebug-setq-form)
@@ -717,7 +663,7 @@ FUNC is not added if it already appears on the list stored in HOOK."
           ((&rest &or symbolp (symbolp form)) &rest form))
          ((multiple-value-bind) 2 ((&rest symbolp) &rest form))
          ((multiple-value-setq) 1 ((&rest symbolp) &rest form))
-         ((incf decf remf pop push pushnew shiftf rotatef) nil (&rest form))
+         ((incf decf remf pushnew shiftf rotatef) nil (&rest form))
          ((letf letf*) 1 ((&rest (&rest form)) &rest form))
          ((callf destructuring-bind) 2 (sexp form &rest form))
          ((callf2) 3 (sexp form form &rest form))
@@ -737,19 +683,14 @@ FUNC is not added if it already appears on the list stored in HOOK."
 (defun cl-hack-byte-compiler ()
   (if (and (not cl-hacked-flag) (fboundp 'byte-compile-file-form))
       (progn
-       (cl-compile-time-init)   ; in cl-macs.el
-       (setq cl-hacked-flag t))))
+       (setq cl-hacked-flag t)         ; Do it first, to prevent recursion.
+       (cl-compile-time-init))))       ; In cl-macs.el.
 
 ;;; Try it now in case the compiler has already been loaded.
 (cl-hack-byte-compiler)
 
 ;;; Also make a hook in case compiler is loaded after this file.
-;;; The compiler doesn't call any hooks when it loads or runs, but
-;;; we can take advantage of the fact that emacs-lisp-mode will be
-;;; called when the compiler reads in the file to be compiled.
-;;; BUG: If the first compilation is `byte-compile' rather than
-;;; `byte-compile-file', we lose.  Oh, well.
-(add-hook 'emacs-lisp-mode-hook 'cl-hack-byte-compiler)
+(add-hook 'bytecomp-load-hook 'cl-hack-byte-compiler)
 
 
 ;;; The following ensures that packages which expect the old-style cl.el
@@ -757,8 +698,7 @@ FUNC is not added if it already appears on the list stored in HOOK."
 
 (provide 'cl)
 
-(provide 'mini-cl)   ; for Epoch
-
 (run-hooks 'cl-load-hook)
 
+;;; arch-tag: 5f07fa74-f153-4524-9303-21f5be125851
 ;;; cl.el ends here