1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-08 10:43:13 +02:00

core: add option type "enum" (closes #1973)

The type "enum" replaces type "integer" when used with string values.

For compatibility, any option created with type "integer" and string values is
automatically created to "enum" on creation, with no error.
This commit is contained in:
Sébastien Helleu
2023-07-06 19:00:16 +02:00
parent 8f9d88edd0
commit 66cb9f6ea2
30 changed files with 1665 additions and 921 deletions
+44 -39
View File
@@ -123,8 +123,42 @@ fset_bar_item_fset_cb (const void *pointer, void *data,
-1);
}
if ((ptr_fset_option->string_values && ptr_fset_option->string_values[0])
|| (ptr_fset_option->type == FSET_OPTION_TYPE_INTEGER))
if (ptr_fset_option->type == FSET_OPTION_TYPE_INTEGER)
{
ptr_option = weechat_config_get (ptr_fset_option->name);
if (ptr_option)
{
weechat_string_dyn_concat (default_and_values,
weechat_color ("bar_fg"),
-1);
weechat_string_dyn_concat (default_and_values, ", ", -1);
weechat_string_dyn_concat (default_and_values, _("values:"), -1);
weechat_string_dyn_concat (default_and_values, " ", -1);
weechat_string_dyn_concat (
default_and_values,
weechat_color (
weechat_config_string (
fset_config_color_help_values)),
-1);
weechat_string_dyn_concat (default_and_values,
ptr_fset_option->min,
-1);
weechat_string_dyn_concat (default_and_values,
weechat_color ("bar_fg"),
-1);
weechat_string_dyn_concat (default_and_values, " ... ", -1);
weechat_string_dyn_concat (
default_and_values,
weechat_color (
weechat_config_string (
fset_config_color_help_values)),
-1);
weechat_string_dyn_concat (default_and_values,
ptr_fset_option->max,
-1);
}
}
else if (ptr_fset_option->type == FSET_OPTION_TYPE_ENUM)
{
ptr_option = weechat_config_get (ptr_fset_option->name);
if (ptr_option)
@@ -135,8 +169,7 @@ fset_bar_item_fset_cb (const void *pointer, void *data,
ptr_string_values = weechat_config_option_get_pointer (
ptr_option, "string_values");
}
if (ptr_string_values
|| (ptr_fset_option->type == FSET_OPTION_TYPE_INTEGER))
if (ptr_string_values)
{
weechat_string_dyn_concat (default_and_values,
weechat_color ("bar_fg"),
@@ -144,32 +177,17 @@ fset_bar_item_fset_cb (const void *pointer, void *data,
weechat_string_dyn_concat (default_and_values, ", ", -1);
weechat_string_dyn_concat (default_and_values, _("values:"), -1);
weechat_string_dyn_concat (default_and_values, " ", -1);
if (ptr_string_values)
for (i = 0; ptr_string_values[i]; i++)
{
for (i = 0; ptr_string_values[i]; i++)
if (i > 0)
{
if (i > 0)
{
weechat_string_dyn_concat (default_and_values,
weechat_color ("bar_fg"),
-1);
weechat_string_dyn_concat (default_and_values,
", ",
-1);
}
weechat_string_dyn_concat (
default_and_values,
weechat_color (
weechat_config_string (
fset_config_color_help_values)),
-1);
weechat_string_dyn_concat (default_and_values,
ptr_string_values[i],
weechat_color ("bar_fg"),
-1);
weechat_string_dyn_concat (default_and_values,
", ",
-1);
}
}
else
{
weechat_string_dyn_concat (
default_and_values,
weechat_color (
@@ -177,20 +195,7 @@ fset_bar_item_fset_cb (const void *pointer, void *data,
fset_config_color_help_values)),
-1);
weechat_string_dyn_concat (default_and_values,
ptr_fset_option->min,
-1);
weechat_string_dyn_concat (default_and_values,
weechat_color ("bar_fg"),
-1);
weechat_string_dyn_concat (default_and_values, " ... ", -1);
weechat_string_dyn_concat (
default_and_values,
weechat_color (
weechat_config_string (
fset_config_color_help_values)),
-1);
weechat_string_dyn_concat (default_and_values,
ptr_fset_option->max,
ptr_string_values[i],
-1);
}
}
+13 -12
View File
@@ -282,7 +282,8 @@ fset_command_fset (const void *pointer, void *data,
fset_command_get_option (&ptr_fset_option, &ptr_option);
if (ptr_fset_option &&
((ptr_fset_option->type == FSET_OPTION_TYPE_INTEGER)
|| (ptr_fset_option->type == FSET_OPTION_TYPE_COLOR)))
|| (ptr_fset_option->type == FSET_OPTION_TYPE_COLOR)
|| (ptr_fset_option->type == FSET_OPTION_TYPE_ENUM)))
{
fset_option_add_value (ptr_fset_option, ptr_option, value);
}
@@ -651,7 +652,7 @@ fset_command_init ()
"(\"end\" to select the last line)\n"
" -toggle: toggle the boolean value\n"
" -add: add \"value\" (which can be a negative number) "
"for integers and colors, set/append to value for other types "
"for integers, colors and enums, set/append to value for other types "
"(set for a negative value, append for a positive value)\n"
" -reset: reset the value of option\n"
" -unset: unset the option\n"
@@ -675,7 +676,7 @@ fset_command_init ()
" * show all options (no filter)\n"
" xxx show only options with \"xxx\" in name\n"
" f:xxx show only configuration file \"xxx\"\n"
" t:xxx show only type \"xxx\" (bool/int/str/col)\n"
" t:xxx show only type \"xxx\" (bool/int/str/col/enum)\n"
" d show only changed options\n"
" d:xxx show only changed options with \"xxx\" in "
"name\n"
@@ -692,7 +693,7 @@ fset_command_init ()
" c:xxx show only options matching the evaluated "
"condition \"xxx\", using following variables: file, section, "
"option, name, parent_name, type, type_en, type_short "
"(bool/int/str/col), type_tiny (b/i/s/c), default_value, "
"(bool/int/str/col/enum), type_tiny (b/i/s/c), default_value, "
"default_value_undef, value, quoted_value, value_undef, "
"value_changed, parent_value, min, max, description, description2, "
"description_en, description_en2, string_values\n"
@@ -712,7 +713,7 @@ fset_command_init ()
" - ${parent_name}: parent option name\n"
" - ${type}: option type (translated)\n"
" - ${type_en}: option type (in English)\n"
" - ${type_short}: short option type (bool/int/str/col)\n"
" - ${type_short}: short option type (bool/int/str/col/enum)\n"
" - ${type_tiny}: tiny option type (b/i/s/c)\n"
" - ${default_value}: option default value\n"
" - ${default_value_undef}: \"1\" if default value is null, "
@@ -731,8 +732,8 @@ fset_command_init ()
" - ${description_en}: option description (in English)\n"
" - ${description_en2}: option description (in English), "
"\"(no description)\" if there's no description\n"
" - ${string_values}: string values allowed for set of an "
"integer option using strings\n"
" - ${string_values}: string values allowed for set of an enum "
"option\n"
" - ${marked}: \"1\" if option is marked, otherwise \"0\"\n"
" - ${index}: index of option in list\n"
" - option data, with color but no spaces:\n"
@@ -762,10 +763,10 @@ fset_command_init ()
"\n"
"Keys and input to set options on fset buffer:\n"
" alt+space t toggle boolean value\n"
" alt+'-' - subtract 1 from value for integer/color, "
"set value for other types\n"
" alt+'+' + add 1 to value for integer/color, append "
"to value for other types\n"
" alt+'-' - subtract 1 from value for "
"integer/color/enum, set value for other types\n"
" alt+'+' + add 1 to value for integer/color/enum, "
"append to value for other types\n"
" alt+f, alt+r r reset value\n"
" alt+f, alt+u u unset value\n"
" alt+enter s set value\n"
@@ -805,7 +806,7 @@ fset_command_init ()
" right button toggle boolean (on/off) or "
"edit the option value\n"
" right button + drag left/right increase/decrease value "
"for integer/color, set/append to value for other types\n"
"for integer/color/enum, set/append to value for other types\n"
" right button + drag up/down mark/unmark multiple options\n"
"\n"
"Note: if input has one or more leading spaces, the following text "
+5 -3
View File
@@ -98,7 +98,7 @@ fset_mouse_focus_cb (const void *pointer, void *data, struct t_hashtable *info)
}
/*
* Get distance between x and x2 (as a positive integer);
* Get distance between x and x2 (as a positive integer).
*/
int
@@ -224,7 +224,8 @@ fset_mouse_hsignal_cb (const void *pointer, void *data, const char *signal,
{
distance = fset_mouse_get_distance_x (hashtable);
if ((ptr_fset_option->type == FSET_OPTION_TYPE_INTEGER)
|| (ptr_fset_option->type == FSET_OPTION_TYPE_COLOR))
|| (ptr_fset_option->type == FSET_OPTION_TYPE_COLOR)
|| (ptr_fset_option->type == FSET_OPTION_TYPE_ENUM))
{
snprintf (str_command, sizeof (str_command),
"/fset -add -%d",
@@ -240,7 +241,8 @@ fset_mouse_hsignal_cb (const void *pointer, void *data, const char *signal,
{
distance = fset_mouse_get_distance_x (hashtable);
if ((ptr_fset_option->type == FSET_OPTION_TYPE_INTEGER)
|| (ptr_fset_option->type == FSET_OPTION_TYPE_COLOR))
|| (ptr_fset_option->type == FSET_OPTION_TYPE_COLOR)
|| (ptr_fset_option->type == FSET_OPTION_TYPE_ENUM))
{
snprintf (str_command, sizeof (str_command),
"/fset -add %d",
+19 -15
View File
@@ -48,11 +48,11 @@ struct t_hook *fset_option_timer_hook = NULL;
/* types */
char *fset_option_type_string[FSET_OPTION_NUM_TYPES] =
{ N_("boolean"), N_("integer"), N_("string"), N_("color") };
{ N_("boolean"), N_("integer"), N_("string"), N_("color"), N_("enum") };
char *fset_option_type_string_short[FSET_OPTION_NUM_TYPES] =
{ "bool", "int", "str", "col" };
{ "bool", "int", "str", "col", "enum" };
char *fset_option_type_string_tiny[FSET_OPTION_NUM_TYPES] =
{ "b", "i", "s", "c" };
{ "b", "i", "s", "c", "e" };
/*
@@ -170,6 +170,18 @@ fset_option_set_value_string (struct t_config_option *option,
*value_string = strdup (*((int *)value) ? "on" : "off");
break;
case FSET_OPTION_TYPE_INTEGER:
snprintf (str_value, sizeof (str_value), "%d", *((int *)value));
*value_string = strdup (str_value);
break;
case FSET_OPTION_TYPE_STRING:
*value_string = strdup (
(default_value) ? weechat_config_string_default (option) : weechat_config_string (option));
break;
case FSET_OPTION_TYPE_COLOR:
*value_string = strdup (
(default_value) ? weechat_config_color_default (option) : weechat_config_color (option));
break;
case FSET_OPTION_TYPE_ENUM:
ptr_string_values = weechat_config_option_get_pointer (
option, "string_values");
if (ptr_string_values)
@@ -179,18 +191,9 @@ fset_option_set_value_string (struct t_config_option *option,
}
else
{
snprintf (str_value, sizeof (str_value), "%d", *((int *)value));
*value_string = strdup (str_value);
*value_string = strdup ("");
}
break;
case FSET_OPTION_TYPE_STRING:
*value_string = strdup (
(default_value) ? weechat_config_string_default (option) : weechat_config_string (option));
break;
case FSET_OPTION_TYPE_COLOR:
*value_string = strdup (
(default_value) ? weechat_config_color_default (option) : weechat_config_color (option));
break;
case FSET_OPTION_NUM_TYPES:
break;
}
@@ -1114,7 +1117,7 @@ fset_option_toggle_value (struct t_fset_option *fset_option,
}
/*
* Adds a value to an integer/color option.
* Adds a value to an integer/enum/color option.
*/
void
@@ -1126,7 +1129,8 @@ fset_option_add_value (struct t_fset_option *fset_option,
if (!fset_option || !option
|| ((fset_option->type != FSET_OPTION_TYPE_INTEGER)
&& (fset_option->type != FSET_OPTION_TYPE_COLOR)))
&& (fset_option->type != FSET_OPTION_TYPE_COLOR)
&& (fset_option->type != FSET_OPTION_TYPE_ENUM)))
return;
snprintf (str_value, sizeof (str_value),
+1
View File
@@ -30,6 +30,7 @@ enum t_fset_option_type
FSET_OPTION_TYPE_INTEGER,
FSET_OPTION_TYPE_STRING,
FSET_OPTION_TYPE_COLOR,
FSET_OPTION_TYPE_ENUM,
/* number of option types */
FSET_OPTION_NUM_TYPES,
};
+7
View File
@@ -120,6 +120,13 @@ enum t_irc_server_option
weechat_config_string(irc_config_server_default[__index]) \
: weechat_config_string_default(irc_config_server_default[__index])))
#define IRC_SERVER_OPTION_ENUM(__server, __index) \
((!weechat_config_option_is_null(__server->options[__index])) ? \
weechat_config_enum(__server->options[__index]) : \
((!weechat_config_option_is_null(irc_config_server_default[__index])) ? \
weechat_config_enum(irc_config_server_default[__index]) \
: weechat_config_enum_default(irc_config_server_default[__index])))
#define IRC_SERVER_DEFAULT_PORT_CLEARTEXT 6667
#define IRC_SERVER_DEFAULT_PORT_TLS 6697
+2
View File
@@ -761,6 +761,8 @@ plugin_load (const char *filename, int init_plugin, int argc, char **argv)
new_plugin->config_boolean_default = &config_file_option_boolean_default;
new_plugin->config_integer = &config_file_option_integer;
new_plugin->config_integer_default = &config_file_option_integer_default;
new_plugin->config_enum = &config_file_option_enum;
new_plugin->config_enum_default = &config_file_option_enum_default;
new_plugin->config_string = &config_file_option_string;
new_plugin->config_string_default = &config_file_option_string_default;
new_plugin->config_color = &config_file_option_color;
+41 -19
View File
@@ -602,49 +602,49 @@ def config_new_option(config_file: str, section: str, name: str, type: str, desc
::
# example
def option4_check_value_cb(data: str, option: str, value: str) -> int:
def option_str_check_value_cb(data: str, option: str, value: str) -> int:
# ...
return 1
# return 0
def option4_change_cb(data: str, option: str) -> None:
def option_str_change_cb(data: str, option: str) -> None:
# ...
def option4_delete_cb(data: str, option: str) -> None:
def option_str_delete_cb(data: str, option: str) -> None:
# ...
option1 = weechat.config_new_option(config_file, section, "option1", "boolean",
option_bool = weechat.config_new_option(config_file, section, "option_bool", "boolean",
"My option, type boolean",
"", 0, 0, "on", "on", 0,
"", "",
"", "",
"", "")
option2 = weechat.config_new_option(config_file, section, "option2", "integer",
option_int = weechat.config_new_option(config_file, section, "option_int", "integer",
"My option, type integer",
"", 0, 100, "15", "15", 0,
"", "",
"", "",
"", "")
option3 = weechat.config_new_option(config_file, section, "option3", "integer",
"My option, type integer (with string values)",
"top|bottom|left|right",
0, 0, "bottom", "bottom", 0,
option_str = weechat.config_new_option(config_file, section, "option_str", "string",
"My option, type string",
"", 0, 0, "test", "test", 1,
"option_str_check_value_cb", "",
"option_str_change_cb", "",
"option_str_delete_cb", "")
option_col = weechat.config_new_option(config_file, section, "option_col", "color",
"My option, type color",
"", 0, 0, "lightblue", "lightblue", 0,
"", "",
"", "",
"", "")
option4 = weechat.config_new_option(config_file, section, "option4", "string",
"My option, type string",
"", 0, 0, "test", "test", 1,
"option4_check_value_cb", "",
"option4_change_cb", "",
"option4_delete_cb", "")
option5 = weechat.config_new_option(config_file, section, "option5", "color",
"My option, type color",
"", 0, 0, "lightblue", "lightblue", 0,
option_enum = weechat.config_new_option(config_file, section, "option_enum", "enum",
"My option, type enum",
"top|bottom|left|right",
0, 0, "bottom", "bottom", 0,
"", "",
"", "",
"", "")
@@ -861,6 +861,28 @@ def config_color_default(option: str) -> str:
...
def config_enum(option: str) -> int:
"""`config_enum in WeeChat plugin API reference <https://weechat.org/doc/weechat/api/#_config_enum>`_
::
# example
option = weechat.config_get("plugin.section.option")
value = weechat.config_enum(option)
"""
...
def config_enum_default(option: str) -> int:
"""`config_enum_default in WeeChat plugin API reference <https://weechat.org/doc/weechat/api/#_config_enum_default>`_
::
# example
option = weechat.config_get("plugin.section.option")
value = weechat.config_enum_default(option)
"""
...
def config_write_option(config_file: str, option: str) -> int:
"""`config_write_option in WeeChat plugin API reference <https://weechat.org/doc/weechat/api/#_config_write_option>`_
::
+7 -1
View File
@@ -68,7 +68,7 @@ struct timeval;
* please change the date with current one; for a second change at same
* date, increment the 01, otherwise please keep 01.
*/
#define WEECHAT_PLUGIN_API_VERSION "20230220-01"
#define WEECHAT_PLUGIN_API_VERSION "20230706-01"
/* macros for defining plugin infos */
#define WEECHAT_PLUGIN_NAME(__name) \
@@ -639,6 +639,8 @@ struct t_weechat_plugin
int (*config_boolean_default) (struct t_config_option *option);
int (*config_integer) (struct t_config_option *option);
int (*config_integer_default) (struct t_config_option *option);
int (*config_enum) (struct t_config_option *option);
int (*config_enum_default) (struct t_config_option *option);
const char *(*config_string) (struct t_config_option *option);
const char *(*config_string_default) (struct t_config_option *option);
const char *(*config_color) (struct t_config_option *option);
@@ -1699,6 +1701,10 @@ extern int weechat_plugin_end (struct t_weechat_plugin *plugin);
(weechat_plugin->config_integer)(__option)
#define weechat_config_integer_default(__option) \
(weechat_plugin->config_integer_default)(__option)
#define weechat_config_enum(__option) \
(weechat_plugin->config_enum)(__option)
#define weechat_config_enum_default(__option) \
(weechat_plugin->config_enum_default)(__option)
#define weechat_config_string(__option) \
(weechat_plugin->config_string)(__option)
#define weechat_config_string_default(__option) \