summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2023-03-02 20:40:46 +0100
committerSébastien Helleu <flashcode@flashtux.org>2023-03-16 20:42:19 +0100
commitccc649d06cdf5a0f9f0b837df42ac50bddcc8ebd (patch)
tree39279f3d8980d97e303c5c8e605acd1be4f8c013 /src/core
parentc82df8f22e02d6c9e86705537fc525dc252a4554 (diff)
downloadweechat-ccc649d06cdf5a0f9f0b837df42ac50bddcc8ebd.zip
core: create config options for all keys that can be managed with `/set` and `/fset` commands
Diffstat (limited to 'src/core')
-rw-r--r--src/core/wee-command.c62
-rw-r--r--src/core/wee-completion.c26
-rw-r--r--src/core/wee-config.c149
-rw-r--r--src/core/wee-config.h2
4 files changed, 129 insertions, 110 deletions
diff --git a/src/core/wee-command.c b/src/core/wee-command.c
index 0d54f96a7..ab3290ee4 100644
--- a/src/core/wee-command.c
+++ b/src/core/wee-command.c
@@ -3812,8 +3812,10 @@ command_key_display_list (const char *message_no_key,
struct t_gui_key *ptr_key;
if (keys_count == 0)
+ {
gui_chat_printf (NULL, message_no_key,
gui_key_context_string[context]);
+ }
else
{
gui_chat_printf (NULL, "");
@@ -3997,7 +3999,7 @@ command_key_reset (int context, const char *key)
COMMAND_CALLBACK(key)
{
struct t_gui_key *ptr_new_key;
- int old_keys_count, keys_added, i, context, rc;
+ int old_keys_count, keys_added, context, rc;
/* make C compiler happy */
(void) pointer;
@@ -4007,17 +4009,20 @@ COMMAND_CALLBACK(key)
/* display all key bindings (current keys) */
if ((argc == 1) || (string_strcmp (argv[1], "list") == 0))
{
- for (i = 0; i < GUI_KEY_NUM_CONTEXTS; i++)
+ for (context = 0; context < GUI_KEY_NUM_CONTEXTS; context++)
{
if ((argc < 3)
- || (string_strcmp (argv[2], gui_key_context_string[i]) == 0))
+ || (string_strcmp (argv[2],
+ gui_key_context_string[context]) == 0))
{
command_key_display_list (_("No key binding defined for "
"context \"%s\""),
/* TRANSLATORS: first "%d" is number of keys */
_("%d key bindings for context "
"\"%s\":"),
- i, gui_keys[i], gui_keys_count[i]);
+ context,
+ gui_keys[context],
+ gui_keys_count[context]);
}
}
return WEECHAT_RC_OK;
@@ -4026,12 +4031,13 @@ COMMAND_CALLBACK(key)
/* display redefined or key bindings added */
if (string_strcmp (argv[1], "listdiff") == 0)
{
- for (i = 0; i < GUI_KEY_NUM_CONTEXTS; i++)
+ for (context = 0; context < GUI_KEY_NUM_CONTEXTS; context++)
{
if ((argc < 3)
- || (string_strcmp (argv[2], gui_key_context_string[i]) == 0))
+ || (string_strcmp (argv[2],
+ gui_key_context_string[context]) == 0))
{
- command_key_display_listdiff (i);
+ command_key_display_listdiff (context);
}
}
return WEECHAT_RC_OK;
@@ -4040,19 +4046,20 @@ COMMAND_CALLBACK(key)
/* display default key bindings */
if (string_strcmp (argv[1], "listdefault") == 0)
{
- for (i = 0; i < GUI_KEY_NUM_CONTEXTS; i++)
+ for (context = 0; context < GUI_KEY_NUM_CONTEXTS; context++)
{
if ((argc < 3)
- || (string_strcmp (argv[2], gui_key_context_string[i]) == 0))
+ || (string_strcmp (argv[2],
+ gui_key_context_string[context]) == 0))
{
command_key_display_list (_("No default key binding for "
"context \"%s\""),
/* TRANSLATORS: first "%d" is number of keys */
_("%d default key bindings for "
"context \"%s\":"),
- i,
- gui_default_keys[i],
- gui_default_keys_count[i]);
+ context,
+ gui_default_keys[context],
+ gui_default_keys_count[context]);
}
}
return WEECHAT_RC_OK;
@@ -4258,18 +4265,22 @@ COMMAND_CALLBACK(key)
{
if ((argc >= 3) && (string_strcmp (argv[2], "-yes") == 0))
{
- for (i = 0; i < GUI_KEY_NUM_CONTEXTS; i++)
+ for (context = 0; context < GUI_KEY_NUM_CONTEXTS; context++)
{
if ((argc < 4)
- || (string_strcmp (argv[3], gui_key_context_string[i]) == 0))
+ || (string_strcmp (argv[3],
+ gui_key_context_string[context]) == 0))
{
- gui_key_free_all (&gui_keys[i], &last_gui_key[i],
- &gui_keys_count[i]);
- gui_key_default_bindings (i);
+ gui_key_free_all (context,
+ &gui_keys[context],
+ &last_gui_key[context],
+ &gui_keys_count[context],
+ 1);
+ gui_key_default_bindings (context, 1);
gui_chat_printf (NULL,
_("Default key bindings restored for "
"context \"%s\""),
- gui_key_context_string[i]);
+ gui_key_context_string[context]);
}
}
}
@@ -4287,22 +4298,23 @@ COMMAND_CALLBACK(key)
/* add missing keys */
if (string_strcmp (argv[1], "missing") == 0)
{
- for (i = 0; i < GUI_KEY_NUM_CONTEXTS; i++)
+ for (context = 0; context < GUI_KEY_NUM_CONTEXTS; context++)
{
if ((argc < 3)
- || (string_strcmp (argv[2], gui_key_context_string[i]) == 0))
+ || (string_strcmp (argv[2],
+ gui_key_context_string[context]) == 0))
{
- old_keys_count = gui_keys_count[i];
+ old_keys_count = gui_keys_count[context];
gui_key_verbose = 1;
- gui_key_default_bindings (i);
+ gui_key_default_bindings (context, 1);
gui_key_verbose = 0;
- keys_added = (gui_keys_count[i] > old_keys_count) ?
- gui_keys_count[i] - old_keys_count : 0;
+ keys_added = (gui_keys_count[context] > old_keys_count) ?
+ gui_keys_count[context] - old_keys_count : 0;
gui_chat_printf (NULL,
NG_("%d new key added", "%d new keys added "
"(context: \"%s\")", keys_added),
keys_added,
- gui_key_context_string[i]);
+ gui_key_context_string[context]);
}
}
return WEECHAT_RC_OK;
diff --git a/src/core/wee-completion.c b/src/core/wee-completion.c
index 4c5fbb12d..150ce8b1d 100644
--- a/src/core/wee-completion.c
+++ b/src/core/wee-completion.c
@@ -1648,7 +1648,7 @@ completion_list_add_keys_contexts_cb (const void *pointer, void *data,
struct t_gui_buffer *buffer,
struct t_gui_completion *completion)
{
- int i;
+ int context;
/* make C compiler happy */
(void) pointer;
@@ -1656,9 +1656,9 @@ completion_list_add_keys_contexts_cb (const void *pointer, void *data,
(void) completion_item;
(void) buffer;
- for (i = 0; i < GUI_KEY_NUM_CONTEXTS; i++)
+ for (context = 0; context < GUI_KEY_NUM_CONTEXTS; context++)
{
- gui_completion_list_add (completion, gui_key_context_string[i],
+ gui_completion_list_add (completion, gui_key_context_string[context],
0, WEECHAT_LIST_POS_END);
}
@@ -1675,7 +1675,7 @@ completion_list_add_keys_codes_cb (const void *pointer, void *data,
struct t_gui_buffer *buffer,
struct t_gui_completion *completion)
{
- int i;
+ int context;
struct t_gui_key *ptr_key;
/* make C compiler happy */
@@ -1684,9 +1684,9 @@ completion_list_add_keys_codes_cb (const void *pointer, void *data,
(void) completion_item;
(void) buffer;
- for (i = 0; i < GUI_KEY_NUM_CONTEXTS; i++)
+ for (context = 0; context < GUI_KEY_NUM_CONTEXTS; context++)
{
- for (ptr_key = gui_keys[i]; ptr_key; ptr_key = ptr_key->next_key)
+ for (ptr_key = gui_keys[context]; ptr_key; ptr_key = ptr_key->next_key)
{
gui_completion_list_add (completion, ptr_key->key,
0, WEECHAT_LIST_POS_SORT);
@@ -1707,7 +1707,7 @@ completion_list_add_keys_codes_for_reset_cb (const void *pointer, void *data,
struct t_gui_buffer *buffer,
struct t_gui_completion *completion)
{
- int i;
+ int context;
struct t_gui_key *ptr_key, *ptr_default_key;
/* make C compiler happy */
@@ -1716,12 +1716,13 @@ completion_list_add_keys_codes_for_reset_cb (const void *pointer, void *data,
(void) completion_item;
(void) buffer;
- for (i = 0; i < GUI_KEY_NUM_CONTEXTS; i++)
+ for (context = 0; context < GUI_KEY_NUM_CONTEXTS; context++)
{
/* keys added or redefined */
- for (ptr_key = gui_keys[i]; ptr_key; ptr_key = ptr_key->next_key)
+ for (ptr_key = gui_keys[context]; ptr_key; ptr_key = ptr_key->next_key)
{
- ptr_default_key = gui_key_search (gui_default_keys[i], ptr_key->key);
+ ptr_default_key = gui_key_search (gui_default_keys[context],
+ ptr_key->key);
if (!ptr_default_key
|| (strcmp (ptr_default_key->command, ptr_key->command) != 0))
{
@@ -1731,10 +1732,11 @@ completion_list_add_keys_codes_for_reset_cb (const void *pointer, void *data,
}
/* keys deleted */
- for (ptr_default_key = gui_default_keys[i]; ptr_default_key;
+ for (ptr_default_key = gui_default_keys[context]; ptr_default_key;
ptr_default_key = ptr_default_key->next_key)
{
- ptr_key = gui_key_search (gui_keys[i], ptr_default_key->key);
+ ptr_key = gui_key_search (gui_keys[context],
+ ptr_default_key->key);
if (!ptr_key)
{
gui_completion_list_add (completion, ptr_default_key->key,
diff --git a/src/core/wee-config.c b/src/core/wee-config.c
index 8aec0eb66..a89eb0540 100644
--- a/src/core/wee-config.c
+++ b/src/core/wee-config.c
@@ -73,6 +73,9 @@ struct t_config_section *weechat_config_section_proxy = NULL;
struct t_config_section *weechat_config_section_bar = NULL;
struct t_config_section *weechat_config_section_custom_bar_item = NULL;
struct t_config_section *weechat_config_section_notify = NULL;
+struct t_config_section *weechat_config_section_key[GUI_KEY_NUM_CONTEXTS] = {
+ NULL, NULL, NULL, NULL,
+};
/* config, startup section */
@@ -1454,7 +1457,7 @@ config_day_change_timer_cb (const void *pointer, void *data,
void
config_weechat_init_after_read ()
{
- int i;
+ int context;
util_setrlimit ();
@@ -1480,10 +1483,10 @@ config_weechat_init_after_read ()
gui_bar_item_custom_use_temp_items ();
/* if no key was found configuration file, then we use default bindings */
- for (i = 0; i < GUI_KEY_NUM_CONTEXTS; i++)
+ for (context = 0; context < GUI_KEY_NUM_CONTEXTS; context++)
{
- if (!gui_keys[i])
- gui_key_default_bindings (i);
+ if (!gui_keys[context])
+ gui_key_default_bindings (context, 1);
}
/* apply filters on all buffers */
@@ -1578,17 +1581,20 @@ int
config_weechat_reload_cb (const void *pointer, void *data,
struct t_config_file *config_file)
{
- int i, rc;
+ int context, rc;
/* make C compiler happy */
(void) pointer;
(void) data;
/* remove all keys */
- for (i = 0; i < GUI_KEY_NUM_CONTEXTS; i++)
+ for (context = 0; context < GUI_KEY_NUM_CONTEXTS; context++)
{
- gui_key_free_all (&gui_keys[i], &last_gui_key[i],
- &gui_keys_count[i]);
+ gui_key_free_all (context,
+ &gui_keys[context],
+ &last_gui_key[context],
+ &gui_keys_count[context],
+ 1);
}
/* remove all proxies */
@@ -1778,7 +1784,6 @@ config_weechat_palette_change_cb (const void *pointer, void *data,
/* make C compiler happy */
(void) pointer;
(void) data;
- (void) option;
error = NULL;
number = (int)strtol (option->name, &error, 10);
@@ -2602,87 +2607,82 @@ config_weechat_filter_write_cb (const void *pointer, void *data,
}
/*
- * Reads a key option in WeeChat configuration file.
+ * Searches key context with the section pointer.
+ *
+ * Returns key context, -1 if not found.
*/
int
-config_weechat_key_read_cb (const void *pointer, void *data,
- struct t_config_file *config_file,
- struct t_config_section *section,
- const char *option_name, const char *value)
+config_weechat_get_key_context (struct t_config_section *section)
+{
+ int context;
+
+ for (context = 0; context < GUI_KEY_NUM_CONTEXTS; context++)
+ {
+ if (section == weechat_config_section_key[context])
+ return context;
+ }
+
+ /* this should never happen */
+ return -1;
+}
+
+/*
+ * Callback called when an option is created in one of these sections:
+ * key
+ * key_search
+ * key_cursor
+ * key_mouse
+ */
+
+int
+config_weechat_key_create_option_cb (const void *pointer, void *data,
+ struct t_config_file *config_file,
+ struct t_config_section *section,
+ const char *option_name,
+ const char *value)
{
int context;
- char *pos;
/* make C compiler happy */
(void) pointer;
(void) data;
(void) config_file;
- if (option_name)
- {
- context = GUI_KEY_CONTEXT_DEFAULT;
- pos = strchr (section->name, '_');
- if (pos)
- {
- context = gui_key_search_context (pos + 1);
- if (context < 0)
- context = GUI_KEY_CONTEXT_DEFAULT;
- }
+ if (!value)
+ return WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE;
- if (value && value[0])
- {
- /* bind key (overwrite any binding with same key) */
- gui_key_bind (NULL, context, option_name, value);
- }
- else
- {
- /* unbind key if no value given */
- gui_key_unbind (NULL, context, option_name);
- }
- }
+ context = config_weechat_get_key_context (section);
+ gui_key_bind (NULL, context, option_name, value);
return WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE;
}
/*
- * Writes section "key" in WeeChat configuration file.
+ * Callback called when an option is deleted in one of these sections:
+ * key
+ * key_search
+ * key_cursor
+ * key_mouse
*/
int
-config_weechat_key_write_cb (const void *pointer, void *data,
- struct t_config_file *config_file,
- const char *section_name)
+config_weechat_key_delete_option_cb (const void *pointer, void *data,
+ struct t_config_file *config_file,
+ struct t_config_section *section,
+ struct t_config_option *option)
{
- struct t_gui_key *ptr_key;
- char *pos;
int context;
/* make C compiler happy */
(void) pointer;
(void) data;
+ (void) config_file;
- if (!config_file_write_line (config_file, section_name, NULL))
- return WEECHAT_CONFIG_WRITE_ERROR;
-
- context = GUI_KEY_CONTEXT_DEFAULT;
- pos = strchr (section_name, '_');
- if (pos)
- {
- context = gui_key_search_context (pos + 1);
- if (context < 0)
- context = GUI_KEY_CONTEXT_DEFAULT;
- }
- for (ptr_key = gui_keys[context]; ptr_key; ptr_key = ptr_key->next_key)
- {
- if (!config_file_write_line (config_file, ptr_key->key,
- "\"%s\"", ptr_key->command))
- {
- return WEECHAT_CONFIG_WRITE_ERROR;
- }
- }
+ context = config_weechat_get_key_context (section);
+ gui_key_unbind (NULL, context, option->name);
- return WEECHAT_CONFIG_WRITE_OK;
+ return WEECHAT_CONFIG_OPTION_UNSET_OK_REMOVED;
}
/*
@@ -2697,7 +2697,7 @@ int
config_weechat_init_options ()
{
struct t_config_section *ptr_section;
- int i;
+ int context;
char section_name[128];
weechat_config_file = config_file_new (
@@ -4946,26 +4946,29 @@ config_weechat_init_options ()
}
/* keys */
- for (i = 0; i < GUI_KEY_NUM_CONTEXTS; i++)
+ for (context = 0; context < GUI_KEY_NUM_CONTEXTS; context++)
{
- snprintf (section_name, sizeof (section_name),
- "key%s%s",
- (i == GUI_KEY_CONTEXT_DEFAULT) ? "" : "_",
- (i == GUI_KEY_CONTEXT_DEFAULT) ? "" : gui_key_context_string[i]);
+ snprintf (
+ section_name, sizeof (section_name),
+ "key%s%s",
+ (context == GUI_KEY_CONTEXT_DEFAULT) ? "" : "_",
+ (context == GUI_KEY_CONTEXT_DEFAULT) ?
+ "" : gui_key_context_string[context]);
ptr_section = config_file_new_section (
weechat_config_file, section_name,
- 0, 0,
- &config_weechat_key_read_cb, NULL, NULL,
- &config_weechat_key_write_cb, NULL, NULL,
- &config_weechat_key_write_cb, NULL, NULL,
+ 1, 1,
+ NULL, NULL, NULL,
+ NULL, NULL, NULL,
NULL, NULL, NULL,
- NULL, NULL, NULL);
+ &config_weechat_key_create_option_cb, NULL, NULL,
+ &config_weechat_key_delete_option_cb, NULL, NULL);
if (!ptr_section)
{
config_file_free (weechat_config_file);
weechat_config_file = NULL;
return 0;
}
+ weechat_config_section_key[context] = ptr_section;
}
return 1;
diff --git a/src/core/wee-config.h b/src/core/wee-config.h
index 921a06203..195efcd75 100644
--- a/src/core/wee-config.h
+++ b/src/core/wee-config.h
@@ -132,6 +132,7 @@ extern struct t_config_section *weechat_config_section_proxy;
extern struct t_config_section *weechat_config_section_bar;
extern struct t_config_section *weechat_config_section_custom_bar_item;
extern struct t_config_section *weechat_config_section_notify;
+extern struct t_config_section *weechat_config_section_key[];
extern struct t_config_option *config_startup_command_after_plugins;
extern struct t_config_option *config_startup_command_before_plugins;
@@ -387,6 +388,7 @@ extern void config_weechat_debug_set_all ();
extern int config_weechat_notify_set (struct t_gui_buffer *buffer,
const char *notify);
extern void config_get_item_time (char *text_time, int max_length);
+extern int config_weechat_get_key_context (struct t_config_section *section);
extern int config_weechat_init ();
extern int config_weechat_read ();
extern int config_weechat_write ();