diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2023-07-06 19:00:16 +0200 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2023-07-08 13:28:40 +0200 |
commit | 66cb9f6ea2e534887e73c885d3f131710c48382d (patch) | |
tree | 1d21dafd605395a57078f9d82d8f525bad588bb6 /src/core | |
parent | 8f9d88edd0106c92daf1ce624638f037f7e8fe0d (diff) | |
download | weechat-66cb9f6ea2e534887e73c885d3f131710c48382d.zip |
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.
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/wee-command.c | 172 | ||||
-rw-r--r-- | src/core/wee-completion.c | 86 | ||||
-rw-r--r-- | src/core/wee-config-file.c | 812 | ||||
-rw-r--r-- | src/core/wee-config-file.h | 6 | ||||
-rw-r--r-- | src/core/wee-doc.c | 22 | ||||
-rw-r--r-- | src/core/wee-eval.c | 8 |
6 files changed, 597 insertions, 509 deletions
diff --git a/src/core/wee-command.c b/src/core/wee-command.c index 9d3f809dc..47518fb2f 100644 --- a/src/core/wee-command.c +++ b/src/core/wee-command.c @@ -3128,99 +3128,35 @@ COMMAND_CALLBACK(help) } break; case CONFIG_OPTION_TYPE_INTEGER: - if (ptr_option->string_values) + gui_chat_printf (NULL, " %s: %s", + _("type"), _("integer")); + gui_chat_printf (NULL, " %s: %d .. %d", + _("values"), + ptr_option->min, ptr_option->max); + if (ptr_option->default_value) { - length = 0; - i = 0; - while (ptr_option->string_values[i]) - { - length += strlen (ptr_option->string_values[i]) + 5; - i++; - } - if (length > 0) - { - string = malloc (length); - if (string) - { - string[0] = '\0'; - i = 0; - while (ptr_option->string_values[i]) - { - strcat (string, "\""); - strcat (string, ptr_option->string_values[i]); - strcat (string, "\""); - if (ptr_option->string_values[i + 1]) - strcat (string, ", "); - i++; - } - gui_chat_printf (NULL, " %s: %s", - _("type"), _("string")); - gui_chat_printf (NULL, " %s: %s", - _("values"), string); - if (ptr_option->default_value) - { - gui_chat_printf (NULL, " %s: \"%s\"", - _("default value"), - ptr_option->string_values[CONFIG_INTEGER_DEFAULT(ptr_option)]); - } - else - { - gui_chat_printf (NULL, " %s: %s", - _("default value"), - _("(undefined)")); - } - if (ptr_option->value) - { - gui_chat_printf (NULL, - " %s: \"%s%s%s\"", - _("current value"), - GUI_COLOR(GUI_COLOR_CHAT_VALUE), - ptr_option->string_values[CONFIG_INTEGER(ptr_option)], - GUI_COLOR(GUI_COLOR_CHAT)); - } - else - { - gui_chat_printf (NULL, - " %s: %s", - _("current value"), - _("(undefined)")); - } - free (string); - } - } + gui_chat_printf (NULL, " %s: %d", + _("default value"), + CONFIG_INTEGER_DEFAULT(ptr_option)); } else { gui_chat_printf (NULL, " %s: %s", - _("type"), _("integer")); - gui_chat_printf (NULL, " %s: %d .. %d", - _("values"), - ptr_option->min, ptr_option->max); - if (ptr_option->default_value) - { - gui_chat_printf (NULL, " %s: %d", - _("default value"), - CONFIG_INTEGER_DEFAULT(ptr_option)); - } - else - { - gui_chat_printf (NULL, " %s: %s", - _("default value"), - _("(undefined)")); - } - if (ptr_option->value) - { - gui_chat_printf (NULL, " %s: %s%d", - _("current value"), - GUI_COLOR(GUI_COLOR_CHAT_VALUE), - CONFIG_INTEGER(ptr_option)); - } - else - { - gui_chat_printf (NULL, " %s: %s", - _("current value"), - _("(undefined)")); - } + _("default value"), + _("(undefined)")); + } + if (ptr_option->value) + { + gui_chat_printf (NULL, " %s: %s%d", + _("current value"), + GUI_COLOR(GUI_COLOR_CHAT_VALUE), + CONFIG_INTEGER(ptr_option)); + } + else + { + gui_chat_printf (NULL, " %s: %s", + _("current value"), + _("(undefined)")); } break; case CONFIG_OPTION_TYPE_STRING: @@ -3306,6 +3242,66 @@ COMMAND_CALLBACK(help) _("(undefined)")); } break; + case CONFIG_OPTION_TYPE_ENUM: + length = 0; + i = 0; + while (ptr_option->string_values[i]) + { + length += strlen (ptr_option->string_values[i]) + 5; + i++; + } + if (length > 0) + { + string = malloc (length); + if (string) + { + string[0] = '\0'; + i = 0; + while (ptr_option->string_values[i]) + { + strcat (string, "\""); + strcat (string, ptr_option->string_values[i]); + strcat (string, "\""); + if (ptr_option->string_values[i + 1]) + strcat (string, ", "); + i++; + } + gui_chat_printf (NULL, " %s: %s", + _("type"), _("enum")); + gui_chat_printf (NULL, " %s: %s", + _("values"), string); + if (ptr_option->default_value) + { + gui_chat_printf (NULL, " %s: \"%s\"", + _("default value"), + ptr_option->string_values[CONFIG_ENUM_DEFAULT(ptr_option)]); + } + else + { + gui_chat_printf (NULL, " %s: %s", + _("default value"), + _("(undefined)")); + } + if (ptr_option->value) + { + gui_chat_printf (NULL, + " %s: \"%s%s%s\"", + _("current value"), + GUI_COLOR(GUI_COLOR_CHAT_VALUE), + ptr_option->string_values[CONFIG_ENUM(ptr_option)], + GUI_COLOR(GUI_COLOR_CHAT)); + } + else + { + gui_chat_printf (NULL, + " %s: %s", + _("current value"), + _("(undefined)")); + } + free (string); + } + } + break; case CONFIG_NUM_OPTION_TYPES: break; } diff --git a/src/core/wee-completion.c b/src/core/wee-completion.c index d6e775be2..cbd068b4d 100644 --- a/src/core/wee-completion.c +++ b/src/core/wee-completion.c @@ -1461,59 +1461,31 @@ completion_list_add_config_option_values_cb (const void *pointer, void *data, } break; case CONFIG_OPTION_TYPE_INTEGER: - if (option_found->string_values) - { - for (i = 0; option_found->string_values[i]; i++) - { - gui_completion_list_add (completion, - option_found->string_values[i], - 0, WEECHAT_LIST_POS_SORT); - } - gui_completion_list_add (completion, "++1", - 0, WEECHAT_LIST_POS_END); + if (option_found->value && CONFIG_INTEGER(option_found) > option_found->min) gui_completion_list_add (completion, "--1", - 0, WEECHAT_LIST_POS_END); - if (option_found->value) - { - gui_completion_list_add (completion, - option_found->string_values[CONFIG_INTEGER(option_found)], - 0, WEECHAT_LIST_POS_BEGINNING); - } - else + 0, WEECHAT_LIST_POS_BEGINNING); + if (option_found->value && CONFIG_INTEGER(option_found) < option_found->max) + gui_completion_list_add (completion, "++1", + 0, WEECHAT_LIST_POS_BEGINNING); + if (option_found->value) + { + length = 64; + value_string = malloc (length); + if (value_string) { + snprintf (value_string, length, + "%d", CONFIG_INTEGER(option_found)); gui_completion_list_add (completion, - WEECHAT_CONFIG_OPTION_NULL, + value_string, 0, WEECHAT_LIST_POS_BEGINNING); + free (value_string); } } else { - if (option_found->value && CONFIG_INTEGER(option_found) > option_found->min) - gui_completion_list_add (completion, "--1", - 0, WEECHAT_LIST_POS_BEGINNING); - if (option_found->value && CONFIG_INTEGER(option_found) < option_found->max) - gui_completion_list_add (completion, "++1", - 0, WEECHAT_LIST_POS_BEGINNING); - if (option_found->value) - { - length = 64; - value_string = malloc (length); - if (value_string) - { - snprintf (value_string, length, - "%d", CONFIG_INTEGER(option_found)); - gui_completion_list_add (completion, - value_string, - 0, WEECHAT_LIST_POS_BEGINNING); - free (value_string); - } - } - else - { - gui_completion_list_add (completion, - WEECHAT_CONFIG_OPTION_NULL, - 0, WEECHAT_LIST_POS_BEGINNING); - } + gui_completion_list_add (completion, + WEECHAT_CONFIG_OPTION_NULL, + 0, WEECHAT_LIST_POS_BEGINNING); } break; case CONFIG_OPTION_TYPE_STRING: @@ -1567,6 +1539,30 @@ completion_list_add_config_option_values_cb (const void *pointer, void *data, 0, WEECHAT_LIST_POS_BEGINNING); } break; + case CONFIG_OPTION_TYPE_ENUM: + for (i = 0; option_found->string_values[i]; i++) + { + gui_completion_list_add (completion, + option_found->string_values[i], + 0, WEECHAT_LIST_POS_SORT); + } + gui_completion_list_add (completion, "++1", + 0, WEECHAT_LIST_POS_END); + gui_completion_list_add (completion, "--1", + 0, WEECHAT_LIST_POS_END); + if (option_found->value) + { + gui_completion_list_add (completion, + option_found->string_values[CONFIG_ENUM(option_found)], + 0, WEECHAT_LIST_POS_BEGINNING); + } + else + { + gui_completion_list_add (completion, + WEECHAT_CONFIG_OPTION_NULL, + 0, WEECHAT_LIST_POS_BEGINNING); + } + break; case CONFIG_NUM_OPTION_TYPES: break; } diff --git a/src/core/wee-config-file.c b/src/core/wee-config-file.c index fc2123ffa..87ca9b47b 100644 --- a/src/core/wee-config-file.c +++ b/src/core/wee-config-file.c @@ -53,7 +53,7 @@ struct t_config_file *config_files = NULL; struct t_config_file *last_config_file = NULL; char *config_option_type_string[CONFIG_NUM_OPTION_TYPES] = -{ N_("boolean"), N_("integer"), N_("string"), N_("color") }; +{ N_("boolean"), N_("integer"), N_("string"), N_("color"), N_("enum") }; char *config_boolean_true[] = { "on", "yes", "y", "true", "t", "1", NULL }; char *config_boolean_false[] = { "off", "no", "n", "false", "f", "0", NULL }; @@ -547,17 +547,9 @@ config_file_hook_config_exec (struct t_config_option *option) "on" : "off"); break; case CONFIG_OPTION_TYPE_INTEGER: - if (option->string_values) - { - hook_config_exec (option_full_name, - option->string_values[CONFIG_INTEGER(option)]); - } - else - { - snprintf (str_value, sizeof (str_value), - "%d", CONFIG_INTEGER(option)); - hook_config_exec (option_full_name, str_value); - } + snprintf (str_value, sizeof (str_value), + "%d", CONFIG_INTEGER(option)); + hook_config_exec (option_full_name, str_value); break; case CONFIG_OPTION_TYPE_STRING: hook_config_exec (option_full_name, (char *)option->value); @@ -566,6 +558,10 @@ config_file_hook_config_exec (struct t_config_option *option) hook_config_exec (option_full_name, gui_color_get_name (CONFIG_COLOR(option))); break; + case CONFIG_OPTION_TYPE_ENUM: + hook_config_exec (option_full_name, + option->string_values[CONFIG_ENUM(option)]); + break; case CONFIG_NUM_OPTION_TYPES: break; } @@ -766,6 +762,22 @@ config_file_new_option (struct t_config_file *config_file, goto error; } + /* + * compatibility with versions < 4.1.0: force enum type for an integer + * with string values + */ + if ((var_type == CONFIG_OPTION_TYPE_INTEGER) + && string_values && string_values[0]) + { + var_type = CONFIG_OPTION_TYPE_ENUM; + } + + if ((var_type == CONFIG_OPTION_TYPE_ENUM) + && (!string_values || !string_values[0])) + { + goto error; + } + if (!null_value_allowed) { if (default_value && !value) @@ -804,7 +816,7 @@ config_file_new_option (struct t_config_file *config_file, new_option->default_value = malloc (sizeof (int)); if (!new_option->default_value) goto error; - CONFIG_INTEGER_DEFAULT(new_option) = int_value; + CONFIG_BOOLEAN_DEFAULT(new_option) = int_value; } if (value) { @@ -812,97 +824,41 @@ config_file_new_option (struct t_config_file *config_file, new_option->value = malloc (sizeof (int)); if (!new_option->value) goto error; - CONFIG_INTEGER(new_option) = int_value; + CONFIG_BOOLEAN(new_option) = int_value; } break; case CONFIG_OPTION_TYPE_INTEGER: - if (string_values && string_values[0]) + new_option->min = min; + new_option->max = max; + if (default_value) { - new_option->string_values = string_split ( - string_values, - "|", - NULL, - WEECHAT_STRING_SPLIT_STRIP_LEFT - | WEECHAT_STRING_SPLIT_STRIP_RIGHT - | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS, - 0, - &argc); - if (!new_option->string_values) + error = NULL; + number = strtol (default_value, &error, 10); + if (!error || error[0]) + number = 0; + if (number < min) + number = min; + else if (number > max) + number = max; + new_option->default_value = malloc (sizeof (int)); + if (!new_option->default_value) goto error; + CONFIG_INTEGER_DEFAULT(new_option) = number; } - if (new_option->string_values) - { - new_option->min = 0; - new_option->max = (argc == 0) ? 0 : argc - 1; - if (default_value) - { - index_value = 0; - for (i = 0; i < argc; i++) - { - if (strcmp (new_option->string_values[i], - default_value) == 0) - { - index_value = i; - break; - } - } - new_option->default_value = malloc (sizeof (int)); - if (!new_option->default_value) - goto error; - CONFIG_INTEGER_DEFAULT(new_option) = index_value; - } - if (value) - { - index_value = 0; - for (i = 0; i < argc; i++) - { - if (strcmp (new_option->string_values[i], - value) == 0) - { - index_value = i; - break; - } - } - new_option->value = malloc (sizeof (int)); - if (!new_option->value) - goto error; - CONFIG_INTEGER(new_option) = index_value; - } - } - else + if (value) { - new_option->min = min; - new_option->max = max; - if (default_value) - { - error = NULL; - number = strtol (default_value, &error, 10); - if (!error || error[0]) - number = 0; - if (number < min) - number = min; - else if (number > max) - number = max; - new_option->default_value = malloc (sizeof (int)); - if (!new_option->default_value) - goto error; - CONFIG_INTEGER_DEFAULT(new_option) = number; - } - if (value) - { - error = NULL; - number = strtol (value, &error, 10); - if (!error || error[0]) - number = 0; - if (number < min) - number = min; - else if (number > max) - number = max; - new_option->value = malloc (sizeof (int)); - if (!new_option->value) - goto error; - CONFIG_INTEGER(new_option) = number; - } + error = NULL; + number = strtol (value, &error, 10); + if (!error || error[0]) + number = 0; + if (number < min) + number = min; + else if (number > max) + number = max; + new_option->value = malloc (sizeof (int)); + if (!new_option->value) + goto error; + CONFIG_INTEGER(new_option) = number; } break; case CONFIG_OPTION_TYPE_STRING: @@ -930,7 +886,7 @@ config_file_new_option (struct t_config_file *config_file, if (!new_option->default_value) goto error; if (!gui_color_assign (new_option->default_value, default_value)) - CONFIG_INTEGER_DEFAULT(new_option) = 0; + CONFIG_COLOR_DEFAULT(new_option) = 0; } if (value) { @@ -938,7 +894,56 @@ config_file_new_option (struct t_config_file *config_file, if (!new_option->value) goto error; if (!gui_color_assign (new_option->value, value)) - CONFIG_INTEGER(new_option) = 0; + CONFIG_COLOR(new_option) = 0; + } + break; + case CONFIG_OPTION_TYPE_ENUM: + new_option->string_values = string_split ( + string_values, + "|", + NULL, + WEECHAT_STRING_SPLIT_STRIP_LEFT + | WEECHAT_STRING_SPLIT_STRIP_RIGHT + | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS, + 0, + &argc); + if (!new_option->string_values) + goto error; + new_option->min = 0; + new_option->max = (argc == 0) ? 0 : argc - 1; + if (default_value) + { + index_value = 0; + for (i = 0; i < argc; i++) + { + if (strcmp (new_option->string_values[i], + default_value) == 0) + { + index_value = i; + break; + } + } + new_option->default_value = malloc (sizeof (int)); + if (!new_option->default_value) + goto error; + CONFIG_ENUM_DEFAULT(new_option) = index_value; + } + if (value) + { + index_value = 0; + for (i = 0; i < argc; i++) + { + if (strcmp (new_option->string_values[i], + value) == 0) + { + index_value = i; + break; + } + } + new_option->value = malloc (sizeof (int)); + if (!new_option->value) + goto error; + CONFIG_ENUM(new_option) = index_value; } break; case CONFIG_NUM_OPTION_TYPES: @@ -1316,7 +1321,7 @@ config_file_option_reset (struct t_config_option *option, int run_callback) { option->value = malloc (sizeof (int)); if (option->value) - CONFIG_INTEGER(option) = 0; + CONFIG_COLOR(option) = 0; else break; } @@ -1328,6 +1333,23 @@ config_file_option_reset (struct t_config_option *option, int run_callback) rc = WEECHAT_CONFIG_OPTION_SET_OK_CHANGED; } break; + case CONFIG_OPTION_TYPE_ENUM: + if (!option->value) + { + option->value = malloc (sizeof (int)); + if (option->value) + CONFIG_ENUM(option) = 0; + else + break; + } + if (CONFIG_ENUM(option) == CONFIG_ENUM_DEFAULT(option)) + rc = WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE; + else + { + CONFIG_ENUM(option) = CONFIG_ENUM_DEFAULT(option); + rc = WEECHAT_CONFIG_OPTION_SET_OK_CHANGED; + } + break; case CONFIG_NUM_OPTION_TYPES: break; } @@ -1464,117 +1486,58 @@ config_file_option_set (struct t_config_option *option, const char *value, old_value = CONFIG_INTEGER(option); if (option->value) { - if (option->string_values) + new_value_ok = 0; + if (strncmp (value, "++", 2) == 0) { - value_int = -1; - if (strncmp (value, "++", 2) == 0) - { - error = NULL; - number = strtol (value + 2, &error, 10); - if (error && !error[0]) - { - number = number % (option->max + 1); - value_int = (old_value + number) % - (option->max + 1); - } - } - else if (strncmp (value, "--", 2) == 0) - { - error = NULL; - number = strtol (value + 2, &error, 10); - if (error && !error[0]) - { - number = number % (option->max + 1); - value_int = (old_value + (option->max + 1) - number) % - (option->max + 1); - } - } - else - { - for (i = 0; option->string_values[i]; i++) - { - if (strcmp (option->string_values[i], value) == 0) - { - value_int = i; - break; - } - } - } - if (value_int >= 0) + error = NULL; + number = strtol (value + 2, &error, 10); + if (error && !error[0]) { - if (old_value_was_null - || (value_int != old_value)) - { - CONFIG_INTEGER(option) = value_int; - rc = WEECHAT_CONFIG_OPTION_SET_OK_CHANGED; - } - else - rc = WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE; + value_int = old_value + number; + if (value_int <= option->max) + new_value_ok = 1; } - else + } + else if (strncmp (value, "--", 2) == 0) + { + error = NULL; + number = strtol (value + 2, &error, 10); + if (error && !error[0]) { - if (old_value_was_null) - { - free (option->value); - option->value = NULL; - } + value_int = old_value - number; + if (value_int >= option->min) + new_value_ok = 1; } } else { - new_value_ok = 0; - if (strncmp (value, "++", 2) == 0) - { - error = NULL; - number = strtol (value + 2, &error, 10); - if (error && !error[0]) - { - value_int = old_value + number; - if (value_int <= option->max) - new_value_ok = 1; - } - } - else if (strncmp (value, "--", 2) == 0) - { - error = NULL; - number = strtol (value + 2, &error, 10); - if (error && !error[0]) - { - value_int = old_value - number; - if (value_int >= option->min) - new_value_ok = 1; - } - } - else + error = NULL; + number = strtol (value, &error, 10); + if (error && !error[0]) { - error = NULL; - number = strtol (value, &error, 10); - if (error && !error[0]) - { - value_int = number; - if ((value_int >= option->min) - && (value_int <= option->max)) - new_value_ok = 1; - } + value_int = number; + if ((value_int >= option->min) + && (value_int <= option->max)) + new_value_ok = 1; } - if (new_value_ok) + } + if (new_value_ok) + { + if (old_value_was_null + || (value_int != old_value)) { - if (old_value_was_null - || (value_int != old_value)) - { - CONFIG_INTEGER(option) = value_int; - rc = WEECHAT_CONFIG_OPTION_SET_OK_CHANGED; - } - else - rc = WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE; + CONFIG_INTEGER(option) = value_int; + rc = WEECHAT_CONFIG_OPTION_SET_OK_CHANGED; } else + rc = WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE; + } + else + { + if (old_value_was_null) { - if (old_value_was_null) - { - free (option->value); - option->value = NULL; - } + free (option->value); + option->value = NULL; } } } @@ -1653,6 +1616,69 @@ config_file_option_set (struct t_config_option *option, const char *value, } } break; + case CONFIG_OPTION_TYPE_ENUM: + old_value = 0; + if (!option->value) + option->value = malloc (sizeof (int)); + else + old_value = CONFIG_ENUM(option); + if (option->value) + { + value_int = -1; + if (strncmp (value, "++", 2) == 0) + { + error = NULL; + number = strtol (value + 2, &error, 10); + if (error && !error[0]) + { + number = number % (option->max + 1); + value_int = (old_value + number) % + (option->max + 1); + } + } + else if (strncmp (value, "--", 2) == 0) + { + error = NULL; + number = strtol (value + 2, &error, 10); + if (error && !error[0]) + { + number = number % (option->max + 1); + value_int = (old_value + (option->max + 1) - number) % + (option->max + 1); + } + } + else + { + for (i = 0; option->string_values[i]; i++) + { + if (strcmp (option->string_values[i], value) == 0) + { + value_int = i; + break; + } + } + } + if (value_int >= 0) + { + if (old_value_was_null + || (value_int != old_value)) + { + CONFIG_ENUM(option) = value_int; + rc = WEECHAT_CONFIG_OPTION_SET_OK_CHANGED; + } + else + rc = WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE; + } + else + { + if (old_value_was_null) + { + free (option->value); + option->value = NULL; + } + } + } + break; case CONFIG_NUM_OPTION_TYPES: break; } @@ -1741,6 +1767,10 @@ config_file_option_toggle (struct t_config_option *option, if (!values) goto end; break; + case CONFIG_OPTION_TYPE_ENUM: + if (!values) + goto end; + break; case CONFIG_NUM_OPTION_TYPES: /* make C compiler happy */ break; @@ -1929,117 +1959,58 @@ config_file_option_set_default (struct t_config_option *option, old_value = CONFIG_INTEGER_DEFAULT(option); if (option->default_value) { - if (option->string_values) + new_value_ok = 0; + if (strncmp (value, "++", 2) == 0) { - value_int = -1; - if (strncmp (value, "++", 2) == 0) - { - error = NULL; - number = strtol (value + 2, &error, 10); - if (error && !error[0]) - { - number = number % (option->max + 1); - value_int = (old_value + number) % - (option->max + 1); - } - } - else if (strncmp (value, "--", 2) == 0) - { - error = NULL; - number = strtol (value + 2, &error, 10); - if (error && !error[0]) - { - number = number % (option->max + 1); - value_int = (old_value + (option->max + 1) - number) % - (option->max + 1); - } - } - else - { - for (i = 0; option->string_values[i]; i++) - { - if (strcmp (option->string_values[i], value) == 0) - { - value_int = i; - break; - } - } - } - if (value_int >= 0) + error = NULL; + number = strtol (value + 2, &error, 10); + if (error && !error[0]) { - if (old_value_was_null - || (value_int != old_value)) - { - CONFIG_INTEGER_DEFAULT(option) = value_int; - rc = WEECHAT_CONFIG_OPTION_SET_OK_CHANGED; - } - else - rc = WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE; + value_int = old_value + number; + if (value_int <= option->max) + new_value_ok = 1; } - else + } + else if (strncmp (value, "--", 2) == 0) + { + error = NULL; + number = strtol (value + 2, &error, 10); + if (error && !error[0]) { - if (old_value_was_null) - { - free (option->default_value); - option->default_value = NULL; - } + value_int = old_value - number; + if (value_int >= option->min) + new_value_ok = 1; } } else { - new_value_ok = 0; - if (strncmp (value, "++", 2) == 0) - { - error = NULL; - number = strtol (value + 2, &error, 10); - if (error && !error[0]) - { - value_int = old_value + number; - if (value_int <= option->max) - new_value_ok = 1; - } - } - else if (strncmp (value, "--", 2) == 0) - { - error = NULL; - number = strtol (value + 2, &error, 10); - if (error && !error[0]) - { - value_int = old_value - number; - if (value_int >= option->min) - new_value_ok = 1; - } - } - else + error = NULL; + number = strtol (value, &error, 10); + if (error && !error[0]) { - error = NULL; - number = strtol (value, &error, 10); - if (error && !error[0]) - { - value_int = number; - if ((value_int >= option->min) - && (value_int <= option->max)) - new_value_ok = 1; - } + value_int = number; + if ((value_int >= option->min) + && (value_int <= option->max)) + new_value_ok = 1; } - if (new_value_ok) + } + if (new_value_ok) + { + if (old_value_was_null + || (value_int != old_value)) { - if (old_value_was_null - || (value_int != old_value)) - { - CONFIG_INTEGER_DEFAULT(option) = value_int; - rc = WEECHAT_CONFIG_OPTION_SET_OK_CHANGED; - } - else - rc = WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE; + CONFIG_INTEGER_DEFAULT(option) = value_int; + rc = WEECHAT_CONFIG_OPTION_SET_OK_CHANGED; } else + rc = WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE; + } + else + { + if (old_value_was_null) { - if (old_value_was_null) - { - free (option->default_value); - option->default_value = NULL; - } + free (option->default_value); + option->default_value = NULL; } } } @@ -2118,6 +2089,69 @@ config_file_option_set_default (struct t_config_option *option, } } break; + case CONFIG_OPTION_TYPE_ENUM: + old_value = 0; + if (!option->default_value) + option->default_value = malloc (sizeof (int)); + else + old_value = CONFIG_ENUM_DEFAULT(option); + if (option->default_value) + { + value_int = -1; + if (strncmp (value, "++", 2) == 0) + { + error = NULL; + number = strtol (value + 2, &error, 10); + if (error && !error[0]) + { + number = number % (option->max + 1); + value_int = (old_value + number) % + (option->max + 1); + } + } + else if (strncmp (value, "--", 2) == 0) + { + error = NULL; + number = strtol (value + 2, &error, 10); + if (error && !error[0]) + { + number = number % (option->max + 1); + value_int = (old_value + (option->max + 1) - number) % + (option->max + 1); + } + } + else + { + for (i = 0; option->string_values[i]; i++) + { + if (strcmp (option->string_values[i], value) == 0) + { + value_int = i; + break; + } + } + } + if (value_int >= 0) + { + if (old_value_was_null + || (value_int != old_value)) + { + CONFIG_ENUM_DEFAULT(option) = value_int; + rc = WEECHAT_CONFIG_OPTION_SET_OK_CHANGED; + } + else + rc = WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE; + } + else + { + if (old_value_was_null) + { + free (option->default_value); + option->default_value = NULL; + } + } + } + break; case CONFIG_NUM_OPTION_TYPES: break; } @@ -2359,33 +2393,15 @@ config_file_option_value_to_string (struct t_config_option *option, return value; break; case CONFIG_OPTION_TYPE_INTEGER: - if (option->string_values) - { - ptr_value = (default_value) ? - option->string_values[CONFIG_INTEGER_DEFAULT(option)] : - option->string_values[CONFIG_INTEGER(option)]; - length = strlen (ptr_value) + ((use_colors) ? 64 : 0) + 1; - value = malloc (length); - if (!value) - return NULL; - snprintf (value, length, - "%s%s", - (use_colors) ? GUI_COLOR(GUI_COLOR_CHAT_VALUE) : "", - ptr_value); - return value; - } - else - { - length = 31 + ((use_colors) ? 64 : 0) + 1; - value = malloc (length); - if (!value) - return NULL; - snprintf (value, length, - "%s%d", - (use_colors) ? GUI_COLOR(GUI_COLOR_CHAT_VALUE) : "", - (default_value) ? CONFIG_INTEGER_DEFAULT(option) : CONFIG_INTEGER(option)); - return value; - } + length = 31 + ((use_colors) ? 64 : 0) + 1; + value = malloc (length); + if (!value) + return NULL; + snprintf (value, length, + "%s%d", + (use_colors) ? GUI_COLOR(GUI_COLOR_CHAT_VALUE) : "", + (default_value) ? CONFIG_INTEGER_DEFAULT(option) : CONFIG_INTEGER(option)); + return value; break; case CONFIG_OPTION_TYPE_STRING: ptr_value = (default_value) ? CONFIG_STRING_DEFAULT(option) : CONFIG_STRING(option); @@ -2418,6 +2434,20 @@ config_file_option_value_to_string (struct t_config_option *option, ptr_value); return value; break; + case CONFIG_OPTION_TYPE_ENUM: + ptr_value = (default_value) ? + option->string_values[CONFIG_ENUM_DEFAULT(option)] : + option->string_values[CONFIG_ENUM(option)]; + length = strlen (ptr_value) + ((use_colors) ? 64 : 0) + 1; + value = malloc (length); + if (!value) + return NULL; + snprintf (value, length, + "%s%s", + (use_colors) ? GUI_COLOR(GUI_COLOR_CHAT_VALUE) : "", + ptr_value); + return value; + break; case CONFIG_NUM_OPTION_TYPES: /* make C compiler happy */ break; @@ -2562,6 +2592,8 @@ int config_file_option_has_changed (struct t_config_option *option) return strcmp (CONFIG_STRING(option), CONFIG_STRING_DEFAULT(option)) != 0; case CONFIG_OPTION_TYPE_COLOR: return CONFIG_COLOR(option) != CONFIG_COLOR_DEFAULT(option); + case CONFIG_OPTION_TYPE_ENUM: + return CONFIG_ENUM(option) != CONFIG_ENUM_DEFAULT(option); case CONFIG_NUM_OPTION_TYPES: /* make C compiler happy */ break; @@ -2673,10 +2705,13 @@ config_file_option_integer (struct t_config_option *option) else return 0; case CONFIG_OPTION_TYPE_INTEGER: - case CONFIG_OPTION_TYPE_COLOR: return CONFIG_INTEGER(option); case CONFIG_OPTION_TYPE_STRING: return 0; + case CONFIG_OPTION_TYPE_COLOR: + return CONFIG_COLOR(option); + case CONFIG_OPTION_TYPE_ENUM: + return CONFIG_ENUM(option); case CONFIG_NUM_OPTION_TYPES: break; } @@ -2701,10 +2736,13 @@ config_file_option_integer_default (struct t_config_option *option) else return 0; case CONFIG_OPTION_TYPE_INTEGER: - case CONFIG_OPTION_TYPE_COLOR: return CONFIG_INTEGER_DEFAULT(option); case CONFIG_OPTION_TYPE_STRING: return 0; + case CONFIG_OPTION_TYPE_COLOR: + return CONFIG_COLOR_DEFAULT(option); + case CONFIG_OPTION_TYPE_ENUM: + return CONFIG_ENUM_DEFAULT(option); case CONFIG_NUM_OPTION_TYPES: break; } @@ -2729,13 +2767,13 @@ config_file_option_string (struct t_config_option *option) else return config_boolean_false[0]; case CONFIG_OPTION_TYPE_INTEGER: - if (option->string_values) - return option->string_values[CONFIG_INTEGER(option)]; return NULL; case CONFIG_OPTION_TYPE_STRING: return CONFIG_STRING(option); case CONFIG_OPTION_TYPE_COLOR: return gui_color_get_name (CONFIG_COLOR(option)); + case CONFIG_OPTION_TYPE_ENUM: + return option->string_values[CONFIG_ENUM(option)]; case CONFIG_NUM_OPTION_TYPES: return NULL; } @@ -2760,13 +2798,13 @@ config_file_option_string_default (struct t_config_option *option) else return config_boolean_false[0]; case CONFIG_OPTION_TYPE_INTEGER: - if (option->string_values) - return option->string_values[CONFIG_INTEGER_DEFAULT(option)]; return NULL; case CONFIG_OPTION_TYPE_STRING: return CONFIG_STRING_DEFAULT(option); case CONFIG_OPTION_TYPE_COLOR: return gui_color_get_name (CONFIG_COLOR_DEFAULT(option)); + case CONFIG_OPTION_TYPE_ENUM: + return option->string_values[CONFIG_ENUM_DEFAULT(option)]; case CONFIG_NUM_OPTION_TYPES: return NULL; } @@ -2805,6 +2843,68 @@ config_file_option_color_default (struct t_config_option *option) } /* + * Returns enum value of an option. + */ + +int +config_file_option_enum (struct t_config_option *option) +{ + if (option && option->value) + { + switch (option->type) + { + case CONFIG_OPTION_TYPE_BOOLEAN: + if (CONFIG_BOOLEAN(option) == CONFIG_BOOLEAN_TRUE) + return 1; + else + return 0; + case CONFIG_OPTION_TYPE_INTEGER: + return CONFIG_INTEGER(option); + case CONFIG_OPTION_TYPE_STRING: + return 0; + case CONFIG_OPTION_TYPE_COLOR: + return CONFIG_COLOR(option); + case CONFIG_OPTION_TYPE_ENUM: + return CONFIG_ENUM(option); + case CONFIG_NUM_OPTION_TYPES: + break; + } + } + return 0; +} + +/* + * Returns default enum value of an option. + */ + +int +config_file_option_enum_default (struct t_config_option *option) +{ + if (option && option->default_value) + { + switch (option->type) + { + case CONFIG_OPTION_TYPE_BOOLEAN: + if (CONFIG_BOOLEAN_DEFAULT(option) == CONFIG_BOOLEAN_TRUE) + return 1; + else + return 0; + case CONFIG_OPTION_TYPE_INTEGER: + return CONFIG_INTEGER_DEFAULT(option); + case CONFIG_OPTION_TYPE_STRING: + return 0; + case CONFIG_OPTION_TYPE_COLOR: + return CONFIG_COLOR_DEFAULT(option); + case CONFIG_OPTION_TYPE_ENUM: + return CONFIG_ENUM_DEFAULT(option); + case CONFIG_NUM_OPTION_TYPES: + break; + } + } + return 0; +} + +/* * Returns a char to add before the name of option to escape it. * * Returns: @@ -2857,16 +2957,10 @@ config_file_write_option (struct t_config_file *config_file, "on" : "off"); break; case CONFIG_OPTION_TYPE_INTEGER: - if (option->string_values) - rc = string_fprintf (config_file->file, "%s%s = %s\n", - config_file_option_escape (option->name), - option->name, - option->string_values[CONFIG_INTEGER(option)]); - else - rc = string_fprintf (config_file->file, "%s%s = %d\n", - config_file_option_escape (option->name), - option->name, - CONFIG_INTEGER(option)); + rc = string_fprintf (config_file->file, "%s%s = %d\n", + config_file_option_escape (option->name), + option->name, + CONFIG_INTEGER(option)); break; case CONFIG_OPTION_TYPE_STRING: rc = string_fprintf (config_file->file, "%s%s = \"%s\"\n", @@ -2880,6 +2974,12 @@ config_file_write_option (struct t_config_file *config_file, option->name, gui_color_get_name (CONFIG_COLOR(option))); break; + case CONFIG_OPTION_TYPE_ENUM: + rc = string_fprintf (config_file->file, "%s%s = %s\n", + config_file_option_escape (option->name), + option->name, + option->string_values[CONFIG_ENUM(option)]); + break; case CONFIG_NUM_OPTION_TYPES: break; } @@ -4286,28 +4386,16 @@ config_file_print_log () "on" : "off") : "null"); break; case CONFIG_OPTION_TYPE_INTEGER: - if (ptr_option->string_values) - { - log_printf (" default value. . . . . . . . : '%s'", - (ptr_option->default_value) ? - ptr_option->string_values[CONFIG_INTEGER_DEFAULT(ptr_option)] : "null"); - log_printf (" value (integer/str). . . . . : '%s'", - (ptr_option->value) ? - ptr_option->string_values[CONFIG_INTEGER(ptr_option)] : "null"); - } + if (ptr_option->default_value) + log_printf (" default value. . . . . . . . : %d", + CONFIG_INTEGER_DEFAULT(ptr_option)); else - { - if (ptr_option->default_value) - log_printf (" default value. . . . . . . . : %d", - CONFIG_INTEGER_DEFAULT(ptr_option)); - else - log_printf (" default value. . . . . . . . : null"); - if (ptr_option->value) - log_printf (" value (integer). . . . . . . : %d", - CONFIG_INTEGER(ptr_option)); - else - log_printf (" value (integer). . . . . . . : null"); - } + log_printf (" default value. . . . . . . . : null"); + if (ptr_option->value) + log_printf (" value (integer). . . . . . . : %d", + CONFIG_INTEGER(ptr_option)); + else + log_printf (" value (integer). . . . . . . : null"); break; case CONFIG_OPTION_TYPE_STRING: if (ptr_option->default_value) @@ -4335,6 +4423,14 @@ config_file_print_log () else log_printf (" value (color). . . . . . . . : null"); break; + case CONFIG_OPTION_TYPE_ENUM: + log_printf (" default value. . . . . . . . : '%s'", + (ptr_option->default_value) ? + ptr_option->string_values[CONFIG_ENUM_DEFAULT(ptr_option)] : "null"); + log_printf (" value (integer/str). . . . . : '%s'", + (ptr_option->value) ? + ptr_option->string_values[CONFIG_ENUM(ptr_option)] : "null"); + break; case CONFIG_NUM_OPTION_TYPES: break; } diff --git a/src/core/wee-config-file.h b/src/core/wee-config-file.h index 9d56930b3..2e00c0529 100644 --- a/src/core/wee-config-file.h +++ b/src/core/wee-config-file.h @@ -39,6 +39,9 @@ #define CONFIG_COLOR(option) (*((int *)((option)->value))) #define CONFIG_COLOR_DEFAULT(option) (*((int *)((option)->default_value))) +#define CONFIG_ENUM(option) (*((int *)((option)->value))) +#define CONFIG_ENUM_DEFAULT(option) (*((int *)((option)->default_value))) + #define CONFIG_BOOLEAN_FALSE 0 #define CONFIG_BOOLEAN_TRUE 1 @@ -136,6 +139,7 @@ enum t_config_option_type CONFIG_OPTION_TYPE_INTEGER, CONFIG_OPTION_TYPE_STRING, CONFIG_OPTION_TYPE_COLOR, + CONFIG_OPTION_TYPE_ENUM, /* number of option types */ CONFIG_NUM_OPTION_TYPES, }; @@ -317,6 +321,8 @@ extern const char *config_file_option_string (struct t_config_option *option); extern const char *config_file_option_string_default (struct t_config_option *option); extern const char *config_file_option_color (struct t_config_option *option); extern const char *config_file_option_color_default (struct t_config_option *option); +extern int config_file_option_enum (struct t_config_option *option); +extern int config_file_option_enum_default (struct t_config_option *option); extern int config_file_write_option (struct t_config_file *config_file, struct t_config_option *option); extern int config_file_write_line (struct t_config_file *config_file, diff --git a/src/core/wee-doc.c b/src/core/wee-doc.c index 406388a9d..21e5ce401 100644 --- a/src/core/wee-doc.c +++ b/src/core/wee-doc.c @@ -541,19 +541,11 @@ doc_gen_user_options (const char *path, const char *lang) values = strdup ("on, off"); break; case CONFIG_OPTION_TYPE_INTEGER: - if (ptr_option->string_values) - { - values = string_rebuild_split_string ( - (const char **)ptr_option->string_values, ", ", 0, -1); - } - else - { - snprintf (str_values, sizeof (str_values), - "%d .. %d", - ptr_option->min, - ptr_option->max); - values = strdup (str_values); - } + snprintf (str_values, sizeof (str_values), + "%d .. %d", + ptr_option->min, + ptr_option->max); + values = strdup (str_values); break; case CONFIG_OPTION_TYPE_STRING: if (ptr_option->max <= 0) @@ -573,6 +565,10 @@ doc_gen_user_options (const char *path, const char *lang) case CONFIG_OPTION_TYPE_COLOR: values = strdup (command_help_option_color_values ()); break; + case CONFIG_OPTION_TYPE_ENUM: + values = string_rebuild_split_string ( + (const char **)ptr_option->string_values, ", ", 0, -1); + break; default: values = NULL; break; diff --git a/src/core/wee-eval.c b/src/core/wee-eval.c index b828a972d..81b4bd875 100644 --- a/src/core/wee-eval.c +++ b/src/core/wee-eval.c @@ -1832,11 +1832,6 @@ eval_replace_vars_cb (void *data, const char *text) EVAL_STR_TRUE : EVAL_STR_FALSE); goto end; case CONFIG_OPTION_TYPE_INTEGER: - if (ptr_option->string_values) - { - value = strdup (ptr_option->string_values[CONFIG_INTEGER(ptr_option)]); - goto end; - } snprintf (str_value, sizeof (str_value), "%d", CONFIG_INTEGER(ptr_option)); value = strdup (str_value); @@ -1847,6 +1842,9 @@ eval_replace_vars_cb (void *data, const char *text) case CONFIG_OPTION_TYPE_COLOR: value = strdup (gui_color_get_name (CONFIG_COLOR(ptr_option))); goto end; + case CONFIG_OPTION_TYPE_ENUM: + value = strdup (ptr_option->string_values[CONFIG_ENUM(ptr_option)]); + goto end; case CONFIG_NUM_OPTION_TYPES: value = strdup (""); goto end; |