diff options
author | Stefan Kangas <stefan@marxist.se> | 2021-12-25 22:58:59 +0100 |
---|---|---|
committer | Stefan Kangas <stefan@marxist.se> | 2021-12-26 15:47:16 +0100 |
commit | 68f15e815e0a475a13d8169cc5d163cf05e7e524 (patch) | |
tree | 3b76e1543e263516319dcee0dedea5d889d6a08c /lisp/emacs-lisp/rmc.el | |
parent | 978987f7ad58cd66fe51cefde53ba4771b189aeb (diff) | |
download | emacs-68f15e815e0a475a13d8169cc5d163cf05e7e524.tar.gz emacs-68f15e815e0a475a13d8169cc5d163cf05e7e524.tar.bz2 emacs-68f15e815e0a475a13d8169cc5d163cf05e7e524.zip |
Factor out new function rmc--add-key-description
* lisp/emacs-lisp/rmc.el (rmc--add-key-description): Factor out
new function from...
(read-multiple-choice): ...here.
* test/lisp/emacs-lisp/rmc-tests.el (test-rmc--add-key-description)
(test-rmc--add-key-description/with-attributes)
(test-rmc--add-key-description/non-graphical-display): New tests.
Diffstat (limited to 'lisp/emacs-lisp/rmc.el')
-rw-r--r-- | lisp/emacs-lisp/rmc.el | 62 |
1 files changed, 30 insertions, 32 deletions
diff --git a/lisp/emacs-lisp/rmc.el b/lisp/emacs-lisp/rmc.el index 8abe570e64b..2f4b10efbbd 100644 --- a/lisp/emacs-lisp/rmc.el +++ b/lisp/emacs-lisp/rmc.el @@ -25,6 +25,33 @@ (require 'seq) +(defun rmc--add-key-description (elem) + (let* ((name (cadr elem)) + (pos (seq-position name (car elem))) + (altered-name + (cond + ;; Not in the name string. + ((not pos) + (format "[%c] %s" (car elem) name)) + ;; The prompt character is in the name, so highlight + ;; it on graphical terminals. + ((display-supports-face-attributes-p + '(:underline t) (window-frame)) + (setq name (copy-sequence name)) + (put-text-property pos (1+ pos) + 'face 'read-multiple-choice-face + name) + name) + ;; And put it in [bracket] on non-graphical terminals. + (t + (concat + (substring name 0 pos) + "[" + (upcase (substring name pos (1+ pos))) + "]" + (substring name (1+ pos))))))) + (cons (car elem) altered-name))) + ;;;###autoload (defun read-multiple-choice (prompt choices &optional help-string) "Ask user to select an entry from CHOICES, promting with PROMPT. @@ -67,42 +94,13 @@ Usage example: \\='((?a \"always\") (?s \"session only\") (?n \"no\")))" - (let* ((altered-names nil) + (let* ((altered-names (mapcar #'rmc--add-key-description + (append choices '((?? "?"))))) (full-prompt (format "%s (%s): " prompt - (mapconcat - (lambda (elem) - (let* ((name (cadr elem)) - (pos (seq-position name (car elem))) - (altered-name - (cond - ;; Not in the name string. - ((not pos) - (format "[%c] %s" (car elem) name)) - ;; The prompt character is in the name, so highlight - ;; it on graphical terminals... - ((display-supports-face-attributes-p - '(:underline t) (window-frame)) - (setq name (copy-sequence name)) - (put-text-property pos (1+ pos) - 'face 'read-multiple-choice-face - name) - name) - ;; And put it in [bracket] on non-graphical terminals. - (t - (concat - (substring name 0 pos) - "[" - (upcase (substring name pos (1+ pos))) - "]" - (substring name (1+ pos))))))) - (push (cons (car elem) altered-name) - altered-names) - altered-name)) - (append choices '((?? "?"))) - ", "))) + (mapconcat (lambda (e) (cdr e)) altered-names ", "))) tchar buf wrong-char answer) (save-window-excursion (save-excursion |