move key binding system into ace from gcli

This commit is contained in:
Joe Walker 2011-04-06 18:48:12 +01:00
commit 4fc695fb67
4 changed files with 182 additions and 125 deletions

View file

@ -36,9 +36,11 @@
*
* ***** END LICENSE BLOCK ***** */
define(function(require, exports, module) {
var keybinding = require('ace/keyboard/keybinding');
exports.launch = function(env) {
var canon = require("cockpit/canon");
var event = require("pilot/event");
@ -395,38 +397,29 @@ exports.launch = function(env) {
// Command to focus the command line from the editor.
canon.addCommand({
name: "focuscli",
bindKey: {
win: "Ctrl-J",
mac: "Command-J"
},
exec: function() {
env.cli.cliView.element.focus();
}
});
keybinding.bindCommand({ win: "Ctrl-J", mac: "Command-J" }, "focuscli");
// Command to focus the editor line from the command line.
canon.addCommand({
name: "focuseditor",
bindKey: {
win: "Ctrl-J",
mac: "Command-J"
},
exec: function() {
env.editor.focus();
}
});
keybinding.bindCommand({ win: "Ctrl-J", mac: "Command-J" }, "focuseditor");
// Fake-Save, works from the editor and the command line.
canon.addCommand({
name: "save",
bindKey: {
win: "Ctrl-S",
mac: "Command-S"
},
exec: function() {
alert("Fake Save File");
}
});
keybinding.bindCommand({ win: "Ctrl-S", mac: "Command-S" }, "save");
};
});

View file

@ -40,8 +40,10 @@
define(function(require, exports, module) {
var lang = require("pilot/lang");
var gcli = require("cockpit/index");
var keybinding = require('ace/keyboard/keybinding');
/**
* TODO: This could be done more concisely and reversibly
@ -54,18 +56,19 @@ exports.startup = function() {
gcli.addCommand({
name: "selectall",
bindKey: { win: "Ctrl-A", mac: "Command-A" },
exec: function(env, args) { env.editor.selectAll(); }
});
keybinding.bindCommand({ win: "Ctrl-A", mac: "Command-A" }, "selectall");
gcli.addCommand({
name: "removeline",
bindKey: { win: "Ctrl-D", mac: "Command-D" },
exec: function(env, args) { env.editor.removeLines(); }
});
keybinding.bindCommand({ win: "Ctrl-D", mac: "Command-D" }, "removeline");
gcli.addCommand({
name: "gotoline",
description: "Move the cursor to the given line",
bindKey: { win: "Ctrl-L", mac: "Command-L" },
params: [
{ name: "line", type: "number", description: "The line number to jump to" }
],
@ -79,25 +82,29 @@ exports.startup = function() {
env.editor.gotoLine(args.line);
}
});
keybinding.bindCommand({ win: "Ctrl-L", mac: "Command-L" }, "gotoline");
gcli.addCommand({
name: "togglecomment",
bindKey: { win: "Ctrl-7", mac: "Command-7" },
exec: function(env, args) { env.editor.toggleCommentLines(); }
});
keybinding.bindCommand({ win: "Ctrl-7", mac: "Command-7" }, "togglecomment");
gcli.addCommand({
name: "findnext",
bindKey: { win: "Ctrl-K", mac: "Command-G" },
exec: function(env, args) { env.editor.findNext(); }
});
keybinding.bindCommand({ win: "Ctrl-K", mac: "Command-G" }, "findnext");
gcli.addCommand({
name: "findprevious",
bindKey: { win: "Ctrl-Shift-K", mac: "Command-Shift-G" },
exec: function(env, args) { env.editor.findPrevious(); }
});
keybinding.bindCommand({ win: "Ctrl-Shift-K", mac: "Command-Shift-G" }, "findprevious");
gcli.addCommand({
name: "find",
description: "Search for the next instance of a string",
bindKey: { win: "Ctrl-F", mac: "Command-F" },
params: [
{ name: "findWhat", type: "string", description: "The text to search for" }
],
@ -109,10 +116,11 @@ exports.startup = function() {
env.editor.find(args.findWhat);
}
});
keybinding.bindCommand({ win: "Ctrl-F", mac: "Command-F" }, "find");
gcli.addCommand({
name: "replace",
description: "Replace the next instance of a string with a given replacement",
bindKey: { win: "Ctrl-R", mac: "Command-Option-F" },
params: [
{ name: "findWhat", type: "string", description: "The text to search for" },
{ name: "replacement", type: "string", description: "The replacement text" }
@ -132,10 +140,11 @@ exports.startup = function() {
env.editor.replace(args.replacement, {needle: args.findWhat});
}
});
keybinding.bindCommand({ win: "Ctrl-R", mac: "Command-Option-F" }, "replace");
gcli.addCommand({
name: "replaceall",
description: "Replace all instances of a string with a given replacement",
bindKey: { win: "Ctrl-Shift-R", mac: "Command-Shift-Option-F" },
params: [
{ name: "findWhat", type: "string", description: "The text to search for" },
{ name: "replacement", type: "string", description: "The replacement text" }
@ -155,245 +164,293 @@ exports.startup = function() {
env.editor.replaceAll(args.replacement, {needle: args.findWhat});
}
});
keybinding.bindCommand({ win: "Ctrl-Shift-R", mac: "Command-Shift-Option-F" }, "replaceall");
gcli.addCommand({
name: "undo",
bindKey: { win: "Ctrl-Z", mac: "Command-Z" },
exec: function(env, args) { env.editor.undo(); }
});
keybinding.bindCommand({ win: "Ctrl-Z", mac: "Command-Z" }, "undo");
gcli.addCommand({
name: "redo",
bindKey: { win: "Ctrl-Shift-Z|Ctrl-Y", mac: "Command-Shift-Z|Command-Y" },
exec: function(env, args) { env.editor.redo(); }
});
keybinding.bindCommand({ win: "Ctrl-Shift-Z|Ctrl-Y", mac: "Command-Shift-Z|Command-Y" }, "redo");
gcli.addCommand({
name: "overwrite",
bindKey: { win: "Insert", mac: "Insert" },
exec: function(env, args) { env.editor.toggleOverwrite(); }
});
keybinding.bindCommand({ win: "Insert", mac: "Insert" }, "overwrite");
gcli.addCommand({
name: "copylinesup",
bindKey: { win: "Ctrl-Alt-Up", mac: "Command-Option-Up" },
exec: function(env, args) { env.editor.copyLinesUp(); }
});
keybinding.bindCommand({ win: "Ctrl-Alt-Up", mac: "Command-Option-Up" }, "copylinesup");
gcli.addCommand({
name: "movelinesup",
bindKey: { win: "Alt-Up", mac: "Option-Up" },
exec: function(env, args) { env.editor.moveLinesUp(); }
});
keybinding.bindCommand({ win: "Alt-Up", mac: "Option-Up" }, "movelinesup");
gcli.addCommand({
name: "selecttostart",
bindKey: { win: "Alt-Shift-Up", mac: "Command-Shift-Up" },
exec: function(env, args) { env.editor.getSelection().selectFileStart(); }
});
keybinding.bindCommand({ win: "Alt-Shift-Up", mac: "Command-Shift-Up" }, "selecttostart");
gcli.addCommand({
name: "gotostart",
bindKey: { win: "Ctrl-Home|Ctrl-Up", mac: "Command-Home|Command-Up" },
exec: function(env, args) { env.editor.navigateFileStart(); }
});
keybinding.bindCommand({ win: "Ctrl-Home|Ctrl-Up", mac: "Command-Home|Command-Up" }, "gotostart");
gcli.addCommand({
name: "selectup",
bindKey: { win: "Shift-Up", mac: "Shift-Up" },
exec: function(env, args) { env.editor.getSelection().selectUp(); }
});
keybinding.bindCommand({ win: "Shift-Up", mac: "Shift-Up" }, "selectup");
gcli.addCommand({
name: "golineup",
bindKey: { win: "Up", mac: "Up|Ctrl-P" },
exec: function(env, args) { env.editor.navigateUp(args.times); }
});
keybinding.bindCommand({ win: "Up", mac: "Up|Ctrl-P" }, "golineup");
gcli.addCommand({
name: "copylinesdown",
bindKey: { win: "Ctrl-Alt-Down", mac: "Command-Option-Down" },
exec: function(env, args) { env.editor.copyLinesDown(); }
});
keybinding.bindCommand({ win: "Ctrl-Alt-Down", mac: "Command-Option-Down" }, "copylinesdown");
gcli.addCommand({
name: "movelinesdown",
bindKey: { win: "Alt-Down", mac: "Option-Down" },
exec: function(env, args) { env.editor.moveLinesDown(); }
});
keybinding.bindCommand({ win: "Alt-Down", mac: "Option-Down" }, "movelinesdown");
gcli.addCommand({
name: "selecttoend",
bindKey: { win: "Alt-Shift-Down", mac: "Command-Shift-Down" },
exec: function(env, args) { env.editor.getSelection().selectFileEnd(); }
});
keybinding.bindCommand({ win: "Alt-Shift-Down", mac: "Command-Shift-Down" }, "selecttoend");
gcli.addCommand({
name: "gotoend",
bindKey: { win: "Ctrl-End|Ctrl-Down", mac: "Command-End|Command-Down" },
exec: function(env, args) { env.editor.navigateFileEnd(); }
});
keybinding.bindCommand({ win: "Ctrl-End|Ctrl-Down", mac: "Command-End|Command-Down" }, "gotoend");
gcli.addCommand({
name: "selectdown",
bindKey: { win: "Shift-Down", mac: "Shift-Down" },
exec: function(env, args) { env.editor.getSelection().selectDown(); }
});
keybinding.bindCommand({ win: "Shift-Down", mac: "Shift-Down" }, "selectdown");
gcli.addCommand({
name: "golinedown",
bindKey: { win: "Down", mac: "Down|Ctrl-N" },
exec: function(env, args) { env.editor.navigateDown(args.times); }
});
keybinding.bindCommand({ win: "Down", mac: "Down|Ctrl-N" }, "golinedown");
gcli.addCommand({
name: "selectwordleft",
bindKey: { win: "Ctrl-Shift-Left", mac: "Option-Shift-Left" },
exec: function(env, args) { env.editor.getSelection().selectWordLeft(); }
});
keybinding.bindCommand({ win: "Ctrl-Shift-Left", mac: "Option-Shift-Left" }, "selectwordleft");
gcli.addCommand({
name: "gotowordleft",
bindKey: { win: "Ctrl-Left", mac: "Option-Left" },
exec: function(env, args) { env.editor.navigateWordLeft(); }
});
keybinding.bindCommand({ win: "Ctrl-Left", mac: "Option-Left" }, "gotowordleft");
gcli.addCommand({
name: "selecttolinestart",
bindKey: { win: "Alt-Shift-Left", mac: "Command-Shift-Left" },
exec: function(env, args) { env.editor.getSelection().selectLineStart(); }
});
keybinding.bindCommand({ win: "Alt-Shift-Left", mac: "Command-Shift-Left" }, "selecttolinestart");
gcli.addCommand({
name: "gotolinestart",
bindKey: { win: "Alt-Left|Home", mac: "Command-Left|Home|Ctrl-A" },
exec: function(env, args) { env.editor.navigateLineStart(); }
});
keybinding.bindCommand({ win: "Alt-Left|Home", mac: "Command-Left|Home|Ctrl-A" }, "");
gcli.addCommand({
name: "selectleft",
bindKey: { win: "Shift-Left", mac: "Shift-Left" },
exec: function(env, args) { env.editor.getSelection().selectLeft(); }
});
keybinding.bindCommand({ win: "Shift-Left", mac: "Shift-Left" }, "gotolinestart");
gcli.addCommand({
name: "gotoleft",
bindKey: { win: "Left", mac: "Left|Ctrl-B" },
exec: function(env, args) { env.editor.navigateLeft(args.times); }
});
keybinding.bindCommand({ win: "Left", mac: "Left|Ctrl-B" }, "gotoleft");
gcli.addCommand({
name: "selectwordright",
bindKey: { win: "Ctrl-Shift-Right", mac: "Option-Shift-Right" },
exec: function(env, args) { env.editor.getSelection().selectWordRight(); }
});
keybinding.bindCommand({ win: "Ctrl-Shift-Right", mac: "Option-Shift-Right" }, "selectwordright");
gcli.addCommand({
name: "gotowordright",
bindKey: { win: "Ctrl-Right", mac: "Option-Right" },
exec: function(env, args) { env.editor.navigateWordRight(); }
});
keybinding.bindCommand({ win: "Ctrl-Right", mac: "Option-Right" }, "");
gcli.addCommand({
name: "selecttolineend",
bindKey: { win: "Alt-Shift-Right", mac: "Command-Shift-Right" },
exec: function(env, args) { env.editor.getSelection().selectLineEnd(); }
});
keybinding.bindCommand({ win: "Alt-Shift-Right", mac: "Command-Shift-Right" }, "gotowordright");
gcli.addCommand({
name: "gotolineend",
bindKey: { win: "Alt-Right|End", mac: "Command-Right|End|Ctrl-E" },
exec: function(env, args) { env.editor.navigateLineEnd(); }
});
keybinding.bindCommand({ win: "Alt-Right|End", mac: "Command-Right|End|Ctrl-E" }, "gotolineend");
gcli.addCommand({
name: "selectright",
bindKey: { win: "Shift-Right", mac: "Shift-Right" },
exec: function(env, args) { env.editor.getSelection().selectRight(); }
});
keybinding.bindCommand({ win: "Shift-Right", mac: "Shift-Right" }, "selectright");
gcli.addCommand({
name: "gotoright",
bindKey: { win: "Right", mac: "Right|Ctrl-F" },
exec: function(env, args) { env.editor.navigateRight(args.times); }
});
keybinding.bindCommand({ win: "Right", mac: "Right|Ctrl-F" }, "gotoright");
gcli.addCommand({
name: "selectpagedown",
bindKey: { win: "Shift-PageDown", mac: "Shift-PageDown" },
exec: function(env, args) { env.editor.selectPageDown(); }
});
keybinding.bindCommand({ win: "Shift-PageDown", mac: "Shift-PageDown" }, "selectpagedown");
gcli.addCommand({
name: "pagedown",
bindKey: { win: null, mac: "PageDown" },
exec: function(env, args) { env.editor.scrollPageDown(); }
});
keybinding.bindCommand({ win: null, mac: "PageDown" }, "pagedown");
gcli.addCommand({
name: "gotopagedown",
bindKey: { win: "PageDown", mac: "Option-PageDown|Ctrl-V" },
exec: function(env, args) { env.editor.gotoPageDown(); }
});
keybinding.bindCommand({ win: "PageDown", mac: "Option-PageDown|Ctrl-V" }, "gotopagedown");
gcli.addCommand({
name: "selectpageup",
bindKey: { win: "Shift-PageUp", mac: "Shift-PageUp" },
exec: function(env, args) { env.editor.selectPageUp(); }
});
keybinding.bindCommand({ win: "Shift-PageUp", mac: "Shift-PageUp" }, "selectpageup");
gcli.addCommand({
name: "pageup",
bindKey: { win: null, mac: "PageUp" },
exec: function(env, args) { env.editor.scrollPageUp(); }
});
keybinding.bindCommand({ win: null, mac: "PageUp" }, "pageup");
gcli.addCommand({
name: "gotopageup",
bindKey: { win: "PageUp", mac: "Option-PageUp" },
exec: function(env, args) { env.editor.gotoPageUp(); }
});
keybinding.bindCommand({ win: "PageUp", mac: "Option-PageUp" }, "gotopageup");
gcli.addCommand({
name: "selectlinestart",
bindKey: { win: "Shift-Home", mac: "Shift-Home" },
exec: function(env, args) { env.editor.getSelection().selectLineStart(); }
});
keybinding.bindCommand({ win: "Shift-Home", mac: "Shift-Home" }, "selectlinestart");
gcli.addCommand({
name: "selectlineend",
bindKey: { win: "Shift-End", mac: "Shift-End" },
exec: function(env, args) { env.editor.getSelection().selectLineEnd(); }
});
keybinding.bindCommand({ win: "Shift-End", mac: "Shift-End" }, "selectlineend");
gcli.addCommand({
name: "del",
bindKey: { win: "Delete", mac: "Delete|Ctrl-D" },
exec: function(env, args) { env.editor.removeRight(); }
});
keybinding.bindCommand({ win: "Delete", mac: "Delete|Ctrl-D" }, "del");
gcli.addCommand({
name: "backspace",
bindKey: {
win: "Ctrl-Backspace|Command-Backspace|Option-Backspace|Shift-Backspace|Backspace",
mac: "Ctrl-Backspace|Command-Backspace|Shift-Backspace|Backspace|Ctrl-H"
},
exec: function(env, args) { env.editor.removeLeft(); }
});
keybinding.bindCommand({
win: "Ctrl-Backspace|Command-Backspace|Option-Backspace|Shift-Backspace|Backspace",
mac: "Ctrl-Backspace|Command-Backspace|Shift-Backspace|Backspace|Ctrl-H"
}, "backspace");
gcli.addCommand({
name: "removetolinestart",
bindKey: { win: null, mac: "Option-Backspace" },
exec: function(env, args) { env.editor.removeToLineStart(); }
});
keybinding.bindCommand({ win: null, mac: "Option-Backspace" }, "removetolinestart");
gcli.addCommand({
name: "removetolineend",
bindKey: { win: null, mac: "Ctrl-K" },
exec: function(env, args) { env.editor.removeToLineEnd(); }
});
keybinding.bindCommand({ win: null, mac: "Ctrl-K" }, "removetolineend");
gcli.addCommand({
name: "removewordleft",
bindKey: { win: null, mac: "Alt-Backspace|Ctrl-Alt-Backspace" },
exec: function(env, args) { env.editor.removeWordLeft(); }
});
keybinding.bindCommand({ win: null, mac: "Alt-Backspace|Ctrl-Alt-Backspace" }, "removewordleft");
gcli.addCommand({
name: "removewordright",
bindKey: { win: null, mac: "Alt-Delete" },
exec: function(env, args) { env.editor.removeWordRight(); }
});
keybinding.bindCommand({ win: null, mac: "Alt-Delete" }, "removewordright");
gcli.addCommand({
name: "outdent",
bindKey: { win: "Shift-Tab", mac: "Shift-Tab" },
exec: function(env, args) { env.editor.blockOutdent(); }
});
keybinding.bindCommand({ win: "Shift-Tab", mac: "Shift-Tab" }, "outdent");
gcli.addCommand({
name: "indent",
bindKey: { win: "Tab", mac: "Tab" },
exec: function(env, args) { env.editor.indent(); }
});
keybinding.bindCommand({ win: "Tab", mac: "Tab" }, "indent");
gcli.addCommand({
name: "inserttext",
exec: function(env, args) {
env.editor.insert(lang.stringRepeat(args.text || "", args.times || 1));
}
});
gcli.addCommand({
name: "centerselection",
bindKey: { win: null, mac: "Ctrl-L" },
exec: function(env, args) { env.editor.centerSelection(); }
});
keybinding.bindCommand({ win: null, mac: "Ctrl-L" }, "centerselection");
gcli.addCommand({
name: "splitline",
bindKey: { win: null, mac: "Ctrl-O" },
exec: function(env, args) { env.editor.splitLine(); }
});
keybinding.bindCommand({ win: null, mac: "Ctrl-O" }, "splitline");
gcli.addCommand({
name: "transposeletters",
bindKey: { win: "Ctrl-T", mac: "Ctrl-T" },
exec: function(env, args) { env.editor.transposeLetters(); }
});
keybinding.bindCommand({ win: "Ctrl-T", mac: "Ctrl-T" }, "transposeletters");
};

View file

@ -38,6 +38,7 @@
define(function(require, exports, module) {
var useragent = require('pilot/useragent');
var event = require('pilot/event');
var typecheck = require('pilot/typecheck');
@ -45,11 +46,16 @@ var canon = require('cockpit/canon');
var keyUtil = require('ace/keys');
var settings = require('ace/settings').settings;
var Requisition = require('cockpit/index').Requisition;
var KeyBinding = function(editor) {
this.$editor = editor;
this.$data = { };
this.$keyboardHandler = null;
// TODO: This is WRONG: we should be taking the env passed in to setup
this.$env = { editor: this.$editor };
this.$requisition = new Requisition(this.$env);
};
(function() {
@ -65,8 +71,7 @@ var KeyBinding = function(editor) {
};
this.$callKeyboardHandler = function (e, hashId, keyOrText, keyCode) {
var env = {editor: this.$editor},
toExecute;
var toExecute;
if (this.$keyboardHandler) {
toExecute =
@ -82,16 +87,23 @@ var KeyBinding = function(editor) {
} else {
toExecute = {
command: "inserttext",
args: {
text: keyOrText
}
args: { text: keyOrText }
};
}
}
if (toExecute) {
var success = canon.exec(toExecute.command,
env, toExecute.args);
// TODO: use requisition to execute the command rather than canon
// do we store a requ, if so where?
var success;
if (typeof toExecute.command === "string" && toExecute.args == null) {
this.$requisition.update({ typed: toExecute.command });
success = this.$requisition.exec();
}
else {
success = canon.exec(toExecute.command, this.$env, toExecute.args);
}
if (success) {
return event.stopEvent(e);
}
@ -117,52 +129,6 @@ exports.KeyBinding = KeyBinding;
*/
var commmandKeyBinding = { };
function splitSafe(s, separator, limit, bLowerCase) {
return (bLowerCase && s.toLowerCase() || s)
.replace(/(?:^\s+|\n|\s+$)/g, "")
.split(new RegExp("[\\s ]*" + separator + "[\\s ]*", "g"), limit || 999);
}
var platform = useragent.isMac ? "mac" : "win";
function buildKeyHash(binding, command) {
if (!binding.mac && binding.mac !== null) {
throw new Error('All key bindings must have a mac key binding');
}
if (!binding.win && binding.win !== null) {
throw new Error('All key bindings must have a windows key binding');
}
if (!binding[platform]) {
// No key mapping for this platform.
return;
}
binding[platform].split("|").forEach(function(keyPart) {
parseKeys(keyPart, command);
});
}
function parseKeys(keys, command) {
var key;
var hashId = 0;
var parts = splitSafe(keys, "\\-", null, true);
parts.forEach(function(part) {
if (keyUtil.KEY_MODS[part])
hashId = hashId | keyUtil.KEY_MODS[part];
else
key = part || "-"; // when empty, the splitSafe removed a '-'
});
if (commmandKeyBinding[hashId] == null) {
commmandKeyBinding[hashId] = {};
}
commmandKeyBinding[hashId][key] = command;
}
canon.buildKeyHash = buildKeyHash;
function findKeyCommand(hashId, textOrKey) {
// Convert keyCode to the string representation.
if (typecheck.isNumber(textOrKey)) {
@ -172,6 +138,47 @@ function findKeyCommand(hashId, textOrKey) {
return commmandKeyBinding[hashId] && commmandKeyBinding[hashId][textOrKey];
}
function splitSafe(s, separator, limit, bLowerCase) {
return (bLowerCase && s.toLowerCase() || s)
.replace(/(?:^\s+|\n|\s+$)/g, "")
.split(new RegExp("[\\s ]*" + separator + "[\\s ]*", "g"), limit || 999);
}
var platform = useragent.isMac ? "mac" : "win";
function bindCommand(bindKey, value) {
if (!bindKey.mac && bindKey.mac !== null) {
throw new Error('All key bindings must have a mac key binding');
}
if (!bindKey.win && bindKey.win !== null) {
throw new Error('All key bindings must have a windows key binding');
}
if (!bindKey[platform]) {
// No key mapping for this platform.
return;
}
bindKey[platform].split("|").forEach(function(keys) {
var key;
var hashId = 0;
var parts = splitSafe(keys, "\\-", null, true);
parts.forEach(function(part) {
if (keyUtil.KEY_MODS[part])
hashId = hashId | keyUtil.KEY_MODS[part];
else
key = part || "-"; // when empty, the splitSafe removed a '-'
});
if (commmandKeyBinding[hashId] == null) {
commmandKeyBinding[hashId] = {};
}
commmandKeyBinding[hashId][key] = value;
});
}
exports.bindCommand = bindCommand;
});

@ -1 +1 @@
Subproject commit 5ad5e2778ad09ad602e6c15c0b3dfa6fdf388e79
Subproject commit c432453874edb9fd4e999c833f66437351c4ff3f