From bf51a081d985f28b64346c2bdb35a04b73bea1a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Sat, 11 Mar 2023 16:19:14 +0100 Subject: [PATCH] core: display a specific error when trying to bind a key without area in mouse context --- src/core/wee-command.c | 110 +++++++++++++---------------------------- src/core/wee-config.c | 2 +- src/gui/gui-buffer.c | 2 +- src/gui/gui-key.c | 46 ++++++++++++----- 4 files changed, 72 insertions(+), 88 deletions(-) diff --git a/src/core/wee-command.c b/src/core/wee-command.c index 1d0479e7e..4dddb8fe6 100644 --- a/src/core/wee-command.c +++ b/src/core/wee-command.c @@ -3918,13 +3918,40 @@ command_key_display_listdiff (int context) } /* - * Resets a key for a given 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. */ int command_key_reset (int context, const char *key) { - struct t_gui_key *ptr_key, *ptr_default_key, *ptr_new_key; + struct t_gui_key *ptr_key, *ptr_default_key; int rc; ptr_key = gui_key_search (gui_keys[context], key); @@ -3937,17 +3964,9 @@ command_key_reset (int context, const char *key) if (strcmp (ptr_key->command, ptr_default_key->command) != 0) { gui_key_verbose = 1; - ptr_new_key = gui_key_bind (NULL, context, key, - ptr_default_key->command); + (void) gui_key_bind (NULL, context, key, + ptr_default_key->command); gui_key_verbose = 0; - if (!ptr_new_key) - { - gui_chat_printf (NULL, - _("%sUnable to bind key \"%s\""), - gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], - key); - return WEECHAT_RC_OK; - } } else { @@ -3976,17 +3995,8 @@ command_key_reset (int context, const char *key) { /* no key, but default key exists */ gui_key_verbose = 1; - ptr_new_key = gui_key_bind (NULL, context, key, - ptr_default_key->command); + (void) gui_key_bind (NULL, context, key, ptr_default_key->command); gui_key_verbose = 0; - if (!ptr_new_key) - { - gui_chat_printf (NULL, - _("%sUnable to bind key \"%s\""), - gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], - key); - return WEECHAT_RC_OK; - } } } else @@ -4095,32 +4105,8 @@ COMMAND_CALLBACK(key) return WEECHAT_RC_OK; } - /* bind new key */ - if (CONFIG_BOOLEAN(config_look_key_bind_safe) - && !gui_key_is_safe (GUI_KEY_CONTEXT_DEFAULT, argv[2])) - { - 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], - argv[2]); - return WEECHAT_RC_OK; - } - gui_key_verbose = 1; - ptr_new_key = gui_key_bind (NULL, GUI_KEY_CONTEXT_DEFAULT, - argv[2], argv_eol[3]); - gui_key_verbose = 0; - if (!ptr_new_key) - { - gui_chat_printf (NULL, - _("%sUnable to bind key \"%s\""), - gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], - argv[2]); - return WEECHAT_RC_OK; - } + command_key_bind (GUI_KEY_CONTEXT_DEFAULT, argv[2], argv_eol[3]); + return WEECHAT_RC_OK; } @@ -4158,33 +4144,7 @@ COMMAND_CALLBACK(key) return WEECHAT_RC_OK; } - /* bind new key */ - if (CONFIG_BOOLEAN(config_look_key_bind_safe) - && !gui_key_is_safe (context, argv[3])) - { - 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], - argv[3]); - return WEECHAT_RC_OK; - } - - gui_key_verbose = 1; - ptr_new_key = gui_key_bind (NULL, context, - argv[3], argv_eol[4]); - gui_key_verbose = 0; - if (!ptr_new_key) - { - gui_chat_printf (NULL, - _("%sUnable to bind key \"%s\""), - gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], - argv[3]); - return WEECHAT_RC_OK; - } + command_key_bind (context, argv[3], argv_eol[4]); return WEECHAT_RC_OK; } diff --git a/src/core/wee-config.c b/src/core/wee-config.c index 26412a3eb..fdb00d645 100644 --- a/src/core/wee-config.c +++ b/src/core/wee-config.c @@ -2666,7 +2666,7 @@ 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); - gui_key_bind (NULL, context, option_name, value); + (void) gui_key_bind (NULL, context, option_name, value); return WEECHAT_CONFIG_OPTION_SET_OK_SAME_VALUE; } diff --git a/src/gui/gui-buffer.c b/src/gui/gui-buffer.c index 4858bc49f..16d85bb57 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) { - gui_key_bind (buffer, 0, property + 9, value); + (void) gui_key_bind (buffer, 0, property + 9, value); } else if (strncmp (property, "key_unbind_", 11) == 0) { diff --git a/src/gui/gui-key.c b/src/gui/gui-key.c index fccb7b428..40e488d21 100644 --- a/src/gui/gui-key.c +++ b/src/gui/gui-key.c @@ -1316,12 +1316,28 @@ gui_key_new (struct t_gui_buffer *buffer, int context, const char *key, struct t_config_option *ptr_option; struct t_gui_key *new_key; + key_amended = NULL; + ptr_option = NULL; + if (!key || !command) + goto error; + + if ((context == GUI_KEY_CONTEXT_MOUSE) && (key[0] != '@')) + { + if (gui_key_verbose) + { + gui_chat_printf (NULL, + _("%sInvalid key for mouse context \"%s\": it " + "must start with \"@area\" (see /help key)"), + gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], + key); + } return NULL; + } key_amended = gui_key_amend (key); if (!key_amended) - return NULL; + goto error; ptr_option = NULL; @@ -1329,20 +1345,12 @@ gui_key_new (struct t_gui_buffer *buffer, int context, const char *key, { ptr_option = gui_key_new_option (context, key_amended, command); if (!ptr_option) - { - free (key_amended); - return NULL; - } + goto error; } new_key = malloc (sizeof (*new_key)); if (!new_key) - { - if (ptr_option) - config_file_option_free (ptr_option, 0); - free (key_amended); - return NULL; - } + goto error; new_key->key = key_amended; new_key->chunks = string_split (key_amended, ",", NULL, 0, 0, @@ -1382,6 +1390,22 @@ gui_key_new (struct t_gui_buffer *buffer, int context, const char *key, WEECHAT_HOOK_SIGNAL_STRING, new_key->key); return new_key; + +error: + if (gui_key_verbose) + { + gui_chat_printf (NULL, + _("%sUnable to bind key \"%s\" in context \"%s\" " + "(see /help key)"), + gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], + key, + gui_key_context_string[context]); + } + if (key_amended) + free (key_amended); + if (ptr_option) + config_file_option_free (ptr_option, 0); + return NULL; } /*