1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-21 08:07:58 +01:00

Replace uses of commands.replaceTokens with util.compileMacro.

This commit is contained in:
Kris Maglione
2010-12-23 03:22:29 -05:00
parent 76fad17378
commit 732919f2cd
3 changed files with 26 additions and 47 deletions

View File

@@ -335,12 +335,13 @@ const Command = Class("Command", {
case "-keys": case "-keys":
let silent = args["-silent"]; let silent = args["-silent"];
rhs = events.canonicalKeys(rhs, true); rhs = events.canonicalKeys(rhs, true);
var action = function action(count) let macro = util.compileMacro(rhs, true);
events.feedkeys(commands.replaceTokens(rhs, { count: count }), var action = function action(count) events.feedkeys(macro({ count: count }),
noremap, silent); noremap, silent);
break; break;
case "-ex": case "-ex":
action = function action() commands.execute(rhs, makeParams.apply(this, arguments), macro = util.compileMacro(rhs, true);
action = function action() commands.execute(macro, makeParams.apply(this, arguments),
false, null, action.sourcing); false, null, action.sourcing);
action.sourcing = io.sourcing && update({}, io.sourcing); action.sourcing = io.sourcing && update({}, io.sourcing);
break; break;
@@ -572,8 +573,10 @@ const Commands = Module("commands", {
args = update({}, args || {}); args = update({}, args || {});
if (tokens) if (tokens && !callable(string))
string = commands.replaceTokens(string, tokens); string = util.compileMacro(string, true);
if (callable(string))
string = string(tokens || {});
let lines = string.split(/\r\n|[\r\n]/); let lines = string.split(/\r\n|[\r\n]/);
@@ -1150,34 +1153,6 @@ const Commands = Module("commands", {
this._exCommands = this._exCommands.filter(function (c) c !== cmd); this._exCommands = this._exCommands.filter(function (c) c !== cmd);
for (let name in values(cmd.names)) for (let name in values(cmd.names))
delete this._exMap[name]; delete this._exMap[name];
},
// FIXME: still belong here? Also used for autocommand parameters.
/**
* Returns a string with all tokens in *string* matching "<key>" replaced
* with "value". Where "key" is a property of the specified *tokens* object
* and "value" is the corresponding value. The <lt> token can be used to
* include a literal "<" in the returned string. Any tokens prefixed with
* "q-" will be quoted except for <q-lt> which is treated like <lt>.
*
* @param {string} str The string with tokens to replace.
* @param {Object} tokens A map object whose keys are replaced with its
* values.
* @returns {string}
*/
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 <q-lt> if they didn't want it)
return "<";
let res = tokens[token];
if (res === undefined) // Ignore anything undefined
res = "<" + token + ">";
if (res === null)
res = "";
if (quote && typeof res != "number")
return Commands.quoteArg['"'](res);
return res;
});
} }
}, { }, {
// returns [count, parsed_argument] // returns [count, parsed_argument]

View File

@@ -586,7 +586,7 @@ lookup:
file.append(config.name + ".vim"); file.append(config.name + ".vim");
dactyl.assert(!file.exists() || args.bang, "File exists"); dactyl.assert(!file.exists() || args.bang, "File exists");
let template = <![CDATA[ let template = util.compileMacro(String(<![CDATA[
" Vim syntax file " Vim syntax file
" Language: Pentadactyl configuration file " Language: Pentadactyl configuration file
" Maintainer: Doug Kearns <dougkearns@gmail.com> " Maintainer: Doug Kearns <dougkearns@gmail.com>
@@ -631,7 +631,7 @@ execute 'syn match <name>Option "\<\%(no\|inv\)\=\%(' .
\ join(s:toggleOptions, '\|') . \ join(s:toggleOptions, '\|') .
\ '\)\>!\=" contained nextgroup=<name>SetMod' \ '\)\>!\=" contained nextgroup=<name>SetMod'
syn match <name>SetMod "\%(\<[a-z_]\+\)\@<=&" contained syn match <name>SetMod "\%(\<lt>[a-z_]\+\)\@<=&" contained
syn region <name>JavaScript start="\%(^\s*\%(javascript\|js\)\s\+\)\@<=" end="$" contains=@javascriptTop keepend oneline syn region <name>JavaScript start="\%(^\s*\%(javascript\|js\)\s\+\)\@<=" end="$" contains=@javascriptTop keepend oneline
syn region <name>JavaScript matchgroup=<name>JavaScriptDelimiter syn region <name>JavaScript matchgroup=<name>JavaScriptDelimiter
@@ -642,7 +642,7 @@ execute 'syn region <name>Css start="' . s:cssRegionStart . '" end="$" contains=
execute 'syn region <name>Css matchgroup=<name>CssDelimiter' execute 'syn region <name>Css matchgroup=<name>CssDelimiter'
\ 'start="' . s:cssRegionStart . '<<\s*\z(\h\w*\)"hs=s+2 end="^\z1$" contains=@cssTop fold' \ 'start="' . s:cssRegionStart . '<<\s*\z(\h\w*\)"hs=s+2 end="^\z1$" contains=@cssTop fold'
syn match <name>Notation "<[0-9A-Za-z-]\+>" syn match <name>Notation "<lt>[0-9A-Za-z-]\+>"
syn match <name>Comment +".*$+ contains=<name>Todo,@Spell syn match <name>Comment +".*$+ contains=<name>Todo,@Spell
syn keyword <name>Todo FIXME NOTE TODO XXX contained syn keyword <name>Todo FIXME NOTE TODO XXX contained
@@ -671,7 +671,7 @@ let &cpo = s:cpo_save
unlet s:cpo_save unlet s:cpo_save
" vim: tw=130 et ts=4 sw=4: " vim: tw=130 et ts=4 sw=4:
]]>; ]]>), true);
const WIDTH = 80; const WIDTH = 80;
function wrap(prefix, items, sep) { function wrap(prefix, items, sep) {
@@ -693,7 +693,7 @@ unlet s:cpo_save
return lines.map(function (l) l.join("")).join("\n").replace(/\s+\n/gm, "\n"); return lines.map(function (l) l.join("")).join("\n").replace(/\s+\n/gm, "\n");
} }
file.write(commands.replaceTokens(String(template), { file.write(template({
name: config.name, name: config.name,
autocommands: wrap("syn keyword " + config.name + "AutoEvent ", autocommands: wrap("syn keyword " + config.name + "AutoEvent ",
keys(config.autocommands)), keys(config.autocommands)),

View File

@@ -256,10 +256,14 @@ const Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
return stack.top; return stack.top;
}, },
compileMacro: function compileFormat(macro) { compileMacro: function compileFormat(macro, keepUnknown) {
let stack = [frame()]; let stack = [frame()];
stack.__defineGetter__("top", function () this[this.length - 1]); stack.__defineGetter__("top", function () this[this.length - 1]);
let unknown = util.identity;
if (!keepUnknown)
unknown = function () "";
function frame() update( function frame() update(
function _frame(obj) function _frame(obj)
_frame === stack.top || _frame.valid(obj) ? _frame === stack.top || _frame.valid(obj) ?
@@ -274,15 +278,15 @@ const Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
let match, end = 0; let match, end = 0;
let re = util.regexp(<![CDATA[ let re = util.regexp(<![CDATA[
(.*?) // 1 ([^]*?) // 1
(?: (?:
(<\[) | // 2 (<\[) | // 2
< (.*?) > | // 3 (< (.*?) >) | // 3 4
(\]>) // 4 (\]>) // 5
) )
]]>, "gy"); ]]>, "gy");
while (match = re.exec(macro)) { while (match = re.exec(macro)) {
let [, prefix, open, macro, close] = match; let [, prefix, open, full, macro, close] = match;
end += match[0].length; end += match[0].length;
if (prefix) if (prefix)
@@ -302,14 +306,14 @@ const Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
let quote = util.identity; let quote = util.identity;
if (flags.q) if (flags.q)
quote = function quote(obj) typeof obj === "number" ? obj : Commands.quote(obj); quote = function quote(obj) typeof obj === "number" ? obj : String.quote(obj);
if (set.has(defaults, name)) if (set.has(defaults, name))
stack.top.elements.push(quote(defaults[name])); stack.top.elements.push(quote(defaults[name]));
else { else {
stack.top.elements.push(update( stack.top.elements.push(update(
function (obj) obj[name] != null ? quote(obj[name]) : "", function (obj) obj[name] != null ? quote(obj[name]) : unknown(full),
{ test: function (obj) obj[name] != null })); { test: function (obj) obj[name] != null && obj[name] !== false }));
for (let elem in array.iterValues(stack)) for (let elem in array.iterValues(stack))
elem.seen[name] = true; elem.seen[name] = true;