[RFC PATCH] emacs: search: allow command line args as part of query

Mark Walters markwalters1009 at gmail.com
Mon Jun 3 02:42:06 PDT 2013


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 an attempt to achieve the same as
id:1349209083-7170-1-git-send-email-jani at nikula.org : that is allow a
saved search to specify the query sort order. However, this is a
rather more complete solution (note as this might be viewed a hack
minimal may be *better* than complete): it works for any search (saved
or from "s") and lets other options (exclude etc) work. What do people
think?

Best wishes

Mark

 emacs/notmuch.el |   31 ++++++++++++++++++++++++++-----
 1 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index 7994d74..26ba7e7 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-full-query)
 (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-full-query)
   (make-local-variable 'notmuch-search-oldest-first)
   (make-local-variable 'notmuch-search-target-thread)
   (make-local-variable 'notmuch-search-target-line)
@@ -896,6 +898,26 @@ PROMPT is the string to prompt with."
 	(read-from-minibuffer prompt nil keymap nil
 			      'notmuch-search-history nil nil)))))
 
+(defun notmuch-search-parse-query (query oldest-first)
+  (setq notmuch-search-oldest-first oldest-first)
+  (if (string-match "^[:blank:]*--.*? -- " query)
+      (let ((actual-query (substring query (match-end 0)))
+	    (args (split-string (match-string 0 query) " " t)))
+	(setq notmuch-search-query-string actual-query)
+	(dolist (arg args nil)
+	  (when (equal arg "--sort=oldest-first")
+	    (setq notmuch-search-oldest-first t)
+	    (message "oldest first found"))
+	  (when (equal arg "--sort=newest-first")
+	    (setq notmuch-search-oldest-first nil)
+	    (message "newest first found")))
+	(setq args (delete "--sort=oldest-first" args))
+	(setq args (delete "--sort=newest-first" args))
+	(setq notmuch-search-full-query (append args (list actual-query))))
+    ;; no special arguments
+    (setq notmuch-search-full-query (list query))
+    (setq notmuch-search-query-string query)))
+
 ;;;###autoload
 (defun notmuch-search (&optional query oldest-first target-thread target-line continuation)
   "Run \"notmuch search\" with the given `query' and display results.
@@ -915,8 +937,7 @@ Other optional parameters are used as follows:
     (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-oldest-first oldest-first)
+    (notmuch-search-parse-query query oldest-first)
     (set 'notmuch-search-target-thread target-thread)
     (set 'notmuch-search-target-line target-line)
     (set 'notmuch-search-continuation continuation)
@@ -928,13 +949,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))
+		     notmuch-search-full-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.
-- 
1.7.9.1



More information about the notmuch mailing list