1 ;;; timer.el --- run a function with args at some time in future.
3 ;; Copyright (C) 1996 Free Software Foundation, Inc.
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)
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 the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
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.
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."))
38 (defalias 'timer-create 'make-itimer)
40 (defalias 'timerp 'itimerp)
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)
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))))
54 (setq timer-when itimer-short-interval))
55 (set-itimer-value timer timer-when))
58 (set-itimer-restart timer delta))
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)
68 (set-itimer-restart timer secs))
71 ;; timer-next-integral-multiple-of-time and timer-relative-time have moved
72 ;; to xemacs-base/timer-funcs.el
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."
79 (+ (itimer-value timer) secs (if usecs (/ usecs 1000000.0) 0))))
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))
92 (setcar (nthcdr 2 list) usecs)
93 (set-itimer-value timer (itimer-time-difference list (current-time)))
96 (set-itimer-restart timer delta))
98 (make-obsolete 'timer-set-time-with-usecs
99 "use `timer-set-time' and `timer-inc-time' instead.")
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)
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))
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)))
121 ;; can't do this, different kind of timer
122 ;;(defalias 'disable-timeout 'cancel-timer)
124 (defun cancel-timer (timer)
125 "Remove TIMER from the list of active timers."
127 (error "Invalid timer"))
128 (delete-itimer timer)
131 ;; cancel-function-timers has moved to xemacs-base/timer-funcs.el
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)))
139 ;; timer-event-handler and timeout-event-p are internal functions (not
140 ;; called by users). We do it differently; see itimer.el.
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.
150 ;;; timer.el ends here