[ < ] [ > ]   [ << ] [Plus haut] [ >> ]         [Top] [Table des matières] [Index] [ ? ]

39.11 Idle Timers

Here is how to set up a timer that runs when Emacs is idle for a certain length of time. Aside from how to set them up, idle timers work just like ordinary timers.

Command: run-with-idle-timer secs repeat function &rest args

Set up a timer which runs when Emacs has been idle for secs seconds. The value of secs may be an integer or a floating point number; a value of the type returned by current-idle-time is also allowed.

If repeat is nil, the timer runs just once, the first time Emacs remains idle for a long enough time. More often repeat is non-nil, which means to run the timer each time Emacs remains idle for secs seconds.

The function run-with-idle-timer returns a timer value which you can use in calling cancel-timer (voir la section Timers for Delayed Execution).

Emacs becomes “idle” when it starts waiting for user input, and it remains idle until the user provides some input. If a timer is set for five seconds of idleness, it runs approximately five seconds after Emacs first becomes idle. Even if repeat is non-nil, this timer will not run again as long as Emacs remains idle, because the duration of idleness will continue to increase and will not go down to five seconds again.

Emacs can do various things while idle: garbage collect, autosave or handle data from a subprocess. But these interludes during idleness do not interfere with idle timers, because they do not reset the clock of idleness to zero. An idle timer set for 600 seconds will run when ten minutes have elapsed since the last user command was finished, even if subprocess output has been accepted thousands of times within those ten minutes, and even if there have been garbage collections and autosaves.

When the user supplies input, Emacs becomes non-idle while executing the input. Then it becomes idle again, and all the idle timers that are set up to repeat will subsequently run another time, one by one.

Function: current-idle-time

This function returns the length of time Emacs has been idle, as a list of three integers: (high low microsec). The integers high and low combine to give the number of seconds of idleness, which is high * 2**16 + low.

The third element, microsec, gives the microseconds since the start of the current second (or 0 for systems that return time with the resolution of only one second).

The main use of this function is when an idle timer function wants to “take a break” for a while. It can set up another idle timer to call the same function again, after a few seconds more idleness. Here's an example:

 
(defvar resume-timer nil
  "Timer that `timer-function' used to reschedule itself, or nil.")

(defun timer-function ()
  ;; If the user types a command while resume-timer
  ;; is active, the next time this function is called from
  ;; its main idle timer, deactivate resume-timer.
  (when resume-timer
    (cancel-timer resume-timer))
  ...do the work for a while...
  (when taking-a-break
    (setq resume-timer
          (run-with-idle-timer
            ;; Compute an idle time break-length
            ;; more than the current value.
            (time-add (current-idle-time)
                      (seconds-to-time break-length))
            nil
            'timer-function))))

Some idle timer functions in user Lisp packages have a loop that does a certain amount of processing each time around, and exits when (input-pending-p) is non-nil. That approach seems very natural but has two problems:

To avoid these problems, don't use that technique. Instead, write such idle timers to reschedule themselves after a brief pause, using the method in the timer-function example above.


[ < ] [ > ]   [ << ] [Plus haut] [ >> ]         [Top] [Table des matières] [Index] [ ? ]

Ce document a été généré par Eric Reinbold le 13 Octobre 2007 en utilisant texi2html 1.78.