From c591c413ba574449276966153419605aa7b09a5f Mon Sep 17 00:00:00 2001
From: jao <jao@gnu.org>
Date: Sun, 6 Jun 2021 04:18:59 +0100
Subject: notmuch: unread count in buffer name

---
 email.org | 69 +++++++++++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 50 insertions(+), 19 deletions(-)

diff --git a/email.org b/email.org
index 2017105..88061a2 100644
--- a/email.org
+++ b/email.org
@@ -370,8 +370,8 @@
               notmuch-show-mark-read-tags '("-new" "-unread")
               notmuch-archive-tags '("+trove" "-new" "-unread" "-flagged")
               notmuch-fcc-dirs
-              '((".*@bigml.com" . "trove/bigml +bigml +sent -new")
-                (".*" . "trove/jao +jao +sent -new"))
+              '((".*@bigml.com" . "bigml/inbox +bigml +sent -new")
+                (".*" . "jao/inbox +jao +sent -new"))
               notmuch-maildir-use-notmuch-insert t
               notmuch-message-headers '("Subject" "To" "Cc" "Date" "List-Id")
               notmuch-wash-signature-lines-max 0
@@ -475,9 +475,26 @@
                   (shr-blocked-images nil))
               (notmuch-refresh-this-buffer)))))
 
+      (defvar-local jao-notmuch--tree-count nil)
+
+      (defun jao-notmuch--tree-set-buffer-name ()
+        (when (derived-mode-p 'notmuch-tree-mode)
+          (rename-buffer (format "*%s - {%s messages left}*"
+                                 notmuch-tree-basic-query
+                                 jao-notmuch--tree-count))))
+
+      (defun jao-notmuch---tree-update-buffer-name ()
+        (when (derived-mode-p 'notmuch-tree-mode)
+          (let ((cnt (or jao-notmuch--tree-count
+                         (plist-get (jao-notmuch-tree-count) :count)
+                         1)))
+            (setq-local jao-notmuch--tree-count (1- cnt))
+            (jao-notmuch--tree-set-buffer-name))))
+
       (defun jao-notmuch-tree-next (thread &optional no-exit)
         "Next message or thread in forest or exit if none."
         (interactive "P")
+        (jao-notmuch---tree-update-buffer-name)
         (if thread
             (notmuch-tree-next-thread)
           (notmuch-tree-next-matching-message (not no-exit))))
@@ -504,20 +521,26 @@
         (interactive)
         (jao-notmuch-tree--tag-and-next '("-unread" "-new") nil t))
 
+      (defun jao-notmuch-tree-count ()
+        (when-let ((q (or notmuch-tree-basic-query notmuch-search-query-string)))
+          (car (notmuch-hello-query-counts `((:query ,q))))))
+
       (defun jao-notmuch-tree-scroll-or-next ()
         "Scroll or next message in forest or exit if none."
         (interactive)
         (if (notmuch-tree-scroll-message-window)
             (notmuch-tree-next-matching-message t)
           (when (not (window-live-p notmuch-tree-message-window))
-            (notmuch-tree-show-message nil))))
+            (notmuch-tree-show-message nil)))
+        (jao-notmuch---tree-update-buffer-name))
 
       (defun jao-notmuch-tree-show-or-scroll ()
         "Show current message, or scroll it if visible."
         (interactive)
         (if (window-live-p notmuch-tree-message-window)
             (scroll-other-window 1)
-          (notmuch-tree-show-message nil)))
+          (notmuch-tree-show-message nil)
+          (jao-notmuch---tree-update-buffer-name)))
 
       (use-package notmuch-tree
         :config
@@ -536,22 +559,30 @@
         (advice-add 'notmuch-tree-format-field
                     :around #'jao-notmuch--format-field)
 
+        (defun jao-notmuch-echo-count ()
+          (interactive)
+          (when-let ((cnts (jao-notmuch-tree-count)))
+            (setq-local jao-notmuch--tree-count (plist-get cnts :count))
+            (jao-notmuch--tree-set-buffer-name)
+            (message "%s messages left" jao-notmuch--tree-count)))
+
         :bind (:map notmuch-tree-mode-map
-               (("." . jao-notmuch-toggle-mime-parts)
-                ("d" . jao-notmuch-tree-delete-message)
-                ("D" . jao-notmuch-tree-delete-thread)
-                ("h" . jao-notmuch-goto-message-buffer)
-                ("H" . jao-notmuch-click-message-buffer)
-                ("i" . jao-notmuch-toggle-images)
-                ("k" . jao-notmuch-tag-jump-and-next)
-                ("K" . jao-notmuch-tree-read-thread)
-                ("n" . jao-notmuch-tree-next)
-                ("RET" . jao-notmuch-tree-show-or-scroll)
-                ("SPC" . jao-notmuch-tree-scroll-or-next))
-               :map notmuch-show-mode-map
-               (("h" . jao-notmuch-goto-index-buffer))
-               :map notmuch-common-keymap
-               (("B" . jao-notmuch-browse-urls))))
+                    (("." . jao-notmuch-toggle-mime-parts)
+                     ("C" . jao-notmuch-echo-count)
+                     ("d" . jao-notmuch-tree-delete-message)
+                     ("D" . jao-notmuch-tree-delete-thread)
+                     ("h" . jao-notmuch-goto-message-buffer)
+                     ("H" . jao-notmuch-click-message-buffer)
+                     ("i" . jao-notmuch-toggle-images)
+                     ("k" . jao-notmuch-tag-jump-and-next)
+                     ("K" . jao-notmuch-tree-read-thread)
+                     ("n" . jao-notmuch-tree-next)
+                     ("RET" . jao-notmuch-tree-show-or-scroll)
+                     ("SPC" . jao-notmuch-tree-scroll-or-next))
+                    :map notmuch-show-mode-map
+                    (("h" . jao-notmuch-goto-index-buffer))
+                    :map notmuch-common-keymap
+                    (("B" . jao-notmuch-browse-urls))))
     #+end_src
 *** address clean-ups
     #+begin_src emacs-lisp
-- 
cgit v1.2.3