2 function SearchCursor(cm, query, pos, caseFold) {
3 this.atOccurrence = false; this.cm = cm;
4 if (caseFold == null && typeof query == "string") caseFold = false;
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 if (!query.global) query = new RegExp(query.source, query.ignoreCase ? "ig" : "g");
15 this.matches = function(reverse, pos) {
18 var line = cm.getLine(pos.line).slice(0, pos.ch), match = query.exec(line), start = 0;
20 start += match.index + 1;
21 line = line.slice(start);
23 var newmatch = query.exec(line);
24 if (newmatch) match = newmatch;
29 query.lastIndex = pos.ch;
30 var line = cm.getLine(pos.line), match = query.exec(line),
31 start = match && match.index;
34 return {from: {line: pos.line, ch: start},
35 to: {line: pos.line, ch: start + match[0].length},
38 } else { // String query
39 if (caseFold) query = query.toLowerCase();
40 var fold = caseFold ? function(str){return str.toLowerCase();} : function(str){return str;};
41 var target = query.split("\n");
42 // Different methods for single-line and multi-line queries
43 if (target.length == 1)
44 this.matches = function(reverse, pos) {
45 var line = fold(cm.getLine(pos.line)), len = query.length, match;
46 if (reverse ? (pos.ch >= len && (match = line.lastIndexOf(query, pos.ch - len)) != -1)
47 : (match = line.indexOf(query, pos.ch)) != -1)
48 return {from: {line: pos.line, ch: match},
49 to: {line: pos.line, ch: match + len}};
52 this.matches = function(reverse, pos) {
53 var ln = pos.line, idx = (reverse ? target.length - 1 : 0), match = target[idx], line = fold(cm.getLine(ln));
54 var offsetA = (reverse ? line.indexOf(match) + match.length : line.lastIndexOf(match));
55 if (reverse ? offsetA >= pos.ch || offsetA != match.length
56 : offsetA <= pos.ch || offsetA != line.length - match.length)
59 if (reverse ? !ln : ln == cm.lineCount() - 1) return;
60 line = fold(cm.getLine(ln += reverse ? -1 : 1));
61 match = target[reverse ? --idx : ++idx];
62 if (idx > 0 && idx < target.length - 1) {
63 if (line != match) return;
66 var offsetB = (reverse ? line.lastIndexOf(match) : line.indexOf(match) + match.length);
67 if (reverse ? offsetB != line.length - match.length : offsetB != match.length)
69 var start = {line: pos.line, ch: offsetA}, end = {line: ln, ch: offsetB};
70 return {from: reverse ? end : start, to: reverse ? start : end};
76 SearchCursor.prototype = {
77 findNext: function() {return this.find(false);},
78 findPrevious: function() {return this.find(true);},
80 find: function(reverse) {
81 var self = this, pos = this.cm.clipPos(reverse ? this.pos.from : this.pos.to);
82 function savePosAndFail(line) {
83 var pos = {line: line, ch: 0};
84 self.pos = {from: pos, to: pos};
85 self.atOccurrence = false;
90 if (this.pos = this.matches(reverse, pos)) {
91 this.atOccurrence = true;
92 return this.pos.match || true;
95 if (!pos.line) return savePosAndFail(0);
96 pos = {line: pos.line-1, ch: this.cm.getLine(pos.line-1).length};
99 var maxLine = this.cm.lineCount();
100 if (pos.line == maxLine - 1) return savePosAndFail(maxLine);
101 pos = {line: pos.line+1, ch: 0};
106 from: function() {if (this.atOccurrence) return this.pos.from;},
107 to: function() {if (this.atOccurrence) return this.pos.to;},
109 replace: function(newText) {
111 if (this.atOccurrence)
112 self.pos.to = this.cm.replaceRange(newText, self.pos.from, self.pos.to);
116 CodeMirror.defineExtension("getSearchCursor", function(query, pos, caseFold) {
117 return new SearchCursor(this, query, pos, caseFold);