Mercurial > emacs
annotate lisp/timer.el @ 47476:68e034e01f4c
(diff-mode): Turn on compilation-minor-mode
support again, but more carefully this time.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Fri, 13 Sep 2002 19:45:36 +0000 |
parents | 31c7343400b9 |
children | e88404e8f2cf |
rev | line source |
---|---|
38412
253f761ad37b
Some fixes to follow coding conventions in files maintained by FSF.
Pavel Janík <Pavel@Janik.cz>
parents:
31290
diff
changeset
|
1 ;;; timer.el --- run a function with args at some time in future |
14449 | 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. | |
14705
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
47 TIME must be in the internal format returned by, e.g., `current-time'. |
46399
ded24b27ba96
(timer-set-time, timer-set-time-with-usecs): Fix docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
46319
diff
changeset
|
48 If optional third argument DELTA is a positive number, make the timer |
14705
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
49 fire repeatedly that many seconds apart." |
14449 | 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))) | |
14870
c51cef393dae
(timer-set-time): Don't set usecs to nil.
Richard M. Stallman <rms@gnu.org>
parents:
14705
diff
changeset
|
54 (aset timer 3 (or (and (consp (cdr time)) (consp (cdr (cdr time))) |
c51cef393dae
(timer-set-time): Don't set usecs to nil.
Richard M. Stallman <rms@gnu.org>
parents:
14705
diff
changeset
|
55 (nth 2 time)) |
c51cef393dae
(timer-set-time): Don't set usecs to nil.
Richard M. Stallman <rms@gnu.org>
parents:
14705
diff
changeset
|
56 0)) |
14449 | 57 (aset timer 4 (and (numberp delta) (> delta 0) delta)) |
58 timer) | |
59 | |
14705
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
60 (defun timer-set-idle-time (timer secs &optional repeat) |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
61 "Set the trigger idle time of TIMER to SECS. |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
62 If optional third argument REPEAT is non-nil, make the timer |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
63 fire each time Emacs is idle for that many seconds." |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
64 (or (timerp timer) |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
65 (error "Invalid timer")) |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
66 (aset timer 1 0) |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
67 (aset timer 2 0) |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
68 (aset timer 3 0) |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
69 (timer-inc-time timer secs) |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
70 (aset timer 4 repeat) |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
71 timer) |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
72 |
16085
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
73 (defun timer-next-integral-multiple-of-time (time secs) |
16654
4f8b4e26cc92
(timer-next-integral-multiple-of-time): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
16407
diff
changeset
|
74 "Yield the next value after TIME that is an integral multiple of SECS. |
4f8b4e26cc92
(timer-next-integral-multiple-of-time): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
16407
diff
changeset
|
75 More precisely, the next value, after TIME, that is an integral multiple |
4f8b4e26cc92
(timer-next-integral-multiple-of-time): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
16407
diff
changeset
|
76 of SECS seconds since the epoch. SECS may be a fraction." |
16085
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
77 (let ((time-base (ash 1 16))) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
78 (if (fboundp 'atan) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
79 ;; Use floating point, taking care to not lose precision. |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
80 (let* ((float-time-base (float time-base)) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
81 (million 1000000.0) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
82 (time-usec (+ (* million |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
83 (+ (* float-time-base (nth 0 time)) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
84 (nth 1 time))) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
85 (nth 2 time))) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
86 (secs-usec (* million secs)) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
87 (mod-usec (mod time-usec secs-usec)) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
88 (next-usec (+ (- time-usec mod-usec) secs-usec)) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
89 (time-base-million (* float-time-base million))) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
90 (list (floor next-usec time-base-million) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
91 (floor (mod next-usec time-base-million) million) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
92 (floor (mod next-usec million)))) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
93 ;; Floating point is not supported. |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
94 ;; Use integer arithmetic, avoiding overflow if possible. |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
95 (let* ((mod-sec (mod (+ (* (mod time-base secs) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
96 (mod (nth 0 time) secs)) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
97 (nth 1 time)) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
98 secs)) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
99 (next-1-sec (+ (- (nth 1 time) mod-sec) secs))) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
100 (list (+ (nth 0 time) (floor next-1-sec time-base)) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
101 (mod next-1-sec time-base) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
102 0))))) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
103 |
14449 | 104 (defun timer-relative-time (time secs &optional usecs) |
105 "Advance TIME by SECS seconds and optionally USECS microseconds. | |
106 SECS may be a fraction." | |
107 (let ((high (car time)) | |
108 (low (if (consp (cdr time)) (nth 1 time) (cdr time))) | |
109 (micro (if (numberp (car-safe (cdr-safe (cdr time)))) | |
110 (nth 2 time) | |
111 0))) | |
112 ;; Add | |
113 (if usecs (setq micro (+ micro usecs))) | |
114 (if (floatp secs) | |
115 (setq micro (+ micro (floor (* 1000000 (- secs (floor secs))))))) | |
116 (setq low (+ low (floor secs))) | |
117 | |
118 ;; Normalize | |
39558
88e97e81d728
(timer-relative-time): Fix computation for negative `micro'.
Gerd Moellmann <gerd@gnu.org>
parents:
38412
diff
changeset
|
119 ;; `/' rounds towards zero while `mod' returns a positive number, |
88e97e81d728
(timer-relative-time): Fix computation for negative `micro'.
Gerd Moellmann <gerd@gnu.org>
parents:
38412
diff
changeset
|
120 ;; so we can't rely on (= a (+ (* 100 (/ a 100)) (mod a 100))). |
88e97e81d728
(timer-relative-time): Fix computation for negative `micro'.
Gerd Moellmann <gerd@gnu.org>
parents:
38412
diff
changeset
|
121 (setq low (+ low (/ micro 1000000) (if (< micro 0) -1 0))) |
14449 | 122 (setq micro (mod micro 1000000)) |
39558
88e97e81d728
(timer-relative-time): Fix computation for negative `micro'.
Gerd Moellmann <gerd@gnu.org>
parents:
38412
diff
changeset
|
123 (setq high (+ high (/ low 65536) (if (< low 0) -1 0))) |
14449 | 124 (setq low (logand low 65535)) |
125 | |
126 (list high low (and (/= micro 0) micro)))) | |
127 | |
128 (defun timer-inc-time (timer secs &optional usecs) | |
129 "Increment the time set in TIMER by SECS seconds and USECS microseconds. | |
46520
31c7343400b9
(timer-inc-time): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
46484
diff
changeset
|
130 SECS may be a fraction. If USECS is omitted, that means it is zero." |
14449 | 131 (let ((time (timer-relative-time |
132 (list (aref timer 1) (aref timer 2) (aref timer 3)) | |
133 secs | |
134 usecs))) | |
135 (aset timer 1 (nth 0 time)) | |
136 (aset timer 2 (nth 1 time)) | |
137 (aset timer 3 (or (nth 2 time) 0)))) | |
138 | |
139 (defun timer-set-time-with-usecs (timer time usecs &optional delta) | |
46319
534629ca4cc4
(timer-set-time-with-usecs): Fix documentation. Simplify extraction of time data.
Juanma Barranquero <lekktu@gmail.com>
parents:
39558
diff
changeset
|
140 "Set the trigger time of TIMER to TIME plus USECS. |
14705
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
141 TIME must be in the internal format returned by, e.g., `current-time'. |
46319
534629ca4cc4
(timer-set-time-with-usecs): Fix documentation. Simplify extraction of time data.
Juanma Barranquero <lekktu@gmail.com>
parents:
39558
diff
changeset
|
142 The microsecond count from TIME is ignored, and USECS is used instead. |
46399
ded24b27ba96
(timer-set-time, timer-set-time-with-usecs): Fix docstring.
Juanma Barranquero <lekktu@gmail.com>
parents:
46319
diff
changeset
|
143 If optional fourth argument DELTA is a positive number, make the timer |
14705
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
144 fire repeatedly that many seconds apart." |
14449 | 145 (or (timerp timer) |
146 (error "Invalid timer")) | |
46319
534629ca4cc4
(timer-set-time-with-usecs): Fix documentation. Simplify extraction of time data.
Juanma Barranquero <lekktu@gmail.com>
parents:
39558
diff
changeset
|
147 (aset timer 1 (nth 0 time)) |
534629ca4cc4
(timer-set-time-with-usecs): Fix documentation. Simplify extraction of time data.
Juanma Barranquero <lekktu@gmail.com>
parents:
39558
diff
changeset
|
148 (aset timer 2 (nth 1 time)) |
14449 | 149 (aset timer 3 usecs) |
150 (aset timer 4 (and (numberp delta) (> delta 0) delta)) | |
151 timer) | |
46484
6d05f3e71417
(timer-set-time-with-usecs): Mark obsolete.
Juanma Barranquero <lekktu@gmail.com>
parents:
46399
diff
changeset
|
152 (make-obsolete 'timer-set-time-with-usecs |
6d05f3e71417
(timer-set-time-with-usecs): Mark obsolete.
Juanma Barranquero <lekktu@gmail.com>
parents:
46399
diff
changeset
|
153 "use `timer-set-time' and `timer-inc-time' instead." |
6d05f3e71417
(timer-set-time-with-usecs): Mark obsolete.
Juanma Barranquero <lekktu@gmail.com>
parents:
46399
diff
changeset
|
154 "21.4") |
14449 | 155 |
156 (defun timer-set-function (timer function &optional args) | |
157 "Make TIMER call FUNCTION with optional ARGS when triggering." | |
158 (or (timerp timer) | |
159 (error "Invalid timer")) | |
160 (aset timer 5 function) | |
161 (aset timer 6 args) | |
162 timer) | |
163 | |
164 (defun timer-activate (timer) | |
165 "Put TIMER on the list of active timers." | |
166 (if (and (timerp timer) | |
167 (integerp (aref timer 1)) | |
168 (integerp (aref timer 2)) | |
169 (integerp (aref timer 3)) | |
170 (aref timer 5)) | |
171 (let ((timers timer-list) | |
172 last) | |
173 ;; Skip all timers to trigger before the new one. | |
174 (while (and timers | |
175 (or (> (aref timer 1) (aref (car timers) 1)) | |
176 (and (= (aref timer 1) (aref (car timers) 1)) | |
177 (> (aref timer 2) (aref (car timers) 2))) | |
178 (and (= (aref timer 1) (aref (car timers) 1)) | |
179 (= (aref timer 2) (aref (car timers) 2)) | |
180 (> (aref timer 3) (aref (car timers) 3))))) | |
181 (setq last timers | |
182 timers (cdr timers))) | |
183 ;; Insert new timer after last which possibly means in front of queue. | |
184 (if last | |
185 (setcdr last (cons timer timers)) | |
186 (setq timer-list (cons timer timers))) | |
187 (aset timer 0 nil) | |
14632
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
188 (aset timer 7 nil) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
189 nil) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
190 (error "Invalid or uninitialized timer"))) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
191 |
30436
9abea5a69fb5
(timer-activate-when-idle): Add optional parameter
Andrew Innes <andrewi@gnu.org>
parents:
30336
diff
changeset
|
192 (defun timer-activate-when-idle (timer &optional dont-wait) |
9abea5a69fb5
(timer-activate-when-idle): Add optional parameter
Andrew Innes <andrewi@gnu.org>
parents:
30336
diff
changeset
|
193 "Arrange to activate TIMER whenever Emacs is next idle. |
9abea5a69fb5
(timer-activate-when-idle): Add optional parameter
Andrew Innes <andrewi@gnu.org>
parents:
30336
diff
changeset
|
194 If optional argument DONT-WAIT is non-nil, then enable the |
9abea5a69fb5
(timer-activate-when-idle): Add optional parameter
Andrew Innes <andrewi@gnu.org>
parents:
30336
diff
changeset
|
195 timer to activate immediately, or at the right time, if Emacs |
9abea5a69fb5
(timer-activate-when-idle): Add optional parameter
Andrew Innes <andrewi@gnu.org>
parents:
30336
diff
changeset
|
196 is already idle." |
14632
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
197 (if (and (timerp timer) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
198 (integerp (aref timer 1)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
199 (integerp (aref timer 2)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
200 (integerp (aref timer 3)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
201 (aref timer 5)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
202 (let ((timers timer-idle-list) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
203 last) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
204 ;; 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
|
205 (while (and timers |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
206 (or (> (aref timer 1) (aref (car timers) 1)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
207 (and (= (aref timer 1) (aref (car timers) 1)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
208 (> (aref timer 2) (aref (car timers) 2))) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
209 (and (= (aref timer 1) (aref (car timers) 1)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
210 (= (aref timer 2) (aref (car timers) 2)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
211 (> (aref timer 3) (aref (car timers) 3))))) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
212 (setq last timers |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
213 timers (cdr timers))) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
214 ;; 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
|
215 (if last |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
216 (setcdr last (cons timer timers)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
217 (setq timer-idle-list (cons timer timers))) |
30436
9abea5a69fb5
(timer-activate-when-idle): Add optional parameter
Andrew Innes <andrewi@gnu.org>
parents:
30336
diff
changeset
|
218 (aset timer 0 (not dont-wait)) |
14632
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
219 (aset timer 7 t) |
14449 | 220 nil) |
221 (error "Invalid or uninitialized timer"))) | |
222 | |
16400
9a39893d9861
(cancel-timer): Add autoload cookie.
Richard M. Stallman <rms@gnu.org>
parents:
16085
diff
changeset
|
223 ;;;###autoload |
14449 | 224 (defalias 'disable-timeout 'cancel-timer) |
16400
9a39893d9861
(cancel-timer): Add autoload cookie.
Richard M. Stallman <rms@gnu.org>
parents:
16085
diff
changeset
|
225 ;;;###autoload |
14449 | 226 (defun cancel-timer (timer) |
227 "Remove TIMER from the list of active timers." | |
228 (or (timerp timer) | |
229 (error "Invalid timer")) | |
230 (setq timer-list (delq timer timer-list)) | |
14632
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
231 (setq timer-idle-list (delq timer timer-idle-list)) |
14449 | 232 nil) |
233 | |
16407
a3bd74a05c45
(cancel-function-timers): Add autoload.
Richard M. Stallman <rms@gnu.org>
parents:
16400
diff
changeset
|
234 ;;;###autoload |
14449 | 235 (defun cancel-function-timers (function) |
236 "Cancel all timers scheduled by `run-at-time' which would run FUNCTION." | |
237 (interactive "aCancel timers of function: ") | |
238 (let ((tail timer-list)) | |
239 (while tail | |
240 (if (eq (aref (car tail) 5) function) | |
241 (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
|
242 (setq tail (cdr tail)))) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
243 (let ((tail timer-idle-list)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
244 (while tail |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
245 (if (eq (aref (car tail) 5) function) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
246 (setq timer-idle-list (delq (car tail) timer-idle-list))) |
14449 | 247 (setq tail (cdr tail))))) |
248 | |
14885
38e792454ee3
(timer-event-handler): Inhibit quitting.
Richard M. Stallman <rms@gnu.org>
parents:
14870
diff
changeset
|
249 ;; Record the last few events, for debugging. |
38e792454ee3
(timer-event-handler): Inhibit quitting.
Richard M. Stallman <rms@gnu.org>
parents:
14870
diff
changeset
|
250 (defvar timer-event-last-2 nil) |
38e792454ee3
(timer-event-handler): Inhibit quitting.
Richard M. Stallman <rms@gnu.org>
parents:
14870
diff
changeset
|
251 (defvar timer-event-last-1 nil) |
38e792454ee3
(timer-event-handler): Inhibit quitting.
Richard M. Stallman <rms@gnu.org>
parents:
14870
diff
changeset
|
252 (defvar timer-event-last nil) |
38e792454ee3
(timer-event-handler): Inhibit quitting.
Richard M. Stallman <rms@gnu.org>
parents:
14870
diff
changeset
|
253 |
17452 | 254 (defvar timer-max-repeats 10 |
255 "*Maximum number of times to repeat a timer, if real time jumps.") | |
256 | |
257 (defun timer-until (timer time) | |
258 "Calculate number of seconds from when TIMER will run, until TIME. | |
259 TIMER is a timer, and stands for the time when its next repeat is scheduled. | |
17457
e614abd1ff44
(timer-until): Fix syntax error.
Karl Heuer <kwzh@gnu.org>
parents:
17452
diff
changeset
|
260 TIME is a time-list." |
17452 | 261 (let ((high (- (car time) (aref timer 1))) |
262 (low (- (nth 1 time) (aref timer 2)))) | |
263 (+ low (* high 65536)))) | |
264 | |
17708
95f0456681cc
(timer-event-handler): Take timer as arg directly.
Richard M. Stallman <rms@gnu.org>
parents:
17457
diff
changeset
|
265 (defun timer-event-handler (timer) |
95f0456681cc
(timer-event-handler): Take timer as arg directly.
Richard M. Stallman <rms@gnu.org>
parents:
17457
diff
changeset
|
266 "Call the handler for the timer TIMER. |
95f0456681cc
(timer-event-handler): Take timer as arg directly.
Richard M. Stallman <rms@gnu.org>
parents:
17457
diff
changeset
|
267 This function is called, by name, directly by the C code." |
14885
38e792454ee3
(timer-event-handler): Inhibit quitting.
Richard M. Stallman <rms@gnu.org>
parents:
14870
diff
changeset
|
268 (setq timer-event-last-2 timer-event-last-1) |
38e792454ee3
(timer-event-handler): Inhibit quitting.
Richard M. Stallman <rms@gnu.org>
parents:
14870
diff
changeset
|
269 (setq timer-event-last-1 timer-event-last) |
17708
95f0456681cc
(timer-event-handler): Take timer as arg directly.
Richard M. Stallman <rms@gnu.org>
parents:
17457
diff
changeset
|
270 (setq timer-event-last timer) |
95f0456681cc
(timer-event-handler): Take timer as arg directly.
Richard M. Stallman <rms@gnu.org>
parents:
17457
diff
changeset
|
271 (let ((inhibit-quit t)) |
14449 | 272 (if (timerp timer) |
273 (progn | |
274 ;; Delete from queue. | |
275 (cancel-timer timer) | |
276 ;; Re-schedule if requested. | |
277 (if (aref timer 4) | |
14632
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
278 (if (aref timer 7) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
279 (timer-activate-when-idle timer) |
14449 | 280 (timer-inc-time timer (aref timer 4) 0) |
17452 | 281 ;; If real time has jumped forward, |
282 ;; perhaps because Emacs was suspended for a long time, | |
283 ;; limit how many times things get repeated. | |
284 (if (and (numberp timer-max-repeats) | |
285 (< 0 (timer-until timer (current-time)))) | |
286 (let ((repeats (/ (timer-until timer (current-time)) | |
287 (aref timer 4)))) | |
288 (if (> repeats timer-max-repeats) | |
289 (timer-inc-time timer (* (aref timer 4) repeats))))) | |
19588
f51f46bef183
(timer-event-handler): Reactivate timer first,
Richard M. Stallman <rms@gnu.org>
parents:
17708
diff
changeset
|
290 (timer-activate timer))) |
f51f46bef183
(timer-event-handler): Reactivate timer first,
Richard M. Stallman <rms@gnu.org>
parents:
17708
diff
changeset
|
291 ;; Run handler. |
f51f46bef183
(timer-event-handler): Reactivate timer first,
Richard M. Stallman <rms@gnu.org>
parents:
17708
diff
changeset
|
292 ;; We do this after rescheduling so that the handler function |
f51f46bef183
(timer-event-handler): Reactivate timer first,
Richard M. Stallman <rms@gnu.org>
parents:
17708
diff
changeset
|
293 ;; can cancel its own timer successfully with cancel-timer. |
f51f46bef183
(timer-event-handler): Reactivate timer first,
Richard M. Stallman <rms@gnu.org>
parents:
17708
diff
changeset
|
294 (condition-case nil |
f51f46bef183
(timer-event-handler): Reactivate timer first,
Richard M. Stallman <rms@gnu.org>
parents:
17708
diff
changeset
|
295 (apply (aref timer 5) (aref timer 6)) |
f51f46bef183
(timer-event-handler): Reactivate timer first,
Richard M. Stallman <rms@gnu.org>
parents:
17708
diff
changeset
|
296 (error nil))) |
14449 | 297 (error "Bogus timer event")))) |
14705
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
298 |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
299 ;; This function is incompatible with the one in levents.el. |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
300 (defun timeout-event-p (event) |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
301 "Non-nil if EVENT is a timeout event." |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
302 (and (listp event) (eq (car event) 'timer-event))) |
14449 | 303 |
304 ;;;###autoload | |
305 (defun run-at-time (time repeat function &rest args) | |
16077 | 306 "Perform an action at time TIME. |
14705
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
307 Repeat the action every REPEAT seconds, if REPEAT is non-nil. |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
308 TIME should be a string like \"11:23pm\", nil meaning now, a number of seconds |
16085
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
309 from now, a value from `current-time', or t (with non-nil REPEAT) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
310 meaning the next integral multiple of REPEAT. |
14705
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
311 REPEAT may be an integer or floating point number. |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
312 The action is to call FUNCTION with arguments ARGS. |
14449 | 313 |
314 This function returns a timer object which you can use in `cancel-timer'." | |
315 (interactive "sRun at time: \nNRepeat interval: \naFunction: ") | |
316 | |
16085
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
317 (or (null repeat) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
318 (and (numberp repeat) (< 0 repeat)) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
319 (error "Invalid repetition interval")) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
320 |
14686 | 321 ;; Special case: nil means "now" and is useful when repeating. |
14449 | 322 (if (null time) |
323 (setq time (current-time))) | |
324 | |
16085
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
325 ;; Special case: t means the next integral multiple of REPEAT. |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
326 (if (and (eq time t) repeat) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
327 (setq time (timer-next-integral-multiple-of-time (current-time) repeat))) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
328 |
14508
87b0d4b7577a
(run-at-time): Handle numbers as relative times in seconds, as the original
Roland McGrath <roland@gnu.org>
parents:
14470
diff
changeset
|
329 ;; 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
|
330 (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
|
331 (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
|
332 |
14449 | 333 ;; Handle relative times like "2 hours and 35 minutes" |
334 (if (stringp time) | |
335 (let ((secs (timer-duration time))) | |
336 (if secs | |
337 (setq time (timer-relative-time (current-time) secs))))) | |
338 | |
339 ;; Handle "11:23pm" and the like. Interpret it as meaning today | |
340 ;; which admittedly is rather stupid if we have passed that time | |
14705
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
341 ;; already. (Though only Emacs hackers hack Emacs at that time.) |
14449 | 342 (if (stringp time) |
343 (progn | |
344 (require 'diary-lib) | |
345 (let ((hhmm (diary-entry-time time)) | |
346 (now (decode-time))) | |
347 (if (>= hhmm 0) | |
348 (setq time | |
349 (encode-time 0 (% hhmm 100) (/ hhmm 100) (nth 3 now) | |
350 (nth 4 now) (nth 5 now) (nth 8 now))))))) | |
351 | |
352 (or (consp time) | |
353 (error "Invalid time format")) | |
354 | |
355 (let ((timer (timer-create))) | |
356 (timer-set-time timer time repeat) | |
357 (timer-set-function timer function args) | |
358 (timer-activate timer) | |
359 timer)) | |
360 | |
361 ;;;###autoload | |
362 (defun run-with-timer (secs repeat function &rest args) | |
363 "Perform an action after a delay of SECS seconds. | |
364 Repeat the action every REPEAT seconds, if REPEAT is non-nil. | |
365 SECS and REPEAT may be integers or floating point numbers. | |
366 The action is to call FUNCTION with arguments ARGS. | |
367 | |
368 This function returns a timer object which you can use in `cancel-timer'." | |
369 (interactive "sRun after delay (seconds): \nNRepeat interval: \naFunction: ") | |
14705
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
370 (apply 'run-at-time secs repeat function args)) |
14632
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
371 |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
372 ;;;###autoload |
14449 | 373 (defun add-timeout (secs function object &optional repeat) |
374 "Add a timer to run SECS seconds from now, to call FUNCTION on OBJECT. | |
375 If REPEAT is non-nil, repeat the timer every REPEAT seconds. | |
376 This function is for compatibility; see also `run-with-timer'." | |
377 (run-with-timer secs repeat function object)) | |
378 | |
14705
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
379 ;;;###autoload |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
380 (defun run-with-idle-timer (secs repeat function &rest args) |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
381 "Perform an action the next time Emacs is idle for SECS seconds. |
30336
01102e63ed88
(run-with-idle-timer): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
19588
diff
changeset
|
382 The action is to call FUNCTION with arguments ARGS. |
14705
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
383 SECS may be an integer or a floating point number. |
30336
01102e63ed88
(run-with-idle-timer): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
19588
diff
changeset
|
384 |
01102e63ed88
(run-with-idle-timer): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
19588
diff
changeset
|
385 If REPEAT is non-nil, do the action each time Emacs has been idle for |
01102e63ed88
(run-with-idle-timer): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
19588
diff
changeset
|
386 exactly SECS seconds (that is, only once for each time Emacs becomes idle). |
14449 | 387 |
14705
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
388 This function returns a timer object which you can use in `cancel-timer'." |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
389 (interactive |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
390 (list (read-from-minibuffer "Run after idle (seconds): " nil nil t) |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
391 (y-or-n-p "Repeat each time Emacs is idle? ") |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
392 (intern (completing-read "Function: " obarray 'fboundp t)))) |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
393 (let ((timer (timer-create))) |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
394 (timer-set-function timer function args) |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
395 (timer-set-idle-time timer secs repeat) |
31290
a7c6f2d2f74f
(run-with-idle-timer): Undo last change, so that timer
Andrew Innes <andrewi@gnu.org>
parents:
30436
diff
changeset
|
396 (timer-activate-when-idle timer) |
14705
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
397 timer)) |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
398 |
14449 | 399 (defun with-timeout-handler (tag) |
400 (throw tag 'timeout)) | |
401 | |
402 ;;;###autoload (put 'with-timeout 'lisp-indent-function 1) | |
403 | |
404 ;;;###autoload | |
405 (defmacro with-timeout (list &rest body) | |
406 "Run BODY, but if it doesn't finish in SECONDS seconds, give up. | |
407 If we give up, we run the TIMEOUT-FORMS and return the value of the last one. | |
14705
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
408 The call should look like: |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
409 (with-timeout (SECONDS TIMEOUT-FORMS...) BODY...) |
14449 | 410 The timeout is checked whenever Emacs waits for some kind of external |
411 event \(such as keyboard input, input from subprocesses, or a certain time); | |
412 if the program loops without waiting in any way, the timeout will not | |
413 be detected." | |
414 (let ((seconds (car list)) | |
415 (timeout-forms (cdr list))) | |
416 `(let ((with-timeout-tag (cons nil nil)) | |
417 with-timeout-value with-timeout-timer) | |
418 (if (catch with-timeout-tag | |
419 (progn | |
420 (setq with-timeout-timer | |
421 (run-with-timer ,seconds nil | |
422 'with-timeout-handler | |
423 with-timeout-tag)) | |
424 (setq with-timeout-value (progn . ,body)) | |
425 nil)) | |
426 (progn . ,timeout-forms) | |
427 (cancel-timer with-timeout-timer) | |
428 with-timeout-value)))) | |
429 | |
430 (defun y-or-n-p-with-timeout (prompt seconds default-value) | |
431 "Like (y-or-n-p PROMPT), with a timeout. | |
432 If the user does not answer after SECONDS seconds, return DEFAULT-VALUE." | |
433 (with-timeout (seconds default-value) | |
434 (y-or-n-p prompt))) | |
435 | |
436 (defvar timer-duration-words | |
437 (list (cons "microsec" 0.000001) | |
438 (cons "microsecond" 0.000001) | |
439 (cons "millisec" 0.001) | |
440 (cons "millisecond" 0.001) | |
441 (cons "sec" 1) | |
442 (cons "second" 1) | |
443 (cons "min" 60) | |
444 (cons "minute" 60) | |
445 (cons "hour" (* 60 60)) | |
446 (cons "day" (* 24 60 60)) | |
447 (cons "week" (* 7 24 60 60)) | |
448 (cons "fortnight" (* 14 24 60 60)) | |
449 (cons "month" (* 30 24 60 60)) ; Approximation | |
450 (cons "year" (* 365.25 24 60 60)) ; Approximation | |
451 ) | |
452 "Alist mapping temporal words to durations in seconds") | |
453 | |
454 (defun timer-duration (string) | |
455 "Return number of seconds specified by STRING, or nil if parsing fails." | |
456 (let ((secs 0) | |
457 (start 0) | |
458 (case-fold-search t)) | |
459 (while (string-match | |
460 "[ \t]*\\([0-9.]+\\)?[ \t]*\\([a-z]+[a-rt-z]\\)s?[ \t]*" | |
461 string start) | |
462 (let ((count (if (match-beginning 1) | |
463 (string-to-number (match-string 1 string)) | |
464 1)) | |
465 (itemsize (cdr (assoc (match-string 2 string) | |
466 timer-duration-words)))) | |
467 (if itemsize | |
468 (setq start (match-end 0) | |
469 secs (+ secs (* count itemsize))) | |
470 (setq secs nil | |
471 start (length string))))) | |
14870
c51cef393dae
(timer-set-time): Don't set usecs to nil.
Richard M. Stallman <rms@gnu.org>
parents:
14705
diff
changeset
|
472 (if (= start (length string)) |
c51cef393dae
(timer-set-time): Don't set usecs to nil.
Richard M. Stallman <rms@gnu.org>
parents:
14705
diff
changeset
|
473 secs |
c51cef393dae
(timer-set-time): Don't set usecs to nil.
Richard M. Stallman <rms@gnu.org>
parents:
14705
diff
changeset
|
474 (if (string-match "\\`[0-9.]+\\'" string) |
c51cef393dae
(timer-set-time): Don't set usecs to nil.
Richard M. Stallman <rms@gnu.org>
parents:
14705
diff
changeset
|
475 (string-to-number string))))) |
14449 | 476 |
477 (provide 'timer) | |
478 | |
479 ;;; timer.el ends here |