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

moved run/system/source to vimperator.io and objectToString to vimp.util

This commit is contained in:
Martin Stubenschrott
2008-02-05 00:12:18 +00:00
parent cb7fef72ba
commit 8c100b78ba
10 changed files with 336 additions and 858 deletions

View File

@@ -687,7 +687,7 @@ vimperator.Buffer = function () //{{{
// TODO: print more useful information, just like the DOM inspector // TODO: print more useful information, just like the DOM inspector
showElementInfo: function (elem) showElementInfo: function (elem)
{ {
vimperator.echo("Element:<br/>" + vimperator.objectToString(elem), vimperator.commandline.FORCE_MULTILINE); vimperator.echo("Element:<br/>" + vimperator.util.objectToString(elem), vimperator.commandline.FORCE_MULTILINE);
}, },
showPageInfo: function (verbose) showPageInfo: function (verbose)

View File

@@ -926,7 +926,7 @@ vimperator.Commands = function () //{{{
} }
if (typeof arg === "object") if (typeof arg === "object")
arg = vimperator.objectToString(arg, color); arg = vimperator.util.objectToString(arg, color);
else if (typeof arg === "function") else if (typeof arg === "function")
arg = vimperator.util.escapeHTML(arg.toString()); arg = vimperator.util.escapeHTML(arg.toString());
else if (typeof arg === "number" || typeof arg === "boolean") else if (typeof arg === "number" || typeof arg === "boolean")
@@ -2081,7 +2081,7 @@ vimperator.Commands = function () //{{{
// return; // return;
//} //}
vimperator.source(args, special); vimperator.io.source(args, special);
}, },
{ {
shortHelp: "Read Ex commands from {file}", shortHelp: "Read Ex commands from {file}",
@@ -2471,7 +2471,7 @@ vimperator.Commands = function () //{{{
var prog = args.shift(); var prog = args.shift();
args.push(url) args.push(url)
vimperator.callFunctionInThread(newThread, vimperator.run, [prog, args, true]); vimperator.callFunctionInThread(newThread, vimperator.io.run, [prog, args, true]);
} }
else else
{ {
@@ -2572,7 +2572,7 @@ vimperator.Commands = function () //{{{
args = args.replace(/(^|[^\\])!/g, "$1" + lastRunCommand); args = args.replace(/(^|[^\\])!/g, "$1" + lastRunCommand);
lastRunCommand = args; lastRunCommand = args;
var output = vimperator.system(args); var output = vimperator.io.system(args);
if (output) if (output)
vimperator.echo(vimperator.util.escapeHTML(output)); vimperator.echo(vimperator.util.escapeHTML(output));
}, },

View File

@@ -364,7 +364,7 @@ vimperator.Editor = function () //{{{
textBox.style.backgroundColor = "#bbbbbb"; textBox.style.backgroundColor = "#bbbbbb";
var newThread = Components.classes["@mozilla.org/thread-manager;1"].getService().newThread(0); var newThread = Components.classes["@mozilla.org/thread-manager;1"].getService().newThread(0);
// TODO: save return value in v:shell_error // 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"); textBox.removeAttribute("readonly");

View File

@@ -185,33 +185,36 @@ vimperator.Events = function () //{{{
// any tab related events // any tab related events
var tabcontainer = getBrowser().mTabContainer; var tabcontainer = getBrowser().mTabContainer;
tabcontainer.addEventListener("TabMove", function (event) if (tabcontainer) // not every VIM-like extension has a tab container
{ {
vimperator.statusline.updateTabCount(); tabcontainer.addEventListener("TabMove", function (event)
vimperator.buffer.updateBufferList(); {
}, false); vimperator.statusline.updateTabCount();
tabcontainer.addEventListener("TabOpen", function (event) vimperator.buffer.updateBufferList();
{ }, false);
vimperator.statusline.updateTabCount(); tabcontainer.addEventListener("TabOpen", function (event)
vimperator.buffer.updateBufferList(); {
}, false); vimperator.statusline.updateTabCount();
tabcontainer.addEventListener("TabClose", function (event) vimperator.buffer.updateBufferList();
{ }, false);
vimperator.statusline.updateTabCount(); tabcontainer.addEventListener("TabClose", function (event)
vimperator.buffer.updateBufferList(); {
}, false); vimperator.statusline.updateTabCount();
tabcontainer.addEventListener("TabSelect", function (event) vimperator.buffer.updateBufferList();
{ }, false);
// TODO: is all of that necessary? tabcontainer.addEventListener("TabSelect", function (event)
vimperator.modes.reset(); {
vimperator.commandline.clear(); // TODO: is all of that necessary?
vimperator.modes.show(); vimperator.modes.reset();
vimperator.statusline.updateTabCount(); vimperator.commandline.clear();
vimperator.buffer.updateBufferList(); vimperator.modes.show();
vimperator.tabs.updateSelectionHistory(); vimperator.statusline.updateTabCount();
vimperator.buffer.updateBufferList();
vimperator.tabs.updateSelectionHistory();
setTimeout(function () { vimperator.focusContent(true); }, 10); // just make sure, that no widget has focus setTimeout(function () { vimperator.focusContent(true); }, 10); // just make sure, that no widget has focus
}, false); }, false);
}
// this adds an event which is is called on each page load, even if the // this adds an event which is is called on each page load, even if the
// page is loaded in a background tab // page is loaded in a background tab

