unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Vladimir Panteleev <notmuch@thecybershadow.net>
To: notmuch@notmuchmail.org
Cc: Vladimir Panteleev <git@thecybershadow.net>
Subject: [PATCH v2] emacs: Add notmuch-update-search-tags
Date: Sat, 26 Aug 2017 01:55:41 +0000	[thread overview]
Message-ID: <20170826015541.25937-1-notmuch@thecybershadow.net> (raw)
In-Reply-To: <41a586b8-5059-7190-3ae6-ab6017795c28@gmail.com>

From: Vladimir Panteleev <git@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

  reply	other threads:[~2017-08-26  1:55 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-14  5:54 [PATCH 0/2] Update search results when tags change Vladimir Panteleev
2017-08-14  5:54 ` [PATCH 1/2] notmuch-tag.el: Fix minor grammar error Vladimir Panteleev
2017-08-23 11:11   ` David Bremner
2017-08-14  5:54 ` [PATCH 2/2] emacs: Add notmuch-update-search-tags Vladimir Panteleev
2017-08-25 11:12   ` David Bremner
2017-08-26  1:50     ` Vladimir Panteleev
2017-08-26  1:55       ` Vladimir Panteleev [this message]
2017-08-26 10:23         ` [PATCH v2] " Mark Walters
2017-08-26 17:15           ` Vladimir Panteleev
2017-08-27  7:46             ` Mark Walters
2017-08-29  1:18         ` David Bremner
2017-09-04 22:05           ` Vladimir Panteleev
2017-09-06 13:11             ` David Bremner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170826015541.25937-1-notmuch@thecybershadow.net \
    --to=notmuch@thecybershadow.net \
    --cc=git@thecybershadow.net \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).