From 3b8f9d4547df77e4c85b3b870ba21a67d97b31dc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= <flashcode@flashtux.org>
Date: Sat, 11 Mar 2023 23:44:44 +0100
Subject: core: check new keys manually added with /set

---
 src/core/wee-command.c | 45 +++++++++++----------------------------------
 src/core/wee-config.c  | 19 ++++++++++++++++++-
 2 files changed, 29 insertions(+), 35 deletions(-)

(limited to 'src/core')

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 ();
 
-- 
cgit v1.2.3