From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by arlo.cworth.org (Postfix) with ESMTP id 4A4116DE0164 for ; Fri, 25 Aug 2017 18:55:57 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at cworth.org X-Spam-Flag: NO X-Spam-Score: -0.05 X-Spam-Level: X-Spam-Status: No, score=-0.05 tagged_above=-999 required=5 tests=[AWL=-0.089, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.211, SPF_PASS=-0.001] autolearn=disabled Received: from arlo.cworth.org ([127.0.0.1]) by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id vEjL8J2TYm3g for ; Fri, 25 Aug 2017 18:55:56 -0700 (PDT) Received: from mail-wr0-f194.google.com (mail-wr0-f194.google.com [209.85.128.194]) by arlo.cworth.org (Postfix) with ESMTPS id 2FC1A6DE00DF for ; Fri, 25 Aug 2017 18:55:56 -0700 (PDT) Received: by mail-wr0-f194.google.com with SMTP id o76so758254wrb.5 for ; Fri, 25 Aug 2017 18:55:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=fxcBdHfAzQcZDdIiD0equP8CqIhpBiGX4GuJ1AnHTsU=; b=TccwX04NVY9OZUltFqk/6GAnJTYmXWI4qGBw4jBr1frjq3rPsLmbc6ndD74dbOY7Gh FDv2pkj0o7cvz5YxDtbfZc0sNgR7EY+vKd11cIXpXa9LK36VwG59uZwHC7t14X5spkmt 1/8zZItknu8XSzLCi0OpDqgtCTbrnZRvT0hCMIv1PaCyjD7GGEmfLXKqFfxCGt/JFk/u iVBhe7vg0y73HcI9AsBjq3/OlwrRzIur9N7ed0vMw3T8iwPL+I41S6SC5Zi2OuGpdGGI kneF771SMWH8v6M7oyzuxEoZfyM1VMD7bmauDJAk7uETHzKZsh+Ue2uE4Oy+WOemWhq1 RpyA== X-Gm-Message-State: AHYfb5hVJvrCmitR1MqmSA8hUC45Re6YQASyocA19uszzQYu41ssZcXq mK3ehlC4dlfLy5SvPac= X-Received: by 10.223.173.193 with SMTP id w59mr168418wrc.135.1503712554317; Fri, 25 Aug 2017 18:55:54 -0700 (PDT) Received: from home.thecybershadow.net ([89.28.117.31]) by smtp.gmail.com with ESMTPSA id z7sm5548653wrg.74.2017.08.25.18.55.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 25 Aug 2017 18:55:53 -0700 (PDT) From: Vladimir Panteleev To: notmuch@notmuchmail.org Cc: Vladimir Panteleev Subject: [PATCH v2] emacs: Add notmuch-update-search-tags Date: Sat, 26 Aug 2017 01:55:41 +0000 Message-Id: <20170826015541.25937-1-notmuch@thecybershadow.net> X-Mailer: git-send-email 2.14.1 In-Reply-To: <41a586b8-5059-7190-3ae6-ab6017795c28@gmail.com> References: <41a586b8-5059-7190-3ae6-ab6017795c28@gmail.com> X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 26 Aug 2017 01:55:57 -0000 From: Vladimir Panteleev 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