(tcl-add-fsf-menu): Use make-lucid-menu-keymap, not
[emacs.git] / lisp / timer.el
blob192df175753f64f57e0794bb2f493a2c8c26fcb9
1 ;;; timer.el --- run a function with args at some time in future
3 ;; Copyright (C) 1990, 1993, 1994 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
7 ;; This file is part of GNU Emacs.
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
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
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.
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.
23 ;;; Commentary:
25 ;; This package gives you the capability to run Emacs Lisp commands at
26 ;; specified times in the future, either as one-shots or periodically.
27 ;; The single entry point is `run-at-time'.
29 ;;; Code:
31 (defvar timer-program (expand-file-name "timer" exec-directory)
32 "The name of the program to run as the timer subprocess.
33 It should normally be in the exec-directory.")
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.")
43 ;; Error symbols for timers
44 (put 'timer-error 'error-conditions '(error timer-error))
45 (put 'timer-error 'error-message "Timer error")
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")
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")
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)
73 ;;;###autoload
74 (defun run-at-time (time repeat function &rest args)
75 "Run a function at a time, and optionally on a regular interval.
76 Arguments are TIME, REPEAT, FUNCTION &rest ARGS.
77 TIME, a string, can be specified absolutely or relative to now.
78 TIME can also be an integer, a number of seconds.
79 REPEAT, an integer number of seconds, is the interval on which to repeat
80 the call to the function. If REPEAT is nil or 0, call it just once.
82 Absolute times may be specified in a wide variety of formats;
83 Something of the form `HOUR:MIN:SEC TIMEZONE MONTH/DAY/YEAR', where
84 all fields are numbers, works; the format used by the Unix `date'
85 command works too.
87 Relative 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."
93 (interactive "sRun at time: \nNRepeat interval: \naFunction: ")
94 (if (equal repeat 0)
95 (setq repeat nil))
96 ;; Make TIME a string.
97 (if (integerp time)
98 (setq time (format "%d sec" time)))
99 (cond ((or (not timer-process)
100 (memq (process-status timer-process) '(exit signal nil)))
101 (if timer-process (delete-process timer-process))
102 (setq timer-process
103 (let ((process-connection-type nil))
104 (start-process "timer" nil timer-program))
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
112 (let* ((token (concat (current-time-string) "-" (length timer-alist)))
113 (elt (list token repeat function args)))
114 (process-send-string timer-process (concat time "@" token "\n"))
115 (setq timer-alist (cons elt timer-alist))
116 elt))
118 (defun cancel-timer (elt)
119 "Cancel a timer previously made with `run-at-time'.
120 The argument should be a value previously returned by `run-at-time'.
121 Cancelling the timer means that nothing special
122 will happen at the specified time."
123 (setcar (cdr elt) nil)
124 (setcar (cdr (cdr elt)) 'ignore))
126 (defun timer-process-filter (proc str)
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
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))))
156 (defun timer-process-sentinel (proc str)
157 (let ((stat (process-status proc)))
158 (if (eq stat 'stop)
159 (continue-process proc)
160 ;; if it exited normally, presumably it was intentional.
161 ;; if there were no pending events, who cares that it exited?
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))))
167 ;; Used to set timer-scratch to "", but nothing uses that var.
168 (setq timer-process nil timer-alist nil))))
170 (defun cancel-function-timers (function)
171 "Cancel all events scheduled by `run-at-time' which would run FUNCTION."
172 (interactive "aCancel timers of function: ")
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)))
180 (provide 'timer)
182 ;;; timer.el ends here