]> code.delx.au - gnu-emacs/blobdiff - lisp/subr.el
(clipboard-yank): bomb out in interactive use if
[gnu-emacs] / lisp / subr.el
index 90f1db78de11267f47b6547381216cc45ce1d02b..88af6f0886921b00cb3c6021570e495287ededaf 100644 (file)
@@ -53,6 +53,12 @@ This is the global do-nothing version.  There is also `testcover-1value'
 that complains if FORM ever does return differing values."
   form)
 
+(defmacro def-edebug-spec (symbol spec)
+  "Set the `edebug-form-spec' property of SYMBOL according to SPEC.
+Both SYMBOL and SPEC are unevaluated. The SPEC can be 0, t, a symbol
+\(naming a function), or a list."
+  `(put (quote ,symbol) 'edebug-form-spec (quote ,spec)))
+
 (defmacro lambda (&rest cdr)
   "Return a lambda expression.
 A call of the form (lambda ARGS DOCSTRING INTERACTIVE BODY) is
@@ -102,6 +108,9 @@ change the list."
   (declare (indent 1) (debug t))
   (cons 'if (cons cond (cons nil body))))
 
+(defvar --dolist-tail-- nil
+  "Temporary variable used in `dolist' expansion.")
+
 (defmacro dolist (spec &rest body)
   "Loop over a list.
 Evaluate BODY with VAR bound to each car from LIST, in turn.
@@ -109,16 +118,22 @@ Then evaluate RESULT to get return value, default nil.
 
 \(fn (VAR LIST [RESULT]) BODY...)"
   (declare (indent 1) (debug ((symbolp form &optional form) body)))
-  (let ((temp (make-symbol "--dolist-temp--")))
+  ;; It would be cleaner to create an uninterned symbol,
+  ;; but that uses a lot more space when many functions in many files
+  ;; use dolist.
+  (let ((temp '--dolist-tail--))
     `(let ((,temp ,(nth 1 spec))
           ,(car spec))
        (while ,temp
         (setq ,(car spec) (car ,temp))
-        (setq ,temp (cdr ,temp))
-        ,@body)
+        ,@body
+        (setq ,temp (cdr ,temp)))
        ,@(if (cdr (cdr spec))
             `((setq ,(car spec) nil) ,@(cdr (cdr spec)))))))
 
+(defvar --dotimes-limit-- nil
+  "Temporary variable used in `dotimes' expansion.")
+
 (defmacro dotimes (spec &rest body)
   "Loop a certain number of times.
 Evaluate BODY with VAR bound to successive integers running from 0,
@@ -127,7 +142,10 @@ the return value (nil if RESULT is omitted).
 
 \(fn (VAR COUNT [RESULT]) BODY...)"
   (declare (indent 1) (debug dolist))
-  (let ((temp (make-symbol "--dotimes-temp--"))
+  ;; It would be cleaner to create an uninterned symbol,
+  ;; but that uses a lot more space when many functions in many files
+  ;; use dotimes.
+  (let ((temp '--dotimes-limit--)
        (start 0)
        (end (nth 1 spec)))
     `(let ((,temp ,end)
@@ -1067,9 +1085,10 @@ the hook's buffer-local value rather than its default value."
            (kill-local-variable hook)
          (set hook hook-value))))))
 
-(defun add-to-list (list-var element &optional append)
+(defun add-to-list (list-var element &optional append compare-fn)
   "Add ELEMENT to the value of LIST-VAR if it isn't there yet.
-The test for presence of ELEMENT is done with `equal'.
+The test for presence of ELEMENT is done with `equal',
+or with COMPARE-FN if that's non-nil.
 If ELEMENT is added, it is added at the beginning of the list,
 unless the optional argument APPEND is non-nil, in which case
 ELEMENT is added at the end.
@@ -1081,7 +1100,13 @@ until a certain package is loaded, you should put the call to `add-to-list'
 into a hook function that will be run only after loading the package.
 `eval-after-load' provides one way to do this.  In some cases
 other hooks, such as major mode hooks, can do the job."
-  (if (member element (symbol-value list-var))
+  (if (if compare-fn
+         (let (present)
+           (dolist (elt (symbol-value list-var))
+             (if (funcall compare-fn element elt)
+                 (setq present t)))
+           present)
+       (member element (symbol-value list-var)))
       (symbol-value list-var)
     (set list-var
         (if append
@@ -1693,6 +1718,42 @@ by doing (clear-string STRING)."
            (sit-for 1)
            t)))
     n))
+
+(defun sit-for (seconds &optional nodisp obsolete)
+  "Perform redisplay, then wait for SECONDS seconds or until input is available.
+SECONDS may be a floating-point value.
+\(On operating systems that do not support waiting for fractions of a
+second, floating-point values are rounded down to the nearest integer.)
+
+If optional arg NODISP is t, don't redisplay, just wait for input.
+Redisplay does not happen if input is available before it starts.
+
+Value is t if waited the full time with no input arriving, and nil otherwise.
+
+An obsolete, but still supported form is
+\(sit-for SECONDS &optional MILLISECONDS NODISP)
+where the optional arg MILLISECONDS specifies an additional wait period,
+in milliseconds; this was useful when Emacs was built without
+floating point support.
+
+\(fn SECONDS &optional NODISP)"
+  (when (or obsolete (numberp nodisp))
+    (setq seconds (+ seconds (* 1e-3 nodisp)))
+    (setq nodisp obsolete))
+  (cond
+   (noninteractive
+    (sleep-for seconds)
+    t)
+   ((input-pending-p)
+    nil)
+   ((<= seconds 0)
+    (or nodisp (redisplay)))
+   (t
+    (or nodisp (redisplay))
+    (let ((read (read-event nil nil seconds)))
+      (or (null read)
+         (progn (push read unread-command-events)
+                nil))))))
 \f
 ;;; Atomic change groups.
 
@@ -2202,7 +2263,8 @@ If you quit, the process is killed with SIGINT, or SIGKILL if you quit again."
 ;;;; Lisp macros to do various things temporarily.
 
 (defmacro with-current-buffer (buffer &rest body)
-  "Execute the forms in BODY with BUFFER as the current buffer.
+  "Execute the forms in BODY with BUFFER temporarily current.
+BUFFER can be a buffer or a buffer name.
 The value returned is the value of the last form in BODY.
 See also `with-temp-buffer'."
   (declare (indent 1) (debug t))
@@ -2339,8 +2401,8 @@ If BODY finishes, `while-no-input' returns whatever value BODY produced."
     `(with-local-quit
        (catch ',catch-sym
         (let ((throw-on-input ',catch-sym))
-          (or (not (sit-for 0 0 t))
-            ,@body))))))
+          (or (input-pending-p)
+              ,@body))))))
 
 (defmacro combine-after-change-calls (&rest body)
   "Execute BODY, but don't call the after-change functions till the end.
@@ -2485,8 +2547,9 @@ STRING should be given if the last search was by `string-match' on STRING."
 (defun looking-back (regexp &optional limit greedy)
   "Return non-nil if text before point matches regular expression REGEXP.
 Like `looking-at' except matches before point, and is slower.
-LIMIT if non-nil speeds up the search by specifying how far back the
-match can start.
+LIMIT if non-nil speeds up the search by specifying a minimum
+starting position, to avoid checking matches that would start
+before LIMIT.
 
 If GREEDY is non-nil, extend the match backwards as far as possible,
 stopping when a single additional previous character cannot be part