From c520e955f78ed000f7dc2aa76030b2db9d3792d3 Mon Sep 17 00:00:00 2001
From: jao <jao@gnu.org>
Date: Wed, 16 Jun 2021 23:42:38 +0100
Subject: jao-notmuch: better query buffer names

---
 lib/net/jao-notmuch.el | 72 +++++++++++++++++++++++++++++++-------------------
 1 file changed, 45 insertions(+), 27 deletions(-)

(limited to 'lib/net')

diff --git a/lib/net/jao-notmuch.el b/lib/net/jao-notmuch.el
index 85c241e..de1885f 100644
--- a/lib/net/jao-notmuch.el
+++ b/lib/net/jao-notmuch.el
@@ -41,25 +41,30 @@
     (setq jao-notmuch-mailboxes-rx (regexp-opt jao-notmuch-mailboxes)))
   (if (string-match jao-notmuch-mailboxes-rx full-path)
       (match-string 0 full-path)
-    (user-error "Message not in any mailbox!")))
+    (user-error "Message not in any registered mailbox!")))
 
 (defun jao-notmuch--msg-props ()
   (if-let ((p (save-excursion
+                (beginning-of-line)
                 (text-property-search-forward :notmuch-message-properties))))
       (prop-match-value p)
     (user-error "No message at point")))
 
-(defun jao-notmuch--move (full-path &optional d seen)
-  (let* ((ff (jao-notmuch--path-to-mailbox full-path))
+(defun jao-notmuch--full-path ()
+  (seq-find #'file-exists-p (plist-get (jao-notmuch--msg-props) :filename)))
+
+(defun jao-notmuch--move (&optional full-path d seen)
+  (let* ((full-path (or full-path (jao-notmuch--full-path)))
+         (ff (jao-notmuch--path-to-mailbox full-path))
          (d (or d (completing-read (format "From %s to: " ff)
                                    (remove ff jao-notmuch-mailboxes) nil t)))
          (dest (string-replace ff d full-path))
          (dest (replace-regexp-in-string ",U=.+$" (if seen "m,S" "m") dest))
-         (fnd (file-name-nondirectory full-path))
-         (dnd (file-name-nondirectory dest)))
-    (when (y-or-n-p (format "%s/%s -> %s/%s? " ff fnd d dnd))
-      (notmuch-tree-tag (append (notmuch-tag-change-list (split-string ff "/") t)
-                                (notmuch-tag-change-list (split-string d "/"))))
+         (ftags (split-string ff "/"))
+         (ttags (split-string d "/")))
+    (when (y-or-n-p (format "%s -> %s? " ftags ttags))
+      (notmuch-tree-tag (append (notmuch-tag-change-list ftags t)
+                                (notmuch-tag-change-list ttags)))
       (notmuch-tree-close-message-window)
       (rename-file full-path dest)
       (shell-command-to-string "notmuch new")
@@ -68,18 +73,7 @@
 (defun jao-notmuch-move-message ()
   "Move message at point to another folder."
   (interactive)
-  (let* ((p (jao-notmuch--msg-props))
-         (f (car (plist-get p :filename))))
-    (jao-notmuch--move (or f ""))))
-
-(defun jao-notmuch-tree-spam ()
-  "Move message at point to the spam folder."
-    (interactive)
-  (let* ((p (jao-notmuch--msg-props))
-         (f (car (plist-get p :filename)))
-         (mbox (car (split-string (jao-notmuch--path-to-mailbox f) "/"))))
-    (notmuch-tree-tag '("-new" "-unread"))
-    (jao-notmuch--move f (concat mbox "/spam") t)))
+  (jao-notmuch--move))
 
 
 ;; Targetting the displayed message from the tree view
@@ -140,30 +134,49 @@
 
 ;; Keeping track of unread messages in current tree view
 
+(defvar-local jao-notmuch--query-name nil)
+
+(defun jao-notmuch--query-name ()
+  (when notmuch-tree-basic-query
+    (or jao-notmuch--query-name
+        (setq jao-notmuch--query-name
+              (let ((q (seq-find (lambda (q)
+                                   (string= (or (plist-get q :query) "")
+                                            notmuch-tree-basic-query))
+                                 notmuch-saved-searches)))
+                (or (plist-get q :name)  notmuch-tree-basic-query))))))
+
 (defun jao-notmuch--unread-count ()
   (save-excursion
     (goto-char (point-min))
-    (let ((p) (cnt))
+    (let ((p) (cnt) (total))
       (while (setq p (text-property-search-forward :notmuch-message-properties))
-        (unless cnt (setq cnt 0))
+        (unless cnt (setq cnt 0 total 0))
         (let ((tags (plist-get (prop-match-value p) :tags)))
+          (setq total (1+ total))
           (when (or (member "unread" tags) (member "new" tags))
             (setq cnt (1+ cnt)))))
-      cnt)))
+      (when cnt (format "%s out of %s messages left" cnt total)))))
 
 (defun jao-notmuch--tree-update-buffer-name (&optional n)
   (when-let ((n (or n (jao-notmuch--unread-count))))
-    (rename-buffer (format "*%s - {%s messages left}*"
-                           notmuch-tree-basic-query
-                           n))
+    (rename-buffer (format "{%s - %s}" (jao-notmuch--query-name) n))
     (jao-minibuffer-refresh)))
 
+(defun jao-notmuch--tree-sentinel (proc &rest _)
+  (let ((buffer (process-buffer proc))
+        (status (process-status proc)))
+    (when (and (eq status 'exit) (buffer-live-p buffer))
+      (with-current-buffer buffer (jao-notmuch--tree-update-buffer-name)))))
+
+(advice-add 'notmuch-tree-process-sentinel :after #'jao-notmuch--tree-sentinel)
+
 (defun jao-notmuch-echo-count ()
   "Show the number of unread messages left in this tree view."
   (interactive)
   (when-let ((n (jao-notmuch--unread-count)))
     (jao-notmuch--tree-update-buffer-name n)
-    (message "%s messages left" n)))
+    (message n)))
 
 (defun jao-notmuch-tree-next (thread &optional no-exit)
   "Next message or thread in forest or exit if none."
@@ -198,6 +211,11 @@
   (interactive)
   (jao-notmuch-tree--tag-and-next '("-unread" "-new") nil t))
 
+(defun jao-notmuch-tree-spam (unmark)
+  (interactive "P")
+  (let ((tags (if unmark '("-spam") '("-unread" "-new" "+spam"))))
+    (jao-notmuch-tree--tag-and-next tags nil nil)))
+
 ;; Scrolling the shown message
 
 (defun jao-notmuch-tree-scroll-or-next ()
-- 
cgit v1.2.3