annotate lisp/itimer.el @ 5724:ede80ef92a74

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