View File

@@ -308,6 +308,167 @@ vimperator.IO = function () //{{{
ocstream.close(); ocstream.close();
ofstream.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 <<EOF ... EOF around those files
source: function (filename, silent)
{
try
{
var file = this.getFile(filename);
if (!file.exists())
{
if (!silent)
vimperator.echoerr("E484: Can't open file " + filename);
return false;
}
var str = this.readFile(filename);
// handle pure javascript files specially
if (/\.js$/.test(filename))
{
eval("with(vimperator){" + str + "}");
}
else
{
var heredoc = "";
var heredocEnd = null; // the string which ends the heredoc
str.split("\n").forEach(function (line)
{
if (heredocEnd) // we already are in a heredoc
{
if (heredocEnd.test(line))
{
eval("with(vimperator){" + heredoc + "}");
heredoc = "";
heredocEnd = null;
}
else
{
heredoc += line + "\n";
}
}
else
{
// check for a heredoc
var [count, cmd, special, args] = vimperator.commands.parseCommand(line);
var command = vimperator.commands.get(cmd);
if (command && command.name == "javascript")
{
var matches = args.match(/(.*)<<\s*([^\s]+)$/);
if (matches)
{
heredocEnd = new RegExp("^" + matches[2] + "$", "m");
if (matches[1])
heredoc = matches[1] + "\n";
}
else
{
command.execute(args, special, count);
}
}
else
{
// execute a normal vimperator command
vimperator.execute(line);
}
}
});
}
vimperator.log("Sourced: " + filename, 3);
}
catch (e)
{
if (!silent)
vimperator.echoerr(e);
}
} }
}; };
//}}} //}}}

View File

@@ -37,7 +37,9 @@ the terms of any one of the MPL, the GPL or the LGPL.
xmlns:html="http://www.w3.org/1999/xhtml" xmlns:html="http://www.w3.org/1999/xhtml"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/x-javascript;version=1.8" src="vimperator.js"/> <script type="application/x-javascript;version=1.8" src="vim.js"/>
<script type="application/x-javascript;version=1.8" src="muttator.js"/>
<script type="application/x-javascript;version=1.8" src="bookmarks.js"/> <script type="application/x-javascript;version=1.8" src="bookmarks.js"/>
<script type="application/x-javascript;version=1.8" src="buffers.js"/> <script type="application/x-javascript;version=1.8" src="buffers.js"/>
<script type="application/x-javascript;version=1.8" src="commands.js"/> <script type="application/x-javascript;version=1.8" src="commands.js"/>

View File

