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

begin cleaning up v.mappings and add a default iterator function to v.modes

This commit is contained in:
Doug Kearns
2007-11-29 11:20:55 +00:00
parent 06a60ef443
commit 04b50114f0
4 changed files with 77 additions and 91 deletions

View File

@@ -1321,7 +1321,7 @@ vimperator.Commands = function () //{{{
{ {
vimperator.mappings.add(new vimperator.Map([mode[index]], [lhs], vimperator.mappings.add(new vimperator.Map([mode[index]], [lhs],
function (count) { vimperator.events.feedkeys((count > 1 ? count : "") + rhs, noremap); }, function (count) { vimperator.events.feedkeys((count > 1 ? count : "") + rhs, noremap); },
{ flags: vimperator.Mappings.flags.COUNT, rhs: rhs, noremapping: noremap} { flags: vimperator.Mappings.flags.COUNT, rhs: rhs, noremap: noremap}
)); ));
} }
else else

View File

@@ -39,19 +39,23 @@ vimperator.Events = function () //{{{
// any tab related events // any tab related events
var tabcontainer = getBrowser().tabContainer; var tabcontainer = getBrowser().tabContainer;
tabcontainer.addEventListener("TabMove", function (event) { tabcontainer.addEventListener("TabMove", function (event)
{
vimperator.statusline.updateTabCount(); vimperator.statusline.updateTabCount();
vimperator.buffer.updateBufferList(); vimperator.buffer.updateBufferList();
}, false); }, false);
tabcontainer.addEventListener("TabOpen", function (event) { tabcontainer.addEventListener("TabOpen", function (event)
{
vimperator.statusline.updateTabCount(); vimperator.statusline.updateTabCount();
vimperator.buffer.updateBufferList(); vimperator.buffer.updateBufferList();
}, false); }, false);
tabcontainer.addEventListener("TabClose", function (event) { tabcontainer.addEventListener("TabClose", function (event)
{
vimperator.statusline.updateTabCount(); vimperator.statusline.updateTabCount();
vimperator.buffer.updateBufferList(); vimperator.buffer.updateBufferList();
}, false); }, false);
tabcontainer.addEventListener("TabSelect", function (event) { tabcontainer.addEventListener("TabSelect", function (event)
{
if (vimperator.mode == vimperator.modes.HINTS) if (vimperator.mode == vimperator.modes.HINTS)
vimperator.modes.reset(); vimperator.modes.reset();
@@ -459,7 +463,8 @@ vimperator.Events = function () //{{{
vimperator.mode == vimperator.modes.VISUAL) vimperator.mode == vimperator.modes.VISUAL)
{ {
this.wantsModeReset = true; this.wantsModeReset = true;
setTimeout(function () { setTimeout(function ()
{
if (vimperator.events.wantsModeReset) if (vimperator.events.wantsModeReset)
vimperator.modes.reset(); vimperator.modes.reset();
}, 10); }, 10);
@@ -646,7 +651,7 @@ vimperator.Events = function () //{{{
var candidateCommand = (vimperator.input.buffer + key).replace(countStr, ""); var candidateCommand = (vimperator.input.buffer + key).replace(countStr, "");
var map; var map;
if (event.noremap) if (event.noremap)
map = vimperator.mappings.getDefaultMap(vimperator.mode, candidateCommand); map = vimperator.mappings.getDefault(vimperator.mode, candidateCommand);
else else
map = vimperator.mappings.get(vimperator.mode, candidateCommand); map = vimperator.mappings.get(vimperator.mode, candidateCommand);

View File

@@ -42,7 +42,9 @@ vimperator.Map = function (modes, cmds, action, extraInfo) //{{{
this.flags = extraInfo.flags || 0; this.flags = extraInfo.flags || 0;
if (extraInfo.usage) if (extraInfo.usage)
{
this.usage = extraInfo.usage; this.usage = extraInfo.usage;
}
else else
{ {
this.usage = this.names[0]; // only the first command name this.usage = this.names[0]; // only the first command name
@@ -57,7 +59,7 @@ vimperator.Map = function (modes, cmds, action, extraInfo) //{{{
this.shortHelp = extraInfo.shortHelp || null; this.shortHelp = extraInfo.shortHelp || null;
this.rhs = extraInfo.rhs || null; this.rhs = extraInfo.rhs || null;
this.noremap = extraInfo.noremapping || null; // XXX: needed for mkv; providing feedkeys true/false still neded? this.noremap = extraInfo.noremap || false; // XXX: needed for mkv; providing feedkeys true/false still neded?
// TODO: are these limited to HINTS mode? // TODO: are these limited to HINTS mode?
// Only set for hints maps // Only set for hints maps
@@ -66,30 +68,27 @@ vimperator.Map = function (modes, cmds, action, extraInfo) //{{{
} }
}; };
vimperator.Map.prototype.hasName = function (name) vimperator.Map.prototype = {
{
for (var i = 0; i < this.names.length; i++) hasName: function (name)
{ {
if (this.names[i] == name) return this.names.some(function (e) { return e == name; });
return true; },
execute: function (motion, count, argument)
{
var args = [];
if (this.flags & vimperator.Mappings.flags.MOTION)
args.push(motion);
if (this.flags & vimperator.Mappings.flags.COUNT)
args.push(count);
if (this.flags & vimperator.Mappings.flags.ARGUMENT)
args.push(argument);
return this.action.apply(this, args);
} }
return false;
};
// Since we will add many Map-objects, we add some functions as prototypes
// this will ensure we only have one copy of each function, not one for each object
vimperator.Map.prototype.execute = function (motion, count, argument)
{
var args = [];
if (this.flags & vimperator.Mappings.flags.MOTION)
args.push(motion);
if (this.flags & vimperator.Mappings.flags.COUNT)
args.push(count);
if (this.flags & vimperator.Mappings.flags.ARGUMENT)
args.push(argument);
return this.action.apply(this, args);
}; };
//}}} //}}}
@@ -99,36 +98,23 @@ vimperator.Mappings = function () //{{{
////////////////////// PRIVATE SECTION ///////////////////////////////////////// ////////////////////// PRIVATE SECTION /////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////{{{ /////////////////////////////////////////////////////////////////////////////{{{
var main = []; // array of default Map() objects var main = []; // default mappings
var user = []; // array of objects created by :map or :cmap var user = []; // user created mappings
for each (var mode in vimperator.modes) for (var mode in vimperator.modes)
{ {
main[mode] = []; main[mode] = [];
user[mode] = []; user[mode] = [];
} }
//vimperator.modes.forEach(function (mode) { main[mode] = user[mode] = []; });
function addDefaultMap(map) function addDefaultMap(map)
{ {
//for (var i = 0; i < map.modes.length; i++) map.modes.forEach(function (mode) { main[mode].push(map); });
//{
// var mode = map.modes[i];
// //if (!main[mode])
// // main[mode] = [];
// main[mode].push(map);
//}
map.modes.forEach(function (mode) { main[mode].push(map) });
} }
function getMap(mode, cmd, stack) function getMap(mode, cmd, stack)
{ {
//if (!stack || !stack[mode] || !stack[mode].length)
// return null;
var maps = stack[mode]; var maps = stack[mode];
var names;
for (var i = 0; i < maps.length; i++) for (var i = 0; i < maps.length; i++)
{ {
@@ -166,9 +152,6 @@ vimperator.Mappings = function () //{{{
{ {
var mappings = stack[mode]; var mappings = stack[mode];
//// FIXME: do we want to document user commands by default?
//mappings = user[mode].concat(main[mode]);
for (var i = 0; i < mappings.length; i++) for (var i = 0; i < mappings.length; i++)
yield mappings[i]; yield mappings[i];
@@ -195,13 +178,13 @@ vimperator.Mappings = function () //{{{
return mappingsIterator(vimperator.modes.NORMAL, main); return mappingsIterator(vimperator.modes.NORMAL, main);
}, },
// FIXME // FIXME:
getIterator: function (mode) getDefaultIterator: function (mode)
{ {
return mappingsIterator(mode, main); return mappingsIterator(mode, main);
}, },
// FIXME // FIXME:
getUserIterator: function (mode) getUserIterator: function (mode)
{ {
return mappingsIterator(mode, user); return mappingsIterator(mode, user);
@@ -209,15 +192,7 @@ vimperator.Mappings = function () //{{{
hasMap: function (mode, cmd) hasMap: function (mode, cmd)
{ {
var userMaps = user[mode]; return userMaps.some(function (map) { return map.hasName(cmd); });
for (var i = 0; i < userMaps.length; i++)
{
if (userMaps[i].names.indexOf(cmd) != -1)
return true;
}
return false;
}, },
add: function (map) add: function (map)
@@ -225,7 +200,7 @@ vimperator.Mappings = function () //{{{
for (var i = 0; i < map.names.length; i++) for (var i = 0; i < map.names.length; i++)
{ {
// only store keysyms with uppercase modifier strings // only store keysyms with uppercase modifier strings
map.names[i] = map.names[i].replace(/[casm]-/g, function ($0) { return $0.toUpperCase(); }); map.names[i] = map.names[i].replace(/[casm]-/g, function (name) { return name.toUpperCase(); });
for (var j = 0; j < map.modes.length; j++) for (var j = 0; j < map.modes.length; j++)
removeMap(map.modes[j], map.names[i]); removeMap(map.modes[j], map.names[i]);
} }
@@ -246,16 +221,11 @@ vimperator.Mappings = function () //{{{
get: function (mode, cmd) get: function (mode, cmd)
{ {
var map = getMap(mode, cmd, user); return getMap(mode, cmd, user) || getMap(mode, cmd, main);
if (!map)
map = getMap(mode, cmd, main);
return map;
}, },
// TODO: move default maps to their own v.normal namespace // TODO: move default maps to their own v.normal namespace
getDefaultMap: function (mode, cmd) getDefault: function (mode, cmd)
{ {
return getMap(mode, cmd, main); return getMap(mode, cmd, main);
}, },
@@ -263,11 +233,9 @@ vimperator.Mappings = function () //{{{
// returns an array of mappings with names which start with "cmd" // returns an array of mappings with names which start with "cmd"
getCandidates: function (mode, cmd) getCandidates: function (mode, cmd)
{ {
var mappings = []; var mappings = user[mode].concat(main[mode]);
var matches = []; var matches = [];
mappings = user[mode].concat(main[mode]);
for (var i = 0; i < mappings.length; i++) for (var i = 0; i < mappings.length; i++)
{ {
var map = mappings[i]; var map = mappings[i];
@@ -275,7 +243,7 @@ vimperator.Mappings = function () //{{{
{ {
if (map.names[j].indexOf(cmd) == 0) if (map.names[j].indexOf(cmd) == 0)
{ {
// for < only return a candidate if it doesn't seem like a <c-x> mapping // for < only return a candidate if it doesn't look like a <c-x> mapping
if (cmd != "<" || !/^<.+>/.test(map.names[j])) if (cmd != "<" || !/^<.+>/.test(map.names[j]))
matches.push(map); matches.push(map);
} }
@@ -318,32 +286,34 @@ vimperator.Mappings = function () //{{{
////////////////////// DEFAULT MAPPINGS //////////////////////////////////////// ////////////////////// DEFAULT MAPPINGS ////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////{{{ /////////////////////////////////////////////////////////////////////////////{{{
var anymode = [vimperator.modes.NORMAL, var allModes = [vimperator.modes.NONE,
vimperator.modes.INSERT, vimperator.modes.NORMAL,
vimperator.modes.VISUAL, vimperator.modes.INSERT,
vimperator.modes.HINTS, vimperator.modes.VISUAL,
vimperator.modes.COMMAND_LINE, vimperator.modes.HINTS,
vimperator.modes.CARET, vimperator.modes.COMMAND_LINE,
vimperator.modes.TEXTAREA]; vimperator.modes.CARET,
var anyNoninsertMode = [vimperator.modes.NORMAL, vimperator.modes.TEXTAREA];
vimperator.modes.VISUAL,
vimperator.modes.HINTS, var noninsertModes = [vimperator.modes.NORMAL,
vimperator.modes.CARET, vimperator.modes.VISUAL,
vimperator.modes.TEXTAREA]; vimperator.modes.HINTS,
vimperator.modes.CARET,
vimperator.modes.TEXTAREA];
// //
// NORMAL mode // NORMAL mode
// {{{ // {{{
// vimperator management // vimperator management
addDefaultMap(new vimperator.Map(anymode, ["<F1>"], addDefaultMap(new vimperator.Map(allModes, ["<F1>"],
function () { vimperator.help(null); }, function () { vimperator.help(null); },
{ {
shortHelp: "Open help window", shortHelp: "Open help window",
help: "The default section is shown, if you need help for a specific topic, try <code class=\"command\">:help &lt;F1&gt;</code>." help: "The default section is shown, if you need help for a specific topic, try <code class=\"command\">:help &lt;F1&gt;</code>."
} }
)); ));
addDefaultMap(new vimperator.Map(anymode, ["<Esc>", "<C-[>"], addDefaultMap(new vimperator.Map(allModes, ["<Esc>", "<C-[>"],
vimperator.events.onEscape, vimperator.events.onEscape,
{ {
shortHelp: "Focus content", shortHelp: "Focus content",
@@ -351,7 +321,7 @@ vimperator.Mappings = function () //{{{
"Also focuses the web page, in case a form field has focus and eats our key presses." "Also focuses the web page, in case a form field has focus and eats our key presses."
} }
)); ));
addDefaultMap(new vimperator.Map(anyNoninsertMode, [":"], addDefaultMap(new vimperator.Map(noninsertModes, [":"],
function () { vimperator.commandline.open(":", "", vimperator.modes.EX); }, function () { vimperator.commandline.open(":", "", vimperator.modes.EX); },
{ {
shortHelp: "Start command line mode", shortHelp: "Start command line mode",
@@ -371,7 +341,7 @@ vimperator.Mappings = function () //{{{
"If you want to select text in this mode, press <code class=\"mapping\">v</code> to start its Visual mode." "If you want to select text in this mode, press <code class=\"mapping\">v</code> to start its Visual mode."
} }
)); ));
addDefaultMap(new vimperator.Map(anymode, ["<C-q>"], addDefaultMap(new vimperator.Map(allModes, ["<C-q>"],
function () { vimperator.modes.passAllKeys = true; }, function () { vimperator.modes.passAllKeys = true; },
{ {
shortHelp: "Temporarily quit Vimperator mode", shortHelp: "Temporarily quit Vimperator mode",
@@ -381,7 +351,7 @@ vimperator.Mappings = function () //{{{
"in this mode to the web page, prepend it with <code class=\"mapping\">&lt;C-v&gt;</code>." "in this mode to the web page, prepend it with <code class=\"mapping\">&lt;C-v&gt;</code>."
} }
)); ));
addDefaultMap(new vimperator.Map(anymode, ["<C-v>"], addDefaultMap(new vimperator.Map(allModes, ["<C-v>"],
function () { vimperator.modes.passNextKey = true; }, function () { vimperator.modes.passNextKey = true; },
{ {
shortHelp: "Pass through next key", shortHelp: "Pass through next key",
@@ -397,7 +367,7 @@ vimperator.Mappings = function () //{{{
help: "Stops loading the current web page." help: "Stops loading the current web page."
} }
)); ));
addDefaultMap(new vimperator.Map(anymode, ["<Nop>"], addDefaultMap(new vimperator.Map(allModes, ["<Nop>"],
function () { return; }, function () { return; },
{ {
shortHelp: "Do nothing", shortHelp: "Do nothing",

View File

@@ -154,6 +154,17 @@ vimperator.modes = (function () //{{{
MENU: 1 << 18, // a popupmenu is active MENU: 1 << 18, // a popupmenu is active
LINE: 1 << 19, // linewise visual mode LINE: 1 << 19, // linewise visual mode
__iterator__: function ()
{
var modes = [this.NONE, this.NORMAL, this.INSERT, this.VISUAL,
this.HINTS, this.COMMAND_LINE, this.CARET, this.TEXTAREA];
for (var i = 0; i < modes.length; i++)
yield modes[i];
throw StopIteration;
},
reset: function (silent) reset: function (silent)
{ {
this.set(vimperator.modes.NORMAL, vimperator.modes.NONE, silent); this.set(vimperator.modes.NORMAL, vimperator.modes.NONE, silent);