Skip to content

Commit a15e640

Browse files
committed
mu4e: implement :hide-if-no-unread
Make mu4e-maildir-shortcut and mu4e-bookmarks understand a property :hide-if-no-unread~, which hides the maildir/bookmark from the main-view if there are no unread messages which the corresponding query.
1 parent 5608892 commit a15e640

7 files changed

+101
-39
lines changed

NEWS.org

+4
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@
9191
automatically translate; this depends on the ~pcre2el~ package which the user
9292
should install when using regular expression-addresses.
9393

94+
- ~mu4e-maildir-shortcuts~ and ~mu4e-bookmarks~ now understand a property
95+
~:hide-if-no-unread~, which hides the maildir/bookmark from the main-view if
96+
there are no unread messages which the corresponding query.
97+
9498
* 1.12 (released on February 24, 2024)
9599

96100
** Some highlights

mu4e/mu4e-bookmarks.el

+4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ item must be unique among `mu4e-bookmarks' and
6565
`mu4e-maildir-shortcuts'.
6666
- `:hide' - if t, the bookmark is hidden from the main-view and
6767
speedbar.
68+
- `:hide-if-no-unread' - if t, the shortcut is hidden from
69+
the main-view if it contains are no unread messages.
70+
71+
You can also use:
6872
- `:hide-unread' - do not show the counts of
6973
unread/total number of matches for the query in the main-view.
7074
This can be useful if a bookmark uses a very slow query.

mu4e/mu4e-folders.el

+5-1
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,13 @@ Each of the list elements is a plist with at least:
105105
Optionally, you can add the following:
106106
`:name' - name of the maildir to be displayed in main-view.
107107
`:hide' - if t, the shortcut is hidden from the main-view.
108+
`:hide-if-no-unread' - if it t, the shortcut is hidden from
109+
the main-view if it contains no unread messages.
110+
111+
You can also use:
108112
`:hide-unread' - do not show the counts of unread/total number
109113
of matches for the maildir in the main-view, and is implied
110-
from `:hide'.
114+
from `:hide'.
111115
112116
For backward compatibility, an older form is recognized as well:
113117

mu4e/mu4e-helpers.el

+27
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,33 @@ This is mu4e's version of Emacs 29's `plistp'."
593593
(let ((len (proper-list-p object)))
594594
(and len (zerop (% len 2)))))
595595

