From 61ce82be4acae20562bb2ccb93ff64772ba553c1 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sat, 20 Feb 2016 09:16:07 +0000 Subject: Refactor command-option parsing. When we introduced command options (for mapping keys to custom-search engines), the parsing was done in the Vomnibar code. This moves the parsing to `commands.coffee`, which is where it should always have been. This is a preliminary step with a view to adding a new `count` command option. --- background_scripts/commands.coffee | 20 ++++++++++++++++++-- content_scripts/vomnibar.coffee | 13 +------------ 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/background_scripts/commands.coffee b/background_scripts/commands.coffee index 80ca0f96..47eb7e8e 100644 --- a/background_scripts/commands.coffee +++ b/background_scripts/commands.coffee @@ -52,11 +52,11 @@ Commands = tokens = line.replace(/\s+$/, "").split /\s+/ switch tokens[0] when "map" - [ _, key, command, options... ] = tokens + [ _, key, command, optionList... ] = tokens if command? and @availableCommands[command] key = @normalizeKey key logMessage? "Mapping #{key} to #{command}" - @mapKeyToCommand { key, command, options } + @mapKeyToCommand { key, command, options: @parseCommandOptions optionList } when "unmap" if tokens.length == 2 @@ -74,6 +74,22 @@ Commands = Settings.set "passNextKeyKeys", (key for own key of @keyToCommandRegistry when @keyToCommandRegistry[key].command == "passNextKey" and 1 < key.length) + # Command options follow command mappings, and are of one of two forms: + # key=value - a value + # key - a flag + parseCommandOptions: (optionList) -> + options = {} + for option in optionList + parse = option.split "=" + switch parse.length + when 1 + options[parse[0]] = true + when 2 + options[parse[0]] = parse[1] + else + console.log "Vimium configuration error: invalid option: #{option}." + options + clearKeyMappingsAndSetDefaults: -> @keyToCommandRegistry = {} @mapKeyToCommand { key, command } for own key, command of defaultKeyMappings diff --git a/content_scripts/vomnibar.coffee b/content_scripts/vomnibar.coffee index 6c08ce92..55a46777 100644 --- a/content_scripts/vomnibar.coffee +++ b/content_scripts/vomnibar.coffee @@ -10,18 +10,7 @@ Vomnibar = options = {} searchEngines = Settings.get("searchEngines") ? "" SearchEngines.refreshAndUse searchEngines, (engines) -> - for option in registryEntry.options - [ key, value ] = option.split "=" - switch key - when "keyword" - if value? and engines[value]? - options.keyword = value - else - console.log "Vimium configuration error: no such custom search engine: #{option}." - else - console.log "Vimium configuration error: unused flag: #{option}." - - callback? options + callback? registryEntry.options # sourceFrameId here (and below) is the ID of the frame from which this request originates, which may be different # from the current frame. -- cgit v1.2.3 From 2c634d9b2324c760007d59d5cafd97a364125991 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sat, 20 Feb 2016 09:44:57 +0000 Subject: Add `count` command option. Examples: map j scrollDown count=10 map q removeTab count=999 --- background_scripts/commands.coffee | 9 +++++++-- background_scripts/main.coffee | 2 ++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/background_scripts/commands.coffee b/background_scripts/commands.coffee index 47eb7e8e..8d73c913 100644 --- a/background_scripts/commands.coffee +++ b/background_scripts/commands.coffee @@ -56,7 +56,7 @@ Commands = if command? and @availableCommands[command] key = @normalizeKey key logMessage? "Mapping #{key} to #{command}" - @mapKeyToCommand { key, command, options: @parseCommandOptions optionList } + @mapKeyToCommand { key, command, options: @parseCommandOptions command, optionList } when "unmap" if tokens.length == 2 @@ -77,7 +77,7 @@ Commands = # Command options follow command mappings, and are of one of two forms: # key=value - a value # key - a flag - parseCommandOptions: (optionList) -> + parseCommandOptions: (command, optionList) -> options = {} for option in optionList parse = option.split "=" @@ -88,6 +88,11 @@ Commands = options[parse[0]] = parse[1] else console.log "Vimium configuration error: invalid option: #{option}." + + # We parse any `count` option immediately (to avoid having to parse it repeatedly later). + unless @availableCommands[command].noRepeat + options.count = try Math.max 1, parseInt options.count + options clearKeyMappingsAndSetDefaults: -> diff --git a/background_scripts/main.coffee b/background_scripts/main.coffee index 5601b20d..283fa454 100644 --- a/background_scripts/main.coffee +++ b/background_scripts/main.coffee @@ -573,6 +573,8 @@ checkKeyQueue = (keysToCheck, tabId, frameId) -> """ if runCommand + # Account for any command-option "count". + count *= registryEntry.options.count ? 1 if not registryEntry.isBackgroundCommand chrome.tabs.sendMessage tabId, name: "executePageCommand" -- cgit v1.2.3 From 70b8645d61a56ea760864d222791494aa6031f92 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sat, 20 Feb 2016 11:56:05 +0000 Subject: Simplify command-option parsing. --- background_scripts/commands.coffee | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/background_scripts/commands.coffee b/background_scripts/commands.coffee index 8d73c913..d251c7ed 100644 --- a/background_scripts/commands.coffee +++ b/background_scripts/commands.coffee @@ -80,14 +80,8 @@ Commands = parseCommandOptions: (command, optionList) -> options = {} for option in optionList - parse = option.split "=" - switch parse.length - when 1 - options[parse[0]] = true - when 2 - options[parse[0]] = parse[1] - else - console.log "Vimium configuration error: invalid option: #{option}." + parse = option.split "=", 2 + options[parse[0]] = if parse.length == 1 then true else parse[1] # We parse any `count` option immediately (to avoid having to parse it repeatedly later). unless @availableCommands[command].noRepeat -- cgit v1.2.3