Commit | Line | Data |
---|---|---|
5e046f6d JB |
1 | ;;; timer.el --- run a function with args at some time in future |
2 | ||
acaf905b | 3 | ;; Copyright (C) 1996, 2001-2012 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 | |
f80efb86 | 36 | (eval-when-compile (require 'cl-lib)) |
e0f0f3ef | 37 | |
f80efb86 | 38 | (cl-defstruct (timer |
e0f0f3ef SM |
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 | ||
2ee3d7f0 | 57 | (gv-define-simple-setter timer--time |
e0f0f3ef SM |
58 | (lambda (timer time) |
59 | (or (timerp timer) (error "Invalid timer")) | |
2ee3d7f0 SM |
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)) | |
f80efb86 SM |
64 | (cadr time)) |
65 | 0)))) | |
e0f0f3ef SM |
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." | |
2ee3d7f0 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) |
2ee3d7f0 SM |
84 | (setf (timer--time timer) secs) |
85 | (setf (timer--time timer) '(0 0 0)) | |
1063efe8 | 86 | (timer-inc-time timer secs)) |
2ee3d7f0 | 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." |
fe955043 PE |
113 | (let ((delta (if (floatp secs) |
114 | (seconds-to-time secs) | |
115 | (list (floor secs 65536) (mod secs 65536))))) | |
116 | (if usecs | |
117 | (setq delta (time-add delta (list 0 0 usecs)))) | |
118 | (time-add time delta))) | |
5e046f6d | 119 | |
e0f0f3ef SM |
120 | (defun timer--time-less-p (t1 t2) |
121 | "Say whether time value T1 is less than time value T2." | |
08235028 | 122 | (time-less-p (timer--time t1) (timer--time t2))) |
e0f0f3ef | 123 | |
5e046f6d JB |
124 | (defun timer-inc-time (timer secs &optional usecs) |
125 | "Increment the time set in TIMER by SECS seconds and USECS microseconds. | |
126 | SECS may be a fraction. If USECS is omitted, that means it is zero." | |
2ee3d7f0 SM |
127 | (setf (timer--time timer) |
128 | (timer-relative-time (timer--time timer) secs usecs))) | |
5e046f6d JB |
129 | |
130 | (defun timer-set-time-with-usecs (timer time usecs &optional delta) | |
131 | "Set the trigger time of TIMER to TIME plus USECS. | |
132 | TIME must be in the internal format returned by, e.g., `current-time'. | |
133 | The microsecond count from TIME is ignored, and USECS is used instead. | |
134 | If optional fourth argument DELTA is a positive number, make the timer | |
135 | fire repeatedly that many seconds apart." | |
2ee3d7f0 SM |
136 | (setf (timer--time timer) time) |
137 | (setf (timer--usecs timer) usecs) | |
138 | (setf (timer--repeat-delay timer) (and (numberp delta) (> delta 0) delta)) | |
5e046f6d JB |
139 | timer) |
140 | (make-obsolete 'timer-set-time-with-usecs | |
141 | "use `timer-set-time' and `timer-inc-time' instead." | |
bf247b6e | 142 | "22.1") |
5e046f6d JB |
143 | |
144 | (defun timer-set-function (timer function &optional args) | |
145 | "Make TIMER call FUNCTION with optional ARGS when triggering." | |
146 | (or (timerp timer) | |
147 | (error "Invalid timer")) | |
2ee3d7f0 SM |
148 | (setf (timer--function timer) function) |
149 | (setf (timer--args timer) args) | |
5e046f6d JB |
150 | timer) |
151 | \f | |
e0f0f3ef | 152 | (defun timer--activate (timer &optional triggered-p reuse-cell idle) |
5e046f6d | 153 | (if (and (timerp timer) |
e0f0f3ef SM |
154 | (integerp (timer--high-seconds timer)) |
155 | (integerp (timer--low-seconds timer)) | |
156 | (integerp (timer--usecs timer)) | |
157 | (timer--function timer)) | |
158 | (let ((timers (if idle timer-idle-list timer-list)) | |
5e046f6d JB |
159 | last) |
160 | ;; Skip all timers to trigger before the new one. | |
e0f0f3ef | 161 | (while (and timers (timer--time-less-p (car timers) timer)) |
5e046f6d JB |
162 | (setq last timers |
163 | timers (cdr timers))) | |
dbbee529 RS |
164 | (if reuse-cell |
165 | (progn | |
166 | (setcar reuse-cell timer) | |
167 | (setcdr reuse-cell timers)) | |
168 | (setq reuse-cell (cons timer timers))) | |
5e046f6d | 169 | ;; Insert new timer after last which possibly means in front of queue. |
7dbe3dbc CY |
170 | (cond (last (setcdr last reuse-cell)) |
171 | (idle (setq timer-idle-list reuse-cell)) | |
172 | (t (setq timer-list reuse-cell))) | |
2ee3d7f0 SM |
173 | (setf (timer--triggered timer) triggered-p) |
174 | (setf (timer--idle-delay timer) idle) | |
5e046f6d JB |
175 | nil) |
176 | (error "Invalid or uninitialized timer"))) | |
177 | ||
7dbe3dbc CY |
178 | (defun timer-activate (timer &optional triggered-p reuse-cell) |
179 | "Insert TIMER into `timer-list'. | |
180 | If TRIGGERED-P is t, make TIMER inactive (put it on the list, but | |
181 | mark it as already triggered). To remove it, use `cancel-timer'. | |
e0f0f3ef | 182 | |
7dbe3dbc CY |
183 | REUSE-CELL, if non-nil, is a cons cell to reuse when inserting |
184 | TIMER into `timer-list' (usually a cell removed from that list by | |
185 | `cancel-timer-internal'; using this reduces consing for repeat | |
186 | timers). If nil, allocate a new cell." | |
e0f0f3ef SM |
187 | (timer--activate timer triggered-p reuse-cell nil)) |
188 | ||
dbbee529 | 189 | (defun timer-activate-when-idle (timer &optional dont-wait reuse-cell) |
7dbe3dbc CY |
190 | "Insert TIMER into `timer-idle-list'. |
191 | This arranges to activate TIMER whenever Emacs is next idle. | |
192 | If optional argument DONT-WAIT is non-nil, set TIMER to activate | |
193 | immediately, or at the right time, if Emacs is already idle. | |
194 | ||
195 | REUSE-CELL, if non-nil, is a cons cell to reuse when inserting | |
196 | TIMER into `timer-idle-list' (usually a cell removed from that | |
197 | list by `cancel-timer-internal'; using this reduces consing for | |
198 | repeat timers). If nil, allocate a new cell." | |
e0f0f3ef | 199 | (timer--activate timer (not dont-wait) reuse-cell 'idle)) |
5e046f6d | 200 | |
5e046f6d | 201 | (defalias 'disable-timeout 'cancel-timer) |
f31b1257 | 202 | |
5e046f6d JB |
203 | (defun cancel-timer (timer) |
204 | "Remove TIMER from the list of active timers." | |
205 | (or (timerp timer) | |
206 | (error "Invalid timer")) | |
207 | (setq timer-list (delq timer timer-list)) | |
208 | (setq timer-idle-list (delq timer timer-idle-list)) | |
209 | nil) | |
210 | ||
dbbee529 | 211 | (defun cancel-timer-internal (timer) |
b8bd37f2 RS |
212 | "Remove TIMER from the list of active timers or idle timers. |
213 | Only to be used in this file. It returns the cons cell | |
214 | that was removed from the timer list." | |
dbbee529 RS |
215 | (let ((cell1 (memq timer timer-list)) |
216 | (cell2 (memq timer timer-idle-list))) | |
217 | (if cell1 | |
218 | (setq timer-list (delq timer timer-list))) | |
219 | (if cell2 | |
220 | (setq timer-idle-list (delq timer timer-idle-list))) | |
221 | (or cell1 cell2))) | |
222 | ||
5e046f6d | 223 | (defun cancel-function-timers (function) |
b8bd37f2 RS |
224 | "Cancel all timers which would run FUNCTION. |
225 | This affects ordinary timers such as are scheduled by `run-at-time', | |
226 | and idle timers such as are scheduled by `run-with-idle-timer'." | |
5e046f6d | 227 | (interactive "aCancel timers of function: ") |
e0f0f3ef SM |
228 | (dolist (timer timer-list) |
229 | (if (eq (timer--function timer) function) | |
230 | (setq timer-list (delq timer timer-list)))) | |
231 | (dolist (timer timer-idle-list) | |
232 | (if (eq (timer--function timer) function) | |
233 | (setq timer-idle-list (delq timer timer-idle-list))))) | |
5e046f6d JB |
234 | \f |
235 | ;; Record the last few events, for debugging. | |
b8bd37f2 RS |
236 | (defvar timer-event-last nil |
237 | "Last timer that was run.") | |
238 | (defvar timer-event-last-1 nil | |
239 | "Next-to-last timer that was run.") | |
240 | (defvar timer-event-last-2 nil | |
241 | "Third-to-last timer that was run.") | |
5e046f6d | 242 | |
78f3273a | 243 | (defcustom timer-max-repeats 10 |
fb7ada5f | 244 | "Maximum number of times to repeat a timer, if many repeats are delayed. |
e00c3f6f RS |
245 | Timer invocations can be delayed because Emacs is suspended or busy, |
246 | or because the system's time changes. If such an occurrence makes it | |
247 | appear that many invocations are overdue, this variable controls | |
78f3273a CY |
248 | how many will really happen." |
249 | :type 'integer | |
250 | :group 'internal) | |
5e046f6d JB |
251 | |
252 | (defun timer-until (timer time) | |
253 | "Calculate number of seconds from when TIMER will run, until TIME. | |
254 | TIMER is a timer, and stands for the time when its next repeat is scheduled. | |
255 | TIME is a time-list." | |
f34755dc | 256 | (float-time (time-subtract time (timer--time timer)))) |
5e046f6d JB |
257 | |
258 | (defun timer-event-handler (timer) | |
259 | "Call the handler for the timer TIMER. | |
260 | This function is called, by name, directly by the C code." | |
261 | (setq timer-event-last-2 timer-event-last-1) | |
262 | (setq timer-event-last-1 timer-event-last) | |
263 | (setq timer-event-last timer) | |
264 | (let ((inhibit-quit t)) | |
265 | (if (timerp timer) | |
dbbee529 RS |
266 | (let (retrigger cell) |
267 | ;; Delete from queue. Record the cons cell that was used. | |
268 | (setq cell (cancel-timer-internal timer)) | |
5e046f6d | 269 | ;; Re-schedule if requested. |
e0f0f3ef SM |
270 | (if (timer--repeat-delay timer) |
271 | (if (timer--idle-delay timer) | |
dbbee529 | 272 | (timer-activate-when-idle timer nil cell) |
e0f0f3ef | 273 | (timer-inc-time timer (timer--repeat-delay timer) 0) |
5e046f6d JB |
274 | ;; If real time has jumped forward, |
275 | ;; perhaps because Emacs was suspended for a long time, | |
276 | ;; limit how many times things get repeated. | |
277 | (if (and (numberp timer-max-repeats) | |
278 | (< 0 (timer-until timer (current-time)))) | |
279 | (let ((repeats (/ (timer-until timer (current-time)) | |
e0f0f3ef | 280 | (timer--repeat-delay timer)))) |
5e046f6d | 281 | (if (> repeats timer-max-repeats) |
e0f0f3ef SM |
282 | (timer-inc-time timer (* (timer--repeat-delay timer) |
283 | repeats))))) | |
dbbee529 | 284 | (timer-activate timer t cell) |
6160f933 | 285 | (setq retrigger t))) |
5e046f6d JB |
286 | ;; Run handler. |
287 | ;; We do this after rescheduling so that the handler function | |
288 | ;; can cancel its own timer successfully with cancel-timer. | |
289 | (condition-case nil | |
0798a8d8 SM |
290 | ;; Timer functions should not change the current buffer. |
291 | ;; If they do, all kinds of nasty surprises can happen, | |
292 | ;; and it can be hellish to track down their source. | |
293 | (save-current-buffer | |
294 | (apply (timer--function timer) (timer--args timer))) | |
6160f933 KS |
295 | (error nil)) |
296 | (if retrigger | |
2ee3d7f0 | 297 | (setf (timer--triggered timer) nil))) |
5e046f6d JB |
298 | (error "Bogus timer event")))) |
299 | ||
300 | ;; This function is incompatible with the one in levents.el. | |
301 | (defun timeout-event-p (event) | |
302 | "Non-nil if EVENT is a timeout event." | |
303 | (and (listp event) (eq (car event) 'timer-event))) | |
304 | \f | |
153ef845 | 305 | |
5cec3056 | 306 | (declare-function diary-entry-time "diary-lib" (s)) |
153ef845 | 307 | |
5e046f6d JB |
308 | (defun run-at-time (time repeat function &rest args) |
309 | "Perform an action at time TIME. | |
310 | Repeat the action every REPEAT seconds, if REPEAT is non-nil. | |
b9be25fd GM |
311 | TIME should be one of: a string giving an absolute time like |
312 | \"11:23pm\" (the acceptable formats are those recognized by | |
313 | `diary-entry-time'; note that such times are interpreted as times | |
314 | today, even if in the past); a string giving a relative time like | |
315 | \"2 hours 35 minutes\" (the acceptable formats are those | |
316 | recognized by `timer-duration'); nil meaning now; a number of | |
317 | seconds from now; a value from `encode-time'; or t (with non-nil | |
318 | REPEAT) meaning the next integral multiple of REPEAT. REPEAT may | |
319 | be an integer or floating point number. The action is to call | |
320 | FUNCTION with arguments ARGS. | |
5e046f6d JB |
321 | |
322 | This function returns a timer object which you can use in `cancel-timer'." | |
323 | (interactive "sRun at time: \nNRepeat interval: \naFunction: ") | |
324 | ||
325 | (or (null repeat) | |
326 | (and (numberp repeat) (< 0 repeat)) | |
327 | (error "Invalid repetition interval")) | |
328 | ||
329 | ;; Special case: nil means "now" and is useful when repeating. | |
330 | (if (null time) | |
331 | (setq time (current-time))) | |
332 | ||
333 | ;; Special case: t means the next integral multiple of REPEAT. | |
334 | (if (and (eq time t) repeat) | |
335 | (setq time (timer-next-integral-multiple-of-time (current-time) repeat))) | |
336 | ||
337 | ;; Handle numbers as relative times in seconds. | |
338 | (if (numberp time) | |
339 | (setq time (timer-relative-time (current-time) time))) | |
340 | ||
b9be25fd | 341 | ;; Handle relative times like "2 hours 35 minutes" |
5e046f6d JB |
342 | (if (stringp time) |
343 | (let ((secs (timer-duration time))) | |
344 | (if secs | |
345 | (setq time (timer-relative-time (current-time) secs))))) | |
346 | ||
347 | ;; Handle "11:23pm" and the like. Interpret it as meaning today | |
348 | ;; which admittedly is rather stupid if we have passed that time | |
349 | ;; already. (Though only Emacs hackers hack Emacs at that time.) | |
350 | (if (stringp time) | |
351 | (progn | |
352 | (require 'diary-lib) | |
353 | (let ((hhmm (diary-entry-time time)) | |
354 | (now (decode-time))) | |
355 | (if (>= hhmm 0) | |
356 | (setq time | |
357 | (encode-time 0 (% hhmm 100) (/ hhmm 100) (nth 3 now) | |
358 | (nth 4 now) (nth 5 now) (nth 8 now))))))) | |
359 | ||
360 | (or (consp time) | |
361 | (error "Invalid time format")) | |
362 | ||
363 | (let ((timer (timer-create))) | |
364 | (timer-set-time timer time repeat) | |
365 | (timer-set-function timer function args) | |
366 | (timer-activate timer) | |
367 | timer)) | |
368 | ||
5e046f6d JB |
369 | (defun run-with-timer (secs repeat function &rest args) |
370 | "Perform an action after a delay of SECS seconds. | |
371 | Repeat the action every REPEAT seconds, if REPEAT is non-nil. | |
372 | SECS and REPEAT may be integers or floating point numbers. | |
373 | The action is to call FUNCTION with arguments ARGS. | |
374 | ||
375 | This function returns a timer object which you can use in `cancel-timer'." | |
376 | (interactive "sRun after delay (seconds): \nNRepeat interval: \naFunction: ") | |
377 | (apply 'run-at-time secs repeat function args)) | |
378 | ||
5e046f6d JB |
379 | (defun add-timeout (secs function object &optional repeat) |
380 | "Add a timer to run SECS seconds from now, to call FUNCTION on OBJECT. | |
381 | If REPEAT is non-nil, repeat the timer every REPEAT seconds. | |
382 | This function is for compatibility; see also `run-with-timer'." | |
383 | (run-with-timer secs repeat function object)) | |
384 | ||
5e046f6d JB |
385 | (defun run-with-idle-timer (secs repeat function &rest args) |
386 | "Perform an action the next time Emacs is idle for SECS seconds. | |
387 | The action is to call FUNCTION with arguments ARGS. | |
1063efe8 CY |
388 | SECS may be an integer, a floating point number, or the internal |
389 | time format (HIGH LOW USECS) returned by, e.g., `current-idle-time'. | |
171d328e RS |
390 | If Emacs is currently idle, and has been idle for N seconds (N < SECS), |
391 | then it will call FUNCTION in SECS - N seconds from now. | |
5e046f6d JB |
392 | |
393 | If REPEAT is non-nil, do the action each time Emacs has been idle for | |
394 | exactly SECS seconds (that is, only once for each time Emacs becomes idle). | |
395 | ||
396 | This function returns a timer object which you can use in `cancel-timer'." | |
397 | (interactive | |
398 | (list (read-from-minibuffer "Run after idle (seconds): " nil nil t) | |
399 | (y-or-n-p "Repeat each time Emacs is idle? ") | |
400 | (intern (completing-read "Function: " obarray 'fboundp t)))) | |
401 | (let ((timer (timer-create))) | |
402 | (timer-set-function timer function args) | |
403 | (timer-set-idle-time timer secs repeat) | |
171d328e | 404 | (timer-activate-when-idle timer t) |
5e046f6d JB |
405 | timer)) |
406 | \f | |
89c020e8 RS |
407 | (defvar with-timeout-timers nil |
408 | "List of all timers used by currently pending `with-timeout' calls.") | |
409 | ||
5e046f6d JB |
410 | (defmacro with-timeout (list &rest body) |
411 | "Run BODY, but if it doesn't finish in SECONDS seconds, give up. | |
412 | If we give up, we run the TIMEOUT-FORMS and return the value of the last one. | |
5e046f6d | 413 | The timeout is checked whenever Emacs waits for some kind of external |
7452fadc | 414 | event (such as keyboard input, input from subprocesses, or a certain time); |
5e046f6d | 415 | if the program loops without waiting in any way, the timeout will not |
7452fadc JB |
416 | be detected. |
417 | \n(fn (SECONDS TIMEOUT-FORMS...) BODY)" | |
bad41229 | 418 | (declare (indent 1) (debug ((form body) body))) |
5e046f6d | 419 | (let ((seconds (car list)) |
bad41229 SM |
420 | (timeout-forms (cdr list)) |
421 | (timeout (make-symbol "timeout"))) | |
422 | `(let ((-with-timeout-value- | |
423 | (catch ',timeout | |
424 | (let* ((-with-timeout-timer- | |
425 | (run-with-timer ,seconds nil | |
426 | (lambda () (throw ',timeout ',timeout)))) | |
427 | (with-timeout-timers | |
428 | (cons -with-timeout-timer- with-timeout-timers))) | |
429 | (unwind-protect | |
430 | ,@body | |
431 | (cancel-timer -with-timeout-timer-)))))) | |
432 | ;; It is tempting to avoid the `if' altogether and instead run | |
433 | ;; timeout-forms in the timer, just before throwing `timeout'. | |
434 | ;; But that would mean that timeout-forms are run in the deeper | |
435 | ;; dynamic context of the timer, with inhibit-quit set etc... | |
436 | (if (eq -with-timeout-value- ',timeout) | |
437 | (progn ,@timeout-forms) | |
438 | -with-timeout-value-)))) | |
5e046f6d | 439 | |
89c020e8 RS |
440 | (defun with-timeout-suspend () |
441 | "Stop the clock for `with-timeout'. Used by debuggers. | |
442 | The idea is that the time you spend in the debugger should not | |
443 | count against these timeouts. | |
444 | ||
445 | The value is a list that the debugger can pass to `with-timeout-unsuspend' | |
446 | when it exits, to make these timers start counting again." | |
447 | (mapcar (lambda (timer) | |
448 | (cancel-timer timer) | |
e0f0f3ef | 449 | (list timer (time-subtract (timer--time timer) (current-time)))) |
89c020e8 RS |
450 | with-timeout-timers)) |
451 | ||
452 | (defun with-timeout-unsuspend (timer-spec-list) | |
453 | "Restart the clock for `with-timeout'. | |
454 | The argument should be a value previously returned by `with-timeout-suspend'." | |
455 | (dolist (elt timer-spec-list) | |
456 | (let ((timer (car elt)) | |
457 | (delay (cadr elt))) | |
64ba814f | 458 | (timer-set-time timer (time-add (current-time) delay)) |
89c020e8 RS |
459 | (timer-activate timer)))) |
460 | ||
5e046f6d JB |
461 | (defun y-or-n-p-with-timeout (prompt seconds default-value) |
462 | "Like (y-or-n-p PROMPT), with a timeout. | |
463 | If the user does not answer after SECONDS seconds, return DEFAULT-VALUE." | |
464 | (with-timeout (seconds default-value) | |
465 | (y-or-n-p prompt))) | |
466 | \f | |
2f7f4bee | 467 | (defconst timer-duration-words |
5e046f6d JB |
468 | (list (cons "microsec" 0.000001) |
469 | (cons "microsecond" 0.000001) | |
470 | (cons "millisec" 0.001) | |
471 | (cons "millisecond" 0.001) | |
472 | (cons "sec" 1) | |
473 | (cons "second" 1) | |
474 | (cons "min" 60) | |
475 | (cons "minute" 60) | |
476 | (cons "hour" (* 60 60)) | |
477 | (cons "day" (* 24 60 60)) | |
478 | (cons "week" (* 7 24 60 60)) | |
479 | (cons "fortnight" (* 14 24 60 60)) | |
480 | (cons "month" (* 30 24 60 60)) ; Approximation | |
481 | (cons "year" (* 365.25 24 60 60)) ; Approximation | |
482 | ) | |
64ba814f | 483 | "Alist mapping temporal words to durations in seconds.") |
5e046f6d JB |
484 | |
485 | (defun timer-duration (string) | |
486 | "Return number of seconds specified by STRING, or nil if parsing fails." | |
487 | (let ((secs 0) | |
488 | (start 0) | |
489 | (case-fold-search t)) | |
490 | (while (string-match | |
491 | "[ \t]*\\([0-9.]+\\)?[ \t]*\\([a-z]+[a-rt-z]\\)s?[ \t]*" | |
492 | string start) | |
493 | (let ((count (if (match-beginning 1) | |
494 | (string-to-number (match-string 1 string)) | |
495 | 1)) | |
496 | (itemsize (cdr (assoc (match-string 2 string) | |
497 | timer-duration-words)))) | |
498 | (if itemsize | |
499 | (setq start (match-end 0) | |
500 | secs (+ secs (* count itemsize))) | |
501 | (setq secs nil | |
502 | start (length string))))) | |
503 | (if (= start (length string)) | |
504 | secs | |
64ba814f | 505 | (if (string-match-p "\\`[0-9.]+\\'" string) |
5e046f6d JB |
506 | (string-to-number string))))) |
507 | \f | |
508 | (provide 'timer) | |
509 | ||
510 | ;;; timer.el ends here |