2
function SearchCursor(cm, query, pos, caseFold) {
3
this.atOccurrence = false; this.cm = cm;
4
if (caseFold == null) caseFold = typeof query == "string" && query == query.toLowerCase();
6
pos = pos ? cm.clipPos(pos) : {line: 0, ch: 0};
7
this.pos = {from: pos, to: pos};
9
// The matches method is filled in based on the type of query.
10
// It takes a position and a direction, and returns an object
11
// describing the next occurrence of the query, or null if no
12
// more matches were found.
13
if (typeof query != "string") // Regexp match
14
this.matches = function(reverse, pos) {
16
var line = cm.getLine(pos.line).slice(0, pos.ch), match = line.match(query), start = 0;
18
var ind = line.indexOf(match[0]);
20
line = line.slice(ind + 1);
21
var newmatch = line.match(query);
22
if (newmatch) match = newmatch;
28
var line = cm.getLine(pos.line).slice(pos.ch), match = line.match(query),
29
start = match && pos.ch + line.indexOf(match[0]);
32
return {from: {line: pos.line, ch: start},
33
to: {line: pos.line, ch: start + match[0].length},
36
else { // String query
37
if (caseFold) query = query.toLowerCase();
38
var fold = caseFold ? function(str){return str.toLowerCase();} : function(str){return str;};
39
var target = query.split("\n");
40
// Different methods for single-line and multi-line queries
41
if (target.length == 1)
42
this.matches = function(reverse, pos) {
43
var line = fold(cm.getLine(pos.line)), len = query.length, match;
44
if (reverse ? (pos.ch >= len && (match = line.lastIndexOf(query, pos.ch - len)) != -1)
45
: (match = line.indexOf(query, pos.ch)) != -1)
46
return {from: {line: pos.line, ch: match},
47
to: {line: pos.line, ch: match + len}};
50
this.matches = function(reverse, pos) {
51
var ln = pos.line, idx = (reverse ? target.length - 1 : 0), match = target[idx], line = fold(cm.getLine(ln));
52
var offsetA = (reverse ? line.indexOf(match) + match.length : line.lastIndexOf(match));
53
if (reverse ? offsetA >= pos.ch || offsetA != match.length
54
: offsetA <= pos.ch || offsetA != line.length - match.length)
57
if (reverse ? !ln : ln == cm.lineCount() - 1) return;
58
line = fold(cm.getLine(ln += reverse ? -1 : 1));
59
match = target[reverse ? --idx : ++idx];
60
if (idx > 0 && idx < target.length - 1) {
61
if (line != match) return;
64
var offsetB = (reverse ? line.lastIndexOf(match) : line.indexOf(match) + match.length);
65
if (reverse ? offsetB != line.length - match.length : offsetB != match.length)
67
var start = {line: pos.line, ch: offsetA}, end = {line: ln, ch: offsetB};
68
return {from: reverse ? end : start, to: reverse ? start : end};
74
SearchCursor.prototype = {
75
findNext: function() {return this.find(false);},
76
findPrevious: function() {return this.find(true);},
78
find: function(reverse) {
79
var self = this, pos = this.cm.clipPos(reverse ? this.pos.from : this.pos.to);
80
function savePosAndFail(line) {
81
var pos = {line: line, ch: 0};
82
self.pos = {from: pos, to: pos};
83
self.atOccurrence = false;
88
if (this.pos = this.matches(reverse, pos)) {
89
this.atOccurrence = true;
90
return this.pos.match || true;
93
if (!pos.line) return savePosAndFail(0);
94
pos = {line: pos.line-1, ch: this.cm.getLine(pos.line-1).length};
97
var maxLine = this.cm.lineCount();
98
if (pos.line == maxLine - 1) return savePosAndFail(maxLine);
99
pos = {line: pos.line+1, ch: 0};
104
from: function() {if (this.atOccurrence) return this.pos.from;},
105
to: function() {if (this.atOccurrence) return this.pos.to;},
107
replace: function(newText) {
109
if (this.atOccurrence)
110
self.pos.to = this.cm.replaceRange(newText, self.pos.from, self.pos.to);
114
CodeMirror.defineExtension("getSearchCursor", function(query, pos, caseFold) {
115
return new SearchCursor(this, query, pos, caseFold);