2015-05-04 03:53:29 -04:00
|
|
|
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
|
|
|
// Distributed under an MIT license: http://codemirror.net/LICENSE
|
|
|
|
|
|
|
|
// Define search commands. Depends on dialog.js or another
|
|
|
|
// implementation of the openDialog method.
|
|
|
|
|
|
|
|
// Replace works a little oddly -- it will do the replace on the next
|
|
|
|
// Ctrl-G (or whatever is bound to findNext) press. You prevent a
|
|
|
|
// replace by making sure the match is no longer selected when hitting
|
|
|
|
// Ctrl-G.
|
|
|
|
|
|
|
|
(function(mod) {
|
|
|
|
if (typeof exports == "object" && typeof module == "object") // CommonJS
|
|
|
|
mod(require("../../lib/codemirror"), require("./searchcursor"), require("../dialog/dialog"));
|
|
|
|
else if (typeof define == "function" && define.amd) // AMD
|
|
|
|
define(["../../lib/codemirror", "./searchcursor", "../dialog/dialog"], mod);
|
|
|
|
else // Plain browser env
|
|
|
|
mod(CodeMirror);
|
|
|
|
})(function(CodeMirror) {
|
|
|
|
"use strict";
|
2016-01-17 15:28:04 -05:00
|
|
|
|
2015-05-04 03:53:29 -04:00
|
|
|
function searchOverlay(query, caseInsensitive) {
|
|
|
|
if (typeof query == "string")
|
|
|
|
query = new RegExp(query.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"), caseInsensitive ? "gi" : "g");
|
|
|
|
else if (!query.global)
|
|
|
|
query = new RegExp(query.source, query.ignoreCase ? "gi" : "g");
|
|
|
|
|
|
|
|
return {token: function(stream) {
|
|
|
|
query.lastIndex = stream.pos;
|
|
|
|
var match = query.exec(stream.string);
|
|
|
|
if (match && match.index == stream.pos) {
|
2016-01-17 15:28:04 -05:00
|
|
|
stream.pos += match[0].length || 1;
|
2015-05-04 03:53:29 -04:00
|
|
|
return "searching";
|
|
|
|
} else if (match) {
|
|
|
|
stream.pos = match.index;
|
|
|
|
} else {
|
|
|
|
stream.skipToEnd();
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
function SearchState() {
|
|
|
|
this.posFrom = this.posTo = this.lastQuery = this.query = null;
|
|
|
|
this.overlay = null;
|
|
|
|
}
|
2016-01-17 15:28:04 -05:00
|
|
|
|
2015-05-04 03:53:29 -04:00
|
|
|
function getSearchState(cm) {
|
|
|
|
return cm.state.search || (cm.state.search = new SearchState());
|
|
|
|
}
|
2016-01-17 15:28:04 -05:00
|
|
|
|
2015-05-04 03:53:29 -04:00
|
|
|
function queryCaseInsensitive(query) {
|
|
|
|
return typeof query == "string" && query == query.toLowerCase();
|
|
|
|
}
|
2016-01-17 15:28:04 -05:00
|
|
|
|
2015-05-04 03:53:29 -04:00
|
|
|
function getSearchCursor(cm, query, pos) {
|
|
|
|
// Heuristic: if the query string is all lowercase, do a case insensitive search.
|
|
|
|
return cm.getSearchCursor(query, pos, queryCaseInsensitive(query));
|
|
|
|
}
|
2016-01-17 15:28:04 -05:00
|
|
|
|
|
|
|
function persistentDialog(cm, text, deflt, f) {
|
|
|
|
cm.openDialog(text, f, {
|
|
|
|
value: deflt,
|
|
|
|
selectValueOnOpen: true,
|
|
|
|
closeOnEnter: false,
|
|
|
|
onClose: function() { clearSearch(cm); }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-05-04 03:53:29 -04:00
|
|
|
function dialog(cm, text, shortText, deflt, f) {
|
|
|
|
if (cm.openDialog) cm.openDialog(text, f, {value: deflt, selectValueOnOpen: true});
|
|
|
|
else f(prompt(shortText, deflt));
|
|
|
|
}
|
2016-01-17 15:28:04 -05:00
|
|
|
|
2015-05-04 03:53:29 -04:00
|
|
|
function confirmDialog(cm, text, shortText, fs) {
|
|
|
|
if (cm.openConfirm) cm.openConfirm(text, fs);
|
|
|
|
else if (confirm(shortText)) fs[0]();
|
|
|
|
}
|
2016-01-17 15:28:04 -05:00
|
|
|
|
|
|
|
function parseString(string) {
|
|
|
|
return string.replace(/\\(.)/g, function(_, ch) {
|
|
|
|
if (ch == "n") return "\n"
|
|
|
|
if (ch == "r") return "\r"
|
|
|
|
return ch
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-04 03:53:29 -04:00
|
|
|
function parseQuery(query) {
|
|
|
|
var isRE = query.match(/^\/(.*)\/([a-z]*)$/);
|
|
|
|
if (isRE) {
|
|
|
|
try { query = new RegExp(isRE[1], isRE[2].indexOf("i") == -1 ? "" : "i"); }
|
|
|
|
catch(e) {} // Not a regular expression after all, do a string search
|
2016-01-17 15:28:04 -05:00
|
|
|
} else {
|
|
|
|
query = parseString(query)
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
|
|
|
if (typeof query == "string" ? query == "" : query.test(""))
|
|
|
|
query = /x^/;
|
|
|
|
return query;
|
|
|
|
}
|
2016-01-17 15:28:04 -05:00
|
|
|
|
2015-05-04 03:53:29 -04:00
|
|
|
var queryDialog =
|
|
|
|
'Search: <input type="text" style="width: 10em" class="CodeMirror-search-field"/> <span style="color: #888" class="CodeMirror-search-hint">(Use /re/ syntax for regexp search)</span>';
|
2016-01-17 15:28:04 -05:00
|
|
|
|
|
|
|
function startSearch(cm, state, query) {
|
|
|
|
state.queryText = query;
|
|
|
|
state.query = parseQuery(query);
|
|
|
|
cm.removeOverlay(state.overlay, queryCaseInsensitive(state.query));
|
|
|
|
state.overlay = searchOverlay(state.query, queryCaseInsensitive(state.query));
|
|
|
|
cm.addOverlay(state.overlay);
|
|
|
|
if (cm.showMatchesOnScrollbar) {
|
|
|
|
if (state.annotate) { state.annotate.clear(); state.annotate = null; }
|
|
|
|
state.annotate = cm.showMatchesOnScrollbar(state.query, queryCaseInsensitive(state.query));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function doSearch(cm, rev, persistent) {
|
2015-05-04 03:53:29 -04:00
|
|
|
var state = getSearchState(cm);
|
|
|
|
if (state.query) return findNext(cm, rev);
|
|
|
|
var q = cm.getSelection() || state.lastQuery;
|
2016-01-17 15:28:04 -05:00
|
|
|
if (persistent && cm.openDialog) {
|
|
|
|
var hiding = null
|
|
|
|
persistentDialog(cm, queryDialog, q, function(query, event) {
|
|
|
|
CodeMirror.e_stop(event);
|
|
|
|
if (!query) return;
|
2016-04-20 06:11:40 -04:00
|
|
|
if (query != state.queryText) {
|
|
|
|
startSearch(cm, state, query);
|
|
|
|
state.posFrom = state.posTo = cm.getCursor();
|
|
|
|
}
|
2016-01-17 15:28:04 -05:00
|
|
|
if (hiding) hiding.style.opacity = 1
|
|
|
|
findNext(cm, event.shiftKey, function(_, to) {
|
|
|
|
var dialog
|
|
|
|
if (to.line < 3 && document.querySelector &&
|
|
|
|
(dialog = cm.display.wrapper.querySelector(".CodeMirror-dialog")) &&
|
|
|
|
dialog.getBoundingClientRect().bottom - 4 > cm.cursorCoords(to, "window").top)
|
|
|
|
(hiding = dialog).style.opacity = .4
|
|
|
|
})
|
2015-05-04 03:53:29 -04:00
|
|
|
});
|
2016-01-17 15:28:04 -05:00
|
|
|
} else {
|
|
|
|
dialog(cm, queryDialog, "Search for:", q, function(query) {
|
|
|
|
if (query && !state.query) cm.operation(function() {
|
|
|
|
startSearch(cm, state, query);
|
|
|
|
state.posFrom = state.posTo = cm.getCursor();
|
|
|
|
findNext(cm, rev);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
2016-01-17 15:28:04 -05:00
|
|
|
|
|
|
|
function findNext(cm, rev, callback) {cm.operation(function() {
|
2015-05-04 03:53:29 -04:00
|
|
|
var state = getSearchState(cm);
|
|
|
|
var cursor = getSearchCursor(cm, state.query, rev ? state.posFrom : state.posTo);
|
|
|
|
if (!cursor.find(rev)) {
|
|
|
|
cursor = getSearchCursor(cm, state.query, rev ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(cm.firstLine(), 0));
|
|
|
|
if (!cursor.find(rev)) return;
|
|
|
|
}
|
|
|
|
cm.setSelection(cursor.from(), cursor.to());
|
2016-01-17 15:28:04 -05:00
|
|
|
cm.scrollIntoView({from: cursor.from(), to: cursor.to()}, 20);
|
2015-05-04 03:53:29 -04:00
|
|
|
state.posFrom = cursor.from(); state.posTo = cursor.to();
|
2016-01-17 15:28:04 -05:00
|
|
|
if (callback) callback(cursor.from(), cursor.to())
|
2015-05-04 03:53:29 -04:00
|
|
|
});}
|
2016-01-17 15:28:04 -05:00
|
|
|
|
2015-05-04 03:53:29 -04:00
|
|
|
function clearSearch(cm) {cm.operation(function() {
|
|
|
|
var state = getSearchState(cm);
|
|
|
|
state.lastQuery = state.query;
|
|
|
|
if (!state.query) return;
|
2016-01-17 15:28:04 -05:00
|
|
|
state.query = state.queryText = null;
|
2015-05-04 03:53:29 -04:00
|
|
|
cm.removeOverlay(state.overlay);
|
|
|
|
if (state.annotate) { state.annotate.clear(); state.annotate = null; }
|
|
|
|
});}
|
|
|
|
|
|
|
|
var replaceQueryDialog =
|
2016-01-17 15:28:04 -05:00
|
|
|
' <input type="text" style="width: 10em" class="CodeMirror-search-field"/> <span style="color: #888" class="CodeMirror-search-hint">(Use /re/ syntax for regexp search)</span>';
|
2015-05-04 03:53:29 -04:00
|
|
|
var replacementQueryDialog = 'With: <input type="text" style="width: 10em" class="CodeMirror-search-field"/>';
|
2016-01-17 15:28:04 -05:00
|
|
|
var doReplaceConfirm = "Replace? <button>Yes</button> <button>No</button> <button>All</button> <button>Stop</button>";
|
|
|
|
|
|
|
|
function replaceAll(cm, query, text) {
|
|
|
|
cm.operation(function() {
|
|
|
|
for (var cursor = getSearchCursor(cm, query); cursor.findNext();) {
|
|
|
|
if (typeof query != "string") {
|
|
|
|
var match = cm.getRange(cursor.from(), cursor.to()).match(query);
|
|
|
|
cursor.replace(text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
|
|
|
|
} else cursor.replace(text);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-05-04 03:53:29 -04:00
|
|
|
function replace(cm, all) {
|
|
|
|
if (cm.getOption("readOnly")) return;
|
2015-05-15 00:58:13 -04:00
|
|
|
var query = cm.getSelection() || getSearchState(cm).lastQuery;
|
2016-01-17 15:28:04 -05:00
|
|
|
var dialogText = all ? "Replace all:" : "Replace:"
|
|
|
|
dialog(cm, dialogText + replaceQueryDialog, dialogText, query, function(query) {
|
2015-05-04 03:53:29 -04:00
|
|
|
if (!query) return;
|
|
|
|
query = parseQuery(query);
|
|
|
|
dialog(cm, replacementQueryDialog, "Replace with:", "", function(text) {
|
2016-01-17 15:28:04 -05:00
|
|
|
text = parseString(text)
|
2015-05-04 03:53:29 -04:00
|
|
|
if (all) {
|
2016-01-17 15:28:04 -05:00
|
|
|
replaceAll(cm, query, text)
|
2015-05-04 03:53:29 -04:00
|
|
|
} else {
|
|
|
|
clearSearch(cm);
|
2016-04-20 06:11:40 -04:00
|
|
|
var cursor = getSearchCursor(cm, query, cm.getCursor("from"));
|
2015-05-04 03:53:29 -04:00
|
|
|
var advance = function() {
|
|
|
|
var start = cursor.from(), match;
|
|
|
|
if (!(match = cursor.findNext())) {
|
|
|
|
cursor = getSearchCursor(cm, query);
|
|
|
|
if (!(match = cursor.findNext()) ||
|
|
|
|
(start && cursor.from().line == start.line && cursor.from().ch == start.ch)) return;
|
|
|
|
}
|
|
|
|
cm.setSelection(cursor.from(), cursor.to());
|
|
|
|
cm.scrollIntoView({from: cursor.from(), to: cursor.to()});
|
|
|
|
confirmDialog(cm, doReplaceConfirm, "Replace?",
|
2016-01-17 15:28:04 -05:00
|
|
|
[function() {doReplace(match);}, advance,
|
|
|
|
function() {replaceAll(cm, query, text)}]);
|
2015-05-04 03:53:29 -04:00
|
|
|
};
|
|
|
|
var doReplace = function(match) {
|
|
|
|
cursor.replace(typeof query == "string" ? text :
|
2016-01-17 15:28:04 -05:00
|
|
|
text.replace(/\$(\d)/g, function(_, i) {return match[i];}));
|
2015-05-04 03:53:29 -04:00
|
|
|
advance();
|
|
|
|
};
|
|
|
|
advance();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
CodeMirror.commands.find = function(cm) {clearSearch(cm); doSearch(cm);};
|
2016-01-17 15:28:04 -05:00
|
|
|
CodeMirror.commands.findPersistent = function(cm) {clearSearch(cm); doSearch(cm, false, true);};
|
2015-05-04 03:53:29 -04:00
|
|
|
CodeMirror.commands.findNext = doSearch;
|
|
|
|
CodeMirror.commands.findPrev = function(cm) {doSearch(cm, true);};
|
|
|
|
CodeMirror.commands.clearSearch = clearSearch;
|
|
|
|
CodeMirror.commands.replace = replace;
|
|
|
|
CodeMirror.commands.replaceAll = function(cm) {replace(cm, true);};
|
|
|
|
});
|