Mercurial > emacs
annotate lisp/timer.el @ 14685:496342156d13
Renamed from cal-chinese.el to avoid 14-character limitation.
author | Karl Heuer <kwzh@gnu.org> |
---|---|
date | Mon, 26 Feb 1996 18:45:24 +0000 |
parents | f50f87fae914 |
children | 6edb5012f490 |
rev | line source |
---|---|
14449 | 1 ;;; timer.el --- run a function with args at some time in future. |
2 | |
3 ;; Copyright (C) 1996 Free Software Foundation, Inc. | |
4 | |
5 ;; Maintainer: FSF | |
6 | |
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 | |
11 ;; the Free Software Foundation; either version 2, or (at your option) | |
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 | |
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. | |
23 | |
24 ;;; Commentary: | |
25 | |
26 ;; This package gives you the capability to run Emacs Lisp commands at | |
27 ;; specified times in the future, either as one-shots or periodically. | |
28 | |
29 ;;; Code: | |
30 | |
31 ;; Layout of a timer vector: | |
14632
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
32 ;; [triggered-p high-seconds low-seconds usecs repeat-delay |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
33 ;; function args idle-delay] |
14449 | 34 |
35 (defun timer-create () | |
36 "Create a timer object." | |
14632
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
37 (let ((timer (make-vector 8 nil))) |
14449 | 38 (aset timer 0 t) |
39 timer)) | |
40 | |
41 (defun timerp (object) | |
42 "Return t if OBJECT is a timer." | |
14632
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
43 (and (vectorp object) (= (length object) 8))) |
14449 | 44 |
45 (defun timer-set-time (timer time &optional delta) | |
46 "Set the trigger time of TIMER to TIME. | |
47 TIME must be in the internal format returned by, e.g., `current-time' | |
48 If optional third argument DELTA is a non-zero integer make the timer | |
49 fire repeatedly that meny seconds apart." | |
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))) | |
54 (aset timer 3 (if (consp (cdr time)) (nth 2 time) 0)) | |
55 (aset timer 4 (and (numberp delta) (> delta 0) delta)) | |
56 timer) | |
57 | |
58 (defun timer-relative-time (time secs &optional usecs) | |
59 "Advance TIME by SECS seconds and optionally USECS microseconds. | |
60 SECS may be a fraction." | |
61 (let ((high (car time)) | |
62 (low (if (consp (cdr time)) (nth 1 time) (cdr time))) | |
63 (micro (if (numberp (car-safe (cdr-safe (cdr time)))) | |
64 (nth 2 time) | |
65 0))) | |
66 ;; Add | |
67 (if usecs (setq micro (+ micro usecs))) | |
68 (if (floatp secs) | |
69 (setq micro (+ micro (floor (* 1000000 (- secs (floor secs))))))) | |
70 (setq low (+ low (floor secs))) | |
71 | |
72 ;; Normalize | |
73 (setq low (+ low (/ micro 1000000))) | |
74 (setq micro (mod micro 1000000)) | |
75 (setq high (+ high (/ low 65536))) | |
76 (setq low (logand low 65535)) | |
77 | |
78 (list high low (and (/= micro 0) micro)))) | |
79 | |
80 (defun timer-inc-time (timer secs &optional usecs) | |
81 "Increment the time set in TIMER by SECS seconds and USECS microseconds. | |
82 SECS may be a fraction." | |
83 (let ((time (timer-relative-time | |
84 (list (aref timer 1) (aref timer 2) (aref timer 3)) | |
85 secs | |
86 usecs))) | |
87 (aset timer 1 (nth 0 time)) | |
88 (aset timer 2 (nth 1 time)) | |
89 (aset timer 3 (or (nth 2 time) 0)))) | |
90 | |
91 (defun timer-set-time-with-usecs (timer time usecs &optional delta) | |
92 "Set the trigger time of TIMER to TIME. | |
93 TIME must be in the internal format returned by, e.g., `current-time' | |
94 If optional third argument DELTA is a non-zero integer make the timer | |
95 fire repeatedly that menu seconds apart." | |
96 (or (timerp timer) | |
97 (error "Invalid timer")) | |
98 (aset timer 1 (car time)) | |
99 (aset timer 2 (if (consp (cdr time)) (car (cdr time)) (cdr time))) | |
100 (aset timer 3 usecs) | |
101 (aset timer 4 (and (numberp delta) (> delta 0) delta)) | |
102 timer) | |
103 | |
104 (defun timer-set-function (timer function &optional args) | |
105 "Make TIMER call FUNCTION with optional ARGS when triggering." | |
106 (or (timerp timer) | |
107 (error "Invalid timer")) | |
108 (aset timer 5 function) | |
109 (aset timer 6 args) | |
110 timer) | |
111 | |
112 (defun timer-activate (timer) | |
113 "Put TIMER on the list of active timers." | |
114 (if (and (timerp timer) | |
115 (integerp (aref timer 1)) | |
116 (integerp (aref timer 2)) | |
117 (integerp (aref timer 3)) | |
118 (aref timer 5)) | |
119 (let ((timers timer-list) | |
120 last) | |
121 ;; Skip all timers to trigger before the new one. | |
122 (while (and timers | |
123 (or (> (aref timer 1) (aref (car timers) 1)) | |
124 (and (= (aref timer 1) (aref (car timers) 1)) | |
125 (> (aref timer 2) (aref (car timers) 2))) | |
126 (and (= (aref timer 1) (aref (car timers) 1)) | |
127 (= (aref timer 2) (aref (car timers) 2)) | |
128 (> (aref timer 3) (aref (car timers) 3))))) | |
129 (setq last timers | |
130 timers (cdr timers))) | |
131 ;; Insert new timer after last which possibly means in front of queue. | |
132 (if last | |
133 (setcdr last (cons timer timers)) | |
134 (setq timer-list (cons timer timers))) | |
135 (aset timer 0 nil) | |
14632
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
136 (aset timer 7 nil) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
137 nil) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
138 (error "Invalid or uninitialized timer"))) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
139 |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
140 (defun timer-activate-when-idle (timer) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
141 "Arrange to activate TIMER whenever Emacs is next idle." |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
142 (if (and (timerp timer) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
143 (integerp (aref timer 1)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
144 (integerp (aref timer 2)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
145 (integerp (aref timer 3)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
146 (aref timer 5)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
147 (let ((timers timer-idle-list) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
148 last) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
149 ;; Skip all timers to trigger before the new one. |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
150 (while (and timers |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
151 (or (> (aref timer 1) (aref (car timers) 1)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
152 (and (= (aref timer 1) (aref (car timers) 1)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
153 (> (aref timer 2) (aref (car timers) 2))) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
154 (and (= (aref timer 1) (aref (car timers) 1)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
155 (= (aref timer 2) (aref (car timers) 2)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
156 (> (aref timer 3) (aref (car timers) 3))))) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
157 (setq last timers |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
158 timers (cdr timers))) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
159 ;; Insert new timer after last which possibly means in front of queue. |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
160 (if last |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
161 (setcdr last (cons timer timers)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
162 (setq timer-idle-list (cons timer timers))) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
163 (aset timer 0 t) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
164 (aset timer 7 t) |
14449 | 165 nil) |
166 (error "Invalid or uninitialized timer"))) | |
167 | |
168 (defalias 'disable-timeout 'cancel-timer) | |
169 (defun cancel-timer (timer) | |
170 "Remove TIMER from the list of active timers." | |
171 (or (timerp timer) | |
172 (error "Invalid timer")) | |
173 (setq timer-list (delq timer timer-list)) | |
14632
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
174 (setq timer-idle-list (delq timer timer-idle-list)) |
14449 | 175 nil) |
176 | |
177 (defun cancel-function-timers (function) | |
178 "Cancel all timers scheduled by `run-at-time' which would run FUNCTION." | |
179 (interactive "aCancel timers of function: ") | |
180 (let ((tail timer-list)) | |
181 (while tail | |
182 (if (eq (aref (car tail) 5) function) | |
183 (setq timer-list (delq (car tail) timer-list))) | |
14632
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
184 (setq tail (cdr tail)))) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
185 (let ((tail timer-idle-list)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
186 (while tail |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
187 (if (eq (aref (car tail) 5) function) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
188 (setq timer-idle-list (delq (car tail) timer-idle-list))) |
14449 | 189 (setq tail (cdr tail))))) |
190 | |
191 ;; Set up the common handler for all timer events. Since the event has | |
192 ;; the timer as parameter we can still distinguish. Note that using | |
193 ;; special-event-map ensures that event timer events that arrive in the | |
194 ;; middle of a key sequence being entered are still handled correctly. | |
195 (define-key special-event-map [timer-event] 'timer-event-handler) | |
196 (defun timer-event-handler (event) | |
197 "Call the handler for the timer in the event EVENT." | |
198 (interactive "e") | |
199 (let ((timer (car-safe (cdr-safe event)))) | |
200 (if (timerp timer) | |
201 (progn | |
202 ;; Delete from queue. | |
203 (cancel-timer timer) | |
204 ;; Run handler | |
205 (apply (aref timer 5) (aref timer 6)) | |
206 ;; Re-schedule if requested. | |
207 (if (aref timer 4) | |
14632
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
208 (if (aref timer 7) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
209 (timer-activate-when-idle timer) |
14449 | 210 (timer-inc-time timer (aref timer 4) 0) |
211 (timer-activate timer)))) | |
212 (error "Bogus timer event")))) | |
213 | |
214 ;;;###autoload | |
215 (defun run-at-time (time repeat function &rest args) | |
216 "Run a function at a time, and optionally on a regular interval. | |
217 Arguments are TIME, REPEAT, FUNCTION &rest ARGS. | |
14509 | 218 TIME is a string like \"11:23pm\" or a value from `encode-time', |
219 or a number of seconds from now. | |
14449 | 220 REPEAT, an integer number of seconds, is the interval on which to repeat |
221 the call to the function. If REPEAT is nil or 0, call it just once. | |
222 | |
223 This function returns a timer object which you can use in `cancel-timer'." | |
224 (interactive "sRun at time: \nNRepeat interval: \naFunction: ") | |
225 | |
226 ;; Special case: nil means "now" and is useful when repeting. | |
227 (if (null time) | |
228 (setq time (current-time))) | |
229 | |
14508
87b0d4b7577a
(run-at-time): Handle numbers as relative times in seconds, as the original
Roland McGrath <roland@gnu.org>
parents:
14470
diff
changeset
|
230 ;; Handle numbers as relative times in seconds. |
87b0d4b7577a
(run-at-time): Handle numbers as relative times in seconds, as the original
Roland McGrath <roland@gnu.org>
parents:
14470
diff
changeset
|
231 (if (numberp time) |
87b0d4b7577a
(run-at-time): Handle numbers as relative times in seconds, as the original
Roland McGrath <roland@gnu.org>
parents:
14470
diff
changeset
|
232 (setq time (timer-relative-time (current-time) time))) |
87b0d4b7577a
(run-at-time): Handle numbers as relative times in seconds, as the original
Roland McGrath <roland@gnu.org>
parents:
14470
diff
changeset
|
233 |
14449 | 234 ;; Handle relative times like "2 hours and 35 minutes" |
235 (if (stringp time) | |
236 (let ((secs (timer-duration time))) | |
237 (if secs | |
238 (setq time (timer-relative-time (current-time) secs))))) | |
239 | |
240 ;; Handle "11:23pm" and the like. Interpret it as meaning today | |
241 ;; which admittedly is rather stupid if we have passed that time | |
242 ;; already. | |
243 (if (stringp time) | |
244 (progn | |
245 (require 'diary-lib) | |
246 (let ((hhmm (diary-entry-time time)) | |
247 (now (decode-time))) | |
248 (if (>= hhmm 0) | |
249 (setq time | |
250 (encode-time 0 (% hhmm 100) (/ hhmm 100) (nth 3 now) | |
251 (nth 4 now) (nth 5 now) (nth 8 now))))))) | |
252 | |
253 (or (consp time) | |
254 (error "Invalid time format")) | |
255 | |
256 (or (null repeat) | |
257 (natnump repeat) | |
258 (error "Invalid repetition interval")) | |
259 | |
260 (let ((timer (timer-create))) | |
261 (timer-set-time timer time repeat) | |
262 (timer-set-function timer function args) | |
263 (timer-activate timer) | |
264 timer)) | |
265 | |
266 ;;;###autoload | |
267 (defun run-with-timer (secs repeat function &rest args) | |
268 "Perform an action after a delay of SECS seconds. | |
269 Repeat the action every REPEAT seconds, if REPEAT is non-nil. | |
270 SECS and REPEAT may be integers or floating point numbers. | |
271 The action is to call FUNCTION with arguments ARGS. | |
272 | |
273 This function returns a timer object which you can use in `cancel-timer'." | |
274 (interactive "sRun after delay (seconds): \nNRepeat interval: \naFunction: ") | |
275 | |
276 (or (null repeat) | |
277 (and (numberp repeat) (>= repeat 0)) | |
278 (error "Invalid repetition interval")) | |
279 | |
280 (let ((timer (timer-create))) | |
14470
eb7a9a73cc06
(run-with-timer): Set repetition interval.
Richard M. Stallman <rms@gnu.org>
parents:
14449
diff
changeset
|
281 (timer-set-time timer (current-time) repeat) |
14449 | 282 (timer-inc-time timer secs) |
283 (timer-set-function timer function args) | |
284 (timer-activate timer) | |
285 timer)) | |
14632
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
286 |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
287 ;;;###autoload |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
288 (defun run-with-idle-timer (secs repeat function &rest args) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
289 "Perform an action the next time Emacs is idle for SECS seconds. |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
290 The action is to call FUNCTION with arguments ARGS. |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
291 If REPEAT is non-nil, do this each time Emacs is idle for SECS seconds. |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
292 SECS may be an integer or a floating point number. |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
293 |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
294 This function returns a timer object which you can use in `cancel-timer'." |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
295 (interactive "sRun after delay (seconds): \nNRepeat interval: \naFunction: ") |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
296 |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
297 (let ((timer (timer-create))) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
298 (timer-set-function timer function args) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
299 ;; Store 0 into the time fields, then add in SECS. |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
300 (aset timer 1 0) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
301 (aset timer 2 0) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
302 (aset timer 3 0) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
303 (timer-inc-time timer secs) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
304 (aset timer 4 repeat) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
305 (timer-activate-when-idle timer) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
306 timer)) |
14449 | 307 |
308 ;;;###autoload | |
309 (defun add-timeout (secs function object &optional repeat) | |
310 "Add a timer to run SECS seconds from now, to call FUNCTION on OBJECT. | |
311 If REPEAT is non-nil, repeat the timer every REPEAT seconds. | |
312 This function is for compatibility; see also `run-with-timer'." | |
313 (run-with-timer secs repeat function object)) | |
314 | |
315 (defun timeout-event-p (event) | |
316 "Non-nil if EVENT is a timeout event." | |
317 (and (listp event) | |
318 (eq (car event) 'timer-event))) | |
319 | |
320 (defun with-timeout-handler (tag) | |
321 (throw tag 'timeout)) | |
322 | |
323 ;;;###autoload (put 'with-timeout 'lisp-indent-function 1) | |
324 | |
325 ;;;###autoload | |
326 (defmacro with-timeout (list &rest body) | |
327 "Run BODY, but if it doesn't finish in SECONDS seconds, give up. | |
328 If we give up, we run the TIMEOUT-FORMS and return the value of the last one. | |
329 The call looks like | |
330 (with-timeout (SECONDS TIMEOUT-FORMS...) BODY...) | |
331 The timeout is checked whenever Emacs waits for some kind of external | |
332 event \(such as keyboard input, input from subprocesses, or a certain time); | |
333 if the program loops without waiting in any way, the timeout will not | |
334 be detected." | |
335 (let ((seconds (car list)) | |
336 (timeout-forms (cdr list))) | |
337 `(let ((with-timeout-tag (cons nil nil)) | |
338 with-timeout-value with-timeout-timer) | |
339 (if (catch with-timeout-tag | |
340 (progn | |
341 (setq with-timeout-timer | |
342 (run-with-timer ,seconds nil | |
343 'with-timeout-handler | |
344 with-timeout-tag)) | |
345 (setq with-timeout-value (progn . ,body)) | |
346 nil)) | |
347 (progn . ,timeout-forms) | |
348 (cancel-timer with-timeout-timer) | |
349 with-timeout-value)))) | |
350 | |
351 (defun y-or-n-p-with-timeout (prompt seconds default-value) | |
352 "Like (y-or-n-p PROMPT), with a timeout. | |
353 If the user does not answer after SECONDS seconds, return DEFAULT-VALUE." | |
354 (with-timeout (seconds default-value) | |
355 (y-or-n-p prompt))) | |
356 | |
357 (defvar timer-duration-words | |
358 (list (cons "microsec" 0.000001) | |
359 (cons "microsecond" 0.000001) | |
360 (cons "millisec" 0.001) | |
361 (cons "millisecond" 0.001) | |
362 (cons "sec" 1) | |
363 (cons "second" 1) | |
364 (cons "min" 60) | |
365 (cons "minute" 60) | |
366 (cons "hour" (* 60 60)) | |
367 (cons "day" (* 24 60 60)) | |
368 (cons "week" (* 7 24 60 60)) | |
369 (cons "fortnight" (* 14 24 60 60)) | |
370 (cons "month" (* 30 24 60 60)) ; Approximation | |
371 (cons "year" (* 365.25 24 60 60)) ; Approximation | |
372 ) | |
373 "Alist mapping temporal words to durations in seconds") | |
374 | |
375 (defun timer-duration (string) | |
376 "Return number of seconds specified by STRING, or nil if parsing fails." | |
377 (let ((secs 0) | |
378 (start 0) | |
379 (case-fold-search t)) | |
380 (while (string-match | |
381 "[ \t]*\\([0-9.]+\\)?[ \t]*\\([a-z]+[a-rt-z]\\)s?[ \t]*" | |
382 string start) | |
383 (let ((count (if (match-beginning 1) | |
384 (string-to-number (match-string 1 string)) | |
385 1)) | |
386 (itemsize (cdr (assoc (match-string 2 string) | |
387 timer-duration-words)))) | |
388 (if itemsize | |
389 (setq start (match-end 0) | |
390 secs (+ secs (* count itemsize))) | |
391 (setq secs nil | |
392 start (length string))))) | |
393 secs)) | |
394 | |
395 (provide 'timer) | |
396 | |
397 ;;; timer.el ends here |