From 632f010068e49c4549d6e9c60e499807c11b2597 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Peter=20Bostr=C3=B6m?= <pbos@kth.se>
Date: Tue, 19 Feb 2013 23:00:57 +0100
Subject: [PATCH] core: API function "config_set_defaults_plugin" for setting
 default value and description for plugin options

Also deprecating "config_set_desc_plugin"
---
 ChangeLog                               |    4 ++
 doc/de/weechat_scripting.de.txt         |    3 +-
 doc/en/weechat_plugin_api.en.txt        |   64 +++++++++++++++++++++++++
 doc/en/weechat_scripting.en.txt         |    3 +-
 doc/fr/weechat_plugin_api.fr.txt        |   66 ++++++++++++++++++++++++++
 doc/fr/weechat_scripting.fr.txt         |    3 +-
 doc/it/weechat_plugin_api.it.txt        |   66 ++++++++++++++++++++++++++
 doc/it/weechat_scripting.it.txt         |    3 +-
 doc/ja/weechat_scripting.ja.txt         |    3 +-
 doc/pl/weechat_scripting.pl.txt         |    3 +-
 src/plugins/guile/weechat-guile-api.c   |   21 ++++++++
 src/plugins/lua/weechat-lua-api.c       |   23 +++++++++
 src/plugins/perl/weechat-perl-api.c     |   23 +++++++++
 src/plugins/plugin-api.c                |   18 +++++++
 src/plugins/plugin-api.h                |    5 ++
 src/plugins/plugin-config.c             |   79 +++++++++++++++++++++++++++++++
 src/plugins/plugin-config.h             |    4 ++
 src/plugins/plugin-script-api.c         |   33 +++++++++++++
 src/plugins/plugin-script-api.h         |    5 ++
 src/plugins/plugin.c                    |    1 +
 src/plugins/python/weechat-python-api.c |   23 +++++++++
 src/plugins/ruby/weechat-ruby-api.c     |   30 ++++++++++++
 src/plugins/tcl/weechat-tcl-api.c       |   26 ++++++++++
 src/plugins/weechat-plugin.h            |    8 ++++
 24 files changed, 511 insertions(+), 6 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 98da3ad..23cee54 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -7,6 +7,10 @@ v0.4.1-dev, 2013-02-18
 Version 0.4.1 (under dev!)
 --------------------------
 
