Path: blob/master/web-gui/buildyourownbotnet/assets/js/codemirror/addon/search/search.js
1294 views
// Define search commands. Depends on dialog.js or another1// implementation of the openDialog method.23// Replace works a little oddly -- it will do the replace on the next4// Ctrl-G (or whatever is bound to findNext) press. You prevent a5// replace by making sure the match is no longer selected when hitting6// Ctrl-G.78(function() {9function searchOverlay(query, caseInsensitive) {10var startChar;11if (typeof query == "string") {12startChar = query.charAt(0);13query = new RegExp("^" + query.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"),14caseInsensitive ? "i" : "");15} else {16query = new RegExp("^(?:" + query.source + ")", query.ignoreCase ? "i" : "");17}18if (typeof query == "string") return {token: function(stream) {19if (stream.match(query)) return "searching";20stream.next();21stream.skipTo(query.charAt(0)) || stream.skipToEnd();22}};23return {token: function(stream) {24if (stream.match(query)) return "searching";25while (!stream.eol()) {26stream.next();27if (startChar)28stream.skipTo(startChar) || stream.skipToEnd();29if (stream.match(query, false)) break;30}31}};32}3334function SearchState() {35this.posFrom = this.posTo = this.query = null;36this.overlay = null;37}38function getSearchState(cm) {39return cm.state.search || (cm.state.search = new SearchState());40}41function queryCaseInsensitive(query) {42return typeof query == "string" && query == query.toLowerCase();43}44function getSearchCursor(cm, query, pos) {45// Heuristic: if the query string is all lowercase, do a case insensitive search.46return cm.getSearchCursor(query, pos, queryCaseInsensitive(query));47}48function dialog(cm, text, shortText, deflt, f) {49if (cm.openDialog) cm.openDialog(text, f, {value: deflt});50else f(prompt(shortText, deflt));51}52function confirmDialog(cm, text, shortText, fs) {53if (cm.openConfirm) cm.openConfirm(text, fs);54else if (confirm(shortText)) fs[0]();55}56function parseQuery(query) {57var isRE = query.match(/^\/(.*)\/([a-z]*)$/);58return isRE ? new RegExp(isRE[1], isRE[2].indexOf("i") == -1 ? "" : "i") : query;59}60var queryDialog =61'Search: <input type="text" style="width: 10em"/> <span style="color: #888">(Use /re/ syntax for regexp search)</span>';62function doSearch(cm, rev) {63var state = getSearchState(cm);64if (state.query) return findNext(cm, rev);65dialog(cm, queryDialog, "Search for:", cm.getSelection(), function(query) {66cm.operation(function() {67if (!query || state.query) return;68state.query = parseQuery(query);69cm.removeOverlay(state.overlay, queryCaseInsensitive(state.query));70state.overlay = searchOverlay(state.query);71cm.addOverlay(state.overlay);72state.posFrom = state.posTo = cm.getCursor();73findNext(cm, rev);74});75});76}77function findNext(cm, rev) {cm.operation(function() {78var state = getSearchState(cm);79var cursor = getSearchCursor(cm, state.query, rev ? state.posFrom : state.posTo);80if (!cursor.find(rev)) {81cursor = getSearchCursor(cm, state.query, rev ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(cm.firstLine(), 0));82if (!cursor.find(rev)) return;83}84cm.setSelection(cursor.from(), cursor.to());85cm.scrollIntoView({from: cursor.from(), to: cursor.to()});86state.posFrom = cursor.from(); state.posTo = cursor.to();87});}88function clearSearch(cm) {cm.operation(function() {89var state = getSearchState(cm);90if (!state.query) return;91state.query = null;92cm.removeOverlay(state.overlay);93});}9495var replaceQueryDialog =96'Replace: <input type="text" style="width: 10em"/> <span style="color: #888">(Use /re/ syntax for regexp search)</span>';97var replacementQueryDialog = 'With: <input type="text" style="width: 10em"/>';98var doReplaceConfirm = "Replace? <button>Yes</button> <button>No</button> <button>Stop</button>";99function replace(cm, all) {100dialog(cm, replaceQueryDialog, "Replace:", cm.getSelection(), function(query) {101if (!query) return;102query = parseQuery(query);103dialog(cm, replacementQueryDialog, "Replace with:", "", function(text) {104if (all) {105cm.operation(function() {106for (var cursor = getSearchCursor(cm, query); cursor.findNext();) {107if (typeof query != "string") {108var match = cm.getRange(cursor.from(), cursor.to()).match(query);109cursor.replace(text.replace(/\$(\d)/, function(_, i) {return match[i];}));110} else cursor.replace(text);111}112});113} else {114clearSearch(cm);115var cursor = getSearchCursor(cm, query, cm.getCursor());116var advance = function() {117var start = cursor.from(), match;118if (!(match = cursor.findNext())) {119cursor = getSearchCursor(cm, query);120if (!(match = cursor.findNext()) ||121(start && cursor.from().line == start.line && cursor.from().ch == start.ch)) return;122}123cm.setSelection(cursor.from(), cursor.to());124cm.scrollIntoView({from: cursor.from(), to: cursor.to()});125confirmDialog(cm, doReplaceConfirm, "Replace?",126[function() {doReplace(match);}, advance]);127};128var doReplace = function(match) {129cursor.replace(typeof query == "string" ? text :130text.replace(/\$(\d)/, function(_, i) {return match[i];}));131advance();132};133advance();134}135});136});137}138139CodeMirror.commands.find = function(cm) {clearSearch(cm); doSearch(cm);};140CodeMirror.commands.findNext = doSearch;141CodeMirror.commands.findPrev = function(cm) {doSearch(cm, true);};142CodeMirror.commands.clearSearch = clearSearch;143CodeMirror.commands.replace = replace;144CodeMirror.commands.replaceAll = function(cm) {replace(cm, true);};145})();146147148