Added command focus-input and new keybinding 'g-i'.
authorDaniel Carl <danielcarl@gmx.de>
Mon, 5 Aug 2013 22:37:32 +0000 (00:37 +0200)
committerDaniel Carl <danielcarl@gmx.de>
Mon, 5 Aug 2013 22:40:33 +0000 (00:40 +0200)
The new command and keybinding are use to focus the first editable element on
the page and to switch vimb into insert mode.

doc/vimb.1
src/command.c
src/command.h
src/default.h
src/dom.c
src/dom.h

index 9e0cdfd..b1f07ef 100644 (file)
@@ -409,6 +409,10 @@ Toggles the webinspector for current page. This is only available if the config
 .B quit, q
 Close the browser.
 .TP
+.B focus-input
+Set the cursor to the first found editable element on the page and switch
+PROJECT into insert mode.
+.TP
 .B source
 Toggle between normal view and source view for the current page.
 .TP
@@ -477,6 +481,10 @@ Toggle show html source of current page.
 .B g\-F
 Opend the Web Inspector for current page.
 .TP
+.B g\-i
+Set cursor to the first editable element in the page and switch to insert
+mode.
+.TP
 .B :
 Start command mode and print `:' to the input box.
 .TP
index da6dd5a..92507ee 100644 (file)
@@ -132,6 +132,7 @@ static CommandInfo cmd_list[] = {
     {"queue-clear",               NULL,    command_queue,                {COMMAND_QUEUE_CLEAR}},
 #endif
     {"pass-through",              NULL,    command_mode,                 {VB_MODE_PASSTHROUGH}},
+    {"focus-input",               NULL,    command_focusinput,           {0}},
 };
 
 static void editor_resume(GPid pid, int status, OpenEditorData *data);
@@ -950,6 +951,16 @@ gboolean command_mode(const Arg *arg)
     return vb_set_mode(arg->i, false);
 }
 
+gboolean command_focusinput(const Arg *arg)
+{
+    if (dom_focus_input(vb.gui.webview)) {
+        vb_set_mode(VB_MODE_INSERT, false);
+
+        return true;
+    }
+    return false;
+}
+
 gboolean command_editor(const Arg *arg)
 {
     char *file_path = NULL;
index 3cdd36f..e9020eb 100644 (file)
@@ -94,5 +94,6 @@ gboolean command_shellcmd(const Arg *arg);
 gboolean command_queue(const Arg *arg);
 #endif
 gboolean command_mode(const Arg *arg);
+gboolean command_focusinput(const Arg *arg);
 
 #endif /* end of include guard: _COMMAND_H */
index 929c222..a64931b 100644 (file)
@@ -84,6 +84,7 @@ static char *default_config[] = {
     "nmap gu=descent",
     "nmap gU=descent!",
     "nmap <ctrl-z>=pass-through",
+    "nmap gi=focus-input",
     "cmap <tab>=next",
     "cmap <shift-tab>=prev",
     "cmap <up>=hist-prev",
index f2fc6fa..5a40fd4 100644 (file)
--- a/src/dom.c
+++ b/src/dom.c
@@ -57,6 +57,33 @@ void dom_clear_focus(WebKitWebView *view)
     }
 }
 
+/**
+ * Set focus to the first found editable element and returns if a element was
+ * found to focus.
+ */
+gboolean dom_focus_input(WebKitWebView *view)
+{
+    gboolean found = false;
+    gulong count;
+    WebKitDOMDocument *doc = webkit_web_view_get_dom_document(view);
+    WebKitDOMNodeList *elems = webkit_dom_document_get_elements_by_tag_name(doc, "*");
+    Element *elem;
+
+    count = webkit_dom_node_list_get_length(elems);
+    for (int i = 0; i < count; i++) {
+        elem = WEBKIT_DOM_ELEMENT(webkit_dom_node_list_item(elems, i));
+
+        if (dom_is_editable(elem)) {
+            webkit_dom_element_focus(elem);
+            found = true;
+            break;
+        }
+    }
+    g_object_unref(elems);
+
+    return found;
+}
+
 /**
  * Indicates if the given dom element is an editable element like text input,
  * password or textarea.
index 0c99b45..c9ba2c2 100644 (file)
--- a/src/dom.h
+++ b/src/dom.h
@@ -44,6 +44,7 @@ typedef struct {
 
 void dom_check_auto_insert(WebKitWebView *view);
 void dom_clear_focus(WebKitWebView *view);
+gboolean dom_focus_input(WebKitWebView *view);
 gboolean dom_is_editable(Element *element);
 Element *dom_get_active_element(WebKitWebView *view);
 const char *dom_editable_element_get_value(Element *element);