aboutsummaryrefslogtreecommitdiffstats
path: root/char-hints-mod2.js
blob: 0d285b2c1c69462e47ca8f03aa503372f76ae1c5 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// PLUGIN_INFO//{{{
var PLUGIN_INFO =
<VimperatorPlugin>
    <name>{NAME}</name>
    <description>character hint mode.</description>
    <author mail="konbu.komuro@gmail.com" homepage="http://d.hatena.ne.jp/hogelog/">hogelog</author>
    <version>0.1</version>
    <minVersion>2.0pre 2008/12/12</minVersion>
    <maxVersion>2.0a1</maxVersion>
    <detail><![CDATA[

== Usage ==
In default setting,
input quickhint in lowercase
and
select charhint label in uppercase.

== OPTIONS ==
set histchars="hjkl":
    show char-hint use h, j, k, l.
set charhintinput=uppercase|lowercase:
    charhint input in uppercase|lowercase
set charhintshow=uppercase|lowercase:
    charhint show in uppercase|lowercase

== TODO ==
     ]]></detail>
    <detail lang="ja"><![CDATA[

== Usage ==
デフォルトの設定では
小文字は候補を絞るためのテキスト入力に
大文字は文字ラベルの選択に使います

== OPTIONS ==
set histchars="hjkl":
    show char-hint use h, j, k, l.
set charhintinput=uppercase|lowercase:
    charhint input in uppercase|lowercase
set charhintshow=uppercase|lowercase:
    charhint show in uppercase|lowercase

== TODO ==
     ]]></detail>
</VimperatorPlugin>;
//}}}

(function () {

    const DEFAULT_HINTCHARS = "HJKLASDFGYUIOPQWERTNMZXCVB";
    const hintContext = hints.addMode;

    let inputCase = function(str) str.toUpperCase();
    let inputRegex = /[A-Z]/;
    let showCase = function(str) str.toUpperCase();

    options.add(["hintchars", "hchar"], //{{{
        "Hint characters",
        "string", DEFAULT_HINTCHARS); //}}}
    options.add(["charhintinput", "chinput"], //{{{
        "Character Hint Input",
        "string", "uppercase",
        {
            setter: function (value)
            {
                switch (value)
                {
                    default:
                    case "uppercase":
                        inputCase = function(str) str.toUpperCase();
                        inputRegex = /[A-Z]/;
                        break;
                    case "lowercase":
                        inputCase = function(str) str.toLowerCase();
                        inputRegex = /[a-z]/;
                        break;
                }
            },
            completer: function () [
                ["uppercase", "Input charhint UpperCase"],
                ["lowercase", "Input charhint LowerCase"],
            ],
        }); //}}}
    options.add(["charhintshow", "chshow"], //{{{
        "Character Hint Show",
        "string", "uppercase",
        {
            setter: function (value)
            {
                switch (value)
                {
                    default:
                    case "uppercase":
                        showCase = function(str) str.toUpperCase();
                        break;
                    case "lowercase":
                        showCase = function(str) str.toLowerCase();
                        break;
                }
            },
            completer: function () [
                ["uppercase", "show charhint UpperCase"],
                ["lowercase", "show charhint LowerCase"],
            ],
        }); //}}}
        

    function chars2num(chars) //{{{
    {
        let num = 0;
        let hintchars = inputCase(options.hintchars);
        let base = hintchars.length;
        for(let i=0,l=chars.length;i<l;++i) {
            num = base*num + hintchars.indexOf(chars[i]);
        }
        return num;
    } //}}}
    function num2chars(num) //{{{
    {
        let chars = "";
        let hintchars = inputCase(options.hintchars);
        let base = hintchars.length;
        do {
            chars = hintchars[((num % base))] + chars;
            num = Math.floor(num/base);
        } while(num>0);

        return chars;
    } //}}}
    function showCharHints() //{{{
    {
        function showHints(win)
        {
            for(let elem in buffer.evaluateXPath("//*[@liberator:highlight and @number]", win.document))
            {
                let num = elem.getAttribute("number");
                let hintchar = num2chars(parseInt(num, 10));
                elem.setAttribute("hintchar", showCase(hintchar));
                if(isValidHint(hintchar))
                    validHints.push(elem);
            }
            Array.forEach(win.frames, showHints);
        }

        validHints = [];
        showHints(window.content);
    } //}}}
    function isValidHint(hint) //{{{
    {
        return inputCase(hint).indexOf(hintInput) == 0;
    } //}}}

    var hintInput = "";
    var validHints = [];
    var charhints = plugins.charhints = {
        show: function (minor, filter, win) //{{{
        {
            charhints.original.show(minor, filter, win);
            hintInput = "";
            showCharHints();
        }, //}}}
        onInput: function (event) //{{{
        {
            let eventkey = events.toString(event);
            if(/^\d$/.test(eventkey)) {
                commandline.command += eventkey;
            }
            let hintString = commandline.command;
            commandline.command = hintString.replace(inputRegex, "");
            charhints.original.onInput(event);
            showCharHints();
            for(let i=0,l=hintString.length;i<l;++i) {
                if(inputRegex.test(hintString[i])) {
                    hintInput += hintString[i];
                }
            }
            if(hintInput.length>0) {
                let numstr = String(chars2num(hintInput));
                // no setTimeout, don't run nice
                setTimeout(function () {
                    for(let i=0,l=numstr.length;i<l;++i) {
                        let num = numstr[i];
                        let alt = new Object;
                        alt.liberatorString = num;
                        charhints.original.onEvent(alt);
                    }
                    statusline.updateInputBuffer(hintInput);
                    if(validHints.length == 1) {
                        charhints.original.processHints(true);
                        return true;
                    }
                }, 10);
            }
        }, //}}}
        onEvent: function (event) //{{{
        {
            if(/^\d$/.test(events.toString(event))) {
                charhints.onInput(event);
            } else {
                charhints.original.onEvent(event);
                statusline.updateInputBuffer(hintInput);
            }
        }, //}}}
    };

    if(!charhints.original) {
        charhints.original = {
            show: hints.show,
            onInput: liberator.eval("onInput", hintContext),
            onEvent: hints.onEvent,
            processHints: liberator.eval("processHints", hintContext),
        };

        charhints.install = function () //{{{
        {
            hints.show = charhints.show;
            hints.onEvent = charhints.onEvent;
            liberator.eval("onInput = plugins.charhints.onInput", hintContext);

            liberator.execute(":hi Hint::after content: attr(hintchar)");
        }; //}}}
        charhints.uninstall = function () //{{{
        {
            hints.show = charhints.original.show;
            hints.onEvent = charhints.original.onEvent;
            liberator.eval("onInput = plugins.charhints.original.onInput", hintContext);

            liberator.execute(":hi Hint::after content: attr(number)");
        }; //}}}
    }
    charhints.install();
})();

// vim: set fdm=marker sw=4 ts=4 et fenc=utf-8: