unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Jan Janak <jan@ryngle.com>
To: notmuch@notmuchmail.org
Subject: [PATCH 2/2] search-tags: Add support for search-terms.
Date: Wed, 25 Nov 2009 04:30:22 +0100	[thread overview]
Message-ID: <1259119822-22593-2-git-send-email-jan@ryngle.com> (raw)
In-Reply-To: <1259119822-22593-1-git-send-email-jan@ryngle.com>

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 |   55 ++++++++++++++++++++++++++++++++++++------------
 notmuch.c             |   13 +++++++----
 2 files changed, 49 insertions(+), 19 deletions(-)

diff --git a/notmuch-search-tags.c b/notmuch-search-tags.c
index 1201165..7a1305e 100644
--- a/notmuch-search-tags.c
+++ b/notmuch-search-tags.c
@@ -21,34 +21,31 @@
 
 #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;
 }
diff --git a/notmuch.c b/notmuch.c
index c57bb5c..5b0284c 100644
--- 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.",
-- 
1.6.3.3

      reply	other threads:[~2009-11-25  3:30 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-23  0:10 [PATCH 1/3] notmuch: New function to retrieve all tags from the database Jan Janak
2009-11-23  0:10 ` [PATCH 2/3] notmuch: New command 'search-tags' Jan Janak
2009-11-23  0:10   ` [PATCH 3/3] notmuch.el: Select tag names with completion Jan Janak
2009-11-25  4:05     ` [PATCH] notmuch.el: When removing tags, offer only those a msg/thread has set Jan Janak
2009-11-26 15:15     ` [PATCH 3/3] notmuch.el: Select tag names with completion Carl Worth
2009-11-27 11:30       ` Jan Janak
2009-11-28  4:10         ` Carl Worth
2009-11-25  3:30   ` [PATCH 1/2] lib: New function to collect tags from a list of messages Jan Janak
2009-11-25  3:30     ` Jan Janak [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1259119822-22593-2-git-send-email-jan@ryngle.com \
    --to=jan@ryngle.com \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).