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