From ff79cbf748eebaeb220b4be399ecc8a0083a9617 Mon Sep 17 00:00:00 2001 From: Kris Maglione Date: Sat, 11 Oct 2008 00:21:07 +0000 Subject: [PATCH] Add commands.replaceTokens --- content/commands.js | 53 ++++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 32 deletions(-) diff --git a/content/commands.js b/content/commands.js index fd5fa82c..f49c0b07 100644 --- a/content/commands.js +++ b/content/commands.js @@ -696,6 +696,21 @@ liberator.Commands = function () //{{{ break; } } + }, + + replaceTokens: function replaceTokens(str, tokens) + { + return str.replace(/<((?:q-)?)([a-zA-Z]+)?>/g, function (match, quote, token) + { + if (token == "lt") // Don't quote, as in vim (but, why so in vim? You'd think people wouldn't say if they didn't want it) + return "<"; + let res = tokens[token]; + if (res == undefined) // Ignore anything undefined + res = "<" + token + ">"; + if (quote) + return '"' + String.replace(res, '"', '\\"', "g") + '"'; + return res; + }); } }; @@ -706,39 +721,13 @@ liberator.Commands = function () //{{{ function userCommand(args, special, count, modifiers) { - function replaceTokens(token) - { - let ret; - let quote = false; + let tokens = { + args: this.argCount && args.string, + bang: this.bang && bang ? "!" : "", + count: this.count && count + }; - // ignore quoting of like Vim, not needed for - if (/^$/.test(token)) - quote = true; - - token = token.replace("q-", ""); - - switch (token) - { - case "": - ret = args.argumentsString; - break; - case "": - ret = bangOpt ? (special ? "!" : "") : token; - break; - case "": - ret = countOpt ? (count > -1 ? count : 0) : token; - break; - case "": - ret = "<"; - break; - default: - ret = token; - } - - return quote ? '"' + ret.replace('"', '\\"', "g") + '"' : ret; - } - - liberator.execute(this.replacementText.replace(/<(?:q-)?[a-z]+>/g, replaceTokens)); + liberator.execute(liberator.commands.replaceTokens(this.replacementText, tokens)); } // TODO: Vim allows commands to be defined without {rep} if there are {attr}s