merge emacs fixes

This commit is contained in:
Chester Wood 2013-01-25 18:18:47 -07:00 • committed by nightwing
commit 984b4ea2e2

View file

@ -50,9 +50,24 @@ var HashHandler = require("./hash_handler").HashHandler;
exports.handler = new HashHandler(); exports.handler = new HashHandler();
var initialized = false; var initialized = false;
// When mark is set, keyboard cursor movement commands become selection
// modification commands. This is a little different than emacs. In
// emacs, keyboard cursor movement always sets mark, but it does not
// highlight the region unless mark has been otherwise explicitly set
// and transient-mark-mode is on.
// In ACE, there is no concept of a region that is not highlighted,
// so we just work with highlighted area === region. It would probably be
// confusing to most users anyway if a cut command, say, were to delete an
// area that was not highlighted.
var markMode;
exports.handler.attach = function(editor) { exports.handler.attach = function(editor) {
if (!initialized) { if (!initialized) {
initialized = true; initialized = true;
// in emacs, gotowordleft/right should not count a space as a word..
editor.session.$selectLongWords = true;
dom.importCssString('\ dom.importCssString('\
.emacs-mode .ace_cursor{\ .emacs-mode .ace_cursor{\
border: 2px rgba(50,250,50,0.8) solid!important;\ border: 2px rgba(50,250,50,0.8) solid!important;\
@ -79,6 +94,9 @@ exports.handler.attach = function(editor) {
}', 'emacsMode' }', 'emacsMode'
); );
} }
markMode = false;
editor.on("click",$resetMarkMode);
editor.renderer.screenToTextCoordinates = screenToTextBlockCoordinates; editor.renderer.screenToTextCoordinates = screenToTextBlockCoordinates;
editor.setStyle("emacs-mode"); editor.setStyle("emacs-mode");
@ -87,8 +105,12 @@ exports.handler.attach = function(editor) {
exports.handler.detach = function(editor) { exports.handler.detach = function(editor) {
delete editor.renderer.screenToTextCoordinates; delete editor.renderer.screenToTextCoordinates;
editor.unsetStyle("emacs-mode"); editor.unsetStyle("emacs-mode");
editor.removeEventListener("click",$resetMarkMode);
}; };
var $resetMarkMode = function(e) {
markMode = null;
}
var keys = require("../lib/keys").KEY_MODS; var keys = require("../lib/keys").KEY_MODS;
var eMods = { var eMods = {
@ -119,6 +141,7 @@ exports.handler.bindKey = function(key, command) {
exports.handler.handleKeyboard = function(data, hashId, key, keyCode) { exports.handler.handleKeyboard = function(data, hashId, key, keyCode) {
if (hashId == -1) { if (hashId == -1) {
markMode = null;
if (data.count) { if (data.count) {
var str = Array(data.count + 1).join(key); var str = Array(data.count + 1).join(key);
data.count = null; data.count = null;
@ -162,9 +185,21 @@ exports.handler.handleKeyboard = function(data, hashId, key, keyCode) {
if (typeof command != "string") { if (typeof command != "string") {
var args = command.args; var args = command.args;
command = command.command; command = command.command;
if (command == "goorselect") {
command = args[0];
if (markMode) {
command = args[1];
}
args = null;
}
} }
if (typeof command == "string") { if (typeof command == "string") {
if (command == "insertstring" ||
command == "splitline" ||
command == "togglecomment") {
markMode = null;
}
command = this.commands[command] || data.editor.commands.commands[command]; command = this.commands[command] || data.editor.commands.commands[command];
} }
@ -190,16 +225,16 @@ exports.handler.handleKeyboard = function(data, hashId, key, keyCode) {
exports.emacsKeys = { exports.emacsKeys = {
// movement // movement
"Up|C-p" : "golineup", "Up|C-p" : {command: "goorselect", args: ["golineup","selectup"]},
"Down|C-n" : "golinedown", "Down|C-n" : {command: "goorselect", args: ["golinedown","selectdown"]},
"Left|C-b" : "gotoleft", "Left|C-b" : {command: "goorselect", args: ["gotoleft","selectleft"]},
"Right|C-f" : "gotoright", "Right|C-f" : {command: "goorselect", args: ["gotoright","selectright"]},
"C-Left|M-b" : "gotowordleft", "C-Left|M-b" : {command: "goorselect", args: ["gotowordleft","selectwordleft"]},
"C-Right|M-f" : "gotowordright", "C-Right|M-f" : {command: "goorselect", args: ["gotowordright","selectwordright"]},
"Home|C-a" : "gotolinestart", "Home|C-a" : {command: "goorselect", args: ["gotolinestart","selecttolinestart"]},
"End|C-e" : "gotolineend", "End|C-e" : {command: "goorselect", args: ["gotolineend","selecttolineend"]},
"C-Home|S-M-,": "gotostart", "C-Home|S-M-,": {command: "goorselect", args: ["gotostart","selecttostart"]},
"C-End|S-M-." : "gotoend", "C-End|S-M-." : {command: "goorselect", args: ["gotoend","selecttoend"]},
// selection // selection
"S-Up|S-C-p" : "selectup", "S-Up|S-C-p" : "selectup",
@ -219,10 +254,10 @@ exports.emacsKeys = {
"C-x C-p": "selectall", "C-x C-p": "selectall",
// todo fix these // todo fix these
"C-Down": "gotopagedown", "C-Down": {command: "goorselect", args: ["gotopagedown","selectpagedown"]},
"C-Up": "gotopageup", "C-Up": {command: "goorselect", args: ["gotopageup","selectpageup"]},
"PageDown|C-v": "gotopagedown", "PageDown|C-v": {command: "goorselect", args: ["gotopagedown","selectpagedown"]},
"PageUp|M-v": "gotopageup", "PageUp|M-v": {command: "goorselect", args: ["gotopageup","selectpageup"]},
"S-C-Down": "selectpagedown", "S-C-Down": "selectpagedown",
"S-C-Up": "selectpageup", "S-C-Up": "selectpageup",
"C-s": "findnext", "C-s": "findnext",
@ -247,16 +282,15 @@ exports.emacsKeys = {
"C-w": "killRegion", "C-w": "killRegion",
"M-w": "killRingSave", "M-w": "killRingSave",
"C-Space": "setMark", "C-Space": "setMark",
"C-x C-x": "exchangePointAndMark", "C-x C-x": "exchangePointAndMark",
"C-t": "transposeletters", "C-t": "transposeletters",
"M-u": "touppercase", // Doesn't work
"M-u": "touppercase",
"M-l": "tolowercase", "M-l": "tolowercase",
"M-/": "autocomplete", "M-/": "autocomplete", // Doesn't work
"C-u": "universalArgument", "C-u": "universalArgument",
"M-;": "togglecomment", "M-;": "togglecomment",
"C-/|C-x u|S-C--|C-z": "undo", "C-/|C-x u|S-C--|C-z": "undo",
@ -290,10 +324,32 @@ exports.handler.addCommands({
editor.multiSelect.toggleBlockSelection(); editor.multiSelect.toggleBlockSelection();
}, },
setMark: function() { setMark: function() {
// Sets mark-mode and clears current selection.
// When in mark-mode, "goto" commands become "select" commands.
// Any insertion or mouse click resets mark-mode.
if (markMode) {
cp = editor.getCursorPosition();
if (editor.selection.isEmpty() &&
markMode.row == cp.row && markMode.column == cp.column) {
// setMark twice in a row at the same place
// resets markmode
markMode = null;
console.log("Mark mode off");
return;
}
}
// turn on mark mode
markMode = editor.getCursorPosition();
editor.selection.setSelectionAnchor(markMode.row, markMode.column);
}, },
exchangePointAndMark: { exchangePointAndMark: {
exec: function(editor) { exec: function(editor) {
var range = editor.selection.getRange(); var range = editor.selection.getRange();
editor.selection.setSelectionRange(range, !editor.selection.isBackwards()); editor.selection.setSelectionRange(range, !editor.selection.isBackwards());
}, },
readonly: true, readonly: true,
@ -317,7 +373,21 @@ exports.handler.addCommands({
multiselectAction: "forEach" multiselectAction: "forEach"
}, },
killLine: function(editor) { killLine: function(editor) {
editor.selection.selectLine(); markMode = null;
if (editor.getCursorPosition().column == 0 &&
editor.selection.isEmpty) {
// If an already empty line is killed, remove
// the line entirely
editor.selection.selectLine();
} else {
// otherwise just remove from the current cursor position
// to the end (but don't delete the selection if it's before
// the cursor)
editor.clearSelection();
editor.selection.selectLineEnd();
}
var range = editor.getSelectionRange(); var range = editor.getSelectionRange();
var text = editor.session.getTextRange(range); var text = editor.session.getTextRange(range);
exports.killRing.add(text); exports.killRing.add(text);