[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Reading char choices

From: Chong Yidong
Subject: Reading char choices
Date: Fri, 07 Jan 2011 11:14:20 -0500

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.

(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)))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]