aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman2005-07-10 17:18:25 +0000
committerRichard M. Stallman2005-07-10 17:18:25 +0000
commit89c020e85ddcd035d7e5d044a2ffe8589f63d26c (patch)
treee57e9eb905e5634460342e80983c35f1637cd08b
parent028d38a278335f728230495e30637606e46682f9 (diff)
downloademacs-89c020e85ddcd035d7e5d044a2ffe8589f63d26c.tar.gz
emacs-89c020e85ddcd035d7e5d044a2ffe8589f63d26c.zip
(with-timeout-timers): New variable.
(with-timeout): Bind that variable to record timers. (with-timeout-suspend, with-timeout-unsuspend): New functions.
-rw-r--r--lisp/emacs-lisp/timer.el32
1 files changed, 31 insertions, 1 deletions
diff --git a/lisp/emacs-lisp/timer.el b/lisp/emacs-lisp/timer.el
index e207766701c..27f14a6d3ad 100644
--- a/lisp/emacs-lisp/timer.el
+++ b/lisp/emacs-lisp/timer.el
@@ -404,6 +404,9 @@ This function returns a timer object which you can use in `cancel-timer'."
404 404
405;;;###autoload (put 'with-timeout 'lisp-indent-function 1) 405;;;###autoload (put 'with-timeout 'lisp-indent-function 1)
406 406
407(defvar with-timeout-timers nil
408 "List of all timers used by currently pending `with-timeout' calls.")
409
407;;;###autoload 410;;;###autoload
408(defmacro with-timeout (list &rest body) 411(defmacro with-timeout (list &rest body)
409 "Run BODY, but if it doesn't finish in SECONDS seconds, give up. 412 "Run BODY, but if it doesn't finish in SECONDS seconds, give up.
@@ -416,19 +419,46 @@ be detected.
416 (let ((seconds (car list)) 419 (let ((seconds (car list))
417 (timeout-forms (cdr list))) 420 (timeout-forms (cdr list)))
418 `(let ((with-timeout-tag (cons nil nil)) 421 `(let ((with-timeout-tag (cons nil nil))
419 with-timeout-value with-timeout-timer) 422 with-timeout-value with-timeout-timer
423 (with-timeout-timers with-timeout-timers))
420 (if (catch with-timeout-tag 424 (if (catch with-timeout-tag
421 (progn 425 (progn
422 (setq with-timeout-timer 426 (setq with-timeout-timer
423 (run-with-timer ,seconds nil 427 (run-with-timer ,seconds nil
424 'with-timeout-handler 428 'with-timeout-handler
425 with-timeout-tag)) 429 with-timeout-tag))
430 (push with-timeout-timer with-timeout-timers)
426 (setq with-timeout-value (progn . ,body)) 431 (setq with-timeout-value (progn . ,body))
427 nil)) 432 nil))
428 (progn . ,timeout-forms) 433 (progn . ,timeout-forms)
429 (cancel-timer with-timeout-timer) 434 (cancel-timer with-timeout-timer)
430 with-timeout-value)))) 435 with-timeout-value))))
431 436
437(defun with-timeout-suspend ()
438 "Stop the clock for `with-timeout'. Used by debuggers.
439The idea is that the time you spend in the debugger should not
440count against these timeouts.
441
442The value is a list that the debugger can pass to `with-timeout-unsuspend'
443when it exits, to make these timers start counting again."
444 (mapcar (lambda (timer)
445 (cancel-timer timer)
446 (list timer
447 (time-subtract
448 ;; The time that this timer will go off.
449 (list (aref timer 1) (aref timer 2) (aref timer 3))
450 (current-time))))
451 with-timeout-timers))
452
453(defun with-timeout-unsuspend (timer-spec-list)
454 "Restart the clock for `with-timeout'.
455The argument should be a value previously returned by `with-timeout-suspend'."
456 (dolist (elt timer-spec-list)
457 (let ((timer (car elt))
458 (delay (cadr elt)))
459 (timer-set-time timer (time-add (current-time) delay))
460 (timer-activate timer))))
461
432(defun y-or-n-p-with-timeout (prompt seconds default-value) 462(defun y-or-n-p-with-timeout (prompt seconds default-value)
433 "Like (y-or-n-p PROMPT), with a timeout. 463 "Like (y-or-n-p PROMPT), with a timeout.
434If the user does not answer after SECONDS seconds, return DEFAULT-VALUE." 464If the user does not answer after SECONDS seconds, return DEFAULT-VALUE."