annotate lisp/=timer.el @ 638:40b255f55df3

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