aboutsummaryrefslogtreecommitdiffstats
path: root/background_scripts/commands.coffee
diff options
context:
space:
mode:
authorStephen Blott2015-05-29 10:18:50 +0100
committerStephen Blott2015-05-29 10:24:41 +0100
commit4fc96415b0e01d7b0a68f74578ab9c69d5a0dd1a (patch)
treea3ee66f33bc1ed432da552c82d7b00b699d50cd1 /background_scripts/commands.coffee
parent42fa6e9506d95073e0ca6924b1b9a082d23bd483 (diff)
downloadvimium-4fc96415b0e01d7b0a68f74578ab9c69d5a0dd1a.tar.bz2
Rework parsing of custom key mappings.
Most of this is just a tidy up of code that's been around for a long time. The only difference, however, is that now a key mapping can include extra data ("extras") after the name of the command. For example, map s vomnibar.activate keyword=g Here, and extra property "extras" is added to the command registry: extras: ["keyword=g"] This is a first step towards direct vomnibar activation for custom search.
Diffstat (limited to 'background_scripts/commands.coffee')
-rw-r--r--background_scripts/commands.coffee64
1 files changed, 24 insertions, 40 deletions
diff --git a/background_scripts/commands.coffee b/background_scripts/commands.coffee
index abfbd9e2..1714ae56 100644
--- a/background_scripts/commands.coffee
+++ b/background_scripts/commands.coffee
@@ -24,21 +24,13 @@ Commands =
noRepeat: options.noRepeat
repeatLimit: options.repeatLimit
- mapKeyToCommand: (key, command) ->
+ mapKeyToCommand: ({ key, command, extras }) ->
unless @availableCommands[command]
- console.log(command, "doesn't exist!")
+ console.log command, "doesn't exist!"
return
- commandDetails = @availableCommands[command]
-
- @keyToCommandRegistry[key] =
- command: command
- isBackgroundCommand: commandDetails.isBackgroundCommand
- passCountToFunction: commandDetails.passCountToFunction
- noRepeat: commandDetails.noRepeat
- repeatLimit: commandDetails.repeatLimit
-
- unmapKey: (key) -> delete @keyToCommandRegistry[key]
+ extras ?= []
+ @keyToCommandRegistry[key] = extend { command, extras }, @availableCommands[command]
# Lower-case the appropriate portions of named keys.
#
@@ -54,37 +46,29 @@ Commands =
"<" + (if optionalPrefix then optionalPrefix else "") + keyName.toLowerCase() + ">")
parseCustomKeyMappings: (customKeyMappings) ->
- lines = customKeyMappings.split("\n")
-
- for line in lines
- continue if (line[0] == "\"" || line[0] == "#")
- splitLine = line.replace(/\s+$/, "").split(/\s+/)
-
- lineCommand = splitLine[0]
-
- if (lineCommand == "map")
- continue if (splitLine.length != 3)
- key = @normalizeKey(splitLine[1])
- vimiumCommand = splitLine[2]
-
- continue unless @availableCommands[vimiumCommand]
-
- console.log("Mapping", key, "to", vimiumCommand)
- @mapKeyToCommand(key, vimiumCommand)
- else if (lineCommand == "unmap")
- continue if (splitLine.length != 2)
-
- key = @normalizeKey(splitLine[1])
- console.log("Unmapping", key)
- @unmapKey(key)
- else if (lineCommand == "unmapAll")
- @keyToCommandRegistry = {}
+ for line in customKeyMappings.split "\n"
+ unless line[0] == "\"" or line[0] == "#"
+ tokens = line.replace(/\s+$/, "").split /\s+/
+ switch tokens[0]
+ when "map"
+ [ _, key, command, extras... ] = tokens
+ if command? and @availableCommands[command]
+ key = @normalizeKey key
+ console.log "Mapping", key, "to", command
+ @mapKeyToCommand { key, command, extras }
+
+ when "unmap"
+ if tokens.length == 2
+ key = @normalizeKey tokens[1]
+ console.log "Unmapping", key
+ delete @keyToCommandRegistry[key]
+
+ when "unmapAll"
+ @keyToCommandRegistry = {}
clearKeyMappingsAndSetDefaults: ->
@keyToCommandRegistry = {}
-
- for key of defaultKeyMappings
- @mapKeyToCommand(key, defaultKeyMappings[key])
+ @mapKeyToCommand { key, command } for key, command of defaultKeyMappings
# An ordered listing of all available commands, grouped by type. This is the order they will
# be shown in the help page.