]> code.delx.au - gnu-emacs/blobdiff - lisp/emacs-lisp/timer.el
Merge changes from emacs-23 branch.
[gnu-emacs] / lisp / emacs-lisp / timer.el
index 9f5f72d81fe38f3a28fc5b8c02e82002fc4d432a..6ae6a86857ea3615a49bd3629700f4308b8e3756 100644 (file)
@@ -1,9 +1,10 @@
 ;;; timer.el --- run a function with args at some time in future
 
-;; Copyright (C) 1996, 2001, 2002, 2003, 2004, 2005,
-;;   2006, 2007, 2008 Free Software Foundation, Inc.
+;; Copyright (C) 1996, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
+;;   2009, 2010  Free Software Foundation, Inc.
 
 ;; Maintainer: FSF
+;; Package: emacs
 
 ;; This file is part of GNU Emacs.
 
@@ -284,7 +285,7 @@ how many will really happen.")
   "Calculate number of seconds from when TIMER will run, until TIME.
 TIMER is a timer, and stands for the time when its next repeat is scheduled.
 TIME is a time-list."
-  ;; FIXME: (time-to-seconds (time-subtract (timer--time timer) time))
+  ;; FIXME: (float-time (time-subtract (timer--time timer) time))
   (let ((high (- (car time) (timer--high-seconds timer)))
        (low (- (nth 1 time) (timer--low-seconds timer))))
     (+ low (* high 65536))))
@@ -321,7 +322,11 @@ This function is called, by name, directly by the C code."
          ;; We do this after rescheduling so that the handler function
          ;; can cancel its own timer successfully with cancel-timer.
          (condition-case nil
-             (apply (timer--function timer) (timer--args timer))
+              ;; Timer functions should not change the current buffer.
+              ;; If they do, all kinds of nasty surprises can happen,
+              ;; and it can be hellish to track down their source.
+              (save-current-buffer
+                (apply (timer--function timer) (timer--args timer)))
            (error nil))
          (if retrigger
              (setf (timer--triggered timer) nil)))
@@ -438,8 +443,6 @@ This function returns a timer object which you can use in `cancel-timer'."
   "This is the timer function used for the timer made by `with-timeout'."
   (throw tag 'timeout))
 
-(put 'with-timeout 'lisp-indent-function 1)
-
 (defvar with-timeout-timers nil
   "List of all timers used by currently pending `with-timeout' calls.")
 
@@ -451,6 +454,7 @@ event (such as keyboard input, input from subprocesses, or a certain time);
 if the program loops without waiting in any way, the timeout will not
 be detected.
 \n(fn (SECONDS TIMEOUT-FORMS...) BODY)"
+  (declare (indent 1))
   (let ((seconds (car list))
        (timeout-forms (cdr list)))
     `(let ((with-timeout-tag (cons nil nil))
@@ -487,7 +491,7 @@ The argument should be a value previously returned by `with-timeout-suspend'."
   (dolist (elt timer-spec-list)
     (let ((timer (car elt))
          (delay (cadr elt)))
-      (timer-set-time timer (time-add (current-time) delay)) 
+      (timer-set-time timer (time-add (current-time) delay))
       (timer-activate timer))))
 
 (defun y-or-n-p-with-timeout (prompt seconds default-value)
@@ -496,7 +500,7 @@ If the user does not answer after SECONDS seconds, return DEFAULT-VALUE."
   (with-timeout (seconds default-value)
     (y-or-n-p prompt)))
 \f
-(defvar timer-duration-words
+(defconst timer-duration-words
   (list (cons "microsec" 0.000001)
        (cons "microsecond" 0.000001)
         (cons "millisec" 0.001)
@@ -512,7 +516,7 @@ If the user does not answer after SECONDS seconds, return DEFAULT-VALUE."
        (cons "month" (* 30 24 60 60))    ; Approximation
        (cons "year" (* 365.25 24 60 60)) ; Approximation
        )
-  "Alist mapping temporal words to durations in seconds")
+  "Alist mapping temporal words to durations in seconds.")
 
 (defun timer-duration (string)
   "Return number of seconds specified by STRING, or nil if parsing fails."
@@ -534,7 +538,7 @@ If the user does not answer after SECONDS seconds, return DEFAULT-VALUE."
                start (length string)))))
     (if (= start (length string))
        secs
-      (if (string-match "\\`[0-9.]+\\'" string)
+      (if (string-match-p "\\`[0-9.]+\\'" string)
          (string-to-number string)))))
 \f
 (provide 'timer)