]> git.r.bdr.sh - rbdr/dotfiles/blob
58fed74d0056b1c879ef4a0ca16e2b50269993b1
[rbdr/dotfiles] /
1 (function(){
2 function SearchCursor(cm, query, pos, caseFold) {
3 this.atOccurrence = false; this.cm = cm;
4 if (caseFold == null && typeof query == "string") caseFold = false;
5
6 pos = pos ? cm.clipPos(pos) : {line: 0, ch: 0};
7 this.pos = {from: pos, to: pos};
8
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) {
16 if (reverse) {
17 query.lastIndex = 0;
18 var line = cm.getLine(pos.line).slice(0, pos.ch), match = query.exec(line), start = 0;
19 while (match) {
20 start += match.index + 1;
21 line = line.slice(start);
22 query.lastIndex = 0;
23 var newmatch = query.exec(line);
24 if (newmatch) match = newmatch;
25 else break;
26 }
27 start--;
28 } else {
29 query.lastIndex = pos.ch;
30 var line = cm.getLine(pos.line), match = query.exec(line),
31 start = match && match.index;
32 }
33 if (match)
34 return {from: {line: pos.line, ch: start},
35 to: {line: pos.line, ch: start + match[0].length},
36 match: match};
37 };
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}};
50 };
51 else
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)
57 return;
58 for (;;) {
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;
64 else continue;
65 }
66 var offsetB = (reverse ? line.lastIndexOf(match) : line.indexOf(match) + match.length);
67 if (reverse ? offsetB != line.length - match.length : offsetB != match.length)
68 return;
69 var start = {line: pos.line, ch: offsetA}, end = {line: ln, ch: offsetB};
70 return {from: reverse ? end : start, to: reverse ? start : end};
71 }
72 };
73 }
74 }
75
76 SearchCursor.prototype = {
77 findNext: function() {return this.find(false);},
78 findPrevious: function() {return this.find(true);},
79
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;
86 return false;
87 }
88
89 for (;;) {
90 if (this.pos = this.matches(reverse, pos)) {
91 this.atOccurrence = true;
92 return this.pos.match || true;
93 }
94 if (reverse) {
95 if (!pos.line) return savePosAndFail(0);
96 pos = {line: pos.line-1, ch: this.cm.getLine(pos.line-1).length};
97 }
98 else {
99 var maxLine = this.cm.lineCount();
100 if (pos.line == maxLine - 1) return savePosAndFail(maxLine);
101 pos = {line: pos.line+1, ch: 0};
102 }
103 }
104 },
105
106 from: function() {if (this.atOccurrence) return this.pos.from;},
107 to: function() {if (this.atOccurrence) return this.pos.to;},
108
109 replace: function(newText) {
110 var self = this;
111 if (this.atOccurrence)
112 self.pos.to = this.cm.replaceRange(newText, self.pos.from, self.pos.to);
113 }
114 };
115
116 CodeMirror.defineExtension("getSearchCursor", function(query, pos, caseFold) {
117 return new SearchCursor(this, query, pos, caseFold);
118 });
119 })();