mu4e-bookmarks/folders: allow -ask to return query-item

Instead of just the query-string or maildir name, allow
mu4e-ask-bookmark / mu4e-ask-folder to return the query-item, when a new
optional parameter QUERY-ITEM is non-nil.
This commit is contained in:
Dirk-Jan C. Binnema
2025-04-13 13:37:04 +03:00
parent a6d68a4146
commit 804c4b4cda
2 changed files with 44 additions and 32 deletions

View File

@ -87,11 +87,14 @@ query."
(declare-function mu4e-query-items "mu4e-query-items") (declare-function mu4e-query-items "mu4e-query-items")
(declare-function mu4e--query-item-display-short-counts "mu4e-query-items") (declare-function mu4e--query-item-display-short-counts "mu4e-query-items")
(defun mu4e-ask-bookmark (prompt) (defun mu4e-ask-bookmark (prompt &optional query-item)
"Ask user for bookmark using PROMPT. "Ask user for bookmark using PROMPT.
Return the corresponding query. The bookmark are as defined in Return the corresponding query. The bookmark are as defined in
`mu4e-bookmarks'. `mu4e-bookmarks'.
If QUERY-ITEM is non-nil, return the full query-item rather than
just the query-string.
The names of the bookmarks are displayed in the minibuffer, The names of the bookmarks are displayed in the minibuffer,
suffixed with the short version of the unread counts, as per suffixed with the short version of the unread counts, as per
`mu4e--query-item-display-short-counts'." `mu4e--query-item-display-short-counts'."
@ -107,10 +110,13 @@ suffixed with the short version of the unread counts, as per
(cons (format "%c%s%s" (cons (format "%c%s%s"
(plist-get bm :key) (plist-get bm :key)
(plist-get bm :name) (plist-get bm :name)
unreads) unreads) bm)))
(plist-get bm :query)))) (mu4e-filter-single-key (mu4e-bookmarks))))
(mu4e-filter-single-key (mu4e-bookmarks))))) (chosen (mu4e-read-option prompt bmarks)))
(mu4e-read-option prompt bmarks))) ;; return either the query string, or the corresponding query-item.
(if query-item
chosen
(plist-get chosen :query))))
(defun mu4e-get-bookmark-query (kar) (defun mu4e-get-bookmark-query (kar)
"Get the corresponding bookmarked query for shortcut KAR. "Get the corresponding bookmarked query for shortcut KAR.

View File

@ -247,9 +247,13 @@ to create it; otherwise return nil."
"Get maildirs under `mu4e-maildir'." "Get maildirs under `mu4e-maildir'."
mu4e-maildir-list) mu4e-maildir-list)
(defun mu4e-ask-maildir (prompt)
(defun mu4e-ask-maildir (prompt &optional query-item)
"Ask the user for a maildir (using PROMPT). "Ask the user for a maildir (using PROMPT).
If QUERY-ITEM is non-nil, return the full query-item rather than
just the query-string.
If the special shortcut \"o\" (for _o_ther) is used, or If the special shortcut \"o\" (for _o_ther) is used, or
if (mu4e-maildir-shortcuts) evaluates to nil, let user choose if (mu4e-maildir-shortcuts) evaluates to nil, let user choose
from all maildirs under `mu4e-maildir'. from all maildirs under `mu4e-maildir'.
@ -257,7 +261,8 @@ from all maildirs under `mu4e-maildir'.
The names of the maildirs are displayed in the minibuffer, The names of the maildirs are displayed in the minibuffer,
suffixed with the short version of the unread counts, as per suffixed with the short version of the unread counts, as per
`mu4e--query-item-display-short-counts'." `mu4e--query-item-display-short-counts'."
(let* ((options (let* ((mdirs
(append
(seq-map (seq-map
(lambda (md) (lambda (md)
(let* ((qitem (mu4e--query-item-for-maildir-shortcut md)) (let* ((qitem (mu4e--query-item-for-maildir-shortcut md))
@ -267,22 +272,23 @@ suffixed with the short version of the unread counts, as per
(plist-get md :key) (plist-get md :key)
(or (plist-get md :name) (or (plist-get md :name)
(plist-get md :maildir)) (plist-get md :maildir))
unreads) unreads) md)))
(plist-get md :maildir)))) (mu4e-filter-single-key (mu4e-maildir-shortcuts)))
(mu4e-filter-single-key (mu4e-maildir-shortcuts)))) '(("oOther..." . other)))) ;; append "Other" pseudo-maildir
(response (chosen (mu4e-read-option prompt mdirs))
(if (not options) ;; special case: handle pseudo-maildir 'other
'other (chosen (if (eq chosen 'other)
(mu4e-read-option prompt (list :maildir
(append options
'(("oOther..." . other)))))))
(substring-no-properties (substring-no-properties
(if (eq response 'other)
(progn
(funcall mu4e-completing-read-function prompt (funcall mu4e-completing-read-function prompt
(mu4e-get-maildirs) nil nil (mu4e-get-maildirs) nil nil
mu4e-maildir-initial-input)) mu4e-maildir-initial-input)))
response)))) chosen)))
;; return either the maildir (as a string), or the corresponding
;; query-item.
(if query-item
chosen
(plist-get chosen :maildir))))
(defun mu4e-ask-maildir-check-exists (prompt) (defun mu4e-ask-maildir-check-exists (prompt)
"Like `mu4e-ask-maildir', PROMPT for existence of the maildir. "Like `mu4e-ask-maildir', PROMPT for existence of the maildir.