From 6bd7ccb3fe783021917d029cbdcbfca86229d13e Mon Sep 17 00:00:00 2001 From: jao Date: Thu, 19 Aug 2021 20:09:46 +0100 Subject: jao-notmuch: better unread counts, including thread and formatter --- lib/net/jao-notmuch.el | 75 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 48 insertions(+), 27 deletions(-) diff --git a/lib/net/jao-notmuch.el b/lib/net/jao-notmuch.el index e2536c0..39d8b82 100644 --- a/lib/net/jao-notmuch.el +++ b/lib/net/jao-notmuch.el @@ -244,38 +244,59 @@ (when-let (ts (if p (plist-get p :tags) (notmuch-show-get-tags))) (or (member "unread" ts) (member "new" ts)))) -(defun jao-notmuch--unread-count () - (let ((p) (cnt) (total)) +(defsubst jao-notmuch-tree--first-p (&optional msg) + (plist-get (or msg (notmuch-tree-get-message-properties)) :first)) + +(defun jao-notmuch--unread-counts (&optional thread) + (let ((p) (cnt) (total) (msg)) (save-excursion - (goto-char (point-min)) - (while (setq p (text-property-search-forward :notmuch-message-properties)) + (if thread + (while (and (not (jao-notmuch-tree--first-p)) + (zerop (forward-line -1)))) + (goto-char (point-min))) + (while (and (setq msg (notmuch-tree-get-message-properties)) + (or (not cnt) + (not thread) + (not (jao-notmuch-tree--first-p msg)))) (unless cnt (setq cnt 0 total 0)) (setq total (1+ total)) - (when (jao-notmuch--looking-at-new-p (prop-match-value p)) - (setq cnt (1+ cnt))))) - (when cnt (format "[%s] %s left" total cnt)))) - -(defun jao-notmuch--tree-update-buffer-name (&optional n) - (when-let ((n (or n (jao-notmuch--unread-count)))) - (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) - (jao-notmuch-tree-hide-all))))) - -(advice-add 'notmuch-tree-process-sentinel :after #'jao-notmuch--tree-sentinel) + (when (jao-notmuch--looking-at-new-p msg) + (setq cnt (1+ cnt))) + (forward-line 1))) + (when cnt (list total cnt)))) + +(defvar jao-notmuch-tree-buffer-name-format "%Q [%N / %T] %n / %t") + +(defun jao-notmuch-tree--format-name (query total new ttotal tnew) + (format-spec jao-notmuch-tree-buffer-name-format + `((?T . ,total) (?N . ,new) (?Q . ,query) + (?t . ,ttotal) (?n . ,tnew)))) + +(defun jao-notmuch--tree-update-buffer-name () + (when-let* ((n (jao-notmuch--unread-counts)) + (nc (jao-notmuch--unread-counts t)) + (q (jao-notmuch--query-name))) + (prog1 + (rename-buffer + (apply #'jao-notmuch-tree--format-name q (append n nc))) + (when (fboundp 'jao-minibuffer-refresh) (jao-minibuffer-refresh))))) + +(defun jao-notmuch-tree--find-update-buffer-name (&rest _args) + (when-let* ((mb (window-buffer notmuch-tree-message-window)) + (tb (seq-find (lambda (b) + (with-current-buffer b + (and (derived-mode-p 'notmuch-tree-mode) + (eq notmuch-tree-message-buffer mb) + b))) + (buffer-list)))) + (with-current-buffer tb (jao-notmuch--tree-update-buffer-name)))) + +(add-hook 'notmuch-after-tag-hook #'jao-notmuch-tree--find-update-buffer-name) (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 n))) + (message (jao-notmuch--tree-update-buffer-name))) ;; Outline mode for tree view @@ -303,8 +324,7 @@ ((notmuch-tree-get-message-id) (notmuch-tree-matching-message prev (not no-exit))))) (when (notmuch-tree-get-message-id) - (jao-notmuch-tree-hide-others t)) - (jao-notmuch--tree-update-buffer-name)) + (jao-notmuch-tree-hide-others t))) (defvar jao-notmuch-tree--prefix-map (let ((m (make-keymap "Thread operations"))) @@ -315,6 +335,7 @@ (define-key m (kbd "h") #'outline-hide-entry) (define-key m (kbd "H") #'outline-hide-body) (define-key m (kbd "o") #'jao-notmuch-tree-hide-others) + (define-key m (kbd "n") #'outline-hide-other) m)) (defun jao-notmuch-tree-outline-setup (&optional prefix) -- cgit v1.2.3