Convert old hints.c to the webkit2 era
authorVirgil Dupras <hsoft@hardcoded.net>
Thu, 27 Apr 2017 02:54:30 +0000 (22:54 -0400)
committerDaniel Carl <danielcarl@gmx.de>
Sat, 6 May 2017 23:31:56 +0000 (01:31 +0200)
Lots of it is still disabled, but the basic hint showing happening when
pressing 'f' works.

src/config.def.h
src/ex.c
src/hints.c [new file with mode: 0644]
src/hints.h [new file with mode: 0644]
src/normal.c
src/scripts/hints.js
src/setting.c

index 95e8970..0e82137 100644 (file)
@@ -51,6 +51,8 @@
 #define SETTING_GUI_FONT_EMPH                 "bold 10pt monospace"
 #define SETTING_HOME_PAGE                     "about:blank"
 
+#define MAXIMUM_HINTS              500
+
 /* CSS style use on creating hints. This might also be averrules by css out of
  * $XDG_CONFIG_HOME/vimb/style.css file. */
 #define HINT_CSS "#_hintContainer{\
index 11fd7b6..39cc60b 100644 (file)
--- a/src/ex.c
+++ b/src/ex.c
@@ -33,6 +33,7 @@
 #include "config.h"
 #include "ex.h"
 #include "handler.h"
+#include "hints.h"
 #include "history.h"
 #include "main.h"
 #include "map.h"
@@ -225,9 +226,7 @@ void ex_enter(Client *c)
 void ex_leave(Client *c)
 {
     completion_clean(c);
-#if 0
-    hints_clear();
-#endif
+    hints_clear(c);
 }
 
 /**
@@ -397,7 +396,7 @@ void ex_input_changed(Client *c, const char *text)
     switch (*text) {
         case ';': /* fall through - the modes are handled by hints_create */
         case 'g':
-            /* TODO create hints */
+            hints_create(c, text);
             break;
         case '/': /* fall through */
         case '?':
