emacs mode

This commit is contained in:
nightwing 2012-04-29 23:29:35 +04:00
commit b1c5129e5a
5 changed files with 331 additions and 109 deletions

View file

@ -20,6 +20,7 @@
* *
* Contributor(s): * Contributor(s):
* Julian Viereck (julian.viereck@gmail.com) * Julian Viereck (julian.viereck@gmail.com)
* Harutyun Amirjanyan (harutyun@c9.io)
* *
* Alternatively, the contents of this file may be used under the terms of * 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 * either the GNU General Public License Version 2 or later (the "GPL"), or
@ -38,113 +39,329 @@
define(function(require, exports, module) { define(function(require, exports, module) {
"use strict"; "use strict";
var StateHandler = require("./state_handler").StateHandler; var dom = require("../lib/dom");
var matchCharacterOnly = require("./state_handler").matchCharacterOnly;
var emacsState = { var screenToTextBlockCoordinates = function(pageX, pageY) {
start: [ var canvasPos = this.scroller.getBoundingClientRect();
{
key: "ctrl-x", var col = Math.floor(
then: "c-x" (pageX + this.scrollLeft - canvasPos.left - this.$padding - dom.getPageScrollLeft()) / this.characterWidth
}, );
{ var row = Math.floor(
regex: [ "(?:command-([0-9]*))*", "(down|ctrl-n)" ], (pageY + this.scrollTop - canvasPos.top - dom.getPageScrollTop()) / this.lineHeight
exec: "golinedown", );
params: [
{ return this.session.screenToDocumentPosition(row, col);
name: "times", };
match: 1,
type: "number", var HashHandler = require("./hash_handler").HashHandler;
defaultValue: 1 exports.handler = new HashHandler();
}
] var initialized = false;
}, exports.handler.attach = function(editor) {
{ if (!initialized) {
regex: [ "(?:command-([0-9]*))*", "(right|ctrl-f)" ], initialized = true;
exec: "gotoright", dom.importCssString('\
params: [ .emacs-mode .ace_cursor{\
{ border: 2px rgba(50,250,50,0.8) solid!important;\
name: "times", -moz-box-sizing: border-box!important;\
match: 1, box-sizing: border-box!important;\
type: "number", background-color: rgba(0,250,0,0.9);\
defaultValue: 1 opacity: 0.5;\
} }\
] .emacs-mode .ace_cursor.ace_hidden{\
}, opacity: 1;\
{ background-color: transparent;\
regex: [ "(?:command-([0-9]*))*", "(up|ctrl-p)" ], }\
exec: "golineup", .emacs-mode .ace_cursor.ace_overwrite {\
params: [ opacity: 1;\
{ background-color: transparent;\
name: "times", border-width: 0 0 2px 2px !important;\
match: 1, }\
type: "number", .emacs-mode .ace_text-layer {\
defaultValue: 1 z-index: 4\
} }\
] .emacs-mode .ace_cursor-layer {\
}, z-index: 2\
{ }', 'emacsMode'
regex: [ "(?:command-([0-9]*))*", "(left|ctrl-b)" ], );
exec: "gotoleft", }
params: [
{ editor.renderer.screenToTextCoordinates = screenToTextBlockCoordinates;
name: "times", editor.setStyle("emacs-mode");
match: 1, };
type: "number",
defaultValue: 1 exports.handler.detach = function(editor) {
} delete editor.renderer.screenToTextCoordinates;
] editor.unsetStyle("emacs-mode");
}, };
{
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]+)*" ], var keys = require("../lib/keys").KEY_MODS;
match: matchCharacterOnly, var eMods = {
exec: "inserttext", C: "ctrl", S: "shift", M: "alt"
params: [ };
{ ["S-C-M", "S-C", "S-M", "C-M", "S", "C", "M"].forEach(function(c) {
name: "times", var hashId = 0;
match: 1, c.split("-").forEach(function(c){
type: "number", hashId = hashId | keys[eMods[c]];
defaultValue: "1" });
}, eMods[hashId] = c.toLowerCase() + "-";
{ });
name: "text",
match: 2 exports.handler.bindKey = function(key, command) {
} if (!key)
] return;
},
{ var ckb = this.commmandKeyBinding;
comment: "This binding matches numbers as long as there is no meta_number in the buffer.", key.split("|").forEach(function(keyPart) {
regex: [ "(command-[0-9]*)*", "([0-9]+)" ], keyPart = keyPart.toLowerCase();
match: matchCharacterOnly, ckb[keyPart] = command;
disallowMatches: [ 1 ], keyPart = keyPart.split(" ")[0];
exec: "inserttext", if (!ckb[keyPart])
params: [ ckb[keyPart] = "null";
{ }, this);
name: "text", };
match: 2,
type: "text"
} exports.handler.handleKeyboard = function(data, hashId, key, keyCode) {
] if (hashId == -1) {
}, if (data.count) {
{ var str = Array(data.count + 1).join(key);
regex: [ "command-([0-9]*)", "(command-[0-9]|[0-9])" ], data.count = null;
comment: "Stops execution if the regex /meta_[0-9]+/ matches to avoid resetting the buffer." return {command: "insertstring", args: str};
} }
], }
"c-x": [
{ if (key == "\x00")
key: "ctrl-g", return;
then: "start"
}, var modifier = eMods[hashId];
{ if (modifier == "c-" || data.universalArgument) {
key: "ctrl-s", var count = parseInt(key[key.length - 1]);
exec: "save", if (count) {
then: "start" 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);
}); });

View file

@ -127,7 +127,7 @@ function HashHandler(config, platform) {
this.bindKey(key, command); this.bindKey(key, command);
}; };
this.parseKeys = function(keys, val) { this.parseKeys = function(keys) {
var key; var key;
var hashId = 0; var hashId = 0;
var parts = keys.toLowerCase().trim().split(/\s*\-\s*/); var parts = keys.toLowerCase().trim().split(/\s*\-\s*/);
@ -139,6 +139,11 @@ function HashHandler(config, platform) {
key = parts[i] || "-"; //when empty, the splitSafe removed a '-' key = parts[i] || "-"; //when empty, the splitSafe removed a '-'
} }
if (parts[0] == "text" && parts.length == 2) {
hashId = -1;
key = parts[1];
}
return { return {
key: key, key: key,
hashId: hashId hashId: hashId

View file

@ -117,7 +117,7 @@ var KeyBinding = function(editor) {
if (toExecute.command != "null") if (toExecute.command != "null")
success = commands.exec(toExecute.command, this.$editor, toExecute.args, e); success = commands.exec(toExecute.command, this.$editor, toExecute.args, e);
else else
success = toExecute.stopEvent == true; success = toExecute.passEvent != true;
if (success && e) if (success && e)
event.stopEvent(e); event.stopEvent(e);

View file

@ -86,7 +86,7 @@ exports.handler = {
} }; } };
} // wait for input } // wait for input
else if (key.length == 1 && (hashId == 0 || hashId == 4)) { //no modifier || shift else if (key.length == 1 && (hashId == 0 || hashId == 4)) { //no modifier || shift
return {command: "null", stopEvent: false}; return {command: "null"};
} else if (key == 'esc') { } else if (key == 'esc') {
return {command: coreCommands.stop}; return {command: coreCommands.stop};
} }

View file

@ -1,7 +1,7 @@
define(function(require, exports, module) { define(function(require, exports, module) {
var registers = require("../registers"); var registers = require("../registers");
var dom = require("ace/lib/dom"); var dom = require("../../../lib/dom");
dom.importCssString('.insert-mode. ace_cursor{\ dom.importCssString('.insert-mode. ace_cursor{\
border-left: 2px solid #333333;\ border-left: 2px solid #333333;\
}\ }\