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