Skip to content

Commit

Permalink
Give current selection as env on :shellcmd #592.
Browse files Browse the repository at this point in the history
Give current selected text as environment variable $VIMB_SELECTION to
scripts called by `:shellcmd`.
  • Loading branch information
fanglingsu committed Jan 9, 2020
1 parent 71693a2 commit e0035ec
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
* The new env variable `$VIMB_SELECTION` is set to the current selected text
whenever a `shellcmd` is run #592.
### Removed
* Expansion of `%` to the current opened URI for `:shellcmd` was removed
because it breaks the `x-hint-command` with URIs containing '%'. But it is
Expand Down
3 changes: 3 additions & 0 deletions doc/vimb.1
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,9 @@ The following environment variables are set for called shell commands.
This variable is set by Vimb everytime a new page is opened to the URI of the
page.
.TP
.B VIMB_SELECTION
This variable is set to the current selected text on the page.
.TP
.B VIMB_TITLE
Contains the title of the current opened page.
.TP
Expand Down
11 changes: 10 additions & 1 deletion src/ex.c
Original file line number Diff line number Diff line change
Expand Up @@ -1105,14 +1105,23 @@ static VbCmdResult ex_set(Client *c, const ExArg *arg)
static VbCmdResult ex_shellcmd(Client *c, const ExArg *arg)
{
int status;
char *stdOut = NULL, *stdErr = NULL;
char *stdOut = NULL, *stdErr = NULL, *selection = 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 (arg->bang) {
if (!g_spawn_command_line_async(arg->rhs->str, &error)) {
g_warning("Can't run '%s': %s", arg->rhs->str, error->message);
Expand Down
25 changes: 25 additions & 0 deletions src/ext-proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,31 @@ void ext_proxy_unlock_input(Client *c, const char *element_id)
dbus_call(c, "UnlockInput", g_variant_new("(ts)", c->page_id, element_id), NULL);
}

/**
* Returns the current selection if there is one as newly allocates string.
*
* Result must be freed by caller with g_free.
*/
char *ext_proxy_get_current_selection(Client *c)
{
char *selection, *js;
gboolean success;
GVariant *jsreturn;

js = g_strdup_printf("getSelection().toString();");
jsreturn = ext_proxy_eval_script_sync(c, js);
g_variant_get(jsreturn, "(bs)", &success, &selection);
g_free(js);

if (!success) {
g_warning("can not get current selection: %s", selection);
g_free(selection);
return NULL;
}

return selection;
}

/**
* Call a dbus method.
*/
Expand Down
1 change: 1 addition & 0 deletions src/ext-proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ void ext_proxy_focus_input(Client *c);
void ext_proxy_set_header(Client *c, const char *headers);
void ext_proxy_lock_input(Client *c, const char *element_id);
void ext_proxy_unlock_input(Client *c, const char *element_id);
char *ext_proxy_get_current_selection(Client *c);

#endif /* end of include guard: _EXT_PROXY_H */

0 comments on commit e0035ec

Please sign in to comment.