Initial Commit
[packages] / xemacs-packages / fsf-compat / timer.el
1 ;;; timer.el --- run a function with args at some time in future.
2
3 ;; Copyright (C) 1996 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
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
11 ;; the Free Software Foundation; either version 2, or (at your option)
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 the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;; This package gives you the capability to run Emacs Lisp commands at
27 ;; specified times in the future, either as one-shots or periodically.
28
29 ;;; Code:
30
31 (unless (fboundp #'run-at-time)
32   (warn "While loading fsf-compat, run-at-time was not fboundp.  This
33 typically means your fsf-compat package is newer than xemacs-base.  You
34 should update your xemacs-base package."))
35
36 (require 'itimer)
37
38 (defalias 'timer-create 'make-itimer)
39
40 (defalias 'timerp 'itimerp)
41
42 ;(defvar timer-idle-list nil
43 ;  "List of active idle-time timers in order of increasing time")
44 (defvaralias 'timer-idle-list 'itimer-list)
45 (defvaralias 'timer-list 'itimer-list)
46
47 (defun timer-set-time (timer time &optional delta)
48   "Set the trigger time of TIMER to TIME.
49 TIME must be in the internal format returned by, e.g., `current-time'.
50 If optional third argument DELTA is a positive integer, make the timer
51 fire repeatedly that many seconds apart."
52   (let ((timer-when (itimer-time-difference time (current-time))))
53     (or (> timer-when 0)
54         (setq timer-when itimer-short-interval))
55     (set-itimer-value timer timer-when))
56   (and (numberp delta)
57        (> delta 0)
58        (set-itimer-restart timer delta))
59   timer)
60
61 (defun timer-set-idle-time (timer secs &optional repeat)
62   "Set the trigger idle time of TIMER to SECS.
63 If optional third argument REPEAT is non-nil, make the timer
64 fire each time Emacs is idle for that many seconds."
65   (set-itimer-is-idle timer t)
66   (set-itimer-value timer secs)
67   (when repeat
68     (set-itimer-restart timer secs))
69   timer)
70
71 ;; timer-next-integral-multiple-of-time and timer-relative-time have moved
72 ;; to xemacs-base/timer-funcs.el
73
74 (defun timer-inc-time (timer secs &optional usecs)
75   "Increment the time set in TIMER by SECS seconds and USECS microseconds.
76 SECS may be a fraction.  If USECS is omitted, that means it is zero."
77   (set-itimer-value
78    timer
79    (+ (itimer-value timer) secs (if usecs (/ usecs 1000000.0) 0))))
80
81 (defun timer-set-time-with-usecs (timer time usecs &optional delta)
82   "Set the trigger time of TIMER to TIME plus USECS.
83 TIME must be in the internal format returned by, e.g., `current-time'.
84 The microsecond count from TIME is ignored, and USECS is used instead.
85 If optional fourth argument DELTA is a positive number, make the timer
86 fire repeatedly that many seconds apart."
87   (let ((list (list nil nil nil)))
88     (setcar list (car time))
89     (setcar (nthcdr 1 list) (if (consp (cdr time))
90                                 (car (cdr time))
91                               (cdr time)))
92     (setcar (nthcdr 2 list) usecs)
93     (set-itimer-value timer (itimer-time-difference list (current-time)))
94     (and (numberp delta)
95          (> delta 0)
96          (set-itimer-restart timer delta))
97     timer))
98 (make-obsolete 'timer-set-time-with-usecs
99                "use `timer-set-time' and `timer-inc-time' instead.")
100
101 (defun timer-set-function (timer function &optional args)
102   "Make TIMER call FUNCTION with optional ARGS when triggering."
103   (set-itimer-function timer function)
104   (set-itimer-function-arguments timer args)
105   (set-itimer-uses-arguments timer t)
106   timer)
107 \f
108 (defun timer-activate (timer &optional triggered-p)
109   "Put TIMER on the list of active timers.
110 TRIGGERED-P is for Emacs compatibility and is currently ignored."
111   (activate-itimer timer))
112
113 (defun timer-activate-when-idle (timer &optional dont-wait)
114   "Arrange to activate TIMER whenever Emacs is next idle.
115 DONT-WAIT is for Emacs compatibility and is currently ignored."
116   (when (and (not (itimer-live-p timer))
117              (not (get-itimer (itimer-name timer))))
118     (set-itimer-is-idle timer t)
119     (activate-itimer timer)))
120
121 ;; can't do this, different kind of timer
122 ;;(defalias 'disable-timeout 'cancel-timer)
123
124 (defun cancel-timer (timer)
125   "Remove TIMER from the list of active timers."
126   (or (timerp timer)
127       (error "Invalid timer"))
128   (delete-itimer timer)
129   nil)
130
131 ;; cancel-function-timers has moved to xemacs-base/timer-funcs.el
132
133 (defun timer-until (timer time)
134   "Calculate number of seconds from when TIMER will run, until TIME.
135 TIMER is a timer, and stands for the time when its next repeat is scheduled.
136 TIME is a time-list."
137   (- (itimer-time-difference time (current-time)) (itimer-value timer)))
138
139 ;; timer-event-handler and timeout-event-p are internal functions (not
140 ;; called by users).  We do it differently; see itimer.el.
141
142 ;; Except for add-timeout, the remaining functions in this file
143 ;; (run-at-time, run-with-timer, run-with-idle-timer, with-timeout-handler,
144 ;; with-timeout, and y-or-n-p-with-timeout) have moved to
145 ;; xemacs-base/timer-funcs.el.  The add-timeout function is an XEmacs builtin.
146
147 \f
148 (provide 'timer)
149
150 ;;; timer.el ends here