diff --git a/NEWS b/NEWS index 3ab8a802..acb38a78 100644 --- a/NEWS +++ b/NEWS @@ -9,6 +9,8 @@ (change with userChrome.css if you don't like it until we have :colorscheme) * :let mapleader="," and in :map support * added new :let and :unlet commands + * :b2 now allowed, no space required before the 2 anymore + * :! to run commands through system() * separated search and Ex command history * added 'visualbellstyle' for styling/hiding the visual bell * merge the existing status bar with the standard FF status bar so that diff --git a/content/commands.js b/content/commands.js index dfe6db40..7d2d044f 100644 --- a/content/commands.js +++ b/content/commands.js @@ -42,7 +42,7 @@ function Command(specs, action, extra_info) //{{{ for (var i = 0; i < specs.length; i++) { var match; - if (match = specs[i].match(/(\w+)\[(\w+)\]/)) + if (match = specs[i].match(/(\w+|!)\[(\w+)\]/)) { short_names.push(match[1]); long_names.push(match[1] + match[2]); @@ -114,7 +114,7 @@ Command.prototype.hasName = function(name) { if (this.specs[i] == name) // literal command name return true; - else if (this.specs[i].match(/^\w+\[\w+\]$/)) // abbreviation spec + else if (this.specs[i].match(/^(\w+|!)\[\w+\]$/)) // abbreviation spec if (matchAbbreviation(name, this.specs[i])) return true; } @@ -194,7 +194,7 @@ function Commands() //{{{ } // 0 - count, 1 - cmd, 2 - special, 3 - args, 4 - heredoc tag - var matches = string.match(/^:*(\d+)?([a-zA-Z]+)(!)?(?:\s+(.*?)\s*)?$/); + var matches = string.match(/^:*(\d+)?([a-zA-Z]+|!)(!)?(?:\s*(.*?)\s*)?$/); if (!matches) return [null, null, null, null, null]; matches.shift(); @@ -1629,6 +1629,31 @@ function Commands() //{{{ short_help: "Set zoom value of the web page", help: "If {value} can be an absolute value between 1 and 2000% or a relative value if prefixed with - or +. " + "If {value} is omitted, zoom is reset to 100%." + }, + { + usage: ["zo[om][!] [value]", "zo[om][!] +{value} | -{value}"], + short_help: "Set zoom value of current web page", + help: "If {value} can be an absolute value between 1 and 2000% or a relative value if prefixed with - or +. " + + "If {value} is omitted, zoom is reset to 100%.
" + + "Normally this command operates on the text zoom, if used with [!] it operates on full zoom." + } + )); + addDefaultCommand(new Command(["!", "run"], + function(args, special) + { + // TODO: if special, run the last command + var output = vimperator.system(args) + if (typeof output === "string") + 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}"], + short_help: "Run a command", + help: "Runs {command} through system() and displays its output. " + + "Input redirection (< foo) not done, do not run commands which require stdin or it will hang Firefox!" } )); //}}} diff --git a/content/vimperator.js b/content/vimperator.js index d73ffe1c..f33931cb 100644 --- a/content/vimperator.js +++ b/content/vimperator.js @@ -629,6 +629,89 @@ const vimperator = (function() //{{{ .quit(nsIAppStartup.eRestart | nsIAppStartup.eAttemptQuit); }, + run: function(program, args, blocking) + { + const WINDOWS = navigator.platform == "Win32"; + + var file = Components.classes["@mozilla.org/file/local;1"]. + createInstance(Components.interfaces.nsILocalFile); + try + { + file.initWithPath(program); + } + catch (e) + { + var dirs = environment_service.get("PATH").split(WINDOWS ? ";" : ":"); + for (var i = 0; i < dirs.length; i++) + { + var path = dirs[i] + (WINDOWS ? "\\" : "/") + program; + try + { + file.initWithPath(path); + if (file.exists()) + break; + } + catch (e) { } + } + } + if (!file.exists()) + { + vimperator.echoerr("command not found: " + program); + return -1; + } + + var process = Components.classes["@mozilla.org/process/util;1"]. + createInstance(Components.interfaces.nsIProcess); + process.init(file); + + var ec = process.run(blocking, args, args.length); + return ec; + }, + + // when https://bugzilla.mozilla.org/show_bug.cgi?id=68702 is fixed + // is fixed, should use that instead of a tmpfile + // TODO: pass "input" as stdin + // TODO: add shell/shellcmdflag options to replace "sh" and "-c" + system: function (str, input) + { + const WINDOWS = navigator.platform == "Win32"; // FIXME: duplicated everywhere + + var fileout = getTempFile(); + if (!fileout) + return ""; + + if (WINDOWS) + var command = str + " > " + fileout.path; + else + var command = str + " > \"" + fileout.path.replace('"', '\\"') + "\""; + + var filein = null; + if (input) + { + filein = getTempFile(); + var fdin = vimperator.fopen(filein, ">"); + fdin.write(input); + fdin.close(); + command += " < \"" + filein.path.replace('"', '\\"') + "\""; + } + + if (WINDOWS) + this.run("cmd.exe", ["/C", command], true); + else + this.run("sh", ["-c", command], true); + + var fd = vimperator.fopen(fileout, "<"); + if (!fd) + return null; + + var s = fd.read(); + fd.close(); + fileout.remove(false); + if (filein) + filein.remove(false); + + return s; + }, // files which end in .js are sourced as pure javascript files, // no need (actually forbidden) to add: js <