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-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.
Return the corresponding query. The bookmark are as defined in
`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,
suffixed with the short version of the unread counts, as per
`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"
(plist-get bm :key)
(plist-get bm :name)
unreads)
(plist-get bm :query))))
(mu4e-filter-single-key (mu4e-bookmarks)))))
(mu4e-read-option prompt bmarks)))
unreads) bm)))
(mu4e-filter-single-key (mu4e-bookmarks))))
(chosen (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)
"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'."
mu4e-maildir-list)
(defun mu4e-ask-maildir (prompt)
(defun mu4e-ask-maildir (prompt &optional query-item)
"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 (mu4e-maildir-shortcuts) evaluates to nil, let user choose
from all maildirs under `mu4e-maildir'.
@ -257,32 +261,34 @@ from all maildirs under `mu4e-maildir'.
The names of the maildirs are displayed in the minibuffer,
suffixed with the short version of the unread counts, as per
`mu4e--query-item-display-short-counts'."
(let* ((options
(seq-map
(lambda (md)
(let* ((qitem (mu4e--query-item-for-maildir-shortcut md))
(unreads (mu4e--query-item-display-short-counts qitem)))
(cons
(format "%c%s%s"
(plist-get md :key)
(or (plist-get md :name)
(plist-get md :maildir))
unreads)
(plist-get md :maildir))))
(mu4e-filter-single-key (mu4e-maildir-shortcuts))))
(response
(if (not options)
'other
(mu4e-read-option prompt
(append options
'(("oOther..." . other)))))))
(substring-no-properties
(if (eq response 'other)
(progn
(funcall mu4e-completing-read-function prompt
(mu4e-get-maildirs) nil nil
mu4e-maildir-initial-input))
response))))
(let* ((mdirs
(append
(seq-map
(lambda (md)
(let* ((qitem (mu4e--query-item-for-maildir-shortcut md))
(unreads (mu4e--query-item-display-short-counts qitem)))
(cons
(format "%c%s%s"
(plist-get md :key)
(or (plist-get md :name)
(plist-get md :maildir))
unreads) md)))
(mu4e-filter-single-key (mu4e-maildir-shortcuts)))
'(("oOther..." . other)))) ;; append "Other" pseudo-maildir
(chosen (mu4e-read-option prompt mdirs))
;; special case: handle pseudo-maildir 'other
(chosen (if (eq chosen 'other)
(list :maildir
(substring-no-properties
(funcall mu4e-completing-read-function prompt
(mu4e-get-maildirs) nil nil
mu4e-maildir-initial-input)))
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)
"Like `mu4e-ask-maildir', PROMPT for existence of the maildir.