From 34b1fb0f4e2ce1696c17e703d0bc43463355d6ba Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sat, 27 Feb 2016 14:20:12 +0000 Subject: Key bindings; initial partially-functioning version. --- background_scripts/commands.coffee | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'background_scripts/commands.coffee') diff --git a/background_scripts/commands.coffee b/background_scripts/commands.coffee index ab9992b3..a1002929 100644 --- a/background_scripts/commands.coffee +++ b/background_scripts/commands.coffee @@ -94,6 +94,25 @@ Commands = @keyToCommandRegistry = {} @mapKeyToCommand { key, command } for own key, command of defaultKeyMappings + # Keys are either literal characters, or "named" - for example (alt+b), (left arrow) or + # 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})>)(.*)$/ + + generateKeyStateStructure: -> + keyMapping = {} + for own keys, registryEntry of @keyToCommandRegistry + currentMapping = keyMapping + while 0 < keys.length + [key, rest] = if 0 == keys.search @namedKeyRegex then [RegExp.$1, RegExp.$2] else [keys[0], keys.slice(1)] + if 0 < rest.length + currentMapping[key] ?= {} + currentMapping = currentMapping[key] + else + currentMapping[key] = registryEntry + keys = rest + chrome.storage.local.set normalModeKeyStateMapping: keyMapping + # An ordered listing of all available commands, grouped by type. This is the order they will # be shown in the help page. commandGroups: @@ -375,6 +394,7 @@ Commands.init() Settings.postUpdateHooks["keyMappings"] = (value) -> Commands.clearKeyMappingsAndSetDefaults() Commands.parseCustomKeyMappings value + Commands.generateKeyStateStructure() refreshCompletionKeysAfterMappingSave() root = exports ? window -- cgit v1.2.3