[PATCH/RFC 3/3] notmuch new: sync maildir custom flag user configuration

Igor Almeida igor.contato at gmail.com
Wed Nov 25 18:16:31 PST 2015


This allows the user to specify in the config file how maildir custom
flags translate to notmuch tags (this mapping is stored in the db metadata).

Command 'notmuch new' will pick up changes in the config and update the
database metadata, but will not retag the messages.

Example config:
	[maildir]
	synchronize_flags=true
	customflag_j = todo
	customflag_e = important

Signed-off-by: Igor Almeida <igor.contato at gmail.com>
---
 notmuch-client.h |  4 ++++
 notmuch-config.c | 24 +++++++++++++++++++++++-
 notmuch-new.c    | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 76 insertions(+), 1 deletion(-)

diff --git a/notmuch-client.h b/notmuch-client.h
index 3bd2903..9f5bb8f 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -324,6 +324,10 @@ void
 notmuch_config_set_maildir_synchronize_flags (notmuch_config_t *config,
 					      notmuch_bool_t synchronize_flags);
 
+const char *
+notmuch_config_get_maildir_customflag_map (notmuch_config_t *config,
+					   const char maildir_custom_flag);
+
 const char **
 notmuch_config_get_search_exclude_tags (notmuch_config_t *config, size_t *length);
 
diff --git a/notmuch-config.c b/notmuch-config.c
index d252bb2..f2a25b1 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -72,7 +72,7 @@ static const char user_config_comment[] =
 static const char maildir_config_comment[] =
     " Maildir compatibility configuration\n"
     "\n"
-    " The following option is supported here:\n"
+    " The following options are supported here:\n"
     "\n"
     "\tsynchronize_flags      Valid values are true and false.\n"
     "\n"
@@ -87,6 +87,11 @@ static const char maildir_config_comment[] =
     "\t\tR	replied\n"
     "\t\tS	unread (added when 'S' flag is not present)\n"
     "\n"
+    "\tIf true, then the following option is also considered, and messages\n"
+    "\twill be tagged based on the lower-case letters in the maildir flags:\n"
+    "\n"
+    "\tcustomflag_[a-z]       Valid values are tag names.\n"
+    "\n"
     "\tThe \"notmuch new\" command will notice flag changes in filenames\n"
     "\tand update tags, while the \"notmuch tag\" and \"notmuch restore\"\n"
     "\tcommands will notice tag changes and update flags in filenames\n";
@@ -127,6 +132,8 @@ struct _notmuch_config {
     notmuch_bool_t maildir_synchronize_flags;
     const char **search_exclude_tags;
     size_t search_exclude_tags_length;
+
+    char *customflag_map[26 /* 'a' to 'z' */];
 };
 
 static int
@@ -931,3 +938,18 @@ notmuch_config_set_maildir_synchronize_flags (notmuch_config_t *config,
 			    "maildir", "synchronize_flags", synchronize_flags);
     config->maildir_synchronize_flags = synchronize_flags;
 }
+
+const char *
+notmuch_config_get_maildir_customflag_map (notmuch_config_t *config,
+					   const char maildir_customflag)
+{
+    char key[] = "customflag_a";
+    key[sizeof(key) - 2] = maildir_customflag;
+
+    int index = maildir_customflag - 'a';
+
+    return _config_get (
+	config,
+	&config->customflag_map[index],
+	"maildir", key);
+}
diff --git a/notmuch-new.c b/notmuch-new.c
index d45d0af..d4bb6c7 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -923,6 +923,50 @@ print_results (const add_files_state_t *state)
     printf ("\n");
 }
 
+static void
+_sync_config_and_database_customflag_mapping (notmuch_config_t *config,
+					      notmuch_database_t *notmuch)
+{
+    char c;
+    const char *config_customflag;
+    const char *db_customflag;
+
+    notmuch_status_t status;
+    int index;
+
+    /* Compare the config mapping and the db mapping, warn the user and
+     * overwrite the db if they differ
+     */
+    for (c = 'a'; c <= 'z'; c++) {
+	index = c - 'a';
+	config_customflag = notmuch_config_get_maildir_customflag_map (config, c);
+
+	if (config_customflag != NULL) {
+	    status = notmuch_database_get_maildir_keyword (
+		notmuch, index, &db_customflag);
+	    if (status) {
+		/* TODO what now? break and let 'notmuch new' fail later? */
+	    } else {
+		if (db_customflag != NULL &&
+			strcmp(config_customflag, db_customflag)) {
+		    printf ("Your configuration for maildir custom flags has "
+			    "changed.\n"
+			    "I will overwrite the mapping in the db metadata, "
+			    "but you will have to retag the messages "
+			    "yourself, with something like this:\n"
+			    "\tnotmuch tag +%s tag:%s\n"
+			    "\tnotmuch tag -%s tag:%s\n",
+				config_customflag, db_customflag,
+				db_customflag, config_customflag);
+		}
+
+		notmuch_database_set_maildir_keyword (
+		    notmuch, index, config_customflag);
+	    }
+	}
+    }
+}
+
 int
 notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])
 {
@@ -1068,6 +1112,11 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])
     if (notmuch == NULL)
 	return EXIT_FAILURE;
 
+    /* Make sure our database's maildir customflag mapping matches the config,
+     * warn the user if not
+     */
+    _sync_config_and_database_customflag_mapping (config, notmuch);
+
     /* Set up our handler for SIGINT. We do this after having
      * potentially done a database upgrade we this interrupt handler
      * won't support. */
-- 
2.5.3



More information about the notmuch mailing list