Mercurial > emacs
annotate lisp/timer.el @ 14596:afb84c1d7750
(decipher-mode, decipher-set-map, decipher-insert,
decipher-make-checkpoint, decipher-resync): Added special support
for font-lock.
(decipher-use-font-lock, decipher-font-lock-keywords): New vars.
(decipher-toggle-font-lock, decipher-turn-on-font-lock): New funcs.
author | Karl Heuer <kwzh@gnu.org> |
---|---|
date | Mon, 19 Feb 1996 07:52:24 +0000 |
parents | c5631aa281ac |
children | f50f87fae914 |
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. | |
14509 | 181 TIME is a string like \"11:23pm\" or a value from `encode-time', |
182 or a number of seconds from now. | |
14449 | 183 REPEAT, an integer number of seconds, is the interval on which to repeat |
184 the call to the function. If REPEAT is nil or 0, call it just once. | |
185 | |
186 This function returns a timer object which you can use in `cancel-timer'." | |
187 (interactive "sRun at time: \nNRepeat interval: \naFunction: ") | |
188 | |
189 ;; Special case: nil means "now" and is useful when repeting. | |
190 (if (null time) | |
191 (setq time (current-time))) | |
192 | |
14508
87b0d4b7577a
(run-at-time): Handle numbers as relative times in seconds, as the original
Roland McGrath <roland@gnu.org>
parents:
14470
diff
changeset
|
193 ;; 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
|
194 (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
|
195 (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
|
196 |
14449 | 197 ;; Handle relative times like "2 hours and 35 minutes" |
198 (if (stringp time) | |
199 (let ((secs (timer-duration time))) | |
200 (if secs | |
201 (setq time (timer-relative-time (current-time) secs))))) | |
202 | |
203 ;; Handle "11:23pm" and the like. Interpret it as meaning today | |
204 ;; which admittedly is rather stupid if we have passed that time | |
205 ;; already. | |
206 (if (stringp time) | |
207 (progn | |
208 (require 'diary-lib) | |
209 (let ((hhmm (diary-entry-time time)) | |
210 (now (decode-time))) | |
211 (if (>= hhmm 0) | |
212 (setq time | |
213 (encode-time 0 (% hhmm 100) (/ hhmm 100) (nth 3 now) | |
214 (nth 4 now) (nth 5 now) (nth 8 now))))))) | |
215 | |
216 (or (consp time) | |
217 (error "Invalid time format")) | |
218 | |
219 (or (null repeat) | |
220 (natnump repeat) | |
221 (error "Invalid repetition interval")) | |
222 | |
223 (let ((timer (timer-create))) | |
224 (timer-set-time timer time repeat) | |
225 (timer-set-function timer function args) | |
226 (timer-activate timer) | |
227 timer)) | |
228 | |
229 ;;;###autoload | |
230 (defun run-with-timer (secs repeat function &rest args) | |
231 "Perform an action after a delay of SECS seconds. | |
232 Repeat the action every REPEAT seconds, if REPEAT is non-nil. | |
233 SECS and REPEAT may be integers or floating point numbers. | |
234 The action is to call FUNCTION with arguments ARGS. | |
235 | |
236 This function returns a timer object which you can use in `cancel-timer'." | |
237 (interactive "sRun after delay (seconds): \nNRepeat interval: \naFunction: ") | |
238 | |
239 (or (null repeat) | |
240 (and (numberp repeat) (>= repeat 0)) | |
241 (error "Invalid repetition interval")) | |
242 | |
243 (let ((timer (timer-create))) | |
14470
eb7a9a73cc06
(run-with-timer): Set repetition interval.
Richard M. Stallman <rms@gnu.org>
parents:
14449
diff
changeset
|
244 (timer-set-time timer (current-time) repeat) |
14449 | 245 (timer-inc-time timer secs) |
246 (timer-set-function timer function args) | |
247 (timer-activate timer) | |
248 timer)) | |
249 | |
250 ;;;###autoload | |
251 (defun add-timeout (secs function object &optional repeat) | |
252 "Add a timer to run SECS seconds from now, to call FUNCTION on OBJECT. | |
253 If REPEAT is non-nil, repeat the timer every REPEAT seconds. | |
254 This function is for compatibility; see also `run-with-timer'." | |
255 (run-with-timer secs repeat function object)) | |
256 | |
257 (defun timeout-event-p (event) | |
258 "Non-nil if EVENT is a timeout event." | |
259 (and (listp event) | |
260 (eq (car event) 'timer-event))) | |
261 | |
262 (defun with-timeout-handler (tag) | |
263 (throw tag 'timeout)) | |
264 | |
265 ;;;###autoload (put 'with-timeout 'lisp-indent-function 1) | |
266 | |
267 ;;;###autoload | |
268 (defmacro with-timeout (list &rest body) | |
269 "Run BODY, but if it doesn't finish in SECONDS seconds, give up. | |
270 If we give up, we run the TIMEOUT-FORMS and return the value of the last one. | |
271 The call looks like | |
272 (with-timeout (SECONDS TIMEOUT-FORMS...) BODY...) | |
273 The timeout is checked whenever Emacs waits for some kind of external | |
274 event \(such as keyboard input, input from subprocesses, or a certain time); | |
275 if the program loops without waiting in any way, the timeout will not | |
276 be detected." | |
277 (let ((seconds (car list)) | |
278 (timeout-forms (cdr list))) | |
279 `(let ((with-timeout-tag (cons nil nil)) | |
280 with-timeout-value with-timeout-timer) | |
281 (if (catch with-timeout-tag | |
282 (progn | |
283 (setq with-timeout-timer | |
284 (run-with-timer ,seconds nil | |
285 'with-timeout-handler | |
286 with-timeout-tag)) | |
287 (setq with-timeout-value (progn . ,body)) | |
288 nil)) | |
289 (progn . ,timeout-forms) | |
290 (cancel-timer with-timeout-timer) | |
291 with-timeout-value)))) | |
292 | |
293 (defun y-or-n-p-with-timeout (prompt seconds default-value) | |
294 "Like (y-or-n-p PROMPT), with a timeout. | |
295 If the user does not answer after SECONDS seconds, return DEFAULT-VALUE." | |
296 (with-timeout (seconds default-value) | |
297 (y-or-n-p prompt))) | |
298 | |
299 (defvar timer-duration-words | |
300 (list (cons "microsec" 0.000001) | |
301 (cons "microsecond" 0.000001) | |
302 (cons "millisec" 0.001) | |
303 (cons "millisecond" 0.001) | |
304 (cons "sec" 1) | |
305 (cons "second" 1) | |
306 (cons "min" 60) | |
307 (cons "minute" 60) | |
308 (cons "hour" (* 60 60)) | |
309 (cons "day" (* 24 60 60)) | |
310 (cons "week" (* 7 24 60 60)) | |
311 (cons "fortnight" (* 14 24 60 60)) | |
312 (cons "month" (* 30 24 60 60)) ; Approximation | |
313 (cons "year" (* 365.25 24 60 60)) ; Approximation | |
314 ) | |
315 "Alist mapping temporal words to durations in seconds") | |
316 | |
317 (defun timer-duration (string) | |
318 "Return number of seconds specified by STRING, or nil if parsing fails." | |
319 (let ((secs 0) | |
320 (start 0) | |
321 (case-fold-search t)) | |
322 (while (string-match | |
323 "[ \t]*\\([0-9.]+\\)?[ \t]*\\([a-z]+[a-rt-z]\\)s?[ \t]*" | |
324 string start) | |
325 (let ((count (if (match-beginning 1) | |
326 (string-to-number (match-string 1 string)) | |
327 1)) | |
328 (itemsize (cdr (assoc (match-string 2 string) | |
329 timer-duration-words)))) | |
330 (if itemsize | |
331 (setq start (match-end 0) | |
332 secs (+ secs (* count itemsize))) | |
333 (setq secs nil | |
334 start (length string))))) | |
335 secs)) | |
336 | |
337 (provide 'timer) | |
338 | |
339 ;;; timer.el ends here |