1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-27 21:36:37 +02:00

core: add option weechat.completion.case_sensitive

This commit is contained in:
Sébastien Helleu
2024-03-24 15:40:06 +01:00
parent 947155f325
commit 907643bc9c
19 changed files with 881 additions and 749 deletions
+7
View File
@@ -297,6 +297,7 @@ struct t_config_option *config_color_status_time = NULL;
/* config, completion section */
struct t_config_option *config_completion_base_word_until_cursor = NULL;
struct t_config_option *config_completion_case_sensitive = NULL;
struct t_config_option *config_completion_command_inline = NULL;
struct t_config_option *config_completion_default_template = NULL;
struct t_config_option *config_completion_nick_add_space = NULL;
@@ -4975,6 +4976,12 @@ config_weechat_init_options ()
"otherwise the base word ends at first space after cursor"),
NULL, 0, 0, "on", NULL, 0,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
config_completion_case_sensitive = config_file_new_option (
weechat_config_file, weechat_config_section_completion,
"case_sensitive", "boolean",
N_("if enabled, the completion is case sensitive by default"),
NULL, 0, 0, "on", NULL, 0,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
config_completion_command_inline = config_file_new_option (
weechat_config_file, weechat_config_section_completion,
"command_inline", "boolean",
+1
View File
@@ -346,6 +346,7 @@ extern struct t_config_option *config_color_status_number;
extern struct t_config_option *config_color_status_time;
extern struct t_config_option *config_completion_base_word_until_cursor;
extern struct t_config_option *config_completion_case_sensitive;
extern struct t_config_option *config_completion_command_inline;
extern struct t_config_option *config_completion_default_template;
extern struct t_config_option *config_completion_nick_add_space;
+132 -72
View File
@@ -65,16 +65,21 @@ gui_completion_word_compare_cb (void *data,
struct t_arraylist *arraylist,
void *pointer1, void *pointer2)
{
struct t_gui_completion *completion;
struct t_gui_completion_word *completion_word1, *completion_word2;
/* make C compiler happy */
(void) data;
(void) arraylist;
completion = (struct t_gui_completion *)data;
completion_word1 = (struct t_gui_completion_word *)pointer1;
completion_word2 = (struct t_gui_completion_word *)pointer2;
return string_strcmp (completion_word1->word, completion_word2->word);
if (completion->case_sensitive)
return string_strcmp (completion_word1->word, completion_word2->word);
else
return string_strcasecmp (completion_word1->word, completion_word2->word);
}
/*
@@ -112,6 +117,7 @@ gui_completion_init (struct t_gui_completion *completion,
completion->plugin = plugin;
completion->buffer = buffer;
completion->context = GUI_COMPLETION_NULL;
completion->case_sensitive = CONFIG_BOOLEAN(config_completion_case_sensitive);
completion->base_command = NULL;
completion->base_command_arg_index = 0;
completion->base_word = NULL;
@@ -125,7 +131,7 @@ gui_completion_init (struct t_gui_completion *completion,
completion->list = arraylist_new (
32, 1, 0,
&gui_completion_word_compare_cb, NULL,
&gui_completion_word_compare_cb, completion,
&gui_completion_word_free_cb, NULL);
completion->word_found = NULL;
@@ -136,7 +142,7 @@ gui_completion_init (struct t_gui_completion *completion,
completion->partial_list = arraylist_new (
0, 0, 0,
&gui_completion_word_compare_cb, NULL,
&gui_completion_word_compare_cb, completion,
&gui_completion_word_free_cb, NULL);
}
@@ -306,59 +312,6 @@ gui_completion_stop (struct t_gui_completion *completion)
}
}
/*
* Searches for a command hook.
*
* Returns pointer to hook found, NULL if not found.
*/
struct t_hook *
gui_completion_search_command (struct t_weechat_plugin *plugin,
const char *command)
{
struct t_hook *ptr_hook, *hook_for_other_plugin, *hook_incomplete_command;
int length_command, allow_incomplete_commands, count_incomplete_commands;
if (!command)
return NULL;
hook_for_other_plugin = NULL;
hook_incomplete_command = NULL;
length_command = utf8_strlen (command);
count_incomplete_commands = 0;
allow_incomplete_commands = CONFIG_BOOLEAN(config_look_command_incomplete);
for (ptr_hook = weechat_hooks[HOOK_TYPE_COMMAND]; ptr_hook;
ptr_hook = ptr_hook->next_hook)
{
if (!ptr_hook->deleted
&& HOOK_COMMAND(ptr_hook, command)
&& HOOK_COMMAND(ptr_hook, command)[0])
{
if (strcmp (HOOK_COMMAND(ptr_hook, command), command) == 0)
{
if (ptr_hook->plugin == plugin)
return ptr_hook;
hook_for_other_plugin = ptr_hook;
}
else if (allow_incomplete_commands
&& (string_strncmp (HOOK_COMMAND(ptr_hook, command),
command,
length_command) == 0))
{
hook_incomplete_command = ptr_hook;
count_incomplete_commands++;
}
}
}
if (hook_for_other_plugin)
return hook_for_other_plugin;
return (count_incomplete_commands == 1) ?
hook_incomplete_command : NULL;
}
/*
* Checks if nick has one or more ignored chars (for nick comparison).
*
@@ -462,6 +415,48 @@ gui_completion_nickncmp (const char *base_word, const char *nick, int max)
return return_cmp;
}
/*
* Compares two strings (follows case sensitive flag in completion structure).
*
* Returns:
* < 0: string1 < string2
* 0: string1 == string2
* > 0: string1 > string2
*/
int
gui_completion_strcmp (struct t_gui_completion *completion,
const char *string1, const char *string2)
{
if (!completion)
return 0;
return (completion->case_sensitive) ?
string_strcmp (string1, string2) : string_strcasecmp (string1, string2);
}
/*
* Compares two strings with max length (follows case sensitive flag in
* completion structure).
*
* Returns:
* < 0: string1 < string2
* 0: string1 == string2
* > 0: string1 > string2
*/
int
gui_completion_strncmp (struct t_gui_completion *completion,
const char *string1, const char *string2, int max)
{
if (!completion)
return 0;
return (completion->case_sensitive) ?
string_strncmp (string1, string2, max) :
string_strncasecmp (string1, string2, max);
}
/*
* Adds a word to completion list.
*/
@@ -478,10 +473,13 @@ gui_completion_list_add (struct t_gui_completion *completion, const char *word,
return;
if (!completion->base_word || !completion->base_word[0]
|| (nick_completion && (gui_completion_nickncmp (completion->base_word, word,
utf8_strlen (completion->base_word)) == 0))
|| (!nick_completion && (string_strncmp (completion->base_word, word,
utf8_strlen (completion->base_word)) == 0)))
|| (nick_completion
&& (gui_completion_nickncmp (completion->base_word, word,
utf8_strlen (completion->base_word)) == 0))
|| (!nick_completion
&& (gui_completion_strncmp (completion,
completion->base_word, word,
utf8_strlen (completion->base_word)) == 0)))
{
completion_word = malloc (sizeof (*completion_word));
if (completion_word)
@@ -679,6 +677,61 @@ gui_completion_get_matching_template (struct t_gui_completion *completion,
return fallback;
}
/*
* Searches for a command hook.
*
* Returns pointer to hook found, NULL if not found.
*/
struct t_hook *
gui_completion_search_command (struct t_gui_completion *completion,
const char *command)
{
struct t_hook *ptr_hook, *hook_for_other_plugin, *hook_incomplete_command;
int length_command, allow_incomplete_commands, count_incomplete_commands;
if (!completion || !command)
return NULL;
hook_for_other_plugin = NULL;
hook_incomplete_command = NULL;
length_command = utf8_strlen (command);
count_incomplete_commands = 0;
allow_incomplete_commands = CONFIG_BOOLEAN(config_look_command_incomplete);
for (ptr_hook = weechat_hooks[HOOK_TYPE_COMMAND]; ptr_hook;
ptr_hook = ptr_hook->next_hook)
{
if (!ptr_hook->deleted
&& HOOK_COMMAND(ptr_hook, command)
&& HOOK_COMMAND(ptr_hook, command)[0])
{
if (gui_completion_strcmp (completion,
HOOK_COMMAND(ptr_hook, command), command) == 0)
{
if (ptr_hook->plugin == completion->buffer->plugin)
return ptr_hook;
hook_for_other_plugin = ptr_hook;
}
else if (allow_incomplete_commands
&& (gui_completion_strncmp (completion,
HOOK_COMMAND(ptr_hook, command),
command,
length_command) == 0))
{
hook_incomplete_command = ptr_hook;
count_incomplete_commands++;
}
}
}
if (hook_for_other_plugin)
return hook_for_other_plugin;
return (count_incomplete_commands == 1) ?
hook_incomplete_command : NULL;
}
/*
* Gets template according to user arguments for command.
*/
@@ -697,7 +750,7 @@ gui_completion_get_template_for_args (struct t_gui_completion *completion,
&& (HOOK_COMMAND(hook_command, cplt_templates)[0][1] == '%')
&& (HOOK_COMMAND(hook_command, cplt_templates)[0][1]))
{
hook_command = gui_completion_search_command (completion->buffer->plugin,
hook_command = gui_completion_search_command (completion,
HOOK_COMMAND(hook_command, cplt_templates)[0] + 2);
if (!hook_command
|| ((HOOK_COMMAND(hook_command, cplt_templates)[0][0] == '%')
@@ -740,8 +793,7 @@ gui_completion_build_list (struct t_gui_completion *completion)
repeat_last = 0;
ptr_hook = gui_completion_search_command (completion->buffer->plugin,
completion->base_command);
ptr_hook = gui_completion_search_command (completion, completion->base_command);
if (!ptr_hook || !HOOK_COMMAND(ptr_hook, completion))
{
completion->context = GUI_COMPLETION_AUTO;
@@ -1076,7 +1128,7 @@ gui_completion_partial_build_list (struct t_gui_completion *completion,
return;
list_temp = arraylist_new (completion->list->size, 1, 0,
&gui_completion_word_compare_cb, NULL,
&gui_completion_word_compare_cb, completion,
&gui_completion_word_free_cb, NULL);
if (!list_temp)
return;
@@ -1206,9 +1258,10 @@ gui_completion_complete (struct t_gui_completion *completion)
ptr_completion_word->word,
length) == 0))
|| (!ptr_completion_word->nick_completion
&& (string_strncmp (completion->base_word,
ptr_completion_word->word,
length) == 0)))
&& (gui_completion_strncmp (completion,
completion->base_word,
ptr_completion_word->word,
length) == 0)))
{
if ((!completion->word_found) || word_found_seen)
{
@@ -1241,9 +1294,10 @@ gui_completion_complete (struct t_gui_completion *completion)
ptr_completion_word2->word,
length) == 0))
|| (!ptr_completion_word->nick_completion
&& (string_strncmp (completion->base_word,
ptr_completion_word2->word,
length) == 0)))
&& (gui_completion_strncmp (completion,
completion->base_word,
ptr_completion_word2->word,
length) == 0)))
{
other_completion++;
}
@@ -1295,8 +1349,12 @@ gui_completion_complete (struct t_gui_completion *completion)
other_completion++;
}
if (completion->word_found &&
(strcmp (ptr_completion_word->word, completion->word_found) == 0))
(gui_completion_strcmp (completion,
ptr_completion_word->word,
completion->word_found) == 0))
{
word_found_seen = 1;
}
index = (completion->direction < 0) ? index - 1 : index + 1;
}
@@ -1535,6 +1593,7 @@ gui_completion_hdata_completion_cb (const void *pointer, void *data,
HDATA_VAR(struct t_gui_completion, plugin, POINTER, 0, NULL, "plugin");
HDATA_VAR(struct t_gui_completion, buffer, POINTER, 0, NULL, "buffer");
HDATA_VAR(struct t_gui_completion, context, INTEGER, 0, NULL, NULL);
HDATA_VAR(struct t_gui_completion, case_sensitive, INTEGER, 0, NULL, NULL);
HDATA_VAR(struct t_gui_completion, base_command, STRING, 0, NULL, NULL);
HDATA_VAR(struct t_gui_completion, base_command_arg_index, INTEGER, 0, NULL, NULL);
HDATA_VAR(struct t_gui_completion, base_word, STRING, 0, NULL, NULL);
@@ -1623,6 +1682,7 @@ gui_completion_print_log ()
ptr_completion->buffer,
ptr_completion->buffer->full_name);
log_printf (" context . . . . . . . . . : %d", ptr_completion->context);
log_printf (" case_sensitive. . . . . . : %d", ptr_completion->case_sensitive);
log_printf (" base_command. . . . . . . : '%s'", ptr_completion->base_command);
log_printf (" base_command_arg_index. . : %d", ptr_completion->base_command_arg_index);
log_printf (" base_word . . . . . . . . : '%s'", ptr_completion->base_word);
+1
View File
@@ -45,6 +45,7 @@ struct t_gui_completion
/* completion context */
struct t_gui_buffer *buffer; /* buffer where completion was asked */
int context; /* context: null, nick, command, cmd arg */
int case_sensitive; /* case sensitive completion? */
char *base_command; /* cmd with arg to complete (can be NULL) */
int base_command_arg_index; /* # arg to complete (if context=cmd arg) */
char *base_word; /* word to complete (when Tab was pressed) */