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