diff --git a/src/hints.c b/src/hints.c
new file mode 100644 (file)
index 0000000..1792120
--- /dev/null
@@ -0,0 +1,379 @@
+/**
+ * vimb - a webkit based vim like browser.
+ *
+ * Copyright (C) 2012-2017 Daniel Carl
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see http://www.gnu.org/licenses/.
+ */
+
+#include "config.h"
+#include <string.h>
+#include <JavaScriptCore/JavaScript.h>
+#include <gdk/gdkkeysyms.h>
+#include <gdk/gdkkeysyms-compat.h>
+#include "hints.h"
+#include "main.h"
+#include "ascii.h"
+#include "command.h"
+#include "hints.js.h"
+#include "input.h"
+#include "map.h"
+#include "ext-proxy.h"
+
+static struct {
+    JSObjectRef    obj;       /* the js object */
+    char           mode;      /* mode identifying char - that last char of the hint prompt */
+    int            promptlen; /* length of the hint prompt chars 2 or 3 */
+    gboolean       gmode;     /* indicate if the hints 'g' mode is used */
+    JSContextRef   ctx;
+    /* holds the setting if JavaScript can open windows automatically that we
+     * have to change to open windows via hinting */
+    gboolean       allow_open_win;
+    guint          timeout_id;
+} hints;
+
+extern struct Vimb vb;
+
+static gboolean call_hints_function(Client *c, const char *func, int count, JSValueRef params[]);
+static void fire_timeout(Client *c, gboolean on);
+static gboolean fire_cb(gpointer data);
+
+
+VbResult hints_keypress(Client *c, int key)
+{
+    JSValueRef arguments[1];
+
+    if (key == KEY_CR) {
+        hints_fire(c);
+
+        return RESULT_COMPLETE;
+    } else if (key == CTRL('H')) {
+        fire_timeout(c, false);
+        arguments[0] = JSValueMakeNull(hints.ctx);
+        if (call_hints_function(c, "update", 1, arguments)) {
+            return RESULT_COMPLETE;
+        }
+    } else if (key == KEY_TAB) {
+        fire_timeout(c, false);
+        hints_focus_next(c, false);
+
+        return RESULT_COMPLETE;
+    } else if (key == KEY_SHIFT_TAB) {
+        fire_timeout(c, false);
+        hints_focus_next(c, true);
+
+        return RESULT_COMPLETE;
+    } else {
+        fire_timeout(c, true);
+        /* try to handle the key by the javascript */
+        /* arguments[0] = js_string_to_ref(hints.ctx, (char[]){key, '\0'}); */
+        /* if (call_hints_function(c, "update", 1, arguments)) {            */
+        /*     return RESULT_COMPLETE;                                      */
+        /* }                                                                */
+    }
+
+    fire_timeout(c, false);
+    return RESULT_ERROR;
+}
+
+void hints_clear(Client *c)
+{
+    if (c->mode->flags & FLAG_HINTING) {
+        c->mode->flags &= ~FLAG_HINTING;
+        vb_input_set_text(c, "");
+
+        call_hints_function(c, "clear", 0, NULL);
+
+        g_signal_emit_by_name(c->webview, "hovering-over-link", NULL, NULL);
+
+        /* if open window was not allowed for JavaScript, restore this */
+        /* if (!hints.allow_open_win) {                                                                                  */
+        /*     WebKitWebSettings *setting = webkit_web_view_get_settings(c->webview);                                    */
+        /*     g_object_set(G_OBJECT(setting), "javascript-can-open-windows-automatically", hints.allow_open_win, NULL); */
+        /* }                                                                                                             */
+    }
+}
+
+void hints_create(Client *c, const char *input)
+{
+    char *jscode;
+
+    /* check if the input contains a valid hinting prompt */
+    if (!hints_parse_prompt(input, &hints.mode, &hints.gmode)) {
+        /* if input is not valid, clear possible previous hint mode */
+        if (c->mode->flags & FLAG_HINTING) {
+            vb_enter(c, 'n');
+        }
+        return;
+    }
+
+    if (!(c->mode->flags & FLAG_HINTING)) {
+        c->mode->flags |= FLAG_HINTING;
+
+        /* WebKitWebSettings *setting = webkit_web_view_get_settings(c->webview); */
+
+        /* before we enable JavaScript to open new windows, we save the actual
+         * value to be able restore it after hints where fired */
+        /* g_object_get(G_OBJECT(setting), "javascript-can-open-windows-automatically", &(hints.allow_open_win), NULL); */
+
+        /* if window open is already allowed there's no need to allow it again */
+        /* if (!hints.allow_open_win) {                                                                  */
+        /*     g_object_set(G_OBJECT(setting), "javascript-can-open-windows-automatically", true, NULL); */
+        /* }                                                                                             */
+
+        hints.promptlen = hints.gmode ? 3 : 2;
+
+        jscode = g_strdup_printf("hints.init('%s', %s, %d, '%s', %s, %s);",
+            (char[]){hints.mode, '\0'},
+            hints.gmode ? "true" : "false",
+            MAXIMUM_HINTS,
+            GET_CHAR(c, "hintkeys"),
+            GET_BOOL(c, "hint-follow-last") ? "true" : "false",      
+            GET_BOOL(c, "hint-number-same-length") ? "true" : "false"
+        );
+
+        ext_proxy_eval_script(c, jscode, NULL);
+        g_free(jscode);
+
+        /* if hinting is started there won't be any additional filter given and
+         * we can go out of this function */
+        return;
+    }
+
+    /* JSValueRef arguments[] = {js_string_to_ref(hints.ctx, *(input + hints.promptlen) ? input + hints.promptlen : "")}; */
+    /* call_hints_function(c, "filter", 1, arguments);                                                                    */
+}
+
+void hints_focus_next(Client *c, const gboolean back)
+{
+    JSValueRef arguments[] = {
+        JSValueMakeNumber(hints.ctx, back)
+    };
+    call_hints_function(c, "focus", 1, arguments);
+}
+
+void hints_fire(Client *c)
+{
+    call_hints_function(c, "fire", 0, NULL);
+}
+
+void hints_follow_link(Client *c, const gboolean back, int count)
+{
+    /* char *json = g_strdup_printf(                            */
+    /*     "[%s]",                                              */
+    /*     back ? vb.config.prevpattern : vb.config.nextpattern */
+    /* );                                                       */
+
+    /* JSValueRef arguments[] = {                               */
+    /*     js_string_to_ref(hints.ctx, back ? "prev" : "next"), */
+    /*     js_object_to_ref(hints.ctx, json),                   */
+    /*     JSValueMakeNumber(hints.ctx, count)                  */
+    /* };                                                       */
+    /* g_free(json);                                            */
+
+    /* call_hints_function(c, "followLink", 3, arguments);         */
+}
+
+void hints_increment_uri(Client *c, int count)
+{
+    JSValueRef arguments[] = {
+        JSValueMakeNumber(hints.ctx, count)
+    };
+
+    call_hints_function(c, "incrementUri", 1, arguments);
+}
+
+/**
+ * Checks if the given hint prompt belong to a known and valid hints mode and
+ * parses the mode and is_gmode into given pointers.
+ *
+ * The given prompt sting may also contain additional chars after the prompt.
+ *
+ * @prompt:   String to be parsed as prompt. The Prompt can be followed by
+ *            additional characters.
+ * @mode:     Pointer to char that will be filled with mode char if prompt was
+ *            valid and given pointer is not NULL.
+ * @is_gmode: Pointer to gboolean to be filled with the flag to indicate gmode
+ *            hinting.
+ */
+gboolean hints_parse_prompt(const char *prompt, char *mode, gboolean *is_gmode)
+{
+    gboolean res;
+    char pmode = '\0';
+#ifdef FEATURE_QUEUE
+    static char *modes   = "eiIoOpPstTxyY";
+    static char *g_modes = "IpPstyY";
+#else
+    static char *modes   = "eiIoOstTxyY";
+    static char *g_modes = "IstyY";
+#endif
+
+    if (!prompt) {
+        return false;
+    }
+
+    /* get the mode identifying char from prompt */
+    if (*prompt == ';') {
+        pmode = prompt[1];
+    } else if (*prompt == 'g' && strlen(prompt) >= 3) {
+        /* get mode for g;X hint modes */
+        pmode = prompt[2];
+    }
+
+    /* no mode found in prompt */
+    if (!pmode) {
+        return false;
+    }
+
+    res = *prompt == 'g'
+        ? strchr(g_modes, pmode) != NULL
+        : strchr(modes, pmode) != NULL;
+
+    /* fill pointer only if the prompt was valid */
+    if (res) {
+        if (mode != NULL) {
+            *mode = pmode;
+        }
+        if (is_gmode != NULL) {
+            *is_gmode = *prompt == 'g';
+        }
+    }
+
+    return res;
+}
+
+static gboolean call_hints_function(Client *c, const char *func, int count, JSValueRef params[])
+{
+    /* char *value = js_object_call_function(hints.ctx, hints.obj, func, count, params); */
+    char *value = "";
+    return true;
+
+    g_return_val_if_fail(value != NULL, false);
+
+    if (!strncmp(value, "ERROR:", 6)) {
+        g_free(value);
+        return false;
+    }
+
+    if (!strncmp(value, "OVER:", 5)) {
+        g_signal_emit_by_name(
+            c->webview, "hovering-over-link", NULL, *(value + 5) == '\0' ? NULL : (value + 5)
+        );
+        g_free(value);
+
+        return true;
+    }
+
+    /* following return values mark fired hints */
+    if (!strncmp(value, "DONE:", 5)) {
+        fire_timeout(c, false);
+        /* Change to normal mode only if we are currently in command mode and
+         * we are not in g-mode hinting. This is required to not switch to
+         * normal mode when the hinting triggered a click that set focus on
+         * editable element that lead vimb to switch to input mode. */
+        if (!hints.gmode && c->mode->id == 'c') {
+            vb_enter(c, 'n');
+        }
+    } else if (!strncmp(value, "INSERT:", 7)) {
+        fire_timeout(c, false);
+        vb_enter(c, 'i');
+        if (hints.mode == 'e') {
+            input_open_editor(c);
+        }
+    } else if (!strncmp(value, "DATA:", 5)) {
+        fire_timeout(c, false);
+        /* switch first to normal mode - else we would clear the inputbox
+         * on switching mode also if we want to show yanked data */
+        if (!hints.gmode) {
+            vb_enter(c, 'n');
+        }
+
+        char *v = (value + 5);
+        Arg a   = {0};
+        /* put the hinted value into register "; */
+        vb_register_add(c, ';', v);
+        switch (hints.mode) {
+            /* used if images should be opened */
+            case 'i':
+            case 'I':
+                a.s = v;
+                a.i = (hints.mode == 'I') ? TARGET_NEW : TARGET_CURRENT;
+                vb_load_uri(c, &a);
+                break;
+
+            case 'O':
+            case 'T':
+                vb_echo(c, MSG_NORMAL, false, "%s %s", (hints.mode == 'T') ? ":tabopen" : ":open", v);
+                if (!hints.gmode) {
+                    vb_enter(c, 'c');
+                }
+                break;
+
+            case 's':
+                a.s = v;
+                a.i = COMMAND_SAVE_URI;
+                command_save(c, &a);
+                break;
+
+            case 'x':
+                map_handle_string(c, GET_CHAR(c, "x-hint-command"), true);
+                break;
+
+            case 'y':
+            case 'Y':
+                a.i = COMMAND_YANK_ARG;
+                a.s = v;
+                command_yank(c, &a, c->state.current_register);
+                break;
+
+#ifdef FEATURE_QUEUE
+            case 'p':
+            case 'P':
+                a.s = v;
+                a.i = (hints.mode == 'P') ? COMMAND_QUEUE_UNSHIFT : COMMAND_QUEUE_PUSH;
+                command_queue(c, &a);
+                break;
+#endif
+        }
+    }
+    g_free(value);
+    return true;
+}
+
+static void fire_timeout(Client *c, gboolean on)
+{
+    int millis;
+    /* remove possible timeout function */
+    if (hints.timeout_id) {
+        g_source_remove(hints.timeout_id);
+        hints.timeout_id = 0;
+    }
+
+    if (on) {
+        millis = GET_INT(c, "hint-timeout");
+        if (millis) {
+            hints.timeout_id = g_timeout_add(millis, (GSourceFunc)fire_cb, c);
+        }
+    }
+}
+
+static gboolean fire_cb(gpointer data)
+{
+    hints_fire(data);
+
+    /* remove timeout id for the timeout that is removed by return value of
+     * false automatic */
+    hints.timeout_id = 0;
+    return false;
+}
diff --git a/src/hints.h b/src/hints.h
new file mode 100644 (file)
index 0000000..6821554
--- /dev/null
@@ -0,0 +1,34 @@
+/**
+ * vimb - a webkit based vim like browser.
+ *
+ * Copyright (C) 2012-2016 Daniel Carl
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see http://www.gnu.org/licenses/.
+ */
+
+#ifndef _HINTS_H
+#define _HINTS_H
+
+#include "main.h"
+
+VbResult hints_keypress(Client *c, int key);
+void hints_create(Client *c, const char *input);
+void hints_fire(Client *c);
+void hints_follow_link(Client *c, gboolean back, int count);
+void hints_increment_uri(Client *c, int count);
+gboolean hints_parse_prompt(const char *prompt, char *mode, gboolean *is_gmode);
+void hints_clear(Client *c);
+void hints_focus_next(Client *c, const gboolean back);
+
+#endif /* end of include guard: _HINTS_H */
index 1dc3261..156ef52 100644 (file)
@@ -408,9 +408,7 @@ static VbResult normal_ex(Client *c, const NormalCmdInfo *info)
     if (info->key == 'F') {
         vb_enter_prompt(c, 'c', ";t", TRUE);
     } else if (info->key == 'f') {
-        g_print("firing!");
-        ext_proxy_eval_script(c, "testHint();", NULL);
-        /* vb_enter_prompt(c, 'c', ";o", TRUE); */
+        vb_enter_prompt(c, 'c', ";o", TRUE);
     } else {
         char prompt[2] = {info->key, '\0'};
         vb_enter_prompt(c, 'c', prompt, TRUE);
index 04cd51c..6f3530f 100644 (file)
@@ -538,8 +538,3 @@ var hints = Object.freeze((function(){
         focus:        focus,
     };
 })());
