(2C-toggle-autoscroll, 2C-autoscroll):
[bpt/emacs.git] / lisp / timer.el
CommitLineData
d501f516
ER
1;;; timer.el --- run a function with args at some time in future
2
8f1204db 3;; Copyright (C) 1990, 1993, 1994 Free Software Foundation, Inc.
5cc564a6 4
eea8d4ef
ER
5;; Maintainer: FSF
6
5cc564a6 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
e5167999 11;; the Free Software Foundation; either version 2, or (at your option)
5cc564a6 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
c91c4e6d
ER
23;;; Commentary:
24
25;; This package gives you the capability to run Emacs Lisp commands at
eb8c3be9 26;; specified times in the future, either as one-shots or periodically.
c91c4e6d
ER
27;; The single entry point is `run-at-time'.
28
e5167999
ER
29;;; Code:
30
63afb1f8
RS
31(defvar timer-program (expand-file-name "timer" exec-directory)
32 "The name of the program to run as the timer subprocess.
33It should normally be in the exec-directory.")
77328ee1 34
5cc564a6 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
63afb1f8
RS
43;; Error symbols for timers
44(put 'timer-error 'error-conditions '(error timer-error))
45(put 'timer-error 'error-message "Timer error")
46
47(put 'timer-abnormal-termination
48 'error-conditions
49 '(error timer-error timer-abnormal-termination))
50(put 'timer-abnormal-termination
51 'error-message
52 "Timer exited abnormally--all events cancelled")
53
54(put 'timer-filter-error
55 'error-conditions
56 '(error timer-error timer-filter-error))
57(put 'timer-filter-error
58 'error-message
59 "Error in timer process filter")
60
61
e2ec008d
RS
62;; This should not be necessary, but on some systems, we get
63;; unkillable processes without this.
64;; It may be a kernel bug, but that's not certain.
65(defun timer-kill-emacs-hook ()
66 (if timer-process
67 (progn
68 (set-process-sentinel timer-process nil)
69 (set-process-filter timer-process nil)
70 (delete-process timer-process))))
71(add-hook 'kill-emacs-hook 'timer-kill-emacs-hook)
72
49116ac0 73;;;###autoload
5cc564a6 74(defun run-at-time (time repeat function &rest args)
75 "Run a function at a time, and optionally on a regular interval.
76Arguments are TIME, REPEAT, FUNCTION &rest ARGS.
16ad0a71
RS
77TIME, a string, can be specified absolutely or relative to now.
78TIME can also be an integer, a number of seconds.
5cc564a6 79REPEAT, an integer number of seconds, is the interval on which to repeat
b2e609ff 80the call to the function. If REPEAT is nil or 0, call it just once.
b6341cd1
JB
81
82Absolute times may be specified in a wide variety of formats;
83Something of the form `HOUR:MIN:SEC TIMEZONE MONTH/DAY/YEAR', where
16ad0a71
RS
84all fields are numbers, works; the format used by the Unix `date'
85command works too.
b6341cd1
JB
86
87Relative times may be specified as a series of numbers followed by units:
88 1 min denotes one minute from now.
89 min does too.
90 1 min 5 sec denotes 65 seconds from now.
91 1 min 2 sec 3 hour 4 day 5 week 6 fortnight 7 month 8 year
92 denotes the sum of all the given durations from now."
5cc564a6 93 (interactive "sRun at time: \nNRepeat interval: \naFunction: ")
b2e609ff
RS
94 (if (equal repeat 0)
95 (setq repeat nil))
16ad0a71
RS
96 ;; Make TIME a string.
97 (if (integerp time)
98 (setq time (format "%d sec" time)))
5cc564a6 99 (cond ((or (not timer-process)
100 (memq (process-status timer-process) '(exit signal nil)))
101 (if timer-process (delete-process timer-process))
77328ee1
JB
102 (setq timer-process
103 (let ((process-connection-type nil))
63afb1f8 104 (start-process "timer" nil timer-program))
5cc564a6 105 timer-alist nil)
106 (set-process-filter timer-process 'timer-process-filter)
107 (set-process-sentinel timer-process 'timer-process-sentinel)
108 (process-kill-without-query timer-process))
109 ((eq (process-status timer-process) 'stop)
110 (continue-process timer-process)))
111 ;; There should be a living, breathing timer process now
16ad0a71
RS
112 (let* ((token (concat (current-time-string) "-" (length timer-alist)))
113 (elt (list token repeat function args)))
a973c898 114 (process-send-string timer-process (concat time "@" token "\n"))
16ad0a71
RS
115 (setq timer-alist (cons elt timer-alist))
116 elt))
117
118(defun cancel-timer (elt)
119 "Cancel a timer previously made with `run-at-time'.
120The argument should be a value previously returned by `run-at-time'.
121Cancelling the timer means that nothing special
122will happen at the specified time."
123 (setcar (cdr elt) nil)
124 (setcar (cdr (cdr elt)) 'ignore))
5cc564a6 125
126(defun timer-process-filter (proc str)
1f66361e
RS
127 (setq timer-out (concat timer-out str))
128 (let (do token error)
129 (while (string-match "\n" timer-out)
130 (setq token (substring timer-out 0 (match-beginning 0))
131 do (assoc token timer-alist)
132 timer-out (substring timer-out (match-end 0)))
133 (cond
134 (do
135 (apply (nth 2 do) (nth 3 do)) ; do it
136 (if (natnump (nth 1 do)) ; reschedule it
137 (send-string proc (concat (nth 1 do) " sec@" (car do) "\n"))
138 (setq timer-alist (delq do timer-alist))))
139 ((string-match "timer: \\([^:]+\\): \\([^@]*\\)@\\(.*\\)$" token)
140 (setq error (substring token (match-beginning 1) (match-end 1))
141 do (substring token (match-beginning 2) (match-end 2))
142 token (assoc (substring token (match-beginning 3) (match-end 3))
143 timer-alist)
144 timer-alist (delq token timer-alist))
145 (or timer-alist
146 timer-dont-exit
147 (process-send-eof proc))
148 ;; Update error message for this particular instance
149 (put 'timer-filter-error
150 'error-message
151 (format "%s for %s; couldn't set at \"%s\""
152 error (nth 2 token) do))
153 (signal 'timer-filter-error (list proc str)))))
154 (or timer-alist timer-dont-exit (process-send-eof proc))))
5cc564a6 155
156(defun timer-process-sentinel (proc str)
157 (let ((stat (process-status proc)))
63afb1f8
RS
158 (if (eq stat 'stop)
159 (continue-process proc)
5cc564a6 160 ;; if it exited normally, presumably it was intentional.
161 ;; if there were no pending events, who cares that it exited?
63afb1f8
RS
162 (or (null timer-alist)
163 (eq stat 'exit)
164 (let ((alist timer-alist))
165 (setq timer-process nil timer-alist nil)
166 (signal 'timer-abnormal-termination (list proc stat str alist))))
1f3a7283
RS
167 ;; Used to set timer-scratch to "", but nothing uses that var.
168 (setq timer-process nil timer-alist nil))))
5cc564a6 169
046c6887 170(defun cancel-function-timers (function)
1433a222 171 "Cancel all events scheduled by `run-at-time' which would run FUNCTION."
046c6887 172 (interactive "aCancel timers of function: ")
5cc564a6 173 (let ((alist timer-alist))
174 (while alist
175 (if (eq (nth 2 (car alist)) function)
176 (setq timer-alist (delq (car alist) timer-alist)))
177 (setq alist (cdr alist))))
178 (or timer-alist timer-dont-exit (process-send-eof timer-process)))
179
180(provide 'timer)
d501f516
ER
181
182;;; timer.el ends here