--- /dev/null
+Return-Path: <uli@scholler.net>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by arlo.cworth.org (Postfix) with ESMTP id CA0646DE175F\r
+ for <notmuch@notmuchmail.org>; Sun, 6 Sep 2015 14:31:13 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0.005\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0.005 tagged_above=-999 required=5 tests=[AWL=0.005]\r
+ autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id r6NM1w0cA3f9 for <notmuch@notmuchmail.org>;\r
+ Sun, 6 Sep 2015 14:31:11 -0700 (PDT)\r
+Received: from idun.ftbfs.de (idun.ftbfs.de [78.47.2.106])\r
+ by arlo.cworth.org (Postfix) with ESMTP id 7AC4E6DE173F\r
+ for <notmuch@notmuchmail.org>; Sun, 6 Sep 2015 14:31:11 -0700 (PDT)\r
+Received: from localhost (localidun [10.0.0.76])\r
+ by idun.ftbfs.de (Postfix) with ESMTP id 86C6DEC388\r
+ for <notmuch@notmuchmail.org>; Sun, 6 Sep 2015 23:31:07 +0200 (CEST)\r
+Received: from localhost ([10.0.0.76])\r
+ by localhost (idun.ftbfs.de [10.0.0.76]) (amavisd-new, port 2525)\r
+ with SMTP id 15704-28 for <notmuch@notmuchmail.org>;\r
+ Sun, 6 Sep 2015 23:31:07 +0200 (CEST)\r
+Received: from saga.ftbfs.de (unknown [10.0.0.77])\r
+ by idun.ftbfs.de (Postfix) with ESMTP id 34830EC385;\r
+ Sun, 6 Sep 2015 23:31:07 +0200 (CEST)\r
+Received: by saga.ftbfs.de (Postfix, from userid 10)\r
+ id D780E5C5A3; Sun, 6 Sep 2015 23:31:06 +0200 (CEST)\r
+Received: by tardis.scholler.priv (Postfix, from userid 1000)\r
+ id 0BE6310F; Sun, 6 Sep 2015 23:30:10 +0200 (CEST)\r
+From: Uli Scholler <uli@scholler.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v3] emacs: wrap current search in parens when filtering\r
+Date: Sun, 6 Sep 2015 23:29:05 +0200\r
+Message-Id: <1441574945-28437-1-git-send-email-uli@scholler.net>\r
+X-Mailer: git-send-email 2.1.4\r
+In-Reply-To: <m2mvwz29dg.fsf@guru.guru-group.fi>\r
+References: <m2mvwz29dg.fsf@guru.guru-group.fi>\r
+X-Virus-Scanned: at idun.ftbfs.de with p-bank undefined\r
+Cc: Uli Scholler <uli@scholler.net>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.18\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sun, 06 Sep 2015 21:31:13 -0000\r
+\r
+When filtering the current search further with notmuch-search-filter,\r
+wrap the current search in parens (if necessary).\r
+\r
+This fixes unexpected behavior when the current search is\r
+complex (like "(tag:this and date:one_week_ago..) or tag:that").\r
+---\r
+ emacs/notmuch.el | 20 +++++++++++++++-----\r
+ 1 file changed, 15 insertions(+), 5 deletions(-)\r
+\r
+diff --git a/emacs/notmuch.el b/emacs/notmuch.el\r
+index 5284e77..463b926 100644\r
+--- a/emacs/notmuch.el\r
++++ b/emacs/notmuch.el\r
+@@ -978,18 +978,28 @@ default sort order is defined by `notmuch-search-oldest-first'."\r
+ (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first))\r
+ (notmuch-search-refresh-view))\r
+ \r
++(defun notmuch-group-disjunctive-query-string (query-string)\r
++ "Group query if it contains a complex expression.\r
++\r
++Enclose QUERY-STRING in parentheses if it matches\r
++`notmuch-search-disjunctive-regexp'."\r
++ (if (string-match-p notmuch-search-disjunctive-regexp query-string)\r
++ (concat "( " query-string " )")\r
++ query-string))\r
++\r
+ (defun notmuch-search-filter (query)\r
+ "Filter the current search results based on an additional query string.\r
+ \r
+ Runs a new search matching only messages that match both the\r
+ current search results AND the additional query string provided."\r
+ (interactive (list (notmuch-read-query "Filter search: ")))\r
+- (let ((grouped-query (if (string-match-p notmuch-search-disjunctive-regexp query)\r
+- (concat "( " query " )")\r
+- query)))\r
+- (notmuch-search (if (string= notmuch-search-query-string "*")\r
++ (let ((grouped-query (notmuch-group-disjunctive-query-string query))\r
++ (grouped-original-query (notmuch-group-disjunctive-query-string\r
++ notmuch-search-query-string)))\r
++ (notmuch-search (if (string= grouped-original-query "*")\r
+ grouped-query\r
+- (concat notmuch-search-query-string " and " grouped-query)) notmuch-search-oldest-first)))\r
++ (concat grouped-original-query " and " grouped-query))\r
++ notmuch-search-oldest-first)))\r
+ \r
+ (defun notmuch-search-filter-by-tag (tag)\r
+ "Filter the current search results based on a single tag.\r
+-- \r
+2.1.4\r
+\r
+\r