;;; 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.
"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))))
;; 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)))
"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.")
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))
(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)
(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)
(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."
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)