annotate lisp/utils/with-timeout.el @ 0:376386a54a3c r19-14

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