aboutsummaryrefslogtreecommitdiffstats
path: root/incuri.js
blob: 0ffb0823a0d02ea66c8d65bbae3adfcb98041d65 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
 * ==VimperatorPlugin==
 * @name           incuri.js
 * @description    increment the number in the URI
 * @description-ja URIに含まれる数字をインクリメント
 * @author         hogelog
 * @version        0.03
 * ==/VimperatorPlugin==
 *
 * COMMANDS:
 *  :decdomain   -> Decrement the number in the domain name
 *  :decfragment -> Decrement the number in the fragment ID
 *  :decpath     -> Decrement the number in the path name
 *  :decport     -> Decrement the number in the port number
 *  :decquery    -> Decrement the number in the query string
 *  :decuri      -> Decrement the number in the URI
 *  :incdomain   -> Increment the number in the domain name
 *  :incfragment -> Increment the number in the fragment ID
 *  :incpath     -> Increment the number in the path name
 *  :incport     -> Increment the number in the port number
 *  :incquery    -> Increment the number in the query string
 *  :incuri      -> Increment the number in the URI
 *
 */

(function() {
    var numreg = /^(.*\D|)(\d+)(\D*)$/;
    function numstr(num, len) {
        var str = String(num);
        while(str.length<len) {
            str = "0" + str;
        }
        return str;
    }
    function makeinc(f, p)
        function(args) {
            var l = window.content.location;
            var part = l[p];
            if(p == "port" && part == "") {
                part = ({
                    "ftp:" : "21", "http:" : "80", "https:" : "443"
                })[l.protocol] || part;
            }
            if(numreg.test(part)) {
                arg = args[0];
                let num = RegExp.$2;
                let quantity = !arg || isNaN(arg) ? 1 : parseInt(arg);
                let nextnum = numstr(f(parseInt(num, 10), quantity), num.length);
                let newpart = RegExp.$1 + nextnum + RegExp.$3;
                if(p == "href") {
                    window.content.location.href = newpart;
                } else {
                    window.content.location.href = [
                        "protocol", "//", "hostname", ":", "port", "pathname",
                        "search", "hash"
                    ].map(function(part) part.length > 2 ? p == part ? newpart
                                                                     : l[part]
                                                         : part)
                     .join("");
                }
            } else {
                liberator.echoerr("Cannot find a number in the " +
                                  p + " <" + part + ">");
            }
        };
    [
        ["uri",      "href",     "URI"],
        ["path",     "pathname", "path name"],
        ["query",    "search",   "query string"],
        ["fragment", "hash",     "fragment ID"],
        ["port",     "port",     "port number"],
        ["domain",   "hostname", "domain name"]
    ].forEach(function(part) {
        var [suffix, prop, name] = part;
        [
            ["In", 1], ["De", -1]
        ].forEach(function(direction) {
            var [prefix, dir] = direction;
            commands
                     .add([prefix.toLowerCase() + "c" + suffix],
                          prefix + "crement the number in the " + name + ".",
                          makeinc(function(x, q) x + dir * q, prop),
                          { argCount : "?" });
        });
    });
})();
// vim: set sw=4 ts=4 sts=4 et: