From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Chong Yidong Newsgroups: gmane.emacs.devel Subject: Reading char choices Date: Fri, 07 Jan 2011 11:14:20 -0500 Message-ID: <871v4ohesj.fsf@stupidchicken.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: dough.gmane.org 1294417469 20408 80.91.229.12 (7 Jan 2011 16:24:29 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 7 Jan 2011 16:24:29 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri Jan 07 17:24:25 2011 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1PbF6y-0007ld-P3 for ged-emacs-devel@m.gmane.org; Fri, 07 Jan 2011 17:24:25 +0100 Original-Received: from localhost ([127.0.0.1]:44913 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PbF4e-0000AL-6M for ged-emacs-devel@m.gmane.org; Fri, 07 Jan 2011 11:21:56 -0500 Original-Received: from [140.186.70.92] (port=52904 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PbF4T-00007M-Ta for emacs-devel@gnu.org; Fri, 07 Jan 2011 11:21:48 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PbF4Q-00026q-8B for emacs-devel@gnu.org; Fri, 07 Jan 2011 11:21:43 -0500 Original-Received: from vm-emlprdomr-04.its.yale.edu ([130.132.50.145]:55671) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PbF4Q-00026P-3M for emacs-devel@gnu.org; Fri, 07 Jan 2011 11:21:42 -0500 Original-Received: from furball (72-254-94-244.client.stsn.net [72.254.94.244]) (authenticated bits=0) by vm-emlprdomr-04.its.yale.edu (8.14.4/8.14.4) with ESMTP id p07GLc6r012233 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT) for ; Fri, 7 Jan 2011 11:21:40 -0500 Original-Received: by furball (Postfix, from userid 1000) id 0A7A8161E9C; Fri, 7 Jan 2011 11:14:20 -0500 (EST) X-Scanned-By: MIMEDefang 2.71 on 130.132.50.145 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:134354 Archived-At: There are a couple of places in Emacs where we do a multiple-choice query, looping until the user inputs one of a list of acceptable chars. See, e.g., dired-query and hack-local-variables-confirm. I think we should assign a standard function to this, as shown below. Thoughts? (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." (let ((cursor-in-echo-area t) (executing-kbd-macro executing-kbd-macro) char done) (while (not done) (message "%s" prompt) (setq char (read-event)) (if (numberp char) (cond ((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)) ((eq (key-binding (vector char)) 'keyboard-quit) (if inhibit-keyboard-quit (keyboard-quit))) ((setq done (memq char chars)))))) ;; Display the question with the answer. (message "%s" (concat prompt (char-to-string char))) char))