comparison lisp/itimer.el @ 209:41ff10fd062f r20-4b3

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