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