13
if (typeof exports == "object" && typeof module == "object")
14
mod(require("../../lib/codemirror"), require("./searchcursor"), require("../dialog/dialog"));
15
else if (typeof define == "function" && define.amd)
16
define(["../../lib/codemirror", "./searchcursor", "../dialog/dialog"], mod);
19
})(function(CodeMirror) {
23
CodeMirror.defineOption("search", {bottom: false});
25
function searchOverlay(query, caseInsensitive) {
26
if (typeof query == "string")
27
query = new RegExp(query.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"), caseInsensitive ? "gi" : "g");
28
else if (!query.global)
29
query = new RegExp(query.source, query.ignoreCase ? "gi" : "g");
31
return {token: function(stream) {
32
query.lastIndex = stream.pos;
33
var match = query.exec(stream.string);
34
if (match && match.index == stream.pos) {
35
stream.pos += match[0].length || 1;
38
stream.pos = match.index;
45
function SearchState() {
46
this.posFrom = this.posTo = this.lastQuery = this.query = null;
50
function getSearchState(cm) {
51
return cm.state.search || (cm.state.search = new SearchState());
54
function queryCaseInsensitive(query) {
55
return typeof query == "string" && query == query.toLowerCase();
58
function getSearchCursor(cm, query, pos) {
60
return cm.getSearchCursor(query, pos, {caseFold: queryCaseInsensitive(query), multiline: true});
63
function persistentDialog(cm, text, deflt, onEnter, onKeyDown) {
64
cm.openDialog(text, onEnter, {
66
selectValueOnOpen: true,
68
onClose: function() { clearSearch(cm); },
70
bottom: cm.options.search.bottom
74
function dialog(cm, text, shortText, deflt, f) {
75
if (cm.openDialog) cm.openDialog(text, f, {value: deflt, selectValueOnOpen: true, bottom: cm.options.search.bottom});
76
else f(prompt(shortText, deflt));
79
function confirmDialog(cm, text, shortText, fs) {
80
if (cm.openConfirm) cm.openConfirm(text, fs);
81
else if (confirm(shortText)) fs[0]();
84
function parseString(string) {
85
return string.replace(/\\([nrt\\])/g, function(match, ch) {
86
if (ch == "n") return "\n"
87
if (ch == "r") return "\r"
88
if (ch == "t") return "\t"
89
if (ch == "\\") return "\\"
94
function parseQuery(query) {
95
var isRE = query.match(/^\/(.*)\/([a-z]*)$/);
97
try { query = new RegExp(isRE[1], isRE[2].indexOf("i") == -1 ? "" : "i"); }
100
query = parseString(query)
102
if (typeof query == "string" ? query == "" : query.test(""))
107
function startSearch(cm, state, query) {
108
state.queryText = query;
109
state.query = parseQuery(query);
110
cm.removeOverlay(state.overlay, queryCaseInsensitive(state.query));
111
state.overlay = searchOverlay(state.query, queryCaseInsensitive(state.query));
112
cm.addOverlay(state.overlay);
113
if (cm.showMatchesOnScrollbar) {
114
if (state.annotate) { state.annotate.clear(); state.annotate = null; }
115
state.annotate = cm.showMatchesOnScrollbar(state.query, queryCaseInsensitive(state.query));
119
function doSearch(cm, rev, persistent, immediate) {
120
var state = getSearchState(cm);
121
if (state.query) return findNext(cm, rev);
122
var q = cm.getSelection() || state.lastQuery;
123
if (q instanceof RegExp && q.source == "x^") q = null
124
if (persistent && cm.openDialog) {
126
var searchNext = function(query, event) {
127
CodeMirror.e_stop(event);
129
if (query != state.queryText) {
130
startSearch(cm, state, query);
131
state.posFrom = state.posTo = cm.getCursor();
133
if (hiding) hiding.style.opacity = 1
134
findNext(cm, event.shiftKey, function(_, to) {
136
if (to.line < 3 && document.querySelector &&
137
(dialog = cm.display.wrapper.querySelector(".CodeMirror-dialog")) &&
138
dialog.getBoundingClientRect().bottom - 4 > cm.cursorCoords(to, "window").top)
139
(hiding = dialog).style.opacity = .4
142
persistentDialog(cm, getQueryDialog(cm), q, searchNext, function(event, query) {
143
var keyName = CodeMirror.keyName(event)
144
var extra = cm.getOption('extraKeys'), cmd = (extra && extra[keyName]) || CodeMirror.keyMap[cm.getOption("keyMap")][keyName]
145
if (cmd == "findNext" || cmd == "findPrev" ||
146
cmd == "findPersistentNext" || cmd == "findPersistentPrev") {
147
CodeMirror.e_stop(event);
148
startSearch(cm, getSearchState(cm), query);
150
} else if (cmd == "find" || cmd == "findPersistent") {
151
CodeMirror.e_stop(event);
152
searchNext(query, event);
155
if (immediate && q) {
156
startSearch(cm, state, q);
160
dialog(cm, getQueryDialog(cm), "Search for:", q, function(query) {
161
if (query && !state.query) cm.operation(function() {
162
startSearch(cm, state, query);
163
state.posFrom = state.posTo = cm.getCursor();
170
function findNext(cm, rev, callback) {cm.operation(function() {
171
var state = getSearchState(cm);
172
var cursor = getSearchCursor(cm, state.query, rev ? state.posFrom : state.posTo);
173
if (!cursor.find(rev)) {
174
cursor = getSearchCursor(cm, state.query, rev ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(cm.firstLine(), 0));
175
if (!cursor.find(rev)) return;
177
cm.setSelection(cursor.from(), cursor.to());
178
cm.scrollIntoView({from: cursor.from(), to: cursor.to()}, 20);
179
state.posFrom = cursor.from(); state.posTo = cursor.to();
180
if (callback) callback(cursor.from(), cursor.to())
183
function clearSearch(cm) {cm.operation(function() {
184
var state = getSearchState(cm);
185
state.lastQuery = state.query;
186
if (!state.query) return;
187
state.query = state.queryText = null;
188
cm.removeOverlay(state.overlay);
189
if (state.annotate) { state.annotate.clear(); state.annotate = null; }
192
function el(tag, attrs) {
193
var element = tag ? document.createElement(tag) : document.createDocumentFragment();
194
for (var key in attrs) {
195
element[key] = attrs[key];
197
for (var i = 2; i < arguments.length; i++) {
198
var child = arguments[i]
199
element.appendChild(typeof child == "string" ? document.createTextNode(child) : child);
204
function getQueryDialog(cm) {
206
el("span", {className: "CodeMirror-search-label"}, cm.phrase("Search:")), " ",
207
el("input", {type: "text", "style": "width: 10em", className: "CodeMirror-search-field"}), " ",
208
el("span", {style: "color: #888", className: "CodeMirror-search-hint"},
209
cm.phrase("(Use /re/ syntax for regexp search)")));
211
function getReplaceQueryDialog(cm) {
212
return el("", null, " ",
213
el("input", {type: "text", "style": "width: 10em", className: "CodeMirror-search-field"}), " ",
214
el("span", {style: "color: #888", className: "CodeMirror-search-hint"},
215
cm.phrase("(Use /re/ syntax for regexp search)")));
217
function getReplacementQueryDialog(cm) {
219
el("span", {className: "CodeMirror-search-label"}, cm.phrase("With:")), " ",
220
el("input", {type: "text", "style": "width: 10em", className: "CodeMirror-search-field"}));
222
function getDoReplaceConfirm(cm) {
224
el("span", {className: "CodeMirror-search-label"}, cm.phrase("Replace?")), " ",
225
el("button", {}, cm.phrase("Yes")), " ",
226
el("button", {}, cm.phrase("No")), " ",
227
el("button", {}, cm.phrase("All")), " ",
228
el("button", {}, cm.phrase("Stop")));
231
function replaceAll(cm, query, text) {
232
cm.operation(function() {
233
for (var cursor = getSearchCursor(cm, query); cursor.findNext();) {
234
if (typeof query != "string") {
235
var match = cm.getRange(cursor.from(), cursor.to()).match(query);
236
cursor.replace(text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
237
} else cursor.replace(text);
242
function replace(cm, all) {
243
if (cm.getOption("readOnly")) return;
244
var query = cm.getSelection() || getSearchState(cm).lastQuery;
245
var dialogText = all ? cm.phrase("Replace all:") : cm.phrase("Replace:")
246
var fragment = el("", null,
247
el("span", {className: "CodeMirror-search-label"}, dialogText),
248
getReplaceQueryDialog(cm))
249
dialog(cm, fragment, dialogText, query, function(query) {
251
query = parseQuery(query);
252
dialog(cm, getReplacementQueryDialog(cm), cm.phrase("Replace with:"), "", function(text) {
253
text = parseString(text)
255
replaceAll(cm, query, text)
258
var cursor = getSearchCursor(cm, query, cm.getCursor("from"));
259
var advance = function() {
260
var start = cursor.from(), match;
261
if (!(match = cursor.findNext())) {
262
cursor = getSearchCursor(cm, query);
263
if (!(match = cursor.findNext()) ||
264
(start && cursor.from().line == start.line && cursor.from().ch == start.ch)) return;
266
cm.setSelection(cursor.from(), cursor.to());
267
cm.scrollIntoView({from: cursor.from(), to: cursor.to()});
268
confirmDialog(cm, getDoReplaceConfirm(cm), cm.phrase("Replace?"),
269
[function() {doReplace(match);}, advance,
270
function() {replaceAll(cm, query, text)}]);
272
var doReplace = function(match) {
273
cursor.replace(typeof query == "string" ? text :
274
text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
283
CodeMirror.commands.find = function(cm) {clearSearch(cm); doSearch(cm);};
284
CodeMirror.commands.findPersistent = function(cm) {clearSearch(cm); doSearch(cm, false, true);};
285
CodeMirror.commands.findPersistentNext = function(cm) {doSearch(cm, false, true, true);};
286
CodeMirror.commands.findPersistentPrev = function(cm) {doSearch(cm, true, true, true);};
287
CodeMirror.commands.findNext = doSearch;
288
CodeMirror.commands.findPrev = function(cm) {doSearch(cm, true);};
289
CodeMirror.commands.clearSearch = clearSearch;
290
CodeMirror.commands.replace = replace;
291
CodeMirror.commands.replaceAll = function(cm) {replace(cm, true);};