If the user runs 'notmuch setup' and there is no configuration file yet
then we also add the new section [new] to the configuration file and
set the tags option as:

  tags=inbox;unread

This will be picked up by 'notmuch new' and all new mail added to the
database will be tagged with the two tags as before.

If the user already has a configuration file and runs 'notmuch setup'
then we just copy whatever tags we find in the old configuration file
to the new one. If there are no tags in the old configuration file then
we assume that the user configured notmuch that way and the new config
file would also have no tags in the section [new].

We never ask the user interactively for the list of tags to be used by
'notmuch new', it is assumed that beginners would want to stick to the
defaults and advanced users can edit the configuration manually.

This revision of the patch includes suggestions from Bart Trojanowski.

Signed-off-by: Jan Janak <jan at ryngle.com>
---
 notmuch-client.h |    4 ++++
 notmuch-config.c |   40 ++++++++++++++++++++++++++++++++++++++++
 notmuch-setup.c  |   18 ++++++++++++++----
 3 files changed, 58 insertions(+), 4 deletions(-)

diff --git a/notmuch-client.h b/notmuch-client.h
index 0fb9c19..bb7d3d4 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -187,6 +187,10 @@ notmuch_config_set_user_other_email (notmuch_config_t 
*config,
 char **
 notmuch_config_get_new_tags (notmuch_config_t *config, size_t *length);

+void
+notmuch_config_set_new_tags (notmuch_config_t *config, const char *tags[],
+                            size_t length);
+
 notmuch_bool_t
 debugger_is_active (void);

diff --git a/notmuch-config.c b/notmuch-config.c
index 57072ce..2bbeb20 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -54,6 +54,16 @@ static const char user_config_comment[] =
     " recipient list of replies, and will set the From address based on the\n"
     " address to which the original email was addressed.\n";

+static const char new_config_comment[] =
+    " Configuration section for 'notmuch new'\n"
+    "\n"
+    " The only supported value at the moment is 'tags. This option contains 
a\n"
+    " list of tags (separated by ';') that should be  automatically applied 
to\n"
+    " newly added messages.\n"
+    "\n"
+    " Note that 'notmuch new' also has a command line option which can be 
used\n"
+    " to add additional tags to the ones configured here.\n";
+
 struct _notmuch_config {
     char *filename;
     GKeyFile *key_file;
@@ -174,6 +184,7 @@ notmuch_config_open (void *ctx,
     GError *error = NULL;
     int is_new = 0;
     char *notmuch_config_env = NULL;
+    const char* def_new_tags[2] = {"inbox", "unread"};

     if (is_new_ret)
        *is_new_ret = 0;
@@ -270,6 +281,20 @@ notmuch_config_open (void *ctx,
        }
     }

+    /* If we have no configuration file then we configure "inbox" and "unread"
+     * tags by default for 'notmuch new'. This ensures that the Emacs mode
+     * would still work as expected.
+     *
+     * We do not ask the user for tags to be used by 'notmuch new'. That's too
+     * much detail for beginners and others can edit the configuration file by
+     * hand.
+     */
+    if (is_new) {
+       notmuch_config_set_new_tags (config, def_new_tags,
+                                    sizeof(def_new_tags) /
+                                    sizeof(const char*));
+    }
+
     /* When we create a new configuration file here, we  add some
      * comments to help the user understand what can be done. */
     if (is_new) {
@@ -279,6 +304,8 @@ notmuch_config_open (void *ctx,
                                database_config_comment, NULL);
        g_key_file_set_comment (config->key_file, "user", NULL,
                                user_config_comment, NULL);
+       g_key_file_set_comment (config->key_file, "new", NULL,
+                               new_config_comment, NULL);
     }

     if (is_new_ret)
@@ -493,3 +520,16 @@ notmuch_config_get_new_tags (notmuch_config_t *config, 
size_t *length)
     *length = config->new_tags_length;
     return config->new_tags;
 }
+
+void
+notmuch_config_set_new_tags (notmuch_config_t *config,
+                            const char *tags[],
+                            size_t length)
+{
+    g_key_file_set_string_list (config->key_file,
+                               "new", "tags",
+                               tags, length);
+
+    talloc_free (config->new_tags);
+    config->user_other_email = NULL;
+}
diff --git a/notmuch-setup.c b/notmuch-setup.c
index d06fbf8..aeffb88 100644
--- a/notmuch-setup.c
+++ b/notmuch-setup.c
@@ -92,12 +92,10 @@ notmuch_setup_command (unused (void *ctx),
                       unused (int argc), unused (char *argv[]))
 {
     char *response = NULL;
-    size_t response_size;
+    size_t response_size, old_other_emails_len, new_tags_len, i;
     notmuch_config_t *config;
-    char **old_other_emails;
-    size_t old_other_emails_len;
+    char **old_other_emails, **new_tags;
     GPtrArray *other_emails;
-    unsigned int i;
     int is_new;

 #define prompt(format, ...)                            \
@@ -147,6 +145,18 @@ notmuch_setup_command (unused (void *ctx),
                                             other_emails->len);
     g_ptr_array_free (other_emails, TRUE);

+    /* If we already have a configuration file then we preserve the tags
+     * configured there. If the original configuration file contains no tags
+     * then we assume that the user configured it that way and add no tags.
+     */
+    if (!is_new) {
+       new_tags = notmuch_config_get_new_tags (config, &new_tags_len);
+       if (new_tags) {
+           notmuch_config_set_new_tags (config, (const char**)new_tags,
+                                        new_tags_len);
+       }
+    }
+
     prompt ("Top-level directory of your email archive [%s]: ",
            notmuch_config_get_database_path (config));
     if (strlen (response)) {
-- 
1.6.3.3

Reply via email to