diff --git a/lib/ace/keyboard/vim.js b/lib/ace/keyboard/vim.js index 53916421..00749bdb 100644 --- a/lib/ace/keyboard/vim.js +++ b/lib/ace/keyboard/vim.js @@ -43,6 +43,7 @@ var keyUtil = require("../lib/keys"); var cmds = require("./vim/commands"); var coreCommands = cmds.coreCommands; var util = require("./vim/maps/util"); +var useragent = require("../lib/useragent"); var startCommands = { "i": { @@ -66,6 +67,28 @@ var startCommands = { }; exports.handler = { + // workaround for j not repeating with `defaults write -g ApplePressAndHoldEnabled -bool true` + handleMacRepeat: function(data, hashId, key) { + if (hashId == -1) { + // record key + data.inputChar = key; + data.lastEvent = "input"; + } else if (data.inputChar && data.$lastHash == hashId && data.$lastKey == key) { + // check for repeated keypress + if (data.lastEvent == "input") { + data.lastEvent = "input1"; + } else if (data.lastEvent == "input1") { + // simulate textinput + return true; + } + } else { + // reset + data.$lastHash = hashId; + data.$lastKey = key; + data.lastEvent = "keypress"; + } + }, + handleKeyboard: function(data, hashId, key, keyCode, e) { // ignore command keys (shift, ctrl etc.) if (hashId != 0 && (key == "" || key == "\x00")) @@ -73,19 +96,25 @@ exports.handler = { if (hashId == 1) key = "ctrl-" + key; - + if (data.state == "start") { + if (useragent.isMac && this.handleMacRepeat(data, hashId, key)) { + hashId = -1; + key = data.inputChar; + } + if (hashId == -1 || hashId == 1) { if (cmds.inputBuffer.idle && startCommands[key]) return startCommands[key]; - - return { command: { - exec: function(editor) {cmds.inputBuffer.push(editor, key);} - } }; - } // wait for input - else if (key.length == 1 && (hashId == 0 || hashId == 4)) { //no modifier || shift + return { + command: { + exec: function(editor) {cmds.inputBuffer.push(editor, key);} + } + }; + } // if no modifier || shift: wait for input. + else if (key.length == 1 && (hashId == 0 || hashId == 4)) { return {command: "null", passEvent: true}; - } else if (key == "esc") { + } else if (key == "esc" && hashId == 0) { return {command: coreCommands.stop}; } } else {