Add standard library headers.
[emacs.git] / lisp / timer.el
blob8e0b5a3c7eb0f812b49a39d459cb4d5f255edd47
1 ;;; timer.el --- run a function with args at some time in future
3 ;; Copyright (C) 1990, 1993 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 ;;; Code:
25 (defvar timer-process nil)
26 (defvar timer-alist ())
27 (defvar timer-out "")
28 (defvar timer-dont-exit nil
29 ;; this is useful for functions which will be doing their own erratic
30 ;; rescheduling or people who otherwise expect to use the process frequently
31 "If non-nil, don't exit the timer process when no more events are pending.")
33 ;;;###autoload
34 (defun run-at-time (time repeat function &rest args)
35 "Run a function at a time, and optionally on a regular interval.
36 Arguments are TIME, REPEAT, FUNCTION &rest ARGS.
37 TIME, a string, can be specified absolutely or relative to now.
38 TIME can also be an integer, a number of seconds.
39 REPEAT, an integer number of seconds, is the interval on which to repeat
40 the call to the function. If REPEAT is nil, call it just once.
42 Absolute times may be specified in a wide variety of formats;
43 Something of the form `HOUR:MIN:SEC TIMEZONE MONTH/DAY/YEAR', where
44 all fields are numbers, works; the format used by the Unix `date'
45 command works too.
47 Relative times may be specified as a series of numbers followed by units:
48 1 min denotes one minute from now.
49 min does too.
50 1 min 5 sec denotes 65 seconds from now.
51 1 min 2 sec 3 hour 4 day 5 week 6 fortnight 7 month 8 year
52 denotes the sum of all the given durations from now."
53 (interactive "sRun at time: \nNRepeat interval: \naFunction: ")
54 ;; Make TIME a string.
55 (if (integerp time)
56 (setq time (format "%d sec" time)))
57 (cond ((or (not timer-process)
58 (memq (process-status timer-process) '(exit signal nil)))
59 (if timer-process (delete-process timer-process))
60 (setq timer-process (let ((process-connection-type nil))
61 (start-process "timer" nil "timer"))
62 timer-alist nil)
63 (set-process-filter timer-process 'timer-process-filter)
64 (set-process-sentinel timer-process 'timer-process-sentinel)
65 (process-kill-without-query timer-process))
66 ((eq (process-status timer-process) 'stop)
67 (continue-process timer-process)))
68 ;; There should be a living, breathing timer process now
69 (let* ((token (concat (current-time-string) "-" (length timer-alist)))
70 (elt (list token repeat function args)))
71 (process-send-string timer-process (concat time "@" token "\n"))
72 (setq timer-alist (cons elt timer-alist))
73 elt))
75 (defun cancel-timer (elt)
76 "Cancel a timer previously made with `run-at-time'.
77 The argument should be a value previously returned by `run-at-time'.
78 Cancelling the timer means that nothing special
79 will happen at the specified time."
80 (setcar (cdr elt) nil)
81 (setcar (cdr (cdr elt)) 'ignore))
83 (defun timer-process-filter (proc str)
84 (setq timer-out (concat timer-out str))
85 (let (do token error)
86 (while (string-match "\n" timer-out)
87 (setq token (substring timer-out 0 (match-beginning 0))
88 do (assoc token timer-alist)
89 timer-out (substring timer-out (match-end 0)))
90 (cond
91 (do
92 (apply (nth 2 do) (nth 3 do)) ; do it
93 (if (natnump (nth 1 do)) ; reschedule it
94 (send-string proc (concat (nth 1 do) " sec@" (car do) "\n"))
95 (setq timer-alist (delq do timer-alist))))
96 ((string-match "timer: \\([^:]+\\): \\([^@]*\\)@\\(.*\\)$" token)
97 (setq error (substring token (match-beginning 1) (match-end 1))
98 do (substring token (match-beginning 2) (match-end 2))
99 token (assoc (substring token (match-beginning 3) (match-end 3))
100 timer-alist)
101 timer-alist (delq token timer-alist))
102 (ding 'no-terminate) ; using error function in process filters is rude
103 (message "%s for %s; couldn't set at \"%s\"" error (nth 2 token) do))))
104 (or timer-alist timer-dont-exit (process-send-eof proc))))
106 (defun timer-process-sentinel (proc str)
107 (let ((stat (process-status proc)))
108 (if (eq stat 'stop) (continue-process proc)
109 ;; if it exited normally, presumably it was intentional.
110 ;; if there were no pending events, who cares that it exited?
111 (if (or (not timer-alist) (eq stat 'exit)) ()
112 (ding 'no-terminate)
113 (message "Timer exited abnormally. All events cancelled."))
114 ;; Used to set timer-scratch to "", but nothing uses that var.
115 (setq timer-process nil timer-alist nil))))
117 (defun cancel-timer (function)
118 "Cancel all events scheduled by `run-at-time' which would run FUNCTION."
119 (interactive "aCancel function: ")
120 (let ((alist timer-alist))
121 (while alist
122 (if (eq (nth 2 (car alist)) function)
123 (setq timer-alist (delq (car alist) timer-alist)))
124 (setq alist (cdr alist))))
125 (or timer-alist timer-dont-exit (process-send-eof timer-process)))
127 (provide 'timer)
129 ;;; timer.el ends here