5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"));
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror"], mod);
11
})(function(CodeMirror) {
12
var ie_lt8 = /MSIE \d/.test(navigator.userAgent) &&
13
(document.documentMode == null || document.documentMode < 8);
15
var Pos = CodeMirror.Pos;
17
var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<", "<": ">>", ">": "<<"};
19
function bracketRegex(config) {
20
return config && config.bracketRegex || /[(){}[\]]/
23
function findMatchingBracket(cm, where, config) {
24
var line = cm.getLineHandle(where.line), pos = where.ch - 1;
25
var afterCursor = config && config.afterCursor
26
if (afterCursor == null)
27
afterCursor = /(^| )cm-fat-cursor($| )/.test(cm.getWrapperElement().className)
28
var re = bracketRegex(config)
34
var match = (!afterCursor && pos >= 0 && re.test(line.text.charAt(pos)) && matching[line.text.charAt(pos)]) ||
35
re.test(line.text.charAt(pos + 1)) && matching[line.text.charAt(++pos)];
36
if (!match) return null;
37
var dir = match.charAt(1) == ">" ? 1 : -1;
38
if (config && config.strict && (dir > 0) != (pos == where.ch)) return null;
39
var style = cm.getTokenTypeAt(Pos(where.line, pos + 1));
41
var found = scanForBracket(cm, Pos(where.line, pos + (dir > 0 ? 1 : 0)), dir, style, config);
42
if (found == null) return null;
43
return {from: Pos(where.line, pos), to: found && found.pos,
44
match: found && found.ch == match.charAt(0), forward: dir > 0};
54
function scanForBracket(cm, where, dir, style, config) {
55
var maxScanLen = (config && config.maxScanLineLength) || 10000;
56
var maxScanLines = (config && config.maxScanLines) || 1000;
59
var re = bracketRegex(config)
60
var lineEnd = dir > 0 ? Math.min(where.line + maxScanLines, cm.lastLine() + 1)
61
: Math.max(cm.firstLine() - 1, where.line - maxScanLines);
62
for (var lineNo = where.line; lineNo != lineEnd; lineNo += dir) {
63
var line = cm.getLine(lineNo);
65
var pos = dir > 0 ? 0 : line.length - 1, end = dir > 0 ? line.length : -1;
66
if (line.length > maxScanLen) continue;
67
if (lineNo == where.line) pos = where.ch - (dir < 0 ? 1 : 0);
68
for (; pos != end; pos += dir) {
69
var ch = line.charAt(pos);
70
if (re.test(ch) && (style === undefined ||
71
(cm.getTokenTypeAt(Pos(lineNo, pos + 1)) || "") == (style || ""))) {
72
var match = matching[ch];
73
if (match && (match.charAt(1) == ">") == (dir > 0)) stack.push(ch);
74
else if (!stack.length) return {pos: Pos(lineNo, pos), ch: ch};
79
return lineNo - dir == (dir > 0 ? cm.lastLine() : cm.firstLine()) ? false : null;
82
function matchBrackets(cm, autoclear, config) {
84
var maxHighlightLen = cm.state.matchBrackets.maxHighlightLineLength || 1000,
85
highlightNonMatching = config && config.highlightNonMatching;
86
var marks = [], ranges = cm.listSelections();
87
for (var i = 0; i < ranges.length; i++) {
88
var match = ranges[i].empty() && findMatchingBracket(cm, ranges[i].head, config);
89
if (match && (match.match || highlightNonMatching !== false) && cm.getLine(match.from.line).length <= maxHighlightLen) {
90
var style = match.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket";
91
marks.push(cm.markText(match.from, Pos(match.from.line, match.from.ch + 1), {className: style}));
92
if (match.to && cm.getLine(match.to.line).length <= maxHighlightLen)
93
marks.push(cm.markText(match.to, Pos(match.to.line, match.to.ch + 1), {className: style}));
100
if (ie_lt8 && cm.state.focused) cm.focus();
102
var clear = function() {
103
cm.operation(function() {
104
for (var i = 0; i < marks.length; i++) marks[i].clear();
107
if (autoclear) setTimeout(clear, 800);
112
function doMatchBrackets(cm) {
113
cm.operation(function() {
114
if (cm.state.matchBrackets.currentlyHighlighted) {
115
cm.state.matchBrackets.currentlyHighlighted();
116
cm.state.matchBrackets.currentlyHighlighted = null;
118
cm.state.matchBrackets.currentlyHighlighted = matchBrackets(cm, false, cm.state.matchBrackets);
122
function clearHighlighted(cm) {
123
if (cm.state.matchBrackets && cm.state.matchBrackets.currentlyHighlighted) {
124
cm.state.matchBrackets.currentlyHighlighted();
125
cm.state.matchBrackets.currentlyHighlighted = null;
129
CodeMirror.defineOption("matchBrackets", false, function(cm, val, old) {
130
if (old && old != CodeMirror.Init) {
131
cm.off("cursorActivity", doMatchBrackets);
132
cm.off("focus", doMatchBrackets)
133
cm.off("blur", clearHighlighted)
134
clearHighlighted(cm);
137
cm.state.matchBrackets = typeof val == "object" ? val : {};
138
cm.on("cursorActivity", doMatchBrackets);
139
cm.on("focus", doMatchBrackets)
140
cm.on("blur", clearHighlighted)
144
CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);});
145
CodeMirror.defineExtension("findMatchingBracket", function(pos, config, oldConfig){
147
if (oldConfig || typeof config == "boolean") {
149
config = config ? {strict: true} : null
151
oldConfig.strict = config
155
return findMatchingBracket(this, pos, config)
157
CodeMirror.defineExtension("scanForBracket", function(pos, dir, style, config){
158
return scanForBracket(this, pos, dir, style, config);