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

From: Juri Linkov <[email-masked]>
Date: Sun, 16 Sep 2018 01:42:37 +0300

> 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 ", ")))
          (message
           (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 a)))
                                                 (nth 0 a))))
                              answers-with-help " or ")))
          (short-answer-map
@@ -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 ()
                                  (interactive)
                                  (delete-minibuffer-contents)
                                  (insert (nth 0 a))
                                  (exit-minibuffer))))
-                           (define-key map [remap self-insert-command]
+                           (define-key map [t]
                              (lambda ()
                                (interactive)
                                (delete-minibuffer-contents)
@@ -370,7 +376,10 @@ read-answer
                       (mapconcat
                        (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")