fix keybinding in a slightly less kludgy way

This commit is contained in:
Joe Walker 2011-04-06 13:34:42 +01:00
commit b6e3f52faf
4 changed files with 91 additions and 183 deletions

View file

@ -397,8 +397,7 @@ exports.launch = function(env) {
name: "focuscli",
bindKey: {
win: "Ctrl-J",
mac: "Command-J",
sender: "editor"
mac: "Command-J"
},
exec: function() {
env.cli.cliView.element.focus();
@ -410,8 +409,7 @@ exports.launch = function(env) {
name: "focuseditor",
bindKey: {
win: "Ctrl-J",
mac: "Command-J",
sender: "cli"
mac: "Command-J"
},
exec: function() {
env.editor.focus();
@ -423,32 +421,12 @@ exports.launch = function(env) {
name: "save",
bindKey: {
win: "Ctrl-S",
mac: "Command-S",
sender: "editor|cli"
mac: "Command-S"
},
exec: function() {
alert("Fake Save File");
}
});
// Fake-Print with custom lookup-sender-match function.
canon.addCommand({
name: "save",
bindKey: {
win: "Ctrl-P",
mac: "Command-P",
sender: function(env, sender, hashId, keyString) {
if (sender == "editor") {
return true;
} else {
alert("Sorry, can only print from the editor");
}
}
},
exec: function() {
alert("Fake Print File");
}
});
};
});

View file

