Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shellex command #707

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/ex.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ typedef enum {
EX_SCR,
EX_SET,
EX_SHELLCMD,
EX_SHELLEX,
EX_SOURCE,
EX_TABOPEN,
} ExCode;
Expand Down Expand Up @@ -154,6 +155,7 @@ static VbCmdResult ex_quit(Client *c, const ExArg *arg);
static VbCmdResult ex_save(Client *c, const ExArg *arg);
static VbCmdResult ex_set(Client *c, const ExArg *arg);
static VbCmdResult ex_shellcmd(Client *c, const ExArg *arg);
static VbCmdResult ex_shellex(Client *c, const ExArg *arg);
static VbCmdResult ex_shortcut(Client *c, const ExArg *arg);
static VbCmdResult ex_source(Client *c, const ExArg *arg);
static VbCmdResult ex_handlers(Client *c, const ExArg *arg);
Expand Down Expand Up @@ -204,6 +206,7 @@ static ExInfo commands[] = {
{"save", EX_SAVE, ex_save, EX_FLAG_RHS|EX_FLAG_EXP},
{"set", EX_SET, ex_set, EX_FLAG_RHS},
{"shellcmd", EX_SHELLCMD, ex_shellcmd, EX_FLAG_CMD|EX_FLAG_EXP|EX_FLAG_BANG},
{"shellex", EX_SHELLEX, ex_shellex, EX_FLAG_CMD|EX_FLAG_EXP},
{"shortcut-add", EX_SCA, ex_shortcut, EX_FLAG_RHS},
{"shortcut-default", EX_SCD, ex_shortcut, EX_FLAG_RHS},
{"shortcut-remove", EX_SCR, ex_shortcut, EX_FLAG_RHS},
Expand Down Expand Up @@ -1151,6 +1154,53 @@ static VbCmdResult ex_shellcmd(Client *c, const ExArg *arg)
return res;
}

static VbCmdResult ex_shellex(Client *c, const ExArg *arg)
{
int status;
char *stdOut = NULL, *stdErr = NULL, *selection = NULL, *ex_line = NULL;
VbCmdResult res;
GError *error = NULL;

if (!*arg->rhs->str) {
return CMD_ERROR;
}

/* Get current selection and write it as VIMB_SELECTION into env. */
selection = ext_proxy_get_current_selection(c);
if (selection) {
g_setenv("VIMB_SELECTION", selection, TRUE);
g_free(selection);
} else {
g_setenv("VIMB_SELECTION", "", TRUE);
}

if (!g_spawn_command_line_sync(arg->rhs->str, &stdOut, &stdErr, &status, &error)) {
g_warning("Can't run '%s': %s", arg->rhs->str, error->message);
g_clear_error(&error);
res = CMD_ERROR | CMD_KEEPINPUT;
} else {
/* the commands success depends not on the return code of the
* called shell command, so we know the result already here */
res = CMD_SUCCESS;
}

if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
/* Read line by line stdOut and execute it */
ex_line = strtok(stdOut, "\n");
while (ex_line) {
res = ex_run_string(c, ex_line, false);
if (!(res & CMD_SUCCESS)) {
break;
}
ex_line = strtok(NULL, "\n");
}
} else {
vb_echo(c, MSG_ERROR, TRUE, "[%d] %s", WEXITSTATUS(status), stdErr);
}

return res;
}

static VbCmdResult ex_handlers(Client *c, const ExArg *arg)
{
char *p;
Expand Down