1 // CodeMirror, copyright (c) by Marijn Haverbeke and others
2 // Distributed under an MIT license: https://codemirror.net/LICENSE
5 if (typeof exports == "object" && typeof module == "object") // CommonJS
6 mod(require("../../lib/codemirror"))
7 else if (typeof define == "function" && define.amd) // AMD
8 define(["../../lib/codemirror"], mod)
9 else // Plain browser env
11 })(function(CodeMirror) {
13 var Pos = CodeMirror.Pos
15 function regexpFlags(regexp) {
16 var flags = regexp.flags
17 return flags != null ? flags : (regexp.ignoreCase ? "i" : "")
18 + (regexp.global ? "g" : "")
19 + (regexp.multiline ? "m" : "")
22 function ensureFlags(regexp, flags) {
23 var current = regexpFlags(regexp), target = current
24 for (var i = 0; i < flags.length; i++) if (target.indexOf(flags.charAt(i)) == -1)
25 target += flags.charAt(i)
26 return current == target ? regexp : new RegExp(regexp.source, target)
29 function maybeMultiline(regexp) {
30 return /\\s|\\n|\n|\\W|\\D|\[\^/.test(regexp.source)
33 function searchRegexpForward(doc, regexp, start) {
34 regexp = ensureFlags(regexp, "g")
35 for (var line = start.line, ch = start.ch, last = doc.lastLine(); line <= last; line++, ch = 0) {
37 var string = doc.getLine(line), match = regexp.exec(string)
39 return {from: Pos(line, match.index),
40 to: Pos(line, match.index + match[0].length),
45 function searchRegexpForwardMultiline(doc, regexp, start) {
46 if (!maybeMultiline(regexp)) return searchRegexpForward(doc, regexp, start)
48 regexp = ensureFlags(regexp, "gm")
50 for (var line = start.line, last = doc.lastLine(); line <= last;) {
51 // This grows the search buffer in exponentially-sized chunks
52 // between matches, so that nearby matches are fast and don't
53 // require concatenating the whole document (in case we're
54 // searching for something that has tons of matches), but at the
55 // same time, the amount of retries is limited.
56 for (var i = 0; i < chunk; i++) {
57 if (line > last) break
58 var curLine = doc.getLine(line++)
59 string = string == null ? curLine : string + "\n" + curLine
62 regexp.lastIndex = start.ch
63 var match = regexp.exec(string)
65 var before = string.slice(0, match.index).split("\n"), inside = match[0].split("\n")
66 var startLine = start.line + before.length - 1, startCh = before[before.length - 1].length
67 return {from: Pos(startLine, startCh),
68 to: Pos(startLine + inside.length - 1,
69 inside.length == 1 ? startCh + inside[0].length : inside[inside.length - 1].length),
75 function lastMatchIn(string, regexp) {
78 regexp.lastIndex = cutOff
79 var newMatch = regexp.exec(string)
80 if (!newMatch) return match
82 cutOff = match.index + (match[0].length || 1)
83 if (cutOff == string.length) return match
87 function searchRegexpBackward(doc, regexp, start) {
88 regexp = ensureFlags(regexp, "g")
89 for (var line = start.line, ch = start.ch, first = doc.firstLine(); line >= first; line--, ch = -1) {
90 var string = doc.getLine(line)
91 if (ch > -1) string = string.slice(0, ch)
92 var match = lastMatchIn(string, regexp)
94 return {from: Pos(line, match.index),
95 to: Pos(line, match.index + match[0].length),
100 function searchRegexpBackwardMultiline(doc, regexp, start) {
101 regexp = ensureFlags(regexp, "gm")
102 var string, chunk = 1
103 for (var line = start.line, first = doc.firstLine(); line >= first;) {
104 for (var i = 0; i < chunk; i++) {
105 var curLine = doc.getLine(line--)
106 string = string == null ? curLine.slice(0, start.ch) : curLine + "\n" + string
110 var match = lastMatchIn(string, regexp)
112 var before = string.slice(0, match.index).split("\n"), inside = match[0].split("\n")
113 var startLine = line + before.length, startCh = before[before.length - 1].length
114 return {from: Pos(startLine, startCh),
115 to: Pos(startLine + inside.length - 1,
116 inside.length == 1 ? startCh + inside[0].length : inside[inside.length - 1].length),
123 if (String.prototype.normalize) {
124 doFold = function(str) { return str.normalize("NFD").toLowerCase() }
125 noFold = function(str) { return str.normalize("NFD") }
127 doFold = function(str) { return str.toLowerCase() }
128 noFold = function(str) { return str }
131 // Maps a position in a case-folded line back to a position in the original line
132 // (compensating for codepoints increasing in number during folding)
133 function adjustPos(orig, folded, pos, foldFunc) {
134 if (orig.length == folded.length) return pos
135 for (var min = 0, max = pos + Math.max(0, orig.length - folded.length);;) {
136 if (min == max) return min
137 var mid = (min + max) >> 1
138 var len = foldFunc(orig.slice(0, mid)).length
139 if (len == pos) return mid
140 else if (len > pos) max = mid
145 function searchStringForward(doc, query, start, caseFold) {
146 // Empty string would match anything and never progress, so we
147 // define it to match nothing instead.
148 if (!query.length) return null
149 var fold = caseFold ? doFold : noFold
150 var lines = fold(query).split(/\r|\n\r?/)
152 search: for (var line = start.line, ch = start.ch, last = doc.lastLine() + 1 - lines.length; line <= last; line++, ch = 0) {
153 var orig = doc.getLine(line).slice(ch), string = fold(orig)
154 if (lines.length == 1) {
155 var found = string.indexOf(lines[0])
156 if (found == -1) continue search
157 var start = adjustPos(orig, string, found, fold) + ch
158 return {from: Pos(line, adjustPos(orig, string, found, fold) + ch),
159 to: Pos(line, adjustPos(orig, string, found + lines[0].length, fold) + ch)}
161 var cutFrom = string.length - lines[0].length
162 if (string.slice(cutFrom) != lines[0]) continue search
163 for (var i = 1; i < lines.length - 1; i++)
164 if (fold(doc.getLine(line + i)) != lines[i]) continue search
165 var end = doc.getLine(line + lines.length - 1), endString = fold(end), lastLine = lines[lines.length - 1]
166 if (endString.slice(0, lastLine.length) != lastLine) continue search
167 return {from: Pos(line, adjustPos(orig, string, cutFrom, fold) + ch),
168 to: Pos(line + lines.length - 1, adjustPos(end, endString, lastLine.length, fold))}
173 function searchStringBackward(doc, query, start, caseFold) {
174 if (!query.length) return null
175 var fold = caseFold ? doFold : noFold
176 var lines = fold(query).split(/\r|\n\r?/)
178 search: for (var line = start.line, ch = start.ch, first = doc.firstLine() - 1 + lines.length; line >= first; line--, ch = -1) {
179 var orig = doc.getLine(line)
180 if (ch > -1) orig = orig.slice(0, ch)
181 var string = fold(orig)
182 if (lines.length == 1) {
183 var found = string.lastIndexOf(lines[0])
184 if (found == -1) continue search
185 return {from: Pos(line, adjustPos(orig, string, found, fold)),
186 to: Pos(line, adjustPos(orig, string, found + lines[0].length, fold))}
188 var lastLine = lines[lines.length - 1]
189 if (string.slice(0, lastLine.length) != lastLine) continue search
190 for (var i = 1, start = line - lines.length + 1; i < lines.length - 1; i++)
191 if (fold(doc.getLine(start + i)) != lines[i]) continue search
192 var top = doc.getLine(line + 1 - lines.length), topString = fold(top)
193 if (topString.slice(topString.length - lines[0].length) != lines[0]) continue search
194 return {from: Pos(line + 1 - lines.length, adjustPos(top, topString, top.length - lines[0].length, fold)),
195 to: Pos(line, adjustPos(orig, string, lastLine.length, fold))}
200 function SearchCursor(doc, query, pos, options) {
201 this.atOccurrence = false
203 pos = pos ? doc.clipPos(pos) : Pos(0, 0)
204 this.pos = {from: pos, to: pos}
207 if (typeof options == "object") {
208 caseFold = options.caseFold
209 } else { // Backwards compat for when caseFold was the 4th argument
214 if (typeof query == "string") {
215 if (caseFold == null) caseFold = false
216 this.matches = function(reverse, pos) {
217 return (reverse ? searchStringBackward : searchStringForward)(doc, query, pos, caseFold)
220 query = ensureFlags(query, "gm")
221 if (!options || options.multiline !== false)
222 this.matches = function(reverse, pos) {
223 return (reverse ? searchRegexpBackwardMultiline : searchRegexpForwardMultiline)(doc, query, pos)
226 this.matches = function(reverse, pos) {
227 return (reverse ? searchRegexpBackward : searchRegexpForward)(doc, query, pos)
232 SearchCursor.prototype = {
233 findNext: function() {return this.find(false)},
234 findPrevious: function() {return this.find(true)},
236 find: function(reverse) {
237 var result = this.matches(reverse, this.doc.clipPos(reverse ? this.pos.from : this.pos.to))
239 // Implements weird auto-growing behavior on null-matches for
240 // backwards-compatiblity with the vim code (unfortunately)
241 while (result && CodeMirror.cmpPos(result.from, result.to) == 0) {
243 if (result.from.ch) result.from = Pos(result.from.line, result.from.ch - 1)
244 else if (result.from.line == this.doc.firstLine()) result = null
245 else result = this.matches(reverse, this.doc.clipPos(Pos(result.from.line - 1)))
247 if (result.to.ch < this.doc.getLine(result.to.line).length) result.to = Pos(result.to.line, result.to.ch + 1)
248 else if (result.to.line == this.doc.lastLine()) result = null
249 else result = this.matches(reverse, Pos(result.to.line + 1, 0))
255 this.atOccurrence = true
256 return this.pos.match || true
258 var end = Pos(reverse ? this.doc.firstLine() : this.doc.lastLine() + 1, 0)
259 this.pos = {from: end, to: end}
260 return this.atOccurrence = false
264 from: function() {if (this.atOccurrence) return this.pos.from},
265 to: function() {if (this.atOccurrence) return this.pos.to},
267 replace: function(newText, origin) {
268 if (!this.atOccurrence) return
269 var lines = CodeMirror.splitLines(newText)
270 this.doc.replaceRange(lines, this.pos.from, this.pos.to, origin)
271 this.pos.to = Pos(this.pos.from.line + lines.length - 1,
272 lines[lines.length - 1].length + (lines.length == 1 ? this.pos.from.ch : 0))
276 CodeMirror.defineExtension("getSearchCursor", function(query, pos, caseFold) {
277 return new SearchCursor(this.doc, query, pos, caseFold)
279 CodeMirror.defineDocExtension("getSearchCursor", function(query, pos, caseFold) {
280 return new SearchCursor(this, query, pos, caseFold)
283 CodeMirror.defineExtension("selectMatches", function(query, caseFold) {
285 var cur = this.getSearchCursor(query, this.getCursor("from"), caseFold)
286 while (cur.findNext()) {
287 if (CodeMirror.cmpPos(cur.to(), this.getCursor("to")) > 0) break
288 ranges.push({anchor: cur.from(), head: cur.to()})
291 this.setSelections(ranges, 0)