Mercurial > emacs
annotate lisp/=timer.el @ 2879:48dd9b2361df
* select.el (xselect-convert-to-class): Just return "Emacs" here.
That's what the class will always be.
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Tue, 18 May 1993 23:25:21 +0000 |
parents | 6ff9eee5cdc9 |
children | ac18ead9965d |
rev | line source |
---|---|
657
fec3f9a1e3e5
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
1 ;;; timer.el --- run a function with args at some time in future |
fec3f9a1e3e5
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
2 |
1747
aae774aae3de
(run-at-time): Use a pipe to talk to the timer process.
Roland McGrath <roland@gnu.org>
parents:
1577
diff
changeset
|
3 ;; Copyright (C) 1990, 1993 Free Software Foundation, Inc. |
840
113281b361ec
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
807
diff
changeset
|
4 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
657
diff
changeset
|
5 ;; Maintainer: FSF |
45 | 6 |
7 ;; This file is part of GNU Emacs. | |
8 | |
9 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
10 ;; it under the terms of the GNU General Public License as published by | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
657
diff
changeset
|
11 ;; the Free Software Foundation; either version 2, or (at your option) |
45 | 12 ;; any later version. |
13 | |
14 ;; GNU Emacs is distributed in the hope that it will be useful, | |
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 ;; GNU General Public License for more details. | |
18 | |
19 ;; You should have received a copy of the GNU General Public License | |
20 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
22 | |
2319
d98c49df2acd
Added or corrected Commentary section
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2086
diff
changeset
|
23 ;;; Commentary: |
d98c49df2acd
Added or corrected Commentary section
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2086
diff
changeset
|
24 |
d98c49df2acd
Added or corrected Commentary section
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2086
diff
changeset
|
25 ;; This package gives you the capability to run Emacs Lisp commands at |
d98c49df2acd
Added or corrected Commentary section
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2086
diff
changeset
|
26 ;; specified simes in the future, either as one-shots or periodically. |
d98c49df2acd
Added or corrected Commentary section
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2086
diff
changeset
|
27 ;; The single entry point is `run-at-time'. |
d98c49df2acd
Added or corrected Commentary section
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2086
diff
changeset
|
28 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
657
diff
changeset
|
29 ;;; Code: |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
657
diff
changeset
|
30 |
2447
6ff9eee5cdc9
* timer.el (timer-program): New defconst.
Jim Blandy <jimb@redhat.com>
parents:
2319
diff
changeset
|
31 ;;; The name of the program to run as the timer subprocess. It should |
6ff9eee5cdc9
* timer.el (timer-program): New defconst.
Jim Blandy <jimb@redhat.com>
parents:
2319
diff
changeset
|
32 ;;; be in exec-directory. |
6ff9eee5cdc9
* timer.el (timer-program): New defconst.
Jim Blandy <jimb@redhat.com>
parents:
2319
diff
changeset
|
33 (defconst timer-program "timer") |
6ff9eee5cdc9
* timer.el (timer-program): New defconst.
Jim Blandy <jimb@redhat.com>
parents:
2319
diff
changeset
|
34 |
45 | 35 (defvar timer-process nil) |
36 (defvar timer-alist ()) | |
37 (defvar timer-out "") | |
38 (defvar timer-dont-exit nil | |
39 ;; this is useful for functions which will be doing their own erratic | |
40 ;; rescheduling or people who otherwise expect to use the process frequently | |
41 "If non-nil, don't exit the timer process when no more events are pending.") | |
42 | |
584 | 43 ;;;###autoload |
45 | 44 (defun run-at-time (time repeat function &rest args) |
45 "Run a function at a time, and optionally on a regular interval. | |
46 Arguments are TIME, REPEAT, FUNCTION &rest ARGS. | |
2086
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
47 TIME, a string, can be specified absolutely or relative to now. |
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
48 TIME can also be an integer, a number of seconds. |
45 | 49 REPEAT, an integer number of seconds, is the interval on which to repeat |
1983
f7da227e3e68
* timer.el (run-at-time): Doc fix.
Jim Blandy <jimb@redhat.com>
parents:
1753
diff
changeset
|
50 the call to the function. If REPEAT is nil, call it just once. |
f7da227e3e68
* timer.el (run-at-time): Doc fix.
Jim Blandy <jimb@redhat.com>
parents:
1753
diff
changeset
|
51 |
f7da227e3e68
* timer.el (run-at-time): Doc fix.
Jim Blandy <jimb@redhat.com>
parents:
1753
diff
changeset
|
52 Absolute times may be specified in a wide variety of formats; |
f7da227e3e68
* timer.el (run-at-time): Doc fix.
Jim Blandy <jimb@redhat.com>
parents:
1753
diff
changeset
|
53 Something of the form `HOUR:MIN:SEC TIMEZONE MONTH/DAY/YEAR', where |
2086
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
54 all fields are numbers, works; the format used by the Unix `date' |
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
55 command works too. |
1983
f7da227e3e68
* timer.el (run-at-time): Doc fix.
Jim Blandy <jimb@redhat.com>
parents:
1753
diff
changeset
|
56 |
f7da227e3e68
* timer.el (run-at-time): Doc fix.
Jim Blandy <jimb@redhat.com>
parents:
1753
diff
changeset
|
57 Relative times may be specified as a series of numbers followed by units: |
f7da227e3e68
* timer.el (run-at-time): Doc fix.
Jim Blandy <jimb@redhat.com>
parents:
1753
diff
changeset
|
58 1 min denotes one minute from now. |
f7da227e3e68
* timer.el (run-at-time): Doc fix.
Jim Blandy <jimb@redhat.com>
parents:
1753
diff
changeset
|
59 min does too. |
f7da227e3e68
* timer.el (run-at-time): Doc fix.
Jim Blandy <jimb@redhat.com>
parents:
1753
diff
changeset
|
60 1 min 5 sec denotes 65 seconds from now. |
f7da227e3e68
* timer.el (run-at-time): Doc fix.
Jim Blandy <jimb@redhat.com>
parents:
1753
diff
changeset
|
61 1 min 2 sec 3 hour 4 day 5 week 6 fortnight 7 month 8 year |
f7da227e3e68
* timer.el (run-at-time): Doc fix.
Jim Blandy <jimb@redhat.com>
parents:
1753
diff
changeset
|
62 denotes the sum of all the given durations from now." |
45 | 63 (interactive "sRun at time: \nNRepeat interval: \naFunction: ") |
2086
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
64 ;; Make TIME a string. |
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
65 (if (integerp time) |
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
66 (setq time (format "%d sec" time))) |
45 | 67 (cond ((or (not timer-process) |
68 (memq (process-status timer-process) '(exit signal nil))) | |
69 (if timer-process (delete-process timer-process)) | |
2447
6ff9eee5cdc9
* timer.el (timer-program): New defconst.
Jim Blandy <jimb@redhat.com>
parents:
2319
diff
changeset
|
70 (setq timer-process |
6ff9eee5cdc9
* timer.el (timer-program): New defconst.
Jim Blandy <jimb@redhat.com>
parents:
2319
diff
changeset
|
71 (let ((process-connection-type nil)) |
6ff9eee5cdc9
* timer.el (timer-program): New defconst.
Jim Blandy <jimb@redhat.com>
parents:
2319
diff
changeset
|
72 ;; Don't search the exec path for the timer program; |
6ff9eee5cdc9
* timer.el (timer-program): New defconst.
Jim Blandy <jimb@redhat.com>
parents:
2319
diff
changeset
|
73 ;; we know exactly which one we want. |
6ff9eee5cdc9
* timer.el (timer-program): New defconst.
Jim Blandy <jimb@redhat.com>
parents:
2319
diff
changeset
|
74 (start-process (expand-file-name timer-program exec-directory) |
6ff9eee5cdc9
* timer.el (timer-program): New defconst.
Jim Blandy <jimb@redhat.com>
parents:
2319
diff
changeset
|
75 nil "timer")) |
45 | 76 timer-alist nil) |
77 (set-process-filter timer-process 'timer-process-filter) | |
78 (set-process-sentinel timer-process 'timer-process-sentinel) | |
79 (process-kill-without-query timer-process)) | |
80 ((eq (process-status timer-process) 'stop) | |
81 (continue-process timer-process))) | |
82 ;; There should be a living, breathing timer process now | |
2086
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
83 (let* ((token (concat (current-time-string) "-" (length timer-alist))) |
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
84 (elt (list token repeat function args))) |
1753
852bc0022185
Use process-send-string instead of send-string.
Michael I. Bushnell <mib@gnu.org>
parents:
1747
diff
changeset
|
85 (process-send-string timer-process (concat time "@" token "\n")) |
2086
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
86 (setq timer-alist (cons elt timer-alist)) |
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
87 elt)) |
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
88 |
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
89 (defun cancel-timer (elt) |
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
90 "Cancel a timer previously made with `run-at-time'. |
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
91 The argument should be a value previously returned by `run-at-time'. |
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
92 Cancelling the timer means that nothing special |
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
93 will happen at the specified time." |
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
94 (setcar (cdr elt) nil) |
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
95 (setcar (cdr (cdr elt)) 'ignore)) |
45 | 96 |
97 (defun timer-process-filter (proc str) | |
98 (setq timer-out (concat timer-out str)) | |
99 (let (do token error) | |
100 (while (string-match "\n" timer-out) | |
101 (setq token (substring timer-out 0 (match-beginning 0)) | |
102 do (assoc token timer-alist) | |
103 timer-out (substring timer-out (match-end 0))) | |
104 (cond | |
2086
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
105 (do |
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
106 (apply (nth 2 do) (nth 3 do)) ; do it |
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
107 (if (natnump (nth 1 do)) ; reschedule it |
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
108 (send-string proc (concat (nth 1 do) " sec@" (car do) "\n")) |
3daa7f080f2b
(run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents:
1983
diff
changeset
|
109 (setq timer-alist (delq do timer-alist)))) |
998 | 110 ((string-match "timer: \\([^:]+\\): \\([^@]*\\)@\\(.*\\)$" token) |
45 | 111 (setq error (substring token (match-beginning 1) (match-end 1)) |
112 do (substring token (match-beginning 2) (match-end 2)) | |
113 token (assoc (substring token (match-beginning 3) (match-end 3)) | |
114 timer-alist) | |
115 timer-alist (delq token timer-alist)) | |
116 (ding 'no-terminate) ; using error function in process filters is rude | |
117 (message "%s for %s; couldn't set at \"%s\"" error (nth 2 token) do)))) | |
118 (or timer-alist timer-dont-exit (process-send-eof proc)))) | |
119 | |
120 (defun timer-process-sentinel (proc str) | |
121 (let ((stat (process-status proc))) | |
122 (if (eq stat 'stop) (continue-process proc) | |
123 ;; if it exited normally, presumably it was intentional. | |
124 ;; if there were no pending events, who cares that it exited? | |
125 (if (or (not timer-alist) (eq stat 'exit)) () | |
126 (ding 'no-terminate) | |
127 (message "Timer exited abnormally. All events cancelled.")) | |
1241
de92360b091b
(timer-process-sentinel): Don't set timer-scratch.
Richard M. Stallman <rms@gnu.org>
parents:
1223
diff
changeset
|
128 ;; Used to set timer-scratch to "", but nothing uses that var. |
de92360b091b
(timer-process-sentinel): Don't set timer-scratch.
Richard M. Stallman <rms@gnu.org>
parents:
1223
diff
changeset
|
129 (setq timer-process nil timer-alist nil)))) |
45 | 130 |
131 (defun cancel-timer (function) | |
1577 | 132 "Cancel all events scheduled by `run-at-time' which would run FUNCTION." |
45 | 133 (interactive "aCancel function: ") |
134 (let ((alist timer-alist)) | |
135 (while alist | |
136 (if (eq (nth 2 (car alist)) function) | |
137 (setq timer-alist (delq (car alist) timer-alist))) | |
138 (setq alist (cdr alist)))) | |
139 (or timer-alist timer-dont-exit (process-send-eof timer-process))) | |
140 | |
141 (provide 'timer) | |
657
fec3f9a1e3e5
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
142 |
fec3f9a1e3e5
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
143 ;;; timer.el ends here |