I happen to have a lot of saved searches that are variants of the
tag:inbox search, (such as "tag:inbox and tag:notmuch"). The logic for
these was always matching inbox first, resulting in "[ inbox ] and
tag:notmuch" rather than "notmuch" as desired.
Anchor the regular expression on both ends to make it look harder for
the better match.
(defun notmuch-search-buffer-title (query)
"Returns the title for a buffer with notmuch search results."
(let* ((folder (rassoc-if (lambda (key)
- (string-match (concat "^" (regexp-quote key))
+ (string-match (concat "^" (regexp-quote key) "$")
query))
(notmuch-saved-searches)))
(folder-name (car folder))