2013-12-04 17:24:04 +00:00
|
|
|
define([
|
2014-05-14 06:05:26 +00:00
|
|
|
"settings!keys,user",
|
2013-12-04 17:24:04 +00:00
|
|
|
"command",
|
|
|
|
"editor",
|
2014-05-14 06:05:26 +00:00
|
|
|
"util/dom2",
|
|
|
|
"util/aceLoad!js/ace/keybinding-vim.js"
|
2013-12-04 17:24:04 +00:00
|
|
|
], function(Settings, command, editor) {
|
2013-08-31 20:23:41 +00:00
|
|
|
|
2013-09-01 04:21:21 +00:00
|
|
|
var keycodes = {
|
2013-12-16 22:58:37 +00:00
|
|
|
9: "Tab",
|
|
|
|
13: "Return",
|
2014-03-25 02:22:52 +00:00
|
|
|
27: "Esc",
|
2013-12-16 22:58:37 +00:00
|
|
|
32: "Space",
|
|
|
|
33: "PageUp",
|
|
|
|
34: "PageDown",
|
|
|
|
35: "End",
|
|
|
|
36: "Home",
|
|
|
|
37: "Left",
|
|
|
|
39: "Right",
|
|
|
|
38: "Up",
|
|
|
|
40: "Down",
|
2014-04-04 06:45:19 +00:00
|
|
|
186: ";",
|
2013-11-19 16:20:05 +00:00
|
|
|
187: "=",
|
2014-04-04 06:45:19 +00:00
|
|
|
189: "-",
|
|
|
|
190: ".",
|
|
|
|
192: "`",
|
|
|
|
220: "\\",
|
|
|
|
222: "'"
|
2013-09-03 15:47:59 +00:00
|
|
|
};
|
2013-09-01 04:21:21 +00:00
|
|
|
|
2013-12-20 17:01:53 +00:00
|
|
|
var defaultAceCommands = ace.require("./commands/default_commands").commands;
|
|
|
|
var AceCommandManager = ace.require("./commands/command_manager").CommandManager;
|
2014-05-14 06:05:26 +00:00
|
|
|
var vimHandler = ace.require("ace/keyboard/vim").handler;
|
2013-12-20 17:01:53 +00:00
|
|
|
|
2013-12-16 22:47:25 +00:00
|
|
|
//back-compat: we now use Ace-style bindings (Ctrl-X) instead of Vim-style (^-x)
|
2013-12-19 19:50:23 +00:00
|
|
|
var normalizeKeys = function(config) {
|
2013-12-16 22:47:25 +00:00
|
|
|
var converted = {};
|
|
|
|
for (var key in config) {
|
|
|
|
var value = config[key];
|
|
|
|
//detect old syntax
|
|
|
|
if (key.indexOf("^-") > -1 || key.indexOf("M-") > -1) {
|
|
|
|
key = key
|
|
|
|
.replace(/\^-/g, "Ctrl-")
|
|
|
|
.replace(/M-/g, "Alt-")
|
|
|
|
.replace(/-([A-Z]+)$/, "-Shift-$1")
|
|
|
|
.replace(/-([a-z]+)$/, function(match) { return match.toUpperCase() });
|
|
|
|
}
|
2013-12-19 19:50:23 +00:00
|
|
|
converted[key.toLowerCase()] = value;
|
2013-12-16 22:47:25 +00:00
|
|
|
}
|
|
|
|
return converted;
|
|
|
|
};
|
|
|
|
|
2014-01-27 19:38:10 +00:00
|
|
|
//need to auto-bind Ace keys, remove Ace conflicts
|
2013-09-06 02:07:01 +00:00
|
|
|
var bindAce = function() {
|
2013-12-20 17:01:53 +00:00
|
|
|
var handler = new AceCommandManager("win", defaultAceCommands);
|
2013-12-19 19:50:23 +00:00
|
|
|
var bindings = normalizeKeys(Settings.get("keys"));
|
2014-05-14 05:11:37 +00:00
|
|
|
var ckb = handler.commandKeyBinding;
|
2013-09-06 02:07:01 +00:00
|
|
|
for (var k in bindings) {
|
|
|
|
var action = bindings[k];
|
2013-12-20 17:01:53 +00:00
|
|
|
//if (!action.ace) continue;
|
2014-05-14 05:11:37 +00:00
|
|
|
var parsed = handler.parseKeys(k);
|
|
|
|
var existing = handler.findKeyCommand(parsed.hashId, parsed.key);
|
|
|
|
var aceCommand = action.command == "ace:command" ? action.argument : action.ace;
|
2014-05-15 00:28:18 +00:00
|
|
|
if (!aceCommand && ckb[parsed.hashId] && ckb[parsed.hashId][parsed.key]) {
|
2014-05-14 05:11:37 +00:00
|
|
|
delete ckb[parsed.hashId][parsed.key];
|
|
|
|
} else {
|
|
|
|
handler.bindKey(k, aceCommand);
|
|
|
|
}
|
2013-09-06 02:07:01 +00:00
|
|
|
}
|
2014-05-14 05:11:37 +00:00
|
|
|
handler.commandKeyBinding = ckb;
|
2014-05-15 00:55:09 +00:00
|
|
|
//remove all existing bindings
|
|
|
|
while(editor.keyBinding.removeKeyboardHandler(editor.getKeyboardHandler()));
|
|
|
|
//add our new bindings
|
2014-05-14 05:11:37 +00:00
|
|
|
editor.keyBinding.setDefaultHandler(handler);
|
2014-05-14 06:05:26 +00:00
|
|
|
if (Settings.get("user").emulateVim) {
|
|
|
|
editor.setKeyboardHandler(vimHandler);
|
|
|
|
}
|
2013-09-06 02:07:01 +00:00
|
|
|
};
|
|
|
|
command.on("init:startup", bindAce);
|
|
|
|
command.on("init:restart", bindAce);
|
|
|
|
|
2013-09-01 04:21:21 +00:00
|
|
|
//we have to listen on keydown, because keypress will get caught by the window manager
|
2013-08-31 20:23:41 +00:00
|
|
|
window.on("keydown", function(e) {
|
|
|
|
var char = String.fromCharCode(e.keyCode);
|
2013-09-01 04:21:21 +00:00
|
|
|
if (e.keyCode in keycodes) {
|
|
|
|
char = keycodes[e.keyCode];
|
|
|
|
}
|
2013-12-16 22:47:25 +00:00
|
|
|
var prefixes = [];
|
|
|
|
if (e.ctrlKey) prefixes.push("Ctrl");
|
|
|
|
if (e.altKey) prefixes.push("Alt");
|
|
|
|
if (e.shiftKey) prefixes.push("Shift");
|
|
|
|
var combo = prefixes.length ? prefixes.join("-") + "-" + char : char;
|
2013-12-19 19:50:23 +00:00
|
|
|
combo = combo.toLowerCase();
|
|
|
|
var keyConfig = normalizeKeys(Settings.get("keys"));
|
2013-08-31 20:23:41 +00:00
|
|
|
if (combo in keyConfig) {
|
|
|
|
e.preventDefault();
|
2013-09-05 15:11:19 +00:00
|
|
|
var action = keyConfig[combo];
|
|
|
|
if (typeof action == "string") {
|
|
|
|
action = {
|
|
|
|
command: action
|
|
|
|
};
|
|
|
|
}
|
2013-09-05 15:24:46 +00:00
|
|
|
if (action.ace) {
|
2013-09-06 02:07:01 +00:00
|
|
|
//we're going to bind these directly on startup
|
|
|
|
//so we shouldn't act on them
|
|
|
|
return;// editor.execCommand(action.ace);
|
2013-09-05 15:24:46 +00:00
|
|
|
}
|
2013-09-05 15:11:19 +00:00
|
|
|
command.fire(action.command, action.argument);
|
2013-08-31 20:23:41 +00:00
|
|
|
}
|
|
|
|
});
|
2013-08-20 00:53:03 +00:00
|
|
|
|
|
|
|
});
|