diff lisp/subr.el @ 112163:b30a0deacfdf

New function read-char-choice for reading a restricted set of chars. * lisp/subr.el (read-char-choice): New function, factored out from dired-query and hack-local-variables-confirm. * lisp/dired-aux.el (dired-query): * lisp/files.el (hack-local-variables-confirm): Use it.
author Chong Yidong <cyd@stupidchicken.com>
date Sat, 08 Jan 2011 14:17:23 -0500
parents 8d03223bf479
children fd05a6b39a42
line wrap: on
line diff
--- a/lisp/subr.el	Sat Jan 08 11:03:31 2011 -0800
+++ b/lisp/subr.el	Sat Jan 08 14:17:23 2011 -0500
@@ -1970,6 +1970,35 @@
 	    t)))
     n))
 
+(defun read-char-choice (prompt chars &optional inhibit-keyboard-quit)
+  "Read and return one of CHARS, prompting for PROMPT.
+Any input that is not one of CHARS is ignored.
+
+If optional argument INHIBIT-KEYBOARD-QUIT is non-nil, ignore
+keyboard-quit events while waiting for a valid input."
+  (unless (consp chars)
+    (error "Called `read-char-choice' without valid char choices"))
+  (let ((cursor-in-echo-area t)
+	(executing-kbd-macro executing-kbd-macro)
+	char done)
+    (while (not done)
+      (unless (get-text-property 0 'face prompt)
+	(setq prompt (propertize prompt 'face 'minibuffer-prompt)))
+      (setq char (let ((inhibit-quit inhibit-keyboard-quit))
+		   (read-event prompt)))
+      (cond
+       ((not (numberp char)))
+       ((memq char chars)
+	(setq done t))
+       ((and executing-kbd-macro (= char -1))
+	;; read-event returns -1 if we are in a kbd macro and
+	;; there are no more events in the macro.  Attempt to
+	;; get an event interactively.
+	(setq executing-kbd-macro nil))))
+    ;; Display the question with the answer.
+    (message "%s%s" prompt (char-to-string char))
+    char))
+
 (defun sit-for (seconds &optional nodisp obsolete)
   "Perform redisplay, then wait for SECONDS seconds or until input is available.
 SECONDS may be a floating-point value.