[PATCH 2/2] WIP: treat notmuch tag without operations as query

Subject:[PATCH 2/2] WIP: treat notmuch tag without operations as query

Date:Thu, 30 Jun 2016 10:40:27 +0200

To:David Bremner ,notmuch@notmuchmail.org

Cc:

From:David Bremner


e.g.

% notmuch tag		# dump all tags
% notmuch tag subject:foo  # all tags of messages with foo in the subject
---
 notmuch-tag.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 52 insertions(+), 6 deletions(-)

diff --git a/notmuch-tag.c b/notmuch-tag.c
index 18d78dd..941c653 100644
--- a/notmuch-tag.c
+++ b/notmuch-tag.c
@@ -186,6 +186,50 @@ tag_file (void *ctx, notmuch_database_t *notmuch, tag_op_flag_t flags,
     return ret || warn;
 }
 
+static int
+_dump_tags (notmuch_config_t *config, const char *query_string)
+{
+    notmuch_database_t *notmuch;
+    notmuch_tags_t *tags;
+    const char *tag;
+    char *status_string;
+
+    notmuch_query_t *query;
+
+    if (notmuch_database_open_verbose (notmuch_config_get_database_path (config),
+				       NOTMUCH_DATABASE_MODE_READ_ONLY,
+				       &notmuch, &status_string)) {
+	    if (status_string) {
+		fputs (status_string, stderr);
+		free (status_string);
+	    }
+	    return EXIT_FAILURE;
+	}
+
+    query = notmuch_query_create (notmuch, query_string);
+    if (query == NULL) {
+	fprintf (stderr, "Out of memory\n");
+	return EXIT_FAILURE;
+    }
+
+    if (print_status_query ("notmuch tag",
+			    query,
+			    notmuch_query_search_tags (query, &tags)))
+	return 1;
+
+    for (;
+	 notmuch_tags_valid (tags);
+	 notmuch_tags_move_to_next (tags))
+    {
+	tag = notmuch_tags_get (tags);
+
+	printf ("%s\n", tag);
+    }
+
+    notmuch_tags_destroy (tags);
+    return EXIT_SUCCESS;
+}
+
 int
 notmuch_tag_command (notmuch_config_t *config, int argc, char *argv[])
 {
@@ -196,6 +240,7 @@ notmuch_tag_command (notmuch_config_t *config, int argc, char *argv[])
     tag_op_flag_t tag_flags = TAG_FLAG_NONE;
     notmuch_bool_t batch = FALSE;
     notmuch_bool_t remove_all = FALSE;
+    notmuch_bool_t with_ops = FALSE;
     FILE *input = stdin;
     char *input_file_name = NULL;
     int opt_index;
@@ -248,17 +293,18 @@ notmuch_tag_command (notmuch_config_t *config, int argc, char *argv[])
 				    &query_string, tag_ops))
 	    return EXIT_FAILURE;
 
-	if (tag_op_list_size (tag_ops) == 0 && ! remove_all) {
-	    fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");
-	    return EXIT_FAILURE;
-	}
+	if (tag_op_list_size (tag_ops) == 0 && ! remove_all)
+	    with_ops = FALSE;
 
-	if (*query_string == '\0') {
-	    fprintf (stderr, "Error: notmuch tag requires at least one search term.\n");
+	if (with_ops && *query_string == '\0') {
+	    fprintf (stderr, "Error: notmuch tag with operations requires at least one search term.\n");
 	    return EXIT_FAILURE;
 	}
     }
 
+    if (! with_ops)
+	return _dump_tags (config, query_string);
+
     if (notmuch_database_open (notmuch_config_get_database_path (config),
 			       NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
 	return EXIT_FAILURE;
-- 
2.8.1


Thread: