michael@0: (function(mod) { michael@0: if (typeof exports == "object" && typeof module == "object") // CommonJS michael@0: mod(require("../../lib/codemirror")); michael@0: else if (typeof define == "function" && define.amd) // AMD michael@0: define(["../../lib/codemirror"], mod); michael@0: else // Plain browser env michael@0: mod(CodeMirror); michael@0: })(function(CodeMirror) { michael@0: var ie_lt8 = /MSIE \d/.test(navigator.userAgent) && michael@0: (document.documentMode == null || document.documentMode < 8); michael@0: michael@0: var Pos = CodeMirror.Pos; michael@0: michael@0: var matching = {"(": ")>", ")": "(<", "[": "]>", "]": "[<", "{": "}>", "}": "{<"}; michael@0: michael@0: function findMatchingBracket(cm, where, strict, config) { michael@0: var line = cm.getLineHandle(where.line), pos = where.ch - 1; michael@0: var match = (pos >= 0 && matching[line.text.charAt(pos)]) || matching[line.text.charAt(++pos)]; michael@0: if (!match) return null; michael@0: var dir = match.charAt(1) == ">" ? 1 : -1; michael@0: if (strict && (dir > 0) != (pos == where.ch)) return null; michael@0: var style = cm.getTokenTypeAt(Pos(where.line, pos + 1)); michael@0: michael@0: var found = scanForBracket(cm, Pos(where.line, pos + (dir > 0 ? 1 : 0)), dir, style || null, config); michael@0: return {from: Pos(where.line, pos), to: found && found.pos, michael@0: match: found && found.ch == match.charAt(0), forward: dir > 0}; michael@0: } michael@0: michael@0: function scanForBracket(cm, where, dir, style, config) { michael@0: var maxScanLen = (config && config.maxScanLineLength) || 10000; michael@0: var maxScanLines = (config && config.maxScanLines) || 500; michael@0: michael@0: var stack = [], re = /[(){}[\]]/; michael@0: var lineEnd = dir > 0 ? Math.min(where.line + maxScanLines, cm.lastLine() + 1) michael@0: : Math.max(cm.firstLine() - 1, where.line - maxScanLines); michael@0: for (var lineNo = where.line; lineNo != lineEnd; lineNo += dir) { michael@0: var line = cm.getLine(lineNo); michael@0: if (!line) continue; michael@0: var pos = dir > 0 ? 0 : line.length - 1, end = dir > 0 ? line.length : -1; michael@0: if (line.length > maxScanLen) continue; michael@0: if (lineNo == where.line) pos = where.ch - (dir < 0 ? 1 : 0); michael@0: for (; pos != end; pos += dir) { michael@0: var ch = line.charAt(pos); michael@0: if (re.test(ch) && (style === undefined || cm.getTokenTypeAt(Pos(lineNo, pos + 1)) == style)) { michael@0: var match = matching[ch]; michael@0: if ((match.charAt(1) == ">") == (dir > 0)) stack.push(ch); michael@0: else if (!stack.length) return {pos: Pos(lineNo, pos), ch: ch}; michael@0: else stack.pop(); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: function matchBrackets(cm, autoclear, config) { michael@0: // Disable brace matching in long lines, since it'll cause hugely slow updates michael@0: var maxHighlightLen = cm.state.matchBrackets.maxHighlightLineLength || 1000; michael@0: var marks = [], ranges = cm.listSelections(); michael@0: for (var i = 0; i < ranges.length; i++) { michael@0: var match = ranges[i].empty() && findMatchingBracket(cm, ranges[i].head, false, config); michael@0: if (match && cm.getLine(match.from.line).length <= maxHighlightLen && michael@0: match.to && cm.getLine(match.to.line).length <= maxHighlightLen) { michael@0: var style = match.match ? "CodeMirror-matchingbracket" : "CodeMirror-nonmatchingbracket"; michael@0: marks.push(cm.markText(match.from, Pos(match.from.line, match.from.ch + 1), {className: style})); michael@0: if (match.to) michael@0: marks.push(cm.markText(match.to, Pos(match.to.line, match.to.ch + 1), {className: style})); michael@0: } michael@0: } michael@0: michael@0: if (marks.length) { michael@0: // Kludge to work around the IE bug from issue #1193, where text michael@0: // input stops going to the textare whever this fires. michael@0: if (ie_lt8 && cm.state.focused) cm.display.input.focus(); michael@0: michael@0: var clear = function() { michael@0: cm.operation(function() { michael@0: for (var i = 0; i < marks.length; i++) marks[i].clear(); michael@0: }); michael@0: }; michael@0: if (autoclear) setTimeout(clear, 800); michael@0: else return clear; michael@0: } michael@0: } michael@0: michael@0: var currentlyHighlighted = null; michael@0: function doMatchBrackets(cm) { michael@0: cm.operation(function() { michael@0: if (currentlyHighlighted) {currentlyHighlighted(); currentlyHighlighted = null;} michael@0: currentlyHighlighted = matchBrackets(cm, false, cm.state.matchBrackets); michael@0: }); michael@0: } michael@0: michael@0: CodeMirror.defineOption("matchBrackets", false, function(cm, val, old) { michael@0: if (old && old != CodeMirror.Init) michael@0: cm.off("cursorActivity", doMatchBrackets); michael@0: if (val) { michael@0: cm.state.matchBrackets = typeof val == "object" ? val : {}; michael@0: cm.on("cursorActivity", doMatchBrackets); michael@0: } michael@0: }); michael@0: michael@0: CodeMirror.defineExtension("matchBrackets", function() {matchBrackets(this, true);}); michael@0: CodeMirror.defineExtension("findMatchingBracket", function(pos, strict){ michael@0: return findMatchingBracket(this, pos, strict); michael@0: }); michael@0: CodeMirror.defineExtension("scanForBracket", function(pos, dir, style){ michael@0: return scanForBracket(this, pos, dir, style); michael@0: }); michael@0: });