annotate lisp/timer.el @ 33576:d8c8c119da4d

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