+* core: API function "config_set_defaults_plugin" for setting default value and
+  descriptions of plugin options
+* core: deprecate API function "config_set_desc_plugin" in favor of
+  "config_set_defaults_plugin"
 * core: allow read of array in hdata without using index
 * core: add option "dirs" for command /debug
 * core: fix detection of iconv with cmake on OS X (bug #38321)
diff --git a/doc/de/weechat_scripting.de.txt b/doc/de/weechat_scripting.de.txt
index a7f7dd4..f5f7bbc 100644
--- a/doc/de/weechat_scripting.de.txt
+++ b/doc/de/weechat_scripting.de.txt
@@ -395,7 +395,8 @@ Liste der Skript API Funktionen:
   config_option_free, config_section_free_options, config_section_free,
   config_free, +
   config_get, config_get_plugin, config_is_set_plugin, config_set_plugin,
-  config_set_desc_plugin, config_unset_plugin
+  config_set_defaults_plugin, config_set_desc_plugin (deprecated),
+  config_unset_plugin
 | Tastenbelegung      |
   key_bind, key_unbind
 | Ausgabe             |
diff --git a/doc/en/weechat_plugin_api.en.txt b/doc/en/weechat_plugin_api.en.txt
index 436b581..5b8b24b 100644
--- a/doc/en/weechat_plugin_api.en.txt
+++ b/doc/en/weechat_plugin_api.en.txt
@@ -5911,11 +5911,75 @@ elif rc == weechat.WEECHAT_CONFIG_OPTION_SET_ERROR:
     # ...
 ----------------------------------------
 
+weechat_config_set_defaults_plugin
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Set default value and description for option in plugins configuration file
+(plugins.conf). This should be called to register the defaults for every static
+option on plugin or script startup.
+
+Prototype:
+
+[source,C]
+----------------------------------------
+int weechat_config_set_defaults_plugin (const char *option_name, const char *default_value, const char *description);
+----------------------------------------
+
+Arguments:
+
+* 'option_name': option name, WeeChat will add prefix "plugins.var.xxx."
+  (where "xxx" is current plugin name)
+* 'default_value': default value for the option
+* 'description': description of the option
+
+Return value:
+
+* 'WEECHAT_CONFIG_OPTION_SET_OK_CHANGED' if option value existed and differs from default
+* 'WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE' if the current value is the same as the default
+* 'WEECHAT_CONFIG_OPTION_SET_ERROR' if other error occurred
+
+C example:
+
+[source,C]
+----------------------------------------
+switch (weechat_config_set_defaults_plugin ("option", "default value", "description of option"))
+{
+    case WEECHAT_CONFIG_OPTION_SET_OK_CHANGED:
+        /* ... */
+        break;
+    case WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE:
+        /* ... */
+        break;
+    case WEECHAT_CONFIG_OPTION_SET_ERROR:
+        /* ... */
+        break;
+}
+----------------------------------------
+
+Script (Python):
+
+[source,python]
+----------------------------------------
+# prototype
+rc = weechat.config_set_defaults_plugin(option_name, default_value, description)
+
+# example
+rc = weechat.config_set_plugin("option", "default value", "description of option")
+if rc == weechat.WEECHAT_CONFIG_OPTION_SET_OK_CHANGED:
+    # ...
+elif rc == weechat.WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE:
+    # ...
+elif rc == weechat.WEECHAT_CONFIG_OPTION_SET_ERROR:
+    # ...
+----------------------------------------
+
 weechat_config_set_desc_plugin
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 _New in version 0.3.5._
 
+_Deprecated in favor of 'weechat_config_set_defaults_plugin'_
+
 Set description for option in plugins configuration file (plugins.conf).
 
 Prototype:
diff --git a/doc/en/weechat_scripting.en.txt b/doc/en/weechat_scripting.en.txt
index 51a94e2..77baffa 100644
--- a/doc/en/weechat_scripting.en.txt
+++ b/doc/en/weechat_scripting.en.txt
@@ -388,7 +388,8 @@ List of functions in script API:
   config_option_free, config_section_free_options, config_section_free,
   config_free, +
   config_get, config_get_plugin, config_is_set_plugin, config_set_plugin,
-  config_set_desc_plugin, config_unset_plugin
+  config_set_defaults_plugin, config_set_desc_plugin (deprecated),
+  config_unset_plugin
 | key bindings        |
   key_bind, key_unbind
 | display             |
diff --git a/doc/fr/weechat_plugin_api.fr.txt b/doc/fr/weechat_plugin_api.fr.txt
index 6080461..69ca0bd 100644
--- a/doc/fr/weechat_plugin_api.fr.txt
+++ b/doc/fr/weechat_plugin_api.fr.txt
@@ -5978,11 +5978,77 @@ elif rc == weechat.WEECHAT_CONFIG_OPTION_SET_ERROR:
     # ...
 ----------------------------------------
 
+// TRANSLATION MISSING
+weechat_config_set_defaults_plugin
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Set default value and description for option in plugins configuration file
+(plugins.conf). This should be called to register the defaults for every static
+option on plugin or script startup.
+
+Prototype:
+
+[source,C]
+----------------------------------------
+int weechat_config_set_defaults_plugin (const char *option_name, const char *default_value, const char *description);
+----------------------------------------
+
+Arguments:
+
+* 'option_name': option name, WeeChat will add prefix "plugins.var.xxx."
+  (where "xxx" is current plugin name)
+* 'default_value': default value for the option
+* 'description': description of the option
+
+Return value:
+
+* 'WEECHAT_CONFIG_OPTION_SET_OK_CHANGED' if option value existed and differs from default
+* 'WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE' if the current value is the same as the default
+* 'WEECHAT_CONFIG_OPTION_SET_ERROR' if other error occurred
+
+C example:
+
+[source,C]
+----------------------------------------
+switch (weechat_config_set_defaults_plugin ("option", "default value", "description of option"))
+{
+    case WEECHAT_CONFIG_OPTION_SET_OK_CHANGED:
+        /* ... */
+        break;
+    case WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE:
+        /* ... */
+        break;
+    case WEECHAT_CONFIG_OPTION_SET_ERROR:
+        /* ... */
+        break;
+}
+----------------------------------------
+
+Script (Python):
+
+[source,python]
+----------------------------------------
+# prototype
+rc = weechat.config_set_defaults_plugin(option_name, default_value, description)
+
+# example
+rc = weechat.config_set_plugin("option", "default value", "description of option")
+if rc == weechat.WEECHAT_CONFIG_OPTION_SET_OK_CHANGED:
+    # ...
+elif rc == weechat.WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE:
+    # ...
+elif rc == weechat.WEECHAT_CONFIG_OPTION_SET_ERROR:
+    # ...
+----------------------------------------
+
 weechat_config_set_desc_plugin
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 _Nouveau dans la version 0.3.5._
 
+// TRANSLATION MISSING
+_Deprecated in favor of 'weechat_config_set_defaults_plugin'_
+
 Affecter une description pour une option dans le fichier de configuration des
 extensions (plugins.conf).
 
diff --git a/doc/fr/weechat_scripting.fr.txt b/doc/fr/weechat_scripting.fr.txt
index d776231..1cc48ee 100644
--- a/doc/fr/weechat_scripting.fr.txt
+++ b/doc/fr/weechat_scripting.fr.txt
@@ -397,7 +397,8 @@ Liste des fonctions de l'API script :
   config_option_free, config_section_free_options, config_section_free,
   config_free, +
   config_get, config_get_plugin, config_is_set_plugin, config_set_plugin,
-  config_set_desc_plugin, config_unset_plugin
+  config_set_defaults_plugin, config_set_desc_plugin (deprecated),
+  config_unset_plugin
 | associations de touches   |
   key_bind, key_unbind
 | affichage                 |
diff --git a/doc/it/weechat_plugin_api.it.txt b/doc/it/weechat_plugin_api.it.txt
index 85fe96b..526b9ac 100644
--- a/doc/it/weechat_plugin_api.it.txt
+++ b/doc/it/weechat_plugin_api.it.txt
@@ -5923,11 +5923,77 @@ elif rc == weechat.WEECHAT_CONFIG_OPTION_SET_ERROR:
     # ...
 ----------------------------------------
 
+// TRANSLATION MISSING
+weechat_config_set_defaults_plugin
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+Set default value and description for option in plugins configuration file
+(plugins.conf). This should be called to register the defaults for every static
+option on plugin or script startup.
+
+Prototype:
+
+[source,C]
+----------------------------------------
+int weechat_config_set_defaults_plugin (const char *option_name, const char *default_value, const char *description);
+----------------------------------------
+
+Arguments:
+
+* 'option_name': option name, WeeChat will add prefix "plugins.var.xxx."
+  (where "xxx" is current plugin name)
+* 'default_value': default value for the option
+* 'description': description of the option
+
+Return value:
+
+* 'WEECHAT_CONFIG_OPTION_SET_OK_CHANGED' if option value existed and differs from default
+* 'WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE' if the current value is the same as the default
+* 'WEECHAT_CONFIG_OPTION_SET_ERROR' if other error occurred
+
+C example:
+
+[source,C]
+----------------------------------------
+switch (weechat_config_set_defaults_plugin ("option", "default value", "description of option"))
+{
+    case WEECHAT_CONFIG_OPTION_SET_OK_CHANGED:
+        /* ... */
+        break;
+    case WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE:
+        /* ... */
+        break;
+    case WEECHAT_CONFIG_OPTION_SET_ERROR:
+        /* ... */
+        break;
+}
+----------------------------------------
+
+Script (Python):
+
+[source,python]
+----------------------------------------
+# prototype
+rc = weechat.config_set_defaults_plugin(option_name, default_value, description)
+
+# example
+rc = weechat.config_set_plugin("option", "default value", "description of option")
+if rc == weechat.WEECHAT_CONFIG_OPTION_SET_OK_CHANGED:
+    # ...
+elif rc == weechat.WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE:
+    # ...
+elif rc == weechat.WEECHAT_CONFIG_OPTION_SET_ERROR:
+    # ...
+----------------------------------------
+
 weechat_config_set_desc_plugin
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 _Novità nella versione 0.3.5._
 
+// TRANSLATION MISSING
+_Deprecated in favor of 'weechat_config_set_defaults_plugin'_
+
 Imposta la descrizione per l'opzione nel file di configurazione dei plugin
 (plugins.conf).
 
diff --git a/doc/it/weechat_scripting.it.txt b/doc/it/weechat_scripting.it.txt
index 0117f88..76f0220 100644
--- a/doc/it/weechat_scripting.it.txt
+++ b/doc/it/weechat_scripting.it.txt
@@ -400,7 +400,8 @@ Elenco di funzioni nelle API per gli script:
   config_option_free, config_section_free_options, config_section_free,
   config_free, +
   config_get, config_get_plugin, config_is_set_plugin, config_set_plugin,
-  config_set_desc_plugin, config_unset_plugin
+  config_set_defaults_plugin, config_set_desc_plugin (deprecated),
+  config_unset_plugin
 | combinazione tasti     |
   key_bind, key_unbind
 | visualizzazione        |
diff --git a/doc/ja/weechat_scripting.ja.txt b/doc/ja/weechat_scripting.ja.txt
index f2cedd5..ed43597 100644
--- a/doc/ja/weechat_scripting.ja.txt
+++ b/doc/ja/weechat_scripting.ja.txt
@@ -378,7 +378,8 @@ API に含まれる関数の詳しい情報は
   config_option_free, config_section_free_options, config_section_free,
   config_free, +
   config_get, config_get_plugin, config_is_set_plugin, config_set_plugin,
-  config_set_desc_plugin, config_unset_plugin
+  config_set_defaults_plugin, config_set_desc_plugin (deprecated),
+  config_unset_plugin
 | キー割り当て        |
   key_bind, key_unbind
 | 表示                |
diff --git a/doc/pl/weechat_scripting.pl.txt b/doc/pl/weechat_scripting.pl.txt
index 74ee024..f948521 100644
--- a/doc/pl/weechat_scripting.pl.txt
+++ b/doc/pl/weechat_scripting.pl.txt
@@ -390,7 +390,8 @@ Lista funkcji w API skryptów:
   config_option_free, config_section_free_options, config_section_free,
   config_free, +
   config_get, config_get_plugin, config_is_set_plugin, config_set_plugin,
-  config_set_desc_plugin, config_unset_plugin
+  config_set_defaults_plugin, config_set_desc_plugin (deprecated),
+  config_unset_plugin
 | przypisania klawiszy|
   key_bind, key_unbind
 | wyświetlanie        |
diff --git a/src/plugins/guile/weechat-guile-api.c b/src/plugins/guile/weechat-guile-api.c
index 9a55c03..c65a886 100644
--- a/src/plugins/guile/weechat-guile-api.c
+++ b/src/plugins/guile/weechat-guile-api.c
@@ -1662,6 +1662,26 @@ weechat_guile_api_config_set_plugin (SCM option, SCM value)
 }
 
 SCM
