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
|
// ==VimperatorPlugin==
// @name Migemized Find
// @description-ja デフォルトのドキュメント内検索をミゲマイズする。
// @license Creative Commons 2.1 (Attribution + Share Alike)
// @version 1.0
// ==/VimperatorPlugin==
//
// Usage:
// 検索ワードの一文字目が
// '/' => 正規表現検索
// '?' => Migemo検索
// 以外 => Migemo検索
//
// Author:
// anekos
//
// Link:
// http://d.hatena.ne.jp/nokturnalmortum/20080805#1217941126
(function () { try {
let XMigemoCore = Components.classes['@piro.sakura.ne.jp/xmigemo/factory;1']
.getService(Components.interfaces.pIXMigemoFactory)
.getService('ja');
function getPosition (elem) {
if (!elem)
return {x: 0, y: 0};
let parent = getPosition(elem.offsetParent);
return { x: (elem.offsetLeft || 0) + parent.x,
y: (elem.offsetTop || 0) + parent.y }
}
let delayCallTimer = null;
let MF = {
lastSearchText: null,
previousSearchText: null,
lastDirection: null,
get buffer function () liberator.buffer,
get document function () content.document,
get storage function () (this.buffer.__migemized_find_storage || (this.buffer.__migemized_find_storage = {})),
get defaultRange function () {
let range = this.document.createRange();
range.selectNodeContents(this.document.body);
return range;
},
get highlightRemover function () (this.storage.highlightRemover || function () void(0)),
set highlightRemover function (fun) (this.storage.highlightRemover = fun),
MODE_NORMAL: 0,
MODE_REGEXP: 1,
MODE_MIGEMO: 2,
// 検索文字列から検索モードと検索文字列を得る。
searchTextToRegExpString: function (str) {
let [head, tail] = [str[0], str.slice(1)];
switch (head) {
case '/':
return tail;
case '?':
return XMigemoCore.getRegExp(tail);
}
return XMigemoCore.getRegExp(str);
},
removeHighlight: function () {
this.highlightRemover()
this.highlightRemover = null;
},
highlightRange: function (range, setRemover) {
let span = this.document.createElement('span');
let spanStyle = 'background-color: lightblue; color: black; border: dotted 3px blue;';
span.setAttribute('style', spanStyle);
range.surroundContents(span);
let scroll = function () {
let pos = getPosition(span);
content.scroll(pos.x - (content.innerWidth / 2),
pos.y - (content.innerHeight / 2));
};
setTimeout(scroll, 0);
let remover = function () {
let range = this.document.createRange();
range.selectNodeContents(span);
let content = range.extractContents();
range.setStartBefore(span);
range.insertNode(content);
range.selectNode(span);
range.deleteContents();
};
if (setRemover)
this.highlightRemover = remover;
return remover;
},
find: function (str, backwards, range, start, end) {
if (!range)
range = this.defaultRange;
if (!start) {
start = range.startContainer.ownerDocument.createRange();
start.setStartBefore(range.startContainer);
}
if (!end) {
end = range.endContainer.ownerDocument.createRange();
end.setEndAfter(range.endContainer);
}
if (backwards)
[start, end] = [end, start];
try {
return XMigemoCore.regExpFind(str, 'i', range, start, end, backwards);
} catch (e) {
return false;
}
},
findFirst: function (str, backwards) {
let f = function () {
this.lastDirection = backwards;
this.lastSearchText = str = this.searchTextToRegExpString(str);
let result = this.storage.lastResult = this.find(str, backwards);
this.removeHighlight();
if (result)
this.highlightRange(result, true);
return result;
};
if (delayCallTimer)
clearTimeout(delayCallTimer);
delayCallTimer = setTimeout(function () f.call(MF), 300);
},
findAgain: function (reverse) {
this.removeHighlight();
let str = this.lastSearchText;
let range = this.defaultRange;
let last = this.storage.lastResult;
let backwards = !!(!this.lastDirection ^ !reverse);
let start, end;
if (last) {
if (backwards) {
end = last.cloneRange();
end.setEnd(last.startContainer, last.startOffset);
} else {
start = last.cloneRange();
start.setStart(last.endContainer, last.endOffset);
}
}
let result = this.storage.lastResult = this.find(str, backwards, range, start, end);
if (!result)
result = this.storage.lastResult = this.find(str, backwards, range);
if (result)
this.highlightRange(result, true);
else
liberator.echoerr('not found: ' + str);
return result;
},
};
let original = {};
let migemized = {
find: function find (str, backwards) {
MF.findFirst(str, backwards);
},
findAgain: function findAgain (reverse) {
MF.findAgain(reverse);
},
searchSubmitted: function searchSubmitted (command, forcedBackward) {
if (!MF.storage.lastResult)
liberator.echoerr('not found: ' + MF.lastSearchText);
MF.previousSearchText = MF.lastSearchText;
},
searchCanceled: function searchCanceled () {
MF.lastSearchText = MF.previousSearchText;
},
};
for (let name in migemized)
original[name] = liberator.search[name];
function set (funcs) {
for (let name in funcs)
liberator.search[name] = funcs[name];
}
set(migemized);
liberator.plugins.migemizedFind = {
install: function () set(migemized),
uninstall: function () set(original),
};
}catch(e){liberator.log(e);}})();
|