aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephen Blott2016-02-27 15:03:09 +0000
committerStephen Blott2016-03-05 05:37:40 +0000
commit47de80f2fcb03c8741ab46308ce982209f74f6ac (patch)
treec9070f1e1777ddef4f10bf91280d88114704408a
parent3f63ceb19046157692eac9e9a13486af7e50a57e (diff)
downloadvimium-47de80f2fcb03c8741ab46308ce982209f74f6ac.tar.bz2
Key bindings; fix passkeys.
-rw-r--r--content_scripts/mode_key_handler.coffee20
-rw-r--r--content_scripts/mode_passkeys.coffee16
-rw-r--r--content_scripts/vimium_frontend.coffee4
3 files changed, 24 insertions, 16 deletions
diff --git a/content_scripts/mode_key_handler.coffee b/content_scripts/mode_key_handler.coffee
index 6e04addb..7f03dd64 100644
--- a/content_scripts/mode_key_handler.coffee
+++ b/content_scripts/mode_key_handler.coffee
@@ -35,7 +35,7 @@ class KeyHandlerMode extends Mode
DomUtils.suppressKeyupAfterEscape handlerStack
false # Suppress event.
- else if keyChar and @keyCharIsKeyStatePrefix keyChar
+ else if keyChar and @mappingForKeyChar keyChar
@advanceKeyState keyChar
commands = @keyState.filter (entry) -> "string" == typeof entry
@invokeCommand commands[0] if 0 < commands.length
@@ -46,7 +46,7 @@ class KeyHandlerMode extends Mode
# handling that event, then we need to suppress propagation of this keydown event to prevent triggering
# page features like Google instant search.
keyChar = KeyboardUtils.getKeyChar event
- if keyChar and (@keyCharIsKeyStatePrefix(keyChar) or @isCountKey keyChar)
+ if keyChar and (@mappingForKeyChar(keyChar) or @isCountKey keyChar)
DomUtils.suppressPropagation event
@keydownEvents[@getEventCode event] = true
@stopBubblingAndTrue
@@ -56,7 +56,7 @@ class KeyHandlerMode extends Mode
onKeypress: (event) ->
keyChar = KeyboardUtils.getKeyCharString event
- if keyChar and @keyCharIsKeyStatePrefix keyChar
+ if keyChar and @mappingForKeyChar keyChar
@advanceKeyState keyChar
commands = @keyState.filter (entry) -> entry.command
@invokeCommand commands[0] if 0 < commands.length
@@ -76,11 +76,12 @@ class KeyHandlerMode extends Mode
else
@continueBubbling
- # This tests whether keyChar is a prefix of any current mapping in the key state.
- keyCharIsKeyStatePrefix: (keyChar) ->
+ # This returns the first mapping for which keyChar is mapped. The return value is truthy if a match is found
+ # and falsy otherwise.
+ mappingForKeyChar: (keyChar) ->
for mapping in @keyState
- return true if keyChar of mapping
- false
+ return mapping if keyChar of mapping
+ null
# This is called whenever a keyChar is matched. We keep any existing entries matching keyChar, and append a
# new copy of the global key mappings.
@@ -115,6 +116,11 @@ class KeyHandlerMode extends Mode
else
'1' <= keyChar <= '9'
+ # True if keyChar would be the first character of a command mapping. This is used by passKeys to decide
+ # whether keyChar is a continuation of a command which the user has already begin entering.
+ isFirstKeyChar: (keyChar) ->
+ @countPrefix == 0 and @keyMapping == @mappingForKeyChar keyChar
+
getEventCode: (event) -> event.keyCode
root = exports ? window
diff --git a/content_scripts/mode_passkeys.coffee b/content_scripts/mode_passkeys.coffee
index 631eb621..426e5369 100644
--- a/content_scripts/mode_passkeys.coffee
+++ b/content_scripts/mode_passkeys.coffee
@@ -1,6 +1,6 @@
class PassKeysMode extends Mode
- constructor: ->
+ constructor: (@normalMode) ->
super
name: "passkeys"
trackState: true # Maintain @enabled, @passKeys and @keyQueue.
@@ -8,14 +8,16 @@ class PassKeysMode extends Mode
keypress: (event) => @handleKeyChar event, String.fromCharCode event.charCode
keyup: (event) => @handleKeyChar event, KeyboardUtils.getKeyChar event
- # Keystrokes are *never* considered passKeys if the keyQueue is not empty. So, for example, if 't' is a
- # passKey, then 'gt' and '99t' will neverthless be handled by Vimium.
+ # Keystrokes are *never* considered passKeys if the user has begin entering a command. So, for example, if
+ # 't' is a passKey, then 'gt' and '99t' will neverthless be handled by Vimium.
handleKeyChar: (event, keyChar) ->
return @continueBubbling if event.altKey or event.ctrlKey or event.metaKey
- if keyChar and not @keyQueue and keyChar.length == 1 and 0 <= @passKeys.indexOf keyChar
- @stopBubblingAndTrue
- else
- @continueBubbling
+ return @continueBubbling unless keyChar and keyChar.length == 1
+ # Test whether the user has already begun entering a command.
+ return @continueBubbling unless @normalMode.isFirstKeyChar keyChar
+ return @continueBubbling unless 0 <= @passKeys.indexOf keyChar
+ # This is a passkey.
+ @stopBubblingAndTrue
root = exports ? window
root.PassKeysMode = PassKeysMode
diff --git a/content_scripts/vimium_frontend.coffee b/content_scripts/vimium_frontend.coffee
index 26942a30..7fe12d62 100644
--- a/content_scripts/vimium_frontend.coffee
+++ b/content_scripts/vimium_frontend.coffee
@@ -132,8 +132,8 @@ window.initializeModes = ->
# Install the permanent modes. The permanently-installed insert mode tracks focus/blur events, and
# activates/deactivates itself accordingly.
- new NormalMode
- new PassKeysMode
+ normalMode = new NormalMode
+ new PassKeysMode normalMode
new InsertMode permanent: true
Scroller.init()