annotate lisp/utils/with-timeout.el @ 97:498bf5da1c90

Added tag r20-0final for changeset dbb370e3c29e
author cvs
date Mon, 13 Aug 2007 09:12:43 +0200
parents 131b0175ea99
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
70
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
1 ;;; with-timeout.el --- timeout hackery
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
2
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
4 ;; Keywords: extensions
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
5
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
6 ;; This file is part of XEmacs.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
7
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
8 ;; XEmacs is free software; you can redistribute it and/or modify it
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
9 ;; under the terms of the GNU General Public License as published by
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
10 ;; the Free Software Foundation; either version 2, or (at your option)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
11 ;; any later version.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
12
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
13 ;; XEmacs is distributed in the hope that it will be useful, but
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
16 ;; General Public License for more details.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
17
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
18 ;; You should have received a copy of the GNU General Public License
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
19 ;; along with XEmacs; see the file COPYING. If not, write to the
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
21 ;; Boston, MA 02111-1307, USA.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
22
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
23 ;;; Synched up with: Not in FSF.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
24
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
25 (defun with-timeout-timer (tag)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
26 ;; I'm pretty sure the condition-case isn't really necessary here,
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
27 ;; but it doesn't hurt.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
28 (condition-case () (throw tag nil) (no-catch nil)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
29
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
30 ;;;###autoload
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
31 (defun with-timeout-internal (with-timeout-seconds with-timeout-tag
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
32 with-timeout-body with-timeout-forms)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
33 (let ((with-timeout-timeout nil))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
34 (unwind-protect
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
35 (progn
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
36 (setq with-timeout-timeout (add-timeout with-timeout-seconds
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
37 'with-timeout-timer
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
38 with-timeout-tag))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
39 (let ((value (catch with-timeout-tag
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
40 (prog1 (funcall with-timeout-body)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
41 (setq with-timeout-tag nil)))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
42 (if with-timeout-tag
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
43 (funcall with-timeout-forms)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
44 value)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
45 (if with-timeout-timeout
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
46 (disable-timeout with-timeout-timeout)))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
47
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
48 ;;;###autoload
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
49 (defmacro with-timeout (seconds-and-timeout-forms &rest body)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
50 "Usage: (with-timeout (seconds &rest timeout-forms) &rest body)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
51 This is just like progn, but if the given number of seconds expires before
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
52 the body returns, then timeout-forms are evaluated and returned instead.
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
53 The body won't be interrupted in the middle of a computation: the check for
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
54 the timer expiration only occurs when body does a redisplay, or prompts the
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
55 user for input, or calls accept-process-output."
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
56 (let ((seconds (car seconds-and-timeout-forms))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
57 (timeout-forms (cdr seconds-and-timeout-forms)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
58 (` (with-timeout-internal (, seconds) '(, (make-symbol "_with_timeout_"))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
59 #'(lambda () (progn (,@ body)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
60 #'(lambda () (progn (,@ timeout-forms)))))))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
61
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
62 (put 'with-timeout 'lisp-indent-function 1)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
63
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
64 ;;;###autoload
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
65 (defun yes-or-no-p-with-timeout (timeout prompt &optional default-value)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
66 "Just like yes-or-no-p, but will time out after TIMEOUT seconds
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
67 if the user has not yes answered, returning DEFAULT-VALUE."
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
68 (with-timeout (timeout
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
69 (message (concat prompt "(yes or no) Timeout to "
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
70 (if default-value "Yes" "No")))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
71 default-value)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
72 (yes-or-no-p prompt)))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
73
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
74 ;;;###autoload
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
75 (defun y-or-n-p-with-timeout (timeout prompt &optional default-value)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
76 "Just like y-or-n-p, but will time out after TIMEOUT seconds
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
77 if the user has not yes answered, returning DEFAULT-VALUE."
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
78 (with-timeout (timeout
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
79 (message (concat prompt "(yes or no) Timeout to "
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
80 (if default-value "Yes" "No")))
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
81 default-value)
131b0175ea99 Import from CVS: tag r20-0b30
cvs
parents:
diff changeset
82 (y-or-n-p prompt)))