Skip to content

Commit

Permalink
sam: use default shell command for <, >, | and ! when applicaple
Browse files Browse the repository at this point in the history
If the shell command is omitted, the last shell command (of any type)
is substituted. The most recently used shell command is stored in a
new register currently named `!`.
  • Loading branch information
martanne committed Nov 16, 2016
1 parent 4a6df4b commit 4707fed
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,8 @@ static const char *key2register(Vis *vis, const char *keys, enum VisRegister *re
*reg = VIS_REG_SEARCH;
else if (keys[0] == ':')
*reg = VIS_REG_COMMAND;
else if (keys[0] == '!')
*reg = VIS_REG_SHELL;
return keys+1;
}

Expand Down
12 changes: 9 additions & 3 deletions sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,15 @@ static char *parse_text(const char **s) {
return buf.data;
}

static char *parse_shellcmd(const char **s) {
static char *parse_shellcmd(Vis *vis, const char **s) {
skip_spaces(s);
return parse_until(s, "\n", NULL, false);
char *cmd = parse_until(s, "\n", NULL, false);
if (!cmd) {
const char *last_cmd = register_get(vis, &vis->registers[VIS_REG_SHELL], NULL);
return last_cmd ? strdup(last_cmd) : NULL;
}
register_put0(vis, &vis->registers[VIS_REG_SHELL], cmd);
return cmd;
}

static void parse_argv(const char **s, const char *argv[], size_t maxarg) {
Expand Down Expand Up @@ -746,7 +752,7 @@ static Command *command_parse(Vis *vis, const char **s, int level, enum SamError
}
}

if (cmddef->flags & CMD_SHELL && !(cmd->argv[1] = parse_shellcmd(s))) {
if (cmddef->flags & CMD_SHELL && !(cmd->argv[1] = parse_shellcmd(vis, s))) {
*err = SAM_ERR_SHELL;
goto fail;
}
Expand Down
1 change: 1 addition & 0 deletions vis.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ enum VisRegister {
VIS_MACRO_REPEAT, /* copy of the above macro once the recording is finished */
VIS_REG_SEARCH, /* last used search pattern "/ */
VIS_REG_COMMAND, /* last used :-command ": */
VIS_REG_SHELL, /* last used shell command given to either <, >, |, or ! */
VIS_REG_INVALID, /* has to be the last 'real' register */
VIS_REG_A, VIS_REG_B, VIS_REG_C, VIS_REG_D, VIS_REG_E,
VIS_REG_F, VIS_REG_G, VIS_REG_H, VIS_REG_I, VIS_REG_J,
Expand Down

0 comments on commit 4707fed

Please sign in to comment.