mu4e-helpers: import new/mu4e--read-char-choice
Starting point; see issue #2157.
This commit is contained in:
committed by
Dirk-Jan C. Binnema
parent
a8c0bbd390
commit
51ba56cf6e
@ -167,6 +167,53 @@ Return the cdr (value) of the matching cell, if any."
|
|||||||
(if match (cdadr match)
|
(if match (cdadr match)
|
||||||
(when match-ci (cdadr match-ci)))))
|
(when match-ci (cdadr match-ci)))))
|
||||||
|
|
||||||
|
(defun new/mu4e--read-char-choice (prompt candidates)
|
||||||
|
"Run a quick `completing-read' for the given CANDIDATES.
|
||||||
|
|
||||||
|
List of CANDIDATES is a list of strings. The first character is
|
||||||
|
used for quick selection."
|
||||||
|
(let* ((candidates-alist
|
||||||
|
(mapcar (lambda (cand)
|
||||||
|
(prog1
|
||||||
|
(cons
|
||||||
|
(concat "["
|
||||||
|
(propertize (substring cand 0 1)
|
||||||
|
'face 'mu4e-highlight-face)
|
||||||
|
"]"
|
||||||
|
(substring cand 1))
|
||||||
|
cand)))
|
||||||
|
candidates))
|
||||||
|
(metadata `(metadata
|
||||||
|
(display-sort-function . ,#'identity)
|
||||||
|
(cycle-sort-function . ,#'identity)))
|
||||||
|
(quick-result)
|
||||||
|
(result
|
||||||
|
(minibuffer-with-setup-hook
|
||||||
|
(lambda ()
|
||||||
|
(add-hook 'post-command-hook
|
||||||
|
(lambda ()
|
||||||
|
;; Exit directly if a quick key is pressed
|
||||||
|
(let ((prefix (minibuffer-contents-no-properties)))
|
||||||
|
(unless (string-empty-p prefix)
|
||||||
|
(mapc (lambda (cand)
|
||||||
|
(when (string-prefix-p prefix (cdr cand) t)
|
||||||
|
(setq quick-result cand)
|
||||||
|
(exit-minibuffer)))
|
||||||
|
candidates-alist))))
|
||||||
|
-1 'local))
|
||||||
|
(completing-read
|
||||||
|
prompt
|
||||||
|
;; Use function with metadata to disable sorting.
|
||||||
|
(lambda (input predicate action)
|
||||||
|
(if (eq action 'metadata)
|
||||||
|
metadata
|
||||||
|
(complete-with-action action candidates-alist input predicate)))
|
||||||
|
;; Require confirmation, if the input does not match a suggestion
|
||||||
|
nil t nil nil nil))))
|
||||||
|
(or (cdr quick-result)
|
||||||
|
(cdr (assoc result candidates-alist)))))
|
||||||
|
|
||||||
|
|
||||||
(defun mu4e--read-choice-builtin (prompt choices)
|
(defun mu4e--read-choice-builtin (prompt choices)
|
||||||
"Read and return one of CHOICES, prompting for PROMPT.
|
"Read and return one of CHOICES, prompting for PROMPT.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user