@@ -420,6 +420,9 @@ vimperator.Options = function () //{{{
// this hack is only needed, because we need to do asynchronous loading of the .vimperatorrc // this hack is only needed, because we need to do asynchronous loading of the .vimperatorrc
setInitialGUI: function () setInitialGUI: function ()
{ {
if (vimperator.config.name != "Vimperator")
return;
if (!guioptionsDone) if (!guioptionsDone)
this.get("guioptions").reset(); this.get("guioptions").reset();
if (!laststatusDone) if (!laststatusDone)
@@ -734,6 +737,12 @@ vimperator.Options = function () //{{{
// we start with an "empty" GUI so that no toolbars or tabbar is shown if the user // we start with an "empty" GUI so that no toolbars or tabbar is shown if the user
// sets them to empty in the .vimperatorrc, which is sourced asynchronously // sets them to empty in the .vimperatorrc, which is sourced asynchronously
if (vimperator.config.name != "Vimperator")
alert("mooh");
else alert("maeh");
if (vimperator.config.name != "Vimperator")
return optionManager;
setShowTabline(0); setShowTabline(0);
setGuiOptions(""); setGuiOptions("");
setLastStatus(0); setLastStatus(0);

View File

@@ -28,17 +28,6 @@ the terms of any one of the MPL, the GPL or the LGPL.
vimperator.util = { //{{{ vimperator.util = { //{{{
escapeHTML: function (str)
{
// XXX: the following code is _much_ slower than a simple .replace()
// :history display went down from 2 to 1 second after changing
//
// var e = window.content.document.createElement("div");
// e.appendChild(window.content.document.createTextNode(str));
// return e.innerHTML;
return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
},
// TODO: use :highlight color groups // TODO: use :highlight color groups
// if "processStrings" is true, any passed strings will be surrounded by " and // if "processStrings" is true, any passed strings will be surrounded by " and
// any line breaks are displayed as \n // any line breaks are displayed as \n
@@ -90,65 +79,15 @@ vimperator.util = { //{{{
return "&lt;unknown type&gt;"; return "&lt;unknown type&gt;";
}, },
// takes a string like 'google bla, www.osnews.com' escapeHTML: function (str)
// and returns an array ['www.google.com/search?q=bla', 'www.osnews.com']
stringToURLArray: function (str)
{ {
var urls = str.split(/\s*\,\s+/); // XXX: the following code is _much_ slower than a simple .replace()
// :history display went down from 2 to 1 second after changing
begin: for (var url = 0; url < urls.length; url++) //
{ // var e = window.content.document.createElement("div");
// strip each 'URL' - makes things simpler later on // e.appendChild(window.content.document.createTextNode(str));
urls[url] = urls[url].replace(/^\s+/, "").replace(/\s+$/, ""); // return e.innerHTML;
return str.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
// first check if it is an existing local file
var file = vimperator.io.getFile(urls[url]);
if (file.exists() && file.isReadable())
{
urls[url] = file.path;
continue;
}
// if the string doesn't look like a valid URL (i.e. contains a space
// or does not contain any of: .:/) try opening it with a search engine
// or keyword bookmark
var matches;
if (/\s/.test(urls[url]) || !/[.:\/]/.test(urls[url]))
{
matches = urls[url].match(/^(\S+)(?:\s+(.+))?$/);
var alias = matches[1];
var text = matches[2] || null;
// TODO: it would be clearer if the appropriate call to
// getSearchURL was made based on whether or not the first word was
// indeed an SE alias rather than seeing if getSearchURL can
// process the call usefully and trying again if it fails - much
// like the comments below ;-)
// check if the first word is a search engine
var searchURL = vimperator.bookmarks.getSearchURL(text, alias);
if (searchURL/* && searchURL.length >= 1*/)
{
urls[url] = searchURL;
continue;
}
else // the first word was not a search engine, search for the whole string in the default engine
{
searchURL = vimperator.bookmarks.getSearchURL(urls[url], null);
if (searchURL/* && searchURL.length >= 1*/)
{
urls[url] = searchURL;
continue;
}
}
}
// if we are here let Firefox handle the url and hope it does
// something useful with it :)
}
return urls;
}, },
formatBytes: function (num, decimalPlaces, humanReadable) formatBytes: function (num, decimalPlaces, humanReadable)
@@ -242,8 +181,121 @@ vimperator.util = { //{{{
return "<a class='hl-URL' href='#'>" + vimperator.util.escapeHTML(str) + "</a>"; return "<a class='hl-URL' href='#'>" + vimperator.util.escapeHTML(str) + "</a>";
else else
return str; return str;
} },
// if color = true it uses HTML markup to color certain items
objectToString: function (object, color)
{
if (object === null)
return "null";
if (typeof object != "object")
return false;
var string = "";
var obj = "";
try
{ // for window.JSON
obj = object.toString();
}
catch (e)
{
obj = "&lt;Object&gt;";
}
if (color)
string += "<span class=\"hl-Title\">" + obj + "</span>::\n";
else
string += obj + "::\n";
try // window.content often does not want to be queried with "var i in object"
{
for (var i in object)
{
var value;
try
{
value = object[i];
}
catch (e)
{
value = "&lt;no value&gt;";
}
if (color)
{
value = this.colorize(value, true);
string += "<span style=\"font-weight: bold;\">" + i + "</span>: " + value + "\n";
}
else
string += i + ": " + value + "\n";
}
}
catch (e) { }
return string;
},
// takes a string like 'google bla, www.osnews.com'
// and returns an array ['www.google.com/search?q=bla', 'www.osnews.com']
stringToURLArray: function (str)
{
var urls = str.split(/\s*\,\s+/);
begin: for (var url = 0; url < urls.length; url++)
{
// strip each 'URL' - makes things simpler later on
urls[url] = urls[url].replace(/^\s+/, "").replace(/\s+$/, "");
// first check if it is an existing local file
var file = vimperator.io.getFile(urls[url]);
if (file.exists() && file.isReadable())
{
urls[url] = file.path;
continue;
}
// if the string doesn't look like a valid URL (i.e. contains a space
// or does not contain any of: .:/) try opening it with a search engine
// or keyword bookmark
var matches;
if (/\s/.test(urls[url]) || !/[.:\/]/.test(urls[url]))
{
matches = urls[url].match(/^(\S+)(?:\s+(.+))?$/);
var alias = matches[1];
var text = matches[2] || null;
// TODO: it would be clearer if the appropriate call to
// getSearchURL was made based on whether or not the first word was
// indeed an SE alias rather than seeing if getSearchURL can
// process the call usefully and trying again if it fails - much
// like the comments below ;-)
// check if the first word is a search engine
var searchURL = vimperator.bookmarks.getSearchURL(text, alias);
if (searchURL/* && searchURL.length >= 1*/)
{
urls[url] = searchURL;
continue;
}
else // the first word was not a search engine, search for the whole string in the default engine
{
searchURL = vimperator.bookmarks.getSearchURL(urls[url], null);
if (searchURL/* && searchURL.length >= 1*/)
{
urls[url] = searchURL;
continue;
}
}
}
// if we are here let Firefox handle the url and hope it does
// something useful with it :)
}
return urls;
}
}; //}}} }; //}}}
// vim: set fdm=marker sw=4 ts=4 et: // vim: set fdm=marker sw=4 ts=4 et:

View File

@@ -26,759 +26,8 @@ the provisions above, a recipient may use your version of this file under
the terms of any one of the MPL, the GPL or the LGPL. the terms of any one of the MPL, the GPL or the LGPL.
}}} ***** END LICENSE BLOCK *****/ }}} ***** END LICENSE BLOCK *****/
const vimperator = (function () //{{{ vimperator.config = {
{ name: "Vimperator",
//////////////////////////////////////////////////////////////////////////////// hostApplication: "Firefox",
////////////////////// PRIVATE SECTION ///////////////////////////////////////// dialogs: []
/////////////////////////////////////////////////////////////////////////////{{{ }
var callbacks = [];
/////////////////////////////////////////////////////////////////////////////}}}
////////////////////// PUBLIC SECTION //////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////{{{
return {
get mode() { return vimperator.modes.main; },
set mode(value) { vimperator.modes.main = value; },
// Global constants
CURRENT_TAB: 1,
NEW_TAB: 2,
NEW_BACKGROUND_TAB: 3,
NEW_WINDOW: 4,
// ###VERSION### and ###DATE### are replaced by the Makefile
version: "###VERSION### (created: ###DATE###)",
input: {
buffer: "", // partial command storage
pendingMotionMap: null, // e.g. "d{motion}" if we wait for a motion of the "d" command
pendingArgMap: null, // pending map storage for commands like m{a-z}
count: -1 // parsed count from the input buffer
},
// @param type can be:
// "submit": when the user pressed enter in the command line
// "change"
// "cancel"
// "complete"
// TODO: "zoom": if the zoom value of the current buffer changed
registerCallback: function (type, mode, func)
{
// TODO: check if callback is already registered
callbacks.push([type, mode, func]);
},
triggerCallback: function (type, mode, data)
{
// dump("type: " + type + " mode: " + mode + "data: " + data + "\n");
for (var i in callbacks)
{
var [thistype, thismode, thisfunc] = callbacks[i];
if (mode == thismode && type == thistype)
return thisfunc.call(this, data);
}
return false;
},
beep: function ()
{
if (vimperator.options["visualbell"])
{
// flash the visual bell
var popup = document.getElementById("vimperator-visualbell");
var win = getBrowser().mPanelContainer;
var box = document.getBoxObjectFor(win);
popup.height = box.height;
popup.width = box.width;
popup.openPopup(win, "overlap", 0, 0, false, false);
setTimeout(function () { popup.hidePopup(); }, 50);
}
else
{
var soundService = Components.classes["@mozilla.org/sound;1"].
getService(Components.interfaces.nsISound);
soundService.beep();
}
},
copyToClipboard: function (str, verbose)
{
var clipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"]
.getService(Components.interfaces.nsIClipboardHelper);
clipboardHelper.copyString(str);
if (verbose)
vimperator.echo("Yanked " + str, vimperator.commandline.FORCE_SINGLELINE);
},
execute: function (str, modifiers)
{
// skip comments and blank lines
if (/^\s*("|$)/.test(str))
return;
if (!modifiers)
modifiers = {};
var [count, cmd, special, args] = vimperator.commands.parseCommand(str.replace(/^'(.*)'$/, "$1"));
var command = vimperator.commands.get(cmd);
if (command === null)
{
vimperator.echoerr("E492: Not an editor command: " + str);
vimperator.focusContent();
return;
}
// TODO: need to perform this test? -- djk
if (command.action === null)
{
vimperator.echoerr("E666: Internal error: command.action === null");
return;
}
// valid command, call it:
command.execute(args, special, count, modifiers);
},
// after pressing Escape, put focus on a non-input field of the browser document
// if clearFocusedElement, also blur a focused link
focusContent: function (clearFocusedElement)
{
var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"].
getService(Components.interfaces.nsIWindowWatcher);
if (window == ww.activeWindow && document.commandDispatcher.focusedElement && clearFocusedElement)
document.commandDispatcher.focusedElement.blur();
content.focus();
},
// partial sixth level expression evaluation
eval: function (string)
{
string = string.toString().replace(/^\s*/, "").replace(/\s*$/, "");
var matches = string.match(/^&(\w+)/);
if (matches)
{
var opt = this.options.get(matches[1]);
if (!opt)
{
this.echoerr("E113: Unknown option: " + matches[1]);
return;
}
var type = opt.type;
var value = opt.getter();
if (type != "boolean" && type != "number")
value = value.toString();
return value;
}
// String
else if (matches = string.match(/^(['"])([^\1]*?[^\\]?)\1/))
{
if (matches)
return matches[2].toString();
else
{
this.echoerr("E115: Missing quote: " + string);
return;
}
}
// Number
else if (matches = string.match(/^(\d+)$/))
{
return parseInt(match[1], 10);
}
var reference = this.variableReference(string);
if (!reference[0])
this.echoerr("E121: Undefined variable: " + string);
else
return reference[0][reference[1]];
return;
},
variableReference: function (string)
{
if (!string)
return [null, null, null];
var matches = string.match(/^([bwtglsv]):(\w+)/);
if (matches) // Variable
{
// Other variables should be implemented
if (matches[1] == "g")
{
if (matches[2] in this.globalVariables)
return [this.globalVariables, matches[2], matches[1]];
else
return [null, matches[2], matches[1]];
}
}
else // Global variable
{
if (string in this.globalVariables)
return [this.globalVariables, string, "g"];
else
return [null, string, "g"];
}
},
// TODO: move to vimp.util.? --mst
// if color = true it uses HTML markup to color certain items
objectToString: function (object, color)
{
if (object === null)
return "null";
if (typeof object != "object")
return false;
var string = "";
var obj = "";
try
{ // for window.JSON
obj = object.toString();
}
catch (e)
{
obj = "&lt;Object&gt;";
}
if (color)
string += "<span class=\"hl-Title\">" + obj + "</span>::\n";
else
string += obj + "::\n";
try // window.content often does not want to be queried with "var i in object"
{
for (var i in object)
{
var value;
try
{
value = object[i];
}
catch (e)
{
value = "&lt;no value&gt;";
}
if (color)
{
value = vimperator.util.colorize(value, true);
string += "<span style=\"font-weight: bold;\">" + i + "</span>: " + value + "\n";
}
else
string += i + ": " + value + "\n";
}
}
catch (e) { }
return string;
},
// logs a message to the javascript error console
// if msg is an object, it is beautified
log: function (msg, level)
{
//if (vimperator.options.getPref("verbose") >= level) // FIXME: hangs vimperator, probably timing issue --mst
if (typeof msg == "object")
msg = this.objectToString(msg, false);
var consoleService = Components.classes["@mozilla.org/consoleservice;1"].
getService(Components.interfaces.nsIConsoleService);
consoleService.logStringMessage("vimperator: " + msg);
},
// open one or more URLs
//
// @param urls: either a string or an array of urls
// The array can look like this:
// ["url1", "url2", "url3", ...] or:
// [["url1", postdata1], ["url2", postdata2], ...]
// @param where: if ommited, CURRENT_TAB is assumed
// @param callback: not implemented, will be allowed to specify a callback function
// which is called, when the page finished loading
// @returns true when load was initiated, or false on error
open: function (urls, where, callback)
{
// convert the string to an array of converted URLs
// -> see vimperator.util.stringToURLArray for more details
if (typeof urls == "string")
urls = vimperator.util.stringToURLArray(urls);
if (urls.length == 0)
return false;
if (!where)
where = vimperator.CURRENT_TAB;
var url = typeof urls[0] == "string" ? urls[0] : urls[0][0];
var postdata = typeof urls[0] == "string" ? null : urls[0][1];
var whichwindow = window;
// decide where to load the first url
switch (where)
{
case vimperator.CURRENT_TAB:
window.loadURI(url, null, postdata); // getBrowser.loadURI() did not work with postdata in my quick experiments --mst
break;
case vimperator.NEW_TAB:
var firsttab = getBrowser().addTab(url, null, null, postdata);
getBrowser().selectedTab = firsttab;
break;
case vimperator.NEW_BACKGROUND_TAB:
getBrowser().addTab(url, null, null, postdata);
break;
case vimperator.NEW_WINDOW:
window.open();
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
whichwindow = wm.getMostRecentWindow("navigator:browser");
whichwindow.loadURI(url, null, postdata);
break;
default:
vimperator.echoerr("Exxx: Invalid 'where' directive in vimperator.open(...)");
return false;
}
// all other URLs are always loaded in background
for (var i = 1; i < urls.length; i++)
{
url = typeof urls[i] == "string" ? urls[i] : urls[i][0];
postdata = typeof urls[i] == "string" ? null : urls[i][1];
whichwindow.getBrowser().addTab(url, null, null, postdata);
}
// TODO: register callbacks
return true;
},
// quit vimperator, no matter how many tabs/windows are open
quit: function (saveSession)
{
vimperator.autocommands.trigger("BrowserExit", "");
if (saveSession)
vimperator.options.setFirefoxPref("browser.startup.page", 3); // start with saved session
else
vimperator.options.setFirefoxPref("browser.startup.page", 1); // start with default homepage session
goQuitApplication();
},
restart: function ()
{
vimperator.autocommands.trigger("BrowserRestart", "");
const nsIAppStartup = Components.interfaces.nsIAppStartup;
// notify all windows that an application quit has been requested.
var os = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
var cancelQuit = Components.classes["@mozilla.org/supports-PRBool;1"]
.createInstance(Components.interfaces.nsISupportsPRBool);
os.notifyObservers(cancelQuit, "quit-application-requested", null);
// something aborted the quit process.
if (cancelQuit.data)
return;
// notify all windows that an application quit has been granted.
os.notifyObservers(null, "quit-application-granted", null);
// enumerate all windows and call shutdown handlers
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
var windows = wm.getEnumerator(null);
while (windows.hasMoreElements())
{
var win = windows.getNext();
if (("tryToClose" in win) && !win.tryToClose())
return;
}
Components.classes["@mozilla.org/toolkit/app-startup;1"].getService(nsIAppStartup)
.quit(nsIAppStartup.eRestart | nsIAppStartup.eAttemptQuit);
},
run: function (program, args, blocking)
{
var file = Components.classes["@mozilla.org/file/local;1"].
createInstance(Components.interfaces.nsILocalFile);
const WINDOWS = navigator.platform == "Win32";
if (!args)
args = [];
if (typeof blocking != "boolean")
blocking = false;
try
{
file.initWithPath(program);
}
catch (e)
{
var environmentService = Components.classes["@mozilla.org/process/environment;1"]
.getService(Components.interfaces.nsIEnvironment);
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)
{
const WINDOWS = navigator.platform == "Win32"; // FIXME: duplicated everywhere
var fileout = vimperator.io.createTempFile();
if (!fileout)
return "";
if (WINDOWS)
var command = str + " > " + fileout.path;
else
var command = str + " > \"" + fileout.path.replace('"', '\\"') + "\"";
var filein = null;
if (input)
{
filein = vimperator.io.createTempFile();
vimperator.io.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 = vimperator.io.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 <<EOF ... EOF around those files
source: function (filename, silent)
{
filename = vimperator.io.expandPath(filename);
try
{
var file = vimperator.io.getFile(filename);
if (!file.exists())
{
if (!silent)
vimperator.echoerr("E484: Can't open file " + filename);
return false;
}
var str = vimperator.io.readFile(filename);
// handle pure javascript files specially
if (/\.js$/.test(filename))
{
eval("with(vimperator){" + str + "}");
}
else
{
var heredoc = "";
var heredocEnd = null; // the string which ends the heredoc
str.split("\n").forEach(function (line)
{
if (heredocEnd) // we already are in a heredoc
{
if (heredocEnd.test(line))
{
eval("with(vimperator){" + heredoc + "}");
heredoc = "";
heredocEnd = null;
}
else
{
heredoc += line + "\n";
}
}
else
{
// check for a heredoc
var [count, cmd, special, args] = vimperator.commands.parseCommand(line);
var command = vimperator.commands.get(cmd);
if (command && command.name == "javascript")
{
var matches = args.match(/(.*)<<\s*([^\s]+)$/);
if (matches)
{
heredocEnd = new RegExp("^" + matches[2] + "$", "m");
if (matches[1])
heredoc = matches[1] + "\n";
}
else
{
command.execute(args, special, count);
}
}
else
{
// execute a normal vimperator command
vimperator.execute(line);
}
}
});
}
vimperator.log("Sourced: " + filename, 3);
}
catch (e)
{
if (!silent)
vimperator.echoerr(e);
}
},
startup: function ()
{
window.dump("Vimperator startup\n");
vimperator.log("Initializing vimperator object...", 1);
// these objects are created here only after the chrome is ready
vimperator.log("Loading module options...", 3);
vimperator.options = vimperator.Options();
vimperator.log("Loading module events...", 3);
vimperator.events = vimperator.Events();
vimperator.log("Loading module commands...", 3);
vimperator.commands = vimperator.Commands();
vimperator.log("Loading module bookmarks...", 3);
vimperator.bookmarks = vimperator.Bookmarks();
vimperator.log("Loading module history...", 3);
vimperator.history = vimperator.History();
vimperator.log("Loading module commandline...", 3);
vimperator.commandline = vimperator.CommandLine();
vimperator.log("Loading module search...", 3);
vimperator.search = vimperator.Search();
vimperator.log("Loading module preview window...", 3);
vimperator.previewwindow = vimperator.InformationList("vimperator-previewwindow", { incrementalFill: false, maxItems: 10 });
vimperator.log("Loading module buffer window...", 3);
vimperator.bufferwindow = vimperator.InformationList("vimperator-bufferwindow", { incrementalFill: false, maxItems: 10 });
vimperator.log("Loading module mappings...", 3);
vimperator.mappings = vimperator.Mappings();
vimperator.log("Loading module statusline...", 3);
vimperator.statusline = vimperator.StatusLine();
vimperator.log("Loading module buffer...", 3);
vimperator.buffer = vimperator.Buffer();
vimperator.log("Loading module editor...", 3);
vimperator.editor = vimperator.Editor();
vimperator.log("Loading module tabs...", 3);
vimperator.tabs = vimperator.Tabs();
vimperator.log("Loading module marks...", 3);
vimperator.marks = vimperator.Marks();
vimperator.log("Loading module quickmarks...", 3);
vimperator.quickmarks = vimperator.QuickMarks();
vimperator.log("Loading module hints...", 3);
vimperator.hints = vimperator.Hints();
vimperator.log("Loading module autocommands...", 3);
vimperator.autocommands = vimperator.AutoCommands();
vimperator.log("Loading module io...", 3);
vimperator.io = vimperator.IO();
vimperator.log("Loading module completion...", 3);
vimperator.completion = vimperator.Completion();
vimperator.log("All modules loaded", 3);
// we define some shortcuts to functions which are used often
vimperator.echo = function (str, flags) { vimperator.commandline.echo(str, vimperator.commandline.HL_NORMAL, flags); };
vimperator.echoerr = function (str, flags) { vimperator.commandline.echo(str, vimperator.commandline.HL_ERRORMSG, flags); };
vimperator.globalVariables = {};
// namespace for plugins/scripts. Actually (only) the active plugin must/can set a
// v.plugins.mode = <str> string to show on v.modes.CUSTOM
// v.plugins.stop = <func> hooked on a v.modes.reset()
// v.plugins.onEvent = <func> function triggered, on keypresses (unless <esc>) (see events.js)
vimperator.plugins = {};
// TODO: move elsewhere
vimperator.registerCallback("submit", vimperator.modes.EX, function (command) { vimperator.execute(command); });
vimperator.registerCallback("complete", vimperator.modes.EX, function (str) { return vimperator.completion.exTabCompletion(str); });
// first time intro message
if (vimperator.options.getPref("firsttime", true))
{
setTimeout(function () {
vimperator.commands.help();
vimperator.options.setPref("firsttime", false);
}, 1000);
}
// disable caret browsing initially
//vimperator.options.setFirefoxPref("accessibility.browsewithcaret", false);
//vimperator.focusContent();
// always start in normal mode
vimperator.modes.reset();
// finally, read a ~/.vimperatorrc
// make sourcing asynchronous, otherwise commands that open new tabs won't work
setTimeout(function () {
var rcFile = vimperator.io.getRCFile();
if (rcFile)
vimperator.source(rcFile.path, true);
else
vimperator.log("No user RC file found", 3);
// also source plugins in ~/.vimperator/plugin/
try
{
var pluginDir = vimperator.io.getSpecialDirectory("plugin");
if (pluginDir)
{
var files = vimperator.io.readDirectory(pluginDir.path);
vimperator.log("Sourcing plugin directory...", 3);
files.forEach(function (file) {
if (!file.isDirectory() && /\.(js|vimp)$/i.test(file.path))
vimperator.source(file.path, false);
});
}
else
{
vimperator.log("No user plugin directory found", 3);
}
}
catch (e)
{
// thrown if directory does not exist
//vimperator.log("Error sourcing plugin directory: " + e);
}
// after sourcing the initialization files, this function will set
// all gui options to their default values, if they have not been
// set before by any rc file
vimperator.options.setInitialGUI();
}, 0);
vimperator.statusline.update();
vimperator.log("Vimperator fully initialized", 1);
},
shutdown: function ()
{
window.dump("Vimperator shutdown\n");
// save our preferences
vimperator.commandline.destroy();
vimperator.quickmarks.destroy();
vimperator.options.destroy();
vimperator.events.destroy();
window.dump("All vimperator modules destroyed\n");
},
sleep: function (ms)
{
var threadManager = Components.classes["@mozilla.org/thread-manager;1"].
getService(Components.interfaces.nsIThreadManager);
var mainThread = threadManager.mainThread;
var then = new Date().getTime(), now = then;
for (; now - then < ms; now = new Date().getTime())
mainThread.processNextEvent(true);
},
get windows()
{
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
var wa = [];
var enumerator = wm.getEnumerator("navigator:browser");
while (enumerator.hasMoreElements())
wa.push(enumerator.getNext());
return wa;
},
// be sure to call GUI related methods like alert() or dump() ONLY in the main thread
callFunctionInThread: function (thread, func, args)
{
function CallbackEvent(func, args)
{
if (!(args instanceof Array))
args = [];
return {
QueryInterface: function (iid)
{
if (iid.equals(Components.interfaces.nsIRunnable) ||
iid.equals(Components.interfaces.nsISupports))
return this;
throw Components.results.NS_ERROR_NO_INTERFACE;
},
run: function ()
{
func.apply(window, args);
}
};
}
if (!thread)
thread = Components.classes["@mozilla.org/thread-manager;1"].getService().newThread(0);
// DISPATCH_SYNC is necessary, otherwise strange things will happen
thread.dispatch(new CallbackEvent(func, args), thread.DISPATCH_SYNC);
}
}; //}}}
})(); //}}}
// called when the chrome is fully loaded and before the main window is shown
window.addEventListener("load", vimperator.startup, false);
window.addEventListener("unload", vimperator.shutdown, false);
// vim: set fdm=marker sw=4 ts=4 et:

View File

@@ -37,7 +37,9 @@ the terms of any one of the MPL, the GPL or the LGPL.
xmlns:html="http://www.w3.org/1999/xhtml" xmlns:html="http://www.w3.org/1999/xhtml"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/x-javascript;version=1.8" src="vim.js"/>
<script type="application/x-javascript;version=1.8" src="vimperator.js"/> <script type="application/x-javascript;version=1.8" src="vimperator.js"/>
<script type="application/x-javascript;version=1.8" src="bookmarks.js"/> <script type="application/x-javascript;version=1.8" src="bookmarks.js"/>
<script type="application/x-javascript;version=1.8" src="buffers.js"/> <script type="application/x-javascript;version=1.8" src="buffers.js"/>
<script type="application/x-javascript;version=1.8" src="commands.js"/> <script type="application/x-javascript;version=1.8" src="commands.js"/>