Show output of :eval in inputbox.
authorDaniel Carl <danielcarl@gmx.de>
Tue, 27 Sep 2016 22:03:32 +0000 (00:03 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Tue, 27 Sep 2016 22:03:32 +0000 (00:03 +0200)
src/ex.c
src/main.c

index e3a5a71..98570d1 100644 (file)
--- a/src/ex.c
+++ b/src/ex.c
@@ -22,6 +22,7 @@
  * commands from inputbox and the ex commands.
  */
 
+#include <JavaScriptCore/JavaScript.h>
 #include <string.h>
 #include <sys/wait.h>
 
@@ -121,6 +122,8 @@ static VbCmdResult execute(Client *c, const ExArg *arg);
 
 static VbCmdResult ex_bookmark(Client *c, const ExArg *arg);
 static VbCmdResult ex_eval(Client *c, const ExArg *arg);
+static void ex_eval_javascript_finished(GObject *object,
+        GAsyncResult *result, Client *c);
 static VbCmdResult ex_hardcopy(Client *c, const ExArg *arg);
 static VbCmdResult ex_map(Client *c, const ExArg *arg);
 static VbCmdResult ex_unmap(Client *c, const ExArg *arg);
@@ -747,11 +750,51 @@ static VbCmdResult ex_bookmark(Client *c, const ExArg *arg)
 
 static VbCmdResult ex_eval(Client *c, const ExArg *arg)
 {
-    /* TODO allow to get the return value and possible errors. */
-    webkit_web_view_run_javascript(c->webview, arg->rhs->str, NULL, NULL, NULL);
+    /* Called as :eval! - don't print to inputbox. */
+    if (arg->bang) {
+        webkit_web_view_run_javascript(c->webview, arg->rhs->str, NULL, NULL, NULL);
+    } else {
+        webkit_web_view_run_javascript(c->webview, arg->rhs->str, NULL,
+                (GAsyncReadyCallback)ex_eval_javascript_finished, c);
+    }
+
     return CMD_SUCCESS;
 }
 
+static void ex_eval_javascript_finished(GObject *object,
+        GAsyncResult *result, Client *c)
+{
+    WebKitJavascriptResult *js_result;
+    JSValueRef value;
+    JSGlobalContextRef context;
+    GError *error = NULL;
+
+    js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object), result, &error);
+    if (!js_result) {
+        vb_echo(c, MSG_ERROR, TRUE, "%s", error->message);
+        g_error_free(error);
+
+        return;
+    }
+
+    context = webkit_javascript_result_get_global_context(js_result);
+    value   = webkit_javascript_result_get_value(js_result);
+    if (JSValueIsString(context, value)) {
+        JSStringRef str_ref;
+        char *string;
+        size_t len;
+
+        str_ref = JSValueToStringCopy(context, value, NULL);
+        len     = JSStringGetMaximumUTF8CStringSize(str_ref);
+        string  = g_new(char, len);
+        JSStringGetUTF8CString(str_ref, string, len);
+        JSStringRelease(str_ref);
+        vb_echo(c, MSG_NORMAL, FALSE, "%s", string);
+        g_free(string);
+    }
+    webkit_javascript_result_unref(js_result);
+}
+
 static VbCmdResult ex_hardcopy(Client *c, const ExArg *arg)
 {
     /* TODO no implemented yet */
index 8644123..93141ad 100644 (file)
@@ -622,6 +622,7 @@ static void input_print(Client *c, gboolean force, MessageType type,
     /* apply input style only if the message type was changed */
     if (type != c->state.input_type) {
         c->state.input_type = type;
+        vb_input_update_style(c);
     }
     vb_input_set_text(c, message);
     if (hide) {