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