[Top][All Lists]

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

bug#32738: 26.1.50; 'read-answer' allows only single characters as short

From: Juri Linkov
Subject: bug#32738: 26.1.50; 'read-answer' allows only single characters as short answers
Date: Sun, 16 Sep 2018 01:42:37 +0300
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (x86_64-pc-linux-gnu)

> This is a feature request.
> The new function read-answer allows only single-character short answers,
> and formats them with %c in the prompt displayed to the user.  This has
> several disadvantages:
>   . Function keys cannot be used as short answers.  For example, it
>     would be good to be able to use F1 for "help", but it is currently
>     impossible.
>   . Character events that aren't valid characters, such as C-M-h,
>     cannot be used, because %c barfs when it sees a character event
>     with modifiers (it doesn't pass the 'characterp' test).
>   . Control characters, like C-h, are shown as raw bytes, with their
>     special face, thus standing out unnecessarily.
> It would be nice if these restrictions could be lifted.

Do you think it should support only characters with modifiers like C-M-h,
or also key sequences?  I guess the latter since this will lift all
restrictions on the KEY arg of define-key.  Since read-answer should
still support single-character for backward-compatibility, this means
adding key sequences as a new feature, so all of these will work:

(read-answer "Question: "
             `(("help0" ?h "show help")
               ("help1" ,(kbd "<f1>") "show help")
               ("help2" ,(kbd "C-M-h") "show help")
               ("help3" ,(kbd "C-h") "show help")))

diff --git a/lisp/emacs-lisp/map-ynp.el b/lisp/emacs-lisp/map-ynp.el
index a61c0adc8f..262284aaf1 100644
--- a/lisp/emacs-lisp/map-ynp.el
+++ b/lisp/emacs-lisp/map-ynp.el
@@ -308,14 +308,18 @@ read-answer
           (format "%s(%s) " question
                   (mapconcat (lambda (a)
                                (if short
-                                   (format "%c" (nth 1 a))
+                                   (if (characterp (nth 1 a))
+                                       (format "%c" (nth 1 a))
+                                     (key-description (nth 1 a)))
                                  (nth 0 a)))
                              answers-with-help ", ")))
           (format "Please answer %s."
                   (mapconcat (lambda (a)
                                (format "`%s'" (if short
-                                                  (string (nth 1 a))
+                                                  (if (characterp (nth 1 a))
+                                                      (string (nth 1 a))
+                                                    (key-description (nth 1 
                                                 (nth 0 a))))
                              answers-with-help " or ")))
@@ -325,13 +329,15 @@ read-answer
                          (let ((map (make-sparse-keymap)))
                            (set-keymap-parent map minibuffer-local-map)
                            (dolist (a answers-with-help)
-                             (define-key map (vector (nth 1 a))
+                             (define-key map (if (characterp (nth 1 a))
+                                                 (vector (nth 1 a))
+                                               (nth 1 a))
                                (lambda ()
                                  (insert (nth 0 a))
-                           (define-key map [remap self-insert-command]
+                           (define-key map [t]
                              (lambda ()
@@ -370,7 +376,10 @@ read-answer
                        (lambda (a)
                          (format "`%s'%s to %s"
-                                 (if short (string (nth 1 a)) (nth 0 a))
+                                 (if short (if (characterp (nth 1 a))
+                                               (string (nth 1 a))
+                                             (key-description (nth 1 a)))
+                                   (nth 0 a))
                                  (if short (format " (%s)" (nth 0 a)) "")
                                  (nth 2 a)))
                        answers-with-help ",\n")

reply via email to

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