From 20a7c70a4c58dca11b1022c74c2af5de02e3d181 Mon Sep 17 00:00:00 2001 From: David Lynch Date: Mon, 15 Aug 2011 23:01:48 -0500 Subject: [PATCH] Improve VIM bindings somewhat Make it easy to have vim bindings which work with optional [count]s. Add undo as a binding while I'm at it. --- lib/ace/keyboard/keybinding/vim.js | 94 +++++++++--------------------- 1 file changed, 26 insertions(+), 68 deletions(-) diff --git a/lib/ace/keyboard/keybinding/vim.js b/lib/ace/keyboard/keybinding/vim.js index 999bfa75..c3a321f5 100644 --- a/lib/ace/keyboard/keybinding/vim.js +++ b/lib/ace/keyboard/keybinding/vim.js @@ -40,6 +40,22 @@ define(function(require, exports, module) { var StateHandler = require("ace/keyboard/state_handler").StateHandler; var matchCharacterOnly = require("ace/keyboard/state_handler").matchCharacterOnly; +var vimcommand = function(key, exec, then) { + return { + regex: [ "([0-9]*)", key ], + exec: exec, + params: [ + { + name: "times", + match: 1, + type: "number", + defaultValue: 1 + } + ], + then: then + } +} + var vimStates = { start: [ { @@ -75,78 +91,20 @@ var vimStates = { exec: "overwrite", then: "replaceMode" }, - { - regex: [ "([0-9]*)", "(k|up)" ], - exec: "golineup", - params: [ - { - name: "times", - match: 1, - type: "number", - defaultValue: 1 - } - ] - }, - { - regex: [ "([0-9]*)", "(j|down|enter)" ], - exec: "golinedown", - params: [ - { - name: "times", - match: 1, - type: "number", - defaultValue: 1 - } - ] - }, - { - regex: [ "([0-9]*)", "(l|right)" ], - exec: "gotoright", - params: [ - { - name: "times", - match: 1, - type: "number", - defaultValue: 1 - } - ] - }, - { - regex: [ "([0-9]*)", "(h|left)" ], - exec: "gotoleft", - params: [ - { - name: "times", - match: 1, - type: "number", - defaultValue: 1 - } - ] - }, + vimcommand("(k|up)", "golineup"), + vimcommand("(j|down)", "golinedown"), + vimcommand("(l|right)", "golineright"), + vimcommand("(h|left)", "golineleft"), { key: "shift-g", exec: "gotoend" }, - { - key: "b", - exec: "gotowordleft" - }, - { - key: "e", - exec: "gotowordright" - }, - { - key: "x", - exec: "del" - }, - { - key: "shift-x", - exec: "backspace" - }, - { - key: "shift-d", - exec: "removetolineend" - }, + vimcommand("b", "gotowordleft"), + vimcommand("e", "gotowordright"), + vimcommand("x", "del"), + vimcommand("shift-x", "backspace"), + vimcommand("shift-d", "removetolineend"), + vimcommand("u", "undo"), // [count] on this may/may not work, depending on browser implementation... { comment: "Catch some keyboard input to stop it here", match: matchCharacterOnly