596+
(defun mu4e-plist-do (func plist)
597+
"Apply FUNC to each element in PLIST.
598+
FUNC receives to arguments: the key and its value."
599+
(when plist
600+
(funcall func (car plist) (cadr plist))
601+
(mu4e-plist-do func (cddr plist))))
602+
603+
(defun mu4e-plist-remove (plist prop)
604+
"Remove PROP from PLIST.
605+
Returns the updated PLIST."
606+
;; inspired by org-plist-delete
607+
(let (p)
608+
(while plist
609+
(if (not (eq prop (car plist)))
610+
(setq p (plist-put p (car plist) (nth 1 plist))))
611+
(setq plist (cddr plist)))
612+
p))
613+
614+
(defun mu4e-plist-remove-nils (plist)
615+
"Remove all properties with value nil from PLIST."
616+
(let (p)
617+
(while plist
618+
(when (cadr plist)
619+
(setq p (plist-put p (car plist) (cadr plist))))
620+
(setq plist (cddr plist)))
621+
p))
622+
596623
(defun mu4e--message-hide-headers ()
597624
"Hide headers based on the `message-hidden-headers' variable.
598625
This is mu4e's version of the post-emacs-28 `message-hide-headers',

mu4e/mu4e-main.el

+8-6
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,11 @@ for aligning them."
223223
(mapconcat
224224
(lambda (item)
225225
(cl-destructuring-bind
226-
(&key hide name key favorite query &allow-other-keys) item
226+
(&key name key favorite query
227+
hide hide-if-no-unread unread &allow-other-keys) item
227228
;; hide items explicitly hidden, without key or wrong category.
228-
(if hide
229-
""
229+
(if (or hide (and hide-if-no-unread (zerop unread)))
230+
"" ;; hide
230231
(let ((item-info
231232
;; note, we have a function for the binding,
232233
;; and perhaps a different one for the lambda.
@@ -235,7 +236,7 @@ for aligning them."
235236
(list #'mu4e-search-maildir #'mu4e-search
236237
query))
237238
((eq item-type 'bookmarks)
238-
(list #'mu4e-search-bookmark #'mu4e-search-bookmark
239+
(list #'mu4e-search-bookmark #'mu4e-search-bookmark
239240
(mu4e-get-bookmark-query key)))
240241
(t
241242
(mu4e-error "Invalid item-type %s" item-type)))))
@@ -410,8 +411,9 @@ instead."
410411
(unless (file-directory-p smtpmail-queue-dir)
411412
(mu4e-error "`smtpmail-queue-dir' does not exist"))
412413
(setq smtpmail-queue-mail (not smtpmail-queue-mail))
413-
(message (concat "Outgoing mail will now be "
414-
(if smtpmail-queue-mail "queued" "sent directly")))
414+
(mu4e-message
415+
(concat "Outgoing mail will now be "
416+
(if smtpmail-queue-mail "queued" "sent directly")))
415417
(unless (or (eq mu4e-split-view 'single-window)
416418
(not (buffer-live-p (get-buffer mu4e-main-buffer-name))))
417419
(mu4e--main-redraw)))

mu4e/mu4e-query-items.el

+8-12
Original file line numberDiff line numberDiff line change
@@ -228,25 +228,21 @@ bookmark or maildir."
228228
(list
229229
:name name
230230
:query query
231-
:key (plist-get item :key)
232231
:count count
233232
:unread unread
234233
:delta-count (- count baseline-count)
235234
:delta-unread delta-unread)))
236235
;; remember the *effective* query too; we don't really need it, but
237236
;; useful for debugging.
238-
(unless (string= query effective-query)
239-
(plist-put value :effective-query effective-query))
240-
;;for matching maildir shortcuts
241-
(when maildir (plist-put value :maildir maildir))
237+
(setq value (plist-put value :effective-query effective-query))
238+
(setq value (plist-put value :maildir maildir))
239+
;; copy some other items from item.
240+
(mu4e-plist-do (lambda (k v)
241+
(when (memq k '(:key :maildir :hide :hide-if-no-unread
242+
:hide-unread))
243+
(setq value (plist-put value k v)))) item)
242244
;; nil props bring me discomfort
243-
(when (plist-get item :favorite)
244-
(plist-put value :favorite t))
245-
(when (plist-get item :hide)
246-
(plist-put value :hide t))
247-
(when (plist-get item :hide-unread)
248-
(plist-put value :hide-unread t))
249-
value))
245+
(mu4e-plist-remove-nils value)))
250246
data))
251247

252248
(defun mu4e-query-items (&optional type)

mu4e/mu4e.texi

+45-20
Original file line numberDiff line numberDiff line change
@@ -840,12 +840,15 @@ instance:
840840
:query "list:mu-discuss.googlegroups.com AND date:7d..now"))
841841
@end lisp
842842

843-
There are optional keys @t{:hide} to hide the bookmark from the main menu, but
844-
still have it available (using @key{b})) and @t{:hide-unread} to avoid
845-
generating the unread-number; that can be useful if you have bookmarks for slow
846-
queries. Note that @t{:hide-unread} is implied when the query is not a string;
847-
this for the common case where the query function involves some user input,
848-
which would be disruptive in this case.
843+
There are optional keys @code{:hide} to hide the bookmark or maildirs from the
844+
main menu, but still have it available (using @key{b})), and
845+
@code{:hide-if-no-unread} to hide it if there are no unread messages.
846+
847+
To customize the display, there is also @code{:hide-unread} to avoid generating
848+
the unread-number; that can be useful if you have bookmarks for slow queries.
849+
Note that @code{:hide-unread} is implied when the query is not a string; this
850+
for the common case where the query function involves some user input, which
851+
would be disruptive in this case.
849852

