[PATCH v2] emacs: Add notmuch-update-search-tags

Vladimir Panteleev notmuch at thecybershadow.net
Fri Aug 25 18:55:41 PDT 2017


From: Vladimir Panteleev <git at thecybershadow.net>

Implement an option which, when enabled, causes any tag changes done
from within notmuch-emacs to instantly update matching threads in open
search buffers.

* notmuch.el: Add notmuch-search-update-results.

* notmuch-tag.el: Add notmuch-update-search-tags; invoke
  notmuch-search-update-results from notmuch-tag when
  notmuch-update-search-tags is non-nil.

* T310-emacs.sh: Add test.
---
 This update now includes a test as well.

 Speaking of which, I had a fun time trying to figure out why my test
 didn't work before I discovered that notmuch-tag-deleted-formats is
 reset in test-lib.el. That took quite a bit of debugging; I think it
 would be good to fix this inconsistency to avoid other contributors
 wasting time on this in the future.

 emacs/notmuch-tag.el | 24 ++++++++++++++++++++++++
 emacs/notmuch.el     | 25 +++++++++++++++++++++++++
 test/T310-emacs.sh   | 18 ++++++++++++++++++
 3 files changed, 67 insertions(+)

diff --git a/emacs/notmuch-tag.el b/emacs/notmuch-tag.el
index 0500927d..3dda7115 100644
--- a/emacs/notmuch-tag.el
+++ b/emacs/notmuch-tag.el
@@ -364,6 +364,23 @@ the messages that were tagged"
   :options '(notmuch-hl-line-mode)
   :group 'notmuch-hooks)
 
+(defcustom notmuch-update-search-tags nil
+  "Update open `notmuch-search' buffers after tags of a message are modified.
+
+When non-nil, instantly update any matching results in open
+`notmuch-search' buffers, so that all tag changes are immediately
+reflected.
+
+Note that this does not cause the list of search results to be
+updated, but only the display of the currently shown search
+results.  If a tag change causes a search query to include or
+exclude results that were respectively absent or present before,
+they will not be added or removed - `notmuch-refresh-this-buffer'
+must be manually invoked to do so."
+  :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 +494,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..9dd9e661 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -662,6 +662,31 @@ 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.
+
+The :orig-tags property is copied over from the old result so
+that tag changes are displayed correctly."
+  (let ((pos (point-min))
+	(results-alist ; Convert list to alist (keyed by :thread).
+	 (mapcar (lambda (result) (cons (plist-get result :thread) result))
+		 results)))
+    (while pos
+      (let (orig-result thread new-result orig-tags final-result)
+	(and ; All of these variables need to be non-nil.
+	 ;; The original search result as it appears in the buffer.
+	 (setq orig-result (get-text-property pos 'notmuch-search-result))
+	 ;; The result's thread ID.
+	 (setq thread (plist-get orig-result :thread))
+	 ;; The matching updated result we received, if any.
+	 (setq new-result (assoc-default thread results-alist))
+	 ;; The original tags of the old result.
+	 (setq orig-tags (plist-get orig-result :orig-tags))
+	 ;; Result with :orig-tags copied from the old result.
+	 (setq final-result (plist-put new-result :orig-tags orig-tags))
+	 (notmuch-search-update-result final-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))
diff --git a/test/T310-emacs.sh b/test/T310-emacs.sh
index fde11790..a4ca09c5 100755
--- a/test/T310-emacs.sh
+++ b/test/T310-emacs.sh
@@ -128,6 +128,24 @@ test_emacs "(notmuch-search \"$os_x_darwin_thread\")
 output=$(notmuch search $os_x_darwin_thread | notmuch_search_sanitize)
 test_expect_equal "$output" "thread:XXX   2009-11-18 [4/4] Jjgod Jiang, Alexander Botero-Lowry; [notmuch] Mac OS X/Darwin compatibility issues (inbox unread)"
 
+test_begin_subtest "Show new tag in search view with notmuch-update-search-tags"
+test_emacs "(let ((notmuch-update-search-tags t)
+		  (notmuch-tag-added-formats '((\".*\" (concat \"new:\" tag)))))
+	      (notmuch-search \"$os_x_darwin_thread\")
+	      (notmuch-test-wait)
+	      (notmuch-tag \"$os_x_darwin_thread\" '(\"+update-search-tags\"))
+	      (test-output))"
+test_expect_equal "$(cat OUTPUT)" $'  2009-11-18 [4/4]   Jjgod Jiang, Alexander Botero-Lowry      [notmuch] Mac OS X/Darwin compatibility issues (inbox unread new:update-search-tags)\nEnd of search results.'
+
+test_begin_subtest "Keep deleted tag in search view with notmuch-update-search-tags"
+test_emacs "(let ((notmuch-update-search-tags t)
+		  (notmuch-tag-deleted-formats '((\".*\" (concat \"deleted:\" tag)))))
+	      (notmuch-search \"$os_x_darwin_thread\")
+	      (notmuch-test-wait)
+	      (notmuch-tag \"$os_x_darwin_thread\" '(\"-update-search-tags\"))
+	      (test-output))"
+test_expect_equal "$(cat OUTPUT)" $'  2009-11-18 [4/4]   Jjgod Jiang, Alexander Botero-Lowry      [notmuch] Mac OS X/Darwin compatibility issues (inbox unread deleted:update-search-tags)\nEnd of search results.'
+
 test_begin_subtest "Add tag (large query)"
 # We use a long query to force us into batch mode and use a funny tag
 # that requires escaping for batch tagging.
-- 
2.14.1



More information about the notmuch mailing list