Mercurial > emacs
annotate lisp/=timer.el @ 1091:3302f6e93ce5
*** empty log message ***
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Fri, 11 Sep 1992 09:04:24 +0000 |
parents | 61c6983219ff |
children | 45f9cbd79ede |
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 |
840
113281b361ec
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
807
diff
changeset
|
3 ;; Copyright (C) 1990 Free Software Foundation, Inc. |
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 | 6 |
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 | |
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 | 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 | |
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 | 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.") | |
32 | |
584 | 33 ;;;###autoload |
45 | 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 REPEAT, an integer number of seconds, is the interval on which to repeat | |
39 the call to the function." | |
40 (interactive "sRun at time: \nNRepeat interval: \naFunction: ") | |
41 (cond ((or (not timer-process) | |
42 (memq (process-status timer-process) '(exit signal nil))) | |
43 (if timer-process (delete-process timer-process)) | |
44 (setq timer-process (start-process "timer" nil "timer") | |
45 timer-alist nil) | |
46 (set-process-filter timer-process 'timer-process-filter) | |
47 (set-process-sentinel timer-process 'timer-process-sentinel) | |
48 (process-kill-without-query timer-process)) | |
49 ((eq (process-status timer-process) 'stop) | |
50 (continue-process timer-process))) | |
51 ;; There should be a living, breathing timer process now | |
52 (let ((token (concat (current-time-string) "-" (length timer-alist)))) | |
998 | 53 (send-string timer-process (concat time "@" token "\n")) |
45 | 54 (setq timer-alist (cons (list token repeat function args) timer-alist)))) |
55 | |
56 (defun timer-process-filter (proc str) | |
57 (setq timer-out (concat timer-out str)) | |
58 (let (do token error) | |
59 (while (string-match "\n" timer-out) | |
60 (setq token (substring timer-out 0 (match-beginning 0)) | |
61 do (assoc token timer-alist) | |
62 timer-out (substring timer-out (match-end 0))) | |
63 (cond | |
64 (do (apply (nth 2 do) (nth 3 do)) ; do it | |
65 (if (natnump (nth 1 do)) ; reschedule it | |
998 | 66 (send-string proc (concat (nth 1 do) " sec@" (car do) "\n")) |
45 | 67 (setq timer-alist (delq do timer-alist)))) |
998 | 68 ((string-match "timer: \\([^:]+\\): \\([^@]*\\)@\\(.*\\)$" token) |
45 | 69 (setq error (substring token (match-beginning 1) (match-end 1)) |
70 do (substring token (match-beginning 2) (match-end 2)) | |
71 token (assoc (substring token (match-beginning 3) (match-end 3)) | |
72 timer-alist) | |
73 timer-alist (delq token timer-alist)) | |
74 (ding 'no-terminate) ; using error function in process filters is rude | |
75 (message "%s for %s; couldn't set at \"%s\"" error (nth 2 token) do)))) | |
76 (or timer-alist timer-dont-exit (process-send-eof proc)))) | |
77 | |
78 (defun timer-process-sentinel (proc str) | |
79 (let ((stat (process-status proc))) | |
80 (if (eq stat 'stop) (continue-process proc) | |
81 ;; if it exited normally, presumably it was intentional. | |
82 ;; if there were no pending events, who cares that it exited? | |
83 (if (or (not timer-alist) (eq stat 'exit)) () | |
84 (ding 'no-terminate) | |
85 (message "Timer exited abnormally. All events cancelled.")) | |
86 (setq timer-process nil timer-alist nil timer-scratch "")))) | |
87 | |
88 (defun cancel-timer (function) | |
89 "Cancel all events scheduled by ``run-at-time'' which would run FUNCTION." | |
90 (interactive "aCancel function: ") | |
91 (let ((alist timer-alist)) | |
92 (while alist | |
93 (if (eq (nth 2 (car alist)) function) | |
94 (setq timer-alist (delq (car alist) timer-alist))) | |
95 (setq alist (cdr alist)))) | |
96 (or timer-alist timer-dont-exit (process-send-eof timer-process))) | |
97 | |
98 (provide 'timer) | |
657
fec3f9a1e3e5
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
99 |
fec3f9a1e3e5
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
584
diff
changeset
|
100 ;;; timer.el ends here |