>From 82769b5ada0a025da849dbdd40f596cd5db6bdd4 Mon Sep 17 00:00:00 2001 From: Bernhard R. Link Date: Tue, 10 Jun 2008 14:24:05 +0200 Subject: [PATCH] SHELLCMD history only shows execute arguments, things to execute are stored as in history as execute commands --- src/actions.c | 36 ++++++++++++++++++++++++------------ src/history.c | 32 +++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 13 deletions(-) diff --git a/src/actions.c b/src/actions.c index c38731f..d463286 100644 --- a/src/actions.c +++ b/src/actions.c @@ -1667,9 +1667,21 @@ exec_completions (char *str) } static cmdret * -read_shellcmd (struct argspec *spec, struct sbuf *s, struct cmdarg **arg) +read_shellcmd (struct argspec *spec, struct sbuf *s, struct cmdarg **arg, const char *prefix) { - return read_string (spec, s, hist_SHELLCMD, exec_completions, arg); + cmdret *ret; + + ret = read_string (spec, s, hist_SHELLCMD, exec_completions, arg); +#ifdef HAVE_HISTORY + if (prefix && !ret) { + /* store for command history */ + char *s = xmalloc (strlen(prefix) + strlen((*arg)->string) + 2); + sprintf (s, "%s %s", prefix, (*arg)->string); + history_add (hist_COMMAND, s); + free(s); + } +#endif + return ret; } /* Return NULL on abort/failure. */ @@ -2092,7 +2104,7 @@ read_number (struct argspec *spec, struct sbuf *s, struct cmdarg **arg) } static cmdret * -read_arg (struct argspec *spec, struct sbuf *s, struct cmdarg **arg) +read_arg (struct argspec *spec, struct sbuf *s, struct cmdarg **arg, const char *prefix) { cmdret *ret = NULL; @@ -2119,7 +2131,7 @@ read_arg (struct argspec *spec, struct sbuf *s, struct cmdarg **arg) ret = read_command (spec, s, arg); break; case arg_SHELLCMD: - ret = read_shellcmd (spec, s, arg); + ret = read_shellcmd (spec, s, arg, prefix); break; case arg_WINDOW: ret = read_window (spec, s, arg); @@ -2144,7 +2156,7 @@ read_arg (struct argspec *spec, struct sbuf *s, struct cmdarg **arg) /* Return -1 on failure. Return the number of args on success. */ static cmdret * parsed_input_to_args (int num_args, struct argspec *argspec, struct list_head *list, - struct list_head *args, int *parsed_args) + struct list_head *args, int *parsed_args, const char *prefix) { struct sbuf *s; struct cmdarg *arg; @@ -2158,7 +2170,7 @@ parsed_input_to_args (int num_args, struct argspec *argspec, struct list_head *l list_for_each_entry (s, list, node) { if (*parsed_args >= num_args) break; - ret = read_arg (&argspec[*parsed_args], s, &arg); + ret = read_arg (&argspec[*parsed_args], s, &arg, prefix); /* If there was an error, then abort. */ if (ret) return ret; @@ -2173,20 +2185,20 @@ parsed_input_to_args (int num_args, struct argspec *argspec, struct list_head *l /* Prompt the user for missing arguments. Returns non-zero on failure. 0 on success. */ static cmdret * -fill_in_missing_args (struct user_command *cmd, struct list_head *list, struct list_head *args) +fill_in_missing_args (struct user_command *cmd, struct list_head *list, struct list_head *args, const char *prefix) { cmdret *ret; struct cmdarg *arg; int i = 0; - ret = parsed_input_to_args (cmd->num_args, cmd->args, list, args, &i); + ret = parsed_input_to_args (cmd->num_args, cmd->args, list, args, &i, prefix); if (ret) return ret; /* Fill in the rest of the required arguments. */ for(; i < cmd->i_required_args; i++) { - ret = read_arg (&cmd->args[i], NULL, &arg); + ret = read_arg (&cmd->args[i], NULL, &arg, prefix); if (ret) return ret; list_add_tail (&arg->node, args); @@ -2459,11 +2471,11 @@ command (int interactive, char *data) /* Interactive commands prompt the user for missing args. */ if (interactive) - result = fill_in_missing_args (uc, &head, &args); + result = fill_in_missing_args (uc, &head, &args, uc->name); else { int parsed_args; - result = parsed_input_to_args (uc->num_args, uc->args, &head, &args, &parsed_args); + result = parsed_input_to_args (uc->num_args, uc->args, &head, &args, &parsed_args, uc->name); } if (result == NULL) @@ -5115,7 +5127,7 @@ cmd_set (int interactive, struct cmdarg **args) if (result) goto failed; result = parsed_input_to_args (ARG(0,variable)->nargs, ARG(0,variable)->args, - &head, &arglist, &parsed_args); + &head, &arglist, &parsed_args, NULL); if (result) goto failed; /* 0 or nargs is acceptable */ diff --git a/src/history.c b/src/history.c index 28292f6..4178cf9 100644 --- a/src/history.c +++ b/src/history.c @@ -86,7 +86,7 @@ history_add (int history_id, char *item) { HIST_ENTRY *h; - if (history_id == hist_NONE) + if (history_id == hist_NONE || history_id == hist_SHELLCMD) return; h = history_get (history_length); @@ -98,16 +98,39 @@ history_add (int history_id, char *item) add_history (item); } +static const char * +extract_shell_part (const char *p) +{ + if (strncmp(p, "exec", 4) && + strncmp(p, "verbexec", 8)) + return NULL; + while( *p && !isspace(*p) ) + p++; + while( *p && isspace(*p) ) + p++; + if (*p) + return p; + return NULL; +} + const char * history_previous (int history_id) { HIST_ENTRY *h = NULL; + const char *p; if (history_id == hist_NONE) return NULL; h = previous_history(); + if (history_id == hist_SHELLCMD) { + p = NULL; + while( h && h->line && !(p = extract_shell_part(h->line))) + h = previous_history(); + return p; + } + return h ? h->line : NULL; } @@ -115,12 +138,19 @@ const char * history_next (int history_id) { HIST_ENTRY *h = NULL; + const char *p; if (history_id == hist_NONE) return NULL; h = next_history(); + if (history_id == hist_SHELLCMD) { + p = NULL; + while( h && h->line && !(p = extract_shell_part(h->line))) + h = next_history(); + return p; + } return h ? h->line : NULL; } -- 1.4.4.4