@ -43,14 +43,6 @@ define(function(require, exports, module) {
var lang = require("pilot/lang");
var gcli = require("cockpit/index");
function bindKey(win, mac) {
return {
win: win,
mac: mac,
sender: "editor"
};
}
/**
* TODO: This could be done more concisely and reversibly
*/
@ -62,18 +54,18 @@ exports.startup = function() {
gcli.addCommand({
name: "selectall",
bindKey: bindKey("Ctrl-A", "Command-A"),
bindKey: { win: "Ctrl-A", mac: "Command-A" },
exec: function(env, args) { env.editor.selectAll(); }
});
gcli.addCommand({
name: "removeline",
bindKey: bindKey("Ctrl-D", "Command-D"),
bindKey: { win: "Ctrl-D", mac: "Command-D" },
exec: function(env, args) { env.editor.removeLines(); }
});
gcli.addCommand({
name: "gotoline",
description: "Move the cursor to the given line",
bindKey: bindKey("Ctrl-L", "Command-L"),
bindKey: { win: "Ctrl-L", mac: "Command-L" },
params: [
{ name: "line", type: "number", description: "The line number to jump to" }
],
@ -89,23 +81,23 @@ exports.startup = function() {
});
gcli.addCommand({
name: "togglecomment",
bindKey: bindKey("Ctrl-7", "Command-7"),
bindKey: { win: "Ctrl-7", mac: "Command-7" },
exec: function(env, args) { env.editor.toggleCommentLines(); }
});
gcli.addCommand({
name: "findnext",
bindKey: bindKey("Ctrl-K", "Command-G"),
bindKey: { win: "Ctrl-K", mac: "Command-G" },
exec: function(env, args) { env.editor.findNext(); }
});
gcli.addCommand({
name: "findprevious",
bindKey: bindKey("Ctrl-Shift-K", "Command-Shift-G"),
bindKey: { win: "Ctrl-Shift-K", mac: "Command-Shift-G" },
exec: function(env, args) { env.editor.findPrevious(); }
});
gcli.addCommand({
name: "find",
description: "Search for the next instance of a string",
bindKey: bindKey("Ctrl-F", "Command-F"),
bindKey: { win: "Ctrl-F", mac: "Command-F" },
params: [
{ name: "findWhat", type: "string", description: "The text to search for" }
],
@ -120,7 +112,7 @@ exports.startup = function() {
gcli.addCommand({
name: "replace",
description: "Replace the next instance of a string with a given replacement",
bindKey: bindKey("Ctrl-R", "Command-Option-F"),
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" }
@ -143,7 +135,7 @@ exports.startup = function() {
gcli.addCommand({
name: "replaceall",
description: "Replace all instances of a string with a given replacement",
bindKey: bindKey("Ctrl-Shift-R", "Command-Shift-Option-F"),
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" }
@ -165,220 +157,220 @@ exports.startup = function() {
});
gcli.addCommand({
name: "undo",
bindKey: bindKey("Ctrl-Z", "Command-Z"),
bindKey: { win: "Ctrl-Z", mac: "Command-Z" },
exec: function(env, args) { env.editor.undo(); }
});
gcli.addCommand({
name: "redo",
bindKey: bindKey("Ctrl-Shift-Z|Ctrl-Y", "Command-Shift-Z|Command-Y"),
bindKey: { win: "Ctrl-Shift-Z|Ctrl-Y", mac: "Command-Shift-Z|Command-Y" },
exec: function(env, args) { env.editor.redo(); }
});
gcli.addCommand({
name: "overwrite",
bindKey: bindKey("Insert", "Insert"),
bindKey: { win: "Insert", mac: "Insert" },
exec: function(env, args) { env.editor.toggleOverwrite(); }
});
gcli.addCommand({
name: "copylinesup",
bindKey: bindKey("Ctrl-Alt-Up", "Command-Option-Up"),
bindKey: { win: "Ctrl-Alt-Up", mac: "Command-Option-Up" },
exec: function(env, args) { env.editor.copyLinesUp(); }
});
gcli.addCommand({
name: "movelinesup",
bindKey: bindKey("Alt-Up", "Option-Up"),
bindKey: { win: "Alt-Up", mac: "Option-Up" },
exec: function(env, args) { env.editor.moveLinesUp(); }
});
gcli.addCommand({
name: "selecttostart",
bindKey: bindKey("Alt-Shift-Up", "Command-Shift-Up"),
bindKey: { win: "Alt-Shift-Up", mac: "Command-Shift-Up" },
exec: function(env, args) { env.editor.getSelection().selectFileStart(); }
});
gcli.addCommand({
name: "gotostart",
bindKey: bindKey("Ctrl-Home|Ctrl-Up", "Command-Home|Command-Up"),
bindKey: { win: "Ctrl-Home|Ctrl-Up", mac: "Command-Home|Command-Up" },
exec: function(env, args) { env.editor.navigateFileStart(); }
});
gcli.addCommand({
name: "selectup",
bindKey: bindKey("Shift-Up", "Shift-Up"),
bindKey: { win: "Shift-Up", mac: "Shift-Up" },
exec: function(env, args) { env.editor.getSelection().selectUp(); }
});
gcli.addCommand({
name: "golineup",
bindKey: bindKey("Up", "Up|Ctrl-P"),
bindKey: { win: "Up", mac: "Up|Ctrl-P" },
exec: function(env, args) { env.editor.navigateUp(args.times); }
});
gcli.addCommand({
name: "copylinesdown",
bindKey: bindKey("Ctrl-Alt-Down", "Command-Option-Down"),
bindKey: { win: "Ctrl-Alt-Down", mac: "Command-Option-Down" },
exec: function(env, args) { env.editor.copyLinesDown(); }
});
gcli.addCommand({
name: "movelinesdown",
bindKey: bindKey("Alt-Down", "Option-Down"),
bindKey: { win: "Alt-Down", mac: "Option-Down" },
exec: function(env, args) { env.editor.moveLinesDown(); }
});
gcli.addCommand({
name: "selecttoend",
bindKey: bindKey("Alt-Shift-Down", "Command-Shift-Down"),
bindKey: { win: "Alt-Shift-Down", mac: "Command-Shift-Down" },
exec: function(env, args) { env.editor.getSelection().selectFileEnd(); }
});
gcli.addCommand({
name: "gotoend",
bindKey: bindKey("Ctrl-End|Ctrl-Down", "Command-End|Command-Down"),
bindKey: { win: "Ctrl-End|Ctrl-Down", mac: "Command-End|Command-Down" },
exec: function(env, args) { env.editor.navigateFileEnd(); }
});
gcli.addCommand({
name: "selectdown",
bindKey: bindKey("Shift-Down", "Shift-Down"),
bindKey: { win: "Shift-Down", mac: "Shift-Down" },
exec: function(env, args) { env.editor.getSelection().selectDown(); }
});
gcli.addCommand({
name: "golinedown",
bindKey: bindKey("Down", "Down|Ctrl-N"),
bindKey: { win: "Down", mac: "Down|Ctrl-N" },
exec: function(env, args) { env.editor.navigateDown(args.times); }
});
gcli.addCommand({
name: "selectwordleft",
bindKey: bindKey("Ctrl-Shift-Left", "Option-Shift-Left"),
bindKey: { win: "Ctrl-Shift-Left", mac: "Option-Shift-Left" },
exec: function(env, args) { env.editor.getSelection().selectWordLeft(); }
});
gcli.addCommand({
name: "gotowordleft",
bindKey: bindKey("Ctrl-Left", "Option-Left"),
bindKey: { win: "Ctrl-Left", mac: "Option-Left" },
exec: function(env, args) { env.editor.navigateWordLeft(); }
});
gcli.addCommand({
name: "selecttolinestart",
bindKey: bindKey("Alt-Shift-Left", "Command-Shift-Left"),
bindKey: { win: "Alt-Shift-Left", mac: "Command-Shift-Left" },
exec: function(env, args) { env.editor.getSelection().selectLineStart(); }
});
gcli.addCommand({
name: "gotolinestart",
bindKey: bindKey("Alt-Left|Home", "Command-Left|Home|Ctrl-A"),
bindKey: { win: "Alt-Left|Home", mac: "Command-Left|Home|Ctrl-A" },
exec: function(env, args) { env.editor.navigateLineStart(); }
});
gcli.addCommand({
name: "selectleft",
bindKey: bindKey("Shift-Left", "Shift-Left"),
bindKey: { win: "Shift-Left", mac: "Shift-Left" },
exec: function(env, args) { env.editor.getSelection().selectLeft(); }
});
gcli.addCommand({
name: "gotoleft",
bindKey: bindKey("Left", "Left|Ctrl-B"),
bindKey: { win: "Left", mac: "Left|Ctrl-B" },
exec: function(env, args) { env.editor.navigateLeft(args.times); }
});
gcli.addCommand({
name: "selectwordright",
bindKey: bindKey("Ctrl-Shift-Right", "Option-Shift-Right"),
bindKey: { win: "Ctrl-Shift-Right", mac: "Option-Shift-Right" },
exec: function(env, args) { env.editor.getSelection().selectWordRight(); }
});
gcli.addCommand({
name: "gotowordright",
bindKey: bindKey("Ctrl-Right", "Option-Right"),
bindKey: { win: "Ctrl-Right", mac: "Option-Right" },
exec: function(env, args) { env.editor.navigateWordRight(); }
});
gcli.addCommand({
name: "selecttolineend",
bindKey: bindKey("Alt-Shift-Right", "Command-Shift-Right"),
bindKey: { win: "Alt-Shift-Right", mac: "Command-Shift-Right" },
exec: function(env, args) { env.editor.getSelection().selectLineEnd(); }
});
gcli.addCommand({
name: "gotolineend",
bindKey: bindKey("Alt-Right|End", "Command-Right|End|Ctrl-E"),
bindKey: { win: "Alt-Right|End", mac: "Command-Right|End|Ctrl-E" },
exec: function(env, args) { env.editor.navigateLineEnd(); }
});
gcli.addCommand({
name: "selectright",
bindKey: bindKey("Shift-Right", "Shift-Right"),
bindKey: { win: "Shift-Right", mac: "Shift-Right" },
exec: function(env, args) { env.editor.getSelection().selectRight(); }
});
gcli.addCommand({
name: "gotoright",
bindKey: bindKey("Right", "Right|Ctrl-F"),
bindKey: { win: "Right", mac: "Right|Ctrl-F" },
exec: function(env, args) { env.editor.navigateRight(args.times); }
});
gcli.addCommand({
name: "selectpagedown",
bindKey: bindKey("Shift-PageDown", "Shift-PageDown"),
bindKey: { win: "Shift-PageDown", mac: "Shift-PageDown" },
exec: function(env, args) { env.editor.selectPageDown(); }
});
gcli.addCommand({
name: "pagedown",
bindKey: bindKey(null, "PageDown"),
bindKey: { win: null, mac: "PageDown" },
exec: function(env, args) { env.editor.scrollPageDown(); }
});
gcli.addCommand({
name: "gotopagedown",
bindKey: bindKey("PageDown", "Option-PageDown|Ctrl-V"),
bindKey: { win: "PageDown", mac: "Option-PageDown|Ctrl-V" },
exec: function(env, args) { env.editor.gotoPageDown(); }
});
gcli.addCommand({
name: "selectpageup",
bindKey: bindKey("Shift-PageUp", "Shift-PageUp"),
bindKey: { win: "Shift-PageUp", mac: "Shift-PageUp" },
exec: function(env, args) { env.editor.selectPageUp(); }
});
gcli.addCommand({
name: "pageup",
bindKey: bindKey(null, "PageUp"),
bindKey: { win: null, mac: "PageUp" },
exec: function(env, args) { env.editor.scrollPageUp(); }
});
gcli.addCommand({
name: "gotopageup",
bindKey: bindKey("PageUp", "Option-PageUp"),
bindKey: { win: "PageUp", mac: "Option-PageUp" },
exec: function(env, args) { env.editor.gotoPageUp(); }
});
gcli.addCommand({
name: "selectlinestart",
bindKey: bindKey("Shift-Home", "Shift-Home"),
bindKey: { win: "Shift-Home", mac: "Shift-Home" },
exec: function(env, args) { env.editor.getSelection().selectLineStart(); }
});
gcli.addCommand({
name: "selectlineend",
bindKey: bindKey("Shift-End", "Shift-End"),
bindKey: { win: "Shift-End", mac: "Shift-End" },
exec: function(env, args) { env.editor.getSelection().selectLineEnd(); }
});
gcli.addCommand({
name: "del",
bindKey: bindKey("Delete", "Delete|Ctrl-D"),
bindKey: { win: "Delete", mac: "Delete|Ctrl-D" },
exec: function(env, args) { env.editor.removeRight(); }
});
gcli.addCommand({
name: "backspace",
bindKey: bindKey(
"Ctrl-Backspace|Command-Backspace|Option-Backspace|Shift-Backspace|Backspace",
"Ctrl-Backspace|Command-Backspace|Shift-Backspace|Backspace|Ctrl-H"
),
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(); }
});
gcli.addCommand({
name: "removetolinestart",
bindKey: bindKey(null, "Option-Backspace"),
bindKey: { win: null, mac: "Option-Backspace" },
exec: function(env, args) { env.editor.removeToLineStart(); }
});
gcli.addCommand({
name: "removetolineend",
bindKey: bindKey(null, "Ctrl-K"),
bindKey: { win: null, mac: "Ctrl-K" },
exec: function(env, args) { env.editor.removeToLineEnd(); }
});
gcli.addCommand({
name: "removewordleft",
bindKey: bindKey(null, "Alt-Backspace|Ctrl-Alt-Backspace"),
bindKey: { win: null, mac: "Alt-Backspace|Ctrl-Alt-Backspace" },
exec: function(env, args) { env.editor.removeWordLeft(); }
});
gcli.addCommand({
name: "removewordright",
bindKey: bindKey(null, "Alt-Delete"),
bindKey: { win: null, mac: "Alt-Delete" },
exec: function(env, args) { env.editor.removeWordRight(); }
});
gcli.addCommand({
name: "outdent",
bindKey: bindKey("Shift-Tab", "Shift-Tab"),
bindKey: { win: "Shift-Tab", mac: "Shift-Tab" },
exec: function(env, args) { env.editor.blockOutdent(); }
});
gcli.addCommand({
name: "indent",
bindKey: bindKey("Tab", "Tab"),
bindKey: { win: "Tab", mac: "Tab" },
exec: function(env, args) { env.editor.indent(); }
});
gcli.addCommand({
@ -389,17 +381,17 @@ exports.startup = function() {
});
gcli.addCommand({
name: "centerselection",
bindKey: bindKey(null, "Ctrl-L"),
bindKey: { win: null, mac: "Ctrl-L" },
exec: function(env, args) { env.editor.centerSelection(); }
});
gcli.addCommand({
name: "splitline",
bindKey: bindKey(null, "Ctrl-O"),
bindKey: { win: null, mac: "Ctrl-O" },
exec: function(env, args) { env.editor.splitLine(); }
});
gcli.addCommand({
name: "transposeletters",
bindKey: bindKey("Ctrl-T", "Ctrl-T"),
bindKey: { win: "Ctrl-T", mac: "Ctrl-T" },
exec: function(env, args) { env.editor.transposeLetters(); }
});
};

View file

@ -77,7 +77,7 @@ var KeyBinding = function(editor) {
if (!toExecute || !toExecute.command) {
if (hashId != 0 || keyCode != 0) {
toExecute = {
command: findKeyCommand(env, "editor", hashId, keyOrText)
command: findKeyCommand(hashId, keyOrText)
};
} else {
toExecute = {
@ -91,7 +91,7 @@ var KeyBinding = function(editor) {
if (toExecute) {
var success = canon.exec(toExecute.command,
env, "editor", toExecute.args);
env, toExecute.args);
if (success) {
return event.stopEvent(e);
}
@ -113,14 +113,9 @@ exports.KeyBinding = KeyBinding;
/**
* A lookup has for command key bindings that use a string as sender.
* A lookup has for command key bindings.
*/
var commmandKeyBinding = {};
/**
* Array with command key bindings that use a function to determine the sender.
*/
var commandKeyBindingFunc = { };
var commmandKeyBinding = { };
function splitSafe(s, separator, limit, bLowerCase) {
return (bLowerCase && s.toLowerCase() || s)
@ -128,112 +123,55 @@ function splitSafe(s, separator, limit, bLowerCase) {
.split(new RegExp("[\\s ]*" + separator + "[\\s ]*", "g"), limit || 999);
}
function parseKeys(keys, val, ret) {
var key,
hashId = 0,
parts = splitSafe(keys, "\\-", null, true),
i = 0,
l = parts.length;
for (; i < l; ++i) {
if (keyUtil.KEY_MODS[parts[i]])
hashId = hashId | keyUtil.KEY_MODS[parts[i]];
else
key = parts[i] || "-"; //when empty, the splitSafe removed a '-'
}
if (ret == null) {
return {
key: key,
hashId: hashId
};
} else {
(ret[hashId] || (ret[hashId] = {}))[key] = val;
}
}
var platform = useragent.isMac ? "mac" : "win";
function buildKeyHash(command) {
var binding = command.bindKey,
key = binding[platform],
ckb = commmandKeyBinding,
ckbf = commandKeyBindingFunc;
if (!binding.sender) {
throw new Error('All key bindings must have a sender');
}
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]) {
if (!binding[platform]) {
// No key mapping for this platform.
return;
}
if (typeof binding.sender == 'string') {
var targets = splitSafe(binding.sender, "\\|", null, true);
targets.forEach(function(target) {
if (!ckb[target]) {
ckb[target] = { };
}
key.split("|").forEach(function(keyPart) {
parseKeys(keyPart, command, ckb[target]);
});
});
} else if (typecheck.isFunction(binding.sender)) {
var val = {
command: command,
sender: binding.sender
};
keyData = parseKeys(key);
if (!ckbf[keyData.hashId]) {
ckbf[keyData.hashId] = { };
}
if (!ckbf[keyData.hashId][keyData.key]) {
ckbf[keyData.hashId][keyData.key] = [ val ];
} else {
ckbf[keyData.hashId][keyData.key].push(val);
}
} else {
throw new Error('Key binding must have a sender that is a string or function');
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(env, sender, hashId, textOrKey) {
function findKeyCommand(hashId, textOrKey) {
// Convert keyCode to the string representation.
if (typecheck.isNumber(textOrKey)) {
textOrKey = keyUtil.keyCodeToString(textOrKey);
}
// Check bindings with functions as sender first.
var bindFuncs = (commandKeyBindingFunc[hashId] || {})[textOrKey] || [];
for (var i = 0; i < bindFuncs.length; i++) {
if (bindFuncs[i].sender(env, sender, hashId, textOrKey)) {
return bindFuncs[i].command;
}
}
var ckbr = commmandKeyBinding[sender];
return ckbr && ckbr[hashId] && ckbr[hashId][textOrKey];
return commmandKeyBinding[hashId] && commmandKeyBinding[hashId][textOrKey];
}
exports.findKeyCommand = findKeyCommand;
function execKeyCommand(env, sender, hashId, textOrKey) {
var command = findKeyCommand(env, sender, hashId, textOrKey);
if (command) {
return exec(command, env, sender, { });
} else {
return false;
}
}
exports.execKeyCommand = execKeyCommand;
});

@ -1 +1 @@
Subproject commit 81d897e6248d4304655dc068353af40a94e35c7a
Subproject commit 5ad5e2778ad09ad602e6c15c0b3dfa6fdf388e79