From 6ab3e85c039f1e58c9765dc07f5d4542e2f8f5c8 Mon Sep 17 00:00:00 2001 From: Simon Lydell Date: Wed, 21 Sep 2016 17:47:14 +0200 Subject: [PATCH] Only apply `vimfx.addKeyOverrides` in Normal mode Previously, the key overrides were applied in _all_ modes, and the current mode name was passed as the second argument to the matching functions. However, it was way to easy to forget to also check the mode in the matching functions (even the documentation forgot to!). For example, if you added `j` as an override for some page, you then couldn't press `j` in Hints mode to match a hint marker. This commit fixes this problem by simply _only_ applying the key overrides in Normal mode. I can't think of any use cases for wanting key overrides in any other mode. This is technically a minor breaking API change. Fixes #812. --- documentation/api.md | 10 +++------- extension/lib/api.coffee | 3 ++- extension/test/test-api.coffee | 9 ++++++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/documentation/api.md b/documentation/api.md index 283a33f..5541c13 100644 --- a/documentation/api.md +++ b/documentation/api.md @@ -128,7 +128,8 @@ are added in order. The methods may be run multiple times. A rule is an `Array` of length 2: 1. The first item is a function that returns `true` if the rule should be - applied and `false` if not. This is called the matching function. + applied and `false` if not. This is called the matching function. The + matching function receives a [location object] as its only argument. 2. The second item is the value that should be used if the rule is applied. This is called the override. @@ -139,8 +140,6 @@ found it is applied. No more rules will be applied. The rules are matched any time the value of a VimFx option is needed. -The matching function receives a [location object]. - The override is an object whose keys are VimFx option names and whose values override the option in question. The values should be formatted as in an [options object]. @@ -168,10 +167,7 @@ vimfx.addOptionOverrides( #### `vimfx.addKeyOverrides(...rules)` The rules are matched any time you press a key that is not part of the tail of a -multi-key shortcut. - -The matching function receives a [location object] as well as the current -mode name (one of the keys of [`vimfx.modes`]). +multi-key Normal mode shortcut. The override is an array of keys which should not activate VimFx commands but be sent to the page. diff --git a/extension/lib/api.coffee b/extension/lib/api.coffee index 4975302..df837d8 100644 --- a/extension/lib/api.coffee +++ b/extension/lib/api.coffee @@ -123,9 +123,10 @@ createConfigAPI = (vimfx, {allowDeprecated = true} = {}) -> { unless vimfx.keyOverrides vimfx.keyOverrides = [] vimfx.options.keyValidator = (keyStr, mode) -> + return true unless mode == 'normal' location = utils.getCurrentLocation() return true unless location - overrides = getOverrides(vimfx.keyOverrides, location, mode) + overrides = getOverrides(vimfx.keyOverrides, location) return keyStr not in (overrides ? []) onShutdown(vimfx, -> vimfx.keyOverrides = []) vimfx.keyOverrides.push(rules...) diff --git a/extension/test/test-api.coffee b/extension/test/test-api.coffee index e4d54a0..28f2f32 100644 --- a/extension/test/test-api.coffee +++ b/extension/test/test-api.coffee @@ -250,6 +250,7 @@ exports['test vimfx.addKeyOverrides'] = (assert, $vimfx, teardown) -> $vimfx.options.translations = {} teardown(-> resetScrollToBottom?() # Defined below. + resetExitIgnoreMode?() # Defined below. resetGetCurrentLocation?() # Defined below. $vimfx.options = originalOptions $vimfx.keyOverrides = originalKeyOverrides @@ -257,16 +258,17 @@ exports['test vimfx.addKeyOverrides'] = (assert, $vimfx, teardown) -> vimfx.addKeyOverrides( [ - (location, mode) -> mode == 'normal' and location.hostname == 'example.co' + (location) -> location.hostname == 'example.co' ['j', ''] ], [ - (location, mode) -> mode == 'ignore' and location.href == 'about:blank' + (location) -> location.href == 'about:blank' [''] ] ) resetScrollToBottom = prefs.tmp('mode.normal.scroll_to_bottom', 'j') + resetExitIgnoreMode = prefs.tmp('mode.ignore.exit', '') $vimfx.createKeyTrees() $vimfx.reset('normal') @@ -321,7 +323,8 @@ exports['test vimfx.addKeyOverrides'] = (assert, $vimfx, teardown) -> {key: 'escape'}, {mode: 'ignore', focusType: 'none'} ) - assert.ok(not match) + assert.equal(match.type, 'full') + assert.ok(match) $vimfx.emit('shutdown') -- 2.39.3