From 69b8d4f81b4dc82ff95e8b845c9a211b62f0248f Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Sun, 25 Jun 2000 22:59:49 +0000 Subject: [PATCH] Subcommand and command parameter completion works now also if you use alias as the base command. git-svn-id: http://svn.irssi.org/repos/irssi/trunk@380 dbcabf3a-b0e7-0310-adc4-f8d773084564 --- src/core/commands.c | 3 -- src/core/commands.h | 3 ++ src/fe-common/core/completion.c | 67 ++++++++++++++++++++++----------- 3 files changed, 49 insertions(+), 24 deletions(-) diff --git a/src/core/commands.c b/src/core/commands.c index 5e0a27ba..3e189913 100644 --- a/src/core/commands.c +++ b/src/core/commands.c @@ -30,9 +30,6 @@ #include "lib-config/iconfig.h" #include "settings.h" -#define alias_find(alias) \ - iconfig_get_str("aliases", alias, NULL) - GSList *commands; char *current_command; diff --git a/src/core/commands.h b/src/core/commands.h index a199a1f5..1f1dc8dd 100644 --- a/src/core/commands.h +++ b/src/core/commands.h @@ -22,6 +22,9 @@ enum { CMDERR_NOT_GOOD_IDEA /* not good idea to do, -yes overrides this */ }; +#define alias_find(alias) \ + iconfig_get_str("aliases", alias, NULL) + #define cmd_return_error(a) { signal_emit("error command", 1, GINT_TO_POINTER(a)); signal_stop(); return; } #define cmd_param_error(a) { cmd_params_free(free_arg); cmd_return_error(a); } diff --git a/src/fe-common/core/completion.c b/src/fe-common/core/completion.c index 31212bcf..714d7934 100644 --- a/src/fe-common/core/completion.c +++ b/src/fe-common/core/completion.c @@ -346,12 +346,12 @@ static GList *completion_get_subcommands(const char *cmd) } /* split the line to command and arguments */ -static char *line_get_command(const char *line, char **args) +static char *line_get_command(const char *line, char **args, int aliases) { const char *ptr, *cmdargs; char *cmd, *checkcmd; - cmd = checkcmd = NULL; + cmd = checkcmd = NULL; *args = ""; cmdargs = NULL; ptr = line; do { @@ -366,7 +366,8 @@ static char *line_get_command(const char *line, char **args) cmdargs = ptr; } - if (!command_find(checkcmd)) { + if (aliases ? !alias_find(checkcmd) : + !command_find(checkcmd)) { /* not found, use the previous */ g_free(checkcmd); break; @@ -374,17 +375,36 @@ static char *line_get_command(const char *line, char **args) /* found, check if it has subcommands */ g_free_not_null(cmd); - cmd = checkcmd; + if (!aliases) + cmd = checkcmd; + else { + cmd = g_strdup(alias_find(checkcmd)); + g_free(checkcmd); + } *args = (char *) cmdargs; } while (ptr != NULL); return cmd; } +static char *expand_aliases(const char *line) +{ + char *cmd, *args, *ret; + + cmd = line_get_command(line, &args, TRUE); + if (cmd == NULL) return g_strdup(line); + if (*args == '\0') return cmd; + + ret = g_strconcat(cmd, " ", args, NULL); + g_free(cmd); + return ret; +} + static void sig_complete_word(GList **list, WINDOW_REC *window, const char *word, const char *linestart, int *want_space) { const char *newword, *cmdchars; + char *signal, *cmd, *args, *line; g_return_if_fail(list != NULL); g_return_if_fail(word != NULL); @@ -409,31 +429,36 @@ static void sig_complete_word(GList **list, WINDOW_REC *window, return; } - if (strchr(cmdchars, *linestart) && is_base_command(linestart+1)) { - /* complete /command's subcommand */ - char *tmp; + /* check only for /command completions from now on */ + cmdchars = strchr(cmdchars, *linestart); + if (cmdchars == NULL) return; - tmp = g_strconcat(linestart+1, " ", word, NULL); - *list = completion_get_subcommands(tmp); - g_free(tmp); + /* check if there's aliases */ + line = linestart[1] == *cmdchars ? g_strdup(linestart+2) : + expand_aliases(linestart+1); + + if (is_base_command(line)) { + /* complete subcommand */ + cmd = g_strconcat(line, " ", word, NULL); + *list = completion_get_subcommands(cmd); + g_free(cmd); if (*list != NULL) signal_stop(); + g_free(line); return; } - if (strchr(cmdchars, *linestart)) { - /* complete /command's parameters */ - char *signal, *cmd, *args; + /* complete parameters */ + cmd = line_get_command(line, &args, FALSE); + if (cmd != NULL) { + signal = g_strconcat("complete command ", cmd, NULL); + signal_emit(signal, 5, list, window, word, args, want_space); - cmd = line_get_command(linestart+1, &args); - if (cmd != NULL) { - signal = g_strconcat("complete command ", cmd, NULL); - signal_emit(signal, 5, list, window, word, args, want_space); - - g_free(signal); - g_free(cmd); - } + g_free(signal); + g_free(cmd); } + + g_free(line); } static void sig_complete_set(GList **list, WINDOW_REC *window,