From: Mark Walters Date: Mon, 3 Jun 2013 20:52:56 +0000 (+0100) Subject: [PATCH v2] emacs: search: allow command line args as part of query X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=bc4a09dadacf469b5cf7b2ee1f1c03c3892ee537;p=notmuch-archives.git [PATCH v2] emacs: search: allow command line args as part of query --- diff --git a/82/788ac96ea8512974fadbe32b7c440cf2088da5 b/82/788ac96ea8512974fadbe32b7c440cf2088da5 new file mode 100644 index 000000000..d924fdb26 --- /dev/null +++ b/82/788ac96ea8512974fadbe32b7c440cf2088da5 @@ -0,0 +1,265 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 1BBED431FAF + for ; Mon, 3 Jun 2013 13:53:14 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: 0.201 +X-Spam-Level: +X-Spam-Status: No, score=0.201 tagged_above=-999 required=5 + tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, + FREEMAIL_ENVFROM_END_DIGIT=1, FREEMAIL_FROM=0.001, + RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id 6BRkwRF7Pc2F for ; + Mon, 3 Jun 2013 13:53:06 -0700 (PDT) +Received: from mail-we0-f176.google.com (mail-we0-f176.google.com + [74.125.82.176]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id 68F0A431FAE + for ; Mon, 3 Jun 2013 13:53:06 -0700 (PDT) +Received: by mail-we0-f176.google.com with SMTP id t61so2118321wes.21 + for ; Mon, 03 Jun 2013 13:53:05 -0700 (PDT) +DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; + h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; + bh=58UsGpDhPimdX29TjAueU5FMTRPqjKKUuniesk7SQAc=; + b=Z3YoZNVIjpAUlCOdbPxMGOa7yyW8HNXCpk279CZHnlPlqkQCp8sQ01N5ZSvwnhIzR+ + /yfVHgn/RZxG9inDYJrzVLteF8ZF7UAGQcqsSCZFnoqGyDsDSTaGGRnZD6Yzz4EPIFHD + yjJWe7yXyNx7XtW6n26lteIQkf5iauyxsGcfUbKHZkBw4ZqRmP0I+slYhZr2zCmmVkZ8 + E9H9B2nUB1bgX/U3isKdXceJ9iLwDIj7vcTpC8IQYIz3LBQhVRTrc7IlM3+14W2zgHnB + urRVjOY7Dp/xYAG+YeA2ilhTOlk8xxqh+WlQxuve2DOuykS6aO8eDHJcAMCkxJrBqMxJ + V/sQ== +X-Received: by 10.180.37.208 with SMTP id a16mr14714899wik.24.1370292783780; + Mon, 03 Jun 2013 13:53:03 -0700 (PDT) +Received: from localhost (93-97-24-31.zone5.bethere.co.uk. [93.97.24.31]) + by mx.google.com with ESMTPSA id d10sm26144562wik.0.2013.06.03.13.53.02 + for + (version=TLSv1.2 cipher=RC4-SHA bits=128/128); + Mon, 03 Jun 2013 13:53:03 -0700 (PDT) +From: Mark Walters +To: notmuch@notmuchmail.org, + jani@nikula.org +Subject: [PATCH v2] emacs: search: allow command line args as part of query +Date: Mon, 3 Jun 2013 21:52:56 +0100 +Message-Id: <1370292776-24535-1-git-send-email-markwalters1009@gmail.com> +X-Mailer: git-send-email 1.7.9.1 +In-Reply-To: <1370252526-18003-1-git-send-email-markwalters1009@gmail.com> +References: <1370252526-18003-1-git-send-email-markwalters1009@gmail.com> +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +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: Mon, 03 Jun 2013 20:53:14 -0000 + +This allows command line arguments for notmuch-search to be part of +the query-string. The string must be of the form +[:blank:]*--cli-arguments -- query. I hope this doesn't clash with +xapian: I believe that queries shouldn't start with a "-". + +Correctly parsed example queries are +--sort=oldest-first -- tag:inbox +--exclude=false -- from:fred + +Some options (currently only sort-order) we parse in emacs, the rest +we just pass to the cli. In light testing it seems to work. + +A full custom parser would be nicer but at least here we are only parsing +the non-query part of a string which is relatively simple: indeed we +already do that in the c code. + +We could just implement the option for sort-order, but I thought for +interface consistency making all the options (sort-order exclude limit +and offset) work was worth the small extra hassle. +--- +This is a rather more complete version of the parent patch with a key +bugfix (thanks to Jani): saved searches with cli arguments work, +refreshing a query works correctly, filtering a query works correctly +(and can take extra cli arguments). + +It may all be too complex, and it may be too hackish but it does seem to work. + +Best wishes + +Mark + + + + emacs/notmuch-hello.el | 5 +++-- + emacs/notmuch-lib.el | 23 +++++++++++++++++++++++ + emacs/notmuch.el | 36 +++++++++++++++++++++++++----------- + 3 files changed, 51 insertions(+), 13 deletions(-) + +diff --git a/emacs/notmuch-hello.el b/emacs/notmuch-hello.el +index c1c6f4b..bcc1843 100644 +--- a/emacs/notmuch-hello.el ++++ b/emacs/notmuch-hello.el +@@ -383,10 +383,11 @@ options will be handled as specified for + `notmuch-hello-insert-searches'." + (with-temp-buffer + (dolist (elem query-alist nil) +- (let ((count-query (if (consp (cdr elem)) ++ (let* ((full-count-query (if (consp (cdr elem)) + ;; do we have a different query for the message count? + (third elem) +- (cdr elem)))) ++ (cdr elem))) ++ (count-query (car (notmuch-parse-query full-count-query)))) + (insert + (notmuch-hello-filtered-query count-query + (or (plist-get options :filter-count) +diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el +index 28f78e0..705d4f3 100644 +--- a/emacs/notmuch-lib.el ++++ b/emacs/notmuch-lib.el +@@ -189,6 +189,29 @@ user-friendly queries." + "Return a query that matches the message with id ID." + (concat "id:" (notmuch-escape-boolean-term id))) + ++(defun notmuch-search-parse-sort-order (args oldest-first) ++ (dolist (arg args nil) ++ (when (equal arg "--sort=oldest-first") ++ (setq oldest-first t)) ++ (when (equal arg "--sort=newest-first") ++ (setq oldest-first nil))) ++ (setq args (delete "--sort=oldest-first" args)) ++ (setq args (delete "--sort=newest-first" args)) ++ (cons oldest-first args)) ++ ++(defun notmuch-parse-query (query) ++ "Parse a query into a search and cli arguments ++ ++Returns a list consisting of query followed by the cli-args (as a ++list). If the string does not have cli-args then this will be nil." ++ ++ (if (string-match "^[[:blank:]]*--.*? -- " query) ++ (let ((actual-query (substring query (match-end 0))) ++ (args (split-string (match-string 0 query) " " t))) ++ (message "Parsing query") ++ (cons actual-query args)) ++ ;; no cli arguments ++ (list query))) + ;; + + (defun notmuch-common-do-stash (text) +diff --git a/emacs/notmuch.el b/emacs/notmuch.el +index 7994d74..6a4052e 100644 +--- a/emacs/notmuch.el ++++ b/emacs/notmuch.el +@@ -255,6 +255,7 @@ For a mouse binding, return nil." + (notmuch-common-do-stash (notmuch-search-find-thread-id))) + + (defvar notmuch-search-query-string) ++(defvar notmuch-search-query-args) + (defvar notmuch-search-target-thread) + (defvar notmuch-search-target-line) + (defvar notmuch-search-continuation) +@@ -409,6 +410,7 @@ Complete list of currently available key bindings: + (interactive) + (kill-all-local-variables) + (make-local-variable 'notmuch-search-query-string) ++ (make-local-variable 'notmuch-search-query-args) + (make-local-variable 'notmuch-search-oldest-first) + (make-local-variable 'notmuch-search-target-thread) + (make-local-variable 'notmuch-search-target-line) +@@ -897,7 +899,7 @@ PROMPT is the string to prompt with." + 'notmuch-search-history nil nil))))) + + ;;;###autoload +-(defun notmuch-search (&optional query oldest-first target-thread target-line continuation) ++(defun notmuch-search (&optional query oldest-first target-thread target-line continuation cli-args) + "Run \"notmuch search\" with the given `query' and display results. + + If `query' is nil, it is read interactively from the minibuffer. +@@ -909,13 +911,20 @@ Other optional parameters are used as follows: + target-line: The line number to move to if the target thread does not + appear in the search results." + (interactive) +- (let* ((query (or query (notmuch-read-query "Notmuch search: "))) ++ (let* ((full-query (or query (notmuch-read-query "Notmuch search: "))) ++ (parsed-query (notmuch-parse-query full-query)) ++ (query (car parsed-query)) ++ (cli-args (or cli-args (cdr parsed-query))) ++ (combined-order-query (notmuch-search-parse-sort-order cli-args oldest-first)) ++ (oldest-first (car combined-order-query)) ++ (cli-args (cdr combined-order-query)) + (buffer (get-buffer-create (notmuch-search-buffer-title query)))) + (switch-to-buffer buffer) + (notmuch-search-mode) + ;; Don't track undo information for this buffer + (set 'buffer-undo-list t) + (set 'notmuch-search-query-string query) ++ (set 'notmuch-search-query-args cli-args) + (set 'notmuch-search-oldest-first oldest-first) + (set 'notmuch-search-target-thread target-thread) + (set 'notmuch-search-target-line target-line) +@@ -928,13 +937,13 @@ Other optional parameters are used as follows: + (erase-buffer) + (goto-char (point-min)) + (save-excursion +- (let ((proc (notmuch-start-notmuch ++ (let ((proc (apply #'notmuch-start-notmuch + "notmuch-search" buffer #'notmuch-search-process-sentinel + "search" "--format=sexp" "--format-version=1" +- (if oldest-first ++ (if notmuch-search-oldest-first + "--sort=oldest-first" + "--sort=newest-first") +- query)) ++ (append cli-args (list query)))) + ;; Use a scratch buffer to accumulate partial output. + ;; This buffer will be killed by the sentinel, which + ;; should be called no matter how the process dies. +@@ -957,9 +966,10 @@ same relative position within the new buffer." + (oldest-first notmuch-search-oldest-first) + (target-thread (notmuch-search-find-thread-id 'bare)) + (query notmuch-search-query-string) +- (continuation notmuch-search-continuation)) ++ (continuation notmuch-search-continuation) ++ (cli-args notmuch-search-query-args)) + (notmuch-kill-this-buffer) +- (notmuch-search query oldest-first target-thread target-line continuation) ++ (notmuch-search query oldest-first target-thread target-line continuation cli-args) + (goto-char (point-min)))) + + (defcustom notmuch-poll-script nil +@@ -1024,18 +1034,22 @@ search." + (set 'notmuch-search-oldest-first (not notmuch-search-oldest-first)) + (notmuch-search-refresh-view)) + +-(defun notmuch-search-filter (query) ++(defun notmuch-search-filter (full-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) ++ (let* ((parsed-query (notmuch-parse-query full-query)) ++ (query (car parsed-query)) ++ (grouped-query (if (string-match-p notmuch-search-disjunctive-regexp query) + (concat "( " query " )") +- query))) ++ query)) ++ (extra-cli-args (cdr parsed-query)) ++ (cli-args (append notmuch-search-query-args extra-cli-args))) + (notmuch-search (if (string= notmuch-search-query-string "*") + grouped-query +- (concat notmuch-search-query-string " and " grouped-query)) notmuch-search-oldest-first))) ++ (concat notmuch-search-query-string " and " grouped-query)) notmuch-search-oldest-first nil nil nil cli-args))) + + (defun notmuch-search-filter-by-tag (tag) + "Filter the current search results based on a single tag. +-- +1.7.9.1 +