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 DEFAULT_BRACKETS = "()[]{}''\"\""; michael@0: var DEFAULT_EXPLODE_ON_ENTER = "[]{}"; michael@0: var SPACE_CHAR_REGEX = /\s/; michael@0: michael@0: CodeMirror.defineOption("autoCloseBrackets", false, function(cm, val, old) { michael@0: if (old != CodeMirror.Init && old) michael@0: cm.removeKeyMap("autoCloseBrackets"); michael@0: if (!val) return; michael@0: var pairs = DEFAULT_BRACKETS, explode = DEFAULT_EXPLODE_ON_ENTER; michael@0: if (typeof val == "string") pairs = val; michael@0: else if (typeof val == "object") { michael@0: if (val.pairs != null) pairs = val.pairs; michael@0: if (val.explode != null) explode = val.explode; michael@0: } michael@0: var map = buildKeymap(pairs); michael@0: if (explode) map.Enter = buildExplodeHandler(explode); michael@0: cm.addKeyMap(map); michael@0: }); michael@0: michael@0: function charsAround(cm, pos) { michael@0: var str = cm.getRange(CodeMirror.Pos(pos.line, pos.ch - 1), michael@0: CodeMirror.Pos(pos.line, pos.ch + 1)); michael@0: return str.length == 2 ? str : null; michael@0: } michael@0: michael@0: function buildKeymap(pairs) { michael@0: var map = { michael@0: name : "autoCloseBrackets", michael@0: Backspace: function(cm) { michael@0: if (cm.getOption("disableInput")) return CodeMirror.Pass; michael@0: var ranges = cm.listSelections(); michael@0: for (var i = 0; i < ranges.length; i++) { michael@0: if (!ranges[i].empty()) return CodeMirror.Pass; michael@0: var around = charsAround(cm, ranges[i].head); michael@0: if (!around || pairs.indexOf(around) % 2 != 0) return CodeMirror.Pass; michael@0: } michael@0: for (var i = ranges.length - 1; i >= 0; i--) { michael@0: var cur = ranges[i].head; michael@0: cm.replaceRange("", CodeMirror.Pos(cur.line, cur.ch - 1), CodeMirror.Pos(cur.line, cur.ch + 1)); michael@0: } michael@0: } michael@0: }; michael@0: var closingBrackets = ""; michael@0: for (var i = 0; i < pairs.length; i += 2) (function(left, right) { michael@0: if (left != right) closingBrackets += right; michael@0: map["'" + left + "'"] = function(cm) { michael@0: if (cm.getOption("disableInput")) return CodeMirror.Pass; michael@0: var ranges = cm.listSelections(), type, next; michael@0: for (var i = 0; i < ranges.length; i++) { michael@0: var range = ranges[i], cur = range.head, curType; michael@0: if (left == "'" && cm.getTokenTypeAt(cur) == "comment") michael@0: return CodeMirror.Pass; michael@0: var next = cm.getRange(cur, CodeMirror.Pos(cur.line, cur.ch + 1)); michael@0: if (!range.empty()) michael@0: curType = "surround"; michael@0: else if (left == right && next == right) michael@0: curType = "skip"; michael@0: else if (left == right && CodeMirror.isWordChar(next)) michael@0: return CodeMirror.Pass; michael@0: else if (cm.getLine(cur.line).length == cur.ch || closingBrackets.indexOf(next) >= 0 || SPACE_CHAR_REGEX.test(next)) michael@0: curType = "both"; michael@0: else michael@0: return CodeMirror.Pass; michael@0: if (!type) type = curType; michael@0: else if (type != curType) return CodeMirror.Pass; michael@0: } michael@0: michael@0: if (type == "skip") { michael@0: cm.execCommand("goCharRight"); michael@0: } else if (type == "surround") { michael@0: var sels = cm.getSelections(); michael@0: for (var i = 0; i < sels.length; i++) michael@0: sels[i] = left + sels[i] + right; michael@0: cm.replaceSelections(sels, "around"); michael@0: } else if (type == "both") { michael@0: cm.replaceSelection(left + right, null); michael@0: cm.execCommand("goCharLeft"); michael@0: } michael@0: }; michael@0: if (left != right) map["'" + right + "'"] = function(cm) { michael@0: var ranges = cm.listSelections(); michael@0: for (var i = 0; i < ranges.length; i++) { michael@0: var range = ranges[i]; michael@0: if (!range.empty() || michael@0: cm.getRange(range.head, CodeMirror.Pos(range.head.line, range.head.ch + 1)) != right) michael@0: return CodeMirror.Pass; michael@0: } michael@0: cm.execCommand("goCharRight"); michael@0: }; michael@0: })(pairs.charAt(i), pairs.charAt(i + 1)); michael@0: return map; michael@0: } michael@0: michael@0: function buildExplodeHandler(pairs) { michael@0: return function(cm) { michael@0: if (cm.getOption("disableInput")) return CodeMirror.Pass; michael@0: var ranges = cm.listSelections(); michael@0: for (var i = 0; i < ranges.length; i++) { michael@0: if (!ranges[i].empty()) return CodeMirror.Pass; michael@0: var around = charsAround(cm, ranges[i].head); michael@0: if (!around || pairs.indexOf(around) % 2 != 0) return CodeMirror.Pass; michael@0: } michael@0: cm.operation(function() { michael@0: cm.replaceSelection("\n\n", null); michael@0: cm.execCommand("goCharLeft"); michael@0: ranges = cm.listSelections(); michael@0: for (var i = 0; i < ranges.length; i++) { michael@0: var line = ranges[i].head.line; michael@0: cm.indentLine(line, null, true); michael@0: cm.indentLine(line + 1, null, true); michael@0: } michael@0: }); michael@0: }; michael@0: } michael@0: });