-
-function testHint() {
-    console.log("harmless testing!");
-    hints.init('t', false, 500, '0123456789', false, false);
-}
index 6b9cee2..a64f0de 100644 (file)
@@ -87,6 +87,10 @@ void setting_init(Client *c)
     setting_add(c, "fontsize", TYPE_INTEGER, &i, webkit, 0, "default-font-size");
     setting_add(c, "frame-flattening", TYPE_BOOLEAN, &off, webkit, 0, "enable-frame-flattening");
     setting_add(c, "header", TYPE_CHAR, &"", headers, FLAG_LIST|FLAG_NODUP, "header");
+    setting_add(c, "hint-timeout", TYPE_INTEGER, &i, NULL, 0, NULL);
+    setting_add(c, "hintkeys", TYPE_CHAR, &"0123456789", NULL, 0, NULL);
+    setting_add(c, "hint-follow-last", TYPE_BOOLEAN, &on, NULL, 0, NULL);
+    setting_add(c, "hint-number-same-length", TYPE_BOOLEAN, &off, NULL, 0, NULL);
     setting_add(c, "html5-database", TYPE_BOOLEAN, &on, webkit, 0, "enable-html5-database");
     setting_add(c, "html5-local-storage", TYPE_BOOLEAN, &on, webkit, 0, "enable-html5-local-storage");
     setting_add(c, "hyperlink-auditing", TYPE_BOOLEAN, &off, webkit, 0, "enable-hyperlink-auditing");