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