annotate lisp/emacs-lisp/timer.el @ 105950:e411fa8e0abf

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