aboutsummaryrefslogtreecommitdiffstats
path: root/walk-input.js
blob: cf84325b97a2c90ca506dfda60efc195a6c5b7b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Vimperator plugin: 'Walk Input'
// Last Change: 2008-05-22.
// License: BSD
// Version: 1.0
// Maintainer: Takayama Fumihiko <tekezo@pqrs.org>

// ------------------------------------------------------------
// The focus walks <input> & <textarea> elements.
// If you type M-i first, the focus moves to "<input name='search' />".
// Then if you type M-i once more, the focus moves to "<input name='name' />".
//
// <html><body>
//     <input name="search" />
//     <a href="xxx">xxx</a>
//     <a href="yyy">yyy</a>
//     <a href="zzz">zzz</a>
//     <input name="name" />
//     <textarea name="comment" />
//  </body></html>

(function(){
var walkinput = function() {
    var win = document.commandDispatcher.focusedWindow;
    var d = win.document;
    var xpath = '//input[@type="text" or @type="password" or @type="search" or not(@type)] | //textarea';
    var list = d.evaluate(xpath, d, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    if (list.snapshotLength == 0) return;

    var focused = document.commandDispatcher.focusedElement;
    var current = null;
    var next = null;
    for (var i = 0; i < list.snapshotLength; ++i) {
        var e = list.snapshotItem(i);
        if (e == focused) {
            current = e;
        } else if (current && next == null) {
            next = e;
        }
    }
    if (next) {
        next.focus();
    } else {
        list.snapshotItem(0).focus();
    }
};

liberator.modules.mappings.add([liberator.modules.modes.NORMAL], ['<M-i>'], 'Walk Input Fields', function() { walkinput(); });
liberator.modules.mappings.add([liberator.modules.modes.INSERT], ['<M-i>'], 'Walk Input Fields', function() { walkinput(); });
liberator.modules.mappings.add([liberator.modules.modes.NORMAL], ['<A-i>'], 'Walk Input Fields', function() { walkinput(); });
liberator.modules.mappings.add([liberator.modules.modes.INSERT], ['<A-i>'], 'Walk Input Fields', function() { walkinput(); });
})();