Merge pull request #1262 from LivelyKernel/emacs-improvements

Emacs improvements
This commit is contained in:
Harutyun Amirjanyan 2013-02-20 21:54:13 -08:00
commit 3788e83a0c
3 changed files with 132 additions and 58 deletions

View file

@ -56,7 +56,6 @@ var $formerLineStart;
exports.handler.attach = function(editor) { exports.handler.attach = function(editor) {
if (!initialized) { if (!initialized) {
initialized = true; initialized = 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;\
@ -101,27 +100,21 @@ exports.handler.attach = function(editor) {
} }
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);
exports.handler.platform = editor.commands.platform;
}; };
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);
}; };
var $kbSessionChange = function(e) { var $kbSessionChange = function(e) {
@ -143,13 +136,15 @@ var $resetMarkMode = function(e) {
e.editor.session.$emacsMark = null; e.editor.session.$emacsMark = null;
} }
var keys = require("../lib/keys").KEY_MODS; var keys = require("../lib/keys").KEY_MODS,
var eMods = { eMods = {C: "ctrl", S: "shift", M: "alt", CMD: "command"},
C: "ctrl", S: "shift", M: "alt" combinations = ["C-S-M-CMD",
}; "S-M-CMD", "C-M-CMD", "C-S-CMD", "C-S-M",
["S-C-M", "S-C", "S-M", "C-M", "S", "C", "M"].forEach(function(c) { "M-CMD", "S-CMD", "S-M", "C-CMD", "C-M", "C-S",
"CMD", "M", "S", "C"];
combinations.forEach(function(c) {
var hashId = 0; var hashId = 0;
c.split("-").forEach(function(c){ c.split("-").forEach(function(c) {
hashId = hashId | keys[eMods[c]]; hashId = hashId | keys[eMods[c]];
}); });
eMods[hashId] = c.toLowerCase() + "-"; eMods[hashId] = c.toLowerCase() + "-";
@ -171,6 +166,7 @@ exports.handler.bindKey = function(key, command) {
exports.handler.handleKeyboard = function(data, hashId, key, keyCode) { exports.handler.handleKeyboard = function(data, hashId, key, keyCode) {
// insertstring data.count times
if (hashId == -1) { if (hashId == -1) {
exports.setMarkMode(null); exports.setMarkMode(null);
if (data.count) { if (data.count) {
@ -184,6 +180,8 @@ exports.handler.handleKeyboard = function(data, hashId, key, keyCode) {
return; return;
var modifier = eMods[hashId]; var modifier = eMods[hashId];
// CTRL + number / universalArgument for setting data.count
if (modifier == "c-" || data.universalArgument) { if (modifier == "c-" || data.universalArgument) {
var count = parseInt(key[key.length - 1]); var count = parseInt(key[key.length - 1]);
if (count) { if (count) {
@ -193,42 +191,47 @@ exports.handler.handleKeyboard = function(data, hashId, key, keyCode) {
} }
data.universalArgument = false; data.universalArgument = false;
if (modifier) // this.commandKeyBinding maps key specs like "c-p" (for CTRL + P) to
key = modifier + key; // command objects, for lookup key needs to include the modifier
if (modifier) key = modifier + key;
if (data.keyChain) // Key combos like CTRL+X H build up the data.keyChain
key = data.keyChain += " " + key; if (data.keyChain) key = data.keyChain += " " + key;
// Key combo prefixes get stored as "null" (String!) in this
// this.commmandKeyBinding. When encountered no command is invoked but we
// buld up data.keyChain
var command = this.commmandKeyBinding[key]; var command = this.commmandKeyBinding[key];
data.keyChain = command == "null" ? key : ""; data.keyChain = command == "null" ? key : "";
if (!command) // there really is no command
return; if (!command) return;
if (command == "null") // we pass b/c of key combo or universalArgument
return {command: "null"}; if (command === "null") return {command: "null"};
if (command == "universalArgument") { if (command === "universalArgument") {
data.universalArgument = true; data.universalArgument = true;
return {command: "null"}; return {command: "null"};
} }
if (typeof command != "string") { // lookup command
var args = command.args; // TODO extract special handling of markmode
command = command.command; // TODO special case command.command is really unnecessary, remove
if (command == "goorselect") { var args;
command = args[0]; if (typeof command !== "string") {
if (exports.markMode()) { args = command.args;
command = args[1]; if (command.command) command = command.command;
} if (command === "goorselect") {
command = exports.markMode() ? args[1] : args[0];
args = null; args = null;
} }
} }
if (typeof command == "string") { if (typeof command === "string") {
if (command == "insertstring" || if (command === "insertstring" ||
command == "splitline" || command === "splitline" ||
command == "togglecomment") { command === "togglecomment") {
exports.setMarkMode(null); exports.setMarkMode(null);
} }
command = this.commands[command] || data.editor.commands.commands[command]; command = this.commands[command] || data.editor.commands.commands[command];
@ -363,10 +366,8 @@ exports.handler.addCommands({
// 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 = exports.markMode();
if (markMode) { if (markMode) {
var cp = editor.getCursorPosition();
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); exports.setMarkMode(null);
@ -374,7 +375,6 @@ exports.handler.addCommands({
return; return;
} }
} }
// turn on mark mode // turn on mark mode
markMode = editor.getCursorPosition(); markMode = editor.getCursorPosition();
exports.setMarkMode(markMode); exports.setMarkMode(markMode);
@ -384,7 +384,6 @@ exports.handler.addCommands({
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,
@ -410,7 +409,6 @@ exports.handler.addCommands({
killLine: function(editor) { killLine: function(editor) {
exports.setMarkMode(null); exports.setMarkMode(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) {
// If an already empty line is killed, remove // If an already empty line is killed, remove
@ -422,7 +420,6 @@ exports.handler.addCommands({
// the cursor) // the cursor)
editor.clearSelection(); editor.clearSelection();
editor.selection.selectLineEnd(); editor.selection.selectLineEnd();
} }
var range = editor.getSelectionRange(); var range = editor.getSelectionRange();
var text = editor.session.getTextRange(range); var text = editor.session.getTextRange(range);
@ -438,7 +435,6 @@ exports.handler.addCommands({
yankRotate: function(editor) { yankRotate: function(editor) {
if (editor.keyBinding.$data.lastCommand != "yank") if (editor.keyBinding.$data.lastCommand != "yank")
return; return;
editor.undo(); editor.undo();
editor.onPaste(exports.killRing.rotate()); editor.onPaste(exports.killRing.rotate());
editor.keyBinding.$data.lastCommand = "yank"; editor.keyBinding.$data.lastCommand = "yank";
@ -449,6 +445,10 @@ exports.handler.addCommands({
}, },
killRingSave: function(editor) { killRingSave: function(editor) {
exports.killRing.add(editor.getCopyText()); exports.killRing.add(editor.getCopyText());
},
keyboardQuit: function(editor) {
editor.selection.clearSelection();
exports.setMarkMode(null);
} }
}); });

View file

@ -0,0 +1,73 @@
/* ***** BEGIN LICENSE BLOCK *****
* Distributed under the BSD license:
*
* Copyright (c) 2010, Ajax.org B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ***** END LICENSE BLOCK ***** */
if (typeof process !== "undefined") {
require("amd-loader");
}
define(function(require, exports, module) {
"use strict";
var EditSession = require("./../edit_session").EditSession,
Editor = require("./../editor").Editor,
MockRenderer = require("./../test/mockrenderer").MockRenderer,
emacs = require('./emacs'),
assert = require("./../test/assertions"),
editor;
function initEditor(docString) {
var doc = new EditSession(docString.split("\n"));
editor = new Editor(new MockRenderer(), doc);
editor.setKeyboardHandler(emacs.handler);
}
module.exports = {
"test: detach removes emacs commands from command manager": function() {
initEditor('');
assert.ok(!!editor.commands.byName["keyboardQuit"], 'setup error: emacs commands not installed');
editor.keyBinding.removeKeyboardHandler(editor.getKeyboardHandler());
assert.ok(!editor.commands.byName["keyboardQuit"], 'emacs commands not removed');
},
"test: keyboardQuit clears selection": function() {
initEditor('foo');
editor.selectAll();
editor.execCommand('keyboardQuit');
assert.ok(editor.selection.isEmpty(), 'selection non-empty');
}
};
});
if (typeof module !== "undefined" && module === require.main) {
require("asyncjs").test.testcase(module.exports).exec()
}

View file

@ -21,6 +21,7 @@ var testNames = [
"ace/editor_navigation_test", "ace/editor_navigation_test",
"ace/editor_text_edit_test", "ace/editor_text_edit_test",
"ace/ext/static_highlight_test", "ace/ext/static_highlight_test",
"ace/keyboard/emacs_test",
"ace/layer/text_test", "ace/layer/text_test",
"ace/lib/event_emitter_test", "ace/lib/event_emitter_test",
"ace/mode/coffee/parser_test", "ace/mode/coffee/parser_test",