5cc564a6 |
1 | ;; Run a function with args at some time in future |
2 | ;; Copyright (C) 1990 Free Software Foundation, Inc. |
3 | |
4 | ;; This file is part of GNU Emacs. |
5 | |
6 | ;; GNU Emacs is free software; you can redistribute it and/or modify |
7 | ;; it under the terms of the GNU General Public License as published by |
8 | ;; the Free Software Foundation; either version 1, or (at your option) |
9 | ;; any later version. |
10 | |
11 | ;; GNU Emacs is distributed in the hope that it will be useful, |
12 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | ;; GNU General Public License for more details. |
15 | |
16 | ;; You should have received a copy of the GNU General Public License |
17 | ;; along with GNU Emacs; see the file COPYING. If not, write to |
18 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
19 | |
20 | (defvar timer-process nil) |
21 | (defvar timer-alist ()) |
22 | (defvar timer-out "") |
23 | (defvar timer-dont-exit nil |
24 | ;; this is useful for functions which will be doing their own erratic |
25 | ;; rescheduling or people who otherwise expect to use the process frequently |
26 | "If non-nil, don't exit the timer process when no more events are pending.") |
27 | |
28 | (defun run-at-time (time repeat function &rest args) |
29 | "Run a function at a time, and optionally on a regular interval. |
30 | Arguments are TIME, REPEAT, FUNCTION &rest ARGS. |
31 | TIME, a string, can be specified absolutely or relative to now. |
32 | REPEAT, an integer number of seconds, is the interval on which to repeat |
33 | the call to the function." |
34 | (interactive "sRun at time: \nNRepeat interval: \naFunction: ") |
35 | (cond ((or (not timer-process) |
36 | (memq (process-status timer-process) '(exit signal nil))) |
37 | (if timer-process (delete-process timer-process)) |
38 | (setq timer-process (start-process "timer" nil "timer") |
39 | timer-alist nil) |
40 | (set-process-filter timer-process 'timer-process-filter) |
41 | (set-process-sentinel timer-process 'timer-process-sentinel) |
42 | (process-kill-without-query timer-process)) |
43 | ((eq (process-status timer-process) 'stop) |
44 | (continue-process timer-process))) |
45 | ;; There should be a living, breathing timer process now |
46 | (let ((token (concat (current-time-string) "-" (length timer-alist)))) |
47 | (send-string timer-process (concat time "\001" token "\n")) |
48 | (setq timer-alist (cons (list token repeat function args) timer-alist)))) |
49 | |
50 | (defun timer-process-filter (proc str) |
51 | (setq timer-out (concat timer-out str)) |
52 | (let (do token error) |
53 | (while (string-match "\n" timer-out) |
54 | (setq token (substring timer-out 0 (match-beginning 0)) |
55 | do (assoc token timer-alist) |
56 | timer-out (substring timer-out (match-end 0))) |
57 | (cond |
58 | (do (apply (nth 2 do) (nth 3 do)) ; do it |
59 | (if (natnump (nth 1 do)) ; reschedule it |
60 | (send-string proc (concat (nth 1 do) " sec\001" (car do) "\n")) |
61 | (setq timer-alist (delq do timer-alist)))) |
62 | ((string-match "timer: \\([^:]+\\): \\([^\001]*\\)\001\\(.*\\)$" token) |
63 | (setq error (substring token (match-beginning 1) (match-end 1)) |
64 | do (substring token (match-beginning 2) (match-end 2)) |
65 | token (assoc (substring token (match-beginning 3) (match-end 3)) |
66 | timer-alist) |
67 | timer-alist (delq token timer-alist)) |
68 | (ding 'no-terminate) ; using error function in process filters is rude |
69 | (message "%s for %s; couldn't set at \"%s\"" error (nth 2 token) do)))) |
70 | (or timer-alist timer-dont-exit (process-send-eof proc)))) |
71 | |
72 | (defun timer-process-sentinel (proc str) |
73 | (let ((stat (process-status proc))) |
74 | (if (eq stat 'stop) (continue-process proc) |
75 | ;; if it exited normally, presumably it was intentional. |
76 | ;; if there were no pending events, who cares that it exited? |
77 | (if (or (not timer-alist) (eq stat 'exit)) () |
78 | (ding 'no-terminate) |
79 | (message "Timer exited abnormally. All events cancelled.")) |
80 | (setq timer-process nil timer-alist nil timer-scratch "")))) |
81 | |
82 | (defun cancel-timer (function) |
83 | "Cancel all events scheduled by ``run-at-time'' which would run FUNCTION." |
84 | (interactive "aCancel function: ") |
85 | (let ((alist timer-alist)) |
86 | (while alist |
87 | (if (eq (nth 2 (car alist)) function) |
88 | (setq timer-alist (delq (car alist) timer-alist))) |
89 | (setq alist (cdr alist)))) |
90 | (or timer-alist timer-dont-exit (process-send-eof timer-process))) |
91 | |
92 | (provide 'timer) |