update emacs mode

This commit is contained in:
nightwing 2013-03-09 18:55:01 +04:00
commit 8634f2661d
4 changed files with 42 additions and 29 deletions

View file

@ -92,6 +92,12 @@ var cmdLine = new layout.singleLineEditor(consoleEl);
cmdLine.editor = env.editor; cmdLine.editor = env.editor;
env.editor.cmdLine = cmdLine; env.editor.cmdLine = cmdLine;
env.editor.showCommandLine = function(val) {
this.cmdLine.focus();
if (typeof val == "string")
this.cmdLine.setValue(val, 1);
};
/** /**
* This demonstrates how you can define commands and bind shortcuts to them. * This demonstrates how you can define commands and bind shortcuts to them.
*/ */

View file

@ -103,7 +103,9 @@ var Editor = function(renderer, session) {
* *
**/ **/
this.setKeyboardHandler = function(keyboardHandler) { this.setKeyboardHandler = function(keyboardHandler) {
if (typeof keyboardHandler == "string" && keyboardHandler) { if (!keyboardHandler) {
this.keyBinding.setKeyboardHandler(null);
} else if (typeof keyboardHandler == "string") {
this.$keybindingId = keyboardHandler; this.$keybindingId = keyboardHandler;
var _self = this; var _self = this;
config.loadModule(["keybinding", keyboardHandler], function(module) { config.loadModule(["keybinding", keyboardHandler], function(module) {
@ -1279,16 +1281,15 @@ var Editor = function(renderer, session) {
_numberRx.lastIndex = 0 _numberRx.lastIndex = 0
var s = this.session.getLine(row) var s = this.session.getLine(row)
while(_numberRx.lastIndex < column - 1 ){ while (_numberRx.lastIndex < column) {
var m = _numberRx.exec(s) var m = _numberRx.exec(s)
if(m.index <= column && m.index+m[0].length >= column){ if(m.index <= column && m.index+m[0].length >= column){
var number = { var number = {
value: m[0], value: m[0],
start: m.index, start: m.index,
end: m.index+m[0].length end: m.index+m[0].length
} }
return number return number;
} }
} }
return null; return null;
@ -2228,4 +2229,4 @@ config.defineOptions(Editor.prototype, "editor", {
}); });
exports.Editor = Editor; exports.Editor = Editor;
}); });

View file

@ -91,28 +91,29 @@ exports.handler.attach = function(editor) {
editor.session.$emacsMark = null; editor.session.$emacsMark = null;
exports.markMode = function() { editor.emacsMarkMode = function() {
return editor.session.$emacsMark; return this.session.$emacsMark;
} }
exports.setMarkMode = function(p) { editor.setEmacsMarkMode = function(p) {
editor.session.$emacsMark = p; this.session.$emacsMark = p;
} }
editor.on("click",$resetMarkMode); editor.on("click", $resetMarkMode);
editor.on("changeSession",$kbSessionChange); editor.on("changeSession",$kbSessionChange);
editor.renderer.screenToTextCoordinates = screenToTextBlockCoordinates; editor.renderer.screenToTextCoordinates = screenToTextBlockCoordinates;
editor.setStyle("emacs-mode"); editor.setStyle("emacs-mode");
editor.commands.addCommands(commands); editor.commands.addCommands(commands);
exports.handler.platform = editor.commands.platform; exports.handler.platform = editor.commands.platform;
editor.$emacsModeHandler = this;
}; };
exports.handler.detach = function(editor) { exports.handler.detach = function(editor) {
delete editor.renderer.screenToTextCoordinates; delete editor.renderer.screenToTextCoordinates;
editor.session.$selectLongWords = $formerLongWords; editor.session.$selectLongWords = $formerLongWords;
editor.session.$useEmacsStyleLineStart = $formerLineStart; editor.session.$useEmacsStyleLineStart = $formerLineStart;
editor.removeEventListener("click",$resetMarkMode); editor.removeEventListener("click", $resetMarkMode);
editor.removeEventListener("changeSession",$kbSessionChange); editor.removeEventListener("changeSession", $kbSessionChange);
editor.unsetStyle("emacs-mode"); editor.unsetStyle("emacs-mode");
editor.commands.removeCommands(commands); editor.commands.removeCommands(commands);
}; };
@ -166,9 +167,10 @@ exports.handler.bindKey = function(key, command) {
exports.handler.handleKeyboard = function(data, hashId, key, keyCode) { exports.handler.handleKeyboard = function(data, hashId, key, keyCode) {
var editor = data.editor;
// insertstring data.count times // insertstring data.count times
if (hashId == -1) { if (hashId == -1) {
exports.setMarkMode(null); editor.setEmacsMarkMode(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;
@ -223,7 +225,7 @@ exports.handler.handleKeyboard = function(data, hashId, key, keyCode) {
args = command.args; args = command.args;
if (command.command) command = command.command; if (command.command) command = command.command;
if (command === "goorselect") { if (command === "goorselect") {
command = exports.markMode() ? args[1] : args[0]; command = editor.emacsMarkMode() ? args[1] : args[0];
args = null; args = null;
} }
} }
@ -232,9 +234,9 @@ exports.handler.handleKeyboard = function(data, hashId, key, keyCode) {
if (command === "insertstring" || if (command === "insertstring" ||
command === "splitline" || command === "splitline" ||
command === "togglecomment") { command === "togglecomment") {
exports.setMarkMode(null); editor.setEmacsMarkMode(null);
} }
command = this.commands[command] || data.editor.commands.commands[command]; command = this.commands[command] || editor.commands.commands[command];
} }
if (!command.readonly && !command.isYank) if (!command.readonly && !command.isYank)
@ -330,10 +332,10 @@ exports.emacsKeys = {
"C-/|C-x u|S-C--|C-z": "undo", "C-/|C-x u|S-C--|C-z": "undo",
"S-C-/|S-C-x u|C--|S-C-z": "redo", //infinite undo? "S-C-/|S-C-x u|C--|S-C-z": "redo", //infinite undo?
// vertical editing // vertical editing
"C-x r": "selectRectangularRegion" "C-x r": "selectRectangularRegion",
"M-x": {command: "focusCommandLine", args: "M-x "}
// todo // todo
// "M-x" "C-x C-t" "M-t" "M-c" "F11" "C-M- "M-q" // "C-x C-t" "M-t" "M-c" "F11" "C-M- "M-q"
}; };
@ -365,21 +367,20 @@ exports.handler.addCommands({
// "goto" commands become "select" commands. // "goto" commands become "select" commands.
// Any insertion or mouse click resets mark-mode. // Any insertion or mouse click resets mark-mode.
// setMark twice in a row at the same place resets markmode // setMark twice in a row at the same place resets markmode
var markMode = exports.markMode(); var markMode = editor.emacsMarkMode();
if (markMode) { if (markMode) {
var cp = editor.getCursorPosition(); var cp = editor.getCursorPosition();
if (editor.selection.isEmpty() && if (editor.selection.isEmpty() &&
markMode.row == cp.row && markMode.column == cp.column) { markMode.row == cp.row && markMode.column == cp.column) {
exports.setMarkMode(null); editor.setEmacsMarkMode(null);
// console.log("Mark mode off"); // console.log("Mark mode off");
return; return;
} }
} }
// turn on mark mode // turn on mark mode
markMode = editor.getCursorPosition(); markMode = editor.getCursorPosition();
exports.setMarkMode(markMode); editor.setEmacsMarkMode(markMode);
editor.selection.setSelectionAnchor(markMode.row, markMode.column); editor.selection.setSelectionAnchor(markMode.row, markMode.column);
}, },
exchangePointAndMark: { exchangePointAndMark: {
exec: function(editor) { exec: function(editor) {
@ -407,7 +408,7 @@ exports.handler.addCommands({
multiselectAction: "forEach" multiselectAction: "forEach"
}, },
killLine: function(editor) { killLine: function(editor) {
exports.setMarkMode(null); editor.setEmacsMarkMode(null);
var pos = editor.getCursorPosition(); var pos = editor.getCursorPosition();
if (pos.column == 0 && if (pos.column == 0 &&
editor.session.doc.getLine(pos.row).length == 0) { editor.session.doc.getLine(pos.row).length == 0) {
@ -448,7 +449,11 @@ exports.handler.addCommands({
}, },
keyboardQuit: function(editor) { keyboardQuit: function(editor) {
editor.selection.clearSelection(); editor.selection.clearSelection();
exports.setMarkMode(null); editor.setEmacsMarkMode(null);
},
focusCommandLine: function(editor, arg) {
if (editor.showCommandLine)
editor.showCommandLine(arg);
} }
}); });

View file

@ -50,11 +50,12 @@ var KeyBinding = function(editor) {
}; };
this.setKeyboardHandler = function(kb) { this.setKeyboardHandler = function(kb) {
if (this.$handlers[this.$handlers.length - 1] == kb) var h = this.$handlers;
if (h[h.length - 1] == kb)
return; return;
while (this.$handlers[1]) while (h[h.length - 1] && h[h.length - 1] != this.defaultHandler)
this.removeKeyboardHandler(this.$handlers[1]); this.removeKeyboardHandler(h[h.length - 1]);
this.addKeyboardHandler(kb, 1); this.addKeyboardHandler(kb, 1);
}; };