aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Blott2016-10-10 13:55:47 +0100
committerGitHub2016-10-10 13:55:47 +0100
commit1eb6bfdcad6d7091911e395083afe1148b2a4c76 (patch)
tree0dc625f101fbd7d2e49b73c71f067fda628d97f4
parenta3c4b530f24ee8bc1f9a1219cf35fb87a1aa0402 (diff)
parent0346b2d971ffab26a8d37946aca1c550b316e317 (diff)
downloadvimium-1eb6bfdcad6d7091911e395083afe1148b2a4c76.tar.bz2
Merge pull request #2306 from smblott-github/add-key-translation
Add "mapkey" command for key mappings.
-rw-r--r--background_scripts/commands.coffee8
-rw-r--r--content_scripts/mode_key_handler.coffee5
-rw-r--r--lib/keyboard_utils.coffee20
-rw-r--r--lib/utils.coffee7
4 files changed, 34 insertions, 6 deletions
diff --git a/background_scripts/commands.coffee b/background_scripts/commands.coffee
index 7a4526c2..d12b704d 100644
--- a/background_scripts/commands.coffee
+++ b/background_scripts/commands.coffee
@@ -9,6 +9,7 @@ Commands =
@clearKeyMappingsAndSetDefaults()
@parseCustomKeyMappings customKeyMappings
@generateKeyStateMapping()
+ chrome.storage.local.set mapKeyRegistry: @mapKeyRegistry
availableCommands: {}
keyToCommandRegistry: {}
@@ -84,6 +85,12 @@ Commands =
when "unmapAll"
@keyToCommandRegistry = {}
+ when "mapkey"
+ if tokens.length == 3
+ fromChar = @parseKeySequence tokens[1]
+ toChar = @parseKeySequence tokens[2]
+ @mapKeyRegistry[fromChar[0]] = toChar[0] if fromChar.length == toChar.length == 1
+
# Push the key mapping for passNextKey into Settings so that it's available in the front end for insert
# mode. We exclude single-key mappings (that is, printable keys) because when users press printable keys
# in insert mode they expect the character to be input, not to be droppped into some special Vimium
@@ -109,6 +116,7 @@ Commands =
clearKeyMappingsAndSetDefaults: ->
@keyToCommandRegistry = {}
+ @mapKeyRegistry = {}
for own key, command of defaultKeyMappings
keySequence = @parseKeySequence key
key = keySequence.join ""
diff --git a/content_scripts/mode_key_handler.coffee b/content_scripts/mode_key_handler.coffee
index 8c0ae4b8..480a79af 100644
--- a/content_scripts/mode_key_handler.coffee
+++ b/content_scripts/mode_key_handler.coffee
@@ -33,8 +33,12 @@ class KeyHandlerMode extends Mode
# We cannot track keyup events if we lose the focus.
blur: (event) => @alwaysContinueBubbling => @keydownEvents = {} if event.target == window
+ @mapKeyRegistry = {}
+ Utils.monitorChromeStorage "mapKeyRegistry", (value) => @mapKeyRegistry = value
+
onKeydown: (event) ->
keyChar = KeyboardUtils.getKeyCharString event
+ keyChar = @mapKeyRegistry[keyChar] ? keyChar
isEscape = KeyboardUtils.isEscape event
if isEscape and (@countPrefix != 0 or @keyState.length != 1)
@keydownEvents[event.keyCode] = true
@@ -61,6 +65,7 @@ class KeyHandlerMode extends Mode
onKeypress: (event) ->
keyChar = KeyboardUtils.getKeyCharString event
+ keyChar = @mapKeyRegistry[keyChar] ? keyChar
if @isMappedKey keyChar
@handleKeyChar keyChar
else if @isCountKey keyChar
diff --git a/lib/keyboard_utils.coffee b/lib/keyboard_utils.coffee
index f0e791d4..c49fb3f4 100644
--- a/lib/keyboard_utils.coffee
+++ b/lib/keyboard_utils.coffee
@@ -79,10 +79,18 @@ KeyboardUtils =
isPrimaryModifierKey: (event) -> if (@platform == "Mac") then event.metaKey else event.ctrlKey
- isEscape: (event) ->
- # c-[ is mapped to ESC in Vim by default.
- (event.keyCode == @keyCodes.ESC) ||
- (event.ctrlKey && @getKeyChar(event) == '[' and not event.metaKey and not event.altKey)
+ isEscape: do ->
+ mapKeyRegistry = {}
+ # NOTE: "?" here for the tests.
+ Utils?.monitorChromeStorage "mapKeyRegistry", (value) => mapKeyRegistry = value
+
+ # TODO(smblott) Change this to use event.key.
+ (event) ->
+ event.keyCode == @keyCodes.ESC || do =>
+ keyChar = @getKeyCharString event, true
+ keyChar = mapKeyRegistry[keyChar] ? keyChar
+ # <c-[> is mapped to Escape in Vim by default.
+ keyChar == "<c-[>"
# TODO. This is probably a poor way of detecting printable characters. However, it shouldn't incorrectly
# identify any of chrome's own keyboard shortcuts as printable.
@@ -97,7 +105,7 @@ KeyboardUtils =
# Return the Vimium key representation for this keyboard event. Return a falsy value (the empty string or
# undefined) when no Vimium representation is appropriate.
- getKeyCharString: (event) ->
+ getKeyCharString: (event, allKeydownEvents = false) ->
switch event.type
when "keypress"
# Ignore modifier keys by themselves.
@@ -107,7 +115,7 @@ KeyboardUtils =
when "keydown"
# Handle special keys and normal input keys with modifiers being pressed.
keyChar = @getKeyChar event
- if 1 < keyChar.length or (keyChar.length == 1 and (event.metaKey or event.ctrlKey or event.altKey))
+ if 1 < keyChar.length or (keyChar.length == 1 and (event.metaKey or event.ctrlKey or event.altKey)) or allKeydownEvents
modifiers = []
keyChar = keyChar.toUpperCase() if event.shiftKey
diff --git a/lib/utils.coffee b/lib/utils.coffee
index c06d8ac5..5a028186 100644
--- a/lib/utils.coffee
+++ b/lib/utils.coffee
@@ -209,6 +209,13 @@ Utils =
makeIdempotent: (func) ->
(args...) -> ([previousFunc, func] = [func, null])[0]? args...
+ monitorChromeStorage: (key, setter) ->
+ # NOTE: "?" here for the tests.
+ chrome?.storage.local.get key, (obj) =>
+ setter obj[key] if obj[key]?
+ chrome.storage.onChanged.addListener (changes, area) =>
+ setter changes[key].newValue if changes[key]?.newValue?
+
# Utility for parsing and using the custom search-engine configuration. We re-use the previous parse if the
# search-engine configuration is unchanged.
SearchEngines =