Give current selection as env on :shellcmd #592.
authorDaniel Carl <danielcarl@gmx.de>
Thu, 9 Jan 2020 23:20:37 +0000 (00:20 +0100)
committerDaniel Carl <danielcarl@gmx.de>
Thu, 9 Jan 2020 23:22:02 +0000 (00:22 +0100)
Give current selected text as environment variable $VIMB_SELECTION to
scripts called by `:shellcmd`.

CHANGELOG.md
doc/vimb.1
src/ex.c
src/ext-proxy.c
src/ext-proxy.h

index 04d152e..d49fbfc 100644 (file)
@@ -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
index 5d62936..5d70b0f 100644 (file)
@@ -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
index 0afa414..acf233c 100644 (file)
--- 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);
index 51d2eb0..c0740c9 100644 (file)
@@ -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.
  */
index 535dcf5..3abdca2 100644 (file)
@@ -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 */