From 8c100b78bac8173f6ce9e173594c77b0abe7ba27 Mon Sep 17 00:00:00 2001 From: Martin Stubenschrott Date: Tue, 5 Feb 2008 00:12:18 +0000 Subject: [PATCH] moved run/system/source to vimperator.io and objectToString to vimp.util --- content/buffers.js | 2 +- content/commands.js | 8 +- content/editor.js | 2 +- content/events.js | 53 +-- content/io.js | 161 +++++++++ content/muttator.xul | 4 +- content/options.js | 9 + content/util.js | 192 +++++++---- content/vimperator.js | 761 +---------------------------------------- content/vimperator.xul | 2 + 10 files changed, 336 insertions(+), 858 deletions(-) diff --git a/content/buffers.js b/content/buffers.js index b0b387c6..48793d5f 100644 --- a/content/buffers.js +++ b/content/buffers.js @@ -687,7 +687,7 @@ vimperator.Buffer = function () //{{{ // TODO: print more useful information, just like the DOM inspector showElementInfo: function (elem) { - vimperator.echo("Element:
" + vimperator.objectToString(elem), vimperator.commandline.FORCE_MULTILINE); + vimperator.echo("Element:
" + vimperator.util.objectToString(elem), vimperator.commandline.FORCE_MULTILINE); }, showPageInfo: function (verbose) diff --git a/content/commands.js b/content/commands.js index 303d533f..c0ac34eb 100644 --- a/content/commands.js +++ b/content/commands.js @@ -926,7 +926,7 @@ vimperator.Commands = function () //{{{ } if (typeof arg === "object") - arg = vimperator.objectToString(arg, color); + arg = vimperator.util.objectToString(arg, color); else if (typeof arg === "function") arg = vimperator.util.escapeHTML(arg.toString()); else if (typeof arg === "number" || typeof arg === "boolean") @@ -2081,7 +2081,7 @@ vimperator.Commands = function () //{{{ // return; //} - vimperator.source(args, special); + vimperator.io.source(args, special); }, { shortHelp: "Read Ex commands from {file}", @@ -2471,7 +2471,7 @@ vimperator.Commands = function () //{{{ var prog = args.shift(); args.push(url) - vimperator.callFunctionInThread(newThread, vimperator.run, [prog, args, true]); + vimperator.callFunctionInThread(newThread, vimperator.io.run, [prog, args, true]); } else { @@ -2572,7 +2572,7 @@ vimperator.Commands = function () //{{{ args = args.replace(/(^|[^\\])!/g, "$1" + lastRunCommand); lastRunCommand = args; - var output = vimperator.system(args); + var output = vimperator.io.system(args); if (output) vimperator.echo(vimperator.util.escapeHTML(output)); }, diff --git a/content/editor.js b/content/editor.js index 5f462d30..c96fb89b 100644 --- a/content/editor.js +++ b/content/editor.js @@ -364,7 +364,7 @@ vimperator.Editor = function () //{{{ textBox.style.backgroundColor = "#bbbbbb"; var newThread = Components.classes["@mozilla.org/thread-manager;1"].getService().newThread(0); // TODO: save return value in v:shell_error - vimperator.callFunctionInThread(newThread, vimperator.run, [prog, args, true]); + vimperator.callFunctionInThread(newThread, vimperator.io.run, [prog, args, true]); textBox.removeAttribute("readonly"); diff --git a/content/events.js b/content/events.js index 82b03923..1e5d1167 100644 --- a/content/events.js +++ b/content/events.js @@ -185,33 +185,36 @@ vimperator.Events = function () //{{{ // any tab related events var tabcontainer = getBrowser().mTabContainer; - tabcontainer.addEventListener("TabMove", function (event) + if (tabcontainer) // not every VIM-like extension has a tab container { - vimperator.statusline.updateTabCount(); - vimperator.buffer.updateBufferList(); - }, false); - tabcontainer.addEventListener("TabOpen", function (event) - { - vimperator.statusline.updateTabCount(); - vimperator.buffer.updateBufferList(); - }, false); - tabcontainer.addEventListener("TabClose", function (event) - { - vimperator.statusline.updateTabCount(); - vimperator.buffer.updateBufferList(); - }, false); - tabcontainer.addEventListener("TabSelect", function (event) - { - // TODO: is all of that necessary? - vimperator.modes.reset(); - vimperator.commandline.clear(); - vimperator.modes.show(); - vimperator.statusline.updateTabCount(); - vimperator.buffer.updateBufferList(); - vimperator.tabs.updateSelectionHistory(); + tabcontainer.addEventListener("TabMove", function (event) + { + vimperator.statusline.updateTabCount(); + vimperator.buffer.updateBufferList(); + }, false); + tabcontainer.addEventListener("TabOpen", function (event) + { + vimperator.statusline.updateTabCount(); + vimperator.buffer.updateBufferList(); + }, false); + tabcontainer.addEventListener("TabClose", function (event) + { + vimperator.statusline.updateTabCount(); + vimperator.buffer.updateBufferList(); + }, false); + tabcontainer.addEventListener("TabSelect", function (event) + { + // TODO: is all of that necessary? + vimperator.modes.reset(); + vimperator.commandline.clear(); + vimperator.modes.show(); + vimperator.statusline.updateTabCount(); + vimperator.buffer.updateBufferList(); + vimperator.tabs.updateSelectionHistory(); - setTimeout(function () { vimperator.focusContent(true); }, 10); // just make sure, that no widget has focus - }, false); + setTimeout(function () { vimperator.focusContent(true); }, 10); // just make sure, that no widget has focus + }, false); + } // this adds an event which is is called on each page load, even if the // page is loaded in a background tab diff --git a/content/io.js b/content/io.js index 5bfbccb7..5318fc8e 100644 --- a/content/io.js +++ b/content/io.js @@ -308,6 +308,167 @@ vimperator.IO = function () //{{{ ocstream.close(); ofstream.close(); + }, + + run: function (program, args, blocking) + { + var file = Components.classes["@mozilla.org/file/local;1"]. + createInstance(Components.interfaces.nsILocalFile); + + if (!args) + args = []; + + if (typeof blocking != "boolean") + blocking = false; + + try + { + file.initWithPath(program); + } + catch (e) + { + var dirs = environmentService.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: add shell/shellcmdflag options to replace "sh" and "-c" + system: function (str, input) + { + var fileout = this.createTempFile(); + if (!fileout) + return ""; + + if (WINDOWS) + var command = str + " > " + fileout.path; + else + var command = str + " > \"" + fileout.path.replace('"', '\\"') + "\""; + + var filein = null; + if (input) + { + filein = this.createTempFile(); + this.writeFile(filein, input); + command += " < \"" + filein.path.replace('"', '\\"') + "\""; + } + + var res; + if (WINDOWS) + res = this.run("cmd.exe", ["/C", command], true); + else + res = this.run("sh", ["-c", command], true); + + var output = this.readFile(fileout); + fileout.remove(false); + if (filein) + filein.remove(false); + + // if there is only one \n at the end, chop it off + if (output && output.indexOf("\n") == output.length - 1) + output = output.substr(0, output.length - 1); + + return output; + }, + + // files which end in .js are sourced as pure javascript files, + // no need (actually forbidden) to add: js < -