1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-23 05:37:58 +01:00

Allow things like :com! foo\n \ echo 1\n \ echo 2

This commit is contained in:
Kris Maglione
2010-10-11 02:47:07 -04:00
parent 5a670ebd7b
commit 4efc6a3972
6 changed files with 127 additions and 99 deletions

View File

@@ -169,7 +169,7 @@ const AutoCommands = Module("autocommands", {
cmd.toString = function toString() "-javascript " + cmd.source; cmd.toString = function toString() "-javascript " + cmd.source;
} }
else { else {
cmd = function cmd(args) dactyl.execute(commands.replaceTokens(cmd.source, args), null, true, cmd.sourcing); cmd = function cmd(args) commands.execute(cmd.source, args, false, null, cmd.sourcing);
cmd.sourcing = io.sourcing && update({}, io.sourcing); cmd.sourcing = io.sourcing && update({}, io.sourcing);
cmd.toString = function toString() cmd.source; cmd.toString = function toString() cmd.source;
} }

View File

@@ -412,6 +412,64 @@ const Commands = Module("commands", {
return res.join(" "); return res.join(" ");
}, },
/**
* Executes an Ex command script.
*
* @param {string} string A string containing the commands to execute.
* @param {object} tokens An optional object containing tokens to be
* interpolated into the command string.
* @param {object} args Optional arguments object to be passed to
* command actions.
* @param {object} sourcing An object containing information about
* the file that is being or has been sourced to obtain the
* command string.
*/
execute: function (string, tokens, silent, args, sourcing) {
io.withSavedValues(["readHeredoc", "sourcing"], function () {
this.sourcing = update({}, sourcing);
args = update({ setFrom: this.file }, args || {});
if (tokens)
string = commands.replaceTokens(string, tokens);
let lines = string.split(/\r\n|[\r\n]/);
this.readHeredoc = function (end) {
let res = [];
this.sourcing.line++;
while (++i < lines.length) {
if (lines[i] === end)
return res.join("\n");
res.push(lines[i]);
}
dactyl.assert(false, "Unexpected end of file waiting for " + end);
};
for (var i = 0; i < lines.length && !this.sourcing.finished; i++) {
// Deal with editors from Silly OSs.
let line = lines[i].replace(/\r$/, "");
this.sourcing.line = sourcing.line + i;
// Process escaped new lines
while (i < lines.length && /^\s*\\/.test(lines[i + 1]))
line += "\n" + lines[++i].replace(/^\s*\\/, "");
try {
dactyl.execute(line, args);
}
catch (e) {
if (!silent) {
dactyl.echoerr("Error detected while processing " + this.sourcing.file);
dactyl.echomsg("line\t" + this.sourcing.line + ":");
dactyl.reportError(e, true);
}
}
}
});
},
/** /**
* Returns the command with matching <b>name</b>. * Returns the command with matching <b>name</b>.
* *
@@ -595,9 +653,11 @@ const Commands = Module("commands", {
outer: outer:
while (i < str.length || complete) { while (i < str.length || complete) {
// skip whitespace var argStart = i;
while (/\s/.test(str[i]) && i < str.length) let re = /^\s*/gy;
i++; re.lastIndex = i;
i += re.exec(str)[0].length;
if (str[i] == "|") { if (str[i] == "|") {
args.string = str.slice(0, i); args.string = str.slice(0, i);
args.trailing = str.slice(i + 1); args.trailing = str.slice(i + 1);
@@ -700,14 +760,20 @@ const Commands = Module("commands", {
complete.highlight(i, sub.length, "SPELLCHECK"); complete.highlight(i, sub.length, "SPELLCHECK");
} }
if (args.length == literal) { if (args.length === literal) {
if (complete) if (complete)
args.completeArg = args.length; args.completeArg = args.length;
let re = /^(?:\s*(?=\n)|\s*)([^]*)/gy;
re.lastIndex = argStart || 0;
sub = re.exec(str)[1];
// Hack. // Hack.
if (sub.substr(0, 2) === "<<" && hereDoc) if (sub.substr(0, 2) === "<<" && hereDoc)
let ([count, arg] = getNextArg(sub)) { let ([count, arg] = getNextArg(sub)) {
sub = arg + sub.substr(count); sub = arg + sub.substr(count);
} }
args.literalArg = sub; args.literalArg = sub;
args.push(sub); args.push(sub);
args.quote = null; args.quote = null;
@@ -997,7 +1063,7 @@ const Commands = Module("commands", {
count: this.count && args.count count: this.count && args.count
}; };
dactyl.execute(commands.replaceTokens(this.replacementText, tokens), null, true, this.sourcing); commands.execute(this.replacementText, tokens, false, null, this.sourcing);
} }
// TODO: offer completion.ex? // TODO: offer completion.ex?

View File

@@ -153,7 +153,7 @@ const ConfigBase = Class(ModuleBase, {
Null color: blue; Null color: blue;
Number color: blue; Number color: blue;
Object color: maroon; Object color: maroon;
String color: green; String color: green; white-space: pre;
Key font-weight: bold; Key font-weight: bold;

View File

@@ -1994,7 +1994,7 @@ const Dactyl = Module("dactyl", {
dactyl.execute(init); dactyl.execute(init);
else { else {
if (rcFile) { if (rcFile) {
io.source(rcFile.path, true); io.source(rcFile.path, false);
services.get("environment").set("MY_" + config.idName + "RC", rcFile.path); services.get("environment").set("MY_" + config.idName + "RC", rcFile.path);
} }
else else

View File

@@ -319,13 +319,8 @@ lookup:
source: function (filename, silent) { source: function (filename, silent) {
defineModule.loadLog.push("sourcing " + filename); defineModule.loadLog.push("sourcing " + filename);
let time = Date.now(); let time = Date.now();
this.withSavedValues(["readHeredoc", "sourcing"], function () {
try { try {
var file = io.File(filename); var file = io.File(filename);
this.sourcing = {
file: file.path,
line: 0
};
if (!file.exists() || !file.isReadable() || file.isDirectory()) { if (!file.exists() || !file.isReadable() || file.isDirectory()) {
if (!silent) if (!silent)
@@ -358,40 +353,8 @@ lookup:
else if (/\.css$/.test(filename)) else if (/\.css$/.test(filename))
storage.styles.registerSheet(uri.spec, false, true); storage.styles.registerSheet(uri.spec, false, true);
else { else {
let lines = file.read().split(/\r\n|[\r\n]/); commands.execute(file.read(), null, silent, null,
{ file: file.path, line: 1 });
this.readHeredoc = function (end) {
let res = [];
io.sourcing.line++;
while (++i < lines.length) {
if (lines[i] === end)
return res.join("\n");
res.push(lines[i]);
}
dactyl.assert(false, "Unexpected end of file waiting for " + end);
};
for (var i = 0; i < lines.length && !this.sourcing.finished; i++) {
// Deal with editors from Silly OSs.
let line = lines[i].replace(/\r$/, "");
this.sourcing.line = i + 1;
// Process escaped new lines
while (i < lines.length && /^\s*\\/.test(lines[i + 1]))
line += "\n" + lines[++i].replace(/^\s*\\/, "");
try {
dactyl.execute(line, { setFrom: file });
}
catch (e) {
if (!silent) {
dactyl.echoerr("Error detected while processing " + file.path);
dactyl.echomsg("line\t" + this.sourcing.line + ":");
dactyl.reportError(e, true);
}
}
}
} }
if (this._scriptNames.indexOf(file.path) == -1) if (this._scriptNames.indexOf(file.path) == -1)
@@ -411,7 +374,6 @@ lookup:
finally { finally {
defineModule.loadLog.push("done sourcing " + filename + ": " + (Date.now() - time) + "ms"); defineModule.loadLog.push("done sourcing " + filename + ": " + (Date.now() - time) + "ms");
} }
});
}, },
// TODO: when https://bugzilla.mozilla.org/show_bug.cgi?id=68702 is // TODO: when https://bugzilla.mozilla.org/show_bug.cgi?id=68702 is

View File

@@ -384,8 +384,8 @@ const Mappings = Module("mappings", {
} }
else if (args["-ex"]) { else if (args["-ex"]) {
rhs = ["-ex", rhs]; rhs = ["-ex", rhs];
action = function action(count) action = function action(count) commands.execute(rhs[1], { count: count },
dactyl.execute(commands.replaceTokens(rhs[1], { count: count }), null, true, action.sourcing); false, null, action.sourcing);
action.sourcing = io.sourcing && update({}, io.sourcing); action.sourcing = io.sourcing && update({}, io.sourcing);
} }
else { else {