850853
There is also the optional @code{:favorite} property, which at most one bookmark
851854
should have; this bookmark is highlighted in the main view, and its
@@ -2180,18 +2183,36 @@ be instructive:
21802183
"List of pre-defined queries that are shown on the main screen.
21812184
21822185
Each of the list elements is a plist with at least:
2183-
:name - the name of the query
2184-
:query - the query expression
2185-
:key - the shortcut key.
2186-
2187-
Optionally, you add the following:
2188-
:hide - if t, bookmark is hidden from the main-view and speedbar.
2189-
:hide-unread - do not show the counts of unread/total number
2190-
of matches for the query. This can be useful if a bookmark uses
2191-
a very slow query. :hide-unread is implied from :hide.
2192-
"
2186+
`:name' - the name of the query
2187+
`:query' - the query expression string or function
2188+
`:key' - the shortcut key (single character)
2189+
2190+
Optionally, you can add the following:
2191+
2192+
- `:favorite' - if t, monitor the results of this query, and make
2193+
it eligible for showing its status in the modeline. At most
2194+
one bookmark should have this set to t (otherwise the _first_
2195+
bookmark is the implicit favorite). The query for the `:favorite'
2196+
item must be unique among `mu4e-bookmarks' and
2197+
`mu4e-maildir-shortcuts'.
2198+
- `:hide' - if t, the bookmark is hidden from the main-view and
2199+
speedbar.
2200+
- `:hide-if-no-unread' - if t, the shortcut is hidden from
2201+
the main-view if it contains are no unread messages.
2202+
2203+
You can also use:
2204+
- `:hide-unread' - do not show the counts of
2205+
unread/total number of matches for the query in the main-view.
2206+
This can be useful if a bookmark uses a very slow query.
2207+
2208+
`:hide-unread' is implied from `:hide'.
2209+
2210+
Note: for efficiency, queries used to determine the unread/all
2211+
counts do not discard duplicate or unreadable messages. Thus, the
2212+
numbers shown may differ from the number you get from a normal
2213+
query."
21932214
:type '(repeat (plist))
2194-
:group 'mu4e)
2215+
:group 'mu4e-bookmarks)
21952216
@end lisp
21962217

21972218
You can replace these or add your own items, by putting in your
@@ -2303,14 +2324,18 @@ maildirs (folders) very quickly --- for example, getting to the @t{/lists}
23032324
folder only requires you to type @kbd{jl}, then change to @t{/work} with
23042325
@kbd{jw}.
23052326

2306-
While in queries you need to quote folder names (maildirs) with spaces in
2307-
them, you should @emph{not} quote them when used in
2308-
@code{mu4e-maildir-shortcuts}, since @t{mu4e} does that automatically for you.
2327+
While in queries you need to quote folder names (maildirs) with spaces in them,
2328+
you should @emph{not} quote them when used in @code{mu4e-maildir-shortcuts},
2329+
since @t{mu4e} does that automatically for you.
23092330

23102331
The very same shortcuts are used by @kbd{M-x mu4e-mark-for-move} (default
23112332
shortcut @key{m}); so, for example, if you want to move a message to the
23122333
@t{/archive} folder, you can do so by typing @kbd{ma}.
23132334

2335+
For further customization, you can use @code{:hide}, @code{:hide-if-no-unread},
2336+
@code{:hide-unread} and @code{:favorite} properties, just like for
2337+
@ref{Bookmarks and Maildirs}.
2338+
23142339
@node Other search functionality
23152340
@section Other search functionality
23162341

0 commit comments

Comments
 (0)