mu4e/server: get cached list of labels

This will be useful with label commands.
This commit is contained in:
Dirk-Jan C. Binnema
2025-08-25 23:11:37 +03:00
committed by Seth Ladygo
parent 3e5ef67650
commit 5a6ca68846
4 changed files with 36 additions and 11 deletions

View File

@ -551,19 +551,35 @@ Server::Private::contacts_handler(const Command& cmd)
mu_debug("sent {} of {} contact(s)", n, store().contacts_cache().size());
}
template<typename T, typename F>
std::string quoted_join(const T& items, F&& func)
{
std::vector<std::string> vec;
std::transform(items.begin(), items.end(), std::back_inserter(vec),
[&](const auto& item) { return quote(func(item)); });
return join(vec, " ");
}
void
Server::Private::data_handler(const Command& cmd)
{
const auto request_type{unwrap(cmd.symbol_arg(":kind"))};
if (request_type == "maildirs") {
auto&& out{make_output_stream()};
mu_print(out, "(");
for (auto&& mdir: store().maildirs())
mu_println(out, "{}", quote(std::move(mdir)));
mu_print(out, ")");
output(mu_format("(:maildirs {})", out.to_string()));
} else
if (request_type == "maildirs")
output(mu_format("(:maildirs ({}))",
quoted_join(store().maildirs(),
[](const auto& item) {
return item; // identity
})));
else if (request_type == "labels")
output(mu_format("(:labels ({}))",
quoted_join(store().label_map(),
[](const auto& item) {
return item.first;
})));
else
throw Error(Error::Code::InvalidArgument,
"invalid request type '{}'", request_type);
}

View File

@ -154,6 +154,9 @@ but also manually invoked searches."
:type 'hook
:group 'mu4e-search)
(defvar mu4e-labels-list nil
"Cached list of labels.")
;; Internals
;;; History

View File

@ -130,6 +130,7 @@ Note: this REPL is not to be confused with the mu REPL as per
;; Cached data
(defvar mu4e-maildir-list)
(defvar mu4e-labels-list)
;; Handlers are not strictly internal, but are not meant
;; for overriding outside mu4e. The are mainly for breaking
@ -470,6 +471,8 @@ The server output is as follows:
;; get some data
((plist-get sexp :maildirs)
(setq mu4e-maildir-list (mu4e--server-plist-get sexp :maildirs)))
((plist-get sexp :labels)
(setq mu4e-labels-list (mu4e--server-plist-get sexp :labels)))
;; receive an error
((plist-get sexp :error)
@ -654,7 +657,7 @@ get at most MAX contacts."
(defun mu4e--server-data (kind)
"Request data of some KIND.
KIND is a symbol. Currently supported kinds: maildirs."
KIND is a symbol. One of maildirs, labels."
(mu4e--server-call-mu
`(data :kind ,kind)))

View File

@ -160,8 +160,9 @@ invoke FUNC (if available) afterwards."
(add-hook 'mu4e-query-items-updated-hook #'mu4e--main-redraw)
(setq mu4e--initialized t) ;; last before we call the server.
(mu4e--server-ping)
;; ask for the maildir-list
;; ask for the maildir-list and labels
(mu4e--server-data 'maildirs)
(mu4e--server-data 'labels)
;; maybe request the list of contacts, automatically refreshed after
;; re-indexing
(mu4e--query-items-refresh 'reset-baseline)
@ -249,7 +250,9 @@ invoke FUNC (if available) afterwards."
(unless (and (not (string= mu4e--contacts-tstamp "0"))
(zerop (plist-get info :updated)))
(mu4e--request-contacts-maybe)
(mu4e--server-data 'maildirs)) ;; update maildir list
(mu4e--server-data 'maildirs)
(mu4e--server-data 'labels)) ;; update maildir/labels list
(mu4e--main-redraw))))
((plist-get info :message)
(mu4e-index-message "%s" (plist-get info :message))))))