On Sat, 4 Jun 2011 17:55:24 -0400, Austin Clements <amdra...@mit.edu> wrote:
> Oh, sorry, I wasn't suggesting setq'ing a global.  I agree that that's
> really ugly.  Rather, something like
> (defun notmuch-query-completions (string)
>   ... as you have it now ...)
> (defun notmuch-read-query (prompt)
>   (let ((notmuch-completions (append (list "folder:" ...)))
>         (keymap ...)
>         (minibuffer-completion-table ...))
>     (read-from-minibuffer ...)))
> Unfortunately, you still need the global defvar to avoid compilation
> warnings, but this at least avoids the side-effects, and is probably
> how programmed completion was intended to be used.

Both alternatives seem about equally ugly to me, since the one using
dynamic scoping still uses side-effects, because it still passes the
completion information around without using it as an argument to
notmuch-query-completions. At least defvar-ing a global variable and
then never actually using it, seems somewhat unclean as well.

> Alternatively, here's a completely different way to structure this
> that avoids globals and dynamic scoping entirely.  This is how some of
> the standard completing read functions appear to work:

Ah right, I forgot that using macros from cl is fine even in library

From 7768f41ac44213c5e2c1dc3b0f13e3edf1d97a26 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

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

diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index 3311fe8..33c34bd 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
@@ -882,6 +885,35 @@ characters as well as `_.+-'.
 	   (concat "*notmuch-search-" query "*"))
+(defun notmuch-read-query (prompt)
+  "Read a notmuch-query from the minibuffer with completion.
+PROMPT is the string to prompt with."
+  (lexical-let
+      ((completions 
+	(append (list "folder:" "thread:" "id:" "date:" "from:" "to:"
+		      "subject:" "attachment:")
+		(mapcar (lambda (tag)
+			  (concat "tag:" tag))
+			(process-lines "notmuch" "search-tags")))))
+    (let ((keymap (copy-keymap minibuffer-local-map))
+	  (minibuffer-completion-table
+	   (completion-table-dynamic
+	    (lambda (string)
+	      ;; generate a list of possible completions for the current input
+	      (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)
+					 completions)))
+	       (t (list string)))))))
+      ;; this was simpler than convincing completing-read to accept spaces:
+      (define-key keymap (kbd "<tab>") 'minibuffer-complete)
+      (read-from-minibuffer prompt nil keymap nil minibuffer-history nil nil))))
 (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 +925,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 (list (notmuch-read-query "Notmuch search: ")))
   (let ((buffer (get-buffer-create (notmuch-search-buffer-title query))))
     (switch-to-buffer buffer)
@@ -991,7 +1023,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 (list (notmuch-read-query "Filter search: ")))
   (let ((grouped-query (if (string-match-p notmuch-search-disjunctive-regexp query)
 			   (concat "( " query " )")

Attachment: pgpjYvXqoOxMf.pgp
Description: PGP signature

notmuch mailing list

Reply via email to