unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* First attempt to add smart completion in notmuch-search
@ 2009-12-18 15:00 racin
  2009-12-18 17:54 ` Carl Worth
  0 siblings, 1 reply; 6+ messages in thread
From: racin @ 2009-12-18 15:00 UTC (permalink / raw
  To: notmuch



Hi!

Here is a first attempt to add "smart completion" to notmuch-search.

What it does is that:

 - It tries to detects when you hit tab to complete a search term
   prefix (like to, from, subject...) , and if so completes your
   prefix;

 - If you try to complete a search term usign a specific prefix (like
   to), it helps you complete it using prefix founds in the messages
   matching the current search string.

The patch has a Emacs-lisp part and a C part.

I'm submitting the patch to know what people think of it, but there
are some things still uncomplete:

 - I think headers should be parsed so that a better output is
   proposed, but I don't know which one yet;

 - There may be performance issues (because fetching headers require
   looking them in each message file), but I'm not sure if this is
   really a problem (since we already fetch headers to display it in
   the summary buffer).

 - I have a problem that notmuch-search-query-string is buffer-local
   and I can't access it from the minibuffer, which I don't know how
   to solve properly

 - There are still some glitches with the "".

 - There may be many other improvements that I haven't thought of.

Anyway, I hope it's useful; I'll continue working on it when I come
back from Holidays.

Thanks for your input,
 Matthieu

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: First attempt to add smart completion in notmuch-search
  2009-12-18 15:00 First attempt to add smart completion in notmuch-search racin
@ 2009-12-18 17:54 ` Carl Worth
  2009-12-24 10:06   ` racin
  2009-12-24 10:07   ` racin
  0 siblings, 2 replies; 6+ messages in thread
