--- /dev/null
+Return-Path: <tomi.ollila@iki.fi>\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 13AFF431FAF\r
+ for <notmuch@notmuchmail.org>; Tue, 4 Jun 2013 12:41:11 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\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 FDEIQQ8cywXP for <notmuch@notmuchmail.org>;\r
+ Tue, 4 Jun 2013 12:40:59 -0700 (PDT)\r
+Received: from guru.guru-group.fi (guru.guru-group.fi [46.183.73.34])\r
+ by olra.theworths.org (Postfix) with ESMTP id B2045431FAE\r
+ for <notmuch@notmuchmail.org>; Tue, 4 Jun 2013 12:40:58 -0700 (PDT)\r
+Received: from guru.guru-group.fi (localhost [IPv6:::1])\r
+ by guru.guru-group.fi (Postfix) with ESMTP id 75800100030;\r
+ Tue, 4 Jun 2013 22:40:54 +0300 (EEST)\r
+From: Tomi Ollila <tomi.ollila@iki.fi>\r
+To: Mark Walters <markwalters1009@gmail.com>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH] emacs: search: allow command line args as part of query\r
+In-Reply-To: <1370359319-2140-1-git-send-email-markwalters1009@gmail.com>\r
+References: <1370292776-24535-1-git-send-email-markwalters1009@gmail.com>\r
+ <1370359319-2140-1-git-send-email-markwalters1009@gmail.com>\r
+User-Agent: Notmuch/0.15.2+172~g0a61aef (http://notmuchmail.org) Emacs/24.3.1\r
+ (x86_64-unknown-linux-gnu)\r
+X-Face: HhBM'cA~<r"^Xv\KRN0P{vn'Y"Kd;zg_y3S[4)KSN~s?O\"QPoL\r
+ $[Xv_BD:i/F$WiEWax}R(MPS`^UaptOGD`*/=@\1lKoVa9tnrg0TW?"r7aRtgk[F\r
+ !)g;OY^,BjTbr)Np:%c_o'jj,Z\r
+Date: Tue, 04 Jun 2013 22:40:54 +0300\r
+Message-ID: <m28v2p7jg9.fsf@guru.guru-group.fi>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain\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 19:41:11 -0000\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
+> 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
+> 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
+> +(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
+> +(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
+> + (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
+\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