annotate lisp/itimer.el @ 351:1057b58c392a r21-1-5

Import from CVS: tag r21-1-5
author cvs
date Mon, 13 Aug 2007 10:54:53 +0200
parents 57709be46d1b
children 8e84bee8ddd0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
267
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
1 ;;; Interval timers for GNU Emacs
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
2 ;;; Copyright (C) 1988, 1991, 1993, 1997, 1998 Kyle E. Jones
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
3 ;;;
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
4 ;;; This program is free software; you can redistribute it and/or modify
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
5 ;;; it under the terms of the GNU General Public License as published by
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
6 ;;; the Free Software Foundation; either version 2, or (at your option)
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
7 ;;; any later version.
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
8 ;;;
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
9 ;;; This program is distributed in the hope that it will be useful,
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
12 ;;; GNU General Public License for more details.
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
13 ;;;
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
14 ;;; A copy of the GNU General Public License can be obtained from this
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
15 ;;; program's author (send electronic mail to kyle@uunet.uu.net) or from
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
16 ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
17 ;;; 02139, USA.
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
18 ;;;
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
19 ;;; Send bug reports to kyle_jones@wonderworks.com
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
20
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
21 (provide 'itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
22
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
23 ;; `itimer' feature means Emacs-Lisp programmers get:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
24 ;; itimerp
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
25 ;; itimer-live-p
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
26 ;; itimer-value
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
27 ;; itimer-restart
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
28 ;; itimer-function
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
29 ;; itimer-uses-arguments
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
30 ;; itimer-function-arguments
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
31 ;; set-itimer-value
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
32 ;; set-itimer-restart
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
33 ;; set-itimer-function
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
34 ;; set-itimer-uses-arguments
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
35 ;; set-itimer-function-arguments
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
36 ;; get-itimer
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
37 ;; start-itimer
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
38 ;; read-itimer
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
39 ;; delete-itimer
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
40 ;; activate-itimer
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
41 ;;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
42 ;; Interactive users get these commands:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
43 ;; edit-itimers
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
44 ;; list-itimers
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
45 ;; start-itimer
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
46 ;;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
47 ;; See the doc strings of these functions for more information.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
48
267
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
49 (defvar itimer-version "1.07"
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
50 "Version number of the itimer package.")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
51
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
52 (defvar itimer-list nil
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
53 "List of all active itimers.")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
54
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
55 (defvar itimer-process nil
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
56 "Process that drives all itimers, if a subprocess is being used.")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
57
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
58 (defvar itimer-timer nil
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
59 "Emacs internal timer that drives the itimer system, if a subprocess
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
60 is not being used to drive the system.")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
61
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
62 (defvar itimer-timer-last-wakeup nil
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
63 "The time the timer driver function last ran.")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
64
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
65 (defvar itimer-short-interval (if (featurep 'lisp-float-type) 1e-3 1)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
66 "Interval used for scheduling an event a very short time in the future.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
67 Used internally to make the scheduler wake up early.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
68 Unit is seconds.")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
69
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
70 ;; This value is maintained internally; it does not determine
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
71 ;; itimer granularity. Itimer granularity is 1 second if your
267
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
72 ;; Emacs doesn't support floats or your system doesn't have a
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
73 ;; clock with microsecond granularity. Otherwise granularity is
267
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
74 ;; to the microsecond, although you can't possibly get timers to be
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
75 ;; executed with this kind of accuracy in practice. There will
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
76 ;; be delays due to system and Emacs internal activity that delay
267
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
77 ;; dealing with synchronous events and process output.
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
78 (defvar itimer-next-wakeup itimer-short-interval
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
79 "Itimer process will wakeup to service running itimers within this
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
80 many seconds.")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
81
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
82 (defvar itimer-edit-map nil
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
83 "Keymap used when in Itimer Edit mode.")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
84
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
85 (if itimer-edit-map
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
86 ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
87 (setq itimer-edit-map (make-sparse-keymap))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
88 (define-key itimer-edit-map "s" 'itimer-edit-set-field)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
89 (define-key itimer-edit-map "d" 'itimer-edit-delete-itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
90 (define-key itimer-edit-map "q" 'itimer-edit-quit)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
91 (define-key itimer-edit-map "\t" 'itimer-edit-next-field)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
92 (define-key itimer-edit-map " " 'next-line)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
93 (define-key itimer-edit-map "n" 'next-line)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
94 (define-key itimer-edit-map "p" 'previous-line)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
95 (define-key itimer-edit-map "\C-?" 'itimer-edit-previous-field)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
96 (define-key itimer-edit-map "x" 'start-itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
97 (define-key itimer-edit-map "?" 'itimer-edit-help))
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
98
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
99 (defvar itimer-inside-driver nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
100
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
101 (defvar itimer-edit-start-marker nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
102
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
103 ;; macros must come first... or byte-compile'd code will throw back its
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
104 ;; head and scream.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
105
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
106 (defmacro itimer-decrement (variable)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
107 (list 'setq variable (list '1- variable)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
108
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
109 (defmacro itimer-increment (variable)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
110 (list 'setq variable (list '1+ variable)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
111
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
112 (defmacro itimer-signum (n)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
113 (list 'if (list '> n 0) 1
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
114 (list 'if (list 'zerop n) 0 -1)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
115
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
116 ;; Itimer access functions should behave as if they were subrs. These
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
117 ;; macros are used to check the arguments to the itimer functions and
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
118 ;; signal errors appropriately if the arguments are not valid.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
119
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
120 (defmacro check-itimer (var)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
121 "If VAR is not bound to an itimer, signal wrong-type-argument.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
122 This is a macro."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
123 (list 'setq var
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
124 (list 'if (list 'itimerp var) var
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
125 (list 'signal ''wrong-type-argument
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
126 (list 'list ''itimerp var)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
127
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
128 (defmacro check-itimer-coerce-string (var)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
129 "If VAR is not bound to a string, look up the itimer that it names and
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
130 bind VAR to it. Otherwise, if VAR is not bound to an itimer, signal
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
131 wrong-type-argument. This is a macro."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
132 (list 'setq var
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
133 (list 'cond
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
134 (list (list 'itimerp var) var)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
135 (list (list 'stringp var) (list 'get-itimer var))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
136 (list t (list 'signal ''wrong-type-argument
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
137 (list 'list ''string-or-itimer-p var))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
138
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
139 (defmacro check-nonnegative-number (var)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
140 "If VAR is not bound to a number, signal wrong-type-argument.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
141 If VAR is not bound to a positive number, signal args-out-of-range.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
142 This is a macro."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
143 (list 'setq var
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
144 (list 'if (list 'not (list 'numberp var))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
145 (list 'signal ''wrong-type-argument
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
146 (list 'list ''natnump var))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
147 (list 'if (list '< var 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
148 (list 'signal ''args-out-of-range (list 'list var))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
149 var))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
150
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
151 (defmacro check-string (var)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
152 "If VAR is not bound to a string, signal wrong-type-argument.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
153 This is a macro."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
154 (list 'setq var
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
155 (list 'if (list 'stringp var) var
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
156 (list 'signal ''wrong-type-argument
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
157 (list 'list ''stringp var)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
158
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
159 ;; Functions to access and modify itimer attributes.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
160
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
161 (defun itimerp (obj)
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
162 "Return t if OBJ is an itimer."
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
163 (and (consp obj) (eq (length obj) 8)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
164
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
165 (defun itimer-live-p (obj)
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
166 "Return non-nil if OBJ is an itimer and is active.
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
167 ``Active'' means Emacs will run it when it expires.
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
168 `activate-timer' must be called on an itimer to make it active.
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
169 Itimers started with `start-itimer' are automatically active."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
170 (and (itimerp obj) (memq obj itimer-list)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
171
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
172 (defun itimer-name (itimer)
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
173 "Return the name of ITIMER."
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
174 (check-itimer itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
175 (car itimer))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
176
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
177 (defun itimer-value (itimer)
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
178 "Return the number of seconds until ITIMER expires."
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
179 (check-itimer itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
180 (nth 1 itimer))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
181
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
182 (defun itimer-restart (itimer)
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
183 "Return the value to which ITIMER will be set at restart.
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
184 Return nil if this itimer doesn't restart."
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
185 (check-itimer itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
186 (nth 2 itimer))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
187
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
188 (defun itimer-function (itimer)
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
189 "Return the function of ITIMER.
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
190 This function is called each time ITIMER expires."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
191 (check-itimer itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
192 (nth 3 itimer))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
193
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
194 (defun itimer-is-idle (itimer)
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
195 "Return non-nil if ITIMER is an idle timer.
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
196 Normal timers expire after a set interval. Idle timers expire
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
197 only after Emacs has been idle for a specific interval.
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
198 ``Idle'' means no command events occur within the interval."
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
199 (check-itimer itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
200 (nth 4 itimer))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
201
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
202 (defun itimer-uses-arguments (itimer)
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
203 "Return non-nil if the function of ITIMER will be called with arguments.
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
204 ITIMER's function is called with the arguments each time ITIMER expires.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
205 The arguments themselves are retrievable with `itimer-function-arguments'."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
206 (check-itimer itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
207 (nth 5 itimer))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
208
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
209 (defun itimer-function-arguments (itimer)
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
210 "Return the function arguments of ITIMER as a list.
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
211 ITIMER's function is called with these argument each time ITIMER expires."
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
212 (check-itimer itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
213 (nth 6 itimer))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
214
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
215 (defun itimer-recorded-run-time (itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
216 (check-itimer itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
217 (nth 7 itimer))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
218
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
219 (defun set-itimer-value (itimer value)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
220 "Set the timeout value of ITIMER to be VALUE.
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
221 Itimer will expire in this many seconds.
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
222 If your version of Emacs supports floating point numbers then
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
223 VALUE can be a floating point number. Otherwise it
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
224 must be an integer.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
225 Returns VALUE."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
226 (check-itimer itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
227 (check-nonnegative-number value)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
228 (let ((inhibit-quit t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
229 ;; If the itimer is in the active list, and under the new
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
230 ;; timeout value would expire before we would normally
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
231 ;; wakeup, wakeup now and recompute a new wakeup time.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
232 (or (and (< value itimer-next-wakeup)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
233 (and (itimer-name itimer) (get-itimer (itimer-name itimer)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
234 (progn (itimer-driver-wakeup)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
235 (setcar (cdr itimer) value)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
236 (itimer-driver-wakeup)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
237 t ))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
238 (setcar (cdr itimer) value))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
239 value))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
240
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
241 ;; Same as set-itimer-value but does not wakeup the driver.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
242 ;; Only should be used by the drivers when processing expired timers.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
243 (defun set-itimer-value-internal (itimer value)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
244 (check-itimer itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
245 (check-nonnegative-number value)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
246 (setcar (cdr itimer) value))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
247
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
248 (defun set-itimer-restart (itimer restart)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
249 "Set the restart value of ITIMER to be RESTART.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
250 If RESTART is nil, ITIMER will not restart when it expires.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
251 If your version of Emacs supports floating point numbers then
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
252 RESTART can be a floating point number. Otherwise it
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
253 must be an integer.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
254 Returns RESTART."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
255 (check-itimer itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
256 (if restart (check-nonnegative-number restart))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
257 (setcar (cdr (cdr itimer)) restart))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
258
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
259 (defun set-itimer-function (itimer function)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
260 "Set the function of ITIMER to be FUNCTION.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
261 FUNCTION will be called when itimer expires.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
262 Returns FUNCTION."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
263 (check-itimer itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
264 (setcar (nthcdr 3 itimer) function))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
265
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
266 (defun set-itimer-is-idle (itimer flag)
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
267 "Set flag that says whether ITIMER is an idle timer.
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
268 If FLAG is non-nil, then ITIMER will be considered an idle timer.
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
269 Returns FLAG."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
270 (check-itimer itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
271 (setcar (nthcdr 4 itimer) flag))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
272
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
273 (defun set-itimer-uses-arguments (itimer flag)
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
274 "Set flag that says whether the function of ITIMER is called with arguments.
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
275 If FLAG is non-nil, then the function will be called with one argument,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
276 otherwise the function will be called with no arguments.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
277 Returns FLAG."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
278 (check-itimer itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
279 (setcar (nthcdr 5 itimer) flag))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
280
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
281 (defun set-itimer-function-arguments (itimer &optional arguments)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
282 "Set the function arguments of ITIMER to be ARGUMENTS.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
283 The function of ITIMER will be called with ARGUMENTS when itimer expires.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
284 Returns ARGUMENTS."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
285 (check-itimer itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
286 (setcar (nthcdr 6 itimer) arguments))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
287
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
288 (defun set-itimer-recorded-run-time (itimer time)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
289 (check-itimer itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
290 (setcar (nthcdr 7 itimer) time))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
291
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
292 (defun get-itimer (name)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
293 "Return itimer named NAME, or nil if there is none."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
294 (check-string name)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
295 (assoc name itimer-list))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
296
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
297 (defun read-itimer (prompt &optional initial-input)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
298 "Read the name of an itimer from the minibuffer and return the itimer
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
299 associated with that name. The user is prompted with PROMPT.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
300 Optional second arg INITIAL-INPUT non-nil is inserted into the
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
301 minibuffer as initial user input."
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
302 (get-itimer (completing-read prompt itimer-list nil 'confirm initial-input)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
303
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
304 (defun delete-itimer (itimer)
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
305 "Delete ITIMER. ITIMER may be an itimer or the name of one."
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
306 (check-itimer-coerce-string itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
307 (setq itimer-list (delq itimer itimer-list)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
308
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
309 (defun start-itimer (name function value &optional restart
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
310 is-idle with-args &rest function-arguments)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
311 "Start an itimer.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
312 Arguments are
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
313 NAME, FUNCTION, VALUE &optional RESTART, IS-IDLE, WITH-ARGS, &rest FUNCTION-ARGUMENTS.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
314 NAME is an identifier for the itimer. It must be a string. If an itimer
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
315 already exists with this name, NAME will be modified slightly to make
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
316 it unique.
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
317 FUNCTION should be a function (or symbol naming one). It
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
318 will be called each time the itimer expires with arguments of
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
319 FUNCTION-ARGUMENTS. The function can access the itimer that
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
320 invoked it through the variable `current-itimer'. If WITH-ARGS
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
321 is nil then FUNCTION is called with no arguments. This is for
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
322 backward compatibility with older versions of the itimer
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
323 package which always called FUNCTION with no arguments.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
324 VALUE is the number of seconds until this itimer expires.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
325 If your version of Emacs supports floating point numbers then
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
326 VALUE can be a floating point number. Otherwise it
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
327 must be an integer.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
328 Optional fourth arg RESTART non-nil means that this itimer should be
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
329 restarted automatically after its function is called. Normally an itimer
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
330 is deleted at expiration after its function has returned.
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
331 If non-nil, RESTART should be a number indicating the value at which
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
332 the itimer should be set at restart time.
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
333 Optional fifth arg IS-IDLE specifies if this is an idle timer.
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
334 Normal timers expire after a set interval. Idle timers expire
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
335 only after Emacs has been idle for specific interval.
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 267
diff changeset
336 ``Idle'' means no command events occur within the interval.
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
337 Returns the newly created itimer."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
338 (interactive
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
339 (list (completing-read "Start itimer: " itimer-list)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
340 (read (completing-read "Itimer function: " obarray 'fboundp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
341 (let (value)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
342 (while (or (not (numberp value)) (< value 0))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
343 (setq value (read-from-minibuffer "Itimer value: " nil nil t)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
344 value)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
345 (let ((restart t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
346 (while (and restart (or (not (numberp restart)) (< restart 0)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
347 (setq restart (read-from-minibuffer "Itimer restart: "
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
348 nil nil t)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
349 restart)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
350 ;; hard to imagine the user specifying these interactively
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
351 nil
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
352 nil ))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
353 (check-string name)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
354 (check-nonnegative-number value)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
355 (if restart (check-nonnegative-number restart))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
356 ;; Make proposed itimer name unique if it's not already.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
357 (let ((oname name)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
358 (num 2))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
359 (while (get-itimer name)
286
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 272
diff changeset
360 (setq name (format "%s<%d>" oname num))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
361 (itimer-increment num)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
362 (activate-itimer (list name value restart function is-idle
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
363 with-args function-arguments (list 0 0 0)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
364 (car itimer-list))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
365
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
366 (defun make-itimer ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
367 "Create an unactivated itimer.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
368 The itimer will not begin running until activated with `activate-itimer'.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
369 Set the itimer's expire interval with `set-itimer-value'.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
370 Set the itimer's function interval with `set-itimer-function'.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
371 Once this is done, the timer can be activated."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
372 (list nil 0 nil 'ignore nil nil nil (list 0 0 0)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
373
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
374 (defun activate-itimer (itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
375 "Activate ITIMER, which was previously created with `make-itimer'.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
376 ITIMER will be added to the global list of running itimers,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
377 its FUNCTION will be called when it expires, and so on."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
378 (check-itimer itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
379 (if (memq itimer itimer-list)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
380 (error "itimer already activated"))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
381 (if (not (numberp (itimer-value itimer)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
382 (error "itimer timeout value not a number: %s" (itimer-value itimer)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
383 (if (<= (itimer-value itimer) 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
384 (error "itimer timeout value not positive: %s" (itimer-value itimer)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
385 ;; If there's no itimer driver/process, start one now.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
386 ;; Otherwise wake up the itimer driver so that seconds slept before
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
387 ;; the new itimer is created won't be counted against it.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
388 (if (or itimer-process itimer-timer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
389 (itimer-driver-wakeup)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
390 (itimer-driver-start))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
391 ;; Roll a unique name for the timer if it doesn't have a name
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
392 ;; already.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
393 (if (not (stringp (car itimer)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
394 (let ((name "itimer-0")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
395 (oname "itimer-")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
396 (num 1))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
397 (while (get-itimer name)
286
57709be46d1b Import from CVS: tag r21-0b41
cvs
parents: 272
diff changeset
398 (setq name (format "%s<%d>" oname num))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
399 (itimer-increment num))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
400 (setcar itimer name))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
401 ;; signal an error if the timer's name matches an already
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
402 ;; activated timer.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
403 (if (get-itimer (itimer-name itimer))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
404 (error "itimer named \"%s\" already existing and activated"
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
405 (itimer-name itimer))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
406 (let ((inhibit-quit t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
407 ;; add the itimer to the global list
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
408 (setq itimer-list (cons itimer itimer-list))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
409 ;; If the itimer process is scheduled to wake up too late for
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
410 ;; the itimer we wake it up to calculate a correct wakeup
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
411 ;; value giving consideration to the newly added itimer.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
412 (if (< (itimer-value itimer) itimer-next-wakeup)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
413 (itimer-driver-wakeup))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
414
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
415 ;; User level functions to list and modify existing itimers.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
416 ;; Itimer Edit major mode, and the editing commands thereof.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
417
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
418 (defun list-itimers ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
419 "Pop up a buffer containing a list of all itimers.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
420 The major mode of the buffer is Itimer Edit mode. This major mode provides
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
421 commands to manipulate itimers; see the documentation for
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
422 `itimer-edit-mode' for more information."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
423 (interactive)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
424 (let* ((buf (get-buffer-create "*Itimer List*"))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
425 (opoint (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
426 (standard-output buf)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
427 (itimers (reverse itimer-list)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
428 (set-buffer buf)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
429 (itimer-edit-mode)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
430 (setq buffer-read-only nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
431 (erase-buffer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
432 (insert
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
433 "Name Value Restart Function Idle Arguments"
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
434 "\n"
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
435 "---- ----- ------- -------- ---- --------")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
436 (if (null itimer-edit-start-marker)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
437 (setq itimer-edit-start-marker (point)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
438 (while itimers
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
439 (newline 1)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
440 (prin1 (itimer-name (car itimers)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
441 (tab-to-tab-stop)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
442 (insert (itimer-truncate-string
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
443 (format "%5.5s" (itimer-value (car itimers))) 5))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
444 (tab-to-tab-stop)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
445 (insert (itimer-truncate-string
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
446 (format "%5.5s" (itimer-restart (car itimers))) 5))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
447 (tab-to-tab-stop)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
448 (insert (itimer-truncate-string
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
449 (format "%.19s" (itimer-function (car itimers))) 19))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
450 (tab-to-tab-stop)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
451 (if (itimer-is-idle (car itimers))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
452 (insert "yes")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
453 (insert "no"))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
454 (tab-to-tab-stop)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
455 (if (itimer-uses-arguments (car itimers))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
456 (prin1 (itimer-function-arguments (car itimers)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
457 (prin1 'NONE))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
458 (setq itimers (cdr itimers)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
459 ;; restore point
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
460 (goto-char opoint)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
461 (if (< (point) itimer-edit-start-marker)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
462 (goto-char itimer-edit-start-marker))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
463 (setq buffer-read-only t)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
464 (display-buffer buf)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
465
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
466 (defun edit-itimers ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
467 "Display a list of all itimers and select it for editing.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
468 The major mode of the buffer containing the listing is Itimer Edit mode.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
469 This major mode provides commands to manipulate itimers; see the documentation
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
470 for `itimer-edit-mode' for more information."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
471 (interactive)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
472 ;; since user is editing, make sure displayed data is reasonably up-to-date
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
473 (if (or itimer-process itimer-timer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
474 (itimer-driver-wakeup))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
475 (list-itimers)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
476 (select-window (get-buffer-window "*Itimer List*"))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
477 (goto-char itimer-edit-start-marker)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
478 (if itimer-list
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
479 (progn
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
480 (forward-sexp 2)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
481 (backward-sexp)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
482 (message "type q to quit, ? for help"))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
483
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
484 ;; no point in making this interactive.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
485 (defun itimer-edit-mode ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
486 "Major mode for manipulating itimers.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
487 Attributes of running itimers are changed by moving the cursor to the
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
488 desired field and typing `s' to set that field. The field will then be
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
489 set to the value read from the minibuffer.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
490
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
491 Commands:
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
492 TAB move forward a field
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
493 DEL move backward a field
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
494 s set a field
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
495 d delete the selected itimer
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
496 x start a new itimer
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
497 ? help"
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
498 (kill-all-local-variables)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
499 (make-local-variable 'tab-stop-list)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
500 (setq major-mode 'itimer-edit-mode
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
501 mode-name "Itimer Edit"
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
502 truncate-lines t
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
503 tab-stop-list '(22 32 40 60 67))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
504 (abbrev-mode 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
505 (auto-fill-mode 0)
267
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
506 (buffer-flush-undo (current-buffer))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
507 (use-local-map itimer-edit-map)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
508 (set-syntax-table emacs-lisp-mode-syntax-table))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
509
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
510 (put 'itimer-edit-mode 'mode-class 'special)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
511
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
512 (defun itimer-edit-help ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
513 "Help function for Itimer Edit."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
514 (interactive)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
515 (if (eq last-command 'itimer-edit-help)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
516 (describe-mode)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
517 (message "TAB, DEL select fields, (s)et field, (d)elete itimer (type ? for more help)")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
518
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
519 (defun itimer-edit-quit ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
520 "End Itimer Edit."
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
521 (interactive)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
522 (bury-buffer (current-buffer))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
523 (if (one-window-p t)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
524 (switch-to-buffer (other-buffer (current-buffer)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
525 (delete-window)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
526
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
527 (defun itimer-edit-set-field ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
528 (interactive)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
529 ;; First two lines in list buffer are headers.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
530 ;; Cry out against the luser who attempts to change a field there.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
531 (if (<= (point) itimer-edit-start-marker)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
532 (error ""))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
533 ;; field-value must be initialized to be something other than a
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
534 ;; number, symbol, or list.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
535 (let (itimer field (field-value ""))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
536 (setq itimer (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
537 ;; read the name of the itimer from the beginning of
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
538 ;; the current line.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
539 (beginning-of-line)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
540 (get-itimer (read (current-buffer))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
541 field (save-excursion
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
542 (itimer-edit-beginning-of-field)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
543 (let ((opoint (point))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
544 (n 0))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
545 ;; count the number of sexprs until we reach the cursor
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
546 ;; and use this info to determine which field the user
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
547 ;; wants to modify.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
548 (beginning-of-line)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
549 (while (and (>= opoint (point)) (< n 6))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
550 (forward-sexp 2)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
551 (backward-sexp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
552 (itimer-increment n))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
553 (cond ((eq n 1) (error "Cannot change itimer name."))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
554 ((eq n 2) 'value)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
555 ((eq n 3) 'restart)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
556 ((eq n 4) 'function)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
557 ((eq n 5) 'is-idle)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
558 (t 'function-argument)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
559 (cond ((eq field 'value)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
560 (let ((prompt "Set itimer value: "))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
561 (while (not (natnump field-value))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
562 (setq field-value (read-from-minibuffer prompt nil nil t)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
563 ((eq field 'restart)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
564 (let ((prompt "Set itimer restart: "))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
565 (while (and field-value (not (natnump field-value)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
566 (setq field-value (read-from-minibuffer prompt nil nil t)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
567 ((eq field 'function)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
568 (let ((prompt "Set itimer function: "))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
569 (while (not (or (and (symbolp field-value) (fboundp field-value))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
570 (and (consp field-value)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
571 (memq (car field-value) '(lambda macro)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
572 (setq field-value
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
573 (read (completing-read prompt obarray 'fboundp nil))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
574 ((eq field 'is-idle)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
575 (setq field-value (not (itimer-is-idle itimer))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
576 ((eq field 'function-argument)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
577 (let ((prompt "Set itimer function argument: "))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
578 (setq field-value (read-expression prompt))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
579 (cond ((not (listp field-value))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
580 (setq field-value (list field-value))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
581 (if (null field-value)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
582 (set-itimer-uses-arguments itimer nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
583 (set-itimer-uses-arguments itimer t)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
584 ;; set the itimer field
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
585 (funcall (intern (concat "set-itimer-" (symbol-name field)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
586 itimer field-value)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
587 ;; move to beginning of field to be changed
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
588 (itimer-edit-beginning-of-field)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
589 ;; modify the list buffer to reflect the change.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
590 (let (buffer-read-only kill-ring)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
591 (kill-sexp 1)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
592 (kill-region (point) (progn (skip-chars-forward " \t") (point)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
593 (prin1 field-value (current-buffer))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
594 (if (not (eolp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
595 (tab-to-tab-stop))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
596 (backward-sexp))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
597
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
598 (defun itimer-edit-delete-itimer ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
599 (interactive)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
600 ;; First two lines in list buffer are headers.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
601 ;; Cry out against the luser who attempts to change a field there.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
602 (if (<= (point) itimer-edit-start-marker)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
603 (error ""))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
604 (delete-itimer
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
605 (read-itimer "Delete itimer: "
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
606 (save-excursion (beginning-of-line) (read (current-buffer)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
607 ;; update list information
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
608 (list-itimers))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
609
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
610 (defun itimer-edit-next-field (count)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
611 (interactive "p")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
612 (itimer-edit-beginning-of-field)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
613 (cond ((> (itimer-signum count) 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
614 (while (not (zerop count))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
615 (forward-sexp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
616 ;; wrap from eob to itimer-edit-start-marker
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
617 (if (eobp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
618 (progn
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
619 (goto-char itimer-edit-start-marker)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
620 (forward-sexp)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
621 (forward-sexp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
622 (backward-sexp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
623 ;; treat fields at beginning of line as if they weren't there.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
624 (if (bolp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
625 (progn
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
626 (forward-sexp 2)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
627 (backward-sexp)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
628 (itimer-decrement count)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
629 ((< (itimer-signum count) 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
630 (while (not (zerop count))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
631 (backward-sexp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
632 ;; treat fields at beginning of line as if they weren't there.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
633 (if (bolp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
634 (backward-sexp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
635 ;; wrap from itimer-edit-start-marker to field at eob.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
636 (if (<= (point) itimer-edit-start-marker)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
637 (progn
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
638 (goto-char (point-max))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
639 (backward-sexp)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
640 (itimer-increment count)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
641
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
642 (defun itimer-edit-previous-field (count)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
643 (interactive "p")
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
644 (itimer-edit-next-field (- count)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
645
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
646 (defun itimer-edit-beginning-of-field ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
647 (let ((forw-back (save-excursion (forward-sexp) (backward-sexp) (point)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
648 (back (save-excursion (backward-sexp) (point))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
649 (cond ((eq forw-back back) (backward-sexp))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
650 ((eq forw-back (point)) t)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
651 (t (backward-sexp)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
652
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
653 (defun itimer-truncate-string (str len)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
654 (if (<= (length str) len)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
655 str
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
656 (substring str 0 len)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
657
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
658 ;; internals of the itimer implementation.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
659
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
660 (defun itimer-run-expired-timers (time-elapsed)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
661 (let ((itimers (copy-sequence itimer-list))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
662 (itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
663 (next-wakeup 600)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
664 (idle-time)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
665 (last-event-time)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
666 (recorded-run-time)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
667 ;; process filters can be hit by stray C-g's from the user,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
668 ;; so we must protect this stuff appropriately.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
669 ;; Quit's are allowed from within itimer functions, but we
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
670 ;; catch them and print a message.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
671 (inhibit-quit t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
672 (setq next-wakeup 600)
267
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
673 (cond ((and (boundp 'last-command-event-time)
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
674 (consp 'last-command-event-time))
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
675 (setq last-event-time last-command-event-time
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
676 idle-time (itimer-time-difference (current-time)
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
677 last-event-time)))
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
678 ((and (boundp 'last-input-time) (consp last-input-time))
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
679 (setq last-event-time (list (car last-input-time)
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
680 (cdr last-input-time)
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
681 0)
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
682 idle-time (itimer-time-difference (current-time)
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
683 last-event-time)))
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
684 ;; no way to do this under FSF Emacs yet.
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
685 (t (setq last-event-time '(0 0 0)
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
686 idle-time 0)))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
687 (while itimers
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
688 (setq itimer (car itimers))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
689 (if (itimer-is-idle itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
690 (setq recorded-run-time (itimer-recorded-run-time itimer))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
691 (set-itimer-value-internal itimer (max 0 (- (itimer-value itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
692 time-elapsed))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
693 (if (if (itimer-is-idle itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
694 (or (> (itimer-time-difference recorded-run-time
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
695 last-event-time)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
696 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
697 (< idle-time (itimer-value itimer)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
698 (> (itimer-value itimer) 0))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
699 (setq next-wakeup
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
700 (if (itimer-is-idle itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
701 (if (< idle-time (itimer-value itimer))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
702 (min next-wakeup (- (itimer-value itimer) idle-time))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
703 (min next-wakeup (itimer-value itimer)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
704 (min next-wakeup (itimer-value itimer))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
705 (and (itimer-is-idle itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
706 (set-itimer-recorded-run-time itimer (current-time)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
707 ;; itimer has expired, we must call its function.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
708 ;; protect our local vars from the itimer function.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
709 ;; allow keyboard quit to occur, but catch and report it.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
710 ;; provide the variable `current-itimer' in case the function
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
711 ;; is interested.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
712 (unwind-protect
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
713 (condition-case condition-data
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
714 (save-match-data
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
715 (let* ((current-itimer itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
716 (quit-flag nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
717 (inhibit-quit nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
718 ;; for FSF Emacs timer.el emulation under XEmacs.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
719 ;; eldoc expect this to be done, apparently.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
720 (this-command nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
721 itimer itimers time-elapsed)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
722 (if (itimer-uses-arguments current-itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
723 (apply (itimer-function current-itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
724 (itimer-function-arguments current-itimer))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
725 (funcall (itimer-function current-itimer)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
726 (error (message "itimer \"%s\" signaled: %s" (itimer-name itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
727 (prin1-to-string condition-data)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
728 (quit (message "itimer \"%s\" quit" (itimer-name itimer))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
729 ;; restart the itimer if we should, otherwise delete it.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
730 (if (null (itimer-restart itimer))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
731 (delete-itimer itimer)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
732 (set-itimer-value-internal itimer (itimer-restart itimer))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
733 (setq next-wakeup (min next-wakeup (itimer-value itimer))))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
734 (setq itimers (cdr itimers)))
267
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
735 ;; make another sweep through the list to catch any timers
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
736 ;; that might have been added by timer functions above.
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
737 (setq itimers itimer-list)
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
738 (while itimers
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
739 (setq next-wakeup (min next-wakeup (itimer-value (car itimers)))
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
740 itimers (cdr itimers)))
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
741 ;; if user is viewing the timer list, update displayed info.
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
742 (let ((b (get-buffer "*Itimer List*")))
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
743 (if (and b (get-buffer-window b))
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
744 (save-excursion
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
745 (list-itimers))))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
746 next-wakeup ))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
747
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
748 (defun itimer-process-filter (process string)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
749 ;; If the itimer process dies and generates output while doing
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
750 ;; so, we may be called before the process-sentinel. Sanity
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
751 ;; check the output just in case...
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
752 (if (not (string-match "^[0-9]" string))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
753 (progn (message "itimer process gave odd output: %s" string)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
754 ;; it may be still alive and waiting for input
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
755 (process-send-string itimer-process "3\n"))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
756 ;; if there are no active itimers, return quickly.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
757 (if itimer-list
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
758 (let ((wakeup nil))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
759 (unwind-protect
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
760 (setq wakeup (itimer-run-expired-timers (string-to-int string)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
761 (and (null wakeup) (process-send-string process "1\n")))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
762 (setq itimer-next-wakeup wakeup))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
763 (setq itimer-next-wakeup 600))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
764 ;; tell itimer-process when to wakeup again
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
765 (process-send-string itimer-process
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
766 (concat (int-to-string itimer-next-wakeup)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
767 "\n"))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
768
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
769 (defun itimer-process-sentinel (process message)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
770 (let ((inhibit-quit t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
771 (if (eq (process-status process) 'stop)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
772 (continue-process process)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
773 ;; not stopped, so it must have died.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
774 ;; cleanup first...
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
775 (delete-process process)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
776 (setq itimer-process nil)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
777 ;; now, if there are any active itimers then we need to immediately
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
778 ;; start another itimer process, otherwise we can wait until the next
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
779 ;; start-itimer call, which will start one automatically.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
780 (if (null itimer-list)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
781 ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
782 ;; there may have been an error message in the echo area;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
783 ;; give the user at least a little time to read it.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
784 (sit-for 2)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
785 (message "itimer process %s... respawning." (substring message 0 -1))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
786 (itimer-process-start)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
787
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
788 (defun itimer-process-start ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
789 (let ((inhibit-quit t)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
790 (process-connection-type nil))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
791 (setq itimer-process (start-process "itimer" nil "itimer"))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
792 (process-kill-without-query itimer-process)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
793 (set-process-filter itimer-process 'itimer-process-filter)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
794 (set-process-sentinel itimer-process 'itimer-process-sentinel)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
795 ;; Tell itimer process to wake up quickly, so that a correct
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
796 ;; wakeup time can be computed. Zero loses because of
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
797 ;; underlying itimer implementations that use 0 to mean
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
798 ;; `disable the itimer'.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
799 (setq itimer-next-wakeup itimer-short-interval)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
800 (process-send-string itimer-process
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
801 (format "%s\n" itimer-next-wakeup))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
802
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
803 (defun itimer-process-wakeup ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
804 (interrupt-process itimer-process)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
805 (accept-process-output))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
806
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
807 (defun itimer-timer-start ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
808 (let ((inhibit-quit t))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
809 (setq itimer-next-wakeup itimer-short-interval
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
810 itimer-timer-last-wakeup (current-time)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
811 itimer-timer (add-timeout itimer-short-interval
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
812 'itimer-timer-driver nil nil))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
813
267
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
814 (defun itimer-disable-timeout (timeout)
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
815 ;; Disgusting hack, but necessary because there is no other way
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
816 ;; to remove a timer that has a restart value from while that
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
817 ;; timer's function is being run. (FSF Emacs only.)
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
818 (if (vectorp timeout)
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
819 (aset timeout 4 nil))
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
820 (disable-timeout timeout))
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
821
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
822 (defun itimer-timer-wakeup ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
823 (let ((inhibit-quit t))
267
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
824 (itimer-disable-timeout itimer-timer)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
825 (setq itimer-timer (add-timeout itimer-short-interval
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
826 'itimer-timer-driver nil 5))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
827
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
828 (defun itimer-time-difference (t1 t2)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
829 (let (usecs secs 65536-secs carry)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
830 (setq usecs (- (nth 2 t1) (nth 2 t2)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
831 (if (< usecs 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
832 (setq carry 1
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
833 usecs (+ usecs 1000000))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
834 (setq carry 0))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
835 (setq secs (- (nth 1 t1) (nth 1 t2) carry))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
836 (if (< secs 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
837 (setq carry 1
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
838 secs (+ secs 65536))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
839 (setq carry 0))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
840 (setq 65536-secs (- (nth 0 t1) (nth 0 t2) carry))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
841 ;; loses for interval larger than the maximum signed Lisp integer.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
842 ;; can't really be helped.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
843 (+ (* 65536-secs 65536)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
844 secs
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
845 (/ usecs (if (featurep 'lisp-float-type) 1e6 1000000)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
846
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
847 (defun itimer-timer-driver (&rest ignored)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
848 ;; inhibit quit because if the user quits at an inopportune
267
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
849 ;; time, the timer process won't be launched again and the
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
850 ;; system stops working. itimer-run-expired-timers allows
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
851 ;; individual timer function to be aborted, so the user can
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
852 ;; escape a feral timer function.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
853 (if (not itimer-inside-driver)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
854 (let* ((inhibit-quit t)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
855 (itimer-inside-driver t)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
856 (now (current-time))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
857 (elapsed (itimer-time-difference now itimer-timer-last-wakeup))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
858 (sleep nil))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
859 (setq itimer-timer-last-wakeup now
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
860 sleep (itimer-run-expired-timers elapsed))
267
966663fcf606 Import from CVS: tag r20-5b32
cvs
parents: 209
diff changeset
861 (itimer-disable-timeout itimer-timer)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
862 (setq itimer-next-wakeup sleep
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
863 itimer-timer (add-timeout sleep 'itimer-timer-driver nil 5)))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
864
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
865 (defun itimer-driver-start ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
866 (if (fboundp 'add-timeout)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
867 (itimer-timer-start)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
868 (itimer-process-start)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
869
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
870 (defun itimer-driver-wakeup ()
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
871 (if (fboundp 'add-timeout)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
872 (itimer-timer-wakeup)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents:
diff changeset
873 (itimer-process-wakeup)))