From 3b8f9d4547df77e4c85b3b870ba21a67d97b31dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Sat, 11 Mar 2023 23:44:44 +0100 Subject: [PATCH] core: check new keys manually added with /set --- src/core/wee-command.c | 45 ++++++++------------------------- src/core/wee-config.c | 19 +++++++++++++- src/gui/gui-buffer.c | 2 +- src/gui/gui-key.c | 25 +++++++++++++++++- src/gui/gui-key.h | 4 +-- tests/unit/gui/test-gui-key.cpp | 1 + 6 files changed, 57 insertions(+), 39 deletions(-) diff --git a/src/core/wee-command.c b/src/core/wee-command.c index 01b1d1574..4a0eb58f9 100644 --- a/src/core/wee-command.c +++ b/src/core/wee-command.c @@ -3917,33 +3917,6 @@ command_key_display_listdiff (int context) } } -/* - * Binds a key in the given context. - */ - -void -command_key_bind (int context, const char *key, const char *command) -{ - if (CONFIG_BOOLEAN(config_look_key_bind_safe) - && (context != GUI_KEY_CONTEXT_MOUSE) - && !gui_key_is_safe (context, key)) - { - gui_chat_printf (NULL, - _("%sIt is not safe to bind key \"%s\" because " - "it does not start with a ctrl or meta code " - "(tip: use alt-k to find key codes); if you " - "want to bind this key anyway, turn off option " - "weechat.look.key_bind_safe"), - gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], - key); - return; - } - - gui_key_verbose = 1; - (void) gui_key_bind (NULL, context, key, command); - gui_key_verbose = 0; -} - /* * Resets a key in the given context. */ @@ -3964,8 +3937,8 @@ command_key_reset (int context, const char *key) if (strcmp (ptr_key->command, ptr_default_key->command) != 0) { gui_key_verbose = 1; - (void) gui_key_bind (NULL, context, key, - ptr_default_key->command); + (void) gui_key_bind (NULL, context, + key, ptr_default_key->command, 1); gui_key_verbose = 0; } else @@ -3995,7 +3968,8 @@ command_key_reset (int context, const char *key) { /* no key, but default key exists */ gui_key_verbose = 1; - (void) gui_key_bind (NULL, context, key, ptr_default_key->command); + (void) gui_key_bind (NULL, context, + key, ptr_default_key->command, 1); gui_key_verbose = 0; } } @@ -4105,7 +4079,10 @@ COMMAND_CALLBACK(key) return WEECHAT_RC_OK; } - command_key_bind (GUI_KEY_CONTEXT_DEFAULT, argv[2], argv_eol[3]); + gui_key_verbose = 1; + (void) gui_key_bind (NULL, GUI_KEY_CONTEXT_DEFAULT, + argv[2], argv_eol[3], 1); + gui_key_verbose = 0; return WEECHAT_RC_OK; } @@ -4144,7 +4121,9 @@ COMMAND_CALLBACK(key) return WEECHAT_RC_OK; } - command_key_bind (context, argv[3], argv_eol[4]); + gui_key_verbose = 1; + gui_key_bind (NULL, context, argv[3], argv_eol[4], 1); + gui_key_verbose = 0; return WEECHAT_RC_OK; } @@ -6417,8 +6396,6 @@ COMMAND_CALLBACK(set) _("Option changed: ") : _("Option created: ")); } - else - gui_chat_printf (NULL, _("Option changed")); break; } diff --git a/src/core/wee-config.c b/src/core/wee-config.c index fdb00d645..2551bdefe 100644 --- a/src/core/wee-config.c +++ b/src/core/wee-config.c @@ -333,6 +333,7 @@ struct t_config_option *config_signal_sigusr2; /* other */ +int config_loading = 0; int config_length_nick_prefix_suffix = 0; int config_length_prefix_same_nick = 0; int config_length_prefix_same_nick_middle = 0; @@ -1627,7 +1628,9 @@ config_weechat_reload_cb (const void *pointer, void *data, /* remove all filters */ gui_filter_free_all (); + config_loading = 1; rc = config_file_reload (config_file); + config_loading = 0; config_weechat_init_after_read (); @@ -2666,7 +2669,19 @@ config_weechat_key_create_option_cb (const void *pointer, void *data, return WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE; context = config_weechat_get_key_context (section); - (void) gui_key_bind (NULL, context, option_name, value); + + if (config_loading) + { + /* don't check key when loading config */ + (void) gui_key_bind (NULL, context, option_name, value, 0); + } + else + { + /* enable verbose and check key if option is manually created */ + gui_key_verbose = 1; + (void) gui_key_bind (NULL, context, option_name, value, 1); + gui_key_verbose = 0; + } return WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE; } @@ -5052,7 +5067,9 @@ config_weechat_read () { int rc; + config_loading = 1; rc = config_file_read (weechat_config_file); + config_loading = 0; config_weechat_init_after_read (); diff --git a/src/gui/gui-buffer.c b/src/gui/gui-buffer.c index 16d85bb57..ab75c2cce 100644 --- a/src/gui/gui-buffer.c +++ b/src/gui/gui-buffer.c @@ -2378,7 +2378,7 @@ gui_buffer_set (struct t_gui_buffer *buffer, const char *property, } else if (strncmp (property, "key_bind_", 9) == 0) { - (void) gui_key_bind (buffer, 0, property + 9, value); + (void) gui_key_bind (buffer, 0, property + 9, value, 0); } else if (strncmp (property, "key_unbind_", 11) == 0) { diff --git a/src/gui/gui-key.c b/src/gui/gui-key.c index 2af5cec54..429421e97 100644 --- a/src/gui/gui-key.c +++ b/src/gui/gui-key.c @@ -1542,6 +1542,8 @@ gui_key_search_part (struct t_gui_buffer *buffer, int context, * If buffer is not null, then key is specific to buffer otherwise it's general * key (for most keys). * + * If check_key == 1, the key is checked before being added. + * * If key already exists, it is removed then added again with new value. * * Returns pointer to new key, NULL if error. @@ -1549,11 +1551,32 @@ gui_key_search_part (struct t_gui_buffer *buffer, int context, struct t_gui_key * gui_key_bind (struct t_gui_buffer *buffer, int context, const char *key, - const char *command) + const char *command, int check_key) { if (!key || !command) return NULL; + if (check_key) + { + if (CONFIG_BOOLEAN(config_look_key_bind_safe) + && (context != GUI_KEY_CONTEXT_MOUSE) + && !gui_key_is_safe (context, key)) + { + if (gui_key_verbose) + { + gui_chat_printf ( + NULL, + _("%sIt is not safe to bind key \"%s\" because it does " + "not start with a ctrl or meta code (tip: use alt-k to " + "find key codes); if you want to bind this key anyway, " + "turn off option weechat.look.key_bind_safe"), + gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], + key); + } + return NULL; + } + } + gui_key_unbind (buffer, context, key); return gui_key_new (buffer, context, key, command, 1); diff --git a/src/gui/gui-key.h b/src/gui/gui-key.h index 7110a0d9c..dc54193e5 100644 --- a/src/gui/gui-key.h +++ b/src/gui/gui-key.h @@ -95,7 +95,6 @@ extern void gui_key_grab_init (int grab_raw_key, int grab_command, extern int gui_key_expand (const char *key, char **key_name, char **key_name_alias); extern char *gui_key_legacy_to_alias (const char *key); -extern int gui_key_is_safe (int context, const char *key); extern struct t_gui_key *gui_key_new (struct t_gui_buffer *buffer, int context, const char *key, @@ -106,7 +105,8 @@ extern struct t_gui_key *gui_key_search (struct t_gui_key *keys, extern struct t_gui_key *gui_key_bind (struct t_gui_buffer *buffer, int context, const char *key, - const char *command); + const char *command, + int check_key); extern int gui_key_bind_plugin (const char *context, struct t_hashtable *keys); extern int gui_key_unbind (struct t_gui_buffer *buffer, int context, const char *key); diff --git a/tests/unit/gui/test-gui-key.cpp b/tests/unit/gui/test-gui-key.cpp index 134276867..f81cfd47d 100644 --- a/tests/unit/gui/test-gui-key.cpp +++ b/tests/unit/gui/test-gui-key.cpp @@ -34,6 +34,7 @@ extern "C" extern int gui_key_get_current_context (); extern char *gui_key_legacy_internal_code (const char *key); extern char *gui_key_fix (const char *key); +extern int gui_key_is_safe (int context, const char *key); extern struct t_config_option *gui_key_new_option (int context, const char *name, const char *value);