Mercurial > emacs
annotate lisp/emacs-lisp/timer.el @ 93321:c3fe9ef79b56
(decode_coding_object): Fix last change.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Thu, 27 Mar 2008 20:04:37 +0000 |
parents | 73a01bf5cb80 |
children | f969e238af3c |
rev | line source |
---|---|
51349 | 1 ;;; timer.el --- run a function with args at some time in future |
2 | |
74466 | 3 ;; Copyright (C) 1996, 2001, 2002, 2003, 2004, 2005, |
79704 | 4 ;; 2006, 2007, 2008 Free Software Foundation, Inc. |
51349 | 5 |
6 ;; Maintainer: FSF | |
7 | |
8 ;; This file is part of GNU Emacs. | |
9 | |
10 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
11 ;; it under the terms of the GNU General Public License as published by | |
78217
935157c0b596
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
76852
diff
changeset
|
12 ;; the Free Software Foundation; either version 3, or (at your option) |
51349 | 13 ;; any later version. |
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 | |
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
64085 | 22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
23 ;; Boston, MA 02110-1301, USA. | |
51349 | 24 |
25 ;;; Commentary: | |
26 | |
27 ;; This package gives you the capability to run Emacs Lisp commands at | |
28 ;; specified times in the future, either as one-shots or periodically. | |
29 | |
30 ;;; Code: | |
31 | |
32 ;; Layout of a timer vector: | |
33 ;; [triggered-p high-seconds low-seconds usecs repeat-delay | |
34 ;; function args idle-delay] | |
72720
a3c85e1915ad
(timer-create, timer-activate): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
72508
diff
changeset
|
35 ;; triggered-p is nil if the timer is active (waiting to be triggered), |
a3c85e1915ad
(timer-create, timer-activate): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
72508
diff
changeset
|
36 ;; t if it is inactive ("already triggered", in theory) |
51349 | 37 |
38 (defun timer-create () | |
72720
a3c85e1915ad
(timer-create, timer-activate): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
72508
diff
changeset
|
39 "Create a timer object which can be passed to `timer-activate'." |
51349 | 40 (let ((timer (make-vector 8 nil))) |
41 (aset timer 0 t) | |
42 timer)) | |
43 | |
44 (defun timerp (object) | |
45 "Return t if OBJECT is a timer." | |
46 (and (vectorp object) (= (length object) 8))) | |
47 | |
48 (defun timer-set-time (timer time &optional delta) | |
49 "Set the trigger time of TIMER to TIME. | |
50 TIME must be in the internal format returned by, e.g., `current-time'. | |
51 If optional third argument DELTA is a positive number, make the timer | |
52 fire repeatedly that many seconds apart." | |
53 (or (timerp timer) | |
54 (error "Invalid timer")) | |
55 (aset timer 1 (car time)) | |
56 (aset timer 2 (if (consp (cdr time)) (car (cdr time)) (cdr time))) | |
57 (aset timer 3 (or (and (consp (cdr time)) (consp (cdr (cdr time))) | |
58 (nth 2 time)) | |
59 0)) | |
60 (aset timer 4 (and (numberp delta) (> delta 0) delta)) | |
61 timer) | |
62 | |
63 (defun timer-set-idle-time (timer secs &optional repeat) | |
64 "Set the trigger idle time of TIMER to SECS. | |
72508
5d642f9eff22
* emacs-lisp/timer.el (timer-set-idle-time, run-with-idle-timer):
Chong Yidong <cyd@stupidchicken.com>
parents:
72450
diff
changeset
|
65 SECS may be an integer, floating point number, or the internal |
5d642f9eff22
* emacs-lisp/timer.el (timer-set-idle-time, run-with-idle-timer):
Chong Yidong <cyd@stupidchicken.com>
parents:
72450
diff
changeset
|
66 time format (HIGH LOW USECS) returned by, e.g., `current-idle-time'. |
51349 | 67 If optional third argument REPEAT is non-nil, make the timer |
68 fire each time Emacs is idle for that many seconds." | |
69 (or (timerp timer) | |
70 (error "Invalid timer")) | |
72508
5d642f9eff22
* emacs-lisp/timer.el (timer-set-idle-time, run-with-idle-timer):
Chong Yidong <cyd@stupidchicken.com>
parents:
72450
diff
changeset
|
71 (if (consp secs) |
5d642f9eff22
* emacs-lisp/timer.el (timer-set-idle-time, run-with-idle-timer):
Chong Yidong <cyd@stupidchicken.com>
parents:
72450
diff
changeset
|
72 (progn (aset timer 1 (car secs)) |
5d642f9eff22
* emacs-lisp/timer.el (timer-set-idle-time, run-with-idle-timer):
Chong Yidong <cyd@stupidchicken.com>
parents:
72450
diff
changeset
|
73 (aset timer 2 (if (consp (cdr secs)) (car (cdr secs)) (cdr secs))) |
5d642f9eff22
* emacs-lisp/timer.el (timer-set-idle-time, run-with-idle-timer):
Chong Yidong <cyd@stupidchicken.com>
parents:
72450
diff
changeset
|
74 (aset timer 3 (or (and (consp (cdr secs)) (consp (cdr (cdr secs))) |
5d642f9eff22
* emacs-lisp/timer.el (timer-set-idle-time, run-with-idle-timer):
Chong Yidong <cyd@stupidchicken.com>
parents:
72450
diff
changeset
|
75 (nth 2 secs)) |
5d642f9eff22
* emacs-lisp/timer.el (timer-set-idle-time, run-with-idle-timer):
Chong Yidong <cyd@stupidchicken.com>
parents:
72450
diff
changeset
|
76 0))) |
5d642f9eff22
* emacs-lisp/timer.el (timer-set-idle-time, run-with-idle-timer):
Chong Yidong <cyd@stupidchicken.com>
parents:
72450
diff
changeset
|
77 (aset timer 1 0) |
5d642f9eff22
* emacs-lisp/timer.el (timer-set-idle-time, run-with-idle-timer):
Chong Yidong <cyd@stupidchicken.com>
parents:
72450
diff
changeset
|
78 (aset timer 2 0) |
5d642f9eff22
* emacs-lisp/timer.el (timer-set-idle-time, run-with-idle-timer):
Chong Yidong <cyd@stupidchicken.com>
parents:
72450
diff
changeset
|
79 (aset timer 3 0) |
5d642f9eff22
* emacs-lisp/timer.el (timer-set-idle-time, run-with-idle-timer):
Chong Yidong <cyd@stupidchicken.com>
parents:
72450
diff
changeset
|
80 (timer-inc-time timer secs)) |
51349 | 81 (aset timer 4 repeat) |
82 timer) | |
83 | |
84 (defun timer-next-integral-multiple-of-time (time secs) | |
85 "Yield the next value after TIME that is an integral multiple of SECS. | |
86 More precisely, the next value, after TIME, that is an integral multiple | |
87 of SECS seconds since the epoch. SECS may be a fraction." | |
88 (let ((time-base (ash 1 16))) | |
89 (if (fboundp 'atan) | |
90 ;; Use floating point, taking care to not lose precision. | |
91 (let* ((float-time-base (float time-base)) | |
92 (million 1000000.0) | |
93 (time-usec (+ (* million | |
94 (+ (* float-time-base (nth 0 time)) | |
95 (nth 1 time))) | |
96 (nth 2 time))) | |
97 (secs-usec (* million secs)) | |
98 (mod-usec (mod time-usec secs-usec)) | |
99 (next-usec (+ (- time-usec mod-usec) secs-usec)) | |
100 (time-base-million (* float-time-base million))) | |
101 (list (floor next-usec time-base-million) | |
102 (floor (mod next-usec time-base-million) million) | |
103 (floor (mod next-usec million)))) | |
104 ;; Floating point is not supported. | |
105 ;; Use integer arithmetic, avoiding overflow if possible. | |
106 (let* ((mod-sec (mod (+ (* (mod time-base secs) | |
107 (mod (nth 0 time) secs)) | |
108 (nth 1 time)) | |
109 secs)) | |
110 (next-1-sec (+ (- (nth 1 time) mod-sec) secs))) | |
111 (list (+ (nth 0 time) (floor next-1-sec time-base)) | |
112 (mod next-1-sec time-base) | |
113 0))))) | |
114 | |
115 (defun timer-relative-time (time secs &optional usecs) | |
116 "Advance TIME by SECS seconds and optionally USECS microseconds. | |
72508
5d642f9eff22
* emacs-lisp/timer.el (timer-set-idle-time, run-with-idle-timer):
Chong Yidong <cyd@stupidchicken.com>
parents:
72450
diff
changeset
|
117 SECS may be either an integer or a floating point number." |
51349 | 118 (let ((high (car time)) |
119 (low (if (consp (cdr time)) (nth 1 time) (cdr time))) | |
120 (micro (if (numberp (car-safe (cdr-safe (cdr time)))) | |
121 (nth 2 time) | |
122 0))) | |
123 ;; Add | |
124 (if usecs (setq micro (+ micro usecs))) | |
125 (if (floatp secs) | |
126 (setq micro (+ micro (floor (* 1000000 (- secs (floor secs))))))) | |
127 (setq low (+ low (floor secs))) | |
128 | |
129 ;; Normalize | |
130 ;; `/' rounds towards zero while `mod' returns a positive number, | |
131 ;; so we can't rely on (= a (+ (* 100 (/ a 100)) (mod a 100))). | |
132 (setq low (+ low (/ micro 1000000) (if (< micro 0) -1 0))) | |
133 (setq micro (mod micro 1000000)) | |
134 (setq high (+ high (/ low 65536) (if (< low 0) -1 0))) | |
135 (setq low (logand low 65535)) | |
136 | |
137 (list high low (and (/= micro 0) micro)))) | |
138 | |
139 (defun timer-inc-time (timer secs &optional usecs) | |
140 "Increment the time set in TIMER by SECS seconds and USECS microseconds. | |
141 SECS may be a fraction. If USECS is omitted, that means it is zero." | |
142 (let ((time (timer-relative-time | |
143 (list (aref timer 1) (aref timer 2) (aref timer 3)) | |
144 secs | |
145 usecs))) | |
146 (aset timer 1 (nth 0 time)) | |
147 (aset timer 2 (nth 1 time)) | |
148 (aset timer 3 (or (nth 2 time) 0)))) | |
149 | |
150 (defun timer-set-time-with-usecs (timer time usecs &optional delta) | |
151 "Set the trigger time of TIMER to TIME plus USECS. | |
152 TIME must be in the internal format returned by, e.g., `current-time'. | |
153 The microsecond count from TIME is ignored, and USECS is used instead. | |
154 If optional fourth argument DELTA is a positive number, make the timer | |
155 fire repeatedly that many seconds apart." | |
156 (or (timerp timer) | |
157 (error "Invalid timer")) | |
158 (aset timer 1 (nth 0 time)) | |
159 (aset timer 2 (nth 1 time)) | |
160 (aset timer 3 usecs) | |
161 (aset timer 4 (and (numberp delta) (> delta 0) delta)) | |
162 timer) | |
163 (make-obsolete 'timer-set-time-with-usecs | |
164 "use `timer-set-time' and `timer-inc-time' instead." | |
59996
aac0a33f5772
Change release version from 21.4 to 22.1 throughout.
Kim F. Storm <storm@cua.dk>
parents:
55625
diff
changeset
|
165 "22.1") |
51349 | 166 |
167 (defun timer-set-function (timer function &optional args) | |
168 "Make TIMER call FUNCTION with optional ARGS when triggering." | |
169 (or (timerp timer) | |
170 (error "Invalid timer")) | |
171 (aset timer 5 function) | |
172 (aset timer 6 args) | |
173 timer) | |
174 | |
66535
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
175 (defun timer-activate (timer &optional triggered-p reuse-cell) |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
176 "Put TIMER on the list of active timers. |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
177 |
72720
a3c85e1915ad
(timer-create, timer-activate): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
72508
diff
changeset
|
178 If TRIGGERED-P is t, that means to make the timer inactive |
a3c85e1915ad
(timer-create, timer-activate): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
72508
diff
changeset
|
179 \(put it on the list, but mark it as already triggered). |
a3c85e1915ad
(timer-create, timer-activate): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
72508
diff
changeset
|
180 To remove from the list, use `cancel-timer'. |
a3c85e1915ad
(timer-create, timer-activate): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
72508
diff
changeset
|
181 |
66535
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
182 REUSE-CELL, if non-nil, is a cons cell to reuse instead |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
183 of allocating a new one." |
51349 | 184 (if (and (timerp timer) |
185 (integerp (aref timer 1)) | |
186 (integerp (aref timer 2)) | |
187 (integerp (aref timer 3)) | |
188 (aref timer 5)) | |
189 (let ((timers timer-list) | |
190 last) | |
191 ;; Skip all timers to trigger before the new one. | |
192 (while (and timers | |
193 (or (> (aref timer 1) (aref (car timers) 1)) | |
194 (and (= (aref timer 1) (aref (car timers) 1)) | |
195 (> (aref timer 2) (aref (car timers) 2))) | |
196 (and (= (aref timer 1) (aref (car timers) 1)) | |
197 (= (aref timer 2) (aref (car timers) 2)) | |
198 (> (aref timer 3) (aref (car timers) 3))))) | |
199 (setq last timers | |
200 timers (cdr timers))) | |
66535
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
201 (if reuse-cell |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
202 (progn |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
203 (setcar reuse-cell timer) |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
204 (setcdr reuse-cell timers)) |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
205 (setq reuse-cell (cons timer timers))) |
51349 | 206 ;; Insert new timer after last which possibly means in front of queue. |
207 (if last | |
66535
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
208 (setcdr last reuse-cell) |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
209 (setq timer-list reuse-cell)) |
55599
055b9a2c6093
(timer-activate): Add optional arg triggered-p.
Kim F. Storm <storm@cua.dk>
parents:
52401
diff
changeset
|
210 (aset timer 0 triggered-p) |
51349 | 211 (aset timer 7 nil) |
212 nil) | |
213 (error "Invalid or uninitialized timer"))) | |
214 | |
66535
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
215 (defun timer-activate-when-idle (timer &optional dont-wait reuse-cell) |
51349 | 216 "Arrange to activate TIMER whenever Emacs is next idle. |
217 If optional argument DONT-WAIT is non-nil, then enable the | |
218 timer to activate immediately, or at the right time, if Emacs | |
66535
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
219 is already idle. |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
220 |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
221 REUSE-CELL, if non-nil, is a cons cell to reuse instead |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
222 of allocating a new one." |
51349 | 223 (if (and (timerp timer) |
224 (integerp (aref timer 1)) | |
225 (integerp (aref timer 2)) | |
226 (integerp (aref timer 3)) | |
227 (aref timer 5)) | |
228 (let ((timers timer-idle-list) | |
229 last) | |
230 ;; Skip all timers to trigger before the new one. | |
231 (while (and timers | |
232 (or (> (aref timer 1) (aref (car timers) 1)) | |
233 (and (= (aref timer 1) (aref (car timers) 1)) | |
234 (> (aref timer 2) (aref (car timers) 2))) | |
235 (and (= (aref timer 1) (aref (car timers) 1)) | |
236 (= (aref timer 2) (aref (car timers) 2)) | |
237 (> (aref timer 3) (aref (car timers) 3))))) | |
238 (setq last timers | |
239 timers (cdr timers))) | |
66535
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
240 (if reuse-cell |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
241 (progn |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
242 (setcar reuse-cell timer) |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
243 (setcdr reuse-cell timers)) |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
244 (setq reuse-cell (cons timer timers))) |
51349 | 245 ;; Insert new timer after last which possibly means in front of queue. |
246 (if last | |
66535
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
247 (setcdr last reuse-cell) |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
248 (setq timer-idle-list reuse-cell)) |
51349 | 249 (aset timer 0 (not dont-wait)) |
250 (aset timer 7 t) | |
251 nil) | |
252 (error "Invalid or uninitialized timer"))) | |
253 | |
254 (defalias 'disable-timeout 'cancel-timer) | |
92948
73a01bf5cb80
* faces.el (xw-defined-colors):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
87649
diff
changeset
|
255 |
51349 | 256 (defun cancel-timer (timer) |
257 "Remove TIMER from the list of active timers." | |
258 (or (timerp timer) | |
259 (error "Invalid timer")) | |
260 (setq timer-list (delq timer timer-list)) | |
261 (setq timer-idle-list (delq timer timer-idle-list)) | |
262 nil) | |
263 | |
66535
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
264 (defun cancel-timer-internal (timer) |
72720
a3c85e1915ad
(timer-create, timer-activate): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
72508
diff
changeset
|
265 "Remove TIMER from the list of active timers or idle timers. |
a3c85e1915ad
(timer-create, timer-activate): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
72508
diff
changeset
|
266 Only to be used in this file. It returns the cons cell |
a3c85e1915ad
(timer-create, timer-activate): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
72508
diff
changeset
|
267 that was removed from the timer list." |
66535
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
268 (let ((cell1 (memq timer timer-list)) |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
269 (cell2 (memq timer timer-idle-list))) |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
270 (if cell1 |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
271 (setq timer-list (delq timer timer-list))) |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
272 (if cell2 |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
273 (setq timer-idle-list (delq timer timer-idle-list))) |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
274 (or cell1 cell2))) |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
275 |
51349 | 276 (defun cancel-function-timers (function) |
72720
a3c85e1915ad
(timer-create, timer-activate): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
72508
diff
changeset
|
277 "Cancel all timers which would run FUNCTION. |
a3c85e1915ad
(timer-create, timer-activate): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
72508
diff
changeset
|
278 This affects ordinary timers such as are scheduled by `run-at-time', |
a3c85e1915ad
(timer-create, timer-activate): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
72508
diff
changeset
|
279 and idle timers such as are scheduled by `run-with-idle-timer'." |
51349 | 280 (interactive "aCancel timers of function: ") |
281 (let ((tail timer-list)) | |
282 (while tail | |
283 (if (eq (aref (car tail) 5) function) | |
284 (setq timer-list (delq (car tail) timer-list))) | |
285 (setq tail (cdr tail)))) | |
286 (let ((tail timer-idle-list)) | |
287 (while tail | |
288 (if (eq (aref (car tail) 5) function) | |
289 (setq timer-idle-list (delq (car tail) timer-idle-list))) | |
290 (setq tail (cdr tail))))) | |
291 | |
292 ;; Record the last few events, for debugging. | |
72720
a3c85e1915ad
(timer-create, timer-activate): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
72508
diff
changeset
|
293 (defvar timer-event-last nil |
a3c85e1915ad
(timer-create, timer-activate): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
72508
diff
changeset
|
294 "Last timer that was run.") |
a3c85e1915ad
(timer-create, timer-activate): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
72508
diff
changeset
|
295 (defvar timer-event-last-1 nil |
a3c85e1915ad
(timer-create, timer-activate): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
72508
diff
changeset
|
296 "Next-to-last timer that was run.") |
a3c85e1915ad
(timer-create, timer-activate): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
72508
diff
changeset
|
297 (defvar timer-event-last-2 nil |
a3c85e1915ad
(timer-create, timer-activate): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
72508
diff
changeset
|
298 "Third-to-last timer that was run.") |
51349 | 299 |
300 (defvar timer-max-repeats 10 | |
73103
76400a9b9e59
(timer-max-repeats): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
72720
diff
changeset
|
301 "*Maximum number of times to repeat a timer, if many repeats are delayed. |
76400a9b9e59
(timer-max-repeats): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
72720
diff
changeset
|
302 Timer invocations can be delayed because Emacs is suspended or busy, |
76400a9b9e59
(timer-max-repeats): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
72720
diff
changeset
|
303 or because the system's time changes. If such an occurrence makes it |
76400a9b9e59
(timer-max-repeats): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
72720
diff
changeset
|
304 appear that many invocations are overdue, this variable controls |
76400a9b9e59
(timer-max-repeats): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
72720
diff
changeset
|
305 how many will really happen.") |
51349 | 306 |
307 (defun timer-until (timer time) | |
308 "Calculate number of seconds from when TIMER will run, until TIME. | |
309 TIMER is a timer, and stands for the time when its next repeat is scheduled. | |
310 TIME is a time-list." | |
311 (let ((high (- (car time) (aref timer 1))) | |
312 (low (- (nth 1 time) (aref timer 2)))) | |
313 (+ low (* high 65536)))) | |
314 | |
315 (defun timer-event-handler (timer) | |
316 "Call the handler for the timer TIMER. | |
317 This function is called, by name, directly by the C code." | |
318 (setq timer-event-last-2 timer-event-last-1) | |
319 (setq timer-event-last-1 timer-event-last) | |
320 (setq timer-event-last timer) | |
321 (let ((inhibit-quit t)) | |
322 (if (timerp timer) | |
66535
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
323 (let (retrigger cell) |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
324 ;; Delete from queue. Record the cons cell that was used. |
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
325 (setq cell (cancel-timer-internal timer)) |
51349 | 326 ;; Re-schedule if requested. |
327 (if (aref timer 4) | |
328 (if (aref timer 7) | |
66535
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
329 (timer-activate-when-idle timer nil cell) |
51349 | 330 (timer-inc-time timer (aref timer 4) 0) |
331 ;; If real time has jumped forward, | |
332 ;; perhaps because Emacs was suspended for a long time, | |
333 ;; limit how many times things get repeated. | |
334 (if (and (numberp timer-max-repeats) | |
335 (< 0 (timer-until timer (current-time)))) | |
336 (let ((repeats (/ (timer-until timer (current-time)) | |
337 (aref timer 4)))) | |
338 (if (> repeats timer-max-repeats) | |
339 (timer-inc-time timer (* (aref timer 4) repeats))))) | |
66535
9bc3dae397fe
(timer-activate, timer-activate-when-idle): New arg REUSE-CELL.
Richard M. Stallman <rms@gnu.org>
parents:
64751
diff
changeset
|
340 (timer-activate timer t cell) |
55599
055b9a2c6093
(timer-activate): Add optional arg triggered-p.
Kim F. Storm <storm@cua.dk>
parents:
52401
diff
changeset
|
341 (setq retrigger t))) |
51349 | 342 ;; Run handler. |
343 ;; We do this after rescheduling so that the handler function | |
344 ;; can cancel its own timer successfully with cancel-timer. | |
345 (condition-case nil | |
346 (apply (aref timer 5) (aref timer 6)) | |
55599
055b9a2c6093
(timer-activate): Add optional arg triggered-p.
Kim F. Storm <storm@cua.dk>
parents:
52401
diff
changeset
|
347 (error nil)) |
055b9a2c6093
(timer-activate): Add optional arg triggered-p.
Kim F. Storm <storm@cua.dk>
parents:
52401
diff
changeset
|
348 (if retrigger |
55625
d1f4ffb8ca41
(timer-event-handler): Fix last change.
Kim F. Storm <storm@cua.dk>
parents:
55599
diff
changeset
|
349 (aset timer 0 nil))) |
51349 | 350 (error "Bogus timer event")))) |
351 | |
352 ;; This function is incompatible with the one in levents.el. | |
353 (defun timeout-event-p (event) | |
354 "Non-nil if EVENT is a timeout event." | |
355 (and (listp event) (eq (car event) 'timer-event))) | |
356 | |
86243
4d615a83cee2
* progmodes/idlw-help.el: Require browse-url unconditionally, it
Dan Nicolaescu <dann@ics.uci.edu>
parents:
78217
diff
changeset
|
357 |
87115
7f228bf43584
Remove directory part from filenames in function declarations.
Glenn Morris <rgm@gnu.org>
parents:
86243
diff
changeset
|
358 (declare-function diary-entry-time "diary-lib" (s)) |
86243
4d615a83cee2
* progmodes/idlw-help.el: Require browse-url unconditionally, it
Dan Nicolaescu <dann@ics.uci.edu>
parents:
78217
diff
changeset
|
359 |
51349 | 360 (defun run-at-time (time repeat function &rest args) |
361 "Perform an action at time TIME. | |
362 Repeat the action every REPEAT seconds, if REPEAT is non-nil. | |
76852 | 363 TIME should be one of: a string giving an absolute time like |
364 \"11:23pm\" (the acceptable formats are those recognized by | |
365 `diary-entry-time'; note that such times are interpreted as times | |
366 today, even if in the past); a string giving a relative time like | |
367 \"2 hours 35 minutes\" (the acceptable formats are those | |
368 recognized by `timer-duration'); nil meaning now; a number of | |
369 seconds from now; a value from `encode-time'; or t (with non-nil | |
370 REPEAT) meaning the next integral multiple of REPEAT. REPEAT may | |
371 be an integer or floating point number. The action is to call | |
372 FUNCTION with arguments ARGS. | |
51349 | 373 |
374 This function returns a timer object which you can use in `cancel-timer'." | |
375 (interactive "sRun at time: \nNRepeat interval: \naFunction: ") | |
376 | |
377 (or (null repeat) | |
378 (and (numberp repeat) (< 0 repeat)) | |
379 (error "Invalid repetition interval")) | |
380 | |
381 ;; Special case: nil means "now" and is useful when repeating. | |
382 (if (null time) | |
383 (setq time (current-time))) | |
384 | |
385 ;; Special case: t means the next integral multiple of REPEAT. | |
386 (if (and (eq time t) repeat) | |
387 (setq time (timer-next-integral-multiple-of-time (current-time) repeat))) | |
388 | |
389 ;; Handle numbers as relative times in seconds. | |
390 (if (numberp time) | |
391 (setq time (timer-relative-time (current-time) time))) | |
392 | |
76852 | 393 ;; Handle relative times like "2 hours 35 minutes" |
51349 | 394 (if (stringp time) |
395 (let ((secs (timer-duration time))) | |
396 (if secs | |
397 (setq time (timer-relative-time (current-time) secs))))) | |
398 | |
399 ;; Handle "11:23pm" and the like. Interpret it as meaning today | |
400 ;; which admittedly is rather stupid if we have passed that time | |
401 ;; already. (Though only Emacs hackers hack Emacs at that time.) | |
402 (if (stringp time) | |
403 (progn | |
404 (require 'diary-lib) | |
405 (let ((hhmm (diary-entry-time time)) | |
406 (now (decode-time))) | |
407 (if (>= hhmm 0) | |
408 (setq time | |
409 (encode-time 0 (% hhmm 100) (/ hhmm 100) (nth 3 now) | |
410 (nth 4 now) (nth 5 now) (nth 8 now))))))) | |
411 | |
412 (or (consp time) | |
413 (error "Invalid time format")) | |
414 | |
415 (let ((timer (timer-create))) | |
416 (timer-set-time timer time repeat) | |
417 (timer-set-function timer function args) | |
418 (timer-activate timer) | |
419 timer)) | |
420 | |
421 (defun run-with-timer (secs repeat function &rest args) | |
422 "Perform an action after a delay of SECS seconds. | |
423 Repeat the action every REPEAT seconds, if REPEAT is non-nil. | |
424 SECS and REPEAT may be integers or floating point numbers. | |
425 The action is to call FUNCTION with arguments ARGS. | |
426 | |
427 This function returns a timer object which you can use in `cancel-timer'." | |
428 (interactive "sRun after delay (seconds): \nNRepeat interval: \naFunction: ") | |
429 (apply 'run-at-time secs repeat function args)) | |
430 | |
431 (defun add-timeout (secs function object &optional repeat) | |
432 "Add a timer to run SECS seconds from now, to call FUNCTION on OBJECT. | |
433 If REPEAT is non-nil, repeat the timer every REPEAT seconds. | |
434 This function is for compatibility; see also `run-with-timer'." | |
435 (run-with-timer secs repeat function object)) | |
436 | |
437 (defun run-with-idle-timer (secs repeat function &rest args) | |
438 "Perform an action the next time Emacs is idle for SECS seconds. | |
439 The action is to call FUNCTION with arguments ARGS. | |
72508
5d642f9eff22
* emacs-lisp/timer.el (timer-set-idle-time, run-with-idle-timer):
Chong Yidong <cyd@stupidchicken.com>
parents:
72450
diff
changeset
|
440 SECS may be an integer, a floating point number, or the internal |
5d642f9eff22
* emacs-lisp/timer.el (timer-set-idle-time, run-with-idle-timer):
Chong Yidong <cyd@stupidchicken.com>
parents:
72450
diff
changeset
|
441 time format (HIGH LOW USECS) returned by, e.g., `current-idle-time'. |
72450
466083d03cb4
(run-with-idle-timer): Pass t to timer-activate-when-idle, so timer
Richard M. Stallman <rms@gnu.org>
parents:
68648
diff
changeset
|
442 If Emacs is currently idle, and has been idle for N seconds (N < SECS), |
466083d03cb4
(run-with-idle-timer): Pass t to timer-activate-when-idle, so timer
Richard M. Stallman <rms@gnu.org>
parents:
68648
diff
changeset
|
443 then it will call FUNCTION in SECS - N seconds from now. |
51349 | 444 |
445 If REPEAT is non-nil, do the action each time Emacs has been idle for | |
446 exactly SECS seconds (that is, only once for each time Emacs becomes idle). | |
447 | |
448 This function returns a timer object which you can use in `cancel-timer'." | |
449 (interactive | |
450 (list (read-from-minibuffer "Run after idle (seconds): " nil nil t) | |
451 (y-or-n-p "Repeat each time Emacs is idle? ") | |
452 (intern (completing-read "Function: " obarray 'fboundp t)))) | |
453 (let ((timer (timer-create))) | |
454 (timer-set-function timer function args) | |
455 (timer-set-idle-time timer secs repeat) | |
72450
466083d03cb4
(run-with-idle-timer): Pass t to timer-activate-when-idle, so timer
Richard M. Stallman <rms@gnu.org>
parents:
68648
diff
changeset
|
456 (timer-activate-when-idle timer t) |
51349 | 457 timer)) |
458 | |
459 (defun with-timeout-handler (tag) | |
72720
a3c85e1915ad
(timer-create, timer-activate): Doc fixes.
Richard M. Stallman <rms@gnu.org>
parents:
72508
diff
changeset
|
460 "This is the timer function used for the timer made by `with-timeout'." |
51349 | 461 (throw tag 'timeout)) |
462 | |
92948
73a01bf5cb80
* faces.el (xw-defined-colors):
Dan Nicolaescu <dann@ics.uci.edu>
parents:
87649
diff
changeset
|
463 (put 'with-timeout 'lisp-indent-function 1) |
51349 | 464 |
64208
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
465 (defvar with-timeout-timers nil |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
466 "List of all timers used by currently pending `with-timeout' calls.") |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
467 |
51349 | 468 (defmacro with-timeout (list &rest body) |
469 "Run BODY, but if it doesn't finish in SECONDS seconds, give up. | |
470 If we give up, we run the TIMEOUT-FORMS and return the value of the last one. | |
471 The timeout is checked whenever Emacs waits for some kind of external | |
63994
f44f159ea6de
(with-timeout): Improve argument/docstring consistency.
Juanma Barranquero <lekktu@gmail.com>
parents:
59996
diff
changeset
|
472 event (such as keyboard input, input from subprocesses, or a certain time); |
51349 | 473 if the program loops without waiting in any way, the timeout will not |
63994
f44f159ea6de
(with-timeout): Improve argument/docstring consistency.
Juanma Barranquero <lekktu@gmail.com>
parents:
59996
diff
changeset
|
474 be detected. |
f44f159ea6de
(with-timeout): Improve argument/docstring consistency.
Juanma Barranquero <lekktu@gmail.com>
parents:
59996
diff
changeset
|
475 \n(fn (SECONDS TIMEOUT-FORMS...) BODY)" |
51349 | 476 (let ((seconds (car list)) |
477 (timeout-forms (cdr list))) | |
478 `(let ((with-timeout-tag (cons nil nil)) | |
64208
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
479 with-timeout-value with-timeout-timer |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
480 (with-timeout-timers with-timeout-timers)) |
51349 | 481 (if (catch with-timeout-tag |
482 (progn | |
483 (setq with-timeout-timer | |
484 (run-with-timer ,seconds nil | |
485 'with-timeout-handler | |
486 with-timeout-tag)) | |
64208
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
487 (push with-timeout-timer with-timeout-timers) |
51349 | 488 (setq with-timeout-value (progn . ,body)) |
489 nil)) | |
490 (progn . ,timeout-forms) | |
491 (cancel-timer with-timeout-timer) | |
492 with-timeout-value)))) | |
493 | |
64208
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
494 (defun with-timeout-suspend () |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
495 "Stop the clock for `with-timeout'. Used by debuggers. |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
496 The idea is that the time you spend in the debugger should not |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
497 count against these timeouts. |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
498 |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
499 The value is a list that the debugger can pass to `with-timeout-unsuspend' |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
500 when it exits, to make these timers start counting again." |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
501 (mapcar (lambda (timer) |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
502 (cancel-timer timer) |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
503 (list timer |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
504 (time-subtract |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
505 ;; The time that this timer will go off. |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
506 (list (aref timer 1) (aref timer 2) (aref timer 3)) |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
507 (current-time)))) |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
508 with-timeout-timers)) |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
509 |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
510 (defun with-timeout-unsuspend (timer-spec-list) |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
511 "Restart the clock for `with-timeout'. |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
512 The argument should be a value previously returned by `with-timeout-suspend'." |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
513 (dolist (elt timer-spec-list) |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
514 (let ((timer (car elt)) |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
515 (delay (cadr elt))) |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
516 (timer-set-time timer (time-add (current-time) delay)) |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
517 (timer-activate timer)))) |
5177474d1ca7
(with-timeout-timers): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
64085
diff
changeset
|
518 |
51349 | 519 (defun y-or-n-p-with-timeout (prompt seconds default-value) |
520 "Like (y-or-n-p PROMPT), with a timeout. | |
521 If the user does not answer after SECONDS seconds, return DEFAULT-VALUE." | |
522 (with-timeout (seconds default-value) | |
523 (y-or-n-p prompt))) | |
524 | |
525 (defvar timer-duration-words | |
526 (list (cons "microsec" 0.000001) | |
527 (cons "microsecond" 0.000001) | |
528 (cons "millisec" 0.001) | |
529 (cons "millisecond" 0.001) | |
530 (cons "sec" 1) | |
531 (cons "second" 1) | |
532 (cons "min" 60) | |
533 (cons "minute" 60) | |
534 (cons "hour" (* 60 60)) | |
535 (cons "day" (* 24 60 60)) | |
536 (cons "week" (* 7 24 60 60)) | |
537 (cons "fortnight" (* 14 24 60 60)) | |
538 (cons "month" (* 30 24 60 60)) ; Approximation | |
539 (cons "year" (* 365.25 24 60 60)) ; Approximation | |
540 ) | |
541 "Alist mapping temporal words to durations in seconds") | |
542 | |
543 (defun timer-duration (string) | |
544 "Return number of seconds specified by STRING, or nil if parsing fails." | |
545 (let ((secs 0) | |
546 (start 0) | |
547 (case-fold-search t)) | |
548 (while (string-match | |
549 "[ \t]*\\([0-9.]+\\)?[ \t]*\\([a-z]+[a-rt-z]\\)s?[ \t]*" | |
550 string start) | |
551 (let ((count (if (match-beginning 1) | |
552 (string-to-number (match-string 1 string)) | |
553 1)) | |
554 (itemsize (cdr (assoc (match-string 2 string) | |
555 timer-duration-words)))) | |
556 (if itemsize | |
557 (setq start (match-end 0) | |
558 secs (+ secs (* count itemsize))) | |
559 (setq secs nil | |
560 start (length string))))) | |
561 (if (= start (length string)) | |
562 secs | |
563 (if (string-match "\\`[0-9.]+\\'" string) | |
564 (string-to-number string))))) | |
565 | |
566 (provide 'timer) | |
567 | |
52401 | 568 ;;; arch-tag: b1a9237b-7787-4382-9e46-8f2c3b3273e0 |
51349 | 569 ;;; timer.el ends here |