annotate lisp/=timer.el @ 2272:0e1b3507ee15

Deleted some VMS caveats. If the src and lisp ChangeLogs are correct, dired and mail and process control are now fully supported.
author Eric S. Raymond <esr@snark.thyrsus.com>
date Fri, 19 Mar 1993 22:56:22 +0000
parents 3daa7f080f2b
children d98c49df2acd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
6
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
7 ;; This file is part of GNU Emacs.
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
8
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
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
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
12 ;; any later version.
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
13
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
14 ;; GNU Emacs is distributed in the hope that it will be useful,
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
17 ;; GNU General Public License for more details.
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
18
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
19 ;; You should have received a copy of the GNU General Public License
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
22
807
4f28bd14272c *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 657
diff changeset
23 ;;; Code:
4f28bd14272c *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 657
diff changeset
24
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
25 (defvar timer-process nil)
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
26 (defvar timer-alist ())
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
27 (defvar timer-out "")
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
28 (defvar timer-dont-exit nil
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
29 ;; this is useful for functions which will be doing their own erratic
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
30 ;; rescheduling or people who otherwise expect to use the process frequently
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
31 "If non-nil, don't exit the timer process when no more events are pending.")
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
32
584
4cd7543be581 *** empty log message ***
Jim Blandy <jimb@redhat.com>
parents: 45
diff changeset
33 ;;;###autoload
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
34 (defun run-at-time (time repeat function &rest args)
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
35 "Run a function at a time, and optionally on a regular interval.
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
36 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
37 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
38 TIME can also be an integer, a number of seconds.
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
39 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
40 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
41
f7da227e3e68 * timer.el (run-at-time): Doc fix.
Jim Blandy <jimb@redhat.com>
parents: 1753
diff changeset
42 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
43 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
44 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
45 command works too.
1983
f7da227e3e68 * timer.el (run-at-time): Doc fix.
Jim Blandy <jimb@redhat.com>
parents: 1753
diff changeset
46
f7da227e3e68 * timer.el (run-at-time): Doc fix.
Jim Blandy <jimb@redhat.com>
parents: 1753
diff changeset
47 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
48 1 min denotes one minute from now.
f7da227e3e68 * timer.el (run-at-time): Doc fix.
Jim Blandy <jimb@redhat.com>
parents: 1753
diff changeset
49 min does too.
f7da227e3e68 * timer.el (run-at-time): Doc fix.
Jim Blandy <jimb@redhat.com>
parents: 1753
diff changeset
50 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
51 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
52 denotes the sum of all the given durations from now."
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
53 (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
54 ;; Make TIME a string.
3daa7f080f2b (run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents: 1983
diff changeset
55 (if (integerp time)
3daa7f080f2b (run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents: 1983
diff changeset
56 (setq time (format "%d sec" time)))
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
57 (cond ((or (not timer-process)
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
58 (memq (process-status timer-process) '(exit signal nil)))
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
59 (if timer-process (delete-process timer-process))
1747
aae774aae3de (run-at-time): Use a pipe to talk to the timer process.
Roland McGrath <roland@gnu.org>
parents: 1577
diff changeset
60 (setq timer-process (let ((process-connection-type nil))
aae774aae3de (run-at-time): Use a pipe to talk to the timer process.
Roland McGrath <roland@gnu.org>
parents: 1577
diff changeset
61 (start-process "timer" nil "timer"))
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
62 timer-alist nil)
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
63 (set-process-filter timer-process 'timer-process-filter)
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
64 (set-process-sentinel timer-process 'timer-process-sentinel)
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
65 (process-kill-without-query timer-process))
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
66 ((eq (process-status timer-process) 'stop)
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
67 (continue-process timer-process)))
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
68 ;; 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
69 (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
70 (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
71 (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
72 (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
73 elt))
3daa7f080f2b (run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents: 1983
diff changeset
74
3daa7f080f2b (run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents: 1983
diff changeset
75 (defun cancel-timer (elt)
3daa7f080f2b (run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents: 1983
diff changeset
76 "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
77 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
78 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
79 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
80 (setcar (cdr elt) nil)
3daa7f080f2b (run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents: 1983
diff changeset
81 (setcar (cdr (cdr elt)) 'ignore))
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
82
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
83 (defun timer-process-filter (proc str)
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
84 (setq timer-out (concat timer-out str))
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
85 (let (do token error)
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
86 (while (string-match "\n" timer-out)
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
87 (setq token (substring timer-out 0 (match-beginning 0))
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
88 do (assoc token timer-alist)
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
89 timer-out (substring timer-out (match-end 0)))
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
90 (cond
2086
3daa7f080f2b (run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents: 1983
diff changeset
91 (do
3daa7f080f2b (run-at-time): Allow an integer as TIME.
Richard M. Stallman <rms@gnu.org>
parents: 1983
diff changeset
92 (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
93 (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
94 (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
95 (setq timer-alist (delq do timer-alist))))
998
61c6983219ff entered into RCS
Jim Blandy <jimb@redhat.com>
parents: 840
diff changeset
96 ((string-match "timer: \\([^:]+\\): \\([^@]*\\)@\\(.*\\)$" token)
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
97 (setq error (substring token (match-beginning 1) (match-end 1))
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
98 do (substring token (match-beginning 2) (match-end 2))
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
99 token (assoc (substring token (match-beginning 3) (match-end 3))
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
100 timer-alist)
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
101 timer-alist (delq token timer-alist))
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
102 (ding 'no-terminate) ; using error function in process filters is rude
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
103 (message "%s for %s; couldn't set at \"%s\"" error (nth 2 token) do))))
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
104 (or timer-alist timer-dont-exit (process-send-eof proc))))
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
105
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
106 (defun timer-process-sentinel (proc str)
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
107 (let ((stat (process-status proc)))
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
108 (if (eq stat 'stop) (continue-process proc)
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
109 ;; if it exited normally, presumably it was intentional.
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
110 ;; if there were no pending events, who cares that it exited?
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
111 (if (or (not timer-alist) (eq stat 'exit)) ()
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
112 (ding 'no-terminate)
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
113 (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
114 ;; 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
115 (setq timer-process nil timer-alist nil))))
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
116
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
117 (defun cancel-timer (function)
1577
a52da8d63ff3 Doc fix.
Christopher Zaborsky <rogue@erratum.com>
parents: 1241
diff changeset
118 "Cancel all events scheduled by `run-at-time' which would run FUNCTION."
45
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
119 (interactive "aCancel function: ")
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
120 (let ((alist timer-alist))
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
121 (while alist
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
122 (if (eq (nth 2 (car alist)) function)
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
123 (setq timer-alist (delq (car alist) timer-alist)))
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
124 (setq alist (cdr alist))))
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
125 (or timer-alist timer-dont-exit (process-send-eof timer-process)))
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
126
a55a3ac41924 Initial revision
root <root>
parents:
diff changeset
127 (provide 'timer)
657
fec3f9a1e3e5 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 584
diff changeset
128
fec3f9a1e3e5 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 584
diff changeset
129 ;;; timer.el ends here