Add support for marks, based on vimb-2.12
authorLeonardo Taccari <iamleot@gmail.com>
Mon, 15 Oct 2018 01:27:19 +0000 (03:27 +0200)
committerLeonardo Taccari <iamleot@gmail.com>
Mon, 15 Oct 2018 01:27:19 +0000 (03:27 +0200)
 - Introduce a scroll_top field in State, similar to scroll_percent but in pixel
 - Adjust the webextension to communicate and update scroll_top
 - Implement normal_map(), mostly based on vimb-2.12

src/ext-proxy.c
src/main.h
src/normal.c
src/webextension/ext-main.c

index 4c99bae..51d2eb0 100644 (file)
@@ -180,16 +180,17 @@ static void on_vertical_scroll(GDBusConnection *connection,
         const char *interface_name, const char *signal_name,
         GVariant *parameters, gpointer data)
 {
-    glong max;
+    glong max, top;
     guint percent;
     guint64 pageid;
     Client *c;
 
-    g_variant_get(parameters, "(ttq)", &pageid, &max, &percent);
+    g_variant_get(parameters, "(ttqt)", &pageid, &max, &percent, &top);
     c = vb_get_client_for_page_id(pageid);
     if (c) {
         c->state.scroll_max     = max;
         c->state.scroll_percent = percent;
+        c->state.scroll_top     = top;
     }
 
     vb_statusbar_update(c);
index 35681a1..56ba9e0 100644 (file)
@@ -154,12 +154,13 @@ struct State {
 
 #define PROMPT_SIZE 4
     char                prompt[PROMPT_SIZE];/* current prompt ':', 'g;t', '/' including nul */
-    gdouble             marks[MARK_SIZE];   /* holds marks set to page with 'm{markchar}' */
+    glong               marks[MARK_SIZE];   /* holds marks set to page with 'm{markchar}' */
     guint               input_timer;
     MessageType         input_type;
     StatusType          status_type;
     glong               scroll_max;         /* Maxmimum scrollable height of the document. */
-    guint               scroll_percent;     /* Current position of the viewport in document. */
+    guint               scroll_percent;     /* Current position of the viewport in document (percent). */
+    glong               scroll_top;         /* Current position of the viewport in document (pixel). */
     char                *title;             /* Window title of the client. */
 
     char                *reg[REG_SIZE];     /* holds the yank buffers */
index a2028a7..02ba27b 100644 (file)
@@ -521,7 +521,37 @@ static VbResult normal_input_open(Client *c, const NormalCmdInfo *info)
 
 static VbResult normal_mark(Client *c, const NormalCmdInfo *info)
 {
-    /* TODO implement setting of marks - we need to get the position in the pagee from the Webextension */
+    glong current;
+    char *js, *mark;
+    int idx;
+
+    /* check if the second char is a valid mark char */
+    if (!(mark = strchr(MARK_CHARS, info->key2))) {
+        return RESULT_ERROR;
+    }
+
+    /* get the index of the mark char */
+    idx = mark - MARK_CHARS;
+
+    if ('m' == info->key) {
+        c->state.marks[idx] = c->state.scroll_top;
+    } else {
+        /* check if the mark was set */
+        if ((int)(c->state.marks[idx] - .5) < 0) {
+            return RESULT_ERROR;
+        }
+
+        current = c->state.scroll_top;
+
+        /* jump to the location */
+        js = g_strdup_printf("window.scroll(window.screenLeft,%ld);", c->state.marks[idx]);
+        ext_proxy_eval_script(c, js, NULL);
+        g_free(js);
+
+        /* save previous adjust as last position */
+        c->state.marks[MARK_TICK] = current;
+    }
+
     return RESULT_COMPLETE;
 }
 
index 10c525c..998a329 100644 (file)
@@ -80,6 +80,7 @@ static const char introspection_xml[] =
     "   <arg type='t' name='page_id' direction='out'/>"
     "   <arg type='t' name='max' direction='out'/>"
     "   <arg type='q' name='percent' direction='out'/>"
+    "   <arg type='t' name='top' direction='out'/>"
     "  </signal>"
     "  <method name='SetHeaderSetting'>"
     "   <arg type='s' name='headers' direction='in'/>"
@@ -249,7 +250,7 @@ static void on_document_scroll(WebKitDOMEventTarget *target, WebKitDOMEvent *eve
 
     if (doc) {
         WebKitDOMElement *body, *de;
-        glong max = 0, scrollTop, scrollHeight, clientHeight;
+        glong max = 0, top = 0, scrollTop, scrollHeight, clientHeight;
         guint percent = 0;
 
         de = webkit_dom_document_get_document_element(doc);
@@ -276,10 +277,11 @@ static void on_document_scroll(WebKitDOMEventTarget *target, WebKitDOMEvent *eve
         max = scrollHeight - clientHeight;
         if (max > 0) {
             percent = (guint)(0.5 + (scrollTop * 100 / max));
+            top = scrollTop;
         }
 
-        dbus_emit_signal("VerticalScroll", g_variant_new("(ttq)",
-                webkit_web_page_get_id(page), max, percent));
+        dbus_emit_signal("VerticalScroll", g_variant_new("(ttqt)",
+                webkit_web_page_get_id(page), max, percent, top));
     }
 }