1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2026-01-08 17:24:12 +01:00

many small style improvements, some useless test functions added (yeah, that's why it's called "pre" software :))

This commit is contained in:
Martin Stubenschrott
2008-02-01 02:45:31 +00:00
parent ee73cd05a9
commit 3b4f00a2e8
10 changed files with 232 additions and 215 deletions

View File

@@ -38,80 +38,6 @@ vimperator.Buffer = function () //{{{
var zoomLevels = [ 1, 10, 25, 50, 75, 90, 100,
120, 150, 200, 300, 500, 1000, 2000 ];
// TODO: rename
function followFrameRelationship(relationship, parsedFrame)
{
var regexps;
var relText;
var patternText;
var revString;
switch (relationship)
{
case "next":
regexps = vimperator.options["nextpattern"].split(",");
revString = "previous";
break;
case "previous":
// TODO: accept prev\%[ious]
regexps = vimperator.options["previouspattern"].split(",");
revString = "next";
break;
default:
vimperator.echoerr("Bad document relationship: " + relationship);
}
relText = new RegExp(relationship, "i");
revText = new RegExp(revString, "i");
var elems = parsedFrame.document.getElementsByTagName("link");
// links have higher priority than normal <a> hrefs
for (var i = 0; i < elems.length; i++)
{
if (relText.test(elems[i].rel) || revText.test(elems[i].rev))
{
vimperator.open(elems[i].href);
return true;
}
}
// no links? ok, look for hrefs
elems = parsedFrame.document.getElementsByTagName("a");
for (var i = 0; i < elems.length; i++)
{
if (relText.test(elems[i].rel) || revText.test(elems[i].rev))
{
vimperator.buffer.followLink(elems[i], vimperator.CURRENT_TAB);
return true;
}
}
for (var pattern = 0; pattern < regexps.length; pattern++)
{
patternText = new RegExp(regexps[pattern], "i");
for (var i = 0; i < elems.length; i++)
{
if (patternText.test(elems[i].textContent))
{
vimperator.buffer.followLink(elems[i], vimperator.CURRENT_TAB);
return true;
}
else
{
// images with alt text being href
var children = elems[i].childNodes;
for (var j = 0; j < children.length; j++)
{
if (patternText.test(children[j].alt))
{
vimperator.buffer.followLink(elems[i], vimperator.CURRENT_TAB);
return true;
}
}
}
}
}
return false;
}
function setZoom(value, fullZoom)
{
if (value < 1 || value > 2000)
@@ -216,6 +142,26 @@ vimperator.Buffer = function () //{{{
win.scrollTo(h, v);
}
vimperator.commands.add(new vimperator.Command(["test"],
function (args, special)
{
alert(args)
},
{
shortHelp: "Test command"
}
));
vimperator.options.add(new vimperator.Option(["test"], "boolean",
{
shortHelp: "Test option",
defaultValue: false
}
));
vimperator.mappings.addDefault([vimperator.modes.NORMAL], ["w"], "Test",
function () { alert("test"); }
);
/////////////////////////////////////////////////////////////////////////////}}}
////////////////////// PUBLIC SECTION //////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////{{{
@@ -310,7 +256,7 @@ vimperator.Buffer = function () //{{{
},
// quick function to get elements inside the document reliably
// argument "args" is something like: @id='myid' or @type='text' (don't forget the quoted around myid)
// argument "args" is something like: @id='myid' or @type='text' (don't forget the quotes around myid)
getElement: function (args, index)
{
return vimperator.buffer.evaluateXPath("//*[" + (args || "") + "]").snapshotItem(index || 0)
@@ -434,11 +380,12 @@ vimperator.Buffer = function () //{{{
return selection;
},
// TODO: move to tabs.js
list: function (fullmode)
{
if (fullmode)
{
// toggle the special buffer previw window
// toggle the special buffer preview window
if (vimperator.bufferwindow.visible())
{
vimperator.bufferwindow.hide();
@@ -644,6 +591,7 @@ vimperator.Buffer = function () //{{{
setTimeout(function () { doc.body.removeChild(indicator); }, 500);
},
// XXX: probably remove this method/functionality
// updates the buffer preview in place only if list is visible
updateBufferList: function ()
{
@@ -655,7 +603,7 @@ vimperator.Buffer = function () //{{{
vimperator.bufferwindow.selectItem(getBrowser().mTabContainer.selectedIndex);
},
// XXX: should this be in v.buffers. or v.tabs.?
// TODO: move to v.tabs.?
// "buffer" is a string which matches the URL or title of a buffer, if it
// is null, the last used string is used again
switchTo: function (buffer, allowNonUnique, count, reverse)
@@ -995,6 +943,79 @@ vimperator.Buffer = function () //{{{
followDocumentRelationship: function (relationship)
{
function followFrameRelationship(relationship, parsedFrame)
{
var regexps;
var relText;
var patternText;
var revString;
switch (relationship)
{
case "next":
regexps = vimperator.options["nextpattern"].split(",");
revString = "previous";
break;
case "previous":
// TODO: accept prev\%[ious]
regexps = vimperator.options["previouspattern"].split(",");
revString = "next";
break;
default:
vimperator.echoerr("Bad document relationship: " + relationship);
}
relText = new RegExp(relationship, "i");
revText = new RegExp(revString, "i");
var elems = parsedFrame.document.getElementsByTagName("link");
// links have higher priority than normal <a> hrefs
for (var i = 0; i < elems.length; i++)
{
if (relText.test(elems[i].rel) || revText.test(elems[i].rev))
{
vimperator.open(elems[i].href);
return true;
}
}
// no links? ok, look for hrefs
elems = parsedFrame.document.getElementsByTagName("a");
for (var i = 0; i < elems.length; i++)
{
if (relText.test(elems[i].rel) || revText.test(elems[i].rev))
{
vimperator.buffer.followLink(elems[i], vimperator.CURRENT_TAB);
return true;
}
}
for (var pattern = 0; pattern < regexps.length; pattern++)
{
patternText = new RegExp(regexps[pattern], "i");
for (var i = 0; i < elems.length; i++)
{
if (patternText.test(elems[i].textContent))
{
vimperator.buffer.followLink(elems[i], vimperator.CURRENT_TAB);
return true;
}
else
{
// images with alt text being href
var children = elems[i].childNodes;
for (var j = 0; j < children.length; j++)
{
if (patternText.test(children[j].alt))
{
vimperator.buffer.followLink(elems[i], vimperator.CURRENT_TAB);
return true;
}
}
}
}
}
return false;
}
var retVal;
if (window.content.frames.length != 0)
{

View File

@@ -470,6 +470,31 @@ vimperator.Editor = function () //{{{
}
},
// System for adding abbreviations:
//
// filter == ! delete all, and set first (END)
//
// if filter == ! remove all and add it as only END
//
// variant 1: rhs matches anywere in loop
//
// 1 mod matches anywhere in loop
// a) simple replace and
// I) (maybe there's another rhs that matches? not possible)
// (when there's another item, it's opposite mod with different rhs)
// (so do nothing further but END)
//
// 2 mod does not match
// a) the opposite is there -> make a ! and put it as only and END
// (b) a ! is there. do nothing END)
//
// variant 2: rhs matches *no*were in loop and filter is c or i
// everykind of current combo is possible to 1 {c,i,!} or two {c and i}
//
// 1 mod is ! split into two i + c END
// 1 not !: opposite mode (first), add/change 'second' and END
// 1 not !: same mode (first), overwrite first this END
//
addAbbreviation: function (filter, lhs, rhs)
{
if (!abbrev[lhs])
@@ -525,33 +550,6 @@ vimperator.Editor = function () //{{{
abbrev[lhs][1] = [filter, rhs];
else
abbrev[lhs][0] = [filter, rhs];
return;
// System above:
// filter == ! delete all, and set first (END)
//
// if filter == ! remove all and add it as only END
//
// variant 1: rhs matches anywere in loop
//
// 1 mod matches anywhere in loop
// a) simple replace and
// I) (maybe there's another rhs that matches? not possible)
// (when there's another item, it's opposite mod with different rhs)
// (so do nothing further but END)
//
// 2 mod does not match
// a) the opposite is there -> make a ! and put it as only and END
// (b) a ! is there. do nothing END)
//
// variant 2: rhs matches *no*were in loop and filter is c or i
// everykind of current combo is possible to 1 {c,i,!} or two {c and i}
//
// 1 mod is ! split into two i + c END
// 1 not !: opposite mode (first), add/change 'second' and END
// 1 not !: same mode (first), overwrite first this END
//
},
removeAbbreviation: function (filter, lhs)
@@ -631,20 +629,20 @@ vimperator.Editor = function () //{{{
for (var lhs in abbrev)
{
for (var i = 0; i < abbrev[lhs].length; i++)
for (var i = 0; i < abbrev[lhs].length; i++)
{
if (lhs == foundWord && (abbrev[lhs][i][0] == filter || abbrev[lhs][i][0] == "!"))
{
if (lhs == foundWord && (abbrev[lhs][i][0] == filter || abbrev[lhs][i][0] == "!"))
{
// if found, replace accordingly
var len = foundWord.length;
var abbrText = abbrev[lhs][i][1];
text = text.substring(0, currStart - len) + abbrText + text.substring(currStart);
textbox.value = text;
textbox.selectionStart = currStart - len + abbrText.length;
textbox.selectionEnd = currEnd - len + abbrText.length;
break;
}
// if found, replace accordingly
var len = foundWord.length;
var abbrText = abbrev[lhs][i][1];
text = text.substring(0, currStart - len) + abbrText + text.substring(currStart);
textbox.value = text;
textbox.selectionStart = currStart - len + abbrText.length;
textbox.selectionEnd = currEnd - len + abbrText.length;
break;
}
}
}
return true;
}

View File

@@ -55,11 +55,30 @@ vimperator.AutoCommands = function() //{{{
return autoCommandsIterator();
},
add: function (auEvent, regex, cmds)
{
var eventsIter = auEvent.split(",");
for (var i = 0; i < eventsIter.length; i++)
{
if (!autoCommands[eventsIter[i]])
autoCommands[eventsIter[i]] = [];
var flag = true;
for (var y = 0; y < autoCommands[eventsIter[i]].length; y++)
{
if (autoCommands[eventsIter[i]][y][0] == regex && autoCommands[eventsIter[i]][y][1] == cmds)
flag = false;
}
if (flag)
autoCommands[eventsIter[i]].push([regex, cmds]);
}
},
remove: function (auEvent, regex) // arguments are filters (NULL = all)
{
if (!auEvent && !regex)
{
autoCommands = {}; // delete all TODO: rather delete.. or something?
autoCommands = {}; // delete all
}
else if (!regex) // remove all on this auEvent
{
@@ -69,7 +88,7 @@ vimperator.AutoCommands = function() //{{{
delete autoCommands[item];
}
}
else if (!auEvent) // delete all match's to this regex
else if (!auEvent) // delete all matches to this regex
{
for (var item in autoCommands)
{
@@ -79,7 +98,7 @@ vimperator.AutoCommands = function() //{{{
if (regex == autoCommands[item][i][0])
{
autoCommands[item].splice(i, 1); // remove array
// keep `i' since this is removed, so a possible next one is at this place now)
// keep `i' since this is removed, so a possible next one is at this place now
}
else
i++;
@@ -135,25 +154,6 @@ vimperator.AutoCommands = function() //{{{
vimperator.commandline.echo(list, vimperator.commandline.HL_NORMAL, vimperator.commandline.FORCE_MULTILINE);
},
add: function (auEvent, regex, cmds)
{
var eventsIter = auEvent.split(",");
for (var i = 0; i < eventsIter.length; i++)
{
if (!autoCommands[eventsIter[i]])
autoCommands[eventsIter[i]] = [];
var flag = true;
for (var y = 0; y < autoCommands[eventsIter[i]].length; y++)
{
if (autoCommands[eventsIter[i]][y][0] == regex && autoCommands[eventsIter[i]][y][1] == cmds)
flag = false;
}
if (flag)
autoCommands[eventsIter[i]].push([regex, cmds]);
}
},
trigger: function (auEvent, url)
{
if (autoCommands[auEvent])
@@ -176,7 +176,7 @@ vimperator.Events = function () //{{{
////////////////////// PRIVATE SECTION /////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////{{{
var inputBufferLength = 0; // counts the number of keys in v.input.buffer (can be different from v.input.buffer.length)
var inputBufferLength = 0; // count the number of keys in v.input.buffer (can be different from v.input.buffer.length)
var skipMap = false; // while feeding the keys (stored in v.input.buffer | no map found) - ignore mappings
var macros = {};
@@ -270,7 +270,7 @@ vimperator.Events = function () //{{{
// NOTE: the order of ["Esc", "Escape"] or ["Escape", "Esc"]
// matters, so use that string as the first item, that you
// want to refer to within Vimperator's source code for
// comparisons like if (key == "Esc") { ... }
// comparisons like if (key == "<Esc>") { ... }
var keyTable = [
[ KeyEvent.DOM_VK_ESCAPE, ["Esc", "Escape"] ],
[ KeyEvent.DOM_VK_LEFT_SHIFT, ["<"] ],
@@ -390,8 +390,8 @@ vimperator.Events = function () //{{{
// code which is only relevant if the page load is the current tab goes here:
if (doc == getBrowser().selectedBrowser.contentDocument)
{
// // FIXME: this currently causes window map events which is _very_ annoying
// // we want to stay in command mode after a page has loaded
// we want to stay in command mode after a page has loaded
// XXX: Does this still causes window map events which is _very_ annoying
setTimeout(function () {
var focused = document.commandDispatcher.focusedElement;
if (focused && focused.value.length == 0)
@@ -412,12 +412,12 @@ vimperator.Events = function () //{{{
// if (vimperator.buffer.loaded == 1)
// return true;
var ms = 10000; // maximum time to wait - TODO: add option
var ms = 15000; // maximum time to wait - TODO: add option
var then = new Date().getTime();
for (var now = then; now - then < ms; now = new Date().getTime())
{
mainThread.processNextEvent(true);
if ((now -then) % 1000 < 10)
if ((now - then) % 1000 < 10)
dump("waited: " + (now - then) + " ms\n");
if (vimperator.buffer.loaded > 0)
@@ -615,7 +615,7 @@ vimperator.Events = function () //{{{
{
charCode = 0;
}
else //an invalid key like <A-xxx> was found, stop propagation here (like Vim)
else // an invalid key like <A-xxx> was found, stop propagation here (like Vim)
{
return;
}
@@ -716,7 +716,6 @@ vimperator.Events = function () //{{{
// a key like F1 is always enclosed in < and >
return "<" + modifier + key + ">";
},
isAcceptKey: function (key)
@@ -797,18 +796,20 @@ vimperator.Events = function () //{{{
{
if (couldCopy)
{
if ((vimperator.mode == vimperator.modes.TEXTAREA || (vimperator.modes.extended & vimperator.modes.TEXTAREA))
if ((vimperator.mode == vimperator.modes.TEXTAREA ||
(vimperator.modes.extended & vimperator.modes.TEXTAREA))
&& !vimperator.options["insertmode"])
vimperator.modes.set(vimperator.modes.VISUAL, vimperator.modes.TEXTAREA);
else if (vimperator.mode == vimperator.modes.CARET)
vimperator.modes.set(vimperator.modes.VISUAL, vimperator.modes.CARET);
}
}
//else
//{
// if (!couldCopy && vimperator.modes.extended & vimperator.modes.CARET)
// vimperator.mode = vimperator.modes.CARET;
//}
// XXX: disabled, as i think automatically starting visual caret mode does more harm than help
// else
// {
// if (!couldCopy && vimperator.modes.extended & vimperator.modes.CARET)
// vimperator.mode = vimperator.modes.CARET;
// }
},
// global escape handler, is called in ALL modes
@@ -824,10 +825,18 @@ vimperator.Events = function () //{{{
switch (vimperator.mode)
{
case vimperator.modes.HINTS:
case vimperator.modes.CUSTOM:
case vimperator.modes.COMMAND_LINE:
case vimperator.modes.NORMAL:
// clear any selection made
var selection = window.content.getSelection();
try
{ // a simple if (selection) does not seem to work
selection.collapseToStart();
}
catch (e) { }
vimperator.commandline.clear();
vimperator.modes.reset();
vimperator.focusContent(true);
break;
case vimperator.modes.VISUAL:
@@ -855,19 +864,9 @@ vimperator.Events = function () //{{{
}
break;
default:
// clear any selection made
var selection = window.content.getSelection();
try
{ // a simple if (selection) does not seem to work
selection.collapseToStart();
}
catch (e) { }
vimperator.commandline.clear();
default: // HINTS, CUSTOM or COMMAND_LINE
vimperator.modes.reset();
vimperator.focusContent(true);
break;
}
}
},
@@ -925,16 +924,9 @@ vimperator.Events = function () //{{{
return false;
}
// FIXME: proper way is to have a better onFocus handler which also handles events for the XUL
if (!vimperator.mode == vimperator.modes.TEXTAREA &&
!vimperator.mode == vimperator.modes.INSERT &&
!vimperator.mode == vimperator.modes.COMMAND_LINE &&
isFormElemFocused()) // non insert mode, but e.g. the location bar has focus
return false;
// just forward event, without checking any mappings
// just forward event without checking any mappings when the MOW is open
if (vimperator.mode == vimperator.modes.COMMAND_LINE &&
vimperator.modes.extended & vimperator.modes.OUTPUT_MULTILINE)
(vimperator.modes.extended & vimperator.modes.OUTPUT_MULTILINE))
{
vimperator.commandline.onMultilineOutputEvent(event);
return false;
@@ -1007,12 +999,12 @@ vimperator.Events = function () //{{{
}
}
//FIXME (maybe): (is an ESC or C-] here): on HINTS mode, it enters
//into 'if (map && !skipMap) below. With that (or however) it
//triggers the onEscape part, where it resets mode. Here I just
//return true, with the effect that it also gets to there (for
//whatever reason). if that happens to be correct, well..
//XXX: why not just do that as well for HINTS mode actually?
// FIXME (maybe): (is an ESC or C-] here): on HINTS mode, it enters
// into 'if (map && !skipMap) below. With that (or however) it
// triggers the onEscape part, where it resets mode. Here I just
// return true, with the effect that it also gets to there (for
// whatever reason). if that happens to be correct, well..
// XXX: why not just do that as well for HINTS mode actually?
if (vimperator.mode == vimperator.modes.CUSTOM)
return true;
@@ -1045,7 +1037,7 @@ vimperator.Events = function () //{{{
vimperator.input.buffer = "";
inputBufferLength = 0;
var tmp = vimperator.input.pendingArgMap; // must be set to null before .execute; if not
vimperator.input.pendingArgMap = null; // v.inputpendingArgMap is still 'true' also for new feeded keys
vimperator.input.pendingArgMap = null; // v.input.pendingArgMap is still 'true' also for new feeded keys
if (key != "<Esc>" && key != "<C-[>")
{
if (vimperator.modes.isReplaying && !waitForPageLoaded())
@@ -1267,7 +1259,9 @@ vimperator.Events = function () //{{{
observe: function (aSubject, aTopic, aData)
{
if (aTopic != "nsPref:changed") return;
if (aTopic != "nsPref:changed")
return;
// aSubject is the nsIPrefBranch we're observing (after appropriate QI)
// aData is the name of the pref that's been changed (relative to aSubject)
switch (aData)

View File

@@ -90,6 +90,12 @@ vimperator.Mappings = function () //{{{
map.modes.forEach(function (mode) { main[mode].push(map); });
}
function addMap(map, userMap)
{
var where = userMap ? user : main;
map.modes.forEach(function (mode) { where[mode].push(map); });
}
function getMap(mode, cmd, stack)
{
var maps = stack[mode];
@@ -192,9 +198,15 @@ vimperator.Mappings = function () //{{{
return user[mode].some(function (map) { return map.hasName(cmd); });
},
addDefault: function (modes, keys, description, action, extra)
{
addMap (new vimperator.Map([vimperator.modes.NORMAL], keys,
action, { shortHelp: description }), false);
},
add: function (map)
{
// a map can have multiple names (see default-map sections, there are many multiples)
// a map can have multiple names
for (var i = 0; i < map.names.length; i++)
{
// only store keysyms with uppercase modifier strings
@@ -223,7 +235,6 @@ vimperator.Mappings = function () //{{{
return getMap(mode, cmd, user) || getMap(mode, cmd, main);
},
// TODO: move default maps to their own v.normal namespace
getDefault: function (mode, cmd)
{
return getMap(mode, cmd, main);

View File

@@ -32,14 +32,6 @@ const vimperator = (function () //{{{
////////////////////// PRIVATE SECTION /////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////{{{
// our services
var soundService = Components.classes["@mozilla.org/sound;1"]
.getService(Components.interfaces.nsISound);
var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
.getService(Components.interfaces.nsIConsoleService);
var environmentService = Components.classes["@mozilla.org/process/environment;1"]
.getService(Components.interfaces.nsIEnvironment);
var callbacks = [];
/////////////////////////////////////////////////////////////////////////////}}}
@@ -107,6 +99,8 @@ const vimperator = (function () //{{{
}
else
{
var soundService = Components.classes["@mozilla.org/sound;1"].
getService(Components.interfaces.nsISound);
soundService.beep();
}
},
@@ -299,6 +293,8 @@ const vimperator = (function () //{{{
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);
},
@@ -435,6 +431,9 @@ const vimperator = (function () //{{{
}
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++)
{
@@ -622,7 +621,7 @@ const vimperator = (function () //{{{
vimperator.quickmarks = vimperator.QuickMarks();
vimperator.log("Loading module hints...", 3);
vimperator.hints = vimperator.Hints();
vimperator.log("Loading module autocommands...", 3); //XXX: what the 3 there, I didn't check
vimperator.log("Loading module autocommands...", 3);
vimperator.autocommands = vimperator.AutoCommands();
vimperator.log("Loading module io...", 3);
vimperator.io = vimperator.IO();