--- /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 A8F78431FAE\r
+ for <notmuch@notmuchmail.org>; Tue, 4 Jun 2013 08:22:16 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 2.7\r
+X-Spam-Level: **\r
+X-Spam-Status: No, score=2.7 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
+ FREEMAIL_REPLY=2.499, 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 UG+AVvb19ev8 for <notmuch@notmuchmail.org>;\r
+ Tue, 4 Jun 2013 08:22:09 -0700 (PDT)\r
+Received: from mail-wg0-f51.google.com (mail-wg0-f51.google.com\r
+ [74.125.82.51]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client\r
+ certificate requested) by olra.theworths.org (Postfix) with ESMTPS id\r
+ 01589431FAF for <notmuch@notmuchmail.org>; Tue, 4 Jun 2013 08:22:06 -0700\r
+ (PDT)\r
+Received: by mail-wg0-f51.google.com with SMTP id b13so342258wgh.30\r
+ for <notmuch@notmuchmail.org>; Tue, 04 Jun 2013 08:22:04 -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=dgLBF3Eg9tdits3sxJq6MaQ4uqKQHoMctRIO9jq4FNU=;\r
+ b=kpfAPLOYHBkK4BOy2DWTGgxAUuIazW/3PI7VeiJvHuYg8wjxIpamtBoYkF3CCvYZGC\r
+ 5kp2HWRuxwc32mVMB8+F3fn9/Au3pkoJss9U5YvCvzXWOX6vWZ7CYX3pkVIRK9fvHNZZ\r
+ Gj6f8w86uDCa9Or5iHCJrHTnZ/Gu8XlTFLNctbGh5OnoDDwSC3zDW7Rz1i1kCKWBuLQ/\r
+ +7Qc0Kg3KHT6bQTzrXl+pi5ggluKzawWWbBYRW5aoNSAeYhFCsa2v14FVX7j+Xd64bzn\r
+ juoD8GqPVJWguPWlBkeoTdOtFfDssFKIng58AB/kfFttra4j0s86t3PeBcbhrGllUkco\r
+ /hcA==\r
+X-Received: by 10.194.158.34 with SMTP id wr2mr24441174wjb.57.1370359324416;\r
+ Tue, 04 Jun 2013 08:22:04 -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 ay7sm3308044wib.9.2013.06.04.08.22.02\r
+ for <multiple recipients>\r
+ (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+ Tue, 04 Jun 2013 08:22:03 -0700 (PDT)\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH] emacs: search: allow command line args as part of query\r
+Date: Tue, 4 Jun 2013 16:21:59 +0100\r
+Message-Id: <1370359319-2140-1-git-send-email-markwalters1009@gmail.com>\r
+X-Mailer: git-send-email 1.7.9.1\r
+In-Reply-To: <1370292776-24535-1-git-send-email-markwalters1009@gmail.com>\r
+References: <1370292776-24535-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: Tue, 04 Jun 2013 15:22:16 -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
+The cli-arguments must be arguments in a whitelist of arguments: this\r
+adds a slight maintenance burden but means we don't have to deal with\r
+users who passed "--format=text" or other incompatible options.\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 sensible options (sort-order\r
+exclude limit and offset) work was worth the extra hassle.\r
+---\r
+\r
+This is a slight change to\r
+1370292776-24535-1-git-send-email-markwalters1009@gmail.com The change\r
+is that we add a whitelist of allowed cli options; other options are\r
+removed and the user is warned (but the query is not aborted).\r
+\r
+One other tiny change is that a query starting with "[[:blank:]]*-- "\r
+is allowed: everything after the -- is part of the real qeury so if\r
+any strange query is accidentally being misparsed the user can prefix\r
+with "-- " and it will give the current behaviour.\r
+\r
+Best wishes\r
+\r
+Mark\r
+\r
+\r
+ emacs/notmuch-hello.el | 5 +++--\r
+ emacs/notmuch-lib.el | 44 ++++++++++++++++++++++++++++++++++++++++++++\r
+ emacs/notmuch.el | 36 +++++++++++++++++++++++++-----------\r
+ 3 files changed, 72 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..65c489e 100644\r
+--- a/emacs/notmuch-lib.el\r
++++ b/emacs/notmuch-lib.el\r
+@@ -189,6 +189,50 @@ 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
++(defvar notmuch-parse-option-whitelist\r
++ '("^--sort=oldest-first$"\r
++ "^--sort=newest-first$"\r
++ "^--exclude=true$"\r
++ "^--exclude=false$"\r
++ "^--exclude=flag$"\r
++ "^--limit=[0-9]*$"\r
++ "^--offset=[0-9]*$"\r
++ "^--$"))\r
++\r
++(defun notmuch-parse-in-whitelist-p (arg)\r
++ (let ((allowed nil))\r
++ (dolist (opt notmuch-parse-option-whitelist nil)\r
++ (setq allowed (or allowed (string-match-p opt arg))))\r
++ allowed))\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 (or (string-match "^[[:blank:]]*--.*? -- " query)\r
++ (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
++ (dolist (arg args nil)\r
++ (unless (notmuch-parse-in-whitelist-p arg)\r
++ (setq args (delete arg args))\r
++ (message "Removing unknown option %s" arg)))\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