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: "use strict"; michael@0: michael@0: var Pos = CodeMirror.Pos; michael@0: function posEq(a, b) { return a.line == b.line && a.ch == b.ch; } michael@0: michael@0: // Kill 'ring' michael@0: michael@0: var killRing = []; michael@0: function addToRing(str) { michael@0: killRing.push(str); michael@0: if (killRing.length > 50) killRing.shift(); michael@0: } michael@0: function growRingTop(str) { michael@0: if (!killRing.length) return addToRing(str); michael@0: killRing[killRing.length - 1] += str; michael@0: } michael@0: function getFromRing(n) { return killRing[killRing.length - (n ? Math.min(n, 1) : 1)] || ""; } michael@0: function popFromRing() { if (killRing.length > 1) killRing.pop(); return getFromRing(); } michael@0: michael@0: var lastKill = null; michael@0: michael@0: function kill(cm, from, to, mayGrow, text) { michael@0: if (text == null) text = cm.getRange(from, to); michael@0: michael@0: if (mayGrow && lastKill && lastKill.cm == cm && posEq(from, lastKill.pos) && cm.isClean(lastKill.gen)) michael@0: growRingTop(text); michael@0: else michael@0: addToRing(text); michael@0: cm.replaceRange("", from, to, "+delete"); michael@0: michael@0: if (mayGrow) lastKill = {cm: cm, pos: from, gen: cm.changeGeneration()}; michael@0: else lastKill = null; michael@0: } michael@0: michael@0: // Boundaries of various units michael@0: michael@0: function byChar(cm, pos, dir) { michael@0: return cm.findPosH(pos, dir, "char", true); michael@0: } michael@0: michael@0: function byWord(cm, pos, dir) { michael@0: return cm.findPosH(pos, dir, "word", true); michael@0: } michael@0: michael@0: function byLine(cm, pos, dir) { michael@0: return cm.findPosV(pos, dir, "line", cm.doc.sel.goalColumn); michael@0: } michael@0: michael@0: function byPage(cm, pos, dir) { michael@0: return cm.findPosV(pos, dir, "page", cm.doc.sel.goalColumn); michael@0: } michael@0: michael@0: function byParagraph(cm, pos, dir) { michael@0: var no = pos.line, line = cm.getLine(no); michael@0: var sawText = /\S/.test(dir < 0 ? line.slice(0, pos.ch) : line.slice(pos.ch)); michael@0: var fst = cm.firstLine(), lst = cm.lastLine(); michael@0: for (;;) { michael@0: no += dir; michael@0: if (no < fst || no > lst) michael@0: return cm.clipPos(Pos(no - dir, dir < 0 ? 0 : null)); michael@0: line = cm.getLine(no); michael@0: var hasText = /\S/.test(line); michael@0: if (hasText) sawText = true; michael@0: else if (sawText) return Pos(no, 0); michael@0: } michael@0: } michael@0: michael@0: function bySentence(cm, pos, dir) { michael@0: var line = pos.line, ch = pos.ch; michael@0: var text = cm.getLine(pos.line), sawWord = false; michael@0: for (;;) { michael@0: var next = text.charAt(ch + (dir < 0 ? -1 : 0)); michael@0: if (!next) { // End/beginning of line reached michael@0: if (line == (dir < 0 ? cm.firstLine() : cm.lastLine())) return Pos(line, ch); michael@0: text = cm.getLine(line + dir); michael@0: if (!/\S/.test(text)) return Pos(line, ch); michael@0: line += dir; michael@0: ch = dir < 0 ? text.length : 0; michael@0: continue; michael@0: } michael@0: if (sawWord && /[!?.]/.test(next)) return Pos(line, ch + (dir > 0 ? 1 : 0)); michael@0: if (!sawWord) sawWord = /\w/.test(next); michael@0: ch += dir; michael@0: } michael@0: } michael@0: michael@0: function byExpr(cm, pos, dir) { michael@0: var wrap; michael@0: if (cm.findMatchingBracket && (wrap = cm.findMatchingBracket(pos, true)) michael@0: && wrap.match && (wrap.forward ? 1 : -1) == dir) michael@0: return dir > 0 ? Pos(wrap.to.line, wrap.to.ch + 1) : wrap.to; michael@0: michael@0: for (var first = true;; first = false) { michael@0: var token = cm.getTokenAt(pos); michael@0: var after = Pos(pos.line, dir < 0 ? token.start : token.end); michael@0: if (first && dir > 0 && token.end == pos.ch || !/\w/.test(token.string)) { michael@0: var newPos = cm.findPosH(after, dir, "char"); michael@0: if (posEq(after, newPos)) return pos; michael@0: else pos = newPos; michael@0: } else { michael@0: return after; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Prefixes (only crudely supported) michael@0: michael@0: function getPrefix(cm, precise) { michael@0: var digits = cm.state.emacsPrefix; michael@0: if (!digits) return precise ? null : 1; michael@0: clearPrefix(cm); michael@0: return digits == "-" ? -1 : Number(digits); michael@0: } michael@0: michael@0: function repeated(cmd) { michael@0: var f = typeof cmd == "string" ? function(cm) { cm.execCommand(cmd); } : cmd; michael@0: return function(cm) { michael@0: var prefix = getPrefix(cm); michael@0: f(cm); michael@0: for (var i = 1; i < prefix; ++i) f(cm); michael@0: }; michael@0: } michael@0: michael@0: function findEnd(cm, by, dir) { michael@0: var pos = cm.getCursor(), prefix = getPrefix(cm); michael@0: if (prefix < 0) { dir = -dir; prefix = -prefix; } michael@0: for (var i = 0; i < prefix; ++i) { michael@0: var newPos = by(cm, pos, dir); michael@0: if (posEq(newPos, pos)) break; michael@0: pos = newPos; michael@0: } michael@0: return pos; michael@0: } michael@0: michael@0: function move(by, dir) { michael@0: var f = function(cm) { michael@0: cm.extendSelection(findEnd(cm, by, dir)); michael@0: }; michael@0: f.motion = true; michael@0: return f; michael@0: } michael@0: michael@0: function killTo(cm, by, dir) { michael@0: kill(cm, cm.getCursor(), findEnd(cm, by, dir), true); michael@0: } michael@0: michael@0: function addPrefix(cm, digit) { michael@0: if (cm.state.emacsPrefix) { michael@0: if (digit != "-") cm.state.emacsPrefix += digit; michael@0: return; michael@0: } michael@0: // Not active yet michael@0: cm.state.emacsPrefix = digit; michael@0: cm.on("keyHandled", maybeClearPrefix); michael@0: cm.on("inputRead", maybeDuplicateInput); michael@0: } michael@0: michael@0: var prefixPreservingKeys = {"Alt-G": true, "Ctrl-X": true, "Ctrl-Q": true, "Ctrl-U": true}; michael@0: michael@0: function maybeClearPrefix(cm, arg) { michael@0: if (!cm.state.emacsPrefixMap && !prefixPreservingKeys.hasOwnProperty(arg)) michael@0: clearPrefix(cm); michael@0: } michael@0: michael@0: function clearPrefix(cm) { michael@0: cm.state.emacsPrefix = null; michael@0: cm.off("keyHandled", maybeClearPrefix); michael@0: cm.off("inputRead", maybeDuplicateInput); michael@0: } michael@0: michael@0: function maybeDuplicateInput(cm, event) { michael@0: var dup = getPrefix(cm); michael@0: if (dup > 1 && event.origin == "+input") { michael@0: var one = event.text.join("\n"), txt = ""; michael@0: for (var i = 1; i < dup; ++i) txt += one; michael@0: cm.replaceSelection(txt); michael@0: } michael@0: } michael@0: michael@0: function addPrefixMap(cm) { michael@0: cm.state.emacsPrefixMap = true; michael@0: cm.addKeyMap(prefixMap); michael@0: cm.on("keyHandled", maybeRemovePrefixMap); michael@0: cm.on("inputRead", maybeRemovePrefixMap); michael@0: } michael@0: michael@0: function maybeRemovePrefixMap(cm, arg) { michael@0: if (typeof arg == "string" && (/^\d$/.test(arg) || arg == "Ctrl-U")) return; michael@0: cm.removeKeyMap(prefixMap); michael@0: cm.state.emacsPrefixMap = false; michael@0: cm.off("keyHandled", maybeRemovePrefixMap); michael@0: cm.off("inputRead", maybeRemovePrefixMap); michael@0: } michael@0: michael@0: // Utilities michael@0: michael@0: function setMark(cm) { michael@0: cm.setCursor(cm.getCursor()); michael@0: cm.setExtending(!cm.getExtending()); michael@0: cm.on("change", function() { cm.setExtending(false); }); michael@0: } michael@0: michael@0: function clearMark(cm) { michael@0: cm.setExtending(false); michael@0: cm.setCursor(cm.getCursor()); michael@0: } michael@0: michael@0: function getInput(cm, msg, f) { michael@0: if (cm.openDialog) michael@0: cm.openDialog(msg + ": ", f, {bottom: true}); michael@0: else michael@0: f(prompt(msg, "")); michael@0: } michael@0: michael@0: function operateOnWord(cm, op) { michael@0: var start = cm.getCursor(), end = cm.findPosH(start, 1, "word"); michael@0: cm.replaceRange(op(cm.getRange(start, end)), start, end); michael@0: cm.setCursor(end); michael@0: } michael@0: michael@0: function toEnclosingExpr(cm) { michael@0: var pos = cm.getCursor(), line = pos.line, ch = pos.ch; michael@0: var stack = []; michael@0: while (line >= cm.firstLine()) { michael@0: var text = cm.getLine(line); michael@0: for (var i = ch == null ? text.length : ch; i > 0;) { michael@0: var ch = text.charAt(--i); michael@0: if (ch == ")") michael@0: stack.push("("); michael@0: else if (ch == "]") michael@0: stack.push("["); michael@0: else if (ch == "}") michael@0: stack.push("{"); michael@0: else if (/[\(\{\[]/.test(ch) && (!stack.length || stack.pop() != ch)) michael@0: return cm.extendSelection(Pos(line, i)); michael@0: } michael@0: --line; ch = null; michael@0: } michael@0: } michael@0: michael@0: function quit(cm) { michael@0: cm.execCommand("clearSearch"); michael@0: clearMark(cm); michael@0: } michael@0: michael@0: // Actual keymap michael@0: michael@0: var keyMap = CodeMirror.keyMap.emacs = { michael@0: "Ctrl-W": function(cm) {kill(cm, cm.getCursor("start"), cm.getCursor("end"));}, michael@0: "Ctrl-K": repeated(function(cm) { michael@0: var start = cm.getCursor(), end = cm.clipPos(Pos(start.line)); michael@0: var text = cm.getRange(start, end); michael@0: if (!/\S/.test(text)) { michael@0: text += "\n"; michael@0: end = Pos(start.line + 1, 0); michael@0: } michael@0: kill(cm, start, end, true, text); michael@0: }), michael@0: "Alt-W": function(cm) { michael@0: addToRing(cm.getSelection()); michael@0: clearMark(cm); michael@0: }, michael@0: "Ctrl-Y": function(cm) { michael@0: var start = cm.getCursor(); michael@0: cm.replaceRange(getFromRing(getPrefix(cm)), start, start, "paste"); michael@0: cm.setSelection(start, cm.getCursor()); michael@0: }, michael@0: "Alt-Y": function(cm) {cm.replaceSelection(popFromRing(), "around", "paste");}, michael@0: michael@0: "Ctrl-Space": setMark, "Ctrl-Shift-2": setMark, michael@0: michael@0: "Ctrl-F": move(byChar, 1), "Ctrl-B": move(byChar, -1), michael@0: "Right": move(byChar, 1), "Left": move(byChar, -1), michael@0: "Ctrl-D": function(cm) { killTo(cm, byChar, 1); }, michael@0: "Delete": function(cm) { killTo(cm, byChar, 1); }, michael@0: "Ctrl-H": function(cm) { killTo(cm, byChar, -1); }, michael@0: "Backspace": function(cm) { killTo(cm, byChar, -1); }, michael@0: michael@0: "Alt-F": move(byWord, 1), "Alt-B": move(byWord, -1), michael@0: "Alt-D": function(cm) { killTo(cm, byWord, 1); }, michael@0: "Alt-Backspace": function(cm) { killTo(cm, byWord, -1); }, michael@0: michael@0: "Ctrl-N": move(byLine, 1), "Ctrl-P": move(byLine, -1), michael@0: "Down": move(byLine, 1), "Up": move(byLine, -1), michael@0: "Ctrl-A": "goLineStart", "Ctrl-E": "goLineEnd", michael@0: "End": "goLineEnd", "Home": "goLineStart", michael@0: michael@0: "Alt-V": move(byPage, -1), "Ctrl-V": move(byPage, 1), michael@0: "PageUp": move(byPage, -1), "PageDown": move(byPage, 1), michael@0: michael@0: "Ctrl-Up": move(byParagraph, -1), "Ctrl-Down": move(byParagraph, 1), michael@0: michael@0: "Alt-A": move(bySentence, -1), "Alt-E": move(bySentence, 1), michael@0: "Alt-K": function(cm) { killTo(cm, bySentence, 1); }, michael@0: michael@0: "Ctrl-Alt-K": function(cm) { killTo(cm, byExpr, 1); }, michael@0: "Ctrl-Alt-Backspace": function(cm) { killTo(cm, byExpr, -1); }, michael@0: "Ctrl-Alt-F": move(byExpr, 1), "Ctrl-Alt-B": move(byExpr, -1), michael@0: michael@0: "Shift-Ctrl-Alt-2": function(cm) { michael@0: cm.setSelection(findEnd(cm, byExpr, 1), cm.getCursor()); michael@0: }, michael@0: "Ctrl-Alt-T": function(cm) { michael@0: var leftStart = byExpr(cm, cm.getCursor(), -1), leftEnd = byExpr(cm, leftStart, 1); michael@0: var rightEnd = byExpr(cm, leftEnd, 1), rightStart = byExpr(cm, rightEnd, -1); michael@0: cm.replaceRange(cm.getRange(rightStart, rightEnd) + cm.getRange(leftEnd, rightStart) + michael@0: cm.getRange(leftStart, leftEnd), leftStart, rightEnd); michael@0: }, michael@0: "Ctrl-Alt-U": repeated(toEnclosingExpr), michael@0: michael@0: "Alt-Space": function(cm) { michael@0: var pos = cm.getCursor(), from = pos.ch, to = pos.ch, text = cm.getLine(pos.line); michael@0: while (from && /\s/.test(text.charAt(from - 1))) --from; michael@0: while (to < text.length && /\s/.test(text.charAt(to))) ++to; michael@0: cm.replaceRange(" ", Pos(pos.line, from), Pos(pos.line, to)); michael@0: }, michael@0: "Ctrl-O": repeated(function(cm) { cm.replaceSelection("\n", "start"); }), michael@0: "Ctrl-T": repeated(function(cm) { michael@0: var pos = cm.getCursor(); michael@0: if (pos.ch < cm.getLine(pos.line).length) pos = Pos(pos.line, pos.ch + 1); michael@0: var from = cm.findPosH(pos, -2, "char"); michael@0: var range = cm.getRange(from, pos); michael@0: if (range.length != 2) return; michael@0: cm.setSelection(from, pos); michael@0: cm.replaceSelection(range.charAt(1) + range.charAt(0), null, "+transpose"); michael@0: }), michael@0: michael@0: "Alt-C": repeated(function(cm) { michael@0: operateOnWord(cm, function(w) { michael@0: var letter = w.search(/\w/); michael@0: if (letter == -1) return w; michael@0: return w.slice(0, letter) + w.charAt(letter).toUpperCase() + w.slice(letter + 1).toLowerCase(); michael@0: }); michael@0: }), michael@0: "Alt-U": repeated(function(cm) { michael@0: operateOnWord(cm, function(w) { return w.toUpperCase(); }); michael@0: }), michael@0: "Alt-L": repeated(function(cm) { michael@0: operateOnWord(cm, function(w) { return w.toLowerCase(); }); michael@0: }), michael@0: michael@0: "Alt-;": "toggleComment", michael@0: michael@0: "Ctrl-/": repeated("undo"), "Shift-Ctrl--": repeated("undo"), michael@0: "Ctrl-Z": repeated("undo"), "Cmd-Z": repeated("undo"), michael@0: "Shift-Alt-,": "goDocStart", "Shift-Alt-.": "goDocEnd", michael@0: "Ctrl-S": "findNext", "Ctrl-R": "findPrev", "Ctrl-G": quit, "Shift-Alt-5": "replace", michael@0: "Alt-/": "autocomplete", michael@0: "Ctrl-J": "newlineAndIndent", "Enter": false, "Tab": "indentAuto", michael@0: michael@0: "Alt-G": function(cm) {cm.setOption("keyMap", "emacs-Alt-G");}, michael@0: "Ctrl-X": function(cm) {cm.setOption("keyMap", "emacs-Ctrl-X");}, michael@0: "Ctrl-Q": function(cm) {cm.setOption("keyMap", "emacs-Ctrl-Q");}, michael@0: "Ctrl-U": addPrefixMap michael@0: }; michael@0: michael@0: CodeMirror.keyMap["emacs-Ctrl-X"] = { michael@0: "Tab": function(cm) { michael@0: cm.indentSelection(getPrefix(cm, true) || cm.getOption("indentUnit")); michael@0: }, michael@0: "Ctrl-X": function(cm) { michael@0: cm.setSelection(cm.getCursor("head"), cm.getCursor("anchor")); michael@0: }, michael@0: michael@0: "Ctrl-S": "save", "Ctrl-W": "save", "S": "saveAll", "F": "open", "U": repeated("undo"), "K": "close", michael@0: "Delete": function(cm) { kill(cm, cm.getCursor(), bySentence(cm, cm.getCursor(), 1), true); }, michael@0: auto: "emacs", nofallthrough: true, disableInput: true michael@0: }; michael@0: michael@0: CodeMirror.keyMap["emacs-Alt-G"] = { michael@0: "G": function(cm) { michael@0: var prefix = getPrefix(cm, true); michael@0: if (prefix != null && prefix > 0) return cm.setCursor(prefix - 1); michael@0: michael@0: getInput(cm, "Goto line", function(str) { michael@0: var num; michael@0: if (str && !isNaN(num = Number(str)) && num == num|0 && num > 0) michael@0: cm.setCursor(num - 1); michael@0: }); michael@0: }, michael@0: auto: "emacs", nofallthrough: true, disableInput: true michael@0: }; michael@0: michael@0: CodeMirror.keyMap["emacs-Ctrl-Q"] = { michael@0: "Tab": repeated("insertTab"), michael@0: auto: "emacs", nofallthrough: true michael@0: }; michael@0: michael@0: var prefixMap = {"Ctrl-G": clearPrefix}; michael@0: function regPrefix(d) { michael@0: prefixMap[d] = function(cm) { addPrefix(cm, d); }; michael@0: keyMap["Ctrl-" + d] = function(cm) { addPrefix(cm, d); }; michael@0: prefixPreservingKeys["Ctrl-" + d] = true; michael@0: } michael@0: for (var i = 0; i < 10; ++i) regPrefix(String(i)); michael@0: regPrefix("-"); michael@0: });