(dired-move-to-filename-regexp): Support file sizes in
[bpt/emacs.git] / lisp / timer.el
CommitLineData
c4ba2209 1;;; timer.el --- run a function with args at some time in future.
d501f516 2
4395bfdb 3;; Copyright (C) 1996 Free Software Foundation, Inc.
5cc564a6 4
eea8d4ef
ER
5;; Maintainer: FSF
6
5cc564a6 7;; This file is part of GNU Emacs.
8
9;; GNU Emacs is free software; you can redistribute it and/or modify
10;; it under the terms of the GNU General Public License as published by
e5167999 11;; the Free Software Foundation; either version 2, or (at your option)
5cc564a6 12;; any later version.
13
14;; GNU Emacs is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
b578f267
EN
20;; along with GNU Emacs; see the file COPYING. If not, write to the
21;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22;; Boston, MA 02111-1307, USA.
5cc564a6 23
c91c4e6d
ER
24;;; Commentary:
25
26;; This package gives you the capability to run Emacs Lisp commands at
eb8c3be9 27;; specified times in the future, either as one-shots or periodically.
c91c4e6d 28
e5167999
ER
29;;; Code:
30
4395bfdb 31;; Layout of a timer vector:
9d690693
KH
32;; [triggered-p high-seconds low-seconds usecs repeat-delay
33;; function args idle-delay]
4395bfdb
RS
34
35(defun timer-create ()
36 "Create a timer object."
9d690693 37 (let ((timer (make-vector 8 nil)))
c4ba2209 38 (aset timer 0 t)
4395bfdb
RS
39 timer))
40
41(defun timerp (object)
42 "Return t if OBJECT is a timer."
9d690693 43 (and (vectorp object) (= (length object) 8)))
4395bfdb
RS
44
45(defun timer-set-time (timer time &optional delta)
46 "Set the trigger time of TIMER to TIME.
cf9b15ce
RS
47TIME must be in the internal format returned by, e.g., `current-time'.
48If optional third argument DELTA is a non-zero integer, make the timer
49fire repeatedly that many seconds apart."
4395bfdb
RS
50 (or (timerp timer)
51 (error "Invalid timer"))
52 (aset timer 1 (car time))
53 (aset timer 2 (if (consp (cdr time)) (car (cdr time)) (cdr time)))
1b966390
RS
54 (aset timer 3 (or (and (consp (cdr time)) (consp (cdr (cdr time)))
55 (nth 2 time))
56 0))
c4ba2209 57 (aset timer 4 (and (numberp delta) (> delta 0) delta))
4395bfdb
RS
58 timer)
59
cf9b15ce
RS
60(defun timer-set-idle-time (timer secs &optional repeat)
61 "Set the trigger idle time of TIMER to SECS.
62If optional third argument REPEAT is non-nil, make the timer
63fire each time Emacs is idle for that many seconds."
64 (or (timerp timer)
65 (error "Invalid timer"))
66 (aset timer 1 0)
67 (aset timer 2 0)
68 (aset timer 3 0)
69 (timer-inc-time timer secs)
70 (aset timer 4 repeat)
71 timer)
72
155f9a94 73(defun timer-next-integral-multiple-of-time (time secs)
59b1de82
RS
74 "Yield the next value after TIME that is an integral multiple of SECS.
75More precisely, the next value, after TIME, that is an integral multiple
76of SECS seconds since the epoch. SECS may be a fraction."
155f9a94
PE
77 (let ((time-base (ash 1 16)))
78 (if (fboundp 'atan)
79 ;; Use floating point, taking care to not lose precision.
80 (let* ((float-time-base (float time-base))
81 (million 1000000.0)
82 (time-usec (+ (* million
83 (+ (* float-time-base (nth 0 time))
84 (nth 1 time)))
85 (nth 2 time)))
86 (secs-usec (* million secs))
87 (mod-usec (mod time-usec secs-usec))
88 (next-usec (+ (- time-usec mod-usec) secs-usec))
89 (time-base-million (* float-time-base million)))
90 (list (floor next-usec time-base-million)
91 (floor (mod next-usec time-base-million) million)
92 (floor (mod next-usec million))))
93 ;; Floating point is not supported.
94 ;; Use integer arithmetic, avoiding overflow if possible.
95 (let* ((mod-sec (mod (+ (* (mod time-base secs)
96 (mod (nth 0 time) secs))
97 (nth 1 time))
98 secs))
99 (next-1-sec (+ (- (nth 1 time) mod-sec) secs)))
100 (list (+ (nth 0 time) (floor next-1-sec time-base))
101 (mod next-1-sec time-base)
102 0)))))
103
c4ba2209
RS
104(defun timer-relative-time (time secs &optional usecs)
105 "Advance TIME by SECS seconds and optionally USECS microseconds.
106SECS may be a fraction."
107 (let ((high (car time))
108 (low (if (consp (cdr time)) (nth 1 time) (cdr time)))
109 (micro (if (numberp (car-safe (cdr-safe (cdr time))))
110 (nth 2 time)
111 0)))
112 ;; Add
113 (if usecs (setq micro (+ micro usecs)))
114 (if (floatp secs)
115 (setq micro (+ micro (floor (* 1000000 (- secs (floor secs)))))))
116 (setq low (+ low (floor secs)))
117
118 ;; Normalize
119 (setq low (+ low (/ micro 1000000)))
120 (setq micro (mod micro 1000000))
121 (setq high (+ high (/ low 65536)))
122 (setq low (logand low 65535))
123
124 (list high low (and (/= micro 0) micro))))
4395bfdb
RS
125
126(defun timer-inc-time (timer secs &optional usecs)
127 "Increment the time set in TIMER by SECS seconds and USECS microseconds.
128SECS may be a fraction."
c4ba2209
RS
129 (let ((time (timer-relative-time
130 (list (aref timer 1) (aref timer 2) (aref timer 3))
131 secs
132 usecs)))
133 (aset timer 1 (nth 0 time))
134 (aset timer 2 (nth 1 time))
135 (aset timer 3 (or (nth 2 time) 0))))
4395bfdb
RS
136
137(defun timer-set-time-with-usecs (timer time usecs &optional delta)
138 "Set the trigger time of TIMER to TIME.
cf9b15ce
RS
139TIME must be in the internal format returned by, e.g., `current-time'.
140If optional third argument DELTA is a non-zero integer, make the timer
141fire repeatedly that many seconds apart."
4395bfdb
RS
142 (or (timerp timer)
143 (error "Invalid timer"))
144 (aset timer 1 (car time))
145 (aset timer 2 (if (consp (cdr time)) (car (cdr time)) (cdr time)))
146 (aset timer 3 usecs)
c4ba2209 147 (aset timer 4 (and (numberp delta) (> delta 0) delta))
4395bfdb
RS
148 timer)
149
150(defun timer-set-function (timer function &optional args)
151 "Make TIMER call FUNCTION with optional ARGS when triggering."
152 (or (timerp timer)
153 (error "Invalid timer"))
154 (aset timer 5 function)
155 (aset timer 6 args)
156 timer)
157\f
158(defun timer-activate (timer)
159 "Put TIMER on the list of active timers."
160 (if (and (timerp timer)
161 (integerp (aref timer 1))
162 (integerp (aref timer 2))
163 (integerp (aref timer 3))
164 (aref timer 5))
165 (let ((timers timer-list)
166 last)
167 ;; Skip all timers to trigger before the new one.
168 (while (and timers
169 (or (> (aref timer 1) (aref (car timers) 1))
170 (and (= (aref timer 1) (aref (car timers) 1))
171 (> (aref timer 2) (aref (car timers) 2)))
172 (and (= (aref timer 1) (aref (car timers) 1))
173 (= (aref timer 2) (aref (car timers) 2))
174 (> (aref timer 3) (aref (car timers) 3)))))
175 (setq last timers
176 timers (cdr timers)))
177 ;; Insert new timer after last which possibly means in front of queue.
178 (if last
179 (setcdr last (cons timer timers))
180 (setq timer-list (cons timer timers)))
181 (aset timer 0 nil)
9d690693
KH
182 (aset timer 7 nil)
183 nil)
184 (error "Invalid or uninitialized timer")))
185
7fe165d7
AI
186(defun timer-activate-when-idle (timer &optional dont-wait)
187 "Arrange to activate TIMER whenever Emacs is next idle.
188If optional argument DONT-WAIT is non-nil, then enable the
189timer to activate immediately, or at the right time, if Emacs
190is already idle."
9d690693
KH
191 (if (and (timerp timer)
192 (integerp (aref timer 1))
193 (integerp (aref timer 2))
194 (integerp (aref timer 3))
195 (aref timer 5))
196 (let ((timers timer-idle-list)
197 last)
198 ;; Skip all timers to trigger before the new one.
199 (while (and timers
200 (or (> (aref timer 1) (aref (car timers) 1))
201 (and (= (aref timer 1) (aref (car timers) 1))
202 (> (aref timer 2) (aref (car timers) 2)))
203 (and (= (aref timer 1) (aref (car timers) 1))
204 (= (aref timer 2) (aref (car timers) 2))
205 (> (aref timer 3) (aref (car timers) 3)))))
206 (setq last timers
207 timers (cdr timers)))
208 ;; Insert new timer after last which possibly means in front of queue.
209 (if last
210 (setcdr last (cons timer timers))
211 (setq timer-idle-list (cons timer timers)))
7fe165d7 212 (aset timer 0 (not dont-wait))
9d690693 213 (aset timer 7 t)
4395bfdb
RS
214 nil)
215 (error "Invalid or uninitialized timer")))
216
388d92ac 217;;;###autoload
c4ba2209 218(defalias 'disable-timeout 'cancel-timer)
388d92ac 219;;;###autoload
4395bfdb
RS
220(defun cancel-timer (timer)
221 "Remove TIMER from the list of active timers."
222 (or (timerp timer)
223 (error "Invalid timer"))
224 (setq timer-list (delq timer timer-list))
9d690693 225 (setq timer-idle-list (delq timer timer-idle-list))
4395bfdb 226 nil)
e2ec008d 227
91c6fd0c 228;;;###autoload
4395bfdb
RS
229(defun cancel-function-timers (function)
230 "Cancel all timers scheduled by `run-at-time' which would run FUNCTION."
231 (interactive "aCancel timers of function: ")
232 (let ((tail timer-list))
233 (while tail
234 (if (eq (aref (car tail) 5) function)
235 (setq timer-list (delq (car tail) timer-list)))
9d690693
KH
236 (setq tail (cdr tail))))
237 (let ((tail timer-idle-list))
238 (while tail
239 (if (eq (aref (car tail) 5) function)
240 (setq timer-idle-list (delq (car tail) timer-idle-list)))
4395bfdb
RS
241 (setq tail (cdr tail)))))
242\f
b883eed3
RS
243;; Record the last few events, for debugging.
244(defvar timer-event-last-2 nil)
245(defvar timer-event-last-1 nil)
246(defvar timer-event-last nil)
247
953c43d8
RS
248(defvar timer-max-repeats 10
249 "*Maximum number of times to repeat a timer, if real time jumps.")
250
251(defun timer-until (timer time)
252 "Calculate number of seconds from when TIMER will run, until TIME.
253TIMER is a timer, and stands for the time when its next repeat is scheduled.
0a1a8ee9 254TIME is a time-list."
953c43d8
RS
255 (let ((high (- (car time) (aref timer 1)))
256 (low (- (nth 1 time) (aref timer 2))))
257 (+ low (* high 65536))))
258
b0144c1b
RS
259(defun timer-event-handler (timer)
260 "Call the handler for the timer TIMER.
261This function is called, by name, directly by the C code."
b883eed3
RS
262 (setq timer-event-last-2 timer-event-last-1)
263 (setq timer-event-last-1 timer-event-last)
b0144c1b
RS
264 (setq timer-event-last timer)
265 (let ((inhibit-quit t))
4395bfdb
RS
266 (if (timerp timer)
267 (progn
268 ;; Delete from queue.
269 (cancel-timer timer)
4395bfdb
RS
270 ;; Re-schedule if requested.
271 (if (aref timer 4)
9d690693
KH
272 (if (aref timer 7)
273 (timer-activate-when-idle timer)
4395bfdb 274 (timer-inc-time timer (aref timer 4) 0)
953c43d8
RS
275 ;; If real time has jumped forward,
276 ;; perhaps because Emacs was suspended for a long time,
277 ;; limit how many times things get repeated.
278 (if (and (numberp timer-max-repeats)
279 (< 0 (timer-until timer (current-time))))
280 (let ((repeats (/ (timer-until timer (current-time))
281 (aref timer 4))))
282 (if (> repeats timer-max-repeats)
283 (timer-inc-time timer (* (aref timer 4) repeats)))))
83ff83c0
RS
284 (timer-activate timer)))
285 ;; Run handler.
286 ;; We do this after rescheduling so that the handler function
287 ;; can cancel its own timer successfully with cancel-timer.
288 (condition-case nil
289 (apply (aref timer 5) (aref timer 6))
290 (error nil)))
4395bfdb 291 (error "Bogus timer event"))))
cf9b15ce
RS
292
293;; This function is incompatible with the one in levents.el.
294(defun timeout-event-p (event)
295 "Non-nil if EVENT is a timeout event."
296 (and (listp event) (eq (car event) 'timer-event)))
4395bfdb 297\f
49116ac0 298;;;###autoload
5cc564a6 299(defun run-at-time (time repeat function &rest args)
fa499361 300 "Perform an action at time TIME.
cf9b15ce
RS
301Repeat the action every REPEAT seconds, if REPEAT is non-nil.
302TIME should be a string like \"11:23pm\", nil meaning now, a number of seconds
155f9a94
PE
303from now, a value from `current-time', or t (with non-nil REPEAT)
304meaning the next integral multiple of REPEAT.
cf9b15ce
RS
305REPEAT may be an integer or floating point number.
306The action is to call FUNCTION with arguments ARGS.
c4ba2209
RS
307
308This function returns a timer object which you can use in `cancel-timer'."
5cc564a6 309 (interactive "sRun at time: \nNRepeat interval: \naFunction: ")
5cc564a6 310
155f9a94
PE
311 (or (null repeat)
312 (and (numberp repeat) (< 0 repeat))
313 (error "Invalid repetition interval"))
314
995b296f 315 ;; Special case: nil means "now" and is useful when repeating.
c4ba2209
RS
316 (if (null time)
317 (setq time (current-time)))
318
155f9a94
PE
319 ;; Special case: t means the next integral multiple of REPEAT.
320 (if (and (eq time t) repeat)
321 (setq time (timer-next-integral-multiple-of-time (current-time) repeat)))
322
0c6be60d
RM
323 ;; Handle numbers as relative times in seconds.
324 (if (numberp time)
325 (setq time (timer-relative-time (current-time) time)))
326
c4ba2209
RS
327 ;; Handle relative times like "2 hours and 35 minutes"
328 (if (stringp time)
329 (let ((secs (timer-duration time)))
330 (if secs
331 (setq time (timer-relative-time (current-time) secs)))))
332
4395bfdb
RS
333 ;; Handle "11:23pm" and the like. Interpret it as meaning today
334 ;; which admittedly is rather stupid if we have passed that time
cf9b15ce 335 ;; already. (Though only Emacs hackers hack Emacs at that time.)
4395bfdb
RS
336 (if (stringp time)
337 (progn
338 (require 'diary-lib)
339 (let ((hhmm (diary-entry-time time))
340 (now (decode-time)))
c4ba2209
RS
341 (if (>= hhmm 0)
342 (setq time
343 (encode-time 0 (% hhmm 100) (/ hhmm 100) (nth 3 now)
344 (nth 4 now) (nth 5 now) (nth 8 now)))))))
4395bfdb
RS
345
346 (or (consp time)
347 (error "Invalid time format"))
348
4395bfdb
RS
349 (let ((timer (timer-create)))
350 (timer-set-time timer time repeat)
351 (timer-set-function timer function args)
c4ba2209
RS
352 (timer-activate timer)
353 timer))
4395bfdb 354
8a9fe4d2 355;;;###autoload
c4ba2209 356(defun run-with-timer (secs repeat function &rest args)
8a9fe4d2 357 "Perform an action after a delay of SECS seconds.
4395bfdb 358Repeat the action every REPEAT seconds, if REPEAT is non-nil.
c4ba2209
RS
359SECS and REPEAT may be integers or floating point numbers.
360The action is to call FUNCTION with arguments ARGS.
361
362This function returns a timer object which you can use in `cancel-timer'."
4395bfdb 363 (interactive "sRun after delay (seconds): \nNRepeat interval: \naFunction: ")
cf9b15ce 364 (apply 'run-at-time secs repeat function args))
4395bfdb 365
cf9b15ce
RS
366;;;###autoload
367(defun add-timeout (secs function object &optional repeat)
368 "Add a timer to run SECS seconds from now, to call FUNCTION on OBJECT.
369If REPEAT is non-nil, repeat the timer every REPEAT seconds.
370This function is for compatibility; see also `run-with-timer'."
371 (run-with-timer secs repeat function object))
9d690693
KH
372
373;;;###autoload
374(defun run-with-idle-timer (secs repeat function &rest args)
375 "Perform an action the next time Emacs is idle for SECS seconds.
cf9b15ce 376The action is to call FUNCTION with arguments ARGS.
37c004c4
RS
377SECS may be an integer or a floating point number.
378
379If REPEAT is non-nil, do the action each time Emacs has been idle for
380exactly SECS seconds (that is, only once for each time Emacs becomes idle).
9d690693
KH
381
382This function returns a timer object which you can use in `cancel-timer'."
cf9b15ce
RS
383 (interactive
384 (list (read-from-minibuffer "Run after idle (seconds): " nil nil t)
385 (y-or-n-p "Repeat each time Emacs is idle? ")
386 (intern (completing-read "Function: " obarray 'fboundp t))))
9d690693
KH
387 (let ((timer (timer-create)))
388 (timer-set-function timer function args)
cf9b15ce 389 (timer-set-idle-time timer secs repeat)
903c64e9 390 (timer-activate-when-idle timer)
9d690693 391 timer))
c4ba2209 392\f
c4ba2209
RS
393(defun with-timeout-handler (tag)
394 (throw tag 'timeout))
395
396;;;###autoload (put 'with-timeout 'lisp-indent-function 1)
397
398;;;###autoload
399(defmacro with-timeout (list &rest body)
400 "Run BODY, but if it doesn't finish in SECONDS seconds, give up.
401If we give up, we run the TIMEOUT-FORMS and return the value of the last one.
cf9b15ce
RS
402The call should look like:
403 (with-timeout (SECONDS TIMEOUT-FORMS...) BODY...)
c4ba2209
RS
404The timeout is checked whenever Emacs waits for some kind of external
405event \(such as keyboard input, input from subprocesses, or a certain time);
406if the program loops without waiting in any way, the timeout will not
407be detected."
408 (let ((seconds (car list))
409 (timeout-forms (cdr list)))
410 `(let ((with-timeout-tag (cons nil nil))
411 with-timeout-value with-timeout-timer)
412 (if (catch with-timeout-tag
413 (progn
414 (setq with-timeout-timer
415 (run-with-timer ,seconds nil
416 'with-timeout-handler
417 with-timeout-tag))
418 (setq with-timeout-value (progn . ,body))
419 nil))
420 (progn . ,timeout-forms)
421 (cancel-timer with-timeout-timer)
422 with-timeout-value))))
423
424(defun y-or-n-p-with-timeout (prompt seconds default-value)
425 "Like (y-or-n-p PROMPT), with a timeout.
426If the user does not answer after SECONDS seconds, return DEFAULT-VALUE."
427 (with-timeout (seconds default-value)
428 (y-or-n-p prompt)))
429\f
430(defvar timer-duration-words
431 (list (cons "microsec" 0.000001)
432 (cons "microsecond" 0.000001)
433 (cons "millisec" 0.001)
434 (cons "millisecond" 0.001)
435 (cons "sec" 1)
436 (cons "second" 1)
437 (cons "min" 60)
438 (cons "minute" 60)
439 (cons "hour" (* 60 60))
440 (cons "day" (* 24 60 60))
441 (cons "week" (* 7 24 60 60))
442 (cons "fortnight" (* 14 24 60 60))
443 (cons "month" (* 30 24 60 60)) ; Approximation
444 (cons "year" (* 365.25 24 60 60)) ; Approximation
445 )
446 "Alist mapping temporal words to durations in seconds")
447
448(defun timer-duration (string)
449 "Return number of seconds specified by STRING, or nil if parsing fails."
450 (let ((secs 0)
451 (start 0)
452 (case-fold-search t))
453 (while (string-match
454 "[ \t]*\\([0-9.]+\\)?[ \t]*\\([a-z]+[a-rt-z]\\)s?[ \t]*"
455 string start)
456 (let ((count (if (match-beginning 1)
457 (string-to-number (match-string 1 string))
458 1))
459 (itemsize (cdr (assoc (match-string 2 string)
460 timer-duration-words))))
461 (if itemsize
462 (setq start (match-end 0)
463 secs (+ secs (* count itemsize)))
464 (setq secs nil
465 start (length string)))))
1b966390
RS
466 (if (= start (length string))
467 secs
468 (if (string-match "\\`[0-9.]+\\'" string)
469 (string-to-number string)))))
4395bfdb 470\f
c4ba2209 471(provide 'timer)
4395bfdb 472
c4ba2209 473;;; timer.el ends here