1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
|
;;; timer-funcs.el --- timer implementation-independent functions
;; Copyright (C) 1996 Free Software Foundation, Inc.
;; Maintainer: FSF
;; This file is part of XEmacs.
;; XEmacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; XEmacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; This file contains functions from Emacs timer.el that do not take timer
;; objects as parameters. This lets us use itimers as the implementation,
;; instead of emulated Emacs timers. Note that a few of these functions
;; RETURN itimers, however.
;;; Code:
(require 'itimer)
(defun timer-next-integral-multiple-of-time (time secs)
"Yield the next value after TIME that is an integral multiple of SECS.
More precisely, the next value, after TIME, that is an integral multiple
of SECS seconds since the epoch. SECS may be a fraction."
(let* ((time-base (ash 1 16))
(float-time-base (float time-base))
(million 1000000.0)
(time-usec (+ (* million (+ (* float-time-base (nth 0 time))
(nth 1 time)))
(nth 2 time)))
(secs-usec (* million secs))
(mod-usec (mod time-usec secs-usec))
(next-usec (+ (- time-usec mod-usec) secs-usec))
(time-base-million (* float-time-base million)))
(list (floor next-usec time-base-million)
(floor (mod next-usec time-base-million) million)
(floor (mod next-usec million)))))
(defun timer-relative-time (time secs &optional usecs)
"Advance TIME by SECS seconds and optionally USECS microseconds.
SECS may be a fraction."
(let ((high (car time))
(low (if (consp (cdr time)) (nth 1 time) (cdr time)))
(micro (if (numberp (car-safe (cdr-safe (cdr time))))
(nth 2 time)
0)))
;; Add
(if usecs (setq micro (+ micro usecs)))
(if (floatp secs)
(setq micro (+ micro (floor (* 1000000 (- secs (floor secs)))))))
(setq low (+ low (floor secs)))
;; Normalize
;; `/' rounds towards zero while `mod' returns a positive number,
;; so we can't rely on (= a (+ (* 100 (/ a 100)) (mod a 100))).
(setq low (+ low (/ micro 1000000) (if (< micro 0) -1 0)))
(setq micro (mod micro 1000000))
(setq high (+ high (/ low 65536) (if (< low 0) -1 0)))
(setq low (logand low 65535))
(list high low (and (/= micro 0) micro))))
(defun cancel-function-timers (function)
"Cancel all itimers scheduled by `run-at-time' which would run FUNCTION."
(interactive "aCancel itimers of function: ")
(dolist (itimer itimer-list)
(if (eq (itimer-function itimer) function)
(delete-itimer itimer))))
(define-error 'invalid-repetition-interval "Invalid repetition interval")
(define-error 'invalid-time "Invalid time format")
;;;###autoload
(defun run-at-time (time repeat function &rest args)
"Perform an action at time TIME.
Repeat the action every REPEAT seconds, if REPEAT is non-nil.
TIME should be a string recognized by `timer-parse-time', like \"11:23pm\",
nil meaning now, a number of seconds from now, a value from `current-time',
or t (with non-nil REPEAT) meaning the next integral multiple of REPEAT.
REPEAT may be an integer or floating point number.
The action is to call FUNCTION with arguments ARGS.
This function returns an itimer object which you can use in `delete-itimer'."
(interactive "sRun at time: \nNRepeat interval: \naFunction: ")
(or (null repeat)
(and (numberp repeat) (< 0 repeat))
(error 'invalid-repetition-interval repeat))
;; Special case: nil means "now" and is useful when repeating.
(if (null time)
(setq time 0))
;; Special case: t means the next integral multiple of REPEAT.
(if (and (eq time t) repeat)
(let ((now (current-time)))
(setq time
(itimer-time-difference
(timer-next-integral-multiple-of-time now repeat)
now))))
;; Handle relative times like "2 hours and 35 minutes"
(if (stringp time)
(let ((spec-time (timer-duration time)))
(if spec-time
(setq time spec-time))))
;; Handle "11:23pm" and the like. Interpret it as meaning today
;; which admittedly is rather stupid if we have passed that time
;; already. (Though only Emacs hackers hack Emacs at that time.)
(if (stringp time)
(let ((spec-time (timer-parse-time time)))
(if spec-time
(setq time
(ceiling
(itimer-time-difference spec-time (current-time)))))))
(or (numberp time)
(error 'invalid-time time))
(apply #'start-itimer "run-at-time" function time repeat nil t args))
;;;###autoload
(defun run-with-timer (secs repeat function &rest args)
"Perform an action after a delay of SECS seconds.
Repeat the action every REPEAT seconds, if REPEAT is non-nil.
SECS and REPEAT may be integers or floating point numbers.
The action is to call FUNCTION with arguments ARGS.
This function returns an itimer object which you can use in `delete-itimer'."
(interactive "sRun after delay (seconds): \nNRepeat interval: \naFunction: ")
(apply #'start-itimer "run-with-timer" function secs repeat nil t args))
;;;###autoload
(defun run-with-idle-timer (secs repeat function &rest args)
"Perform an action the next time XEmacs is idle for SECS seconds.
The action is to call FUNCTION with arguments ARGS.
SECS may be an integer or a floating point number.
If REPEAT is non-nil, do the action each time XEmacs has been idle for
exactly SECS seconds (that is, only once for each time XEmacs becomes idle).
This function returns an itimer object which you can use in `delete-itimer'."
(interactive
(list (read-from-minibuffer "Run after idle (seconds): " nil nil t)
(y-or-n-p "Repeat each time XEmacs is idle? ")
(intern (completing-read "Function: " obarray 'fboundp t))))
(apply #'start-itimer "run-with-idle-timer" function secs
(if repeat secs nil) t t args))
(defun with-timeout-handler (tag)
(throw tag 'timeout))
;;;###autoload (put 'with-timeout 'lisp-indent-function 1)
;;;###autoload
(defmacro with-timeout (list &rest body)
"Run BODY, but if it doesn't finish in SECONDS seconds, give up.
If we give up, we run the TIMEOUT-FORMS and return the value of the last one.
The call should look like:
(with-timeout (SECONDS TIMEOUT-FORMS...) BODY...)
The timeout is checked whenever XEmacs waits for some kind of external
event \(such as keyboard input, input from subprocesses, or a certain time);
if the program loops without waiting in any way, the timeout will not
be detected."
(let ((seconds (car list))
(timeout-forms (cdr list)))
`(let ((with-timeout-tag (cons nil nil))
with-timeout-value with-timeout-timer)
(unwind-protect
(if (catch with-timeout-tag
(progn
(setq with-timeout-timer
(start-itimer "with-timeout" #'with-timeout-handler
,seconds nil nil t with-timeout-tag))
(setq with-timeout-value (progn ,@body))
nil))
,@timeout-forms
with-timeout-value)
(delete-itimer with-timeout-timer)))))
;;;###autoload
(defun y-or-n-p-with-timeout (prompt seconds default-value)
"Like (y-or-n-p PROMPT), with a timeout.
If the user does not answer after SECONDS seconds, return DEFAULT-VALUE."
(with-timeout (seconds default-value)
(y-or-n-p prompt)))
(defvar timer-duration-words
(list (cons "microsec" 0.000001)
(cons "microsecond" 0.000001)
(cons "millisec" 0.001)
(cons "millisecond" 0.001)
(cons "sec" 1)
(cons "second" 1)
(cons "min" 60)
(cons "minute" 60)
(cons "hour" (* 60 60))
(cons "day" (* 24 60 60))
(cons "week" (* 7 24 60 60))
(cons "fortnight" (* 14 24 60 60))
(cons "month" (* 30 24 60 60)) ; Approximation
(cons "year" (* 365.25 24 60 60)) ; Approximation
)
"Alist mapping temporal words to durations in seconds")
(defun timer-duration (string)
"Return number of seconds specified by STRING, or nil if parsing fails."
(let ((secs 0)
(start 0)
(case-fold-search t))
(while (string-match
"[ \t]*\\([0-9.]+\\)?[ \t]*\\([a-z]+[a-rt-z]\\)s?[ \t]*"
string start)
(let ((count (if (match-beginning 1)
(string-to-number (match-string 1 string))
1))
(itemsize (cdr (assoc (match-string 2 string)
timer-duration-words))))
(if itemsize
(setq start (match-end 0)
secs (+ secs (* count itemsize)))
(setq secs nil
start (length string)))))
(if (= start (length string))
secs
(if (string-match "\\`[0-9.]+\\'" string)
(string-to-number string)))))
(defun timer-parse-time (s)
"Return an encoded time represesnting the time specifed by S.
This is a time in the same format as returned by `current-time'. This
function assumes that the specified time is during the current day.
S can be of the form XXXX, X:XX, XX:XX, XXXXXX, X:XX:XX, or XX:XX:XX (military
time), or any of the above followed by \"am\", \"AM\", \"pm\", or \"PM\". If
S does not describe a recognizable time, nil is returned."
(let ((case-fold-search t)
(now (decode-time))
hour minute second)
(cond ((string-match;; Hour only XXam or XXpm
"\\`[ \t\n\\^M]*\\([0-9]?[0-9]\\)\\([ap]\\)m\\>" s)
(setq hour (+ (% (string-to-number (match-string 1 s)) 12)
(if (char-equal ?a (aref s (match-beginning 2)))
0 12))
minute 0
second 0))
((string-match;; Hour and minute XX:XXam or XX:XXpm
"^[ \t]*\\([0-9]?[0-9]\\):\\([0-9][0-9]\\)\\([ap]\\)m\\>" s)
(setq hour (+ (% (string-to-number (match-string 1 s)) 12)
(if (char-equal ?a (aref s (match-beginning 3)))
0 12))
minute (string-to-number (match-string 2 s))
second 0))
((string-match;; Hour, minute, and seconds XX:XX:XXam or XX:XX:XXpm
"^[ \t]*\\([0-9]?[0-9]\\):\\([0-9][0-9]\\):\\([0-9][0-9]\\)\\([ap]\\)m\\>" s)
(setq hour (+ (% (string-to-number (match-string 1 s)) 12)
(if (char-equal ?a (aref s (match-beginning 4)))
0 12))
minute (string-to-number (match-string 2 s))
second (string-to-number (match-string 3 s))))
((string-match;; Military time
"\\`[ \t\n\\^M]*\\([0-9]?[0-9]\\)[:.]?\\([0-9][0-9]\\)[:.]?\\([0-9][0-9]\\)?\\(\\>\\|[^ap]\\)" s)
(setq hour (string-to-number (match-string 1 s))
minute (string-to-number (match-string 2 s))
second (if (match-beginning 3)
(string-to-number (match-string 3 s))
0))))
(if hour
(nconc
(encode-time
second minute hour (nth 3 now) (nth 4 now) (nth 5 now) (nth 8 now))
'(0)))))
(provide 'timer-funcs)
;;; timer-funcs.el ends here
|