Commit | Line | Data |
---|---|---|
5e046f6d JB |
1 | ;;; timer.el --- run a function with args at some time in future |
2 | ||
73b0cd50 | 3 | ;; Copyright (C) 1996, 2001-2011 Free Software Foundation, Inc. |
5e046f6d JB |
4 | |
5 | ;; Maintainer: FSF | |
bd78fa1d | 6 | ;; Package: emacs |
5e046f6d JB |
7 | |
8 | ;; This file is part of GNU Emacs. | |
9 | ||
d6cba7ae | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
5e046f6d | 11 | ;; it under the terms of the GNU General Public License as published by |
d6cba7ae GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
5e046f6d JB |
14 | |
15 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | ;; GNU General Public License for more details. | |
19 | ||
20 | ;; You should have received a copy of the GNU General Public License | |
d6cba7ae | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
5e046f6d JB |
22 | |
23 | ;;; Commentary: | |
24 | ||
25 | ;; This package gives you the capability to run Emacs Lisp commands at | |
26 | ;; specified times in the future, either as one-shots or periodically. | |
27 | ||
28 | ;;; Code: | |
29 | ||
30 | ;; Layout of a timer vector: | |
31 | ;; [triggered-p high-seconds low-seconds usecs repeat-delay | |
32 | ;; function args idle-delay] | |
b8bd37f2 RS |
33 | ;; triggered-p is nil if the timer is active (waiting to be triggered), |
34 | ;; t if it is inactive ("already triggered", in theory) | |
5e046f6d | 35 | |
e0f0f3ef SM |
36 | (eval-when-compile (require 'cl)) |
37 | ||
38 | (defstruct (timer | |
39 | (:constructor nil) | |
40 | (:copier nil) | |
41 | (:constructor timer-create ()) | |
42 | (:type vector) | |
43 | (:conc-name timer--)) | |
44 | (triggered t) | |
45 | high-seconds low-seconds usecs repeat-delay function args idle-delay) | |
5e046f6d JB |
46 | |
47 | (defun timerp (object) | |
48 | "Return t if OBJECT is a timer." | |
49 | (and (vectorp object) (= (length object) 8))) | |
50 | ||
e0f0f3ef SM |
51 | ;; Pseudo field `time'. |
52 | (defun timer--time (timer) | |
53 | (list (timer--high-seconds timer) | |
54 | (timer--low-seconds timer) | |
55 | (timer--usecs timer))) | |
56 | ||
57 | (defsetf timer--time | |
58 | (lambda (timer time) | |
59 | (or (timerp timer) (error "Invalid timer")) | |
60 | (setf (timer--high-seconds timer) (pop time)) | |
61 | (setf (timer--low-seconds timer) | |
62 | (if (consp time) (car time) time)) | |
63 | (setf (timer--usecs timer) (or (and (consp time) (consp (cdr time)) | |
64 | (cadr time)) | |
65 | 0)))) | |
66 | ||
67 | ||
5e046f6d JB |
68 | (defun timer-set-time (timer time &optional delta) |
69 | "Set the trigger time of TIMER to TIME. | |
70 | TIME must be in the internal format returned by, e.g., `current-time'. | |
71 | If optional third argument DELTA is a positive number, make the timer | |
72 | fire repeatedly that many seconds apart." | |
e0f0f3ef SM |
73 | (setf (timer--time timer) time) |
74 | (setf (timer--repeat-delay timer) (and (numberp delta) (> delta 0) delta)) | |
5e046f6d JB |
75 | timer) |
76 | ||
77 | (defun timer-set-idle-time (timer secs &optional repeat) | |
78 | "Set the trigger idle time of TIMER to SECS. | |
1063efe8 CY |
79 | SECS may be an integer, floating point number, or the internal |
80 | time format (HIGH LOW USECS) returned by, e.g., `current-idle-time'. | |
5e046f6d JB |
81 | If optional third argument REPEAT is non-nil, make the timer |
82 | fire each time Emacs is idle for that many seconds." | |
1063efe8 | 83 | (if (consp secs) |
e0f0f3ef SM |
84 | (setf (timer--time timer) secs) |
85 | (setf (timer--time timer) '(0 0 0)) | |
1063efe8 | 86 | (timer-inc-time timer secs)) |
e0f0f3ef | 87 | (setf (timer--repeat-delay timer) repeat) |
5e046f6d JB |
88 | timer) |
89 | ||
90 | (defun timer-next-integral-multiple-of-time (time secs) | |
91 | "Yield the next value after TIME that is an integral multiple of SECS. | |
92 | More precisely, the next value, after TIME, that is an integral multiple | |
93 | of SECS seconds since the epoch. SECS may be a fraction." | |
94 | (let ((time-base (ash 1 16))) | |
9a0dd02d GM |
95 | ;; Use floating point, taking care to not lose precision. |
96 | (let* ((float-time-base (float time-base)) | |
97 | (million 1000000.0) | |
98 | (time-usec (+ (* million | |
99 | (+ (* float-time-base (nth 0 time)) | |
100 | (nth 1 time))) | |
101 | (nth 2 time))) | |
102 | (secs-usec (* million secs)) | |
103 | (mod-usec (mod time-usec secs-usec)) | |
104 | (next-usec (+ (- time-usec mod-usec) secs-usec)) | |
105 | (time-base-million (* float-time-base million))) | |
106 | (list (floor next-usec time-base-million) | |
107 | (floor (mod next-usec time-base-million) million) | |
108 | (floor (mod next-usec million)))))) | |
5e046f6d JB |
109 | |
110 | (defun timer-relative-time (time secs &optional usecs) | |
111 | "Advance TIME by SECS seconds and optionally USECS microseconds. | |
1063efe8 | 112 | SECS may be either an integer or a floating point number." |
e0f0f3ef | 113 | ;; FIXME: we should just use (time-add time (list 0 secs usecs)) |
5e046f6d JB |
114 | (let ((high (car time)) |
115 | (low (if (consp (cdr time)) (nth 1 time) (cdr time))) | |
116 | (micro (if (numberp (car-safe (cdr-safe (cdr time)))) | |
117 | (nth 2 time) | |
118 | 0))) | |
119 | ;; Add | |
120 | (if usecs (setq micro (+ micro usecs))) | |
121 | (if (floatp secs) | |
122 | (setq micro (+ micro (floor (* 1000000 (- secs (floor secs))))))) | |
123 | (setq low (+ low (floor secs))) | |
124 | ||
125 | ;; Normalize | |
126 | ;; `/' rounds towards zero while `mod' returns a positive number, | |
127 | ;; so we can't rely on (= a (+ (* 100 (/ a 100)) (mod a 100))). | |
128 | (setq low (+ low (/ micro 1000000) (if (< micro 0) -1 0))) | |
129 | (setq micro (mod micro 1000000)) | |
130 | (setq high (+ high (/ low 65536) (if (< low 0) -1 0))) | |
131 | (setq low (logand low 65535)) | |
132 | ||
133 | (list high low (and (/= micro 0) micro)))) | |
134 | ||
e0f0f3ef SM |
135 | (defun timer--time-less-p (t1 t2) |
136 | "Say whether time value T1 is less than time value T2." | |
137 | ;; FIXME just use time-less-p. | |
138 | (destructuring-bind (high1 low1 micro1) (timer--time t1) | |
139 | (destructuring-bind (high2 low2 micro2) (timer--time t2) | |
140 | (or (< high1 high2) | |
141 | (and (= high1 high2) | |
142 | (or (< low1 low2) | |
143 | (and (= low1 low2) | |
144 | (< micro1 micro2)))))))) | |
145 | ||
5e046f6d JB |
146 | (defun timer-inc-time (timer secs &optional usecs) |
147 | "Increment the time set in TIMER by SECS seconds and USECS microseconds. | |
148 | SECS may be a fraction. If USECS is omitted, that means it is zero." | |
e0f0f3ef SM |
149 | (setf (timer--time timer) |
150 | (timer-relative-time (timer--time timer) secs usecs))) | |
5e046f6d JB |
151 | |
152 | (defun timer-set-time-with-usecs (timer time usecs &optional delta) | |
153 | "Set the trigger time of TIMER to TIME plus USECS. | |
154 | TIME must be in the internal format returned by, e.g., `current-time'. | |
155 | The microsecond count from TIME is ignored, and USECS is used instead. | |
156 | If optional fourth argument DELTA is a positive number, make the timer | |
157 | fire repeatedly that many seconds apart." | |
e0f0f3ef SM |
158 | (setf (timer--time timer) time) |
159 | (setf (timer--usecs timer) usecs) | |
160 | (setf (timer--repeat-delay timer) (and (numberp delta) (> delta 0) delta)) | |
5e046f6d JB |
161 | timer) |
162 | (make-obsolete 'timer-set-time-with-usecs | |
163 | "use `timer-set-time' and `timer-inc-time' instead." | |
bf247b6e | 164 | "22.1") |
5e046f6d JB |
165 | |
166 | (defun timer-set-function (timer function &optional args) | |
167 | "Make TIMER call FUNCTION with optional ARGS when triggering." | |
168 | (or (timerp timer) | |
169 | (error "Invalid timer")) | |
e0f0f3ef SM |
170 | (setf (timer--function timer) function) |
171 | (setf (timer--args timer) args) | |
5e046f6d JB |
172 | timer) |
173 | \f | |
e0f0f3ef | 174 | (defun timer--activate (timer &optional triggered-p reuse-cell idle) |
5e046f6d | 175 | (if (and (timerp timer) |
e0f0f3ef SM |
176 | (integerp (timer--high-seconds timer)) |
177 | (integerp (timer--low-seconds timer)) | |
178 | (integerp (timer--usecs timer)) | |
179 | (timer--function timer)) | |
180 | (let ((timers (if idle timer-idle-list timer-list)) | |
5e046f6d JB |
181 | last) |
182 | ;; Skip all timers to trigger before the new one. | |
e0f0f3ef | 183 | (while (and timers (timer--time-less-p (car timers) timer)) |
5e046f6d JB |
184 | (setq last timers |
185 | timers (cdr timers))) | |
dbbee529 RS |
186 | (if reuse-cell |
187 | (progn | |
188 | (setcar reuse-cell timer) | |
189 | (setcdr reuse-cell timers)) | |
190 | (setq reuse-cell (cons timer timers))) | |
5e046f6d JB |
191 | ;; Insert new timer after last which possibly means in front of queue. |
192 | (if last | |
dbbee529 | 193 | (setcdr last reuse-cell) |
e0f0f3ef SM |
194 | (if idle |
195 | (setq timer-idle-list reuse-cell) | |
196 | (setq timer-list reuse-cell))) | |
197 | (setf (timer--triggered timer) triggered-p) | |
198 | (setf (timer--idle-delay timer) idle) | |
5e046f6d JB |
199 | nil) |
200 | (error "Invalid or uninitialized timer"))) | |
201 | ||
e0f0f3ef SM |
202 | (defun timer-activate (timer &optional triggered-p reuse-cell idle) |
203 | "Put TIMER on the list of active timers. | |
204 | ||
205 | If TRIGGERED-P is t, that means to make the timer inactive | |
206 | \(put it on the list, but mark it as already triggered). | |
207 | To remove from the list, use `cancel-timer'. | |
208 | ||
209 | REUSE-CELL, if non-nil, is a cons cell to reuse instead | |
210 | of allocating a new one." | |
211 | (timer--activate timer triggered-p reuse-cell nil)) | |
212 | ||
dbbee529 | 213 | (defun timer-activate-when-idle (timer &optional dont-wait reuse-cell) |
5e046f6d JB |
214 | "Arrange to activate TIMER whenever Emacs is next idle. |
215 | If optional argument DONT-WAIT is non-nil, then enable the | |
216 | timer to activate immediately, or at the right time, if Emacs | |
dbbee529 RS |
217 | is already idle. |
218 | ||
219 | REUSE-CELL, if non-nil, is a cons cell to reuse instead | |
220 | of allocating a new one." | |
e0f0f3ef | 221 | (timer--activate timer (not dont-wait) reuse-cell 'idle)) |
5e046f6d | 222 | |
5e046f6d | 223 | (defalias 'disable-timeout 'cancel-timer) |
f31b1257 | 224 | |
5e046f6d JB |
225 | (defun cancel-timer (timer) |
226 | "Remove TIMER from the list of active timers." | |
227 | (or (timerp timer) | |
228 | (error "Invalid timer")) | |
229 | (setq timer-list (delq timer timer-list)) | |
230 | (setq timer-idle-list (delq timer timer-idle-list)) | |
231 | nil) | |
232 | ||
dbbee529 | 233 | (defun cancel-timer-internal (timer) |
b8bd37f2 RS |
234 | "Remove TIMER from the list of active timers or idle timers. |
235 | Only to be used in this file. It returns the cons cell | |
236 | that was removed from the timer list." | |
dbbee529 RS |
237 | (let ((cell1 (memq timer timer-list)) |
238 | (cell2 (memq timer timer-idle-list))) | |
239 | (if cell1 | |
240 | (setq timer-list (delq timer timer-list))) | |
241 | (if cell2 | |
242 | (setq timer-idle-list (delq timer timer-idle-list))) | |
243 | (or cell1 cell2))) | |
244 | ||
5e046f6d | 245 | (defun cancel-function-timers (function) |
b8bd37f2 RS |
246 | "Cancel all timers which would run FUNCTION. |
247 | This affects ordinary timers such as are scheduled by `run-at-time', | |
248 | and idle timers such as are scheduled by `run-with-idle-timer'." | |
5e046f6d | 249 | (interactive "aCancel timers of function: ") |
e0f0f3ef SM |
250 | (dolist (timer timer-list) |
251 | (if (eq (timer--function timer) function) | |
252 | (setq timer-list (delq timer timer-list)))) | |
253 | (dolist (timer timer-idle-list) | |
254 | (if (eq (timer--function timer) function) | |
255 | (setq timer-idle-list (delq timer timer-idle-list))))) | |
5e046f6d JB |
256 | \f |
257 | ;; Record the last few events, for debugging. | |
b8bd37f2 RS |
258 | (defvar timer-event-last nil |
259 | "Last timer that was run.") | |
260 | (defvar timer-event-last-1 nil | |
261 | "Next-to-last timer that was run.") | |
262 | (defvar timer-event-last-2 nil | |
263 | "Third-to-last timer that was run.") | |
5e046f6d JB |
264 | |
265 | (defvar timer-max-repeats 10 | |
e00c3f6f RS |
266 | "*Maximum number of times to repeat a timer, if many repeats are delayed. |
267 | Timer invocations can be delayed because Emacs is suspended or busy, | |
268 | or because the system's time changes. If such an occurrence makes it | |
269 | appear that many invocations are overdue, this variable controls | |
270 | how many will really happen.") | |
5e046f6d JB |
271 | |
272 | (defun timer-until (timer time) | |
273 | "Calculate number of seconds from when TIMER will run, until TIME. | |
274 | TIMER is a timer, and stands for the time when its next repeat is scheduled. | |
275 | TIME is a time-list." | |
5b669447 | 276 | ;; FIXME: (float-time (time-subtract (timer--time timer) time)) |
e0f0f3ef SM |
277 | (let ((high (- (car time) (timer--high-seconds timer))) |
278 | (low (- (nth 1 time) (timer--low-seconds timer)))) | |
5e046f6d JB |
279 | (+ low (* high 65536)))) |
280 | ||
281 | (defun timer-event-handler (timer) | |
282 | "Call the handler for the timer TIMER. | |
283 | This function is called, by name, directly by the C code." | |
284 | (setq timer-event-last-2 timer-event-last-1) | |
285 | (setq timer-event-last-1 timer-event-last) | |
286 | (setq timer-event-last timer) | |
287 | (let ((inhibit-quit t)) | |
288 | (if (timerp timer) | |
dbbee529 RS |
289 | (let (retrigger cell) |
290 | ;; Delete from queue. Record the cons cell that was used. | |
291 | (setq cell (cancel-timer-internal timer)) | |
5e046f6d | 292 | ;; Re-schedule if requested. |
e0f0f3ef SM |
293 | (if (timer--repeat-delay timer) |
294 | (if (timer--idle-delay timer) | |
dbbee529 | 295 | (timer-activate-when-idle timer nil cell) |
e0f0f3ef | 296 | (timer-inc-time timer (timer--repeat-delay timer) 0) |
5e046f6d JB |
297 | ;; If real time has jumped forward, |
298 | ;; perhaps because Emacs was suspended for a long time, | |
299 | ;; limit how many times things get repeated. | |
300 | (if (and (numberp timer-max-repeats) | |
301 | (< 0 (timer-until timer (current-time)))) | |
302 | (let ((repeats (/ (timer-until timer (current-time)) | |
e0f0f3ef | 303 | (timer--repeat-delay timer)))) |
5e046f6d | 304 | (if (> repeats timer-max-repeats) |
e0f0f3ef SM |
305 | (timer-inc-time timer (* (timer--repeat-delay timer) |
306 | repeats))))) | |
dbbee529 | 307 | (timer-activate timer t cell) |
6160f933 | 308 | (setq retrigger t))) |
5e046f6d JB |
309 | ;; Run handler. |
310 | ;; We do this after rescheduling so that the handler function | |
311 | ;; can cancel its own timer successfully with cancel-timer. | |
312 | (condition-case nil | |
0798a8d8 SM |
313 | ;; Timer functions should not change the current buffer. |
314 | ;; If they do, all kinds of nasty surprises can happen, | |
315 | ;; and it can be hellish to track down their source. | |
316 | (save-current-buffer | |
317 | (apply (timer--function timer) (timer--args timer))) | |
6160f933 KS |
318 | (error nil)) |
319 | (if retrigger | |
e0f0f3ef | 320 | (setf (timer--triggered timer) nil))) |
5e046f6d JB |
321 | (error "Bogus timer event")))) |
322 | ||
323 | ;; This function is incompatible with the one in levents.el. | |
324 | (defun timeout-event-p (event) | |
325 | "Non-nil if EVENT is a timeout event." | |
326 | (and (listp event) (eq (car event) 'timer-event))) | |
327 | \f | |
153ef845 | 328 | |
5cec3056 | 329 | (declare-function diary-entry-time "diary-lib" (s)) |
153ef845 | 330 | |
5e046f6d JB |
331 | (defun run-at-time (time repeat function &rest args) |
332 | "Perform an action at time TIME. | |
333 | Repeat the action every REPEAT seconds, if REPEAT is non-nil. | |
b9be25fd GM |
334 | TIME should be one of: a string giving an absolute time like |
335 | \"11:23pm\" (the acceptable formats are those recognized by | |
336 | `diary-entry-time'; note that such times are interpreted as times | |
337 | today, even if in the past); a string giving a relative time like | |
338 | \"2 hours 35 minutes\" (the acceptable formats are those | |
339 | recognized by `timer-duration'); nil meaning now; a number of | |
340 | seconds from now; a value from `encode-time'; or t (with non-nil | |
341 | REPEAT) meaning the next integral multiple of REPEAT. REPEAT may | |
342 | be an integer or floating point number. The action is to call | |
343 | FUNCTION with arguments ARGS. | |
5e046f6d JB |
344 | |
345 | This function returns a timer object which you can use in `cancel-timer'." | |
346 | (interactive "sRun at time: \nNRepeat interval: \naFunction: ") | |
347 | ||
348 | (or (null repeat) | |
349 | (and (numberp repeat) (< 0 repeat)) | |
350 | (error "Invalid repetition interval")) | |
351 | ||
352 | ;; Special case: nil means "now" and is useful when repeating. | |
353 | (if (null time) | |
354 | (setq time (current-time))) | |
355 | ||
356 | ;; Special case: t means the next integral multiple of REPEAT. | |
357 | (if (and (eq time t) repeat) | |
358 | (setq time (timer-next-integral-multiple-of-time (current-time) repeat))) | |
359 | ||
360 | ;; Handle numbers as relative times in seconds. | |
361 | (if (numberp time) | |
362 | (setq time (timer-relative-time (current-time) time))) | |
363 | ||
b9be25fd | 364 | ;; Handle relative times like "2 hours 35 minutes" |
5e046f6d JB |
365 | (if (stringp time) |
366 | (let ((secs (timer-duration time))) | |
367 | (if secs | |
368 | (setq time (timer-relative-time (current-time) secs))))) | |
369 | ||
370 | ;; Handle "11:23pm" and the like. Interpret it as meaning today | |
371 | ;; which admittedly is rather stupid if we have passed that time | |
372 | ;; already. (Though only Emacs hackers hack Emacs at that time.) | |
373 | (if (stringp time) | |
374 | (progn | |
375 | (require 'diary-lib) | |
376 | (let ((hhmm (diary-entry-time time)) | |
377 | (now (decode-time))) | |
378 | (if (>= hhmm 0) | |
379 | (setq time | |
380 | (encode-time 0 (% hhmm 100) (/ hhmm 100) (nth 3 now) | |
381 | (nth 4 now) (nth 5 now) (nth 8 now))))))) | |
382 | ||
383 | (or (consp time) | |
384 | (error "Invalid time format")) | |
385 | ||
386 | (let ((timer (timer-create))) | |
387 | (timer-set-time timer time repeat) | |
388 | (timer-set-function timer function args) | |
389 | (timer-activate timer) | |
390 | timer)) | |
391 | ||
5e046f6d JB |
392 | (defun run-with-timer (secs repeat function &rest args) |
393 | "Perform an action after a delay of SECS seconds. | |
394 | Repeat the action every REPEAT seconds, if REPEAT is non-nil. | |
395 | SECS and REPEAT may be integers or floating point numbers. | |
396 | The action is to call FUNCTION with arguments ARGS. | |
397 | ||
398 | This function returns a timer object which you can use in `cancel-timer'." | |
399 | (interactive "sRun after delay (seconds): \nNRepeat interval: \naFunction: ") | |
400 | (apply 'run-at-time secs repeat function args)) | |
401 | ||
5e046f6d JB |
402 | (defun add-timeout (secs function object &optional repeat) |
403 | "Add a timer to run SECS seconds from now, to call FUNCTION on OBJECT. | |
404 | If REPEAT is non-nil, repeat the timer every REPEAT seconds. | |
405 | This function is for compatibility; see also `run-with-timer'." | |
406 | (run-with-timer secs repeat function object)) | |
407 | ||
5e046f6d JB |
408 | (defun run-with-idle-timer (secs repeat function &rest args) |
409 | "Perform an action the next time Emacs is idle for SECS seconds. | |
410 | The action is to call FUNCTION with arguments ARGS. | |
1063efe8 CY |
411 | SECS may be an integer, a floating point number, or the internal |
412 | time format (HIGH LOW USECS) returned by, e.g., `current-idle-time'. | |
171d328e RS |
413 | If Emacs is currently idle, and has been idle for N seconds (N < SECS), |
414 | then it will call FUNCTION in SECS - N seconds from now. | |
5e046f6d JB |
415 | |
416 | If REPEAT is non-nil, do the action each time Emacs has been idle for | |
417 | exactly SECS seconds (that is, only once for each time Emacs becomes idle). | |
418 | ||
419 | This function returns a timer object which you can use in `cancel-timer'." | |
420 | (interactive | |
421 | (list (read-from-minibuffer "Run after idle (seconds): " nil nil t) | |
422 | (y-or-n-p "Repeat each time Emacs is idle? ") | |
423 | (intern (completing-read "Function: " obarray 'fboundp t)))) | |
424 | (let ((timer (timer-create))) | |
425 | (timer-set-function timer function args) | |
426 | (timer-set-idle-time timer secs repeat) | |
171d328e | 427 | (timer-activate-when-idle timer t) |
5e046f6d JB |
428 | timer)) |
429 | \f | |
430 | (defun with-timeout-handler (tag) | |
b8bd37f2 | 431 | "This is the timer function used for the timer made by `with-timeout'." |
5e046f6d JB |
432 | (throw tag 'timeout)) |
433 | ||
89c020e8 RS |
434 | (defvar with-timeout-timers nil |
435 | "List of all timers used by currently pending `with-timeout' calls.") | |
436 | ||
5e046f6d JB |
437 | (defmacro with-timeout (list &rest body) |
438 | "Run BODY, but if it doesn't finish in SECONDS seconds, give up. | |
439 | If we give up, we run the TIMEOUT-FORMS and return the value of the last one. | |
5e046f6d | 440 | The timeout is checked whenever Emacs waits for some kind of external |
7452fadc | 441 | event (such as keyboard input, input from subprocesses, or a certain time); |
5e046f6d | 442 | if the program loops without waiting in any way, the timeout will not |
7452fadc JB |
443 | be detected. |
444 | \n(fn (SECONDS TIMEOUT-FORMS...) BODY)" | |
f291fe60 | 445 | (declare (indent 1)) |
5e046f6d JB |
446 | (let ((seconds (car list)) |
447 | (timeout-forms (cdr list))) | |
448 | `(let ((with-timeout-tag (cons nil nil)) | |
89c020e8 RS |
449 | with-timeout-value with-timeout-timer |
450 | (with-timeout-timers with-timeout-timers)) | |
5e046f6d JB |
451 | (if (catch with-timeout-tag |
452 | (progn | |
453 | (setq with-timeout-timer | |
454 | (run-with-timer ,seconds nil | |
455 | 'with-timeout-handler | |
456 | with-timeout-tag)) | |
89c020e8 | 457 | (push with-timeout-timer with-timeout-timers) |
5e046f6d JB |
458 | (setq with-timeout-value (progn . ,body)) |
459 | nil)) | |
460 | (progn . ,timeout-forms) | |
461 | (cancel-timer with-timeout-timer) | |
462 | with-timeout-value)))) | |
463 | ||
89c020e8 RS |
464 | (defun with-timeout-suspend () |
465 | "Stop the clock for `with-timeout'. Used by debuggers. | |
466 | The idea is that the time you spend in the debugger should not | |
467 | count against these timeouts. | |
468 | ||
469 | The value is a list that the debugger can pass to `with-timeout-unsuspend' | |
470 | when it exits, to make these timers start counting again." | |
471 | (mapcar (lambda (timer) | |
472 | (cancel-timer timer) | |
e0f0f3ef | 473 | (list timer (time-subtract (timer--time timer) (current-time)))) |
89c020e8 RS |
474 | with-timeout-timers)) |
475 | ||
476 | (defun with-timeout-unsuspend (timer-spec-list) | |
477 | "Restart the clock for `with-timeout'. | |
478 | The argument should be a value previously returned by `with-timeout-suspend'." | |
479 | (dolist (elt timer-spec-list) | |
480 | (let ((timer (car elt)) | |
481 | (delay (cadr elt))) | |
64ba814f | 482 | (timer-set-time timer (time-add (current-time) delay)) |
89c020e8 RS |
483 | (timer-activate timer)))) |
484 | ||
5e046f6d JB |
485 | (defun y-or-n-p-with-timeout (prompt seconds default-value) |
486 | "Like (y-or-n-p PROMPT), with a timeout. | |
487 | If the user does not answer after SECONDS seconds, return DEFAULT-VALUE." | |
488 | (with-timeout (seconds default-value) | |
489 | (y-or-n-p prompt))) | |
490 | \f | |
2f7f4bee | 491 | (defconst timer-duration-words |
5e046f6d JB |
492 | (list (cons "microsec" 0.000001) |
493 | (cons "microsecond" 0.000001) | |
494 | (cons "millisec" 0.001) | |
495 | (cons "millisecond" 0.001) | |
496 | (cons "sec" 1) | |
497 | (cons "second" 1) | |
498 | (cons "min" 60) | |
499 | (cons "minute" 60) | |
500 | (cons "hour" (* 60 60)) | |
501 | (cons "day" (* 24 60 60)) | |
502 | (cons "week" (* 7 24 60 60)) | |
503 | (cons "fortnight" (* 14 24 60 60)) | |
504 | (cons "month" (* 30 24 60 60)) ; Approximation | |
505 | (cons "year" (* 365.25 24 60 60)) ; Approximation | |
506 | ) | |
64ba814f | 507 | "Alist mapping temporal words to durations in seconds.") |
5e046f6d JB |
508 | |
509 | (defun timer-duration (string) | |
510 | "Return number of seconds specified by STRING, or nil if parsing fails." | |
511 | (let ((secs 0) | |
512 | (start 0) | |
513 | (case-fold-search t)) | |
514 | (while (string-match | |
515 | "[ \t]*\\([0-9.]+\\)?[ \t]*\\([a-z]+[a-rt-z]\\)s?[ \t]*" | |
516 | string start) | |
517 | (let ((count (if (match-beginning 1) | |
518 | (string-to-number (match-string 1 string)) | |
519 | 1)) | |
520 | (itemsize (cdr (assoc (match-string 2 string) | |
521 | timer-duration-words)))) | |
522 | (if itemsize | |
523 | (setq start (match-end 0) | |
524 | secs (+ secs (* count itemsize))) | |
525 | (setq secs nil | |
526 | start (length string))))) | |
527 | (if (= start (length string)) | |
528 | secs | |
64ba814f | 529 | (if (string-match-p "\\`[0-9.]+\\'" string) |
5e046f6d JB |
530 | (string-to-number string))))) |
531 | \f | |
532 | (provide 'timer) | |
533 | ||
534 | ;;; timer.el ends here |