forked from vimpr/vimperator-plugins
-
Notifications
You must be signed in to change notification settings - Fork 1
/
char-hints-mod2.js
311 lines (287 loc) · 10.2 KB
/
char-hints-mod2.js
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// PLUGIN_INFO//{{{
var PLUGIN_INFO =
<VimperatorPlugin>
<name>{NAME}</name>
<description>character hint mode.</description>
<author mail="[email protected]" homepage="http://d.hatena.ne.jp/hogelog/">hogelog</author>
<version>0.3.2</version>
<minVersion>2.3pre 2010/01/26</minVersion>
<maxVersion>2.4pre</maxVersion>
<updateURL>https://github.com/vimpr/vimperator-plugins/raw/master/char-hints-mod2.js</updateURL>
<detail><![CDATA[
== Usage ==
In default setting,
input quickhint in lowercase
and
select charhint label in uppercase.
== SETTING ==
let g:hintchars:
set character used by char-hint.
e.g.)
let g:hintchars="hjkl"
let g:hintsio:
- "i" setting char-hint input lowercase.
- "I" setting char-hint input uppercase.
- "o" setting char-hint show lowercase.
- "O" setting char-hint show uppercase.
Default setting is "IO".
e.g.)
let g:hintsio="i"
let g:hintlabeling:
- "s" setting simple n-base decimal hint labeling (n = hintchars.length)
- "a" setting adjust no overlap labeling
Default setting is "s".
e.g.)
let g:hintlabeling="a"
== TODO ==
]]></detail>
<detail lang="ja"><![CDATA[
== Usage ==
デフォルトの設定では
小文字は候補を絞るためのテキスト入力に、
大文字は文字ラベルの選択に使います。
== SETTING ==
let g:hintchars:
set character used by char-hint.
e.g.)
let g:hintchars="hjkl"
let g:hintsio:
- "i" setting char-hint input lowercase.
- "I" setting char-hint input uppercase.
- "o" setting char-hint show lowercase.
- "O" setting char-hint show uppercase.
Default setting is "IO".
e.g.)
let g:hintsio="i"
let g:hintlabeling:
- "s" setting simple n-base decimal hint labeling (n = hintchars.length)
- "a" setting adjust no overlap labeling
Default setting is "s".
e.g.)
let g:hintlabeling="a"
== TODO ==
]]></detail>
</VimperatorPlugin>;
//}}}
(function () {
(function(){
//override _showHints
const key = "Hints.prototype._showHints";
let conf = userContext[key],original;
if(conf) original = conf;
else original = userContext[key] = Hints.prototype._showHints;
const target = "String(hintnum).indexOf(String(activeHint)) == 0";
let source = original.toSource();
if(source.indexOf(target)>=0){
source = source.replace(target,
"num2chars(hintnum).indexOf(num2chars(activeHint)) == 0");
Hints.prototype._showHints = eval("(function() "+source+")()");
}else{
liberator.echoerr(new Error("_showHints override failed!"));
}
})()
;
(function(){
const source = Hints.prototype._checkUnique.toSource();
if(source.indexOf("10")<0) return;
Hints.prototype._checkUnique = eval("(function() "+source.replace("10",<>hintchars.length</>)+")()");
})()
;
const DEFAULT_HINTCHARS = "HJKLASDFGYUIOPQWERTNMZXCVB";
const hintContext = modules.hints;
let hintchars = DEFAULT_HINTCHARS;
let inputCase = function(str) str.toUpperCase();
let inputRegex = /[A-Z]/;
let showCase = function(str) str.toUpperCase();
let getStartCount = function() 0;
let timer = null;
function chars2num(chars) //{{{
{
let num = 0;
hintchars = inputCase(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 = "";
hintchars = inputCase(hintchars);
let base = hintchars.length;
do {
chars = hintchars[((num % base))] + chars;
num = Math.floor(num/base);
} while(num>0);
return chars;
} //}}}
function getAdjustStartCount(base, count) //{{{
{
if(count < base) {
return 0;
} else if(count >= Math.pow(base, 2)) {
return base;
}
var start = Math.floor(count / base);
var adjust = count + start;
var next_start;
while(start != (next_start = Math.floor(adjust / base))) {
adjust += start;
start = next_start;
}
return start;
} //}}}
function getCharHints(win) //{{{
{
let hints = [];
(function (win) {
let elems = [elem for(elem in util.evaluateXPath('//*[@liberator:highlight="Hint" and @number]', win.document))];
hints = hints.concat(elems);
Array.forEach(win.frames, arguments.callee);
})(win);
return hints;
} //}}}
function showCharHints(hints) //{{{
{
let start = getStartCount(hintchars.length, hints.length);
for(let i=0,len=hints.length;i<len;++i) {
let hint = hints[i];
let num = hint.getAttribute("number");
let hintchar = num2chars(parseInt(num, 10)+start);
hint.setAttribute("hintchar", showCase(hintchar));
}
} //}}}
function isValidHint(hintInput, hint) //{{{
{
if(hintInput.length == 0) return false;
let hintchar = hint.getAttribute("hintchar");
return inputCase(hintchar).indexOf(hintInput) == 0;
} //}}}
function setIOType(type) //{{{
{
switch (type)
{
case "I":
inputCase = function(str) str.toUpperCase();
inputRegex = /[A-Z]/;
break;
case "i":
inputCase = function(str) str.toLowerCase();
inputRegex = /[a-z]/;
break;
case "O":
showCase = function(str) str.toUpperCase();
break;
case "o":
showCase = function(str) str.toLowerCase();
break;
}
} //}}}
function clearOriginalTimeout() //{{{
{
liberator.eval('if(_activeTimeout) clearTimeout(_activeTimeout);_activeTimeout = null;', hintContext);
} //}}}
function processHintInput(hintInput, hints) //{{{
{
let start = getStartCount(hintchars.length, hints.length);
let num = chars2num(hintInput)-start;
if(num < 0) return;
hintContext._hintNumber = Math.floor(num/10);
charhints.original.onEvent.apply(hintContext,[{
liberatorString: String(num%10)
}]);
statusline.updateInputBuffer(hintInput);
} //}}}
var hintInput = "";
var charhints = plugins.charhints = {
show: function (minor, filter, win) //{{{
{
charhints.original.show.apply(hintContext, arguments);
hintInput = "";
let hints = getCharHints(window.content);
showCharHints(hints);
}, //}}}
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.apply(hintContext, arguments);
for(let i=0,l=hintString.length;i<l;++i) {
if(inputRegex.test(hintString[i])) {
hintInput += hintString[i];
}
}
let hints = getCharHints(window.content);
showCharHints(hints);
if(hintInput.length>0) processHintInput(hintInput, hints);
}, //}}}
onEvent: function (event) //{{{
{
if(/^\d$/.test(events.toString(event))) {
charhints.onInput(event);
} else {
charhints.original.onEvent.apply(hintContext,arguments);
clearOriginalTimeout();
statusline.updateInputBuffer(hintInput);
}
}, //}}}
processHints: function (followFirst) //{{{
{
// don't followFirst if processHints call from
// charhints.original.onEvent(alt) in processHintInput
let caller = arguments.callee.caller;
if(caller == charhints.original.onEvent && caller.caller == processHintInput)
return charhints.original.processHints.apply(hintContext,[false]);
return charhints.original.processHints.apply(hintContext,arguments);
}, //}}}
};
if(!charhints.original) {
charhints.original = {
show: hints.show,
onInput: hints._onInput,
onEvent: hints.onEvent,
processHints: hints._processHints,
};
charhints.install = function () //{{{
{
hints.show = charhints.show;
hints.onEvent = charhints.onEvent;
liberator.eval("_onInput = liberator.plugins.charhints.onInput", hintContext);
liberator.eval("_processHints = liberator.plugins.charhints.processHints", hintContext);
liberator.execute(":hi Hint::after content: attr(hintchar)", true, true);
if(liberator.globalVariables.hintsio) {
let hintsio = liberator.globalVariables.hintsio;
Array.forEach(hintsio, setIOType);
}
if(liberator.globalVariables.hintchars) {
hintchars = liberator.globalVariables.hintchars;
}
if(liberator.globalVariables.hintlabeling) {
switch(liberator.globalVariables.hintlabeling) {
default:
case "s":
getStartCount = function() 0;
break;
case "a":
getStartCount = getAdjustStartCount;
break;
}
}
}; //}}}
charhints.uninstall = function () //{{{
{
hints.show = charhints.original.show;
hints.onEvent = charhints.original.onEvent;
liberator.eval("_onInput = liberator.plugins.charhints.original.onInput", hintContext);
liberator.eval("_processHints = liberator.plugins.charhints.original.processHints", hintContext);
liberator.execute(":hi Hint::after content: attr(number)", true, true);
}; //}}}
}
charhints.install();
})();
// vim: set fdm=marker sw=4 ts=4 et fenc=utf-8: