michael@0: // Define search commands. Depends on dialog.js or another michael@0: // implementation of the openDialog method. michael@0: michael@0: // Replace works a little oddly -- it will do the replace on the next michael@0: // Ctrl-G (or whatever is bound to findNext) press. You prevent a michael@0: // replace by making sure the match is no longer selected when hitting michael@0: // Ctrl-G. michael@0: michael@0: (function(mod) { michael@0: if (typeof exports == "object" && typeof module == "object") // CommonJS michael@0: mod(require("../../lib/codemirror"), require("./searchcursor"), require("../dialog/dialog")); michael@0: else if (typeof define == "function" && define.amd) // AMD michael@0: define(["../../lib/codemirror", "./searchcursor", "../dialog/dialog"], mod); michael@0: else // Plain browser env michael@0: mod(CodeMirror); michael@0: })(function(CodeMirror) { michael@0: "use strict"; michael@0: function searchOverlay(query, caseInsensitive) { michael@0: var startChar; michael@0: if (typeof query == "string") { michael@0: startChar = query.charAt(0); michael@0: query = new RegExp("^" + query.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"), michael@0: caseInsensitive ? "i" : ""); michael@0: } else { michael@0: query = new RegExp("^(?:" + query.source + ")", query.ignoreCase ? "i" : ""); michael@0: } michael@0: return {token: function(stream) { michael@0: if (stream.match(query)) return "searching"; michael@0: while (!stream.eol()) { michael@0: stream.next(); michael@0: if (startChar && !caseInsensitive) michael@0: stream.skipTo(startChar) || stream.skipToEnd(); michael@0: if (stream.match(query, false)) break; michael@0: } michael@0: }}; michael@0: } michael@0: michael@0: function SearchState() { michael@0: this.posFrom = this.posTo = this.query = null; michael@0: this.overlay = null; michael@0: } michael@0: function getSearchState(cm) { michael@0: return cm.state.search || (cm.state.search = new SearchState()); michael@0: } michael@0: function queryCaseInsensitive(query) { michael@0: return typeof query == "string" && query == query.toLowerCase(); michael@0: } michael@0: function getSearchCursor(cm, query, pos) { michael@0: // Heuristic: if the query string is all lowercase, do a case insensitive search. michael@0: return cm.getSearchCursor(query, pos, queryCaseInsensitive(query)); michael@0: } michael@0: function dialog(cm, text, shortText, deflt, f) { michael@0: if (cm.openDialog) cm.openDialog(text, f, {value: deflt}); michael@0: else f(prompt(shortText, deflt)); michael@0: } michael@0: function confirmDialog(cm, text, shortText, fs) { michael@0: if (cm.openConfirm) cm.openConfirm(text, fs); michael@0: else if (confirm(shortText)) fs[0](); michael@0: } michael@0: function parseQuery(query) { michael@0: var isRE = query.match(/^\/(.*)\/([a-z]*)$/); michael@0: if (isRE) { michael@0: query = new RegExp(isRE[1], isRE[2].indexOf("i") == -1 ? "" : "i"); michael@0: if (query.test("")) query = /x^/; michael@0: } else if (query == "") { michael@0: query = /x^/; michael@0: } michael@0: return query; michael@0: } michael@0: var queryDialog; michael@0: function doSearch(cm, rev) { michael@0: if (!queryDialog) { michael@0: let doc = cm.getWrapperElement().ownerDocument; michael@0: let inp = doc.createElement("input"); michael@0: let txt = doc.createTextNode(cm.l10n("findCmd.promptMessage")); michael@0: michael@0: inp.type = "text"; michael@0: inp.style.width = "10em"; michael@0: inp.style.MozMarginStart = "1em"; michael@0: michael@0: queryDialog = doc.createElement("div"); michael@0: queryDialog.appendChild(txt); michael@0: queryDialog.appendChild(inp); michael@0: } michael@0: var state = getSearchState(cm); michael@0: if (state.query) return findNext(cm, rev); michael@0: dialog(cm, queryDialog, "Search for:", cm.getSelection(), function(query) { michael@0: cm.operation(function() { michael@0: if (!query || state.query) return; michael@0: state.query = parseQuery(query); michael@0: cm.removeOverlay(state.overlay, queryCaseInsensitive(state.query)); michael@0: state.overlay = searchOverlay(state.query, queryCaseInsensitive(state.query)); michael@0: cm.addOverlay(state.overlay); michael@0: state.posFrom = state.posTo = cm.getCursor(); michael@0: findNext(cm, rev); michael@0: }); michael@0: }); michael@0: } michael@0: function findNext(cm, rev) {cm.operation(function() { michael@0: var state = getSearchState(cm); michael@0: var cursor = getSearchCursor(cm, state.query, rev ? state.posFrom : state.posTo); michael@0: if (!cursor.find(rev)) { michael@0: cursor = getSearchCursor(cm, state.query, rev ? CodeMirror.Pos(cm.lastLine()) : CodeMirror.Pos(cm.firstLine(), 0)); michael@0: if (!cursor.find(rev)) return; michael@0: } michael@0: cm.setSelection(cursor.from(), cursor.to()); michael@0: cm.scrollIntoView({from: cursor.from(), to: cursor.to()}); michael@0: state.posFrom = cursor.from(); state.posTo = cursor.to(); michael@0: });} michael@0: function clearSearch(cm) {cm.operation(function() { michael@0: var state = getSearchState(cm); michael@0: if (!state.query) return; michael@0: state.query = null; michael@0: cm.removeOverlay(state.overlay); michael@0: });} michael@0: michael@0: var replaceQueryDialog = michael@0: 'Replace: (Use /re/ syntax for regexp search)'; michael@0: var replacementQueryDialog = 'With: '; michael@0: var doReplaceConfirm = "Replace? "; michael@0: function replace(cm, all) { michael@0: dialog(cm, replaceQueryDialog, "Replace:", cm.getSelection(), function(query) { michael@0: if (!query) return; michael@0: query = parseQuery(query); michael@0: dialog(cm, replacementQueryDialog, "Replace with:", "", function(text) { michael@0: if (all) { michael@0: cm.operation(function() { michael@0: for (var cursor = getSearchCursor(cm, query); cursor.findNext();) { michael@0: if (typeof query != "string") { michael@0: var match = cm.getRange(cursor.from(), cursor.to()).match(query); michael@0: cursor.replace(text.replace(/\$(\d)/g, function(_, i) {return match[i];})); michael@0: } else cursor.replace(text); michael@0: } michael@0: }); michael@0: } else { michael@0: clearSearch(cm); michael@0: var cursor = getSearchCursor(cm, query, cm.getCursor()); michael@0: var advance = function() { michael@0: var start = cursor.from(), match; michael@0: if (!(match = cursor.findNext())) { michael@0: cursor = getSearchCursor(cm, query); michael@0: if (!(match = cursor.findNext()) || michael@0: (start && cursor.from().line == start.line && cursor.from().ch == start.ch)) return; michael@0: } michael@0: cm.setSelection(cursor.from(), cursor.to()); michael@0: cm.scrollIntoView({from: cursor.from(), to: cursor.to()}); michael@0: confirmDialog(cm, doReplaceConfirm, "Replace?", michael@0: [function() {doReplace(match);}, advance]); michael@0: }; michael@0: var doReplace = function(match) { michael@0: cursor.replace(typeof query == "string" ? text : michael@0: text.replace(/\$(\d)/g, function(_, i) {return match[i];})); michael@0: advance(); michael@0: }; michael@0: advance(); michael@0: } michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: CodeMirror.commands.find = function(cm) {clearSearch(cm); doSearch(cm);}; michael@0: CodeMirror.commands.findNext = doSearch; michael@0: CodeMirror.commands.findPrev = function(cm) {doSearch(cm, true);}; michael@0: CodeMirror.commands.clearSearch = clearSearch; michael@0: CodeMirror.commands.replace = replace; michael@0: CodeMirror.commands.replaceAll = function(cm) {replace(cm, true);}; michael@0: });