1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-20 22:47:59 +01:00

Preserve file/line information with saved ex commands (:au, :com, :map -ex).

This commit is contained in:
Kris Maglione
2010-10-05 15:29:18 -04:00
parent 2ee74581d1
commit 7f574a512f
8 changed files with 126 additions and 119 deletions

View File

@@ -128,16 +128,7 @@ const AutoCommands = Module("autocommands", {
lastPattern = autoCmd.pattern; lastPattern = autoCmd.pattern;
dactyl.echomsg("autocommand " + autoCmd.command, 9); dactyl.echomsg("autocommand " + autoCmd.command, 9);
if (typeof autoCmd.command == "function") { autoCmd.command(args);
try {
autoCmd.command.call(autoCmd, args);
}
catch (e) {
dactyl.echoerr(e);
}
}
else
dactyl.execute(commands.replaceTokens(autoCmd.command, args), null, true);
} }
} }
} }
@@ -173,8 +164,16 @@ const AutoCommands = Module("autocommands", {
if (args.length > 2) { // add new command, possibly removing all others with the same event/pattern if (args.length > 2) { // add new command, possibly removing all others with the same event/pattern
if (args.bang) if (args.bang)
autocommands.remove(event, regex); autocommands.remove(event, regex);
if (args["-javascript"]) if (args["-javascript"]) {
cmd = dactyl.userFunc("args", "with(args) {" + cmd + "}"); cmd = dactyl.userFunc("args", "with(args) {" + cmd + "}");
cmd.toString = function toString() "-javascript " + cmd.source;
}
else {
cmd = function cmd(args) dactyl.execute(commands.replaceTokens(cmd.source, args), null, true, cmd.sourcing);
cmd.sourcing = io.sourcing && update({}, io.sourcing);
cmd.toString = function toString() cmd.source;
}
cmd.source = args[2];
autocommands.add(events, regex, cmd); autocommands.add(events, regex, cmd);
} }
else { else {

View File

@@ -404,14 +404,10 @@ const CommandLine = Module("commandline", {
}, },
runSilently: function (func, self) { runSilently: function (func, self) {
let wasSilent = this._silent; this.withSavedValues(["_silent"], function () {
this._silent = true; this._silent = true;
try {
func.call(self); func.call(self);
} });
finally {
this._silent = wasSilent;
}
}, },
hideCompletions: function () { hideCompletions: function () {

View File

@@ -997,7 +997,7 @@ const Commands = Module("commands", {
count: this.count && args.count count: this.count && args.count
}; };
dactyl.execute(commands.replaceTokens(this.replacementText, tokens)); dactyl.execute(commands.replaceTokens(this.replacementText, tokens), null, true, this.sourcing);
} }
// TODO: offer completion.ex? // TODO: offer completion.ex?
@@ -1062,7 +1062,8 @@ const Commands = Module("commands", {
bang: bangOpt, bang: bangOpt,
count: countOpt, count: countOpt,
completer: completeFunc, completer: completeFunc,
replacementText: args.literalArg replacementText: args.literalArg,
sourcing: io.sourcing && update({}, io.sourcing)
}, args.bang); }, args.bang);
if (!added) if (!added)

View File

@@ -330,7 +330,7 @@ const Dactyl = Module("dactyl", {
*/ */
userFunc: function () { userFunc: function () {
return this.userEval( return this.userEval(
"(function (" + "(function userFunction(" +
Array.slice(arguments, 0, -1).join(", ") + Array.slice(arguments, 0, -1).join(", ") +
") { " + arguments[arguments.length - 1] + " })"); ") { " + arguments[arguments.length - 1] + " })");
}, },
@@ -344,7 +344,7 @@ const Dactyl = Module("dactyl", {
* @param {boolean} silent Whether the command should be echoed on the * @param {boolean} silent Whether the command should be echoed on the
* command line. * command line.
*/ */
execute: function (str, modifiers, silent) { execute: function (str, modifiers, silent, sourcing) {
// skip comments and blank lines // skip comments and blank lines
if (/^\s*("|$)/.test(str)) if (/^\s*("|$)/.test(str))
return; return;
@@ -359,7 +359,10 @@ const Dactyl = Module("dactyl", {
if (!silent) if (!silent)
commandline.command = str.replace(/^\s*:\s*/, ""); commandline.command = str.replace(/^\s*:\s*/, "");
io.withSavedValues(["sourcing"], function () {
io.sourcing = sourcing || io.sourcing;
command.execute(args, modifiers); command.execute(args, modifiers);
});
} }
}, },

View File

@@ -412,7 +412,7 @@ const RangeFind = Class("RangeFind", {
}, },
iter: function (word) { iter: function (word) {
let saved = ["range", "lastRange", "lastString"].map(function (s) [s, this[s]], this); let saved = ["lastRange", "lastString", "range"].map(function (s) [s, this[s]], this);
try { try {
this.range = this.ranges[0]; this.range = this.ranges[0];
this.lastRange = null; this.lastRange = null;

View File

@@ -317,10 +317,9 @@ lookup:
* @param {boolean} silent Whether errors should be reported. * @param {boolean} silent Whether errors should be reported.
*/ */
source: function (filename, silent) { source: function (filename, silent) {
let wasSourcing = this.sourcing;
let readHeredoc = this.readHeredoc;
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 = { this.sourcing = {
@@ -412,9 +411,8 @@ lookup:
} }
finally { finally {
defineModule.loadLog.push("done sourcing " + filename + ": " + (Date.now() - time) + "ms"); defineModule.loadLog.push("done sourcing " + filename + ": " + (Date.now() - time) + "ms");
this.sourcing = wasSourcing;
this.readHeredoc = readHeredoc;
} }
});
}, },
// 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,9 +384,9 @@ const Mappings = Module("mappings", {
} }
else if (args["-ex"]) { else if (args["-ex"]) {
rhs = ["-ex", rhs]; rhs = ["-ex", rhs];
action = function (count) { action = function action(count)
dactyl.execute(commands.replaceTokens(rhs[1], { count: count })); dactyl.execute(commands.replaceTokens(rhs[1], { count: count }), null, true, action.sourcing);
}; action.sourcing = io.sourcing && update({}, io.sourcing);
} }
else { else {
rhs = [events.canonicalKeys(rhs)]; rhs = [events.canonicalKeys(rhs)];

View File

@@ -764,6 +764,16 @@ Class.prototype = {
*/ */
init: function () {}, init: function () {},
withSavedValues: function (names, callback, self) {
let vals = names.map(function (name) this[name], this);
try {
return callback.call(self || this);
}
finally {
names.forEach(function (name, i) this[name] = vals[i], this);
}
},
toString: function () "[instance " + this.constructor.className + "]", toString: function () "[instance " + this.constructor.className + "]",
/** /**