From: Daniel Carl Date: Mon, 5 Aug 2013 22:37:32 +0000 (+0200) Subject: Added command focus-input and new keybinding 'g-i'. X-Git-Url: https://git.owens.tech/assets/me.jpg/assets/me.jpg/git?a=commitdiff_plain;h=991acdde7cdc37fffe473f6cdd6b2cc6a2fe794d;p=vimb.git Added command focus-input and new keybinding 'g-i'. The new command and keybinding are use to focus the first editable element on the page and to switch vimb into insert mode. --- diff --git a/doc/vimb.1 b/doc/vimb.1 index 9e0cdfd..b1f07ef 100644 --- a/doc/vimb.1 +++ b/doc/vimb.1 @@ -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 diff --git a/src/command.c b/src/command.c index da6dd5a..92507ee 100644 --- a/src/command.c +++ b/src/command.c @@ -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; diff --git a/src/command.h b/src/command.h index 3cdd36f..e9020eb 100644 --- a/src/command.h +++ b/src/command.h @@ -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 */ diff --git a/src/default.h b/src/default.h index 929c222..a64931b 100644 --- a/src/default.h +++ b/src/default.h @@ -84,6 +84,7 @@ static char *default_config[] = { "nmap gu=descent", "nmap gU=descent!", "nmap =pass-through", + "nmap gi=focus-input", "cmap =next", "cmap =prev", "cmap =hist-prev", diff --git a/src/dom.c b/src/dom.c index f2fc6fa..5a40fd4 100644 --- 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. diff --git a/src/dom.h b/src/dom.h index 0c99b45..c9ba2c2 100644 --- 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);