Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 16D1C431FBF for ; Tue, 24 Nov 2009 19:30:32 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id MZVYX--60eKu for ; Tue, 24 Nov 2009 19:30:31 -0800 (PST) Received: from mail-bw0-f224.google.com (mail-bw0-f224.google.com [209.85.218.224]) by olra.theworths.org (Postfix) with ESMTP id 43124431FAE for ; Tue, 24 Nov 2009 19:30:31 -0800 (PST) Received: by bwz24 with SMTP id 24so5548847bwz.30 for ; Tue, 24 Nov 2009 19:30:30 -0800 (PST) Received: by 10.204.157.24 with SMTP id z24mr6964174bkw.208.1259119830174; Tue, 24 Nov 2009 19:30:30 -0800 (PST) Received: from x61s.janakj (r2c34.net.upc.cz [62.245.66.34]) by mx.google.com with ESMTPS id 16sm1608458bwz.15.2009.11.24.19.30.29 (version=TLSv1/SSLv3 cipher=RC4-MD5); Tue, 24 Nov 2009 19:30:29 -0800 (PST) Received: by x61s.janakj (Postfix, from userid 1000) id DB9D7440656; Wed, 25 Nov 2009 04:30:22 +0100 (CET) From: Jan Janak To: notmuch@notmuchmail.org Date: Wed, 25 Nov 2009 04:30:22 +0100 Message-Id: <1259119822-22593-2-git-send-email-jan@ryngle.com> X-Mailer: git-send-email 1.6.3.3 In-Reply-To: <1259119822-22593-1-git-send-email-jan@ryngle.com> References: <1258935056-9746-2-git-send-email-jan@ryngle.com> <1259119822-22593-1-git-send-email-jan@ryngle.com> Subject: [notmuch] [PATCH 2/2] search-tags: Add support for search-terms. X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 25 Nov 2009 03:30:32 -0000 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 --- 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." }, + "[ [...] ]", + "\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, "[]", "\t\tThis message, or more detailed help for the named command.", -- 1.6.3.3