From 804c4b4cdafa45b250907dde2c4b2993ee41ae26 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sun, 13 Apr 2025 13:37:04 +0300 Subject: [PATCH] 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. --- mu4e/mu4e-bookmarks.el | 16 +++++++---- mu4e/mu4e-folders.el | 60 +++++++++++++++++++++++------------------- 2 files changed, 44 insertions(+), 32 deletions(-) diff --git a/mu4e/mu4e-bookmarks.el b/mu4e/mu4e-bookmarks.el index af4442ae..0f3d186d 100644 --- a/mu4e/mu4e-bookmarks.el +++ b/mu4e/mu4e-bookmarks.el @@ -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. diff --git a/mu4e/mu4e-folders.el b/mu4e/mu4e-folders.el index db74eba6..e9ca02b4 100644 --- a/mu4e/mu4e-folders.el +++ b/mu4e/mu4e-folders.el @@ -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.