1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-20 09:48:00 +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;
dactyl.echomsg("autocommand " + autoCmd.command, 9);
if (typeof autoCmd.command == "function") {
try {
autoCmd.command.call(autoCmd, args);
}
catch (e) {
dactyl.echoerr(e);
}
}
else
dactyl.execute(commands.replaceTokens(autoCmd.command, args), null, true);
autoCmd.command(args);
}
}
}
@@ -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.bang)
autocommands.remove(event, regex);
if (args["-javascript"])
if (args["-javascript"]) {
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);
}
else {

View File

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

View File

@@ -997,7 +997,7 @@ const Commands = Module("commands", {
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?
@@ -1062,7 +1062,8 @@ const Commands = Module("commands", {
bang: bangOpt,
count: countOpt,
completer: completeFunc,
replacementText: args.literalArg
replacementText: args.literalArg,
sourcing: io.sourcing && update({}, io.sourcing)
}, args.bang);
if (!added)

View File

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

View File

@@ -412,7 +412,7 @@ const RangeFind = Class("RangeFind", {
},
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 {
this.range = this.ranges[0];
this.lastRange = null;

View File

@@ -317,10 +317,9 @@ lookup:
* @param {boolean} silent Whether errors should be reported.
*/
source: function (filename, silent) {
let wasSourcing = this.sourcing;
let readHeredoc = this.readHeredoc;
defineModule.loadLog.push("sourcing " + filename);
let time = Date.now();
this.withSavedValues(["readHeredoc", "sourcing"], function () {
try {
var file = io.File(filename);
this.sourcing = {
@@ -412,9 +411,8 @@ lookup:
}
finally {
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

View File

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

View File

@@ -764,6 +764,16 @@ Class.prototype = {
*/
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 + "]",
/**