michael@0: (function(mod) { michael@0: if (typeof exports == "object" && typeof module == "object") // CommonJS michael@0: mod(require("../../lib/codemirror")); michael@0: else if (typeof define == "function" && define.amd) // AMD michael@0: define(["../../lib/codemirror"], mod); michael@0: else // Plain browser env michael@0: mod(CodeMirror); michael@0: })(function(CodeMirror) { michael@0: "use strict"; michael@0: var Pos = CodeMirror.Pos; michael@0: michael@0: function SearchCursor(doc, query, pos, caseFold) { michael@0: this.atOccurrence = false; this.doc = doc; michael@0: if (caseFold == null && typeof query == "string") caseFold = false; michael@0: michael@0: pos = pos ? doc.clipPos(pos) : Pos(0, 0); michael@0: this.pos = {from: pos, to: pos}; michael@0: michael@0: // The matches method is filled in based on the type of query. michael@0: // It takes a position and a direction, and returns an object michael@0: // describing the next occurrence of the query, or null if no michael@0: // more matches were found. michael@0: if (typeof query != "string") { // Regexp match michael@0: if (!query.global) query = new RegExp(query.source, query.ignoreCase ? "ig" : "g"); michael@0: this.matches = function(reverse, pos) { michael@0: if (reverse) { michael@0: query.lastIndex = 0; michael@0: var line = doc.getLine(pos.line).slice(0, pos.ch), cutOff = 0, match, start; michael@0: for (;;) { michael@0: query.lastIndex = cutOff; michael@0: var newMatch = query.exec(line); michael@0: if (!newMatch) break; michael@0: match = newMatch; michael@0: start = match.index; michael@0: cutOff = match.index + (match[0].length || 1); michael@0: if (cutOff == line.length) break; michael@0: } michael@0: var matchLen = (match && match[0].length) || 0; michael@0: if (!matchLen) { michael@0: if (start == 0 && line.length == 0) {match = undefined;} michael@0: else if (start != doc.getLine(pos.line).length) { michael@0: matchLen++; michael@0: } michael@0: } michael@0: } else { michael@0: query.lastIndex = pos.ch; michael@0: var line = doc.getLine(pos.line), match = query.exec(line); michael@0: var matchLen = (match && match[0].length) || 0; michael@0: var start = match && match.index; michael@0: if (start + matchLen != line.length && !matchLen) matchLen = 1; michael@0: } michael@0: if (match && matchLen) michael@0: return {from: Pos(pos.line, start), michael@0: to: Pos(pos.line, start + matchLen), michael@0: match: match}; michael@0: }; michael@0: } else { // String query michael@0: var origQuery = query; michael@0: if (caseFold) query = query.toLowerCase(); michael@0: var fold = caseFold ? function(str){return str.toLowerCase();} : function(str){return str;}; michael@0: var target = query.split("\n"); michael@0: // Different methods for single-line and multi-line queries michael@0: if (target.length == 1) { michael@0: if (!query.length) { michael@0: // Empty string would match anything and never progress, so michael@0: // we define it to match nothing instead. michael@0: this.matches = function() {}; michael@0: } else { michael@0: this.matches = function(reverse, pos) { michael@0: if (reverse) { michael@0: var orig = doc.getLine(pos.line).slice(0, pos.ch), line = fold(orig); michael@0: var match = line.lastIndexOf(query); michael@0: if (match > -1) { michael@0: match = adjustPos(orig, line, match); michael@0: return {from: Pos(pos.line, match), to: Pos(pos.line, match + origQuery.length)}; michael@0: } michael@0: } else { michael@0: var orig = doc.getLine(pos.line).slice(pos.ch), line = fold(orig); michael@0: var match = line.indexOf(query); michael@0: if (match > -1) { michael@0: match = adjustPos(orig, line, match) + pos.ch; michael@0: return {from: Pos(pos.line, match), to: Pos(pos.line, match + origQuery.length)}; michael@0: } michael@0: } michael@0: }; michael@0: } michael@0: } else { michael@0: var origTarget = origQuery.split("\n"); michael@0: this.matches = function(reverse, pos) { michael@0: var last = target.length - 1; michael@0: if (reverse) { michael@0: if (pos.line - (target.length - 1) < doc.firstLine()) return; michael@0: if (fold(doc.getLine(pos.line).slice(0, origTarget[last].length)) != target[target.length - 1]) return; michael@0: var to = Pos(pos.line, origTarget[last].length); michael@0: for (var ln = pos.line - 1, i = last - 1; i >= 1; --i, --ln) michael@0: if (target[i] != fold(doc.getLine(ln))) return; michael@0: var line = doc.getLine(ln), cut = line.length - origTarget[0].length; michael@0: if (fold(line.slice(cut)) != target[0]) return; michael@0: return {from: Pos(ln, cut), to: to}; michael@0: } else { michael@0: if (pos.line + (target.length - 1) > doc.lastLine()) return; michael@0: var line = doc.getLine(pos.line), cut = line.length - origTarget[0].length; michael@0: if (fold(line.slice(cut)) != target[0]) return; michael@0: var from = Pos(pos.line, cut); michael@0: for (var ln = pos.line + 1, i = 1; i < last; ++i, ++ln) michael@0: if (target[i] != fold(doc.getLine(ln))) return; michael@0: if (doc.getLine(ln).slice(0, origTarget[last].length) != target[last]) return; michael@0: return {from: from, to: Pos(ln, origTarget[last].length)}; michael@0: } michael@0: }; michael@0: } michael@0: } michael@0: } michael@0: michael@0: SearchCursor.prototype = { michael@0: findNext: function() {return this.find(false);}, michael@0: findPrevious: function() {return this.find(true);}, michael@0: michael@0: find: function(reverse) { michael@0: var self = this, pos = this.doc.clipPos(reverse ? this.pos.from : this.pos.to); michael@0: function savePosAndFail(line) { michael@0: var pos = Pos(line, 0); michael@0: self.pos = {from: pos, to: pos}; michael@0: self.atOccurrence = false; michael@0: return false; michael@0: } michael@0: michael@0: for (;;) { michael@0: if (this.pos = this.matches(reverse, pos)) { michael@0: this.atOccurrence = true; michael@0: return this.pos.match || true; michael@0: } michael@0: if (reverse) { michael@0: if (!pos.line) return savePosAndFail(0); michael@0: pos = Pos(pos.line-1, this.doc.getLine(pos.line-1).length); michael@0: } michael@0: else { michael@0: var maxLine = this.doc.lineCount(); michael@0: if (pos.line == maxLine - 1) return savePosAndFail(maxLine); michael@0: pos = Pos(pos.line + 1, 0); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: from: function() {if (this.atOccurrence) return this.pos.from;}, michael@0: to: function() {if (this.atOccurrence) return this.pos.to;}, michael@0: michael@0: replace: function(newText) { michael@0: if (!this.atOccurrence) return; michael@0: var lines = CodeMirror.splitLines(newText); michael@0: this.doc.replaceRange(lines, this.pos.from, this.pos.to); michael@0: this.pos.to = Pos(this.pos.from.line + lines.length - 1, michael@0: lines[lines.length - 1].length + (lines.length == 1 ? this.pos.from.ch : 0)); michael@0: } michael@0: }; michael@0: michael@0: // Maps a position in a case-folded line back to a position in the original line michael@0: // (compensating for codepoints increasing in number during folding) michael@0: function adjustPos(orig, folded, pos) { michael@0: if (orig.length == folded.length) return pos; michael@0: for (var pos1 = Math.min(pos, orig.length);;) { michael@0: var len1 = orig.slice(0, pos1).toLowerCase().length; michael@0: if (len1 < pos) ++pos1; michael@0: else if (len1 > pos) --pos1; michael@0: else return pos1; michael@0: } michael@0: } michael@0: michael@0: CodeMirror.defineExtension("getSearchCursor", function(query, pos, caseFold) { michael@0: return new SearchCursor(this.doc, query, pos, caseFold); michael@0: }); michael@0: CodeMirror.defineDocExtension("getSearchCursor", function(query, pos, caseFold) { michael@0: return new SearchCursor(this, query, pos, caseFold); michael@0: }); michael@0: michael@0: CodeMirror.defineExtension("selectMatches", function(query, caseFold) { michael@0: var ranges = [], next; michael@0: var cur = this.getSearchCursor(query, this.getCursor("from"), caseFold); michael@0: while (next = cur.findNext()) { michael@0: if (CodeMirror.cmpPos(cur.to(), this.getCursor("to")) > 0) break; michael@0: ranges.push({anchor: cur.from(), head: cur.to()}); michael@0: } michael@0: if (ranges.length) michael@0: this.setSelections(ranges, 0); michael@0: }); michael@0: });