+weechat_guile_api_config_set_defaults_plugin (SCM option, SCM default_value,
+                                              SCM description)
+{
+    int rc;
+
+    API_FUNC(1, "config_set_defaults_plugin", API_RETURN_INT(WEECHAT_CONFIG_OPTION_SET_ERROR));
+    if (!scm_is_string (option) || !scm_is_string (default_value) ||
+        !scm_is_string (description))
+        API_WRONG_ARGS(API_RETURN_INT(WEECHAT_CONFIG_OPTION_SET_ERROR));
+
+    rc = plugin_script_api_config_set_defaults_plugin (weechat_guile_plugin,
+                                                       guile_current_script,
+                                                       API_SCM_TO_STRING(option),
+                                                       API_SCM_TO_STRING(default_value),
+                                                       API_SCM_TO_STRING(description));
+
+    API_RETURN_INT(rc);
+}
+
+SCM
 weechat_guile_api_config_set_desc_plugin (SCM option, SCM description)
 {
     API_FUNC(1, "config_set_desc_plugin", API_RETURN_ERROR);
@@ -4627,6 +4647,7 @@ weechat_guile_api_module_init (void *data)
     API_DEF_FUNC(config_get_plugin, 1);
     API_DEF_FUNC(config_is_set_plugin, 1);
     API_DEF_FUNC(config_set_plugin, 2);
+    API_DEF_FUNC(config_set_defaults_plugin, 3);
     API_DEF_FUNC(config_set_desc_plugin, 2);
     API_DEF_FUNC(config_unset_plugin, 1);
     API_DEF_FUNC(key_bind, 2);
diff --git a/src/plugins/lua/weechat-lua-api.c b/src/plugins/lua/weechat-lua-api.c
index 4624d9b..f3c724d 100644
--- a/src/plugins/lua/weechat-lua-api.c
+++ b/src/plugins/lua/weechat-lua-api.c
@@ -1785,6 +1785,28 @@ weechat_lua_api_config_set_plugin (lua_State *L)
 }
 
 static int
