From: Daniel Carl Date: Thu, 9 Jan 2020 23:20:37 +0000 (+0100) Subject: Give current selection as env on :shellcmd #592. X-Git-Url: https://git.owens.tech/assets/wrapped.html/assets/wrapped.html/git?a=commitdiff_plain;h=e0035ecdd056d38af1ec21c577f5c121344eb5f4;p=vimb.git Give current selection as env on :shellcmd #592. Give current selected text as environment variable $VIMB_SELECTION to scripts called by `:shellcmd`. --- diff --git a/CHANGELOG.md b/CHANGELOG.md index 04d152e..d49fbfc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/doc/vimb.1 b/doc/vimb.1 index 5d62936..5d70b0f 100644 --- a/doc/vimb.1 +++ b/doc/vimb.1 @@ -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 diff --git a/src/ex.c b/src/ex.c index 0afa414..acf233c 100644 --- a/src/ex.c +++ b/src/ex.c @@ -1105,7 +1105,7 @@ 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; @@ -1113,6 +1113,15 @@ static VbCmdResult ex_shellcmd(Client *c, const ExArg *arg) 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); diff --git a/src/ext-proxy.c b/src/ext-proxy.c index 51d2eb0..c0740c9 100644 --- a/src/ext-proxy.c +++ b/src/ext-proxy.c @@ -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. */ diff --git a/src/ext-proxy.h b/src/ext-proxy.h index 535dcf5..3abdca2 100644 --- a/src/ext-proxy.h +++ b/src/ext-proxy.h @@ -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 */