1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-24 20:06:38 +02:00

core: fix search of commands with raw code and alias matching

When raw code and alias for a key are both matching, the raw code must always
have higher priority.

This commit fixes this behavior and gives priority to raw code.
This commit is contained in:
Sébastien Helleu
2023-03-14 20:49:32 +01:00
parent fe6b69c41c
commit 313b40235a
2 changed files with 112 additions and 12 deletions
+22 -11
View File
@@ -1608,13 +1608,16 @@ gui_key_search_part (struct t_gui_buffer *buffer, int context,
const char **chunks2, int chunks2_count,
int *exact_match)
{
struct t_gui_key *ptr_key;
int rc;
struct t_gui_key *ptr_key, *key1_found, *key2_found;
int rc, rc1, rc2;
if ((!chunks1 && !chunks2) || !exact_match)
return NULL;
*exact_match = 0;
key1_found = NULL;
key2_found = NULL;
rc1 = 0;
rc2 = 0;
for (ptr_key = (buffer) ? buffer->keys : gui_keys[context]; ptr_key;
ptr_key = ptr_key->next_key)
@@ -1629,11 +1632,13 @@ gui_key_search_part (struct t_gui_buffer *buffer, int context,
chunks1_count,
(const char **)ptr_key->chunks,
ptr_key->chunks_count);
if (rc > 0)
if (rc > rc1)
{
rc1 = rc;
key1_found = ptr_key;
/* exit immediately if raw key is an exact match */
if (rc == 2)
*exact_match = 1;
break;
break;
}
}
if (chunks2)
@@ -1642,17 +1647,23 @@ gui_key_search_part (struct t_gui_buffer *buffer, int context,
chunks2_count,
(const char **)ptr_key->chunks,
ptr_key->chunks_count);
if (rc > 0)
if (rc > rc2)
{
if (rc == 2)
*exact_match = 1;
break;
rc2 = rc;
key2_found = ptr_key;
}
}
}
}
return ptr_key;
if (key1_found)
{
*exact_match = (rc1 == 2) ? 1 : 0;
return key1_found;
}
*exact_match = (rc2 == 2) ? 1 : 0;
return key2_found;
}
/*
+90 -1
View File
@@ -1304,6 +1304,7 @@ TEST(GuiKey, SearchPart)
char **chunks1, **chunks2;
int chunks1_count, chunks2_count, exact_match;
/* keys meta-a and meta-w */
chunks1 = string_split ("meta-a", ",", NULL, 0, 0, &chunks1_count);
chunks2 = string_split ("meta-w", ",", NULL, 0, 0, &chunks2_count);
@@ -1340,7 +1341,8 @@ TEST(GuiKey, SearchPart)
STRCMP_EQUAL("meta-w,meta-b", ptr_key->key);
LONGS_EQUAL(0, exact_match);
new_key = gui_key_new (NULL, GUI_KEY_CONTEXT_DEFAULT, "meta-w", "/mute", 1);
new_key = gui_key_new (NULL, GUI_KEY_CONTEXT_DEFAULT,
"meta-w", "/print meta-w", 1);
exact_match = -1;
ptr_key = gui_key_search_part (NULL, GUI_KEY_CONTEXT_DEFAULT,
@@ -1349,6 +1351,7 @@ TEST(GuiKey, SearchPart)
&exact_match);
CHECK(ptr_key);
STRCMP_EQUAL("meta-w", ptr_key->key);
STRCMP_EQUAL("/print meta-w", ptr_key->command);
LONGS_EQUAL(1, exact_match);
gui_key_free (GUI_KEY_CONTEXT_DEFAULT,
@@ -1357,6 +1360,92 @@ TEST(GuiKey, SearchPart)
&gui_keys_count[GUI_KEY_CONTEXT_DEFAULT],
new_key,
1);
string_free_split (chunks1);
string_free_split (chunks2);
/* keys ctrl-h and backspace */
chunks1 = string_split ("ctrl-h", ",", NULL, 0, 0, &chunks1_count);
chunks2 = string_split ("backspace", ",", NULL, 0, 0, &chunks2_count);
exact_match = -1;
ptr_key = gui_key_search_part (NULL, GUI_KEY_CONTEXT_DEFAULT,
(const char **)chunks1, chunks1_count,
(const char **)chunks2, chunks2_count,
&exact_match);
CHECK(ptr_key);
STRCMP_EQUAL("backspace", ptr_key->key);
STRCMP_EQUAL("/input delete_previous_char", ptr_key->command);
LONGS_EQUAL(1, exact_match);
new_key = gui_key_new (NULL, GUI_KEY_CONTEXT_DEFAULT,
"ctrl-h", "/print ctrl-h", 1);
exact_match = -1;
ptr_key = gui_key_search_part (NULL, GUI_KEY_CONTEXT_DEFAULT,
(const char **)chunks1, chunks1_count,
(const char **)chunks2, chunks2_count,
&exact_match);
CHECK(ptr_key);
STRCMP_EQUAL("ctrl-h", ptr_key->key);
STRCMP_EQUAL("/print ctrl-h", ptr_key->command);
LONGS_EQUAL(1, exact_match);
gui_key_free (GUI_KEY_CONTEXT_DEFAULT,
&gui_keys[GUI_KEY_CONTEXT_DEFAULT],
&last_gui_key[GUI_KEY_CONTEXT_DEFAULT],
&gui_keys_count[GUI_KEY_CONTEXT_DEFAULT],
new_key,
1);
new_key = gui_key_new (NULL, GUI_KEY_CONTEXT_DEFAULT,
"ctrl-h,j", "/print ctrl-h,j", 1);
exact_match = -1;
ptr_key = gui_key_search_part (NULL, GUI_KEY_CONTEXT_DEFAULT,
(const char **)chunks1, chunks1_count,
(const char **)chunks2, chunks2_count,
&exact_match);
CHECK(ptr_key);
STRCMP_EQUAL("ctrl-h,j", ptr_key->key);
STRCMP_EQUAL("/print ctrl-h,j", ptr_key->command);
LONGS_EQUAL(0, exact_match);
string_free_split (chunks1);
chunks1 = string_split ("ctrl-h,j", ",", NULL, 0, 0, &chunks1_count);
exact_match = -1;
ptr_key = gui_key_search_part (NULL, GUI_KEY_CONTEXT_DEFAULT,
(const char **)chunks1, chunks1_count,
(const char **)chunks2, chunks2_count,
&exact_match);
CHECK(ptr_key);
STRCMP_EQUAL("ctrl-h,j", ptr_key->key);
STRCMP_EQUAL("/print ctrl-h,j", ptr_key->command);
LONGS_EQUAL(1, exact_match);
string_free_split (chunks1);
chunks1 = string_split ("ctrl-q,j", ",", NULL, 0, 0, &chunks1_count);
exact_match = -1;
ptr_key = gui_key_search_part (NULL, GUI_KEY_CONTEXT_DEFAULT,
(const char **)chunks1, chunks1_count,
(const char **)chunks2, chunks2_count,
&exact_match);
CHECK(ptr_key);
STRCMP_EQUAL("backspace", ptr_key->key);
STRCMP_EQUAL("/input delete_previous_char", ptr_key->command);
LONGS_EQUAL(1, exact_match);
gui_key_free (GUI_KEY_CONTEXT_DEFAULT,
&gui_keys[GUI_KEY_CONTEXT_DEFAULT],
&last_gui_key[GUI_KEY_CONTEXT_DEFAULT],
&gui_keys_count[GUI_KEY_CONTEXT_DEFAULT],
new_key,
1);
string_free_split (chunks1);
string_free_split (chunks2);
}
/*