diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2014-08-23 12:13:11 +0200 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2014-08-23 12:13:11 +0200 |
commit | 8c586eb49a226007f18c7fde200531bdedf73ddb (patch) | |
tree | efe3aeb6a29e4741dbae6cf6604a555392ce925c /src/core | |
parent | f6c2fd9bce0b9d19f630d241001ebafa6a64a17d (diff) | |
download | weechat-8c586eb49a226007f18c7fde200531bdedf73ddb.zip |
core: allow incomplete commands if unambiguous (task #5419)
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/wee-config.c | 7 | ||||
-rw-r--r-- | src/core/wee-config.h | 1 | ||||
-rw-r--r-- | src/core/wee-hook.c | 115 | ||||
-rw-r--r-- | src/core/wee-hook.h | 8 | ||||
-rw-r--r-- | src/core/wee-input.c | 61 |
5 files changed, 128 insertions, 64 deletions
diff --git a/src/core/wee-config.c b/src/core/wee-config.c index eb02347bb..d415fb7bd 100644 --- a/src/core/wee-config.c +++ b/src/core/wee-config.c @@ -103,6 +103,7 @@ struct t_config_option *config_look_color_nick_offline; struct t_config_option *config_look_color_pairs_auto_reset; struct t_config_option *config_look_color_real_white; struct t_config_option *config_look_command_chars; +struct t_config_option *config_look_command_incomplete; struct t_config_option *config_look_confirm_quit; struct t_config_option *config_look_day_change; struct t_config_option *config_look_day_change_message_1date; @@ -2219,6 +2220,12 @@ config_weechat_init_options () "input must start with one of these chars; the slash (\"/\") is " "always considered as command prefix (example: \".$\")"), NULL, 0, 0, "", NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL); + config_look_command_incomplete = config_file_new_option ( + weechat_config_file, ptr_section, + "command_incomplete", "boolean", + N_("if set, incomplete and unambigous commands are allowed, for " + "example /he for /help"), + NULL, 0, 0, "off", NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL); config_look_confirm_quit = config_file_new_option ( weechat_config_file, ptr_section, "confirm_quit", "boolean", diff --git a/src/core/wee-config.h b/src/core/wee-config.h index 3ec2b72b1..5d1128bf1 100644 --- a/src/core/wee-config.h +++ b/src/core/wee-config.h @@ -141,6 +141,7 @@ extern struct t_config_option *config_look_color_nick_offline; extern struct t_config_option *config_look_color_pairs_auto_reset; extern struct t_config_option *config_look_color_real_white; extern struct t_config_option *config_look_command_chars; +extern struct t_config_option *config_look_command_incomplete; extern struct t_config_option *config_look_confirm_quit; extern struct t_config_option *config_look_day_change; extern struct t_config_option *config_look_day_change_message_1date; diff --git a/src/core/wee-hook.c b/src/core/wee-hook.c index 60d212e0e..fd437f844 100644 --- a/src/core/wee-hook.c +++ b/src/core/wee-hook.c @@ -38,6 +38,7 @@ #include "weechat.h" #include "wee-hook.h" +#include "wee-config.h" #include "wee-hashtable.h" #include "wee-hdata.h" #include "wee-infolist.h" @@ -654,12 +655,12 @@ hook_command (struct t_weechat_plugin *plugin, const char *command, * Executes a command hook. * * Returns: - * 0: command executed and failed - * 1: command executed successfully - * -1: command not found - * -2: command is ambiguous (same command exists for another plugin, and we - * don't know which one to run) - * -3: command is already running + * HOOK_COMMAND_EXEC_OK: command executed successfully + * HOOK_COMMAND_EXEC_ERROR: command executed and failed + * HOOK_COMMAND_EXEC_NOT_FOUND: command not found + * HOOK_COMMAND_EXEC_AMBIGUOUS: command is ambiguous (same command exists for + * another plugin, and we don't know which one to run) + * HOOK_COMMAND_EXEC_RUNNING: command is already running */ int @@ -668,62 +669,79 @@ hook_command_exec (struct t_gui_buffer *buffer, int any_plugin, { struct t_hook *ptr_hook, *next_hook; struct t_hook *hook_plugin, *hook_other_plugin, *hook_other_plugin2; + struct t_hook *hook_incomplete_command; char **argv, **argv_eol, *ptr_command_name; - int argc, rc, count_other_plugin; + int argc, rc, length_command_name, allow_incomplete_commands; + int count_other_plugin, count_incomplete_commands; if (!buffer || !string || !string[0]) - return -1; + return HOOK_COMMAND_EXEC_NOT_FOUND; if (hook_command_run_exec (buffer, string) == WEECHAT_RC_OK_EAT) - return 1; + return HOOK_COMMAND_EXEC_OK; argv = string_split (string, " ", 0, 0, &argc); if (argc == 0) { string_free_split (argv); - return -1; + return HOOK_COMMAND_EXEC_NOT_FOUND; } argv_eol = string_split (string, " ", 1, 0, NULL); ptr_command_name = utf8_next_char (argv[0]); + length_command_name = strlen (ptr_command_name); hook_exec_start (); hook_plugin = NULL; hook_other_plugin = NULL; hook_other_plugin2 = NULL; + hook_incomplete_command = NULL; count_other_plugin = 0; + allow_incomplete_commands = CONFIG_BOOLEAN(config_look_command_incomplete); + count_incomplete_commands = 0; ptr_hook = weechat_hooks[HOOK_TYPE_COMMAND]; while (ptr_hook) { next_hook = ptr_hook->next_hook; - if (!ptr_hook->deleted - && (string_strcasecmp (ptr_command_name, - HOOK_COMMAND(ptr_hook, command)) == 0)) + if (!ptr_hook->deleted) { - if (ptr_hook->plugin == plugin) - { - if (!hook_plugin) - hook_plugin = ptr_hook; - } - else + if (string_strcasecmp (ptr_command_name, + HOOK_COMMAND(ptr_hook, command)) == 0) { - if (any_plugin) + if (ptr_hook->plugin == plugin) { - if (!hook_other_plugin) - hook_other_plugin = ptr_hook; - else if (!hook_other_plugin2) - hook_other_plugin2 = ptr_hook; - count_other_plugin++; + if (!hook_plugin) + hook_plugin = ptr_hook; } + else + { + if (any_plugin) + { + if (!hook_other_plugin) + hook_other_plugin = ptr_hook; + else if (!hook_other_plugin2) + hook_other_plugin2 = ptr_hook; + count_other_plugin++; + } + } + } + else if (allow_incomplete_commands + && (string_strncasecmp (ptr_command_name, + HOOK_COMMAND(ptr_hook, command), + length_command_name) == 0)) + { + hook_incomplete_command = ptr_hook; + count_incomplete_commands++; } } ptr_hook = next_hook; } - rc = -1; + rc = HOOK_COMMAND_EXEC_NOT_FOUND; + ptr_hook = NULL; if (hook_plugin || hook_other_plugin) { @@ -735,7 +753,7 @@ hook_command_exec (struct t_gui_buffer *buffer, int any_plugin, * command was found for other plugins with the same priority * => we don't know which one to run! */ - rc = -2; + rc = HOOK_COMMAND_EXEC_AMBIGUOUS_PLUGINS; } else { @@ -758,24 +776,35 @@ hook_command_exec (struct t_gui_buffer *buffer, int any_plugin, */ ptr_hook = (hook_plugin) ? hook_plugin : hook_other_plugin; } + } + } + else if (hook_incomplete_command) + { + if (count_incomplete_commands == 1) + ptr_hook = hook_incomplete_command; + else + rc = HOOK_COMMAND_EXEC_AMBIGUOUS_INCOMPLETE; + } - if (ptr_hook->running >= HOOK_COMMAND_MAX_CALLS) - { - /* loop in execution of command => do NOT execute again */ - rc = -3; - } + /* execute the command for the hook found */ + if (ptr_hook) + { + if (ptr_hook->running >= HOOK_COMMAND_MAX_CALLS) + { + /* loop in execution of command => do NOT execute again */ + rc = HOOK_COMMAND_EXEC_RUNNING; + } + else + { + /* execute the command! */ + ptr_hook->running++; + rc = (int) (HOOK_COMMAND(ptr_hook, callback)) + (ptr_hook->callback_data, buffer, argc, argv, argv_eol); + ptr_hook->running--; + if (rc == WEECHAT_RC_ERROR) + rc = HOOK_COMMAND_EXEC_ERROR; else - { - /* execute the command! */ - ptr_hook->running++; - rc = (int) (HOOK_COMMAND(ptr_hook, callback)) - (ptr_hook->callback_data, buffer, argc, argv, argv_eol); - ptr_hook->running--; - if (rc == WEECHAT_RC_ERROR) - rc = 0; - else - rc = 1; - } + rc = HOOK_COMMAND_EXEC_OK; } } diff --git a/src/core/wee-hook.h b/src/core/wee-hook.h index e033b6639..0e1778ad2 100644 --- a/src/core/wee-hook.h +++ b/src/core/wee-hook.h @@ -82,6 +82,14 @@ enum t_hook_type /* max calls that can be done for a command (recursive calls) */ #define HOOK_COMMAND_MAX_CALLS 5 +/* return code when a command is executed */ +#define HOOK_COMMAND_EXEC_OK 1 +#define HOOK_COMMAND_EXEC_ERROR 0 +#define HOOK_COMMAND_EXEC_NOT_FOUND -1 +#define HOOK_COMMAND_EXEC_AMBIGUOUS_PLUGINS -2 +#define HOOK_COMMAND_EXEC_AMBIGUOUS_INCOMPLETE -3 +#define HOOK_COMMAND_EXEC_RUNNING -4 + /* flags for fd hooks */ #define HOOK_FD_FLAG_READ 1 #define HOOK_FD_FLAG_WRITE 2 diff --git a/src/core/wee-input.c b/src/core/wee-input.c index ad4eb08b6..6469a1916 100644 --- a/src/core/wee-input.c +++ b/src/core/wee-input.c @@ -100,16 +100,38 @@ input_exec_command (struct t_gui_buffer *buffer, /* execute command */ switch (hook_command_exec (buffer, any_plugin, plugin, command)) { - case 0: /* command hooked, KO */ + case HOOK_COMMAND_EXEC_OK: + /* command hooked, OK (executed) */ + break; + case HOOK_COMMAND_EXEC_ERROR: + /* command hooked, error */ gui_chat_printf_date_tags (NULL, 0, GUI_FILTER_TAG_NO_FILTER, _("%sError with command \"%s\" (help on " "command: /help %s)"), gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], command, command_name + 1); break; - case 1: /* command hooked, OK (executed) */ + case HOOK_COMMAND_EXEC_NOT_FOUND: + /* + * command not found: if unknown commands are accepted by this + * buffer, just send input text as data to buffer, + * otherwise display error + */ + if (buffer->input_get_unknown_commands) + { + input_exec_data (buffer, string); + } + else + { + gui_chat_printf_date_tags (NULL, 0, GUI_FILTER_TAG_NO_FILTER, + _("%sError: unknown command \"%s\" " + "(type /help for help)"), + gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], + command_name); + } break; - case -2: /* command is ambiguous (exists for other plugins) */ + case HOOK_COMMAND_EXEC_AMBIGUOUS_PLUGINS: + /* command is ambiguous (exists for other plugins) */ gui_chat_printf_date_tags (NULL, 0, GUI_FILTER_TAG_NO_FILTER, _("%sError: ambiguous command \"%s\": " "it exists in many plugins and not in " @@ -118,30 +140,27 @@ input_exec_command (struct t_gui_buffer *buffer, command_name, plugin_get_name (plugin)); break; - case -3: /* command is running */ + case HOOK_COMMAND_EXEC_AMBIGUOUS_INCOMPLETE: + /* + * command is ambiguous (incomplete command and many commands + * start with this name) + */ + gui_chat_printf_date_tags (NULL, 0, GUI_FILTER_TAG_NO_FILTER, + _("%sError: incomplete command \"%s\" " + "and many commands start with this " + "name"), + gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], + command_name); + break; + case HOOK_COMMAND_EXEC_RUNNING: + /* command is running */ gui_chat_printf_date_tags (NULL, 0, GUI_FILTER_TAG_NO_FILTER, _("%sError: too much calls to command " "\"%s\" (looping)"), gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], command_name); break; - default: /* no command hooked */ - /* - * if unknown commands are accepted by this buffer, just send - * input text as data to buffer, otherwise display error - */ - if (buffer->input_get_unknown_commands) - { - input_exec_data (buffer, string); - } - else - { - gui_chat_printf_date_tags (NULL, 0, GUI_FILTER_TAG_NO_FILTER, - _("%sError: unknown command \"%s\" " - "(type /help for help)"), - gui_chat_prefix[GUI_CHAT_PREFIX_ERROR], - command_name); - } + default: break; } free (command); |