[PATCH 2/2] emacs: Add notmuch-update-search-tags

Vladimir Panteleev git at thecybershadow.net
Sun Aug 13 22:54:57 PDT 2017


Implement an option which, when enabled, causes any tag changes done
from within notmuch-emacs to instantly update matching threads in open
search buffers.
---
 emacs/notmuch-tag.el | 16 ++++++++++++++++
 emacs/notmuch.el     | 15 +++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/emacs/notmuch-tag.el b/emacs/notmuch-tag.el
index 0500927d..41fdeaef 100644
--- a/emacs/notmuch-tag.el
+++ b/emacs/notmuch-tag.el
@@ -364,6 +364,15 @@ the messages that were tagged"
   :options '(notmuch-hl-line-mode)
   :group 'notmuch-hooks)
 
+(defcustom notmuch-update-search-tags t
+  "Update open `notmuch-search' buffers after tags of a message are modified.
+
+Instantly update any matching results in open `notmuch-search'
+buffers, so that all tag changes are immediately reflected."
+  :type 'boolean
+  :group 'notmuch-search
+  :group 'notmuch-tag)
+
 (defvar notmuch-select-tag-history nil
   "Variable to store minibuffer history for
 `notmuch-select-tag-with-completion' function.")
@@ -477,6 +486,13 @@ notmuch-after-tag-hook will be run."
       (let ((batch-op (concat (mapconcat #'notmuch-hex-encode tag-changes " ")
 			      " -- " query)))
 	(notmuch-call-notmuch-process :stdin-string batch-op "tag" "--batch")))
+    (when notmuch-update-search-tags
+      (let ((results (notmuch-call-notmuch-sexp
+		      "search" "--format=sexp" "--format-version=4" query)))
+	(dolist (buffer (buffer-list))
+	  (when (eq (buffer-local-value 'major-mode buffer) 'notmuch-search-mode)
+	    (with-current-buffer buffer
+	      (notmuch-search-update-results results))))))
     (run-hooks 'notmuch-after-tag-hook)))
 
 (defun notmuch-tag-change-list (tags &optional reverse)
diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index 44402f8a..b5fe4e60 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -662,6 +662,21 @@ of the result."
 			  (min init-point (- new-end 1)))))
 	(goto-char new-point)))))
 
+(defun notmuch-search-update-results (results)
+  "Replace results with threads matching RESULTS in-place and redraw them."
+  (let ((pos (next-single-property-change 1 'notmuch-search-result))
+	(results-alist
+	 (mapcar (lambda (result) (cons (plist-get result :thread) result))
+		 results)))
+    (while pos
+      (let* ((prop (get-text-property pos 'notmuch-search-result))
+	     (thread (when prop (plist-get prop :thread)))
+	     (result (when thread (assoc-default thread results-alist))))
+	;; (message "found matching thread: %s" thread)
+	(when result
+	  (notmuch-search-update-result result pos)))
+      (setq pos (next-single-property-change pos 'notmuch-search-result)))))
+
 (defun notmuch-search-process-sentinel (proc msg)
   "Add a message to let user know when \"notmuch search\" exits"
   (let ((buffer (process-buffer proc))
-- 
2.13.3



More information about the notmuch mailing list