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