Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | (function(mod) { |
michael@0 | 2 | if (typeof exports == "object" && typeof module == "object") // CommonJS |
michael@0 | 3 | mod(require("../../lib/codemirror")); |
michael@0 | 4 | else if (typeof define == "function" && define.amd) // AMD |
michael@0 | 5 | define(["../../lib/codemirror"], mod); |
michael@0 | 6 | else // Plain browser env |
michael@0 | 7 | mod(CodeMirror); |
michael@0 | 8 | })(function(CodeMirror) { |
michael@0 | 9 | "use strict"; |
michael@0 | 10 | var Pos = CodeMirror.Pos; |
michael@0 | 11 | |
michael@0 | 12 | function SearchCursor(doc, query, pos, caseFold) { |
michael@0 | 13 | this.atOccurrence = false; this.doc = doc; |
michael@0 | 14 | if (caseFold == null && typeof query == "string") caseFold = false; |
michael@0 | 15 | |
michael@0 | 16 | pos = pos ? doc.clipPos(pos) : Pos(0, 0); |
michael@0 | 17 | this.pos = {from: pos, to: pos}; |
michael@0 | 18 | |
michael@0 | 19 | // The matches method is filled in based on the type of query. |
michael@0 | 20 | // It takes a position and a direction, and returns an object |
michael@0 | 21 | // describing the next occurrence of the query, or null if no |
michael@0 | 22 | // more matches were found. |
michael@0 | 23 | if (typeof query != "string") { // Regexp match |
michael@0 | 24 | if (!query.global) query = new RegExp(query.source, query.ignoreCase ? "ig" : "g"); |
michael@0 | 25 | this.matches = function(reverse, pos) { |
michael@0 | 26 | if (reverse) { |
michael@0 | 27 | query.lastIndex = 0; |
michael@0 | 28 | var line = doc.getLine(pos.line).slice(0, pos.ch), cutOff = 0, match, start; |
michael@0 | 29 | for (;;) { |
michael@0 | 30 | query.lastIndex = cutOff; |
michael@0 | 31 | var newMatch = query.exec(line); |
michael@0 | 32 | if (!newMatch) break; |
michael@0 | 33 | match = newMatch; |
michael@0 | 34 | start = match.index; |
michael@0 | 35 | cutOff = match.index + (match[0].length || 1); |
michael@0 | 36 | if (cutOff == line.length) break; |
michael@0 | 37 | } |
michael@0 | 38 | var matchLen = (match && match[0].length) || 0; |
michael@0 | 39 | if (!matchLen) { |
michael@0 | 40 | if (start == 0 && line.length == 0) {match = undefined;} |
michael@0 | 41 | else if (start != doc.getLine(pos.line).length) { |
michael@0 | 42 | matchLen++; |
michael@0 | 43 | } |
michael@0 | 44 | } |
michael@0 | 45 | } else { |
michael@0 | 46 | query.lastIndex = pos.ch; |
michael@0 | 47 | var line = doc.getLine(pos.line), match = query.exec(line); |
michael@0 | 48 | var matchLen = (match && match[0].length) || 0; |
michael@0 | 49 | var start = match && match.index; |
michael@0 | 50 | if (start + matchLen != line.length && !matchLen) matchLen = 1; |
michael@0 | 51 | } |
michael@0 | 52 | if (match && matchLen) |
michael@0 | 53 | return {from: Pos(pos.line, start), |
michael@0 | 54 | to: Pos(pos.line, start + matchLen), |
michael@0 | 55 | match: match}; |
michael@0 | 56 | }; |
michael@0 | 57 | } else { // String query |
michael@0 | 58 | var origQuery = query; |
michael@0 | 59 | if (caseFold) query = query.toLowerCase(); |
michael@0 | 60 | var fold = caseFold ? function(str){return str.toLowerCase();} : function(str){return str;}; |
michael@0 | 61 | var target = query.split("\n"); |
michael@0 | 62 | // Different methods for single-line and multi-line queries |
michael@0 | 63 | if (target.length == 1) { |
michael@0 | 64 | if (!query.length) { |
michael@0 | 65 | // Empty string would match anything and never progress, so |
michael@0 | 66 | // we define it to match nothing instead. |
michael@0 | 67 | this.matches = function() {}; |
michael@0 | 68 | } else { |
michael@0 | 69 | this.matches = function(reverse, pos) { |
michael@0 | 70 | if (reverse) { |
michael@0 | 71 | var orig = doc.getLine(pos.line).slice(0, pos.ch), line = fold(orig); |
michael@0 | 72 | var match = line.lastIndexOf(query); |
michael@0 | 73 | if (match > -1) { |
michael@0 | 74 | match = adjustPos(orig, line, match); |
michael@0 | 75 | return {from: Pos(pos.line, match), to: Pos(pos.line, match + origQuery.length)}; |
michael@0 | 76 | } |
michael@0 | 77 | } else { |
michael@0 | 78 | var orig = doc.getLine(pos.line).slice(pos.ch), line = fold(orig); |
michael@0 | 79 | var match = line.indexOf(query); |
michael@0 | 80 | if (match > -1) { |
michael@0 | 81 | match = adjustPos(orig, line, match) + pos.ch; |
michael@0 | 82 | return {from: Pos(pos.line, match), to: Pos(pos.line, match + origQuery.length)}; |
michael@0 | 83 | } |
michael@0 | 84 | } |
michael@0 | 85 | }; |
michael@0 | 86 | } |
michael@0 | 87 | } else { |
michael@0 | 88 | var origTarget = origQuery.split("\n"); |
michael@0 | 89 | this.matches = function(reverse, pos) { |
michael@0 | 90 | var last = target.length - 1; |
michael@0 | 91 | if (reverse) { |
michael@0 | 92 | if (pos.line - (target.length - 1) < doc.firstLine()) return; |
michael@0 | 93 | if (fold(doc.getLine(pos.line).slice(0, origTarget[last].length)) != target[target.length - 1]) return; |
michael@0 | 94 | var to = Pos(pos.line, origTarget[last].length); |
michael@0 | 95 | for (var ln = pos.line - 1, i = last - 1; i >= 1; --i, --ln) |
michael@0 | 96 | if (target[i] != fold(doc.getLine(ln))) return; |
michael@0 | 97 | var line = doc.getLine(ln), cut = line.length - origTarget[0].length; |
michael@0 | 98 | if (fold(line.slice(cut)) != target[0]) return; |
michael@0 | 99 | return {from: Pos(ln, cut), to: to}; |
michael@0 | 100 | } else { |
michael@0 | 101 | if (pos.line + (target.length - 1) > doc.lastLine()) return; |
michael@0 | 102 | var line = doc.getLine(pos.line), cut = line.length - origTarget[0].length; |
michael@0 | 103 | if (fold(line.slice(cut)) != target[0]) return; |
michael@0 | 104 | var from = Pos(pos.line, cut); |
michael@0 | 105 | for (var ln = pos.line + 1, i = 1; i < last; ++i, ++ln) |
michael@0 | 106 | if (target[i] != fold(doc.getLine(ln))) return; |
michael@0 | 107 | if (doc.getLine(ln).slice(0, origTarget[last].length) != target[last]) return; |
michael@0 | 108 | return {from: from, to: Pos(ln, origTarget[last].length)}; |
michael@0 | 109 | } |
michael@0 | 110 | }; |
michael@0 | 111 | } |
michael@0 | 112 | } |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | SearchCursor.prototype = { |
michael@0 | 116 | findNext: function() {return this.find(false);}, |
michael@0 | 117 | findPrevious: function() {return this.find(true);}, |
michael@0 | 118 | |
michael@0 | 119 | find: function(reverse) { |
michael@0 | 120 | var self = this, pos = this.doc.clipPos(reverse ? this.pos.from : this.pos.to); |
michael@0 | 121 | function savePosAndFail(line) { |
michael@0 | 122 | var pos = Pos(line, 0); |
michael@0 | 123 | self.pos = {from: pos, to: pos}; |
michael@0 | 124 | self.atOccurrence = false; |
michael@0 | 125 | return false; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | for (;;) { |
michael@0 | 129 | if (this.pos = this.matches(reverse, pos)) { |
michael@0 | 130 | this.atOccurrence = true; |
michael@0 | 131 | return this.pos.match || true; |
michael@0 | 132 | } |
michael@0 | 133 | if (reverse) { |
michael@0 | 134 | if (!pos.line) return savePosAndFail(0); |
michael@0 | 135 | pos = Pos(pos.line-1, this.doc.getLine(pos.line-1).length); |
michael@0 | 136 | } |
michael@0 | 137 | else { |
michael@0 | 138 | var maxLine = this.doc.lineCount(); |
michael@0 | 139 | if (pos.line == maxLine - 1) return savePosAndFail(maxLine); |
michael@0 | 140 | pos = Pos(pos.line + 1, 0); |
michael@0 | 141 | } |
michael@0 | 142 | } |
michael@0 | 143 | }, |
michael@0 | 144 | |
michael@0 | 145 | from: function() {if (this.atOccurrence) return this.pos.from;}, |
michael@0 | 146 | to: function() {if (this.atOccurrence) return this.pos.to;}, |
michael@0 | 147 | |
michael@0 | 148 | replace: function(newText) { |
michael@0 | 149 | if (!this.atOccurrence) return; |
michael@0 | 150 | var lines = CodeMirror.splitLines(newText); |
michael@0 | 151 | this.doc.replaceRange(lines, this.pos.from, this.pos.to); |
michael@0 | 152 | this.pos.to = Pos(this.pos.from.line + lines.length - 1, |
michael@0 | 153 | lines[lines.length - 1].length + (lines.length == 1 ? this.pos.from.ch : 0)); |
michael@0 | 154 | } |
michael@0 | 155 | }; |
michael@0 | 156 | |
michael@0 | 157 | // Maps a position in a case-folded line back to a position in the original line |
michael@0 | 158 | // (compensating for codepoints increasing in number during folding) |
michael@0 | 159 | function adjustPos(orig, folded, pos) { |
michael@0 | 160 | if (orig.length == folded.length) return pos; |
michael@0 | 161 | for (var pos1 = Math.min(pos, orig.length);;) { |
michael@0 | 162 | var len1 = orig.slice(0, pos1).toLowerCase().length; |
michael@0 | 163 | if (len1 < pos) ++pos1; |
michael@0 | 164 | else if (len1 > pos) --pos1; |
michael@0 | 165 | else return pos1; |
michael@0 | 166 | } |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | CodeMirror.defineExtension("getSearchCursor", function(query, pos, caseFold) { |
michael@0 | 170 | return new SearchCursor(this.doc, query, pos, caseFold); |
michael@0 | 171 | }); |
michael@0 | 172 | CodeMirror.defineDocExtension("getSearchCursor", function(query, pos, caseFold) { |
michael@0 | 173 | return new SearchCursor(this, query, pos, caseFold); |
michael@0 | 174 | }); |
michael@0 | 175 | |
michael@0 | 176 | CodeMirror.defineExtension("selectMatches", function(query, caseFold) { |
michael@0 | 177 | var ranges = [], next; |
michael@0 | 178 | var cur = this.getSearchCursor(query, this.getCursor("from"), caseFold); |
michael@0 | 179 | while (next = cur.findNext()) { |
michael@0 | 180 | if (CodeMirror.cmpPos(cur.to(), this.getCursor("to")) > 0) break; |
michael@0 | 181 | ranges.push({anchor: cur.from(), head: cur.to()}); |
michael@0 | 182 | } |
michael@0 | 183 | if (ranges.length) |
michael@0 | 184 | this.setSelections(ranges, 0); |
michael@0 | 185 | }); |
michael@0 | 186 | }); |