[PATCH] notmuch new --new-tags=tags...

Anthony Towns aj at erisian.com.au
Sat Apr 10 08:03:04 PDT 2010


Hi *,

The attached patch makes "notmuch new --new-tags=unread,new" set the
"unread" and "new" tags on any new mail it finds rather than "unread"
and "inbox". Or whatever other tags you happen to specify.

Signed-off-by: Anthony Towns <aj at erisian.com.au>
---
 NEWS          |    3 +++
 notmuch-new.c |   49 +++++++++++++++++++++++++++++++++++++++++++++----
 notmuch.1     |   19 ++++++++++++++++++-
 notmuch.c     |    7 ++++++-
 4 files changed, 72 insertions(+), 6 deletions(-)

diff --git a/NEWS b/NEWS
index f29ac27..cbfae5a 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,6 @@
+The "notmuch new" command accepts a "--new-tags=tags..." option to override
+the default tags applied to new mail.
+
 Notmuch 0.1 (2010-04-05)
 ========================
 This is the first release of the notmuch mail system.
diff --git a/notmuch-new.c b/notmuch-new.c
index 44b50aa..77c99e0 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -32,6 +32,11 @@ typedef struct _filename_list {
     _filename_node_t **tail;
 } _filename_list_t;

+typedef struct _tag_list {
+    char *tag;
+    struct _tag_list *next;
+} _tag_list_t;
+
 typedef struct {
     int output_is_a_tty;
     int verbose;
@@ -41,6 +46,8 @@ typedef struct {
     int added_messages;
     struct timeval tv_start;

+    _tag_list_t *new_msg_tags;
+
     _filename_list_t *removed_files;
     _filename_list_t *removed_directories;
 } add_files_state_t;
@@ -93,11 +100,40 @@ _filename_list_add (_filename_list_t *list,
     list->tail = &node->next;
 }

+static _tag_list_t *
+_parse_tags (void *ctx, const char *orig_str)
+{
+    _tag_list_t *tag_head = NULL, **tag_tail = &tag_head;
+    char *dupe_str, *start, *comma;
+
+    dupe_str = talloc_strdup(ctx, orig_str);
+    start = dupe_str;
+
+    do {
+	comma = strchr(start, ',');
+	if (comma)
+	    *(comma++) = '\0';
+
+	if (*start != '\0') {
+	    *tag_tail = talloc(dupe_str, _tag_list_t);
+	    (*tag_tail)->tag = start;
+	    (*tag_tail)->next = NULL;
+	    tag_tail = &(*tag_tail)->next;
+ 	}
+
+	start = comma;
+    } while (start);
+
+    return tag_head;
+}
+
 static void
-tag_inbox_and_unread (notmuch_message_t *message)
+tag_new_message (add_files_state_t *state, notmuch_message_t *message)
 {
-    notmuch_message_add_tag (message, "inbox");
-    notmuch_message_add_tag (message, "unread");
+    _tag_list_t *cur;
+    for (cur = state->new_msg_tags; cur; cur = cur->next) {
+	notmuch_message_add_tag (message, cur->tag);
+    }
 }

 static void
@@ -412,7 +448,7 @@ add_files_recursive (notmuch_database_t *notmuch,
 	/* success */
 	case NOTMUCH_STATUS_SUCCESS:
 	    state->added_messages++;
-	    tag_inbox_and_unread (message);
+	    tag_new_message (state, message);
 	    break;
 	/* Non-fatal issues (go on to next file) */
 	case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
@@ -714,6 +750,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
     struct stat st;
     const char *db_path;
     char *dot_notmuch_path;
+    const char *new_msg_tags = "inbox,unread";
     struct sigaction action;
     _filename_node_t *f;
     int renamed_files, removed_files;
@@ -726,12 +763,16 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
     for (i = 0; i < argc && argv[i][0] == '-'; i++) {
 	if (STRNCMP_LITERAL (argv[i], "--verbose") == 0) {
 	    add_files_state.verbose = 1;
+	} else if (STRNCMP_LITERAL (argv[i], "--new-tags=") == 0) {
+	    new_msg_tags = argv[i]+strlen("--new-tags=");
 	} else {
 	    fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
 	    return 1;
 	}
     }

+    add_files_state.new_msg_tags = _parse_tags(ctx, new_msg_tags);
+
     config = notmuch_config_open (ctx, NULL, NULL);
     if (config == NULL)
 	return 1;
diff --git a/notmuch.1 b/notmuch.1
index 86830f4..3bab17c 100644
--- a/notmuch.1
+++ b/notmuch.1
@@ -85,7 +85,7 @@ The
 command is used to incorporate new mail into the notmuch database.
 .RS 4
 .TP 4
-.B new
+.BR new " [options...]"

 Find and import any new messages to the database.

@@ -109,6 +109,23 @@ whenever new mail is delivered and you wish to
incorporate it into the
 database. These subsequent runs will be much quicker than the initial
 run.

+Supported options for
+.B new
+include:
+.RS 4
+.TP 4
+.BR \-\-new\-tags= tags...
+
+Set the listed tags (separated by commas) on new messages
+instead of the default
+.B "inbox"
+and
+.B "unread"
+tags.
+
+.RE
+.RS 4
+
 Invoking
 .B notmuch
 with no command argument will run
diff --git a/notmuch.c b/notmuch.c
index dcfda32..f7b16e3 100644
--- a/notmuch.c
+++ b/notmuch.c
@@ -126,7 +126,7 @@ command_t commands[] = {
       "\tInvoking notmuch with no command argument will run setup if\n"
       "\tthe setup command has not previously been completed." },
     { "new", notmuch_new_command,
-      "[--verbose]",
+      "[--verbose] [--new-tags=inbox,unread]",
       "Find and import new messages to the notmuch database.",
       "\tScans all sub-directories of the mail directory, performing\n"
       "\tfull-text indexing on new messages that are found. Each new\n"
@@ -148,6 +148,11 @@ command_t commands[] = {
       "\t\tVerbose operation. Shows paths of message files as\n"
       "\t\tthey are being indexed.\n"
       "\n"
+      "\t--new-tags=tags...\n"
+      "\n"
+      "\t\tSet the listed tags (separated by commas) on new messages\n"
+      "\t\tinstead of the default \"inbox\" and \"unread\" tags.\n"
+      "\n"
       "\tInvoking notmuch with no command argument will run new if\n"
       "\tthe setup command has previously been completed, but new has\n"
       "\tnot previously been run." },
-- 
1.7.0


More information about the notmuch mailing list