Use `declare' in defmacros.
[bpt/emacs.git] / lisp / emacs-lisp / timer.el
CommitLineData
5e046f6d
JB
1;;; timer.el --- run a function with args at some time in future
2
5b669447 3;; Copyright (C) 1996, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
114f9c96 4;; 2009, 2010 Free Software Foundation, Inc.
5e046f6d
JB
5
6;; Maintainer: FSF
bd78fa1d 7;; Package: emacs
5e046f6d
JB
8
9;; This file is part of GNU Emacs.
10
d6cba7ae 11;; GNU Emacs is free software: you can redistribute it and/or modify
5e046f6d 12;; it under the terms of the GNU General Public License as published by
d6cba7ae
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
5e046f6d
JB
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
d6cba7ae 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
5e046f6d
JB
23
24;;; Commentary:
25
26;; This package gives you the capability to run Emacs Lisp commands at
27;; specified times in the future, either as one-shots or periodically.
28
29;;; Code:
30
31;; Layout of a timer vector:
32;; [triggered-p high-seconds low-seconds usecs repeat-delay
33;; function args idle-delay]
b8bd37f2
RS
34;; triggered-p is nil if the timer is active (waiting to be triggered),
35;; t if it is inactive ("already triggered", in theory)
5e046f6d 36
e0f0f3ef
SM
37(eval-when-compile (require 'cl))
38
39(defstruct (timer
40 (:constructor nil)
41 (:copier nil)
42 (:constructor timer-create ())
43 (:type vector)
44 (:conc-name timer--))
45 (triggered t)
46 high-seconds low-seconds usecs repeat-delay function args idle-delay)
5e046f6d
JB
47
48(defun timerp (object)
49 "Return t if OBJECT is a timer."
50 (and (vectorp object) (= (length object) 8)))
51
e0f0f3ef
SM
52;; Pseudo field `time'.
53(defun timer--time (timer)
54 (list (timer--high-seconds timer)
55 (timer--low-seconds timer)
56 (timer--usecs timer)))
57
58(defsetf timer--time
59 (lambda (timer time)
60 (or (timerp timer) (error "Invalid timer"))
61 (setf (timer--high-seconds timer) (pop time))
62 (setf (timer--low-seconds timer)
63 (if (consp time) (car time) time))
64 (setf (timer--usecs timer) (or (and (consp time) (consp (cdr time))
65 (cadr time))
66 0))))
67
68
5e046f6d
JB
69(defun timer-set-time (timer time &optional delta)
70 "Set the trigger time of TIMER to TIME.
71TIME must be in the internal format returned by, e.g., `current-time'.
72If optional third argument DELTA is a positive number, make the timer
73fire repeatedly that many seconds apart."
e0f0f3ef
SM
74 (setf (timer--time timer) time)
75 (setf (timer--repeat-delay timer) (and (numberp delta) (> delta 0) delta))
5e046f6d
JB
76 timer)
77
78(defun timer-set-idle-time (timer secs &optional repeat)
79 "Set the trigger idle time of TIMER to SECS.
1063efe8
CY
80SECS may be an integer, floating point number, or the internal
81time format (HIGH LOW USECS) returned by, e.g., `current-idle-time'.
5e046f6d
JB
82If optional third argument REPEAT is non-nil, make the timer
83fire each time Emacs is idle for that many seconds."
1063efe8 84 (if (consp secs)
e0f0f3ef
SM
85 (setf (timer--time timer) secs)
86 (setf (timer--time timer) '(0 0 0))
1063efe8 87 (timer-inc-time timer secs))
e0f0f3ef 88 (setf (timer--repeat-delay timer) repeat)
5e046f6d
JB
89 timer)
90
91(defun timer-next-integral-multiple-of-time (time secs)
92 "Yield the next value after TIME that is an integral multiple of SECS.
93More precisely, the next value, after TIME, that is an integral multiple
94of SECS seconds since the epoch. SECS may be a fraction."
95 (let ((time-base (ash 1 16)))
96 (if (fboundp 'atan)
97 ;; Use floating point, taking care to not lose precision.
98 (let* ((float-time-base (float time-base))
99 (million 1000000.0)
100 (time-usec (+ (* million
101 (+ (* float-time-base (nth 0 time))
102 (nth 1 time)))
103 (nth 2 time)))
104 (secs-usec (* million secs))
105 (mod-usec (mod time-usec secs-usec))
106 (next-usec (+ (- time-usec mod-usec) secs-usec))
107 (time-base-million (* float-time-base million)))
108 (list (floor next-usec time-base-million)
109 (floor (mod next-usec time-base-million) million)
110 (floor (mod next-usec million))))
111 ;; Floating point is not supported.
112 ;; Use integer arithmetic, avoiding overflow if possible.
113 (let* ((mod-sec (mod (+ (* (mod time-base secs)
114 (mod (nth 0 time) secs))
115 (nth 1 time))
116 secs))
117 (next-1-sec (+ (- (nth 1 time) mod-sec) secs)))
118 (list (+ (nth 0 time) (floor next-1-sec time-base))
119 (mod next-1-sec time-base)
120 0)))))
121
122(defun timer-relative-time (time secs &optional usecs)
123 "Advance TIME by SECS seconds and optionally USECS microseconds.
1063efe8 124SECS may be either an integer or a floating point number."
e0f0f3ef 125 ;; FIXME: we should just use (time-add time (list 0 secs usecs))
5e046f6d
JB
126 (let ((high (car time))
127 (low (if (consp (cdr time)) (nth 1 time) (cdr time)))
128 (micro (if (numberp (car-safe (cdr-safe (cdr time))))
129 (nth 2 time)
130 0)))
131 ;; Add
132 (if usecs (setq micro (+ micro usecs)))
133 (if (floatp secs)
134 (setq micro (+ micro (floor (* 1000000 (- secs (floor secs)))))))
135 (setq low (+ low (floor secs)))
136
137 ;; Normalize
138 ;; `/' rounds towards zero while `mod' returns a positive number,
139 ;; so we can't rely on (= a (+ (* 100 (/ a 100)) (mod a 100))).
140 (setq low (+ low (/ micro 1000000) (if (< micro 0) -1 0)))
141 (setq micro (mod micro 1000000))
142 (setq high (+ high (/ low 65536) (if (< low 0) -1 0)))
143 (setq low (logand low 65535))
144
145 (list high low (and (/= micro 0) micro))))
146
e0f0f3ef
SM
147(defun timer--time-less-p (t1 t2)
148 "Say whether time value T1 is less than time value T2."
149 ;; FIXME just use time-less-p.
150 (destructuring-bind (high1 low1 micro1) (timer--time t1)
151 (destructuring-bind (high2 low2 micro2) (timer--time t2)
152 (or (< high1 high2)
153 (and (= high1 high2)
154 (or (< low1 low2)
155 (and (= low1 low2)
156 (< micro1 micro2))))))))
157
5e046f6d
JB
158(defun timer-inc-time (timer secs &optional usecs)
159 "Increment the time set in TIMER by SECS seconds and USECS microseconds.
160SECS may be a fraction. If USECS is omitted, that means it is zero."
e0f0f3ef
SM
161 (setf (timer--time timer)
162 (timer-relative-time (timer--time timer) secs usecs)))
5e046f6d
JB
163
164(defun timer-set-time-with-usecs (timer time usecs &optional delta)
165 "Set the trigger time of TIMER to TIME plus USECS.
166TIME must be in the internal format returned by, e.g., `current-time'.
167The microsecond count from TIME is ignored, and USECS is used instead.
168If optional fourth argument DELTA is a positive number, make the timer
169fire repeatedly that many seconds apart."
e0f0f3ef
SM
170 (setf (timer--time timer) time)
171 (setf (timer--usecs timer) usecs)
172 (setf (timer--repeat-delay timer) (and (numberp delta) (> delta 0) delta))
5e046f6d
JB
173 timer)
174(make-obsolete 'timer-set-time-with-usecs
175 "use `timer-set-time' and `timer-inc-time' instead."
bf247b6e 176 "22.1")
5e046f6d
JB
177
178(defun timer-set-function (timer function &optional args)
179 "Make TIMER call FUNCTION with optional ARGS when triggering."
180 (or (timerp timer)
181 (error "Invalid timer"))
e0f0f3ef
SM
182 (setf (timer--function timer) function)
183 (setf (timer--args timer) args)
5e046f6d
JB
184 timer)
185\f
e0f0f3ef 186(defun timer--activate (timer &optional triggered-p reuse-cell idle)
5e046f6d 187 (if (and (timerp timer)
e0f0f3ef
SM
188 (integerp (timer--high-seconds timer))
189 (integerp (timer--low-seconds timer))
190 (integerp (timer--usecs timer))
191 (timer--function timer))
192 (let ((timers (if idle timer-idle-list timer-list))
5e046f6d
JB
193 last)
194 ;; Skip all timers to trigger before the new one.
e0f0f3ef 195 (while (and timers (timer--time-less-p (car timers) timer))
5e046f6d
JB
196 (setq last timers
197 timers (cdr timers)))
dbbee529
RS
198 (if reuse-cell
199 (progn
200 (setcar reuse-cell timer)
201 (setcdr reuse-cell timers))
202 (setq reuse-cell (cons timer timers)))
5e046f6d
JB
203 ;; Insert new timer after last which possibly means in front of queue.
204 (if last
dbbee529 205 (setcdr last reuse-cell)
e0f0f3ef
SM
206 (if idle
207 (setq timer-idle-list reuse-cell)
208 (setq timer-list reuse-cell)))
209 (setf (timer--triggered timer) triggered-p)
210 (setf (timer--idle-delay timer) idle)
5e046f6d
JB
211 nil)
212 (error "Invalid or uninitialized timer")))
213
e0f0f3ef
SM
214(defun timer-activate (timer &optional triggered-p reuse-cell idle)
215 "Put TIMER on the list of active timers.
216
217If TRIGGERED-P is t, that means to make the timer inactive
218\(put it on the list, but mark it as already triggered).
219To remove from the list, use `cancel-timer'.
220
221REUSE-CELL, if non-nil, is a cons cell to reuse instead
222of allocating a new one."
223 (timer--activate timer triggered-p reuse-cell nil))
224
dbbee529 225(defun timer-activate-when-idle (timer &optional dont-wait reuse-cell)
5e046f6d
JB
226 "Arrange to activate TIMER whenever Emacs is next idle.
227If optional argument DONT-WAIT is non-nil, then enable the
228timer to activate immediately, or at the right time, if Emacs
dbbee529
RS
229is already idle.
230
231REUSE-CELL, if non-nil, is a cons cell to reuse instead
232of allocating a new one."
e0f0f3ef 233 (timer--activate timer (not dont-wait) reuse-cell 'idle))
5e046f6d 234
5e046f6d 235(defalias 'disable-timeout 'cancel-timer)
f31b1257 236
5e046f6d
JB
237(defun cancel-timer (timer)
238 "Remove TIMER from the list of active timers."
239 (or (timerp timer)
240 (error "Invalid timer"))
241 (setq timer-list (delq timer timer-list))
242 (setq timer-idle-list (delq timer timer-idle-list))
243 nil)
244
dbbee529 245(defun cancel-timer-internal (timer)
b8bd37f2
RS
246 "Remove TIMER from the list of active timers or idle timers.
247Only to be used in this file. It returns the cons cell
248that was removed from the timer list."
dbbee529
RS
249 (let ((cell1 (memq timer timer-list))
250 (cell2 (memq timer timer-idle-list)))
251 (if cell1
252 (setq timer-list (delq timer timer-list)))
253 (if cell2
254 (setq timer-idle-list (delq timer timer-idle-list)))
255 (or cell1 cell2)))
256
5e046f6d 257(defun cancel-function-timers (function)
b8bd37f2
RS
258 "Cancel all timers which would run FUNCTION.
259This affects ordinary timers such as are scheduled by `run-at-time',
260and idle timers such as are scheduled by `run-with-idle-timer'."
5e046f6d 261 (interactive "aCancel timers of function: ")
e0f0f3ef
SM
262 (dolist (timer timer-list)
263 (if (eq (timer--function timer) function)
264 (setq timer-list (delq timer timer-list))))
265 (dolist (timer timer-idle-list)
266 (if (eq (timer--function timer) function)
267 (setq timer-idle-list (delq timer timer-idle-list)))))
5e046f6d
JB
268\f
269;; Record the last few events, for debugging.
b8bd37f2
RS
270(defvar timer-event-last nil
271 "Last timer that was run.")
272(defvar timer-event-last-1 nil
273 "Next-to-last timer that was run.")
274(defvar timer-event-last-2 nil
275 "Third-to-last timer that was run.")
5e046f6d
JB
276
277(defvar timer-max-repeats 10
e00c3f6f
RS
278 "*Maximum number of times to repeat a timer, if many repeats are delayed.
279Timer invocations can be delayed because Emacs is suspended or busy,
280or because the system's time changes. If such an occurrence makes it
281appear that many invocations are overdue, this variable controls
282how many will really happen.")
5e046f6d
JB
283
284(defun timer-until (timer time)
285 "Calculate number of seconds from when TIMER will run, until TIME.
286TIMER is a timer, and stands for the time when its next repeat is scheduled.
287TIME is a time-list."
5b669447 288 ;; FIXME: (float-time (time-subtract (timer--time timer) time))
e0f0f3ef
SM
289 (let ((high (- (car time) (timer--high-seconds timer)))
290 (low (- (nth 1 time) (timer--low-seconds timer))))
5e046f6d
JB
291 (+ low (* high 65536))))
292
293(defun timer-event-handler (timer)
294 "Call the handler for the timer TIMER.
295This function is called, by name, directly by the C code."
296 (setq timer-event-last-2 timer-event-last-1)
297 (setq timer-event-last-1 timer-event-last)
298 (setq timer-event-last timer)
299 (let ((inhibit-quit t))
300 (if (timerp timer)
dbbee529
RS
301 (let (retrigger cell)
302 ;; Delete from queue. Record the cons cell that was used.
303 (setq cell (cancel-timer-internal timer))
5e046f6d 304 ;; Re-schedule if requested.
e0f0f3ef
SM
305 (if (timer--repeat-delay timer)
306 (if (timer--idle-delay timer)
dbbee529 307 (timer-activate-when-idle timer nil cell)
e0f0f3ef 308 (timer-inc-time timer (timer--repeat-delay timer) 0)
5e046f6d
JB
309 ;; If real time has jumped forward,
310 ;; perhaps because Emacs was suspended for a long time,
311 ;; limit how many times things get repeated.
312 (if (and (numberp timer-max-repeats)
313 (< 0 (timer-until timer (current-time))))
314 (let ((repeats (/ (timer-until timer (current-time))
e0f0f3ef 315 (timer--repeat-delay timer))))
5e046f6d 316 (if (> repeats timer-max-repeats)
e0f0f3ef
SM
317 (timer-inc-time timer (* (timer--repeat-delay timer)
318 repeats)))))
dbbee529 319 (timer-activate timer t cell)
6160f933 320 (setq retrigger t)))
5e046f6d
JB
321 ;; Run handler.
322 ;; We do this after rescheduling so that the handler function
323 ;; can cancel its own timer successfully with cancel-timer.
324 (condition-case nil
0798a8d8
SM
325 ;; Timer functions should not change the current buffer.
326 ;; If they do, all kinds of nasty surprises can happen,
327 ;; and it can be hellish to track down their source.
328 (save-current-buffer
329 (apply (timer--function timer) (timer--args timer)))
6160f933
KS
330 (error nil))
331 (if retrigger
e0f0f3ef 332 (setf (timer--triggered timer) nil)))
5e046f6d
JB
333 (error "Bogus timer event"))))
334
335;; This function is incompatible with the one in levents.el.
336(defun timeout-event-p (event)
337 "Non-nil if EVENT is a timeout event."
338 (and (listp event) (eq (car event) 'timer-event)))
339\f
153ef845 340
5cec3056 341(declare-function diary-entry-time "diary-lib" (s))
153ef845 342
5e046f6d
JB
343(defun run-at-time (time repeat function &rest args)
344 "Perform an action at time TIME.
345Repeat the action every REPEAT seconds, if REPEAT is non-nil.
b9be25fd
GM
346TIME should be one of: a string giving an absolute time like
347\"11:23pm\" (the acceptable formats are those recognized by
348`diary-entry-time'; note that such times are interpreted as times
349today, even if in the past); a string giving a relative time like
350\"2 hours 35 minutes\" (the acceptable formats are those
351recognized by `timer-duration'); nil meaning now; a number of
352seconds from now; a value from `encode-time'; or t (with non-nil
353REPEAT) meaning the next integral multiple of REPEAT. REPEAT may
354be an integer or floating point number. The action is to call
355FUNCTION with arguments ARGS.
5e046f6d
JB
356
357This function returns a timer object which you can use in `cancel-timer'."
358 (interactive "sRun at time: \nNRepeat interval: \naFunction: ")
359
360 (or (null repeat)
361 (and (numberp repeat) (< 0 repeat))
362 (error "Invalid repetition interval"))
363
364 ;; Special case: nil means "now" and is useful when repeating.
365 (if (null time)
366 (setq time (current-time)))
367
368 ;; Special case: t means the next integral multiple of REPEAT.
369 (if (and (eq time t) repeat)
370 (setq time (timer-next-integral-multiple-of-time (current-time) repeat)))
371
372 ;; Handle numbers as relative times in seconds.
373 (if (numberp time)
374 (setq time (timer-relative-time (current-time) time)))
375
b9be25fd 376 ;; Handle relative times like "2 hours 35 minutes"
5e046f6d
JB
377 (if (stringp time)
378 (let ((secs (timer-duration time)))
379 (if secs
380 (setq time (timer-relative-time (current-time) secs)))))
381
382 ;; Handle "11:23pm" and the like. Interpret it as meaning today
383 ;; which admittedly is rather stupid if we have passed that time
384 ;; already. (Though only Emacs hackers hack Emacs at that time.)
385 (if (stringp time)
386 (progn
387 (require 'diary-lib)
388 (let ((hhmm (diary-entry-time time))
389 (now (decode-time)))
390 (if (>= hhmm 0)
391 (setq time
392 (encode-time 0 (% hhmm 100) (/ hhmm 100) (nth 3 now)
393 (nth 4 now) (nth 5 now) (nth 8 now)))))))
394
395 (or (consp time)
396 (error "Invalid time format"))
397
398 (let ((timer (timer-create)))
399 (timer-set-time timer time repeat)
400 (timer-set-function timer function args)
401 (timer-activate timer)
402 timer))
403
5e046f6d
JB
404(defun run-with-timer (secs repeat function &rest args)
405 "Perform an action after a delay of SECS seconds.
406Repeat the action every REPEAT seconds, if REPEAT is non-nil.
407SECS and REPEAT may be integers or floating point numbers.
408The action is to call FUNCTION with arguments ARGS.
409
410This function returns a timer object which you can use in `cancel-timer'."
411 (interactive "sRun after delay (seconds): \nNRepeat interval: \naFunction: ")
412 (apply 'run-at-time secs repeat function args))
413
5e046f6d
JB
414(defun add-timeout (secs function object &optional repeat)
415 "Add a timer to run SECS seconds from now, to call FUNCTION on OBJECT.
416If REPEAT is non-nil, repeat the timer every REPEAT seconds.
417This function is for compatibility; see also `run-with-timer'."
418 (run-with-timer secs repeat function object))
419
5e046f6d
JB
420(defun run-with-idle-timer (secs repeat function &rest args)
421 "Perform an action the next time Emacs is idle for SECS seconds.
422The action is to call FUNCTION with arguments ARGS.
1063efe8
CY
423SECS may be an integer, a floating point number, or the internal
424time format (HIGH LOW USECS) returned by, e.g., `current-idle-time'.
171d328e
RS
425If Emacs is currently idle, and has been idle for N seconds (N < SECS),
426then it will call FUNCTION in SECS - N seconds from now.
5e046f6d
JB
427
428If REPEAT is non-nil, do the action each time Emacs has been idle for
429exactly SECS seconds (that is, only once for each time Emacs becomes idle).
430
431This function returns a timer object which you can use in `cancel-timer'."
432 (interactive
433 (list (read-from-minibuffer "Run after idle (seconds): " nil nil t)
434 (y-or-n-p "Repeat each time Emacs is idle? ")
435 (intern (completing-read "Function: " obarray 'fboundp t))))
436 (let ((timer (timer-create)))
437 (timer-set-function timer function args)
438 (timer-set-idle-time timer secs repeat)
171d328e 439 (timer-activate-when-idle timer t)
5e046f6d
JB
440 timer))
441\f
442(defun with-timeout-handler (tag)
b8bd37f2 443 "This is the timer function used for the timer made by `with-timeout'."
5e046f6d
JB
444 (throw tag 'timeout))
445
89c020e8
RS
446(defvar with-timeout-timers nil
447 "List of all timers used by currently pending `with-timeout' calls.")
448
5e046f6d
JB
449(defmacro with-timeout (list &rest body)
450 "Run BODY, but if it doesn't finish in SECONDS seconds, give up.
451If we give up, we run the TIMEOUT-FORMS and return the value of the last one.
5e046f6d 452The timeout is checked whenever Emacs waits for some kind of external
7452fadc 453event (such as keyboard input, input from subprocesses, or a certain time);
5e046f6d 454if the program loops without waiting in any way, the timeout will not
7452fadc
JB
455be detected.
456\n(fn (SECONDS TIMEOUT-FORMS...) BODY)"
f291fe60 457 (declare (indent 1))
5e046f6d
JB
458 (let ((seconds (car list))
459 (timeout-forms (cdr list)))
460 `(let ((with-timeout-tag (cons nil nil))
89c020e8
RS
461 with-timeout-value with-timeout-timer
462 (with-timeout-timers with-timeout-timers))
5e046f6d
JB
463 (if (catch with-timeout-tag
464 (progn
465 (setq with-timeout-timer
466 (run-with-timer ,seconds nil
467 'with-timeout-handler
468 with-timeout-tag))
89c020e8 469 (push with-timeout-timer with-timeout-timers)
5e046f6d
JB
470 (setq with-timeout-value (progn . ,body))
471 nil))
472 (progn . ,timeout-forms)
473 (cancel-timer with-timeout-timer)
474 with-timeout-value))))
475
89c020e8
RS
476(defun with-timeout-suspend ()
477 "Stop the clock for `with-timeout'. Used by debuggers.
478The idea is that the time you spend in the debugger should not
479count against these timeouts.
480
481The value is a list that the debugger can pass to `with-timeout-unsuspend'
482when it exits, to make these timers start counting again."
483 (mapcar (lambda (timer)
484 (cancel-timer timer)
e0f0f3ef 485 (list timer (time-subtract (timer--time timer) (current-time))))
89c020e8
RS
486 with-timeout-timers))
487
488(defun with-timeout-unsuspend (timer-spec-list)
489 "Restart the clock for `with-timeout'.
490The argument should be a value previously returned by `with-timeout-suspend'."
491 (dolist (elt timer-spec-list)
492 (let ((timer (car elt))
493 (delay (cadr elt)))
64ba814f 494 (timer-set-time timer (time-add (current-time) delay))
89c020e8
RS
495 (timer-activate timer))))
496
5e046f6d
JB
497(defun y-or-n-p-with-timeout (prompt seconds default-value)
498 "Like (y-or-n-p PROMPT), with a timeout.
499If the user does not answer after SECONDS seconds, return DEFAULT-VALUE."
500 (with-timeout (seconds default-value)
501 (y-or-n-p prompt)))
502\f
2f7f4bee 503(defconst timer-duration-words
5e046f6d
JB
504 (list (cons "microsec" 0.000001)
505 (cons "microsecond" 0.000001)
506 (cons "millisec" 0.001)
507 (cons "millisecond" 0.001)
508 (cons "sec" 1)
509 (cons "second" 1)
510 (cons "min" 60)
511 (cons "minute" 60)
512 (cons "hour" (* 60 60))
513 (cons "day" (* 24 60 60))
514 (cons "week" (* 7 24 60 60))
515 (cons "fortnight" (* 14 24 60 60))
516 (cons "month" (* 30 24 60 60)) ; Approximation
517 (cons "year" (* 365.25 24 60 60)) ; Approximation
518 )
64ba814f 519 "Alist mapping temporal words to durations in seconds.")
5e046f6d
JB
520
521(defun timer-duration (string)
522 "Return number of seconds specified by STRING, or nil if parsing fails."
523 (let ((secs 0)
524 (start 0)
525 (case-fold-search t))
526 (while (string-match
527 "[ \t]*\\([0-9.]+\\)?[ \t]*\\([a-z]+[a-rt-z]\\)s?[ \t]*"
528 string start)
529 (let ((count (if (match-beginning 1)
530 (string-to-number (match-string 1 string))
531 1))
532 (itemsize (cdr (assoc (match-string 2 string)
533 timer-duration-words))))
534 (if itemsize
535 (setq start (match-end 0)
536 secs (+ secs (* count itemsize)))
537 (setq secs nil
538 start (length string)))))
539 (if (= start (length string))
540 secs
64ba814f 541 (if (string-match-p "\\`[0-9.]+\\'" string)
5e046f6d
JB
542 (string-to-number string)))))
543\f
544(provide 'timer)
545
e0f0f3ef 546;; arch-tag: b1a9237b-7787-4382-9e46-8f2c3b3273e0
5e046f6d 547;;; timer.el ends here