+ (timer-set-idle-time timer secs repeat)
+ (timer-activate-when-idle timer)
+ timer))
+\f
+(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 Emacs 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)
+ (if (catch with-timeout-tag
+ (progn
+ (setq with-timeout-timer
+ (run-with-timer ,seconds nil
+ 'with-timeout-handler
+ with-timeout-tag))
+ (setq with-timeout-value (progn . ,body))
+ nil))
+ (progn . ,timeout-forms)
+ (cancel-timer with-timeout-timer)
+ with-timeout-value))))
+
+(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)))
+\f
+(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)))))