From b1c5129e5a3a804aa224c73b2cbde34b2deb856a Mon Sep 17 00:00:00 2001 From: nightwing Date: Sun, 29 Apr 2012 23:29:35 +0400 Subject: [PATCH] emacs mode --- lib/ace/keyboard/emacs.js | 427 ++++++++++++++++++++++-------- lib/ace/keyboard/hash_handler.js | 7 +- lib/ace/keyboard/keybinding.js | 2 +- lib/ace/keyboard/vim.js | 2 +- lib/ace/keyboard/vim/maps/util.js | 2 +- 5 files changed, 331 insertions(+), 109 deletions(-) diff --git a/lib/ace/keyboard/emacs.js b/lib/ace/keyboard/emacs.js index 5f9fbe8d..e2c65e9d 100644 --- a/lib/ace/keyboard/emacs.js +++ b/lib/ace/keyboard/emacs.js @@ -20,6 +20,7 @@ * * Contributor(s): * Julian Viereck (julian.viereck@gmail.com) + * Harutyun Amirjanyan (harutyun@c9.io) * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or @@ -38,113 +39,329 @@ define(function(require, exports, module) { "use strict"; -var StateHandler = require("./state_handler").StateHandler; -var matchCharacterOnly = require("./state_handler").matchCharacterOnly; +var dom = require("../lib/dom"); -var emacsState = { - start: [ - { - key: "ctrl-x", - then: "c-x" - }, - { - regex: [ "(?:command-([0-9]*))*", "(down|ctrl-n)" ], - exec: "golinedown", - params: [ - { - name: "times", - match: 1, - type: "number", - defaultValue: 1 - } - ] - }, - { - regex: [ "(?:command-([0-9]*))*", "(right|ctrl-f)" ], - exec: "gotoright", - params: [ - { - name: "times", - match: 1, - type: "number", - defaultValue: 1 - } - ] - }, - { - regex: [ "(?:command-([0-9]*))*", "(up|ctrl-p)" ], - exec: "golineup", - params: [ - { - name: "times", - match: 1, - type: "number", - defaultValue: 1 - } - ] - }, - { - regex: [ "(?:command-([0-9]*))*", "(left|ctrl-b)" ], - exec: "gotoleft", - params: [ - { - name: "times", - match: 1, - type: "number", - defaultValue: 1 - } - ] - }, - { - comment: "This binding matches all printable characters except numbers as long as they are no numbers and print them n times.", - regex: [ "(?:command-([0-9]*))", "([^0-9]+)*" ], - match: matchCharacterOnly, - exec: "inserttext", - params: [ - { - name: "times", - match: 1, - type: "number", - defaultValue: "1" - }, - { - name: "text", - match: 2 - } - ] - }, - { - comment: "This binding matches numbers as long as there is no meta_number in the buffer.", - regex: [ "(command-[0-9]*)*", "([0-9]+)" ], - match: matchCharacterOnly, - disallowMatches: [ 1 ], - exec: "inserttext", - params: [ - { - name: "text", - match: 2, - type: "text" - } - ] - }, - { - regex: [ "command-([0-9]*)", "(command-[0-9]|[0-9])" ], - comment: "Stops execution if the regex /meta_[0-9]+/ matches to avoid resetting the buffer." - } - ], - "c-x": [ - { - key: "ctrl-g", - then: "start" - }, - { - key: "ctrl-s", - exec: "save", - then: "start" - } - ] +var screenToTextBlockCoordinates = function(pageX, pageY) { + var canvasPos = this.scroller.getBoundingClientRect(); + + var col = Math.floor( + (pageX + this.scrollLeft - canvasPos.left - this.$padding - dom.getPageScrollLeft()) / this.characterWidth + ); + var row = Math.floor( + (pageY + this.scrollTop - canvasPos.top - dom.getPageScrollTop()) / this.lineHeight + ); + + return this.session.screenToDocumentPosition(row, col); +}; + +var HashHandler = require("./hash_handler").HashHandler; +exports.handler = new HashHandler(); + +var initialized = false; +exports.handler.attach = function(editor) { + if (!initialized) { + initialized = true; + dom.importCssString('\ + .emacs-mode .ace_cursor{\ + border: 2px rgba(50,250,50,0.8) solid!important;\ + -moz-box-sizing: border-box!important;\ + box-sizing: border-box!important;\ + background-color: rgba(0,250,0,0.9);\ + opacity: 0.5;\ + }\ + .emacs-mode .ace_cursor.ace_hidden{\ + opacity: 1;\ + background-color: transparent;\ + }\ + .emacs-mode .ace_cursor.ace_overwrite {\ + opacity: 1;\ + background-color: transparent;\ + border-width: 0 0 2px 2px !important;\ + }\ + .emacs-mode .ace_text-layer {\ + z-index: 4\ + }\ + .emacs-mode .ace_cursor-layer {\ + z-index: 2\ + }', 'emacsMode' + ); + } + + editor.renderer.screenToTextCoordinates = screenToTextBlockCoordinates; + editor.setStyle("emacs-mode"); +}; + +exports.handler.detach = function(editor) { + delete editor.renderer.screenToTextCoordinates; + editor.unsetStyle("emacs-mode"); +}; + + +var keys = require("../lib/keys").KEY_MODS; +var eMods = { + C: "ctrl", S: "shift", M: "alt" +}; +["S-C-M", "S-C", "S-M", "C-M", "S", "C", "M"].forEach(function(c) { + var hashId = 0; + c.split("-").forEach(function(c){ + hashId = hashId | keys[eMods[c]]; + }); + eMods[hashId] = c.toLowerCase() + "-"; +}); + +exports.handler.bindKey = function(key, command) { + if (!key) + return; + + var ckb = this.commmandKeyBinding; + key.split("|").forEach(function(keyPart) { + keyPart = keyPart.toLowerCase(); + ckb[keyPart] = command; + keyPart = keyPart.split(" ")[0]; + if (!ckb[keyPart]) + ckb[keyPart] = "null"; + }, this); +}; + + +exports.handler.handleKeyboard = function(data, hashId, key, keyCode) { + if (hashId == -1) { + if (data.count) { + var str = Array(data.count + 1).join(key); + data.count = null; + return {command: "insertstring", args: str}; + } + } + + if (key == "\x00") + return; + + var modifier = eMods[hashId]; + if (modifier == "c-" || data.universalArgument) { + var count = parseInt(key[key.length - 1]); + if (count) { + data.count = count; + return {command: "null"}; + } + } + data.universalArgument = false; + + if (modifier) + key = modifier + key; + + if (data.keyChain) + key = data.keyChain += " " + key; + + var command = this.commmandKeyBinding[key]; + data.keyChain = command == "null" ? key : ""; + + if (!command) + return; + + if (command == "null") + return {command: "null"}; + + if (command == "universalArgument") { + data.universalArgument = true; + return {command: "null"}; + } + + if (typeof command != "string") { + var args = command.args; + command = command.command; + } + + if (typeof command == "string") { + command = this.commands[command] || data.editor.commands.commands[command]; + } + + if (!command.readonly && !command.isYank) + data.lastCommand = null; + + if (data.count) { + var count = data.count; + data.count = 0; + return { + args: args, + command: { + exec: function(editor, args) { + for (var i = 0; i < count; i++) + command.exec(editor, args); + } + } + }; + } + + return {command: command, args: args}; +}; + +exports.emacsKeys = { + // movement + "Up|C-p" : "golineup", + "Down|C-n" : "golinedown", + "Left|C-b" : "gotoleft", + "Right|C-f" : "gotoright", + "C-Left|M-b" : "gotowordleft", + "C-Right|M-f" : "gotowordright", + "Home|C-a" : "gotolinestart", + "End|C-e" : "gotolineend", + "C-Home|S-M-,": "gotostart", + "C-End|S-M-." : "gotoend", + + // selection + "S-Up|S-C-p" : "selectup", + "S-Down|S-C-n" : "selectdown", + "S-Left|S-C-b" : "selectleft", + "S-Right|S-C-f" : "selectright", + "S-C-Left|S-M-b" : "selectwordleft", + "S-C-Right|S-M-f" : "selectwordright", + "S-Home|S-C-a" : "selecttolinestart", + "S-End|S-C-e" : "selecttolineend", + "S-C-Home" : "selecttostart", + "S-C-End" : "selecttoend", + + "C-l|M-s" : "centerselection", + "M-g": "gotoline", + "C-x C-p": "selectall", + + // todo fix these + "C-Down": "gotopagedown", + "C-Up": "gotopageup", + "PageDown|C-v": "gotopagedown", + "PageUp|M-v": "gotopageup", + "S-C-Down": "selectpagedown", + "S-C-Up": "selectpageup", + "C-s": "findnext", + "C-r": "findprevious", + "M-C-s": "findnext", + "M-C-r": "findprevious", + "S-M-5": "replace", + + // basic editing + "Backspace": "backspace", + "Delete|C-d": "del", + "Return|C-m": {command: "insertstring", args: "\n"}, // "newline" + "C-o": "splitline", + + "M-d|C-Delete": {command: "killWord", args: "right"}, + "C-Backspace|M-Backspace|M-Delete": {command: "killWord", args: "left"}, + "C-k": "killLine", + + "C-y|S-Delete": "yank", + "M-y": "yankRotate", + "C-g": "keyboardQuit", + + "C-w": "killRegion", + "M-w": "killRingSave", + + "C-Space": "setMark", + "C-x C-x": "exchangePointAndMark", + + "C-t": "transposeletters", + + "M-u": "touppercase", + "M-l": "tolowercase", + "M-/": "autocomplete", + "C-u": "universalArgument", + "M-;": "togglecomment", + + "C-/|C-x u|S-C--|C-z": "undo", + "S-C-/|S-C-x u|C--|S-C-z": "redo", //infinite undo? + // vertical editing + "C-x r": "selectRectangularRegion" + + // todo + // "M-x" "C-x C-t" "M-t" "M-c" "F11" "C-M- "M-q" +}; + + +exports.handler.bindKeys(exports.emacsKeys); + +exports.handler.addCommands({ + selectRectangularRegion: function(editor) { + editor.multiSelect.toggleBlockSelection(); + }, + setMark: function() { + }, + exchangePointAndMark: { + exec: function(editor) { + var range = editor.selection.getRange(); + editor.selection.setSelectionRange(range, !editor.selection.isBackwards()); + }, + readonly: true, + multiselectAction: "forEach" + }, + killWord: { + exec: function(editor, dir) { + editor.clearSelection(); + if (dir == "left") + editor.selection.selectWordLeft(); + else + editor.selection.selectWordRight(); + + var range = editor.getSelectionRange(); + var text = editor.session.getTextRange(range); + exports.killRing.add(text); + + editor.session.remove(range); + editor.clearSelection(); + }, + multiselectAction: "forEach" + }, + killLine: function(editor) { + editor.selection.selectLine(); + var range = editor.getSelectionRange(); + var text = editor.session.getTextRange(range); + exports.killRing.add(text); + + editor.session.remove(range); + editor.clearSelection(); + }, + yank: function(editor) { + editor.onPaste(exports.killRing.get()); + editor.keyBinding.$data.lastCommand = "yank"; + }, + yankRotate: function(editor) { + if (editor.keyBinding.$data.lastCommand != "yank") + return; + + editor.undo(); + editor.onPaste(exports.killRing.rotate()); + editor.keyBinding.$data.lastCommand = "yank"; + }, + killRegion: function(editor) { + exports.killRing.add(editor.getCopyText()); + editor.cut(); + }, + killRingSave: function(editor) { + exports.killRing.add(editor.getCopyText()); + } +}); + +var commands = exports.handler.commands; +commands.yank.isYank = true; +commands.yankRotate.isYank = true; + +exports.killRing = { + $data: [], + add: function(str) { + str && this.$data.push(str); + if (this.$data.length > 30) + this.$data.shift(); + }, + get: function() { + return this.$data[this.$data.length - 1] || ""; + }, + pop: function() { + if (this.$data.length > 1) + this.$data.pop(); + return this.get(); + }, + rotate: function() { + this.$data.unshift(this.$data.pop()); + return this.get(); + } }; -exports.handler = new StateHandler(emacsState); }); diff --git a/lib/ace/keyboard/hash_handler.js b/lib/ace/keyboard/hash_handler.js index 53a9c9c4..0db64573 100644 --- a/lib/ace/keyboard/hash_handler.js +++ b/lib/ace/keyboard/hash_handler.js @@ -127,7 +127,7 @@ function HashHandler(config, platform) { this.bindKey(key, command); }; - this.parseKeys = function(keys, val) { + this.parseKeys = function(keys) { var key; var hashId = 0; var parts = keys.toLowerCase().trim().split(/\s*\-\s*/); @@ -138,6 +138,11 @@ function HashHandler(config, platform) { else key = parts[i] || "-"; //when empty, the splitSafe removed a '-' } + + if (parts[0] == "text" && parts.length == 2) { + hashId = -1; + key = parts[1]; + } return { key: key, diff --git a/lib/ace/keyboard/keybinding.js b/lib/ace/keyboard/keybinding.js index cea77f50..2c336438 100644 --- a/lib/ace/keyboard/keybinding.js +++ b/lib/ace/keyboard/keybinding.js @@ -117,7 +117,7 @@ var KeyBinding = function(editor) { if (toExecute.command != "null") success = commands.exec(toExecute.command, this.$editor, toExecute.args, e); else - success = toExecute.stopEvent == true; + success = toExecute.passEvent != true; if (success && e) event.stopEvent(e); diff --git a/lib/ace/keyboard/vim.js b/lib/ace/keyboard/vim.js index a94e9dc8..e5698913 100644 --- a/lib/ace/keyboard/vim.js +++ b/lib/ace/keyboard/vim.js @@ -86,7 +86,7 @@ exports.handler = { } }; } // wait for input else if (key.length == 1 && (hashId == 0 || hashId == 4)) { //no modifier || shift - return {command: "null", stopEvent: false}; + return {command: "null"}; } else if (key == 'esc') { return {command: coreCommands.stop}; } diff --git a/lib/ace/keyboard/vim/maps/util.js b/lib/ace/keyboard/vim/maps/util.js index 54e66681..fb79f39e 100644 --- a/lib/ace/keyboard/vim/maps/util.js +++ b/lib/ace/keyboard/vim/maps/util.js @@ -1,7 +1,7 @@ define(function(require, exports, module) { var registers = require("../registers"); -var dom = require("ace/lib/dom"); +var dom = require("../../../lib/dom"); dom.importCssString('.insert-mode. ace_cursor{\ border-left: 2px solid #333333;\ }\