--- /dev/null
+Return-Path: <jani@nikula.org>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id AE4F4431FBC\r
+ for <notmuch@notmuchmail.org>; Fri, 17 Jan 2014 08:39:09 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id WVYs4bR+NIm4 for <notmuch@notmuchmail.org>;\r
+ Fri, 17 Jan 2014 08:39:02 -0800 (PST)\r
+Received: from mail-ea0-f169.google.com (mail-ea0-f169.google.com\r
+ [209.85.215.169]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 69A14431FB6\r
+ for <notmuch@notmuchmail.org>; Fri, 17 Jan 2014 08:39:02 -0800 (PST)\r
+Received: by mail-ea0-f169.google.com with SMTP id l9so1625117eaj.28\r
+ for <notmuch@notmuchmail.org>; Fri, 17 Jan 2014 08:39:01 -0800 (PST)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=1e100.net; s=20130820;\r
+ h=x-gm-message-state:from:to:cc:subject:date:message-id;\r
+ bh=h8UkzEHbSu9ewJzylDmJmr9Mt/2Tx1AfLcu18xbblaQ=;\r
+ b=HruogG3qRP7HKgBU4OhhtT8454dDoVmGDHRQCxfbCL70nGEXFPv04y8trYk4mo3uZp\r
+ GCnsHgf0kojQdz+gmPA0sAiRfNm8iH13QhCGAsWLkcS3t0grCZu1TGdq/JP6ZnATIRYk\r
+ z5KH6Sk+cHvGCXo+ACKYVSHxfWL/dzrFG3RBJzb1XVSbYC2Gc6bH1v+ntHiiKdg9D5Fk\r
+ v7jj5q9LnM70RkqEsEJbBaZ8hTABm1vHPfRRFkzKoGBGFLAd45Lu48Qanx2RfsE/eFKL\r
+ HgoB6GXjuOMXpA1K0l99XntRts9czZsBZnlgdADVoQlyjlEXKW5lLyq6IiGLHmm8WdA7\r
+ cVrg==\r
+X-Gm-Message-State:\r
+ ALoCoQkWELhL+gPZ9kgX5OrbOuLLi0t3gO6PPpS8Y+79ggCHtGRTymbeaczSl/51ayOCzuOPJEmU\r
+X-Received: by 10.14.32.132 with SMTP id o4mr3713545eea.14.1389976741107;\r
+ Fri, 17 Jan 2014 08:39:01 -0800 (PST)\r
+Received: from localhost (dsl-hkibrasgw2-58c36f-91.dhcp.inet.fi.\r
+ [88.195.111.91])\r
+ by mx.google.com with ESMTPSA id v1sm28217872eef.9.2014.01.17.08.38.59\r
+ for <multiple recipients>\r
+ (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+ Fri, 17 Jan 2014 08:39:00 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH] cli: abstract common config get/set code\r
+Date: Fri, 17 Jan 2014 18:38:58 +0200\r
+Message-Id: <1389976738-25056-1-git-send-email-jani@nikula.org>\r
+X-Mailer: git-send-email 1.8.5.2\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://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: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Fri, 17 Jan 2014 16:39:09 -0000\r
+\r
+Pretty straightforward abstraction similar to get/set list.\r
+\r
+---\r
+\r
+v2 of id:1376839205-5115-1-git-send-email-jani@nikula.org adding a few\r
+comments about config value caching per David's request. Dropped the\r
+2nd patch as too tricky.\r
+---\r
+ notmuch-config.c | 86 +++++++++++++++++++++++---------------------------------\r
+ 1 file changed, 35 insertions(+), 51 deletions(-)\r
+\r
+diff --git a/notmuch-config.c b/notmuch-config.c\r
+index 6845e3c..4aad9eb 100644\r
+--- a/notmuch-config.c\r
++++ b/notmuch-config.c\r
+@@ -496,6 +496,32 @@ notmuch_config_is_new (notmuch_config_t *config)\r
+ return config->is_new;\r
+ }\r
+ \r
++static const char *\r
++_config_get (notmuch_config_t *config, char **field,\r
++ const char *group, const char *key)\r
++{\r
++ /* read from config file and cache value, if not cached already */\r
++ if (*field == NULL) {\r
++ char *value;\r
++ value = g_key_file_get_string (config->key_file, group, key, NULL);\r
++ if (value) {\r
++ *field = talloc_strdup (config, value);\r
++ free (value);\r
++ }\r
++ }\r
++ return *field;\r
++}\r
++\r
++static void\r
++_config_set (notmuch_config_t *config, char **field,\r
++ const char *group, const char *key, const char *value)\r
++{\r
++ g_key_file_set_string (config->key_file, group, key, value);\r
++\r
++ /* drop the cached value */\r
++ talloc_free (*field);\r
++ *field = NULL;\r
++}\r
+ \r
+ static const char **\r
+ _config_get_list (notmuch_config_t *config,\r
+@@ -504,6 +530,7 @@ _config_get_list (notmuch_config_t *config,\r
+ {\r
+ assert(outlist);\r
+ \r
++ /* read from config file and cache value, if not cached already */\r
+ if (*outlist == NULL) {\r
+ \r
+ char **inlist = g_key_file_get_string_list (config->key_file,\r
+@@ -535,6 +562,8 @@ _config_set_list (notmuch_config_t *config,\r
+ size_t length, const char ***config_var )\r
+ {\r
+ g_key_file_set_string_list (config->key_file, group, name, list, length);\r
++\r
++ /* drop the cached value */\r
+ talloc_free (*config_var);\r
+ *config_var = NULL;\r
+ }\r
+@@ -542,85 +571,40 @@ _config_set_list (notmuch_config_t *config,\r
+ const char *\r
+ notmuch_config_get_database_path (notmuch_config_t *config)\r
+ {\r
+- char *path;\r
+-\r
+- if (config->database_path == NULL) {\r
+- path = g_key_file_get_string (config->key_file,\r
+- "database", "path", NULL);\r
+- if (path) {\r
+- config->database_path = talloc_strdup (config, path);\r
+- free (path);\r
+- }\r
+- }\r
+-\r
+- return config->database_path;\r
++ return _config_get (config, &config->database_path, "database", "path");\r
+ }\r
+ \r
+ void\r
+ notmuch_config_set_database_path (notmuch_config_t *config,\r
+ const char *database_path)\r
+ {\r
+- g_key_file_set_string (config->key_file,\r
+- "database", "path", database_path);\r
+-\r
+- talloc_free (config->database_path);\r
+- config->database_path = NULL;\r
++ _config_set (config, &config->database_path, "database", "path", database_path);\r
+ }\r
+ \r
+ const char *\r
+ notmuch_config_get_user_name (notmuch_config_t *config)\r
+ {\r
+- char *name;\r
+-\r
+- if (config->user_name == NULL) {\r
+- name = g_key_file_get_string (config->key_file,\r
+- "user", "name", NULL);\r
+- if (name) {\r
+- config->user_name = talloc_strdup (config, name);\r
+- free (name);\r
+- }\r
+- }\r
+-\r
+- return config->user_name;\r
++ return _config_get (config, &config->user_name, "user", "name");\r
+ }\r
+ \r
+ void\r
+ notmuch_config_set_user_name (notmuch_config_t *config,\r
+ const char *user_name)\r
+ {\r
+- g_key_file_set_string (config->key_file,\r
+- "user", "name", user_name);\r
+-\r
+- talloc_free (config->user_name);\r
+- config->user_name = NULL;\r
++ _config_set (config, &config->user_name, "user", "name", user_name);\r
+ }\r
+ \r
+ const char *\r
+ notmuch_config_get_user_primary_email (notmuch_config_t *config)\r
+ {\r
+- char *email;\r
+-\r
+- if (config->user_primary_email == NULL) {\r
+- email = g_key_file_get_string (config->key_file,\r
+- "user", "primary_email", NULL);\r
+- if (email) {\r
+- config->user_primary_email = talloc_strdup (config, email);\r
+- free (email);\r
+- }\r
+- }\r
+-\r
+- return config->user_primary_email;\r
++ return _config_get (config, &config->user_primary_email, "user", "primary_email");\r
+ }\r
+ \r
+ void\r
+ notmuch_config_set_user_primary_email (notmuch_config_t *config,\r
+ const char *primary_email)\r
+ {\r
+- g_key_file_set_string (config->key_file,\r
+- "user", "primary_email", primary_email);\r
+-\r
+- talloc_free (config->user_primary_email);\r
+- config->user_primary_email = NULL;\r
++ _config_set (config, &config->user_primary_email, "user", "primary_email", primary_email);\r
+ }\r
+ \r
+ const char **\r
+-- \r
+1.8.5.2\r
+\r