Allow read-multiple-choice to do long-form answers
* doc/lispref/commands.texi (Reading One Event): Document it. * lisp/emacs-lisp/rmc.el (read-multiple-choice): Allow using long-form answers instead of single character ones. (read-multiple-choice--long-answers): New function. (read-multiple-choice--short-answers): Refactored out from the main function.
This commit is contained in:
parent
49910adf87
commit
bed9fd41ef
3 changed files with 31 additions and 4 deletions
|
|
@ -3198,7 +3198,7 @@ causes it to evaluate @code{help-form} and display the result. It
|
||||||
then continues to wait for a valid input character, or keyboard-quit.
|
then continues to wait for a valid input character, or keyboard-quit.
|
||||||
@end defun
|
@end defun
|
||||||
|
|
||||||
@defun read-multiple-choice prompt choices &optional help-string show-help
|
@defun read-multiple-choice prompt choices &optional help-string show-help long-form
|
||||||
Ask user a multiple choice question. @var{prompt} should be a string
|
Ask user a multiple choice question. @var{prompt} should be a string
|
||||||
that will be displayed as the prompt.
|
that will be displayed as the prompt.
|
||||||
|
|
||||||
|
|
@ -3217,6 +3217,11 @@ If optional argument @var{show-help} is non-@code{nil}, the help
|
||||||
buffer will be displayed immediately, before any user input. If it is
|
buffer will be displayed immediately, before any user input. If it is
|
||||||
a string, use it as the name of the help buffer.
|
a string, use it as the name of the help buffer.
|
||||||
|
|
||||||
|
If optional argument @var{long-form} is non-@code{nil}, the user
|
||||||
|
will have to type in long-form answers (using @code{completing-read})
|
||||||
|
instead of hitting a single key. The answers must be among the second
|
||||||
|
elements of the values in the @var{choices} list.
|
||||||
|
|
||||||
The return value is the matching value from @var{choices}.
|
The return value is the matching value from @var{choices}.
|
||||||
|
|
||||||
@lisp
|
@lisp
|
||||||
|
|
|
||||||
3
etc/NEWS
3
etc/NEWS
|
|
@ -2104,6 +2104,9 @@ patcomp.el, pc-mode.el, pc-select.el, s-region.el, and sregex.el.
|
||||||
|
|
||||||
* Lisp Changes in Emacs 29.1
|
* Lisp Changes in Emacs 29.1
|
||||||
|
|
||||||
|
+++
|
||||||
|
** 'read-multiple-choice' can now use long-form answers.
|
||||||
|
|
||||||
+++
|
+++
|
||||||
** 'read-regexp' now allows the user to indicate whether to use case folding.
|
** 'read-regexp' now allows the user to indicate whether to use case folding.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,8 +23,6 @@
|
||||||
|
|
||||||
;;; Code:
|
;;; Code:
|
||||||
|
|
||||||
(require 'seq)
|
|
||||||
|
|
||||||
(defun rmc--add-key-description (elem)
|
(defun rmc--add-key-description (elem)
|
||||||
(let* ((char (car elem))
|
(let* ((char (car elem))
|
||||||
(name (cadr elem))
|
(name (cadr elem))
|
||||||
|
|
@ -125,7 +123,8 @@
|
||||||
buf))
|
buf))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun read-multiple-choice (prompt choices &optional help-string show-help)
|
(defun read-multiple-choice (prompt choices &optional help-string show-help
|
||||||
|
long-form)
|
||||||
"Ask user to select an entry from CHOICES, promting with PROMPT.
|
"Ask user to select an entry from CHOICES, promting with PROMPT.
|
||||||
This function allows to ask the user a multiple-choice question.
|
This function allows to ask the user a multiple-choice question.
|
||||||
|
|
||||||
|
|
@ -163,12 +162,21 @@ dialogs. Otherwise, the function will always use text-mode dialogs.
|
||||||
|
|
||||||
The return value is the matching entry from the CHOICES list.
|
The return value is the matching entry from the CHOICES list.
|
||||||
|
|
||||||
|
If LONG-FORM, do a `completing-read' over the NAME elements in
|
||||||
|
CHOICES instead.
|
||||||
|
|
||||||
Usage example:
|
Usage example:
|
||||||
|
|
||||||
\(read-multiple-choice \"Continue connecting?\"
|
\(read-multiple-choice \"Continue connecting?\"
|
||||||
\\='((?a \"always\")
|
\\='((?a \"always\")
|
||||||
(?s \"session only\")
|
(?s \"session only\")
|
||||||
(?n \"no\")))"
|
(?n \"no\")))"
|
||||||
|
(if long-form
|
||||||
|
(read-multiple-choice--long-answers prompt choices)
|
||||||
|
(read-multiple-choice--short-answers
|
||||||
|
prompt choices help-string show-help)))
|
||||||
|
|
||||||
|
(defun read-multiple-choice--short-answers (prompt choices help-string show-help)
|
||||||
(let* ((prompt-choices
|
(let* ((prompt-choices
|
||||||
(if show-help choices (append choices '((?? "?")))))
|
(if show-help choices (append choices '((?? "?")))))
|
||||||
(altered-names (mapcar #'rmc--add-key-description prompt-choices))
|
(altered-names (mapcar #'rmc--add-key-description prompt-choices))
|
||||||
|
|
@ -244,6 +252,17 @@ Usage example:
|
||||||
(kill-buffer buf))
|
(kill-buffer buf))
|
||||||
(assq tchar choices)))
|
(assq tchar choices)))
|
||||||
|
|
||||||
|
(defun read-multiple-choice--long-answers (prompt choices)
|
||||||
|
(let ((answer
|
||||||
|
(completing-read
|
||||||
|
(concat prompt " ("
|
||||||
|
(mapconcat #'identity (mapcar #'cadr choices) "/")
|
||||||
|
") ")
|
||||||
|
(mapcar #'cadr choices) nil t)))
|
||||||
|
(seq-find (lambda (elem)
|
||||||
|
(equal (cadr elem) answer))
|
||||||
|
choices)))
|
||||||
|
|
||||||
(provide 'rmc)
|
(provide 'rmc)
|
||||||
|
|
||||||
;;; rmc.el ends here
|
;;; rmc.el ends here
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue