emacs-elpa-diffs
[Top][All Lists]
Advanced

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

[elpa] externals/compat 1250ea0507: Add read-multiple-choice from Emacs


From: ELPA Syncer
Subject: [elpa] externals/compat 1250ea0507: Add read-multiple-choice from Emacs 26
Date: Fri, 5 Aug 2022 06:57:24 -0400 (EDT)

branch: externals/compat
commit 1250ea050737db8ba07c44eaeab7be2e4faefe0a
Author: Philip Kaludercic <philipk@posteo.net>
Commit: Philip Kaludercic <philipk@posteo.net>

    Add read-multiple-choice from Emacs 26
---
 compat-26.el | 43 +++++++++++++++++++++++++++++++++++++++++++
 compat.texi  | 24 ++++++++++++++++++++++++
 2 files changed, 67 insertions(+)

diff --git a/compat-26.el b/compat-26.el
index 83b89c56c1..76087b8066 100644
--- a/compat-26.el
+++ b/compat-26.el
@@ -627,5 +627,48 @@ If VALUE is nil, PROPERTY is removed from IMAGE."
                     (cdr ,image*)
                     ,property* ,value*)))))))
 
+;;;; Defined in rmc.el
+
+;;*UNTESTED
+(compat-defun read-multiple-choice
+    (prompt choices &optional _help-string _show-help long-form)
+  "Ask user to select an entry from CHOICES, promting with PROMPT.
+This function allows to ask the user a multiple-choice question.
+
+CHOICES should be a list of the form (KEY NAME [DESCRIPTION]).
+KEY is a character the user should type to select the entry.
+NAME is a short name for the entry to be displayed while prompting
+\(if there's no room, it might be shortened).
+
+If LONG-FORM, do a `completing-read' over the NAME elements in
+CHOICES instead."
+  :note "This is a partial implementation of `read-multiple-choice', that
+among other things doesn't offer any help and ignores the
+optional DESCRIPTION field."
+  (if long-form
+      (let ((options (mapconcat #'cadr choices "/"))
+            choice)
+        (setq prompt (concat prompt " (" options "): "))
+        (setq choice (completing-read prompt (mapcar #'cadr choices) nil t))
+        (catch 'found
+          (dolist (option choices)
+            (when (string= choice (cadr option))
+              (throw 'found option)))
+          (error "Invalid choice")))
+    (let ((options
+           (mapconcat
+            (lambda (opt)
+              (format
+               "[%s] %s"
+               (key-description (string (car opt)))
+               (cadr opt)))
+            choices " "))
+          choice)
+      (setq prompt (concat prompt " (" options "): "))
+      (while (not (setq choice (assq (read-char prompt) choices)))
+        (message "Invalid choice")
+        (sit-for 1))
+      choice)))
+
 (compat--inhibit-prefixed (provide 'compat-26))
 ;;; compat-26.el ends here
diff --git a/compat.texi b/compat.texi
index 2335e99721..08d0a5a936 100644
--- a/compat.texi
+++ b/compat.texi
@@ -530,6 +530,30 @@ See @ref{File Name Expansion,File Name Expansion,,elisp,}.
 See @ref{File Name Expansion,File Name Expansion,,elisp,}.
 @end defun
 
+@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
+that will be displayed as the prompt.
+
+@var{choices} is an alist where the first element in each entry is a
+character to be entered, the second element is a short name for the
+entry to be displayed while prompting (if there's room, it might be
+shortened), and the third, optional entry is a longer explanation that
+will be displayed in a help buffer if the user requests more help.
+
+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.
+
+@b{Note:} The Compat implementation of this function ignores the
+optional arguments @var{help-string} and @var{show-help}.  Therefore the
+optional third element in each @var{choices} entry will also be
+disregarded.
+
+See @ref{Reading One Event,Reading One Event,,elisp,}.
+@end defun
+
+
 @defun image-property
 Defined in @code{image.el}.
 



reply via email to

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