aboutsummaryrefslogtreecommitdiffstats
path: root/background_scripts
diff options
context:
space:
mode:
authorStephen Blott2016-10-23 16:02:53 +0100
committerGitHub2016-10-23 16:02:53 +0100
commit458c7401394a8e2a97dbddfbf850b643990a1e70 (patch)
treee637b7cc11efc4332c6662388a1c8de3abb33896 /background_scripts
parentf01c01383d5826a028d4c5c2c43d7b5f18f2fef2 (diff)
parent4486be1f40c582bf159e034f590cb82cb84caafc (diff)
downloadvimium-458c7401394a8e2a97dbddfbf850b643990a1e70.tar.bz2
Merge pull request #2315 from smblott-github/better-line-parsing
Better line parsing (for custom key mappings)
Diffstat (limited to 'background_scripts')
-rw-r--r--background_scripts/bg_utils.coffee8
-rw-r--r--background_scripts/commands.coffee57
2 files changed, 36 insertions, 29 deletions
diff --git a/background_scripts/bg_utils.coffee b/background_scripts/bg_utils.coffee
index ea54c900..26033476 100644
--- a/background_scripts/bg_utils.coffee
+++ b/background_scripts/bg_utils.coffee
@@ -77,4 +77,12 @@ BgUtils =
logElement.value += "#{dateString}: #{message}\n"
logElement.scrollTop = 2000000000
+ # Remove comments and leading/trailing whitespace from a list of lines, and merge lines where the last
+ # character on the preceding line is "\".
+ parseLines: (text) ->
+ for line in text.replace(/\\\n/g, "").split("\n").map((line) -> line.trim())
+ continue if line.length == 0
+ continue if line[0] in '#"'
+ line
+
root.BgUtils = BgUtils
diff --git a/background_scripts/commands.coffee b/background_scripts/commands.coffee
index d12b704d..6aa8ca0d 100644
--- a/background_scripts/commands.coffee
+++ b/background_scripts/commands.coffee
@@ -61,35 +61,34 @@ Commands =
[key[0], @parseKeySequence(key[1..])...]
parseCustomKeyMappings: (customKeyMappings) ->
- 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, optionList... ] = tokens
- keySequence = @parseKeySequence key
- if command? and @availableCommands[command]
- key = keySequence.join ""
- BgUtils.log "mapping [\"#{keySequence.join '", "'}\"] to #{command}"
- @mapKeyToCommand { key, command, keySequence, options: @parseCommandOptions command, optionList }
- else
- BgUtils.log "skipping [\"#{keySequence.join '", "'}\"] for #{command} -- something is not right"
-
- when "unmap"
- if tokens.length == 2
- keySequence = @parseKeySequence tokens[1]
- key = keySequence.join ""
- BgUtils.log "Unmapping #{key}"
- delete @keyToCommandRegistry[key]
-
- when "unmapAll"
- @keyToCommandRegistry = {}
-
- when "mapkey"
- if tokens.length == 3
- fromChar = @parseKeySequence tokens[1]
- toChar = @parseKeySequence tokens[2]
- @mapKeyRegistry[fromChar[0]] = toChar[0] if fromChar.length == toChar.length == 1
+ for line in BgUtils.parseLines customKeyMappings
+ tokens = line.split /\s+/
+ switch tokens[0]
+ when "map"
+ [ _, key, command, optionList... ] = tokens
+ keySequence = @parseKeySequence key
+ if command? and @availableCommands[command]
+ key = keySequence.join ""
+ BgUtils.log "mapping [\"#{keySequence.join '", "'}\"] to #{command}"
+ @mapKeyToCommand { key, command, keySequence, options: @parseCommandOptions command, optionList }
+ else
+ BgUtils.log "skipping [\"#{keySequence.join '", "'}\"] for #{command} -- something is not right"
+
+ when "unmap"
+ if tokens.length == 2
+ keySequence = @parseKeySequence tokens[1]
+ key = keySequence.join ""
+ BgUtils.log "Unmapping #{key}"
+ delete @keyToCommandRegistry[key]
+
+ when "unmapAll"
+ @keyToCommandRegistry = {}
+
+ when "mapkey"
+ if tokens.length == 3
+ fromChar = @parseKeySequence tokens[1]
+ toChar = @parseKeySequence tokens[2]
+ @mapKeyRegistry[fromChar[0]] = toChar[0] if fromChar.length == toChar.length == 1
# Push the key mapping for passNextKey into Settings so that it's available in the front end for insert
# mode. We exclude single-key mappings (that is, printable keys) because when users press printable keys