From: Carl Worth @ 2009-12-18 17:54 UTC (permalink / raw
  To: racin, notmuch

[-- Attachment #1: Type: text/plain, Size: 235 bytes --]

On Fri, 18 Dec 2009 16:00:49 +0100, racin@free.fr wrote:
> Here is a first attempt to add "smart completion" to notmuch-search.

Hi Matthieu,

This all sounds quite interesting!

I look forward to actually seeing the patch. ;-)

-Carl

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: First attempt to add smart completion in notmuch-search
  2009-12-18 17:54 ` Carl Worth
@ 2009-12-24 10:06   ` racin
  2009-12-24 10:07   ` racin
  1 sibling, 0 replies; 6+ messages in thread
From: racin @ 2009-12-24 10:06 UTC (permalink / raw
  To: Carl Worth; +Cc: notmuch

Hum, sure...

Matthieu

Quoting Carl Worth <cworth@cworth.org>:

> On Fri, 18 Dec 2009 16:00:49 +0100, racin@free.fr wrote:
> > Here is a first attempt to add "smart completion" to notmuch-search.
>
> Hi Matthieu,
>
> This all sounds quite interesting!
>
> I look forward to actually seeing the patch. ;-)
>
> -Carl
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: First attempt to add smart completion in notmuch-search
  2009-12-18 17:54 ` Carl Worth
  2009-12-24 10:06   ` racin
@ 2009-12-24 10:07   ` racin
  2009-12-26 16:19     ` Kan-Ru Chen
  1 sibling, 1 reply; 6+ messages in thread
From: racin @ 2009-12-24 10:07 UTC (permalink / raw
  To: Carl Worth; +Cc: notmuch

[-- Attachment #1: Type: text/plain, Size: 380 bytes --]

OK, I had a problem using this #!#)($! webmail client. Here's the patch.

Matthieu

Quoting Carl Worth <cworth@cworth.org>:

> On Fri, 18 Dec 2009 16:00:49 +0100, racin@free.fr wrote:
> > Here is a first attempt to add "smart completion" to notmuch-search.
>
> Hi Matthieu,
>
> This all sounds quite interesting!
>
> I look forward to actually seeing the patch. ;-)
>
> -Carl
>



[-- Attachment #2: patch --]
[-- Type: application/octet-stream, Size: 6359 bytes --]

diff --git a/notmuch-client.h b/notmuch-client.h
index 50a30fe..eb5572f 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -123,6 +123,9 @@ notmuch_tag_command (void *ctx, int argc, char *argv[]);
 int
 notmuch_search_tags_command (void *ctx, int argc, char *argv[]);
 
+int
+notmuch_search_headers_command (void *ctx, int argc, char *argv[]);
+
 const char *
 notmuch_time_relative_date (const void *ctx, time_t then);
 
diff --git a/notmuch-search-tags.c b/notmuch-search-tags.c
index 7a1305e..32e5afc 100644
--- a/notmuch-search-tags.c
+++ b/notmuch-search-tags.c
@@ -96,3 +96,99 @@ error:
     if (db) notmuch_database_close (db);
     return 1;
 }
+
+/* TODO: only take headers that match regexp? */
+/* TODO: limit the max number of queries? */
+void *
+notmuch_messages_print_headers (notmuch_messages_t *messages, const char *header)
+{
+    notmuch_tags_t *tags, *msg_tags;
+    notmuch_message_t *msg;
+    GHashTable *htable;
+    GList *keys, *l;
+    const char *tag;
+    const char *msg_header;
+
+
+    htable = g_hash_table_new_full (g_str_hash, g_str_equal, free, NULL);
+
+    while ((msg = notmuch_messages_get (messages))) {
+      msg_header = notmuch_message_get_header (msg, header);
+      g_hash_table_insert (htable, xstrdup (msg_header), NULL);
+      notmuch_message_destroy (msg);
+      notmuch_messages_advance (messages);
+    }
+
+    keys = g_hash_table_get_keys (htable);
+
+    /* TODO: sort by alphabetical order? */
+    for (l = keys; l; l = l->next) {
+      printf("%s\n", (char *)l->data);
+    }
+
+    g_list_free (keys);
+    g_hash_table_destroy (htable);
+}
+
+int
+notmuch_search_headers_command (void *ctx, int argc, char *argv[])
+{
+    notmuch_messages_t *msgs;
+    notmuch_tags_t *tags;
+    notmuch_config_t *config;
+    notmuch_database_t *db;
+    notmuch_query_t *query;
+    char *query_str;
+
+    tags = NULL;
+    config = NULL;
+    db = NULL;
+    query = NULL;
+
+    if ((config = notmuch_config_open (ctx, NULL, NULL)) == NULL) {
+	goto error;
+    }
+
+    db = notmuch_database_open (notmuch_config_get_database_path (config),
+				NOTMUCH_DATABASE_MODE_READ_ONLY);
+    if (db == NULL) {
+	goto error;
+    }
+
+    if (argc < 3) {
+         fprintf( stderr, "Wrong number of arguments\n");
+         fprintf( stderr, "Arguments: <header> <regexp> <query>\n");
+	 goto error;
+    }
+
+    const char *header = argv[0];
+    const char *regexp = argv[1]; // Ignored for now.
+
+    if(( query_str = query_string_from_args (ctx, argc-2, &argv[2])) == NULL) {
+      fprintf (stderr, "Out of memory.\n");
+      goto error;
+    }
+
+    if (*query_str == '\0') {
+      fprintf (stderr, "Error: Invalid search string.\n");
+      goto error;
+    }
+
+    if ((query = notmuch_query_create (db, query_str)) == NULL) {
+      fprintf (stderr, "Out of memory\n");
+      goto error;
+    }
+
+    msgs = notmuch_query_search_messages (query);
+    if ((notmuch_messages_print_headers (msgs, header)) == NULL) goto error;
+
+    notmuch_query_destroy (query);
+    notmuch_database_close (db);
+    return 0;
+
+error:
+    if (tags) notmuch_tags_destroy (tags);
+    if (query) notmuch_query_destroy (query);
+    if (db) notmuch_database_close (db);
+    return 1;
+}
diff --git a/notmuch.c b/notmuch.c
index 2ac8a59..0921906 100644
--- a/notmuch.c
+++ b/notmuch.c
@@ -284,6 +284,11 @@ command_t commands[] = {
       "\t\tcontain tags only from messages that match the search-term(s).\n"
       "\n"
       "\t\tIn both cases the list will be alphabetically sorted." },
+    { "search-headers", notmuch_search_headers_command,
+      "search-headers <header> <regexp> <search-terms> [...]",
+      "\t\tList all headers <header>  matching <regexp> in messages"
+      " matching <search-terms>"
+    },
     { "help", notmuch_help_command,
       "[<command>]",
       "\t\tThis message, or more detailed help for the named command.",
diff --git a/notmuch.el b/notmuch.el
index 97914f2..8d9a3f9 100644
--- a/notmuch.el
+++ b/notmuch.el
@@ -1286,7 +1286,8 @@ characters as well as `_.+-'.
 ;;;###autoload
 (defun notmuch-search (query &optional oldest-first)
   "Run \"notmuch search\" with the given query string and display results."
-  (interactive "sNotmuch search: ")
+  (interactive (list (completing-read "Notmuch-search: "
+				      'notmuch-search-compute-completion-function)))
   (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
     (switch-to-buffer buffer)
     (notmuch-search-mode)
@@ -1494,3 +1495,46 @@ Currently available key bindings:
       (goto-line n))))
 
 (provide 'notmuch)
+
+(defun notmuch-search-compute-completion-function (string predicate flag)
+  "Smart completion for notmuch. "
+  (let ((to-complete)
+	(prefix)
+	(completion-list))
+  ;; First case: complete a header
+    (cond ((string-match "\\(.*\\<\\)\\(\\w+\\):\\(\".*?\\|\\w*?\\)$" string)
+	   (setq header (match-string 2 string))
+	   (setq prefix (concat  (match-string 1 string) (match-string 2 string) ":"))
+	   (setq to-complete (match-string 3 string))
+	   (setq completion-list
+		 (split-string 
+		  (with-output-to-string
+		    (with-current-buffer standard-output
+		      ;; XXX: We should add to the query string the what's already
+		      ;; in the minibuffer; this requires a more complex parser
+		      ;; to handle incomplete parenthesis, etc.
+		      (apply 'call-process notmuch-command nil t nil 
+			     "search-headers" header "dummy" 
+			     ;(list notmuch-search-query-string)
+			     ; XXX: notmuch-search-query-string is not accessible
+			     ; from the minibuffer
+			     (list "tag:inbox")
+			     )))
+		  "\n+" t))
+	   )
+  ;; Second case: complete a prefix
+	  ((string-match "\\(.*\\<\\)\\(\\w*\\)$" string)
+	   (setq to-complete (match-string 2 string))
+	   (setq completion-list (list "from:" "to:" "subject:\"" "attachment:\"" 
+				       "tag:" "id:" "and " "or "))
+	   (setq prefix (match-string 1 string))
+	   )
+  ;; Nothing to complete.	  
+	  (t (setq to-complete "")
+	     (setq completion-list nil)
+	     (setq prefix string)))
+    (case flag
+      (nil (concat prefix (try-completion to-complete completion-list)))
+      ('t (mapcar (lambda (string) (concat prefix string))
+		  (all-completions to-complete completion-list)))
+      ('lambda (test-completion to-complete completion-list)))))

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: First attempt to add smart completion in notmuch-search
  2009-12-24 10:07   ` racin
@ 2009-12-26 16:19     ` Kan-Ru Chen
  2009-12-27 18:37       ` racin
  0 siblings, 1 reply; 6+ messages in thread
From: Kan-Ru Chen @ 2009-12-26 16:19 UTC (permalink / raw
  To: racin, Carl Worth; +Cc: notmuch

[-- Attachment #1: Type: text/plain, Size: 471 bytes --]

On Thu, 24 Dec 2009 11:07:16 +0100, racin@free.fr wrote:
> OK, I had a problem using this #!#)($! webmail client. Here's the patch.
> 
> Matthieu

Is completion of headers useful right now? AFAIK, notmuch does not index
headers other than [subject, from, to].

Completion of keywords is still great. Thanks for this patch :)

           Kanru

-- 
Kan-Ru Chen | http://kanru.info

Q: Why are my replies five sentences or less?
A: http://five.sentenc.es/

[-- Attachment #2: Type: application/pgp-signature, Size: 835 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: First attempt to add smart completion in notmuch-search
  2009-12-26 16:19     ` Kan-Ru Chen
@ 2009-12-27 18:37       ` racin
  0 siblings, 0 replies; 6+ messages in thread
From: racin @ 2009-12-27 18:37 UTC (permalink / raw
  To: Kan-Ru Chen; +Cc: notmuch

Well, then it's useful for [subject, from, to], right? :)
In the future, other prefix terms, like tags, could also be completed this way.

Matthieu

Quoting Kan-Ru Chen <kanru@kanru.info>:

> On Thu, 24 Dec 2009 11:07:16 +0100, racin@free.fr wrote:
> > OK, I had a problem using this #!#)($! webmail client. Here's the patch.
> >
> > Matthieu
>
> Is completion of headers useful right now? AFAIK, notmuch does not index
> headers other than [subject, from, to].
>
> Completion of keywords is still great. Thanks for this patch :)
>
>            Kanru
>
> --
> Kan-Ru Chen | http://kanru.info
>
> Q: Why are my replies five sentences or less?
> A: http://five.sentenc.es/
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2009-12-27 18:37 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-12-18 15:00 First attempt to add smart completion in notmuch-search racin
2009-12-18 17:54 ` Carl Worth
2009-12-24 10:06   ` racin
2009-12-24 10:07   ` racin
2009-12-26 16:19     ` Kan-Ru Chen
2009-12-27 18:37       ` racin

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).