51349
+ ��膩��� 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 ;; Layout of a timer vector:
+ ��膩��� 32 ;; [triggered-p high-seconds low-seconds usecs repeat-delay
+ ��膩��� 33 ;; function args idle-delay]
+ ��膩��� 34
+ ��膩��� 35 (defun timer-create ()
+ ��膩��� 36 "Create a timer object."
+ ��膩��� 37 (let ((timer (make-vector 8 nil)))
+ ��膩��� 38 (aset timer 0 t)
+ ��膩��� 39 timer))
+ ��膩��� 40
+ ��膩��� 41 (defun timerp (object)
+ ��膩��� 42 "Return t if OBJECT is a timer."
+ ��膩��� 43 (and (vectorp object) (= (length object) 8)))
+ ��膩��� 44
+ ��膩��� 45 (defun timer-set-time (timer time &optional delta)
+ ��膩��� 46 "Set the trigger time of TIMER to TIME.
+ ��膩��� 47 TIME must be in the internal format returned by, e.g., `current-time'.
+ ��膩��� 48 If optional third argument DELTA is a positive number, make the timer
+ ��膩��� 49 fire repeatedly that many seconds apart."
+ ��膩��� 50 (or (timerp timer)
+ ��膩��� 51 (error "Invalid timer"))
+ ��膩��� 52 (aset timer 1 (car time))
+ ��膩��� 53 (aset timer 2 (if (consp (cdr time)) (car (cdr time)) (cdr time)))
+ ��膩��� 54 (aset timer 3 (or (and (consp (cdr time)) (consp (cdr (cdr time)))
+ ��膩��� 55 (nth 2 time))
+ ��膩��� 56 0))
+ ��膩��� 57 (aset timer 4 (and (numberp delta) (> delta 0) delta))
+ ��膩��� 58 timer)
+ ��膩��� 59
+ ��膩��� 60 (defun timer-set-idle-time (timer secs &optional repeat)
+ ��膩��� 61 "Set the trigger idle time of TIMER to SECS.
+ ��膩��� 62 If optional third argument REPEAT is non-nil, make the timer
+ ��膩��� 63 fire each time Emacs is idle for that many seconds."
+ ��膩��� 64 (or (timerp timer)
+ ��膩��� 65 (error "Invalid timer"))
+ ��膩��� 66 (aset timer 1 0)
+ ��膩��� 67 (aset timer 2 0)
+ ��膩��� 68 (aset timer 3 0)
+ ��膩��� 69 (timer-inc-time timer secs)
+ ��膩��� 70 (aset timer 4 repeat)
+ ��膩��� 71 timer)
+ ��膩��� 72
+ ��膩��� 73 (defun timer-next-integral-multiple-of-time (time secs)
+ ��膩��� 74 "Yield the next value after TIME that is an integral multiple of SECS.
+ ��膩��� 75 More precisely, the next value, after TIME, that is an integral multiple
+ ��膩��� 76 of SECS seconds since the epoch. SECS may be a fraction."
+ ��膩��� 77 (let ((time-base (ash 1 16)))
+ ��膩��� 78 (if (fboundp 'atan)
+ ��膩��� 79 ;; Use floating point, taking care to not lose precision.
+ ��膩��� 80 (let* ((float-time-base (float time-base))
+ ��膩��� 81 (million 1000000.0)
+ ��膩��� 82 (time-usec (+ (* million
+ ��膩��� 83 (+ (* float-time-base (nth 0 time))
+ ��膩��� 84 (nth 1 time)))
+ ��膩��� 85 (nth 2 time)))
+ ��膩��� 86 (secs-usec (* million secs))
+ ��膩��� 87 (mod-usec (mod time-usec secs-usec))
+ ��膩��� 88 (next-usec (+ (- time-usec mod-usec) secs-usec))
+ ��膩��� 89 (time-base-million (* float-time-base million)))
+ ��膩��� 90 (list (floor next-usec time-base-million)
+ ��膩��� 91 (floor (mod next-usec time-base-million) million)
+ ��膩��� 92 (floor (mod next-usec million))))
+ ��膩��� 93 ;; Floating point is not supported.
+ ��膩��� 94 ;; Use integer arithmetic, avoiding overflow if possible.
+ ��膩��� 95 (let* ((mod-sec (mod (+ (* (mod time-base secs)
+ ��膩��� 96 (mod (nth 0 time) secs))
+ ��膩��� 97 (nth 1 time))
+ ��膩��� 98 secs))
+ ��膩��� 99 (next-1-sec (+ (- (nth 1 time) mod-sec) secs)))
+ ��膩��� 100 (list (+ (nth 0 time) (floor next-1-sec time-base))
+ ��膩��� 101 (mod next-1-sec time-base)
+ ��膩��� 102 0)))))
+ ��膩��� 103
+ ��膩��� 104 (defun timer-relative-time (time secs &optional usecs)
+ ��膩��� 105 "Advance TIME by SECS seconds and optionally USECS microseconds.
+ ��膩��� 106 SECS may be a fraction."
+ ��膩��� 107 (let ((high (car time))
+ ��膩��� 108 (low (if (consp (cdr time)) (nth 1 time) (cdr time)))
+ ��膩��� 109 (micro (if (numberp (car-safe (cdr-safe (cdr time))))
+ ��膩��� 110 (nth 2 time)
+ ��膩��� 111 0)))
+ ��膩��� 112 ;; Add
+ ��膩��� 113 (if usecs (setq micro (+ micro usecs)))
+ ��膩��� 114 (if (floatp secs)
+ ��膩��� 115 (setq micro (+ micro (floor (* 1000000 (- secs (floor secs)))))))
+ ��膩��� 116 (setq low (+ low (floor secs)))
+ ��膩��� 117
+ ��膩��� 118 ;; Normalize
+ ��膩��� 119 ;; `/' rounds towards zero while `mod' returns a positive number,
+ ��膩��� 120 ;; so we can't rely on (= a (+ (* 100 (/ a 100)) (mod a 100))).
+ ��膩��� 121 (setq low (+ low (/ micro 1000000) (if (< micro 0) -1 0)))
+ ��膩��� 122 (setq micro (mod micro 1000000))
+ ��膩��� 123 (setq high (+ high (/ low 65536) (if (< low 0) -1 0)))
+ ��膩��� 124 (setq low (logand low 65535))
+ ��膩��� 125
+ ��膩��� 126 (list high low (and (/= micro 0) micro))))
+ ��膩��� 127
+ ��膩��� 128 (defun timer-inc-time (timer secs &optional usecs)
+ ��膩��� 129 "Increment the time set in TIMER by SECS seconds and USECS microseconds.
+ ��膩��� 130 SECS may be a fraction. If USECS is omitted, that means it is zero."
+ ��膩��� 131 (let ((time (timer-relative-time
+ ��膩��� 132 (list (aref timer 1) (aref timer 2) (aref timer 3))
+ ��膩��� 133 secs
+ ��膩��� 134 usecs)))
+ ��膩��� 135 (aset timer 1 (nth 0 time))
+ ��膩��� 136 (aset timer 2 (nth 1 time))
+ ��膩��� 137 (aset timer 3 (or (nth 2 time) 0))))
+ ��膩��� 138
+ ��膩��� 139 (defun timer-set-time-with-usecs (timer time usecs &optional delta)
+ ��膩��� 140 "Set the trigger time of TIMER to TIME plus USECS.
+ ��膩��� 141 TIME must be in the internal format returned by, e.g., `current-time'.
+ ��膩��� 142 The microsecond count from TIME is ignored, and USECS is used instead.
+ ��膩��� 143 If optional fourth argument DELTA is a positive number, make the timer
+ ��膩��� 144 fire repeatedly that many seconds apart."
+ ��膩��� 145 (or (timerp timer)
+ ��膩��� 146 (error "Invalid timer"))
+ ��膩��� 147 (aset timer 1 (nth 0 time))
+ ��膩��� 148 (aset timer 2 (nth 1 time))
+ ��膩��� 149 (aset timer 3 usecs)
+ ��膩��� 150 (aset timer 4 (and (numberp delta) (> delta 0) delta))
+ ��膩��� 151 timer)
+ ��膩��� 152 (make-obsolete 'timer-set-time-with-usecs
+ ��膩��� 153 "use `timer-set-time' and `timer-inc-time' instead."
+ ��膩��� 154 "21.4")
+ ��膩��� 155
+ ��膩��� 156 (defun timer-set-function (timer function &optional args)
+ ��膩��� 157 "Make TIMER call FUNCTION with optional ARGS when triggering."
+ ��膩��� 158 (or (timerp timer)
+ ��膩��� 159 (error "Invalid timer"))
+ ��膩��� 160 (aset timer 5 function)
+ ��膩��� 161 (aset timer 6 args)
+ ��膩��� 162 timer)
+ ��膩��� 163
+ ��膩��� 164 (defun timer-activate (timer)
+ ��膩��� 165 "Put TIMER on the list of active timers."
+ ��膩��� 166 (if (and (timerp timer)
+ ��膩��� 167 (integerp (aref timer 1))
+ ��膩��� 168 (integerp (aref timer 2))
+ ��膩��� 169 (integerp (aref timer 3))
+ ��膩��� 170 (aref timer 5))
+ ��膩��� 171 (let ((timers timer-list)
+ ��膩��� 172 last)
+ ��膩��� 173 ;; Skip all timers to trigger before the new one.
+ ��膩��� 174 (while (and timers
+ ��膩��� 175 (or (> (aref timer 1) (aref (car timers) 1))
+ ��膩��� 176 (and (= (aref timer 1) (aref (car timers) 1))
+ ��膩��� 177 (> (aref timer 2) (aref (car timers) 2)))
+ ��膩��� 178 (and (= (aref timer 1) (aref (car timers) 1))
+ ��膩��� 179 (= (aref timer 2) (aref (car timers) 2))
+ ��膩��� 180 (> (aref timer 3) (aref (car timers) 3)))))
+ ��膩��� 181 (setq last timers
+ ��膩��� 182 timers (cdr timers)))
+ ��膩��� 183 ;; Insert new timer after last which possibly means in front of queue.
+ ��膩��� 184 (if last
+ ��膩��� 185 (setcdr last (cons timer timers))
+ ��膩��� 186 (setq timer-list (cons timer timers)))
+ ��膩��� 187 (aset timer 0 nil)
+ ��膩��� 188 (aset timer 7 nil)
+ ��膩��� 189 nil)
+ ��膩��� 190 (error "Invalid or uninitialized timer")))
+ ��膩��� 191
+ ��膩��� 192 (defun timer-activate-when-idle (timer &optional dont-wait)
+ ��膩��� 193 "Arrange to activate TIMER whenever Emacs is next idle.
+ ��膩��� 194 If optional argument DONT-WAIT is non-nil, then enable the
+ ��膩��� 195 timer to activate immediately, or at the right time, if Emacs
+ ��膩��� 196 is already idle."
+ ��膩��� 197 (if (and (timerp timer)
+ ��膩��� 198 (integerp (aref timer 1))
+ ��膩��� 199 (integerp (aref timer 2))
+ ��膩��� 200 (integerp (aref timer 3))
+ ��膩��� 201 (aref timer 5))
+ ��膩��� 202 (let ((timers timer-idle-list)
+ ��膩��� 203 last)
+ ��膩��� 204 ;; Skip all timers to trigger before the new one.
+ ��膩��� 205 (while (and timers
+ ��膩��� 206 (or (> (aref timer 1) (aref (car timers) 1))
+ ��膩��� 207 (and (= (aref timer 1) (aref (car timers) 1))
+ ��膩��� 208 (> (aref timer 2) (aref (car timers) 2)))
+ ��膩��� 209 (and (= (aref timer 1) (aref (car timers) 1))
+ ��膩��� 210 (= (aref timer 2) (aref (car timers) 2))
+ ��膩��� 211 (> (aref timer 3) (aref (car timers) 3)))))
+ ��膩��� 212 (setq last timers
+ ��膩��� 213 timers (cdr timers)))
+ ��膩��� 214 ;; Insert new timer after last which possibly means in front of queue.
+ ��膩��� 215 (if last
+ ��膩��� 216 (setcdr last (cons timer timers))
+ ��膩��� 217 (setq timer-idle-list (cons timer timers)))
+ ��膩��� 218 (aset timer 0 (not dont-wait))
+ ��膩��� 219 (aset timer 7 t)
+ ��膩��� 220 nil)
+ ��膩��� 221 (error "Invalid or uninitialized timer")))
+ ��膩��� 222
+ ��膩��� 223 ;;;###autoload
+ ��膩��� 224 (defalias 'disable-timeout 'cancel-timer)
+ ��膩��� 225 ;;;###autoload
+ ��膩��� 226 (defun cancel-timer (timer)
+ ��膩��� 227 "Remove TIMER from the list of active timers."
+ ��膩��� 228 (or (timerp timer)
+ ��膩��� 229 (error "Invalid timer"))
+ ��膩��� 230 (setq timer-list (delq timer timer-list))
+ ��膩��� 231 (setq timer-idle-list (delq timer timer-idle-list))
+ ��膩��� 232 nil)
+ ��膩��� 233
+ ��膩��� 234 ;;;###autoload
+ ��膩��� 235 (defun cancel-function-timers (function)
+ ��膩��� 236 "Cancel all timers scheduled by `run-at-time' which would run FUNCTION."
+ ��膩��� 237 (interactive "aCancel timers of function: ")
+ ��膩��� 238 (let ((tail timer-list))
+ ��膩��� 239 (while tail
+ ��膩��� 240 (if (eq (aref (car tail) 5) function)
+ ��膩��� 241 (setq timer-list (delq (car tail) timer-list)))
+ ��膩��� 242 (setq tail (cdr tail))))
+ ��膩��� 243 (let ((tail timer-idle-list))
+ ��膩��� 244 (while tail
+ ��膩��� 245 (if (eq (aref (car tail) 5) function)
+ ��膩��� 246 (setq timer-idle-list (delq (car tail) timer-idle-list)))
+ ��膩��� 247 (setq tail (cdr tail)))))
+ ��膩��� 248
+ ��膩��� 249 ;; Record the last few events, for debugging.
+ ��膩��� 250 (defvar timer-event-last-2 nil)
+ ��膩��� 251 (defvar timer-event-last-1 nil)
+ ��膩��� 252 (defvar timer-event-last nil)
+ ��膩��� 253
+ ��膩��� 254 (defvar timer-max-repeats 10
+ ��膩��� 255 "*Maximum number of times to repeat a timer, if real time jumps.")
+ ��膩��� 256
+ ��膩��� 257 (defun timer-until (timer time)
+ ��膩��� 258 "Calculate number of seconds from when TIMER will run, until TIME.
+ ��膩��� 259 TIMER is a timer, and stands for the time when its next repeat is scheduled.
+ ��膩��� 260 TIME is a time-list."
+ ��膩��� 261 (let ((high (- (car time) (aref timer 1)))
+ ��膩��� 262 (low (- (nth 1 time) (aref timer 2))))
+ ��膩��� 263 (+ low (* high 65536))))
+ ��膩��� 264
+ ��膩��� 265 (defun timer-event-handler (timer)
+ ��膩��� 266 "Call the handler for the timer TIMER.
+ ��膩��� 267 This function is called, by name, directly by the C code."
+ ��膩��� 268 (setq timer-event-last-2 timer-event-last-1)
+ ��膩��� 269 (setq timer-event-last-1 timer-event-last)
+ ��膩��� 270 (setq timer-event-last timer)
+ ��膩��� 271 (let ((inhibit-quit t))
+ ��膩��� 272 (if (timerp timer)
+ ��膩��� 273 (progn
+ ��膩��� 274 ;; Delete from queue.
+ ��膩��� 275 (cancel-timer timer)
+ ��膩��� 276 ;; Re-schedule if requested.
+ ��膩��� 277 (if (aref timer 4)
+ ��膩��� 278 (if (aref timer 7)
+ ��膩��� 279 (timer-activate-when-idle timer)
+ ��膩��� 280 (timer-inc-time timer (aref timer 4) 0)
+ ��膩��� 281 ;; If real time has jumped forward,
+ ��膩��� 282 ;; perhaps because Emacs was suspended for a long time,
+ ��膩��� 283 ;; limit how many times things get repeated.
+ ��膩��� 284 (if (and (numberp timer-max-repeats)
+ ��膩��� 285 (< 0 (timer-until timer (current-time))))
+ ��膩��� 286 (let ((repeats (/ (timer-until timer (current-time))
+ ��膩��� 287 (aref timer 4))))
+ ��膩��� 288 (if (> repeats timer-max-repeats)
+ ��膩��� 289 (timer-inc-time timer (* (aref timer 4) repeats)))))
+ ��膩��� 290 (timer-activate timer)))
+ ��膩��� 291 ;; Run handler.
+ ��膩��� 292 ;; We do this after rescheduling so that the handler function
+ ��膩��� 293 ;; can cancel its own timer successfully with cancel-timer.
+ ��膩��� 294 (condition-case nil
+ ��膩��� 295 (apply (aref timer 5) (aref timer 6))
+ ��膩��� 296 (error nil)))
+ ��膩��� 297 (error "Bogus timer event"))))
+ ��膩��� 298
+ ��膩��� 299 ;; This function is incompatible with the one in levents.el.
+ ��膩��� 300 (defun timeout-event-p (event)
+ ��膩��� 301 "Non-nil if EVENT is a timeout event."
+ ��膩��� 302 (and (listp event) (eq (car event) 'timer-event)))
+ ��膩��� 303
+ ��膩��� 304 ;;;###autoload
+ ��膩��� 305 (defun run-at-time (time repeat function &rest args)
+ ��膩��� 306 "Perform an action at time TIME.
+ ��膩��� 307 Repeat the action every REPEAT seconds, if REPEAT is non-nil.
+ ��膩��� 308 TIME should be a string like \"11:23pm\", nil meaning now, a number of seconds
+ ��膩��� 309 from now, a value from `current-time', or t (with non-nil REPEAT)
+ ��膩��� 310 meaning the next integral multiple of REPEAT.
+ ��膩��� 311 REPEAT may be an integer or floating point number.
+ ��膩��� 312 The action is to call FUNCTION with arguments ARGS.
+ ��膩��� 313
+ ��膩��� 314 This function returns a timer object which you can use in `cancel-timer'."
+ ��膩��� 315 (interactive "sRun at time: \nNRepeat interval: \naFunction: ")
+ ��膩��� 316
+ ��膩��� 317 (or (null repeat)
+ ��膩��� 318 (and (numberp repeat) (< 0 repeat))
+ ��膩��� 319 (error "Invalid repetition interval"))
+ ��膩��� 320
+ ��膩��� 321 ;; Special case: nil means "now" and is useful when repeating.
+ ��膩��� 322 (if (null time)
+ ��膩��� 323 (setq time (current-time)))
+ ��膩��� 324
+ ��膩��� 325 ;; Special case: t means the next integral multiple of REPEAT.
+ ��膩��� 326 (if (and (eq time t) repeat)
+ ��膩��� 327 (setq time (timer-next-integral-multiple-of-time (current-time) repeat)))
+ ��膩��� 328
+ ��膩��� 329 ;; Handle numbers as relative times in seconds.
+ ��膩��� 330 (if (numberp time)
+ ��膩��� 331 (setq time (timer-relative-time (current-time) time)))
+ ��膩��� 332
+ ��膩��� 333 ;; Handle relative times like "2 hours and 35 minutes"
+ ��膩��� 334 (if (stringp time)
+ ��膩��� 335 (let ((secs (timer-duration time)))
+ ��膩��� 336 (if secs
+ ��膩��� 337 (setq time (timer-relative-time (current-time) secs)))))
+ ��膩��� 338
+ ��膩��� 339 ;; Handle "11:23pm" and the like. Interpret it as meaning today
+ ��膩��� 340 ;; which admittedly is rather stupid if we have passed that time
+ ��膩��� 341 ;; already. (Though only Emacs hackers hack Emacs at that time.)
+ ��膩��� 342 (if (stringp time)
+ ��膩��� 343 (progn
+ ��膩��� 344 (require 'diary-lib)
+ ��膩��� 345 (let ((hhmm (diary-entry-time time))
+ ��膩��� 346 (now (decode-time)))
+ ��膩��� 347 (if (>= hhmm 0)
+ ��膩��� 348 (setq time
+ ��膩��� 349 (encode-time 0 (% hhmm 100) (/ hhmm 100) (nth 3 now)
+ ��膩��� 350 (nth 4 now) (nth 5 now) (nth 8 now)))))))
+ ��膩��� 351
+ ��膩��� 352 (or (consp time)
+ ��膩��� 353 (error "Invalid time format"))
+ ��膩��� 354
+ ��膩��� 355 (let ((timer (timer-create)))
+ ��膩��� 356 (timer-set-time timer time repeat)
+ ��膩��� 357 (timer-set-function timer function args)
+ ��膩��� 358 (timer-activate timer)
+ ��膩��� 359 timer))
+ ��膩��� 360
+ ��膩��� 361 ;;;###autoload
+ ��膩��� 362 (defun run-with-timer (secs repeat function &rest args)
+ ��膩��� 363 "Perform an action after a delay of SECS seconds.
+ ��膩��� 364 Repeat the action every REPEAT seconds, if REPEAT is non-nil.
+ ��膩��� 365 SECS and REPEAT may be integers or floating point numbers.
+ ��膩��� 366 The action is to call FUNCTION with arguments ARGS.
+ ��膩��� 367
+ ��膩��� 368 This function returns a timer object which you can use in `cancel-timer'."
+ ��膩��� 369 (interactive "sRun after delay (seconds): \nNRepeat interval: \naFunction: ")
+ ��膩��� 370 (apply 'run-at-time secs repeat function args))
+ ��膩��� 371
+ ��膩��� 372 ;;;###autoload
+ ��膩��� 373 (defun add-timeout (secs function object &optional repeat)
+ ��膩��� 374 "Add a timer to run SECS seconds from now, to call FUNCTION on OBJECT.
+ ��膩��� 375 If REPEAT is non-nil, repeat the timer every REPEAT seconds.
+ ��膩��� 376 This function is for compatibility; see also `run-with-timer'."
+ ��膩��� 377 (run-with-timer secs repeat function object))
+ ��膩��� 378
+ ��膩��� 379 ;;;###autoload
+ ��膩��� 380 (defun run-with-idle-timer (secs repeat function &rest args)
+ ��膩��� 381 "Perform an action the next time Emacs is idle for SECS seconds.
+ ��膩��� 382 The action is to call FUNCTION with arguments ARGS.
+ ��膩��� 383 SECS may be an integer or a floating point number.
+ ��膩��� 384
+ ��膩��� 385 If REPEAT is non-nil, do the action each time Emacs has been idle for
+ ��膩��� 386 exactly SECS seconds (that is, only once for each time Emacs becomes idle).
+ ��膩��� 387
+ ��膩��� 388 This function returns a timer object which you can use in `cancel-timer'."
+ ��膩��� 389 (interactive
+ ��膩��� 390 (list (read-from-minibuffer "Run after idle (seconds): " nil nil t)
+ ��膩��� 391 (y-or-n-p "Repeat each time Emacs is idle? ")
+ ��膩��� 392 (intern (completing-read "Function: " obarray 'fboundp t))))
+ ��膩��� 393 (let ((timer (timer-create)))
+ ��膩��� 394 (timer-set-function timer function args)
+ ��膩��� 395 (timer-set-idle-time timer secs repeat)
+ ��膩��� 396 (timer-activate-when-idle timer)
+ ��膩��� 397 timer))
+ ��膩��� 398
+ ��膩��� 399 (defun with-timeout-handler (tag)
+ ��膩��� 400 (throw tag 'timeout))
+ ��膩��� 401
+ ��膩��� 402 ;;;###autoload (put 'with-timeout 'lisp-indent-function 1)
+ ��膩��� 403
+ ��膩��� 404 ;;;###autoload
+ ��膩��� 405 (defmacro with-timeout (list &rest body)
+ ��膩��� 406 "Run BODY, but if it doesn't finish in SECONDS seconds, give up.
+ ��膩��� 407 If we give up, we run the TIMEOUT-FORMS and return the value of the last one.
+ ��膩��� 408 The call should look like:
+ ��膩��� 409 (with-timeout (SECONDS TIMEOUT-FORMS...) BODY...)
+ ��膩��� 410 The timeout is checked whenever Emacs waits for some kind of external
+ ��膩��� 411 event \(such as keyboard input, input from subprocesses, or a certain time);
+ ��膩��� 412 if the program loops without waiting in any way, the timeout will not
+ ��膩��� 413 be detected."
+ ��膩��� 414 (let ((seconds (car list))
+ ��膩��� 415 (timeout-forms (cdr list)))
+ ��膩��� 416 `(let ((with-timeout-tag (cons nil nil))
+ ��膩��� 417 with-timeout-value with-timeout-timer)
+ ��膩��� 418 (if (catch with-timeout-tag
+ ��膩��� 419 (progn
+ ��膩��� 420 (setq with-timeout-timer
+ ��膩��� 421 (run-with-timer ,seconds nil
+ ��膩��� 422 'with-timeout-handler
+ ��膩��� 423 with-timeout-tag))
+ ��膩��� 424 (setq with-timeout-value (progn . ,body))
+ ��膩��� 425 nil))
+ ��膩��� 426 (progn . ,timeout-forms)
+ ��膩��� 427 (cancel-timer with-timeout-timer)
+ ��膩��� 428 with-timeout-value))))
+ ��膩��� 429
+ ��膩��� 430 (defun y-or-n-p-with-timeout (prompt seconds default-value)
+ ��膩��� 431 "Like (y-or-n-p PROMPT), with a timeout.
+ ��膩��� 432 If the user does not answer after SECONDS seconds, return DEFAULT-VALUE."
+ ��膩��� 433 (with-timeout (seconds default-value)
+ ��膩��� 434 (y-or-n-p prompt)))
+ ��膩��� 435
+ ��膩��� 436 (defvar timer-duration-words
+ ��膩��� 437 (list (cons "microsec" 0.000001)
+ ��膩��� 438 (cons "microsecond" 0.000001)
+ ��膩��� 439 (cons "millisec" 0.001)
+ ��膩��� 440 (cons "millisecond" 0.001)
+ ��膩��� 441 (cons "sec" 1)
+ ��膩��� 442 (cons "second" 1)
+ ��膩��� 443 (cons "min" 60)
+ ��膩��� 444 (cons "minute" 60)
+ ��膩��� 445 (cons "hour" (* 60 60))
+ ��膩��� 446 (cons "day" (* 24 60 60))
+ ��膩��� 447 (cons "week" (* 7 24 60 60))
+ ��膩��� 448 (cons "fortnight" (* 14 24 60 60))
+ ��膩��� 449 (cons "month" (* 30 24 60 60)) ; Approximation
+ ��膩��� 450 (cons "year" (* 365.25 24 60 60)) ; Approximation
+ ��膩��� 451 )
+ ��膩��� 452 "Alist mapping temporal words to durations in seconds")
+ ��膩��� 453
+ ��膩��� 454 (defun timer-duration (string)
+ ��膩��� 455 "Return number of seconds specified by STRING, or nil if parsing fails."
+ ��膩��� 456 (let ((secs 0)
+ ��膩��� 457 (start 0)
+ ��膩��� 458 (case-fold-search t))
+ ��膩��� 459 (while (string-match
+ ��膩��� 460 "[ \t]*\\([0-9.]+\\)?[ \t]*\\([a-z]+[a-rt-z]\\)s?[ \t]*"
+ ��膩��� 461 string start)
+ ��膩��� 462 (let ((count (if (match-beginning 1)
+ ��膩��� 463 (string-to-number (match-string 1 string))
+ ��膩��� 464 1))
+ ��膩��� 465 (itemsize (cdr (assoc (match-string 2 string)
+ ��膩��� 466 timer-duration-words))))
+ ��膩��� 467 (if itemsize
+ ��膩��� 468 (setq start (match-end 0)
+ ��膩��� 469 secs (+ secs (* count itemsize)))
+ ��膩��� 470 (setq secs nil
+ ��膩��� 471 start (length string)))))
+ ��膩��� 472 (if (= start (length string))
+ ��膩��� 473 secs
+ ��膩��� 474 (if (string-match "\\`[0-9.]+\\'" string)
+ ��膩��� 475 (string-to-number string)))))
+ ��膩��� 476
+ ��膩��� 477 (provide 'timer)
+ ��膩��� 478
+ ��膩��� 479 ;;; timer.el ends here