From decf9a145d4a1f7107fe0ce533765891960f5555 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Sun, 23 Oct 2016 15:50:46 +0100 Subject: Better line parsing (simplified). --- background_scripts/bg_utils.coffee | 14 +++------- background_scripts/commands.coffee | 57 +++++++++++++++++++------------------- 2 files changed, 32 insertions(+), 39 deletions(-) diff --git a/background_scripts/bg_utils.coffee b/background_scripts/bg_utils.coffee index 27ecb0cc..26033476 100644 --- a/background_scripts/bg_utils.coffee +++ b/background_scripts/bg_utils.coffee @@ -77,18 +77,12 @@ BgUtils = logElement.value += "#{dateString}: #{message}\n" logElement.scrollTop = 2000000000 - # Remove comments from a list of lines and merge lines where the last character on the preceding line is "\". - parseLines: (lines) -> - previousText = [] - for line in lines - line = line.replace /^\s+/g, '' + # 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 '#"' - if line[line.length-1] == "\\" - previousText.push line[...line.length-1] - continue - line = previousText.join(" ") + line - previousText = [] line root.BgUtils = BgUtils diff --git a/background_scripts/commands.coffee b/background_scripts/commands.coffee index d24bdb05..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 BgUtils.parseLines 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 -- cgit v1.2.3