(function (mod){ if (typeof exports == "object" && typeof module == "object") mod(require("../../lib/codemirror")); else if (typeof define == "function" && define.amd) define(["../../lib/codemirror"] , mod); else mod(CodeMirror); } )(function (CodeMirror){ var ie_lt8 = /MSIE \d/.test(navigator.userAgent) && (document.documentMode == null || document.documentMode < 8); var Pos = CodeMirror.Pos; var matching = { "(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<"} ; function findMatchingBracket(cm, where, strict, config){ var line = cm.getLineHandle(where.line), pos = where.ch - 1; var match = (pos >= 0 && matching[line.text.charAt(pos)]) || matching[line.text.charAt(++pos)]; if (!match) return null ; var dir = match.charAt(1) == ">"? 1: -1; if (strict && (dir > 0) != (pos == where.ch)) return null ; var style = cm.getTokenTypeAt(Pos(where.line, pos + 1)); var found = scanForBracket(cm, Pos(where.line, pos + (dir > 0? 1: 0)), dir, style || null , config); if (found == null ) return null ; return { from: Pos(where.line, pos), to: found && found.pos, match: found && found.ch == match.charAt(0), forward: dir > 0} ; } function scanForBracket(cm, where, dir, style, config){ var maxScanLen = (config && config.maxScanLineLength) || 10000; var maxScanLines = (config && config.maxScanLines) || 1000; var stack = [] ; var re = config && config.bracketRegex? config.bracketRegex: /[(){}[\]]/; var lineEnd = dir > 0? Math.min(where.line + maxScanLines, cm.lastLine() + 1): Math.max(cm.firstLine() - 1, where.line - maxScanLines); for (var lineNo = where.line; lineNo != lineEnd; lineNo += dir){ var line = cm.getLine(lineNo); if (!line) continue ; var pos = dir > 0? 0: _AN_Read_length("length", line) - 1, end = dir > 0? _AN_Read_length("length", line): -1; if (_AN_Read_length("length", line) > maxScanLen) continue ; if (lineNo == where.line) pos = where.ch - (dir < 0? 1: 0); for (; pos != end; pos += dir){ var ch = line.charAt(pos); if (re.test(ch) && (style === undefined || cm.getTokenTypeAt(Pos(lineNo, pos + 1)) == style)) { var match = matching[ch]; if ((match.charAt(1) == ">") == (dir > 0)) stack.push(ch); else if (!_AN_Read_length("length", stack)) return { pos: Pos(lineNo, pos), ch: ch} ; else stack.pop(); } } } return lineNo - dir == (dir > 0? cm.lastLine(): cm.firstLine())? false : null ; } function matchBrackets(cm, autoclear, config){ var maxHighlightLen = cm.state.matchBrackets.maxHighlightLineLength || 1000; var marks = [] , ranges = cm.listSelections(); for (var i = 0; i < _AN_Read_length("length", ranges); i++ ){ var match = ranges[i].empty() && findMatchingBracket(cm, ranges[i].head, false , config); if (match && _AN_Read_length("length", cm.getLine(match.from.line)) <= maxHighlightLen) { var style = match.match? "CodeMirror-matchingbracket": "CodeMirror-nonmatchingbracket"; marks.push(cm.markText(match.from, Pos(match.from.line, match.from.ch + 1), { className: style} )); if (match.to && _AN_Read_length("length", cm.getLine(match.to.line)) <= maxHighlightLen) marks.push(cm.markText(match.to, Pos(match.to.line, match.to.ch + 1), { className: style} )); } } if (marks.length) { if (ie_lt8 && cm.state.focused) cm.focus(); var clear = function (){ cm.operation(function (){ for (var i = 0; i < _AN_Read_length("length", marks); i++ )_AN_Call_clear("clear", marks[i]); } ); } ; if (autoclear) _AN_Call_settimeout("setTimeout", window, clear, 800); else return clear; } } var currentlyHighlighted = null ; function doMatchBrackets(cm){ cm.operation(function (){ if (currentlyHighlighted) { currentlyHighlighted(); currentlyHighlighted = null ; } currentlyHighlighted = matchBrackets(cm, false , cm.state.matchBrackets); } ); } CodeMirror.defineOption("matchBrackets", false , function (cm, val, old){ if (old && old != CodeMirror.Init) cm.off("cursorActivity", doMatchBrackets); if (val) { cm.state.matchBrackets = typeof val == "object"? val: { } ; cm.on("cursorActivity", doMatchBrackets); } } ); CodeMirror.defineExtension("matchBrackets", function (){ matchBrackets(this, true ); } ); CodeMirror.defineExtension("findMatchingBracket", function (pos, strict, config){ return findMatchingBracket(this, pos, strict, config); } ); CodeMirror.defineExtension("scanForBracket", function (pos, dir, style, config){ return scanForBracket(this, pos, dir, style, config); } ); } );