+weechat_lua_api_config_set_defaults_plugin (lua_State *L)
+{
+    const char *option, *default_value, *description;
+    int rc;
+
+    API_FUNC(1, "config_set_defaults_plugin", API_RETURN_INT(WEECHAT_CONFIG_OPTION_SET_ERROR));
+    if (lua_gettop (lua_current_interpreter) < 3)
+        API_WRONG_ARGS(API_RETURN_INT(WEECHAT_CONFIG_OPTION_SET_ERROR));
+
+    option = lua_tostring (lua_current_interpreter, -3);
+    default_value = lua_tostring (lua_current_interpreter, -2);
+    description = lua_tostring (lua_current_interpreter, -1);
+
+    rc = plugin_script_api_config_set_defaults_plugin (weechat_lua_plugin,
+                                                       lua_current_script,
+                                                       option, default_value,
+                                                       description);
+
+    API_RETURN_INT(rc);
+}
+
+static int
 weechat_lua_api_config_set_desc_plugin (lua_State *L)
 {
     const char *option, *description;
@@ -5503,6 +5525,7 @@ const struct luaL_Reg weechat_lua_api_funcs[] = {
     API_DEF_FUNC(config_get_plugin),
     API_DEF_FUNC(config_is_set_plugin),
     API_DEF_FUNC(config_set_plugin),
+    API_DEF_FUNC(config_set_defaults_plugin),
     API_DEF_FUNC(config_set_desc_plugin),
     API_DEF_FUNC(config_unset_plugin),
     API_DEF_FUNC(key_bind),
diff --git a/src/plugins/perl/weechat-perl-api.c b/src/plugins/perl/weechat-perl-api.c
index 3578e9f..f9bb315 100644
--- a/src/plugins/perl/weechat-perl-api.c
+++ b/src/plugins/perl/weechat-perl-api.c
@@ -1666,6 +1666,28 @@ XS (XS_weechat_api_config_set_plugin)
     API_RETURN_INT(rc);
 }
 
+XS (XS_weechat_api_config_set_defaults_plugin)
+{
+    char *option, *default_value, *description;
+    int rc;
+    dXSARGS;
+
+    API_FUNC(1, "config_set_defaults_plugin", API_RETURN_INT(WEECHAT_CONFIG_OPTION_SET_ERROR));
+    if (items < 2)
+        API_WRONG_ARGS(API_RETURN_INT(WEECHAT_CONFIG_OPTION_SET_ERROR));
+
+    option = SvPV_nolen (ST (0));
+    default_value = SvPV_nolen (ST (1));
+    description = SvPV_nolen (ST (2));
+
+    rc = plugin_script_api_config_set_defaults_plugin (weechat_perl_plugin,
+                                                       perl_current_script,
+                                                       option, default_value,
+                                                       description);
+
+    API_RETURN_INT(rc);
+}
+
 XS (XS_weechat_api_config_set_desc_plugin)
 {
     char *option, *description;
@@ -4860,6 +4882,7 @@ weechat_perl_api_init (pTHX)
     API_DEF_FUNC(config_get_plugin);
     API_DEF_FUNC(config_is_set_plugin);
     API_DEF_FUNC(config_set_plugin);
+    API_DEF_FUNC(config_set_defaults_plugin);
     API_DEF_FUNC(config_set_desc_plugin);
     API_DEF_FUNC(config_unset_plugin);
     API_DEF_FUNC(key_bind);
diff --git a/src/plugins/plugin-api.c b/src/plugins/plugin-api.c
index 6d06d92..652388c 100644
--- a/src/plugins/plugin-api.c
+++ b/src/plugins/plugin-api.c
@@ -178,7 +178,25 @@ plugin_api_config_set_plugin (struct t_weechat_plugin *plugin,
 }
 
 /*
+ * Sets default value and description of a plugin option.
+ */
+
+int
+plugin_api_config_set_defaults_plugin (struct t_weechat_plugin *plugin,
+                                       const char *option_name,
+                                       const char *default_value,
+                                       const char *description)
+{
+    if (!plugin || !option_name)
+        return WEECHAT_CONFIG_OPTION_SET_OPTION_NOT_FOUND;
+
+    return plugin_config_set_defaults (plugin->name, option_name,
+                                       default_value, description);
+}
+
+/*
  * Sets description of a plugin option.
+ * Note: Deprecated in favor of 'config_set_defaults_plugin'
  */
 
 void
diff --git a/src/plugins/plugin-api.h b/src/plugins/plugin-api.h
index 9e3035c..9541011 100644
--- a/src/plugins/plugin-api.h
+++ b/src/plugins/plugin-api.h
@@ -42,6 +42,11 @@ extern int plugin_api_config_is_set_plugin (struct t_weechat_plugin *plugin,
 extern int plugin_api_config_set_plugin (struct t_weechat_plugin *plugin,
                                          const char *option_name,
                                          const char *value);
+extern int plugin_api_config_set_defaults_plugin (struct t_weechat_plugin *plugin,
+                                                  const char *option_name,
+                                                  const char *default_value,
+                                                  const char *description);
+/* Note: Deprecated in favor of 'config_set_defaults_plugin' */
 extern void plugin_api_config_set_desc_plugin (struct t_weechat_plugin *plugin,
                                                const char *option_name,
                                                const char *description);
diff --git a/src/plugins/plugin-config.c b/src/plugins/plugin-config.c
index b83dce3..e60af8c 100644
--- a/src/plugins/plugin-config.c
+++ b/src/plugins/plugin-config.c
@@ -128,6 +128,85 @@ plugin_config_set (const char *plugin_name, const char *option_name,
     return rc;
 }
 
+void plugin_config_set_desc_internal (const char *option,
+                                      const char *description);
+
+/*
+ * Sets default value and description for a plugin option
+ * (this function must not be called directly).
+ */
+
+int
+plugin_config_set_defaults_internal (const char *option,
+                                     const char *default_value,
+                                     const char *description)
+{
+    int rc;
+    struct t_config_option *ptr_option;
+    char *default_value_dup;
+
+    ptr_option = config_file_search_option (plugin_config_file,
+                                            plugin_config_section_var,
+                                            option);
+
+    if (ptr_option)
+    {
+        default_value_dup = strdup(default_value);
+
+        if (!default_value_dup)
+            return WEECHAT_CONFIG_OPTION_SET_ERROR;
+
+        free (ptr_option->default_value);
+        ptr_option->default_value = default_value_dup;
+        plugin_config_set_desc_internal (option, description);
+
+        rc = (strcmp(ptr_option->value, default_value) == 0) ?
+             WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE :
+             WEECHAT_CONFIG_OPTION_SET_OK_CHANGED;
+    }
+    else
+    {
+        ptr_option = config_file_new_option (
+            plugin_config_file, plugin_config_section_var,
+            option, "string", NULL,
+            NULL, 0, 0, default_value, default_value, 0, NULL, NULL, NULL, NULL, NULL, NULL);
+        rc = (ptr_option) ? WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE : WEECHAT_CONFIG_OPTION_SET_ERROR;
+
+        plugin_config_set_desc_internal (option, description);
+    }
+
+
+    return rc;
+}
+
+/*
+ * Sets default value and description for a plugin option
+ * (option is created if not found).
+ */
+
+int
+plugin_config_set_defaults (const char *plugin_name, const char *option_name,
+                            const char *default_value, const char *description)
+{
+    int length, rc;
+    char *option_full_name;
+
+    length = strlen (plugin_name) + 1 + strlen (option_name) + 1;
+    option_full_name = malloc (length);
+
+    if (option_full_name == NULL)
+        return WEECHAT_CONFIG_OPTION_SET_ERROR;
+
+    snprintf (option_full_name, length, "%s.%s",
+              plugin_name, option_name);
+    string_tolower (option_full_name);
+    rc = plugin_config_set_defaults_internal (option_full_name, default_value,
+                                              description);
+    free (option_full_name);
+
+    return rc;
+}
+
 /*
  * Callback for changes on a description option.
  */
diff --git a/src/plugins/plugin-config.h b/src/plugins/plugin-config.h
index d623bf2..f3ee94d 100644
--- a/src/plugins/plugin-config.h
+++ b/src/plugins/plugin-config.h
@@ -29,6 +29,10 @@ extern struct t_config_option *plugin_config_search (const char *plugin_name,
                                                      const char *option_name);
 extern int plugin_config_set (const char *plugin_name, const char *option_name,
                               const char *value);
+extern int plugin_config_set_defaults (const char *plugin_name,
+                                       const char *option_name,
+                                       const char *default_value,
+                                       const char *description);
 extern void plugin_config_set_desc (const char *plugin_name,
                                     const char *option_name,
                                     const char *description);
diff --git a/src/plugins/plugin-script-api.c b/src/plugins/plugin-script-api.c
index 1762903..19ee255 100644
--- a/src/plugins/plugin-script-api.c
+++ b/src/plugins/plugin-script-api.c
@@ -1414,7 +1414,40 @@ plugin_script_api_config_set_plugin (struct t_weechat_plugin *weechat_plugin,
 }
 
 /*
+ * Sets default value and desciption of a script option
+ * (format in file is "plugin.script.option").
+ */
+
+int
+plugin_script_api_config_set_defaults_plugin (struct t_weechat_plugin *weechat_plugin,
+                                              struct t_plugin_script *script,
+                                              const char *option,
+                                              const char *default_value,
+                                              const char *description)
+{
+    char *option_fullname;
+    int return_code;
+
+    option_fullname = malloc ((strlen (script->name) +
+                               strlen (option) + 2));
+    if (!option_fullname)
+        return 0;
+
+    strcpy (option_fullname, script->name);
+    strcat (option_fullname, ".");
+    strcat (option_fullname, option);
+
+    return_code = weechat_config_set_defaults_plugin (option_fullname,
+                                                      default_value,
+                                                      description);
+    free (option_fullname);
+
+    return return_code;
+}
+
+/*
  * Sets description of a script option.
+ * Note: Deprecated in favor of 'config_set_defaults_plugin'
  */
 
 void
diff --git a/src/plugins/plugin-script-api.h b/src/plugins/plugin-script-api.h
index e32e454..aa2fcbc 100644
--- a/src/plugins/plugin-script-api.h
+++ b/src/plugins/plugin-script-api.h
@@ -341,6 +341,11 @@ extern int plugin_script_api_config_is_set_plugin (struct t_weechat_plugin *weec
 extern int plugin_script_api_config_set_plugin (struct t_weechat_plugin *weechat_plugin,
                                                 struct t_plugin_script *script,
                                                 const char *option, const char *value);
+extern int plugin_script_api_config_set_defaults_plugin (struct t_weechat_plugin *weechat_plugin,
+                                                         struct t_plugin_script *script,
+                                                         const char *option, const char *default_value,
+                                                         const char *description);
+/* Note: Deprecated in favor of 'config_set_defaults_plugin' */
 extern void plugin_script_api_config_set_desc_plugin (struct t_weechat_plugin *weechat_plugin,
                                                       struct t_plugin_script *script,
                                                       const char *option,
diff --git a/src/plugins/plugin.c b/src/plugins/plugin.c
index 207b891..86c7b87 100644
--- a/src/plugins/plugin.c
+++ b/src/plugins/plugin.c
@@ -631,6 +631,7 @@ plugin_load (const char *filename, int argc, char **argv)
         new_plugin->config_get_plugin = &plugin_api_config_get_plugin;
         new_plugin->config_is_set_plugin = &plugin_api_config_is_set_plugin;
         new_plugin->config_set_plugin = &plugin_api_config_set_plugin;
+        new_plugin->config_set_defaults_plugin = &plugin_api_config_set_defaults_plugin;
         new_plugin->config_set_desc_plugin = &plugin_api_config_set_desc_plugin;
         new_plugin->config_unset_plugin = &plugin_api_config_unset_plugin;
 
diff --git a/src/plugins/python/weechat-python-api.c b/src/plugins/python/weechat-python-api.c
index 6eef2ac..498aa6f 100644
--- a/src/plugins/python/weechat-python-api.c
+++ b/src/plugins/python/weechat-python-api.c
@@ -1733,6 +1733,28 @@ weechat_python_api_config_set_plugin (PyObject *self, PyObject *args)
 }
 
 static PyObject *
+weechat_python_api_config_set_defaults_plugin (PyObject *self, PyObject *args)
+{
+    char *option, *default_value, *description;
+    int rc;
+
+    API_FUNC(1, "config_set_defaults_plugin", API_RETURN_INT(WEECHAT_CONFIG_OPTION_SET_ERROR));
+    option = NULL;
+    default_value = NULL;
+    description = NULL;
+    if (!PyArg_ParseTuple (args, "sss", &option, &default_value, &description))
+        API_WRONG_ARGS(API_RETURN_INT(WEECHAT_CONFIG_OPTION_SET_ERROR));
+
+    rc = plugin_script_api_config_set_defaults_plugin (weechat_python_plugin,
+                                                       python_current_script,
+                                                       option,
+                                                       default_value,
+                                                       description);
+
+    API_RETURN_INT(rc);
+}
+
+static PyObject *
 weechat_python_api_config_set_desc_plugin (PyObject *self, PyObject *args)
 {
     char *option, *description;
@@ -5012,6 +5034,7 @@ PyMethodDef weechat_python_funcs[] =
     API_DEF_FUNC(config_get_plugin),
     API_DEF_FUNC(config_is_set_plugin),
     API_DEF_FUNC(config_set_plugin),
+    API_DEF_FUNC(config_set_defaults_plugin),
     API_DEF_FUNC(config_set_desc_plugin),
     API_DEF_FUNC(config_unset_plugin),
     API_DEF_FUNC(key_bind),
diff --git a/src/plugins/ruby/weechat-ruby-api.c b/src/plugins/ruby/weechat-ruby-api.c
index bbc2fbc..c04d852 100644
--- a/src/plugins/ruby/weechat-ruby-api.c
+++ b/src/plugins/ruby/weechat-ruby-api.c
@@ -2038,6 +2038,35 @@ weechat_ruby_api_config_set_plugin (VALUE class, VALUE option, VALUE value)
 }
 
 static VALUE
+weechat_ruby_api_config_set_defaults_plugin (VALUE class, VALUE option,
+                                             VALUE default_value,
+                                             VALUE description)
+{
+    char *c_option, *c_default_value, *c_description;
+    int rc;
+
+    API_FUNC(1, "config_set_defaults_plugin", API_RETURN_INT(WEECHAT_CONFIG_OPTION_SET_ERROR));
+    if (NIL_P (option) || NIL_P (default_value) || NIL_P (description))
+        API_WRONG_ARGS(API_RETURN_INT(WEECHAT_CONFIG_OPTION_SET_ERROR));
+
+    Check_Type (option, T_STRING);
+    Check_Type (default_value, T_STRING);
+    Check_Type (description, T_STRING);
+
+    c_option = StringValuePtr (option);
+    c_default_value = StringValuePtr (default_value);
+    c_description = StringValuePtr (description);
+
+    rc = plugin_script_api_config_set_defaults_plugin (weechat_ruby_plugin,
+                                                       ruby_current_script,
+                                                       c_option,
+                                                       c_default_value,
+                                                       c_description);
+
+    API_RETURN_INT(rc);
+}
+
+static VALUE
 weechat_ruby_api_config_set_desc_plugin (VALUE class, VALUE option,
                                          VALUE description)
 {
@@ -5946,6 +5975,7 @@ weechat_ruby_api_init (VALUE ruby_mWeechat)
     API_DEF_FUNC(config_get_plugin, 1);
     API_DEF_FUNC(config_is_set_plugin, 1);
     API_DEF_FUNC(config_set_plugin, 2);
+    API_DEF_FUNC(config_set_defaults_plugin, 3);
     API_DEF_FUNC(config_set_desc_plugin, 2);
     API_DEF_FUNC(config_unset_plugin, 1);
     API_DEF_FUNC(key_bind, 2);
diff --git a/src/plugins/tcl/weechat-tcl-api.c b/src/plugins/tcl/weechat-tcl-api.c
index ab6613f..5a0c787 100644
--- a/src/plugins/tcl/weechat-tcl-api.c
+++ b/src/plugins/tcl/weechat-tcl-api.c
@@ -2015,6 +2015,31 @@ weechat_tcl_api_config_set_plugin (ClientData clientData, Tcl_Interp *interp,
 }
 
 static int
+weechat_tcl_api_config_set_defaults_plugin (ClientData clientData, Tcl_Interp *interp,
+                                   int objc, Tcl_Obj *CONST objv[])
+{
+    Tcl_Obj *objp;
+    char *option, *default_value, *description;
+    int i, rc;
+
+    API_FUNC(1, "config_set_defaults_plugin", API_RETURN_INT(WEECHAT_CONFIG_OPTION_SET_ERROR));
+    if (objc < 4)
+        API_WRONG_ARGS(API_RETURN_INT(WEECHAT_CONFIG_OPTION_SET_ERROR));
+
+    option = Tcl_GetStringFromObj (objv[1], &i);
+    default_value = Tcl_GetStringFromObj (objv[2], &i);
+    description = Tcl_GetStringFromObj (objv[3], &i);
+
+    rc = plugin_script_api_config_set_defaults_plugin (weechat_tcl_plugin,
+                                                       tcl_current_script,
+                                                       option,
+                                                       default_value,
+                                                       description);
+
+    API_RETURN_INT(rc);
+}
+
+static int
 weechat_tcl_api_config_set_desc_plugin (ClientData clientData, Tcl_Interp *interp,
                                         int objc, Tcl_Obj *CONST objv[])
 {
@@ -5697,6 +5722,7 @@ void weechat_tcl_api_init (Tcl_Interp *interp)
     API_DEF_FUNC(config_get_plugin);
     API_DEF_FUNC(config_is_set_plugin);
     API_DEF_FUNC(config_set_plugin);
+    API_DEF_FUNC(config_set_defaults_plugin);
     API_DEF_FUNC(config_set_desc_plugin);
     API_DEF_FUNC(config_unset_plugin);
     API_DEF_FUNC(key_bind);
diff --git a/src/plugins/weechat-plugin.h b/src/plugins/weechat-plugin.h
index b965f01..7974e87 100644
--- a/src/plugins/weechat-plugin.h
+++ b/src/plugins/weechat-plugin.h
@@ -466,6 +466,10 @@ struct t_weechat_plugin
                                  const char *option_name);
     int (*config_set_plugin) (struct t_weechat_plugin *plugin,
                               const char *option_name, const char *value);
+    int (*config_set_defaults_plugin) (struct t_weechat_plugin *plugin,
+                                       const char *option_name,
+                                       const char *default_value,
+                                       const char *description);
     void (*config_set_desc_plugin) (struct t_weechat_plugin *plugin,
                                     const char *option_name,
                                     const char *description);
@@ -1276,6 +1280,10 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin);
 #define weechat_config_set_plugin(__option, __value)                    \
     weechat_plugin->config_set_plugin(weechat_plugin, __option,         \
                                       __value)
+#define weechat_config_set_defaults_plugin(__option, __default_value,   \
+                                           __description)               \
+    weechat_plugin->config_set_defaults_plugin(weechat_plugin, __option,         \
+                                      __default_value, __description)
 #define weechat_config_set_desc_plugin(__option, __description)         \
     weechat_plugin->config_set_desc_plugin(weechat_plugin, __option,    \
                                       __description)
-- 
1.7.9.5

