diff options
| -rw-r--r-- | lisp/timer.el | 93 |
1 files changed, 47 insertions, 46 deletions
diff --git a/lisp/timer.el b/lisp/timer.el index b380ea531e3..95e41965fce 100644 --- a/lisp/timer.el +++ b/lisp/timer.el | |||
| @@ -44,9 +44,9 @@ | |||
| 44 | 44 | ||
| 45 | (defun timer-set-time (timer time &optional delta) | 45 | (defun timer-set-time (timer time &optional delta) |
| 46 | "Set the trigger time of TIMER to TIME. | 46 | "Set the trigger time of TIMER to TIME. |
| 47 | TIME must be in the internal format returned by, e.g., `current-time' | 47 | TIME must be in the internal format returned by, e.g., `current-time'. |
| 48 | If optional third argument DELTA is a non-zero integer make the timer | 48 | If optional third argument DELTA is a non-zero integer, make the timer |
| 49 | fire repeatedly that meny seconds apart." | 49 | fire repeatedly that many seconds apart." |
| 50 | (or (timerp timer) | 50 | (or (timerp timer) |
| 51 | (error "Invalid timer")) | 51 | (error "Invalid timer")) |
| 52 | (aset timer 1 (car time)) | 52 | (aset timer 1 (car time)) |
| @@ -55,6 +55,19 @@ fire repeatedly that meny seconds apart." | |||
| 55 | (aset timer 4 (and (numberp delta) (> delta 0) delta)) | 55 | (aset timer 4 (and (numberp delta) (> delta 0) delta)) |
| 56 | timer) | 56 | timer) |
| 57 | 57 | ||
| 58 | (defun timer-set-idle-time (timer secs &optional repeat) | ||
| 59 | "Set the trigger idle time of TIMER to SECS. | ||
| 60 | If optional third argument REPEAT is non-nil, make the timer | ||
| 61 | fire each time Emacs is idle for that many seconds." | ||
| 62 | (or (timerp timer) | ||
| 63 | (error "Invalid timer")) | ||
| 64 | (aset timer 1 0) | ||
| 65 | (aset timer 2 0) | ||
| 66 | (aset timer 3 0) | ||
| 67 | (timer-inc-time timer secs) | ||
| 68 | (aset timer 4 repeat) | ||
| 69 | timer) | ||
| 70 | |||
| 58 | (defun timer-relative-time (time secs &optional usecs) | 71 | (defun timer-relative-time (time secs &optional usecs) |
| 59 | "Advance TIME by SECS seconds and optionally USECS microseconds. | 72 | "Advance TIME by SECS seconds and optionally USECS microseconds. |
| 60 | SECS may be a fraction." | 73 | SECS may be a fraction." |
| @@ -90,9 +103,9 @@ SECS may be a fraction." | |||
| 90 | 103 | ||
| 91 | (defun timer-set-time-with-usecs (timer time usecs &optional delta) | 104 | (defun timer-set-time-with-usecs (timer time usecs &optional delta) |
| 92 | "Set the trigger time of TIMER to TIME. | 105 | "Set the trigger time of TIMER to TIME. |
| 93 | TIME must be in the internal format returned by, e.g., `current-time' | 106 | TIME must be in the internal format returned by, e.g., `current-time'. |
| 94 | If optional third argument DELTA is a non-zero integer make the timer | 107 | If optional third argument DELTA is a non-zero integer, make the timer |
| 95 | fire repeatedly that menu seconds apart." | 108 | fire repeatedly that many seconds apart." |
| 96 | (or (timerp timer) | 109 | (or (timerp timer) |
| 97 | (error "Invalid timer")) | 110 | (error "Invalid timer")) |
| 98 | (aset timer 1 (car time)) | 111 | (aset timer 1 (car time)) |
| @@ -193,6 +206,7 @@ fire repeatedly that menu seconds apart." | |||
| 193 | ;; special-event-map ensures that event timer events that arrive in the | 206 | ;; special-event-map ensures that event timer events that arrive in the |
| 194 | ;; middle of a key sequence being entered are still handled correctly. | 207 | ;; middle of a key sequence being entered are still handled correctly. |
| 195 | (define-key special-event-map [timer-event] 'timer-event-handler) | 208 | (define-key special-event-map [timer-event] 'timer-event-handler) |
| 209 | |||
| 196 | (defun timer-event-handler (event) | 210 | (defun timer-event-handler (event) |
| 197 | "Call the handler for the timer in the event EVENT." | 211 | "Call the handler for the timer in the event EVENT." |
| 198 | (interactive "e") | 212 | (interactive "e") |
| @@ -210,15 +224,20 @@ fire repeatedly that menu seconds apart." | |||
| 210 | (timer-inc-time timer (aref timer 4) 0) | 224 | (timer-inc-time timer (aref timer 4) 0) |
| 211 | (timer-activate timer)))) | 225 | (timer-activate timer)))) |
| 212 | (error "Bogus timer event")))) | 226 | (error "Bogus timer event")))) |
| 227 | |||
| 228 | ;; This function is incompatible with the one in levents.el. | ||
| 229 | (defun timeout-event-p (event) | ||
| 230 | "Non-nil if EVENT is a timeout event." | ||
| 231 | (and (listp event) (eq (car event) 'timer-event))) | ||
| 213 | 232 | ||
| 214 | ;;;###autoload | 233 | ;;;###autoload |
| 215 | (defun run-at-time (time repeat function &rest args) | 234 | (defun run-at-time (time repeat function &rest args) |
| 216 | "Run a function at a time, and optionally on a regular interval. | 235 | "Perform an action after a delay of SECS seconds. |
| 217 | Arguments are TIME, REPEAT, FUNCTION &rest ARGS. | 236 | Repeat the action every REPEAT seconds, if REPEAT is non-nil. |
| 218 | TIME is a string like \"11:23pm\" or a value from `encode-time', | 237 | TIME should be a string like \"11:23pm\", nil meaning now, a number of seconds |
| 219 | or a number of seconds from now. | 238 | from now, or a value from `encode-time'. |
| 220 | REPEAT, an integer number of seconds, is the interval on which to repeat | 239 | REPEAT may be an integer or floating point number. |
| 221 | the call to the function. If REPEAT is nil or 0, call it just once. | 240 | The action is to call FUNCTION with arguments ARGS. |
| 222 | 241 | ||
| 223 | This function returns a timer object which you can use in `cancel-timer'." | 242 | This function returns a timer object which you can use in `cancel-timer'." |
| 224 | (interactive "sRun at time: \nNRepeat interval: \naFunction: ") | 243 | (interactive "sRun at time: \nNRepeat interval: \naFunction: ") |
| @@ -239,7 +258,7 @@ This function returns a timer object which you can use in `cancel-timer'." | |||
| 239 | 258 | ||
| 240 | ;; Handle "11:23pm" and the like. Interpret it as meaning today | 259 | ;; Handle "11:23pm" and the like. Interpret it as meaning today |
| 241 | ;; which admittedly is rather stupid if we have passed that time | 260 | ;; which admittedly is rather stupid if we have passed that time |
| 242 | ;; already. | 261 | ;; already. (Though only Emacs hackers hack Emacs at that time.) |
| 243 | (if (stringp time) | 262 | (if (stringp time) |
| 244 | (progn | 263 | (progn |
| 245 | (require 'diary-lib) | 264 | (require 'diary-lib) |
| @@ -272,51 +291,33 @@ The action is to call FUNCTION with arguments ARGS. | |||
| 272 | 291 | ||
| 273 | This function returns a timer object which you can use in `cancel-timer'." | 292 | This function returns a timer object which you can use in `cancel-timer'." |
| 274 | (interactive "sRun after delay (seconds): \nNRepeat interval: \naFunction: ") | 293 | (interactive "sRun after delay (seconds): \nNRepeat interval: \naFunction: ") |
| 294 | (apply 'run-at-time secs repeat function args)) | ||
| 275 | 295 | ||
| 276 | (or (null repeat) | 296 | ;;;###autoload |
| 277 | (and (numberp repeat) (>= repeat 0)) | 297 | (defun add-timeout (secs function object &optional repeat) |
| 278 | (error "Invalid repetition interval")) | 298 | "Add a timer to run SECS seconds from now, to call FUNCTION on OBJECT. |
| 279 | 299 | If REPEAT is non-nil, repeat the timer every REPEAT seconds. | |
| 280 | (let ((timer (timer-create))) | 300 | This function is for compatibility; see also `run-with-timer'." |
| 281 | (timer-set-time timer (current-time) repeat) | 301 | (run-with-timer secs repeat function object)) |
| 282 | (timer-inc-time timer secs) | ||
| 283 | (timer-set-function timer function args) | ||
| 284 | (timer-activate timer) | ||
| 285 | timer)) | ||
| 286 | 302 | ||
| 287 | ;;;###autoload | 303 | ;;;###autoload |
| 288 | (defun run-with-idle-timer (secs repeat function &rest args) | 304 | (defun run-with-idle-timer (secs repeat function &rest args) |
| 289 | "Perform an action the next time Emacs is idle for SECS seconds. | 305 | "Perform an action the next time Emacs is idle for SECS seconds. |
| 290 | The action is to call FUNCTION with arguments ARGS. | ||
| 291 | If REPEAT is non-nil, do this each time Emacs is idle for SECS seconds. | 306 | If REPEAT is non-nil, do this each time Emacs is idle for SECS seconds. |
| 292 | SECS may be an integer or a floating point number. | 307 | SECS may be an integer or a floating point number. |
| 308 | The action is to call FUNCTION with arguments ARGS. | ||
| 293 | 309 | ||
| 294 | This function returns a timer object which you can use in `cancel-timer'." | 310 | This function returns a timer object which you can use in `cancel-timer'." |
| 295 | (interactive "sRun after delay (seconds): \nNRepeat interval: \naFunction: ") | 311 | (interactive |
| 296 | 312 | (list (read-from-minibuffer "Run after idle (seconds): " nil nil t) | |
| 313 | (y-or-n-p "Repeat each time Emacs is idle? ") | ||
| 314 | (intern (completing-read "Function: " obarray 'fboundp t)))) | ||
| 297 | (let ((timer (timer-create))) | 315 | (let ((timer (timer-create))) |
| 298 | (timer-set-function timer function args) | 316 | (timer-set-function timer function args) |
| 299 | ;; Store 0 into the time fields, then add in SECS. | 317 | (timer-set-idle-time timer secs repeat) |
| 300 | (aset timer 1 0) | ||
| 301 | (aset timer 2 0) | ||
| 302 | (aset timer 3 0) | ||
| 303 | (timer-inc-time timer secs) | ||
| 304 | (aset timer 4 repeat) | ||
| 305 | (timer-activate-when-idle timer) | 318 | (timer-activate-when-idle timer) |
| 306 | timer)) | 319 | timer)) |
| 307 | 320 | ||
| 308 | ;;;###autoload | ||
| 309 | (defun add-timeout (secs function object &optional repeat) | ||
| 310 | "Add a timer to run SECS seconds from now, to call FUNCTION on OBJECT. | ||
| 311 | If REPEAT is non-nil, repeat the timer every REPEAT seconds. | ||
| 312 | This function is for compatibility; see also `run-with-timer'." | ||
| 313 | (run-with-timer secs repeat function object)) | ||
| 314 | |||
| 315 | (defun timeout-event-p (event) | ||
| 316 | "Non-nil if EVENT is a timeout event." | ||
| 317 | (and (listp event) | ||
| 318 | (eq (car event) 'timer-event))) | ||
| 319 | |||
| 320 | (defun with-timeout-handler (tag) | 321 | (defun with-timeout-handler (tag) |
| 321 | (throw tag 'timeout)) | 322 | (throw tag 'timeout)) |
| 322 | 323 | ||
| @@ -326,8 +327,8 @@ This function is for compatibility; see also `run-with-timer'." | |||
| 326 | (defmacro with-timeout (list &rest body) | 327 | (defmacro with-timeout (list &rest body) |
| 327 | "Run BODY, but if it doesn't finish in SECONDS seconds, give up. | 328 | "Run BODY, but if it doesn't finish in SECONDS seconds, give up. |
| 328 | If we give up, we run the TIMEOUT-FORMS and return the value of the last one. | 329 | If we give up, we run the TIMEOUT-FORMS and return the value of the last one. |
| 329 | The call looks like | 330 | The call should look like: |
| 330 | (with-timeout (SECONDS TIMEOUT-FORMS...) BODY...) | 331 | (with-timeout (SECONDS TIMEOUT-FORMS...) BODY...) |
| 331 | The timeout is checked whenever Emacs waits for some kind of external | 332 | The timeout is checked whenever Emacs waits for some kind of external |
| 332 | event \(such as keyboard input, input from subprocesses, or a certain time); | 333 | event \(such as keyboard input, input from subprocesses, or a certain time); |
| 333 | if the program loops without waiting in any way, the timeout will not | 334 | if the program loops without waiting in any way, the timeout will not |