]> code.delx.au - gnu-emacs/blobdiff - lisp/timer.el
Capitalization in comment.
[gnu-emacs] / lisp / timer.el
index 68a9e105dae0d5908ccd7637d44bbef7eb2e3f64..0c159c6023dba8d7ab0639607fbc9d61bdc213e9 100644 (file)
@@ -1,4 +1,4 @@
-;;; timer.el --- run a function with args at some time in future.
+;;; timer.el --- run a function with args at some time in future
 
 ;; Copyright (C) 1996 Free Software Foundation, Inc.
 
@@ -116,9 +116,11 @@ SECS may be a fraction."
     (setq low (+ low (floor secs)))
 
     ;; Normalize
-    (setq low (+ low (/ micro 1000000)))
+    ;; `/' rounds towards zero while `mod' returns a positive number,
+    ;; so we can't rely on (= a (+ (* 100 (/ a 100)) (mod a 100))).
+    (setq low (+ low (/ micro 1000000) (if (< micro 0) -1 0)))
     (setq micro (mod micro 1000000))
-    (setq high (+ high (/ low 65536)))
+    (setq high (+ high (/ low 65536) (if (< low 0) -1 0)))
     (setq low (logand low 65535))
 
     (list high low (and (/= micro 0) micro))))
@@ -183,8 +185,11 @@ fire repeatedly that many seconds apart."
        nil)
     (error "Invalid or uninitialized timer")))
 
-(defun timer-activate-when-idle (timer)
-  "Arrange to activate TIMER whenever Emacs is next idle."
+(defun timer-activate-when-idle (timer &optional dont-wait)
+  "Arrange to activate TIMER whenever Emacs is next idle.
+If optional argument DONT-WAIT is non-nil, then enable the
+timer to activate immediately, or at the right time, if Emacs
+is already idle."
   (if (and (timerp timer)
           (integerp (aref timer 1))
           (integerp (aref timer 2))
@@ -206,7 +211,7 @@ fire repeatedly that many seconds apart."
        (if last
            (setcdr last (cons timer timers))
          (setq timer-idle-list (cons timer timers)))
-       (aset timer 0 t)
+       (aset timer 0 (not dont-wait))
        (aset timer 7 t)
        nil)
     (error "Invalid or uninitialized timer")))
@@ -264,10 +269,6 @@ This function is called, by name, directly by the C code."
        (progn
          ;; Delete from queue.
          (cancel-timer timer)
-         ;; Run handler
-         (condition-case nil
-             (apply (aref timer 5) (aref timer 6))
-           (error nil))
          ;; Re-schedule if requested.
          (if (aref timer 4)
              (if (aref timer 7)
@@ -282,7 +283,13 @@ This function is called, by name, directly by the C code."
                                      (aref timer 4))))
                      (if (> repeats timer-max-repeats)
                          (timer-inc-time timer (* (aref timer 4) repeats)))))
-               (timer-activate timer))))
+               (timer-activate timer)))
+         ;; Run handler.
+         ;; We do this after rescheduling so that the handler function
+         ;; can cancel its own timer successfully with cancel-timer.
+         (condition-case nil
+             (apply (aref timer 5) (aref timer 6))
+           (error nil)))
       (error "Bogus timer event"))))
 
 ;; This function is incompatible with the one in levents.el.
@@ -368,9 +375,11 @@ This function is for compatibility; see also `run-with-timer'."
 ;;;###autoload
 (defun run-with-idle-timer (secs repeat function &rest args)
   "Perform an action the next time Emacs is idle for SECS seconds.
-If REPEAT is non-nil, do this each time Emacs is idle for SECS seconds.
-SECS may be an integer or a floating point number.
 The action is to call FUNCTION with arguments ARGS.
+SECS may be an integer or a floating point number.
+
+If REPEAT is non-nil, do the action each time Emacs has been idle for
+exactly SECS seconds (that is, only once for each time Emacs becomes idle).
 
 This function returns a timer object which you can use in `cancel-timer'."
   (interactive