[notmuch] [PATCH 2/4] notmuch: Config option to specify tags to be applied by 'notmuch new'.

Jan Janak jan at ryngle.com
Tue Nov 24 14:10:28 PST 2009


Add support for section [new] in the configuration file. This section
is supposed to contain options for 'notmuch new'. Currently there is
only one option called tags.

The tags option can be used to configure a set of tags to be applied
by 'notmuch new'. Individual tags are separated by semicolon.

'notmuch new' is modified not to apply 'inbox' and 'unread' by default,
but instead it obtains the set of tags to be applied from the new
configuration file option.

Signed-off-by: Jan Janak <jan at ryngle.com>
---
 notmuch-client.h |    3 +++
 notmuch-config.c |   44 ++++++++++++++++++++++++++++++++++++++++++++
 notmuch-new.c    |   19 ++++++++++++++++++-
 3 files changed, 65 insertions(+), 1 deletions(-)

diff --git a/notmuch-client.h b/notmuch-client.h
index c04eaeb..0fb9c19 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -184,6 +184,9 @@ notmuch_config_set_user_other_email (notmuch_config_t *config,
 				     const char *other_email[],
 				     size_t length);
 
+char **
+notmuch_config_get_new_tags (notmuch_config_t *config, size_t *length);
+
 notmuch_bool_t
 debugger_is_active (void);
 
diff --git a/notmuch-config.c b/notmuch-config.c
index fc65d6b..7f62a80 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -22,6 +22,7 @@
 
 #include <pwd.h>
 #include <netdb.h>
+#include <ctype.h>
 
 static const char toplevel_config_comment[] =
     " .notmuch-config - Configuration file for the notmuch mail system\n"
@@ -62,6 +63,9 @@ struct _notmuch_config {
     char *user_primary_email;
     char **user_other_email;
     size_t user_other_email_length;
+
+    char **new_tags;
+    size_t new_tags_length;
 };
 
 static int
@@ -199,6 +203,8 @@ notmuch_config_open (void *ctx,
     config->user_primary_email = NULL;
     config->user_other_email = NULL;
     config->user_other_email_length = 0;
+    config->new_tags = NULL;
+    config->new_tags_length = 0;
 
     if (! g_key_file_load_from_file (config->key_file,
 				     config->filename,
@@ -450,3 +456,41 @@ notmuch_config_set_user_other_email (notmuch_config_t *config,
     talloc_free (config->user_other_email);
     config->user_other_email = NULL;
 }
+
+char **
+notmuch_config_get_new_tags (notmuch_config_t *config, size_t *length)
+{
+    char **tags;
+    size_t len;
+    unsigned int i;
+    char *start, *end;
+
+    if (config->new_tags == NULL) {
+	config->new_tags_length = 0;
+	tags = g_key_file_get_string_list (config->key_file, "new", "tags",
+					   &len, NULL);
+
+	if (tags) {
+	    config->new_tags = talloc_size (config, sizeof(char*) *
+					    (len + 1));
+	    for (i = 0; i < len; i++) {
+		/* Remove leading and trailing white space around the tag and
+		 * filter out empty tags. */
+		start = tags[i];
+		end = start + strlen (start) - 1;
+		while (isspace (*start)) start++;
+		while (end > start && isspace (*end)) end--;
+		if (end >= start) {
+		    config->new_tags[config->new_tags_length++] =
+			talloc_strndup (config->new_tags, start,
+					end - start + 1);
+		}
+	    }
+	    config->new_tags[config->new_tags_length] = NULL;
+	    g_strfreev (tags);
+	}
+    }
+
+    *length = config->new_tags_length;
+    return config->new_tags;
+}
diff --git a/notmuch-new.c b/notmuch-new.c
index 9970407..10745e8 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -24,6 +24,8 @@
 
 static volatile sig_atomic_t do_add_files_print_progress = 0;
 
+static notmuch_config_t *config = NULL;
+
 static void
 handle_sigalrm (unused (int signal))
 {
@@ -68,6 +70,21 @@ add_files_print_progress (add_files_state_t *state)
     fflush (stdout);
 }
 
+static void
+apply_tags (notmuch_message_t *message)
+{
+    char** tags;
+    unsigned int count, i;
+
+    if ((tags = notmuch_config_get_new_tags (config, &count)) == NULL)
+	return;
+
+    for (i = 0; i < count; i++) {
+	if (tags[i])
+	    notmuch_message_add_tag (message, tags[i]);
+    }
+}
+
 static int ino_cmp(const struct dirent **a, const struct dirent **b)
 {
     return ((*a)->d_ino < (*b)->d_ino) ? -1 : 1;
@@ -191,6 +208,7 @@ add_files_recursive (notmuch_database_t *notmuch,
 		    /* success */
 		    case NOTMUCH_STATUS_SUCCESS:
 			state->added_messages++;
+			apply_tags (message);
 			break;
 		    /* Non-fatal issues (go on to next file) */
 		    case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
@@ -388,7 +406,6 @@ count_files (const char *path, int *count)
 int
 notmuch_new_command (void *ctx, int argc, char *argv[])
 {
-    notmuch_config_t *config;
     notmuch_database_t *notmuch;
     add_files_state_t add_files_state;
     double elapsed;
-- 
1.6.3.3



More information about the notmuch mailing list