*** empty log message ***
[bpt/emacs.git] / lisp / timer.el
index c06c685..e860f84 100644 (file)
@@ -183,8 +183,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 +209,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")))
@@ -237,12 +240,6 @@ fire repeatedly that many seconds apart."
           (setq timer-idle-list (delq (car tail) timer-idle-list)))
       (setq tail (cdr tail)))))
 \f
-;; Set up the common handler for all timer events.  Since the event has
-;; the timer as parameter we can still distinguish.  Note that using
-;; special-event-map ensures that event timer events that arrive in the
-;; middle of a key sequence being entered are still handled correctly.
-(define-key special-event-map [timer-event] 'timer-event-handler)
-
 ;; Record the last few events, for debugging.
 (defvar timer-event-last-2 nil)
 (defvar timer-event-last-1 nil)
@@ -259,22 +256,17 @@ TIME is a time-list."
        (low (- (nth 1 time) (aref timer 2))))
     (+ low (* high 65536))))
   
-(defun timer-event-handler (event)
-  "Call the handler for the timer in the event EVENT."
-  (interactive "e")
+(defun timer-event-handler (timer)
+  "Call the handler for the timer TIMER.
+This function is called, by name, directly by the C code."
   (setq timer-event-last-2 timer-event-last-1)
   (setq timer-event-last-1 timer-event-last)
-  (setq timer-event-last (cons event (copy-sequence event)))
-  (let ((inhibit-quit t)
-       (timer (car-safe (cdr-safe event))))
+  (setq timer-event-last timer)
+  (let ((inhibit-quit t))
     (if (timerp timer)
        (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)
@@ -289,7 +281,13 @@ TIME is a time-list."
                                      (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.
@@ -375,9 +373,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