annotate lisp/utils/with-timeout.el @ 16:0293115a14e9 r19-15b91

Import from CVS: tag r19-15b91
author cvs
date Mon, 13 Aug 2007 08:49:20 +0200
parents 376386a54a3c
children
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
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 0
diff changeset
19 ;; along with XEmacs; see the file COPYING. If not, write to the
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 0
diff changeset
20 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 0
diff changeset
21 ;; Boston, MA 02111-1307, USA.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 ;;; Synched up with: Not in FSF.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 (defun with-timeout-timer (tag)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 ;; I'm pretty sure the condition-case isn't really necessary here,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 ;; but it doesn't hurt.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 (condition-case () (throw tag nil) (no-catch nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 (defun with-timeout-internal (with-timeout-seconds with-timeout-tag
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 with-timeout-body with-timeout-forms)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 (let ((with-timeout-timeout nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 (unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 (setq with-timeout-timeout (add-timeout with-timeout-seconds
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 'with-timeout-timer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 with-timeout-tag))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 (let ((value (catch with-timeout-tag
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 (prog1 (funcall with-timeout-body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 (setq with-timeout-tag nil)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 (if with-timeout-tag
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 (funcall with-timeout-forms)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 value)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 (if with-timeout-timeout
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 (disable-timeout with-timeout-timeout)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 (defmacro with-timeout (seconds-and-timeout-forms &rest body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 "Usage: (with-timeout (seconds &rest timeout-forms) &rest body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 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
52 the body returns, then timeout-forms are evaluated and returned instead.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 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
54 the timer expiration only occurs when body does a redisplay, or prompts the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 user for input, or calls accept-process-output."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 (let ((seconds (car seconds-and-timeout-forms))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 (timeout-forms (cdr seconds-and-timeout-forms)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 (` (with-timeout-internal (, seconds) '(, (make-symbol "_with_timeout_"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 #'(lambda () (progn (,@ body)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 #'(lambda () (progn (,@ timeout-forms)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 (put 'with-timeout 'lisp-indent-function 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 (defun yes-or-no-p-with-timeout (timeout prompt &optional default-value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 "Just like yes-or-no-p, but will time out after TIMEOUT seconds
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 if the user has not yes answered, returning DEFAULT-VALUE."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 (with-timeout (timeout
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 (message (concat prompt "(yes or no) Timeout to "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 (if default-value "Yes" "No")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 default-value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 (yes-or-no-p prompt)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 (defun y-or-n-p-with-timeout (timeout prompt &optional default-value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 "Just like y-or-n-p, but will time out after TIMEOUT seconds
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 if the user has not yes answered, returning DEFAULT-VALUE."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 (with-timeout (timeout
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 (message (concat prompt "(yes or no) Timeout to "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 (if default-value "Yes" "No")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 default-value)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 (y-or-n-p prompt)))