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