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
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
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;
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);
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.
*/
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 */