On Sat, 4 Jun 2011 11:32:15 -0400, Austin Clements <amdra...@mit.edu> wrote:
> Dynamic scoping is obnoxious, but I think programmed completion is
> steeped in the assumption that you'll use it.  This code would be much
> simpler if notmuch-query-completions took only `string' and used the
> dynamically-bound all-compls (which should probably be renamed
> notmuch-completions or something if you do this).  Then this could be
> just
>   (minibuffer-completion-table (completion-table-dynamic
> #'notmuch-query-completions)))
> and there'd be no need for quasiquoting, comments, and fake lexical scoping.

Sounds reasonable, I guess I really should stop fighting all those ugly
parts of elisp with unreadable constructs like that. I made it a global
variable though to avoid compilation warnings about notmuch-completion
being a free variable. Since it's contents are not dependent on
how/where notmuch-read-query is called, this shouldn't cause any
problems, except my personal discomfort arising from the use of side
effects for something as simple as this. :)

> > +    (define-key keymap (kbd "<tab>") 'minibuffer-complete)
> 
> This probably deserves a comment about why you're doing so much work
> to avoid completing-read (which I assume is because it also binds SPC,
> even if require-match is nil, which is unfortunate).

Yes, that was the reason.

Another thing that bugs me, is that I did not find a better way of doing
the completion: Ideally I'd like to just specify a list of completions
for individual words and have emacs handle separating the input string
into individual words, but I couldn't find any options to accomplish
that.

An updated patch is attached.
From 70642aecbf63428d9bcedc108c55f65574a792e7 Mon Sep 17 00:00:00 2001
From: Daniel Schoepe <daniel.scho...@googlemail.com>
Date: Sat, 4 Jun 2011 14:17:44 +0200
Subject: [PATCH] emacs: Tab completion for notmuch-search and
 notmuch-search-filter

This patch adds completion with <tab> in the minibuffer for
notmuch-search and notmuch-search-filter.
---
 emacs/notmuch.el |   35 +++++++++++++++++++++++++++++++++--
 1 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index 3311fe8..17c214c 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -72,6 +72,9 @@ For example:
   :type '(alist :key-type (string) :value-type (string))
   :group 'notmuch)
 
+(defvar notmuch-completions nil
+  "List of completions used in notmuch-query-completions")
+
 (defun notmuch-select-tag-with-completion (prompt &rest search-terms)
   (let ((tag-list
 	 (with-output-to-string
@@ -882,6 +885,34 @@ characters as well as `_.+-'.
 	   (concat "*notmuch-search-" query "*"))
 	  )))
 
+(defun notmuch-query-completions (string)
+  "Return possible completions for STRING."
+  (cond
+   ;; this ugly regexp is used to get the last word of the input
+   ;; possibly preceded by a '('
+   ((string-match "\\(^\\|.* (?\\)\\([^ ]*\\)$" string)
+    (mapcar (lambda (compl)
+	      (concat (match-string-no-properties 1 string) compl))
+	    (all-completions (match-string-no-properties 2 string)
+			     notmuch-completions)))
+   (t (list string))))
+
+(defun notmuch-read-query (prompt)
+  "Read a notmuch-query from the minibuffer with completion.
+
+PROMPT is the string to prompt with."
+  (let ((keymap (copy-keymap minibuffer-local-map))
+	(minibuffer-completion-table (completion-table-dynamic #'notmuch-query-completions)))
+    ;; this was simpler than convincing completing-read to accept spaces:
+    (define-key keymap (kbd "<tab>") 'minibuffer-complete)
+    (setq notmuch-completions
+	  (append (list "folder:" "thread:" "id:" "date:" "from:" "to:"
+			"subject:" "attachment:")
+		  (mapcar (lambda (tag)
+			    (concat "tag:" tag))
+			  (process-lines "notmuch" "search-tags"))))
+    (read-from-minibuffer prompt nil keymap nil minibuffer-history nil nil)))
+
 ;;;###autoload
 (defun notmuch-search (query &optional oldest-first target-thread target-line continuation)
   "Run \"notmuch search\" with the given query string and display results.
@@ -893,7 +924,7 @@ The optional parameters are used as follows:
                  current if it appears in the search results.
   target-line: The line number to move to if the target thread does not
                appear in the search results."
-  (interactive "sNotmuch search: ")
+  (interactive (notmuch-read-query "Notmuch search: "))
   (let ((buffer (get-buffer-create (notmuch-search-buffer-title query))))
     (switch-to-buffer buffer)
     (notmuch-search-mode)
@@ -991,7 +1022,7 @@ search."
 
 Runs a new search matching only messages that match both the
 current search results AND the additional query string provided."
-  (interactive "sFilter search: ")
+  (interactive (notmuch-read-query "Filter search: "))
   (let ((grouped-query (if (string-match-p notmuch-search-disjunctive-regexp query)
 			   (concat "( " query " )")
 			 query)))
-- 
1.7.5.3

Attachment: pgpZUqPb2jMu2.pgp
Description: PGP signature

_______________________________________________
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch

Reply via email to