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