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

merge new vimperator.io object

This commit is contained in:
Doug Kearns
2007-10-22 02:55:52 +00:00
parent dd3196f0b4
commit 05ab034753
4 changed files with 230 additions and 287 deletions

View File

@@ -104,87 +104,6 @@ const vimperator = (function() //{{{
vimperator.echo("-- " + str_mode + str_extended + " --");
}
function expandPath(path)
{
const WINDOWS = navigator.platform == "Win32";
// TODO: proper pathname separator translation like Vim
if (WINDOWS)
path = path.replace('/', '\\', 'g');
// expand "~" to VIMPERATOR_HOME or HOME (USERPROFILE or HOMEDRIVE\HOMEPATH on Windows if HOME is not set)
if (/^~/.test(path))
{
var home = environment_service.get("VIMPERATOR_HOME");
if (!home)
home = environment_service.get("HOME");
if (WINDOWS && !home)
home = environment_service.get("USERPROFILE") ||
environment_service.get("HOMEDRIVE") + environment_service.get("HOMEPATH");
path = path.replace("~", home);
}
// expand any $ENV vars
var env_vars = path.match(/\$\w+\b/g); // this is naive but so is Vim and we like to be compatible
if (env_vars)
{
var expansion;
for (var i = 0; i < env_vars.length; i++)
{
expansion = environment_service.get(env_vars[i].replace("$", ""));
if (expansion)
path = path.replace(env_vars[i], expansion);
}
}
return path;
}
// TODO: add this functionality to LocalFile or wait for Scriptable I/O in FUEL
function pathExists(path)
{
var p = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
p.initWithPath(expandPath(path));
return p.exists();
}
function getPluginDir()
{
var plugin_dir;
if (navigator.platform == "Win32")
plugin_dir = "~/vimperator/plugin";
else
plugin_dir = "~/.vimperator/plugin";
plugin_dir = expandPath(plugin_dir);
return pathExists(plugin_dir) ? plugin_dir : null;
}
function getRCFile()
{
var rc_file1 = expandPath("~/.vimperatorrc");
var rc_file2 = expandPath("~/_vimperatorrc");
if (navigator.platform == "Win32")
[rc_file1, rc_file2] = [rc_file2, rc_file1]
if (pathExists(rc_file1))
return rc_file1;
else if (pathExists(rc_file2))
return rc_file2;
else
return null;
}
/////////////////////////////////////////////////////////////////////////////}}}
////////////////////// PUBLIC SECTION //////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////{{{
@@ -652,7 +571,7 @@ const vimperator = (function() //{{{
{
const WINDOWS = navigator.platform == "Win32"; // FIXME: duplicated everywhere
var fileout = getTempFile();
var fileout = vimperator.io.createTempFile();
if (!fileout)
return "";
@@ -664,10 +583,8 @@ const vimperator = (function() //{{{
var filein = null;
if (input)
{
filein = getTempFile();
var fdin = vimperator.io.fopen(filein, ">");
fdin.write(input);
fdin.close();
filein = vimperator.io.createTempFile();
vimperator.io.writeFile(filein, input);
command += " < \"" + filein.path.replace('"', '\\"') + "\"";
}
@@ -677,48 +594,38 @@ const vimperator = (function() //{{{
else
res = this.run("sh", ["-c", command], true);
var fd = vimperator.io.fopen(fileout, "<");
if (!fd)
return "";
var s = fd.read();
fd.close();
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 (s && s.indexOf("\n") == s.length-1)
s = s.substr(0, s.length-1);
if (output && output.indexOf("\n") == output.length-1)
output = output.substr(0, output.length-1);
return s;
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 = expandPath(filename);
filename = vimperator.io.expandPath(filename);
try
{
var fd = vimperator.io.fopen(filename, "<");
if (!fd)
return;
var s = fd.read();
fd.close();
var str = vimperator.io.readFile(filename);
// handle pure javascript files specially
if (filename.search("\.js$") != -1)
{
eval(s);
eval(str);
}
else
{
var heredoc = "";
var heredoc_end = null; // the string which ends the heredoc
s.split("\n").forEach(function(line)
str.split("\n").forEach(function(line)
{
if (heredoc_end) // we already are in a heredoc
{
@@ -834,10 +741,10 @@ const vimperator = (function() //{{{
// make sourcing asynchronous, otherwise commands that open new tabs won't work
setTimeout(function() {
var rc_file = getRCFile();
var rc_file = vimperator.io.getRCFile();
if (rc_file)
vimperator.source(rc_file, true);
vimperator.source(rc_file.path, true);
else
vimperator.log("No user RC file found", 3);
@@ -845,15 +752,13 @@ const vimperator = (function() //{{{
var entries = [];
try
{
var plugin_dir = getPluginDir();
var plugin_dir = vimperator.io.getPluginDir();
if (plugin_dir)
{
var fd = vimperator.io.fopen(plugin_dir);
var entries = fd.read();
fd.close();
var files = vimperator.io.readDirectory(plugin_dir.path);
vimperator.log("Sourcing plugin directory...", 3);
entries.forEach(function(file) {
files.forEach(function(file) {
if (!file.isDirectory())
vimperator.source(file.path, false);
});