diff options
| author | Stephen Blott | 2016-03-05 05:43:42 +0000 |
|---|---|---|
| committer | Stephen Blott | 2016-03-05 05:43:42 +0000 |
| commit | 62359adda7bc38917de38e3fc794d37817fa05ed (patch) | |
| tree | 816d696b785f8a58b06ddd01560fee05ca0299e7 /background_scripts/commands.coffee | |
| parent | 27d3d0087c86a6effd25049cbf0d9273eb0af9db (diff) | |
| parent | fb67cfdd2ca8c09453cc896fd02d08ed5a74a8a4 (diff) | |
| download | vimium-62359adda7bc38917de38e3fc794d37817fa05ed.tar.bz2 | |
Merge pull request #2022 from smblott-github/generalised-key-bindings
Key handling in content scripts.
Diffstat (limited to 'background_scripts/commands.coffee')
| -rw-r--r-- | background_scripts/commands.coffee | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/background_scripts/commands.coffee b/background_scripts/commands.coffee index ab9992b3..7429b6f0 100644 --- a/background_scripts/commands.coffee +++ b/background_scripts/commands.coffee @@ -2,6 +2,13 @@ Commands = init: -> for own command, description of commandDescriptions @addCommand(command, description[0], description[1]) + @loadKeyMappings Settings.get "keyMappings" + Settings.postUpdateHooks["keyMappings"] = @loadKeyMappings.bind this + + loadKeyMappings: (customKeyMappings) -> + @clearKeyMappingsAndSetDefaults() + @parseCustomKeyMappings customKeyMappings + @generateKeyStateMapping() availableCommands: {} keyToCommandRegistry: {} @@ -94,6 +101,25 @@ Commands = @keyToCommandRegistry = {} @mapKeyToCommand { key, command } for own key, command of defaultKeyMappings + # This generates a nested key-to-command mapping structure. There is an example in mode_key_handler.coffee. + generateKeyStateMapping: -> + # Keys are either literal characters, or "named" - for example <a-b> (alt+b), <left> (left arrow) or <f12> + # This regular expression captures two groups: the first is a named key, the second is the remainder of + # the string. + namedKeyRegex = /^(<(?:[amc]-.|(?:[amc]-)?[a-z0-9]{2,5})>)(.*)$/ + keyStateMapping = {} + for own keys, registryEntry of @keyToCommandRegistry + currentMapping = keyStateMapping + while 0 < keys.length + [key, keys] = if 0 == keys.search namedKeyRegex then [RegExp.$1, RegExp.$2] else [keys[0], keys[1..]] + if currentMapping[key]?.command + break # Do not overwrite existing command bindings, they take priority. + else if 0 < keys.length + currentMapping = currentMapping[key] ?= {} + else + currentMapping[key] = registryEntry + chrome.storage.local.set normalModeKeyStateMapping: keyStateMapping + # An ordered listing of all available commands, grouped by type. This is the order they will # be shown in the help page. commandGroups: @@ -371,11 +397,5 @@ commandDescriptions = Commands.init() -# Register postUpdateHook for keyMappings setting. -Settings.postUpdateHooks["keyMappings"] = (value) -> - Commands.clearKeyMappingsAndSetDefaults() - Commands.parseCustomKeyMappings value - refreshCompletionKeysAfterMappingSave() - root = exports ? window root.Commands = Commands |
