1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-22 17:27:57 +01:00

add :!! support and don't show error on :!non-existing-command (as the non-existing-command can't be known if it really does not exist)

This commit is contained in:
Martin Stubenschrott
2007-10-09 15:49:18 +00:00
parent 35f1205176
commit 46df40ef03
2 changed files with 32 additions and 17 deletions

View File

@@ -129,6 +129,7 @@ function Commands() //{{{
/////////////////////////////////////////////////////////////////////////////{{{ /////////////////////////////////////////////////////////////////////////////{{{
var ex_commands = []; var ex_commands = [];
var last_run_command = ""; // updated whenever the users runs a command with :!
function addDefaultCommand(command) function addDefaultCommand(command)
{ {
@@ -177,7 +178,6 @@ function Commands() //{{{
return null; return null;
} }
// FIXME: doesn't really belong here... // FIXME: doesn't really belong here...
// return [null, null, null, null, heredoc_tag || false]; // return [null, null, null, null, heredoc_tag || false];
// [count, cmd, special, args] = match; // [count, cmd, special, args] = match;
@@ -1764,19 +1764,25 @@ function Commands() //{{{
addDefaultCommand(new Command(["!", "run"], addDefaultCommand(new Command(["!", "run"],
function(args, special) function(args, special)
{ {
// TODO: if special, run the last command // :!! needs to be treated specially as the command parser sets the special flag but removes the ! from args
if (special)
args = "!" + (args || "");
// TODO: better escaping of ! to also substitute \\! correctly
args = args.replace(/(^|[^\\])!/g, "$1" + last_run_command);
last_run_command = args;
var output = vimperator.system(args) var output = vimperator.system(args)
if (typeof output === "string") if (output)
vimperator.echo(vimperator.util.escapeHTML(output)); vimperator.echo(vimperator.util.escapeHTML(output));
else
// FIXME: why are we accepting only a string return value from v.system()? -- djk
vimperator.echoerr("Invalid system command: " + args);
}, },
{ {
usage: ["!{command}"], usage: ["!{cmd}"],
short_help: "Run a command", short_help: "Run a command",
help: "Runs {command} through system() and displays its output. " + help: "Runs <code class=\"argument\">{cmd}</code> through system() and displays its output. " +
"Input redirection (< foo) not done, do not run commands which require stdin or it will hang Firefox!" "Any '!' in <code class=\"argument\">{cmd}</code> is replaced with the previous external command. " +
"But not when there is a backslash before the '!', then that backslash is removed.<br/>" +
"Input redirection (< foo) not done, also do not run commands which require stdin or it will hang Firefox!"
} }
)); ));
//}}} //}}}

View File

@@ -543,10 +543,15 @@ const vimperator = (function() //{{{
run: function(program, args, blocking) run: function(program, args, blocking)
{ {
var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
const WINDOWS = navigator.platform == "Win32"; const WINDOWS = navigator.platform == "Win32";
var file = Components.classes["@mozilla.org/file/local;1"]. if (!args)
createInstance(Components.interfaces.nsILocalFile); args = [];
if (typeof(blocking) != "boolean")
blocking = false;
try try
{ {
file.initWithPath(program); file.initWithPath(program);
@@ -566,14 +571,14 @@ const vimperator = (function() //{{{
catch (e) { } catch (e) { }
} }
} }
if (!file.exists()) if (!file.exists())
{ {
vimperator.echoerr("command not found: " + program); vimperator.echoerr("command not found: " + program);
return -1; return -1;
} }
var process = Components.classes["@mozilla.org/process/util;1"]. var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
createInstance(Components.interfaces.nsIProcess);
process.init(file); process.init(file);
var ec = process.run(blocking, args, args.length); var ec = process.run(blocking, args, args.length);
@@ -582,7 +587,6 @@ const vimperator = (function() //{{{
// when https://bugzilla.mozilla.org/show_bug.cgi?id=68702 is fixed // when https://bugzilla.mozilla.org/show_bug.cgi?id=68702 is fixed
// is fixed, should use that instead of a tmpfile // is fixed, should use that instead of a tmpfile
// TODO: pass "input" as stdin
// TODO: add shell/shellcmdflag options to replace "sh" and "-c" // TODO: add shell/shellcmdflag options to replace "sh" and "-c"
system: function (str, input) system: function (str, input)
{ {
@@ -607,14 +611,15 @@ const vimperator = (function() //{{{
command += " < \"" + filein.path.replace('"', '\\"') + "\""; command += " < \"" + filein.path.replace('"', '\\"') + "\"";
} }
var res;
if (WINDOWS) if (WINDOWS)
this.run("cmd.exe", ["/C", command], true); res = this.run("cmd.exe", ["/C", command], true);
else else
this.run("sh", ["-c", command], true); res = this.run("sh", ["-c", command], true);
var fd = vimperator.fopen(fileout, "<"); var fd = vimperator.fopen(fileout, "<");
if (!fd) if (!fd)
return null; return "";
var s = fd.read(); var s = fd.read();
fd.close(); fd.close();
@@ -622,6 +627,10 @@ const vimperator = (function() //{{{
if (filein) if (filein)
filein.remove(false); filein.remove(false);
// if there is only one \n at the end, chop it off
if (s && s.indexOf("\n") == s.length-1)
s = s.substr(0, s.length-1);
return s; return s;
}, },