]> git.notmuchmail.org Git - notmuch/commitdiff
search-tags: Add support for search-terms.
authorJan Janak <jan@ryngle.com>
Wed, 25 Nov 2009 03:30:22 +0000 (04:30 +0100)
committerCarl Worth <cworth@cworth.org>
Thu, 26 Nov 2009 15:06:41 +0000 (07:06 -0800)
This patch adds support for search-terms to 'notmuch search-tags'. If
no search-term is provided then the command returns a list of all tags
from the database.

If the user provides one or more search-terms as arguments then the
command collects tags from matching messages only.

This could be used by functions in the Emacs mode to further limit the
list of tags offered for completion. For example, functions that remove
tags from message(s) could offer only tags present in the message(s).

Signed-off-by: Jan Janak <jan@ryngle.com>
notmuch-search-tags.c
notmuch.c

index 1201165067d2746ea0aaaba59c3b17efa99a012e..7a1305e2fed98368ae42995a1bf3f28fcaae997e 100644 (file)
 
 #include "notmuch-client.h"
 
-static int
-list_all_tags (notmuch_database_t* db)
+static void
+print_tags (notmuch_tags_t *tags)
 {
-    notmuch_tags_t* tags;
-    const char* t;
+    const char *t;
 
-    if ((tags = notmuch_database_get_all_tags (db)) == NULL) {
-       fprintf (stderr, "Error while obtaining tags from the database.\n");
-       return 1;
-    }
-
-    while((t = notmuch_tags_get (tags))) {
+    while ((t = notmuch_tags_get (tags))) {
        printf ("%s\n", t);
        notmuch_tags_advance (tags);
     }
-
-    notmuch_tags_destroy (tags);
-    return 0;
 }
 
 int
 notmuch_search_tags_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;
@@ -60,12 +57,42 @@ notmuch_search_tags_command (void *ctx, int argc, char *argv[])
        goto error;
     }
 
-    if (list_all_tags (db) != 0) goto error;
+    if (argc > 0) {
+       if ((query_str = query_string_from_args (ctx, argc, argv)) == 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 ((tags = notmuch_messages_collect_tags (msgs)) == NULL) goto error;
+    } else {
+       if ((tags = notmuch_database_get_all_tags (db)) == NULL) {
+           fprintf (stderr, "Error while getting tags from the database.\n");
+           goto error;
+       }
+    }
+
+    print_tags (tags);
 
+    notmuch_tags_destroy (tags);
+    if (query) 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;
 }
index c57bb5c5eef8cc47ff3f8dbf83d1f8b48c75c28f..5b0284cd9d731e882d6358ba5bc47af4b2364847 100644 (file)
--- a/notmuch.c
+++ b/notmuch.c
@@ -255,11 +255,14 @@ command_t commands[] = {
       "\t\t\"notmuch restore\" command provides you a way to import\n"
       "\t\tall of your tags (or labels as sup calls them)." },
     { "search-tags", notmuch_search_tags_command,
-      NULL,
-      "List all tags found in the database.",
-      "\t\tThis command returns an alphabetically sorted list of all tags\n"
-      "\t\tthat are present in the database. In other words, the resulting\n"
-      "\t\tlist is a collection of all tags from all messages." },
+      "[<search-terms> [...] ]",
+      "\t\tList all tags found in the database or matching messages.",
+      "\t\tRun this command without any search-term(s) to obtain a list\n"
+      "\t\tof all tags found in the database. If you provide one or more\n"
+      "\t\tsearch-terms as argument(s) then the resulting list will\n"
+      "\t\tcontain tags only from messages that match the search-term(s).\n"
+      "\n"
+      "\t\tIn both cases the list will be alphabetically sorted." },
     { "help", notmuch_help_command,
       "[<command>]",
       "\t\tThis message, or more detailed help for the named command.",