--- /dev/null
+Return-Path: <igor.contato@gmail.com>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by arlo.cworth.org (Postfix) with ESMTP id 275DA6DE19A0\r
+ for <notmuch@notmuchmail.org>; Wed, 25 Nov 2015 18:17:16 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.705\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.705 tagged_above=-999 required=5 tests=[AWL=0.096,\r
+ DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_FROM=0.001,\r
+ RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_PASS=-0.001]\r
+ autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id yaO4D-L94-rr for <notmuch@notmuchmail.org>;\r
+ Wed, 25 Nov 2015 18:17:14 -0800 (PST)\r
+Received: from mail-qg0-f46.google.com (mail-qg0-f46.google.com\r
+ [209.85.192.46])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id CE9316DE1989\r
+ for <notmuch@notmuchmail.org>; Wed, 25 Nov 2015 18:16:55 -0800 (PST)\r
+Received: by qgcc31 with SMTP id c31so45657815qgc.3\r
+ for <notmuch@notmuchmail.org>; Wed, 25 Nov 2015 18:16:54 -0800 (PST)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+ h=from:to:cc:subject:date:message-id:in-reply-to:references;\r
+ bh=2ZT7nQ5OyGT646RI+kkwMsxiIBYqBcNG34q48V+odqY=;\r
+ b=pquWHaOBIyvrPo6qK3Vc/B1xgaoyqQ2mSu3obJhPEJQ+0QbvHDwPnEwbNAgvnwIczD\r
+ Dbi6qXihmIOL3775xsNbc/k03MowV+E1JwbpxE3rkos79bNJv3RaoT5rpqNgoLLZ9DPf\r
+ VvXNI9tHzry/W7smSFW2N+zUl96ZWapIaALROtXfhNHX+bWBGK/3kx7f/vYUOyT9pINM\r
+ evHt+RgSQRQ08/q5rsozGtoSWVpDF6VsJY+t6IrkGXpNIXt7Y+/i+Y/r7q6+bHQ/dwGq\r
+ KGeo/kEfDoprxGze6T0DZVIv5IwR/Y9iqoc3P3nCVttVAORUOxptvD2PUrW8PoGKl5Bq\r
+ RGZA==\r
+X-Received: by 10.140.31.161 with SMTP id f30mr42861846qgf.95.1448504214817;\r
+ Wed, 25 Nov 2015 18:16:54 -0800 (PST)\r
+Received: from arch-laptop.localdomain ([200.195.16.216])\r
+ by smtp.gmail.com with ESMTPSA id h206sm1566011qhc.43.2015.11.25.18.16.52\r
+ (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128);\r
+ Wed, 25 Nov 2015 18:16:53 -0800 (PST)\r
+From: Igor Almeida <igor.contato@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH/RFC 3/3] notmuch new: sync maildir custom flag user\r
+ configuration\r
+Date: Wed, 25 Nov 2015 23:16:31 -0300\r
+Message-Id: <1448504191-30974-4-git-send-email-igor.contato@gmail.com>\r
+X-Mailer: git-send-email 2.5.3\r
+In-Reply-To: <1448504191-30974-1-git-send-email-igor.contato@gmail.com>\r
+References: <1448504191-30974-1-git-send-email-igor.contato@gmail.com>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.20\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Thu, 26 Nov 2015 02:17:16 -0000\r
+\r
+This allows the user to specify in the config file how maildir custom\r
+flags translate to notmuch tags (this mapping is stored in the db metadata).\r
+\r
+Command 'notmuch new' will pick up changes in the config and update the\r
+database metadata, but will not retag the messages.\r
+\r
+Example config:\r
+ [maildir]\r
+ synchronize_flags=true\r
+ customflag_j = todo\r
+ customflag_e = important\r
+\r
+Signed-off-by: Igor Almeida <igor.contato@gmail.com>\r
+---\r
+ notmuch-client.h | 4 ++++\r
+ notmuch-config.c | 24 +++++++++++++++++++++++-\r
+ notmuch-new.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++\r
+ 3 files changed, 76 insertions(+), 1 deletion(-)\r
+\r
+diff --git a/notmuch-client.h b/notmuch-client.h\r
+index 3bd2903..9f5bb8f 100644\r
+--- a/notmuch-client.h\r
++++ b/notmuch-client.h\r
+@@ -324,6 +324,10 @@ void\r
+ notmuch_config_set_maildir_synchronize_flags (notmuch_config_t *config,\r
+ notmuch_bool_t synchronize_flags);\r
+ \r
++const char *\r
++notmuch_config_get_maildir_customflag_map (notmuch_config_t *config,\r
++ const char maildir_custom_flag);\r
++\r
+ const char **\r
+ notmuch_config_get_search_exclude_tags (notmuch_config_t *config, size_t *length);\r
+ \r
+diff --git a/notmuch-config.c b/notmuch-config.c\r
+index d252bb2..f2a25b1 100644\r
+--- a/notmuch-config.c\r
++++ b/notmuch-config.c\r
+@@ -72,7 +72,7 @@ static const char user_config_comment[] =\r
+ static const char maildir_config_comment[] =\r
+ " Maildir compatibility configuration\n"\r
+ "\n"\r
+- " The following option is supported here:\n"\r
++ " The following options are supported here:\n"\r
+ "\n"\r
+ "\tsynchronize_flags Valid values are true and false.\n"\r
+ "\n"\r
+@@ -87,6 +87,11 @@ static const char maildir_config_comment[] =\r
+ "\t\tR replied\n"\r
+ "\t\tS unread (added when 'S' flag is not present)\n"\r
+ "\n"\r
++ "\tIf true, then the following option is also considered, and messages\n"\r
++ "\twill be tagged based on the lower-case letters in the maildir flags:\n"\r
++ "\n"\r
++ "\tcustomflag_[a-z] Valid values are tag names.\n"\r
++ "\n"\r
+ "\tThe \"notmuch new\" command will notice flag changes in filenames\n"\r
+ "\tand update tags, while the \"notmuch tag\" and \"notmuch restore\"\n"\r
+ "\tcommands will notice tag changes and update flags in filenames\n";\r
+@@ -127,6 +132,8 @@ struct _notmuch_config {\r
+ notmuch_bool_t maildir_synchronize_flags;\r
+ const char **search_exclude_tags;\r
+ size_t search_exclude_tags_length;\r
++\r
++ char *customflag_map[26 /* 'a' to 'z' */];\r
+ };\r
+ \r
+ static int\r
+@@ -931,3 +938,18 @@ notmuch_config_set_maildir_synchronize_flags (notmuch_config_t *config,\r
+ "maildir", "synchronize_flags", synchronize_flags);\r
+ config->maildir_synchronize_flags = synchronize_flags;\r
+ }\r
++\r
++const char *\r
++notmuch_config_get_maildir_customflag_map (notmuch_config_t *config,\r
++ const char maildir_customflag)\r
++{\r
++ char key[] = "customflag_a";\r
++ key[sizeof(key) - 2] = maildir_customflag;\r
++\r
++ int index = maildir_customflag - 'a';\r
++\r
++ return _config_get (\r
++ config,\r
++ &config->customflag_map[index],\r
++ "maildir", key);\r
++}\r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index d45d0af..d4bb6c7 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -923,6 +923,50 @@ print_results (const add_files_state_t *state)\r
+ printf ("\n");\r
+ }\r
+ \r
++static void\r
++_sync_config_and_database_customflag_mapping (notmuch_config_t *config,\r
++ notmuch_database_t *notmuch)\r
++{\r
++ char c;\r
++ const char *config_customflag;\r
++ const char *db_customflag;\r
++\r
++ notmuch_status_t status;\r
++ int index;\r
++\r
++ /* Compare the config mapping and the db mapping, warn the user and\r
++ * overwrite the db if they differ\r
++ */\r
++ for (c = 'a'; c <= 'z'; c++) {\r
++ index = c - 'a';\r
++ config_customflag = notmuch_config_get_maildir_customflag_map (config, c);\r
++\r
++ if (config_customflag != NULL) {\r
++ status = notmuch_database_get_maildir_keyword (\r
++ notmuch, index, &db_customflag);\r
++ if (status) {\r
++ /* TODO what now? break and let 'notmuch new' fail later? */\r
++ } else {\r
++ if (db_customflag != NULL &&\r
++ strcmp(config_customflag, db_customflag)) {\r
++ printf ("Your configuration for maildir custom flags has "\r
++ "changed.\n"\r
++ "I will overwrite the mapping in the db metadata, "\r
++ "but you will have to retag the messages "\r
++ "yourself, with something like this:\n"\r
++ "\tnotmuch tag +%s tag:%s\n"\r
++ "\tnotmuch tag -%s tag:%s\n",\r
++ config_customflag, db_customflag,\r
++ db_customflag, config_customflag);\r
++ }\r
++\r
++ notmuch_database_set_maildir_keyword (\r
++ notmuch, index, config_customflag);\r
++ }\r
++ }\r
++ }\r
++}\r
++\r
+ int\r
+ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])\r
+ {\r
+@@ -1068,6 +1112,11 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])\r
+ if (notmuch == NULL)\r
+ return EXIT_FAILURE;\r
+ \r
++ /* Make sure our database's maildir customflag mapping matches the config,\r
++ * warn the user if not\r
++ */\r
++ _sync_config_and_database_customflag_mapping (config, notmuch);\r
++\r
+ /* Set up our handler for SIGINT. We do this after having\r
+ * potentially done a database upgrade we this interrupt handler\r
+ * won't support. */\r
+-- \r
+2.5.3\r
+\r