]> code.delx.au - gnu-emacs/blobdiff - lisp/emacs-lisp/cl.el
(byte-compile-warning-types): Add interactive-only.
[gnu-emacs] / lisp / emacs-lisp / cl.el
index 830a3e8d1ffc2eaa11dcd3a6c974a2d88e17f941..2f6c799f528590914073f78070c6130008aaddb0 100644 (file)
@@ -108,6 +108,9 @@ 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.")
 
+(add-hook 'cl-unload-hook 'cl-cannot-unload)
+(defun cl-cannot-unload ()
+  (error "Cannot unload the feature `cl'"))
 
 ;;; Predicates.
 
@@ -123,7 +126,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)
@@ -131,7 +134,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)
@@ -139,7 +142,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'."
@@ -148,7 +151,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'."
@@ -178,7 +181,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))))
@@ -203,12 +206,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.
 
@@ -222,7 +255,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))
@@ -291,10 +324,6 @@ always returns nil."
 
 (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)
@@ -325,47 +354,45 @@ 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 caaar (x)
@@ -475,20 +502,6 @@ SEQ, this is like `mapcar'.  With several, it is like the Common Lisp
 ;;    (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 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.
 Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
@@ -565,18 +578,26 @@ Keywords supported:  :test :test-not :key"
 (put 'cl-assertion-failed 'error-conditions '(error))
 (put 'cl-assertion-failed 'error-message "Assertion failed")
 
+(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
@@ -585,14 +606,14 @@ Keywords supported:  :test :test-not :key"
           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)
@@ -661,8 +682,8 @@ Keywords supported:  :test :test-not :key"
 (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)
@@ -678,4 +699,5 @@ Keywords supported:  :test :test-not :key"
 
 (run-hooks 'cl-load-hook)
 
+;;; arch-tag: 5f07fa74-f153-4524-9303-21f5be125851
 ;;; cl.el ends here