From 3a754133dbc138390503341fd2e9beba3e43aa4b Mon Sep 17 00:00:00 2001 From: Jose Antonio Marquez Date: Fri, 27 Jan 2012 12:05:17 -0800 Subject: Merged old FileIO --- imports/codemirror/keymap/emacs.js | 29 +++++++++++++++ imports/codemirror/keymap/vim.js | 76 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100755 imports/codemirror/keymap/emacs.js create mode 100755 imports/codemirror/keymap/vim.js (limited to 'imports/codemirror/keymap') diff --git a/imports/codemirror/keymap/emacs.js b/imports/codemirror/keymap/emacs.js new file mode 100755 index 00000000..8fd3564e --- /dev/null +++ b/imports/codemirror/keymap/emacs.js @@ -0,0 +1,29 @@ +// TODO number prefixes +(function() { + // Really primitive kill-ring implementation. + var killRing = []; + function addToRing(str) { + killRing.push(str); + if (killRing.length > 50) killRing.shift(); + } + function getFromRing() { return killRing[killRing.length - 1] || ""; } + function popFromRing() { if (killRing.length > 1) killRing.pop(); return getFromRing(); } + + CodeMirror.keyMap.emacs = { + "Ctrl-X": function(cm) {cm.setOption("keyMap", "emacs-Ctrl-X");}, + "Ctrl-W": function(cm) {addToRing(cm.getSelection()); cm.replaceSelection("");}, + "Ctrl-Alt-W": function(cm) {addToRing(cm.getSelection()); cm.replaceSelection("");}, + "Alt-W": function(cm) {addToRing(cm.getSelection());}, + "Ctrl-Y": function(cm) {cm.replaceSelection(getFromRing());}, + "Alt-Y": function(cm) {cm.replaceSelection(popFromRing());}, + "Ctrl-/": "undo", "Shift-Ctrl--": "undo", "Shift-Alt-,": "goDocStart", "Shift-Alt-.": "goDocEnd", + "Ctrl-S": "findNext", "Ctrl-R": "findPrev", "Ctrl-G": "clearSearch", "Shift-Alt-5": "replace", + "Ctrl-Z": "undo", "Cmd-Z": "undo", + fallthrough: ["basic", "emacsy"] + }; + + CodeMirror.keyMap["emacs-Ctrl-X"] = { + "Ctrl-S": "save", "Ctrl-W": "save", "S": "saveAll", "F": "open", "U": "undo", "K": "close", + auto: "emacs", catchall: function(cm) {/*ignore*/} + }; +})(); diff --git a/imports/codemirror/keymap/vim.js b/imports/codemirror/keymap/vim.js new file mode 100755 index 00000000..e03e0128 --- /dev/null +++ b/imports/codemirror/keymap/vim.js @@ -0,0 +1,76 @@ +(function() { + var count = ""; + function pushCountDigit(digit) { return function(cm) {count += digit;} } + function popCount() { var i = parseInt(count); count = ""; return i || 1; } + function countTimes(func) { + if (typeof func == "string") func = CodeMirror.commands[func]; + return function(cm) { for (var i = 0, c = popCount(); i < c; ++i) func(cm); } + } + + function iterObj(o, f) { + for (var prop in o) if (o.hasOwnProperty(prop)) f(prop, o[prop]); + } + + var word = [/\w/, /[^\w\s]/], bigWord = [/\S/]; + function findWord(line, pos, dir, regexps) { + var stop = 0, next = -1; + if (dir > 0) { stop = line.length; next = 0; } + var start = stop, end = stop; + // Find bounds of next one. + outer: for (; pos != stop; pos += dir) { + for (var i = 0; i < regexps.length; ++i) { + if (regexps[i].test(line.charAt(pos + next))) { + start = pos; + for (; pos != stop; pos += dir) { + if (!regexps[i].test(line.charAt(pos + next))) break; + } + end = pos; + break outer; + } + } + } + return {from: Math.min(start, end), to: Math.max(start, end)}; + } + function moveToWord(cm, regexps, dir, where) { + var cur = cm.getCursor(), ch = cur.ch, line = cm.getLine(cur.line), word; + while (true) { + word = findWord(line, ch, dir, regexps); + ch = word[where == "end" ? "to" : "from"]; + if (ch == cur.ch && word.from != word.to) ch = word[dir < 0 ? "from" : "to"]; + else break; + } + cm.setCursor(cur.line, word[where == "end" ? "to" : "from"], true); + } + + var map = CodeMirror.keyMap.vim = { + "0": function(cm) {count.length > 0 ? pushCountDigit("0")(cm) : CodeMirror.commands.goLineStart(cm);}, + "I": function(cm) {popCount(); cm.setOption("keyMap", "vim-insert");}, + "G": function(cm) {cm.setOption("keyMap", "vim-prefix-g");}, + catchall: function(cm) {/*ignore*/} + }; + // Add bindings for number keys + for (var i = 1; i < 10; ++i) map[i] = pushCountDigit(i); + // Add bindings that are influenced by number keys + iterObj({"H": "goColumnLeft", "L": "goColumnRight", "J": "goLineDown", "K": "goLineUp", + "Left": "goColumnLeft", "Right": "goColumnRight", "Down": "goLineDown", "Up": "goLineUp", + "Backspace": "goCharLeft", "Space": "goCharRight", + "B": function(cm) {moveToWord(cm, word, -1, "end");}, + "E": function(cm) {moveToWord(cm, word, 1, "end");}, + "W": function(cm) {moveToWord(cm, word, 1, "start");}, + "Shift-B": function(cm) {moveToWord(cm, bigWord, -1, "end");}, + "Shift-E": function(cm) {moveToWord(cm, bigWord, 1, "end");}, + "Shift-W": function(cm) {moveToWord(cm, bigWord, 1, "start");}, + "U": "undo", "Ctrl-R": "redo", "Shift-4": "goLineEnd"}, + function(key, cmd) { map[key] = countTimes(cmd); }); + + CodeMirror.keyMap["vim-prefix-g"] = { + "E": countTimes(function(cm) { moveToWord(cm, word, -1, "start");}), + "Shift-E": countTimes(function(cm) { moveToWord(cm, bigWord, -1, "start");}), + auto: "vim", catchall: function(cm) {/*ignore*/} + }; + + CodeMirror.keyMap["vim-insert"] = { + "Esc": function(cm) {cm.setOption("keyMap", "vim");}, + fallthrough: ["default"] + }; +})(); -- cgit v1.2.3