Mercurial > emacs
annotate lisp/timer.el @ 16306:450f26426101 libc-960925 libc-960926 libc-960927 libc-960928 libc-960929 libc-961001 libc-961004 libc-961005 libc-961006 libc-961007 libc-961008 libc-961009 libc-961010 libc-961011 libc-961012 libc-961013 libc-961014 libc-961015 libc-961016 libc-961017 libc-961018 libc-961019 libc-961020 libc-961021 libc-961022 libc-961023 libc-961024 libc-961025 libc-961026 libc-961027 libc-961028
(-vxsim*): New operating system.
author | Richard Kenner <kenner@gnu.org> |
---|---|
date | Tue, 24 Sep 1996 02:44:17 +0000 |
parents | b215fb935fc3 |
children | 9a39893d9861 |
rev | line source |
---|---|
14449 | 1 ;;; timer.el --- run a function with args at some time in future. |
2 | |
3 ;; Copyright (C) 1996 Free Software Foundation, Inc. | |
4 | |
5 ;; Maintainer: FSF | |
6 | |
7 ;; This file is part of GNU Emacs. | |
8 | |
9 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
10 ;; it under the terms of the GNU General Public License as published by | |
11 ;; the Free Software Foundation; either version 2, or (at your option) | |
12 ;; any later version. | |
13 | |
14 ;; GNU Emacs is distributed in the hope that it will be useful, | |
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 ;; GNU General Public License for more details. | |
18 | |
19 ;; You should have received a copy of the GNU General Public License | |
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
22 ;; Boston, MA 02111-1307, USA. | |
23 | |
24 ;;; Commentary: | |
25 | |
26 ;; This package gives you the capability to run Emacs Lisp commands at | |
27 ;; specified times in the future, either as one-shots or periodically. | |
28 | |
29 ;;; Code: | |
30 | |
31 ;; Layout of a timer vector: | |
14632
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
32 ;; [triggered-p high-seconds low-seconds usecs repeat-delay |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
33 ;; function args idle-delay] |
14449 | 34 |
35 (defun timer-create () | |
36 "Create a timer object." | |
14632
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
37 (let ((timer (make-vector 8 nil))) |
14449 | 38 (aset timer 0 t) |
39 timer)) | |
40 | |
41 (defun timerp (object) | |
42 "Return t if OBJECT is a timer." | |
14632
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
43 (and (vectorp object) (= (length object) 8))) |
14449 | 44 |
45 (defun timer-set-time (timer time &optional delta) | |
46 "Set the trigger time of TIMER to TIME. | |
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'. |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
48 If optional third argument DELTA is a non-zero integer, make the timer |
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) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
74 "Yield the next value after TIME that is an integral number of SECS |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
75 since the epoch. SECS may be a fraction." |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
76 (let ((time-base (ash 1 16))) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
77 (if (fboundp 'atan) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
78 ;; 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
|
79 (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
|
80 (million 1000000.0) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
81 (time-usec (+ (* million |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
82 (+ (* float-time-base (nth 0 time)) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
83 (nth 1 time))) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
84 (nth 2 time))) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
85 (secs-usec (* million secs)) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
86 (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
|
87 (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
|
88 (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
|
89 (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
|
90 (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
|
91 (floor (mod next-usec million)))) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
92 ;; Floating point is not supported. |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
93 ;; 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
|
94 (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
|
95 (mod (nth 0 time) secs)) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
96 (nth 1 time)) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
97 secs)) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
98 (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
|
99 (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
|
100 (mod next-1-sec time-base) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
101 0))))) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
102 |
14449 | 103 (defun timer-relative-time (time secs &optional usecs) |
104 "Advance TIME by SECS seconds and optionally USECS microseconds. | |
105 SECS may be a fraction." | |
106 (let ((high (car time)) | |
107 (low (if (consp (cdr time)) (nth 1 time) (cdr time))) | |
108 (micro (if (numberp (car-safe (cdr-safe (cdr time)))) | |
109 (nth 2 time) | |
110 0))) | |
111 ;; Add | |
112 (if usecs (setq micro (+ micro usecs))) | |
113 (if (floatp secs) | |
114 (setq micro (+ micro (floor (* 1000000 (- secs (floor secs))))))) | |
115 (setq low (+ low (floor secs))) | |
116 | |
117 ;; Normalize | |
118 (setq low (+ low (/ micro 1000000))) | |
119 (setq micro (mod micro 1000000)) | |
120 (setq high (+ high (/ low 65536))) | |
121 (setq low (logand low 65535)) | |
122 | |
123 (list high low (and (/= micro 0) micro)))) | |
124 | |
125 (defun timer-inc-time (timer secs &optional usecs) | |
126 "Increment the time set in TIMER by SECS seconds and USECS microseconds. | |
127 SECS may be a fraction." | |
128 (let ((time (timer-relative-time | |
129 (list (aref timer 1) (aref timer 2) (aref timer 3)) | |
130 secs | |
131 usecs))) | |
132 (aset timer 1 (nth 0 time)) | |
133 (aset timer 2 (nth 1 time)) | |
134 (aset timer 3 (or (nth 2 time) 0)))) | |
135 | |
136 (defun timer-set-time-with-usecs (timer time usecs &optional delta) | |
137 "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
|
138 TIME must be in the internal format returned by, e.g., `current-time'. |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
139 If optional third argument DELTA is a non-zero integer, make the timer |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
140 fire repeatedly that many seconds apart." |
14449 | 141 (or (timerp timer) |
142 (error "Invalid timer")) | |
143 (aset timer 1 (car time)) | |
144 (aset timer 2 (if (consp (cdr time)) (car (cdr time)) (cdr time))) | |
145 (aset timer 3 usecs) | |
146 (aset timer 4 (and (numberp delta) (> delta 0) delta)) | |
147 timer) | |
148 | |
149 (defun timer-set-function (timer function &optional args) | |
150 "Make TIMER call FUNCTION with optional ARGS when triggering." | |
151 (or (timerp timer) | |
152 (error "Invalid timer")) | |
153 (aset timer 5 function) | |
154 (aset timer 6 args) | |
155 timer) | |
156 | |
157 (defun timer-activate (timer) | |
158 "Put TIMER on the list of active timers." | |
159 (if (and (timerp timer) | |
160 (integerp (aref timer 1)) | |
161 (integerp (aref timer 2)) | |
162 (integerp (aref timer 3)) | |
163 (aref timer 5)) | |
164 (let ((timers timer-list) | |
165 last) | |
166 ;; Skip all timers to trigger before the new one. | |
167 (while (and timers | |
168 (or (> (aref timer 1) (aref (car timers) 1)) | |
169 (and (= (aref timer 1) (aref (car timers) 1)) | |
170 (> (aref timer 2) (aref (car timers) 2))) | |
171 (and (= (aref timer 1) (aref (car timers) 1)) | |
172 (= (aref timer 2) (aref (car timers) 2)) | |
173 (> (aref timer 3) (aref (car timers) 3))))) | |
174 (setq last timers | |
175 timers (cdr timers))) | |
176 ;; Insert new timer after last which possibly means in front of queue. | |
177 (if last | |
178 (setcdr last (cons timer timers)) | |
179 (setq timer-list (cons timer timers))) | |
180 (aset timer 0 nil) | |
14632
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
181 (aset timer 7 nil) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
182 nil) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
183 (error "Invalid or uninitialized timer"))) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
184 |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
185 (defun timer-activate-when-idle (timer) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
186 "Arrange to activate TIMER whenever Emacs is next idle." |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
187 (if (and (timerp timer) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
188 (integerp (aref timer 1)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
189 (integerp (aref timer 2)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
190 (integerp (aref timer 3)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
191 (aref timer 5)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
192 (let ((timers timer-idle-list) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
193 last) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
194 ;; 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
|
195 (while (and timers |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
196 (or (> (aref timer 1) (aref (car timers) 1)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
197 (and (= (aref timer 1) (aref (car timers) 1)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
198 (> (aref timer 2) (aref (car timers) 2))) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
199 (and (= (aref timer 1) (aref (car timers) 1)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
200 (= (aref timer 2) (aref (car timers) 2)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
201 (> (aref timer 3) (aref (car timers) 3))))) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
202 (setq last timers |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
203 timers (cdr timers))) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
204 ;; 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
|
205 (if last |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
206 (setcdr last (cons timer timers)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
207 (setq timer-idle-list (cons timer timers))) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
208 (aset timer 0 t) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
209 (aset timer 7 t) |
14449 | 210 nil) |
211 (error "Invalid or uninitialized timer"))) | |
212 | |
213 (defalias 'disable-timeout 'cancel-timer) | |
214 (defun cancel-timer (timer) | |
215 "Remove TIMER from the list of active timers." | |
216 (or (timerp timer) | |
217 (error "Invalid timer")) | |
218 (setq timer-list (delq timer timer-list)) | |
14632
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
219 (setq timer-idle-list (delq timer timer-idle-list)) |
14449 | 220 nil) |
221 | |
222 (defun cancel-function-timers (function) | |
223 "Cancel all timers scheduled by `run-at-time' which would run FUNCTION." | |
224 (interactive "aCancel timers of function: ") | |
225 (let ((tail timer-list)) | |
226 (while tail | |
227 (if (eq (aref (car tail) 5) function) | |
228 (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
|
229 (setq tail (cdr tail)))) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
230 (let ((tail timer-idle-list)) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
231 (while tail |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
232 (if (eq (aref (car tail) 5) function) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
233 (setq timer-idle-list (delq (car tail) timer-idle-list))) |
14449 | 234 (setq tail (cdr tail))))) |
235 | |
236 ;; Set up the common handler for all timer events. Since the event has | |
237 ;; the timer as parameter we can still distinguish. Note that using | |
238 ;; special-event-map ensures that event timer events that arrive in the | |
239 ;; middle of a key sequence being entered are still handled correctly. | |
240 (define-key special-event-map [timer-event] 'timer-event-handler) | |
14705
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
241 |
14885
38e792454ee3
(timer-event-handler): Inhibit quitting.
Richard M. Stallman <rms@gnu.org>
parents:
14870
diff
changeset
|
242 ;; Record the last few events, for debugging. |
38e792454ee3
(timer-event-handler): Inhibit quitting.
Richard M. Stallman <rms@gnu.org>
parents:
14870
diff
changeset
|
243 (defvar timer-event-last-2 nil) |
38e792454ee3
(timer-event-handler): Inhibit quitting.
Richard M. Stallman <rms@gnu.org>
parents:
14870
diff
changeset
|
244 (defvar timer-event-last-1 nil) |
38e792454ee3
(timer-event-handler): Inhibit quitting.
Richard M. Stallman <rms@gnu.org>
parents:
14870
diff
changeset
|
245 (defvar timer-event-last nil) |
38e792454ee3
(timer-event-handler): Inhibit quitting.
Richard M. Stallman <rms@gnu.org>
parents:
14870
diff
changeset
|
246 |
14449 | 247 (defun timer-event-handler (event) |
248 "Call the handler for the timer in the event EVENT." | |
249 (interactive "e") | |
14885
38e792454ee3
(timer-event-handler): Inhibit quitting.
Richard M. Stallman <rms@gnu.org>
parents:
14870
diff
changeset
|
250 (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
|
251 (setq timer-event-last-1 timer-event-last) |
14923
20c5d04f8013
(timer-event-handler): Save copies of events too.
Richard M. Stallman <rms@gnu.org>
parents:
14885
diff
changeset
|
252 (setq timer-event-last (cons event (copy-sequence event))) |
14885
38e792454ee3
(timer-event-handler): Inhibit quitting.
Richard M. Stallman <rms@gnu.org>
parents:
14870
diff
changeset
|
253 (let ((inhibit-quit t) |
38e792454ee3
(timer-event-handler): Inhibit quitting.
Richard M. Stallman <rms@gnu.org>
parents:
14870
diff
changeset
|
254 (timer (car-safe (cdr-safe event)))) |
14449 | 255 (if (timerp timer) |
256 (progn | |
257 ;; Delete from queue. | |
258 (cancel-timer timer) | |
259 ;; Run handler | |
14870
c51cef393dae
(timer-set-time): Don't set usecs to nil.
Richard M. Stallman <rms@gnu.org>
parents:
14705
diff
changeset
|
260 (condition-case nil |
c51cef393dae
(timer-set-time): Don't set usecs to nil.
Richard M. Stallman <rms@gnu.org>
parents:
14705
diff
changeset
|
261 (apply (aref timer 5) (aref timer 6)) |
c51cef393dae
(timer-set-time): Don't set usecs to nil.
Richard M. Stallman <rms@gnu.org>
parents:
14705
diff
changeset
|
262 (error nil)) |
14449 | 263 ;; Re-schedule if requested. |
264 (if (aref timer 4) | |
14632
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
265 (if (aref timer 7) |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
266 (timer-activate-when-idle timer) |
14449 | 267 (timer-inc-time timer (aref timer 4) 0) |
268 (timer-activate timer)))) | |
269 (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
|
270 |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
271 ;; 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
|
272 (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
|
273 "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
|
274 (and (listp event) (eq (car event) 'timer-event))) |
14449 | 275 |
276 ;;;###autoload | |
277 (defun run-at-time (time repeat function &rest args) | |
16077 | 278 "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
|
279 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
|
280 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
|
281 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
|
282 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
|
283 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
|
284 The action is to call FUNCTION with arguments ARGS. |
14449 | 285 |
286 This function returns a timer object which you can use in `cancel-timer'." | |
287 (interactive "sRun at time: \nNRepeat interval: \naFunction: ") | |
288 | |
16085
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
289 (or (null repeat) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
290 (and (numberp repeat) (< 0 repeat)) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
291 (error "Invalid repetition interval")) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
292 |
14686 | 293 ;; Special case: nil means "now" and is useful when repeating. |
14449 | 294 (if (null time) |
295 (setq time (current-time))) | |
296 | |
16085
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
297 ;; 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
|
298 (if (and (eq time t) repeat) |
b215fb935fc3
(timer-next-integral-multiple-of-time): New function.
Paul Eggert <eggert@twinsun.com>
parents:
16077
diff
changeset
|
299 (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
|
300 |
14508
87b0d4b7577a
(run-at-time): Handle numbers as relative times in seconds, as the original
Roland McGrath <roland@gnu.org>
parents:
14470
diff
changeset
|
301 ;; 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
|
302 (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
|
303 (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
|
304 |
14449 | 305 ;; Handle relative times like "2 hours and 35 minutes" |
306 (if (stringp time) | |
307 (let ((secs (timer-duration time))) | |
308 (if secs | |
309 (setq time (timer-relative-time (current-time) secs))))) | |
310 | |
311 ;; Handle "11:23pm" and the like. Interpret it as meaning today | |
312 ;; 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
|
313 ;; already. (Though only Emacs hackers hack Emacs at that time.) |
14449 | 314 (if (stringp time) |
315 (progn | |
316 (require 'diary-lib) | |
317 (let ((hhmm (diary-entry-time time)) | |
318 (now (decode-time))) | |
319 (if (>= hhmm 0) | |
320 (setq time | |
321 (encode-time 0 (% hhmm 100) (/ hhmm 100) (nth 3 now) | |
322 (nth 4 now) (nth 5 now) (nth 8 now))))))) | |
323 | |
324 (or (consp time) | |
325 (error "Invalid time format")) | |
326 | |
327 (let ((timer (timer-create))) | |
328 (timer-set-time timer time repeat) | |
329 (timer-set-function timer function args) | |
330 (timer-activate timer) | |
331 timer)) | |
332 | |
333 ;;;###autoload | |
334 (defun run-with-timer (secs repeat function &rest args) | |
335 "Perform an action after a delay of SECS seconds. | |
336 Repeat the action every REPEAT seconds, if REPEAT is non-nil. | |
337 SECS and REPEAT may be integers or floating point numbers. | |
338 The action is to call FUNCTION with arguments ARGS. | |
339 | |
340 This function returns a timer object which you can use in `cancel-timer'." | |
341 (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
|
342 (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
|
343 |
f50f87fae914
(run-with-idle-timer): New function.
Karl Heuer <kwzh@gnu.org>
parents:
14509
diff
changeset
|
344 ;;;###autoload |
14449 | 345 (defun add-timeout (secs function object &optional repeat) |
346 "Add a timer to run SECS seconds from now, to call FUNCTION on OBJECT. | |
347 If REPEAT is non-nil, repeat the timer every REPEAT seconds. | |
348 This function is for compatibility; see also `run-with-timer'." | |
349 (run-with-timer secs repeat function object)) | |
350 | |
14705
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
351 ;;;###autoload |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
352 (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
|
353 "Perform an action the next time Emacs is idle for SECS seconds. |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
354 If REPEAT is non-nil, do this each time Emacs is idle for SECS seconds. |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
355 SECS may be an integer or a floating point number. |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
356 The action is to call FUNCTION with arguments ARGS. |
14449 | 357 |
14705
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
358 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
|
359 (interactive |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
360 (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
|
361 (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
|
362 (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
|
363 (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
|
364 (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
|
365 (timer-set-idle-time timer secs repeat) |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
366 (timer-activate-when-idle timer) |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
367 timer)) |
5e7b3fbecc8d
(timer-set-time, timer-set-time-with-usecs): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14686
diff
changeset
|
368 |
14449 | 369 (defun with-timeout-handler (tag) |
370 (throw tag 'timeout)) | |
371 | |
372 ;;;###autoload (put 'with-timeout 'lisp-indent-function 1) | |
373 | |
374 ;;;###autoload | |
375 (defmacro with-timeout (list &rest body) | |
376 "Run BODY, but if it doesn't finish in SECONDS seconds, give up. | |
377 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
|
378 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
|
379 (with-timeout (SECONDS TIMEOUT-FORMS...) BODY...) |
14449 | 380 The timeout is checked whenever Emacs waits for some kind of external |
381 event \(such as keyboard input, input from subprocesses, or a certain time); | |
382 if the program loops without waiting in any way, the timeout will not | |
383 be detected." | |
384 (let ((seconds (car list)) | |
385 (timeout-forms (cdr list))) | |
386 `(let ((with-timeout-tag (cons nil nil)) | |
387 with-timeout-value with-timeout-timer) | |
388 (if (catch with-timeout-tag | |
389 (progn | |
390 (setq with-timeout-timer | |
391 (run-with-timer ,seconds nil | |
392 'with-timeout-handler | |
393 with-timeout-tag)) | |
394 (setq with-timeout-value (progn . ,body)) | |
395 nil)) | |
396 (progn . ,timeout-forms) | |
397 (cancel-timer with-timeout-timer) | |
398 with-timeout-value)))) | |
399 | |
400 (defun y-or-n-p-with-timeout (prompt seconds default-value) | |
401 "Like (y-or-n-p PROMPT), with a timeout. | |
402 If the user does not answer after SECONDS seconds, return DEFAULT-VALUE." | |
403 (with-timeout (seconds default-value) | |
404 (y-or-n-p prompt))) | |
405 | |
406 (defvar timer-duration-words | |
407 (list (cons "microsec" 0.000001) | |
408 (cons "microsecond" 0.000001) | |
409 (cons "millisec" 0.001) | |
410 (cons "millisecond" 0.001) | |
411 (cons "sec" 1) | |
412 (cons "second" 1) | |
413 (cons "min" 60) | |
414 (cons "minute" 60) | |
415 (cons "hour" (* 60 60)) | |
416 (cons "day" (* 24 60 60)) | |
417 (cons "week" (* 7 24 60 60)) | |
418 (cons "fortnight" (* 14 24 60 60)) | |
419 (cons "month" (* 30 24 60 60)) ; Approximation | |
420 (cons "year" (* 365.25 24 60 60)) ; Approximation | |
421 ) | |
422 "Alist mapping temporal words to durations in seconds") | |
423 | |
424 (defun timer-duration (string) | |
425 "Return number of seconds specified by STRING, or nil if parsing fails." | |
426 (let ((secs 0) | |
427 (start 0) | |
428 (case-fold-search t)) | |
429 (while (string-match | |
430 "[ \t]*\\([0-9.]+\\)?[ \t]*\\([a-z]+[a-rt-z]\\)s?[ \t]*" | |
431 string start) | |
432 (let ((count (if (match-beginning 1) | |
433 (string-to-number (match-string 1 string)) | |
434 1)) | |
435 (itemsize (cdr (assoc (match-string 2 string) | |
436 timer-duration-words)))) | |
437 (if itemsize | |
438 (setq start (match-end 0) | |
439 secs (+ secs (* count itemsize))) | |
440 (setq secs nil | |
441 start (length string))))) | |
14870
c51cef393dae
(timer-set-time): Don't set usecs to nil.
Richard M. Stallman <rms@gnu.org>
parents:
14705
diff
changeset
|
442 (if (= start (length string)) |
c51cef393dae
(timer-set-time): Don't set usecs to nil.
Richard M. Stallman <rms@gnu.org>
parents:
14705
diff
changeset
|
443 secs |
c51cef393dae
(timer-set-time): Don't set usecs to nil.
Richard M. Stallman <rms@gnu.org>
parents:
14705
diff
changeset
|
444 (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
|
445 (string-to-number string))))) |
14449 | 446 |
447 (provide 'timer) | |
448 | |
449 ;;; timer.el ends here |