--- /dev/null
+Return-Path: <m.walters@qmul.ac.uk>\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 B3911431FB6\r
+ for <notmuch@notmuchmail.org>; Tue, 4 Jun 2013 14:12:30 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 1.401\r
+X-Spam-Level: *\r
+X-Spam-Status: No, score=1.401 tagged_above=-999 required=5\r
+ tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001,\r
+ FREEMAIL_REPLY=2.499, NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_MED=-2.3]\r
+ 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 I7Me27Q0KajJ for <notmuch@notmuchmail.org>;\r
+ Tue, 4 Jun 2013 14:12:23 -0700 (PDT)\r
+Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6])\r
+ (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id F351C431FAE\r
+ for <notmuch@notmuchmail.org>; Tue, 4 Jun 2013 14:12:22 -0700 (PDT)\r
+Received: from smtp.qmul.ac.uk ([138.37.6.40])\r
+ by mail2.qmul.ac.uk with esmtp (Exim 4.71)\r
+ (envelope-from <m.walters@qmul.ac.uk>)\r
+ id 1UjyWc-0003Ff-Nt; Tue, 04 Jun 2013 22:12:17 +0100\r
+Received: from 93-97-24-31.zone5.bethere.co.uk ([93.97.24.31] helo=localhost)\r
+ by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.71)\r
+ (envelope-from <m.walters@qmul.ac.uk>)\r
+ id 1UjyWc-0003O5-2J; Tue, 04 Jun 2013 22:12:14 +0100\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: Tomi Ollila <tomi.ollila@iki.fi>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH] emacs: search: allow command line args as part of query\r
+In-Reply-To: <m28v2p7jg9.fsf@guru.guru-group.fi>\r
+References: <1370292776-24535-1-git-send-email-markwalters1009@gmail.com>\r
+ <1370359319-2140-1-git-send-email-markwalters1009@gmail.com>\r
+ <m28v2p7jg9.fsf@guru.guru-group.fi>\r
+User-Agent: Notmuch/0.14+255~gff3cc55 (http://notmuchmail.org) Emacs/23.4.1\r
+ (x86_64-pc-linux-gnu)\r
+Date: Tue, 04 Jun 2013 22:12:12 +0100\r
+Message-ID: <87bo7loa1f.fsf@qmul.ac.uk>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+X-Sender-Host-Address: 93.97.24.31\r
+X-QM-SPAM-Info: Sender has good ham record. :)\r
+X-QM-Body-MD5: 1a032ec29fed26dbff244980b766a925 (of first 20000 bytes)\r
+X-SpamAssassin-Score: 0.6\r
+X-SpamAssassin-SpamBar: /\r
+X-SpamAssassin-Report: The QM spam filters have analysed this message to\r
+ determine if it is\r
+ spam. We require at least 5.0 points to mark a message as spam.\r
+ This message scored 0.6 points. Summary of the scoring: \r
+ * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail\r
+ provider * (markwalters1009[at]gmail.com)\r
+ * 1.0 FREEMAIL_REPLY From and body contain different freemails\r
+ * -0.4 AWL AWL: From: address is in the auto white-list\r
+X-QM-Scan-Virus: ClamAV says the message is clean\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 21:12:30 -0000\r
+\r
+\r
+\r
+> On Tue, Jun 04 2013, Mark Walters <markwalters1009@gmail.com> wrote:\r
+>\r
+>> This allows command line arguments for notmuch-search to be part of\r
+>> the query-string. \r
+>\r
+> Nice feature -- some comments below:\r
+>\r
+\r
+Thanks for the review! replies below\r
+\r
+>> The string must be of the form\r
+>> [:blank:]*--cli-arguments -- query. I hope this doesn't clash with\r
+>\r
+> One problem requiring trailing '--' noticed after reviewed -- if one\r
+> forgets that the whole string is going to be query string.\r
+\r
+One easy fix would be to require any query starting with --something to\r
+be prefixed by "-- ". We could also just assume the query starts if we\r
+see a term not prefixed with -- or having a "--" term. What do you think?\r
+\r
+>> xapian: I believe that queries shouldn't start with a "-".\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
+> If one gave --sort=oldest-first --sort=newest-first oldest-first is\r
+> chosen instead of newest-first as both are removed from args -- that\r
+> should be pretty easy to fix by putting deletes inside whens.\r
+\r
+I think this one is ok as the delete is outside the dolist.\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
+> is zero numbers of numbers for --limit & --offset good ([0-9]*) ?\r
+\r
+You are quite right (ie + would be better).\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
+>\r
+> Should the whitespace matching in locations above be consistent:\r
+> like "^[[:blank:]]*--.*?[[:blank:]]--[[:blank:]]". Blank matches\r
+> space & tab (according to http://www.gnu.org/software/emacs/manual/html_node/elisp/Char-Classes.html#Char-Classes )\r
+>\r
+> Hmm, learned a bit:\r
+>\r
+> (split-string " foo bar ") -> ("foo" "bar")\r
+> (split-string " foo bar " " ") -> ("" "" "foo" "" "" "bar" "" "")\r
+> (split-string " foo bar " " " t) -> ("foo" "bar")\r
+> (split-string " foo bar " " +") -> ("" "foo" "bar" "")\r
+> (split-string " foo bar " " +" t) -> ("foo" "bar")\r
+>\r
+> -> ... (args (split-string (match-string 0 query) "[[:blank:]]" t)))\r
+\r
+These are both clear improvements.\r
+\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
+>\r
+> And finally, I'd suggest it is an error to encounter unknown\r
+> option and in that case operation is aborted.\r
+\r
+I think this would be better. I am not quite sure how to implement it,\r
+and I wasn't sure what notmuch-hello should do when displaying counts of\r
+an "illegal" saved search. What do you think?\r
+\r
+Best wishes\r
+\r
+Mark\r
+\r
+\r
+\r
+\r
+\r
+>\r
+> Tomi\r
+>\r
+>\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
+>> _______________________________________________\r
+>> notmuch mailing list\r
+>> notmuch@notmuchmail.org\r
+>> http://notmuchmail.org/mailman/listinfo/notmuch\r