1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-22 23:18:01 +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],
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

View File

@@ -39,19 +39,23 @@ vimperator.Events = function () //{{{
// any tab related events
var tabcontainer = getBrowser().tabContainer;
tabcontainer.addEventListener("TabMove", function (event) {
tabcontainer.addEventListener("TabMove", function (event)
{
vimperator.statusline.updateTabCount();
vimperator.buffer.updateBufferList();
}, false);
tabcontainer.addEventListener("TabOpen", function (event) {
tabcontainer.addEventListener("TabOpen", function (event)
{
vimperator.statusline.updateTabCount();
vimperator.buffer.updateBufferList();
}, false);
tabcontainer.addEventListener("TabClose", function (event) {
tabcontainer.addEventListener("TabClose", function (event)
{
vimperator.statusline.updateTabCount();
vimperator.buffer.updateBufferList();
}, false);
tabcontainer.addEventListener("TabSelect", function (event) {
tabcontainer.addEventListener("TabSelect", function (event)
{
if (vimperator.mode == vimperator.modes.HINTS)
vimperator.modes.reset();
@@ -459,7 +463,8 @@ vimperator.Events = function () //{{{
vimperator.mode == vimperator.modes.VISUAL)
{
this.wantsModeReset = true;
setTimeout(function () {
setTimeout(function ()
{
if (vimperator.events.wantsModeReset)
vimperator.modes.reset();
}, 10);
@@ -646,7 +651,7 @@ vimperator.Events = function () //{{{
var candidateCommand = (vimperator.input.buffer + key).replace(countStr, "");
var map;
if (event.noremap)
map = vimperator.mappings.getDefaultMap(vimperator.mode, candidateCommand);
map = vimperator.mappings.getDefault(vimperator.mode, candidateCommand);
else
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;
if (extraInfo.usage)
{
this.usage = extraInfo.usage;
}
else
{
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.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?
// Only set for hints maps
@@ -66,30 +68,27 @@ vimperator.Map = function (modes, cmds, action, extraInfo) //{{{
}
};
vimperator.Map.prototype.hasName = function (name)
{
for (var i = 0; i < this.names.length; i++)
vimperator.Map.prototype = {
hasName: function (name)
{
if (this.names[i] == name)
return true;
return this.names.some(function (e) { return e == name; });
},
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 /////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////{{{
var main = []; // array of default Map() objects
var user = []; // array of objects created by :map or :cmap
var main = []; // default mappings
var user = []; // user created mappings
for each (var mode in vimperator.modes)
for (var mode in vimperator.modes)
{
main[mode] = [];
user[mode] = [];
}
//vimperator.modes.forEach(function (mode) { main[mode] = user[mode] = []; });
function addDefaultMap(map)
{
//for (var i = 0; i < map.modes.length; i++)
//{
// var mode = map.modes[i];
// //if (!main[mode])
// // main[mode] = [];
// main[mode].push(map);
//}
map.modes.forEach(function (mode) { main[mode].push(map) });
map.modes.forEach(function (mode) { main[mode].push(map); });
}
function getMap(mode, cmd, stack)
{
//if (!stack || !stack[mode] || !stack[mode].length)
// return null;
var maps = stack[mode];
var names;
for (var i = 0; i < maps.length; i++)
{
@@ -166,9 +152,6 @@ vimperator.Mappings = function () //{{{
{
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++)
yield mappings[i];
@@ -195,13 +178,13 @@ vimperator.Mappings = function () //{{{
return mappingsIterator(vimperator.modes.NORMAL, main);
},
// FIXME
getIterator: function (mode)
// FIXME:
getDefaultIterator: function (mode)
{
return mappingsIterator(mode, main);
},
// FIXME
// FIXME:
getUserIterator: function (mode)
{
return mappingsIterator(mode, user);
@@ -209,15 +192,7 @@ vimperator.Mappings = function () //{{{
hasMap: function (mode, cmd)
{
var userMaps = user[mode];
for (var i = 0; i < userMaps.length; i++)
{
if (userMaps[i].names.indexOf(cmd) != -1)
return true;
}
return false;
return userMaps.some(function (map) { return map.hasName(cmd); });
},
add: function (map)
@@ -225,7 +200,7 @@ vimperator.Mappings = function () //{{{
for (var i = 0; i < map.names.length; i++)
{
// 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++)
removeMap(map.modes[j], map.names[i]);
}
@@ -246,16 +221,11 @@ vimperator.Mappings = function () //{{{
get: function (mode, cmd)
{
var map = getMap(mode, cmd, user);
if (!map)
map = getMap(mode, cmd, main);
return map;
return getMap(mode, cmd, user) || getMap(mode, cmd, main);
},
// TODO: move default maps to their own v.normal namespace
getDefaultMap: function (mode, cmd)
getDefault: function (mode, cmd)
{
return getMap(mode, cmd, main);
},
@@ -263,11 +233,9 @@ vimperator.Mappings = function () //{{{
// returns an array of mappings with names which start with "cmd"
getCandidates: function (mode, cmd)
{
var mappings = [];
var mappings = user[mode].concat(main[mode]);
var matches = [];
mappings = user[mode].concat(main[mode]);
for (var i = 0; i < mappings.length; i++)
{
var map = mappings[i];
@@ -275,7 +243,7 @@ vimperator.Mappings = function () //{{{
{
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]))
matches.push(map);
}
@@ -318,32 +286,34 @@ vimperator.Mappings = function () //{{{
////////////////////// DEFAULT MAPPINGS ////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////{{{
var anymode = [vimperator.modes.NORMAL,
vimperator.modes.INSERT,
vimperator.modes.VISUAL,
vimperator.modes.HINTS,
vimperator.modes.COMMAND_LINE,
vimperator.modes.CARET,
vimperator.modes.TEXTAREA];
var anyNoninsertMode = [vimperator.modes.NORMAL,
vimperator.modes.VISUAL,
vimperator.modes.HINTS,
vimperator.modes.CARET,
vimperator.modes.TEXTAREA];
var allModes = [vimperator.modes.NONE,
vimperator.modes.NORMAL,
vimperator.modes.INSERT,
vimperator.modes.VISUAL,
vimperator.modes.HINTS,
vimperator.modes.COMMAND_LINE,
vimperator.modes.CARET,
vimperator.modes.TEXTAREA];
var noninsertModes = [vimperator.modes.NORMAL,
vimperator.modes.VISUAL,
vimperator.modes.HINTS,
vimperator.modes.CARET,
vimperator.modes.TEXTAREA];
//
// NORMAL mode
// {{{
// vimperator management
addDefaultMap(new vimperator.Map(anymode, ["<F1>"],
addDefaultMap(new vimperator.Map(allModes, ["<F1>"],
function () { vimperator.help(null); },
{
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>."
}
));
addDefaultMap(new vimperator.Map(anymode, ["<Esc>", "<C-[>"],
addDefaultMap(new vimperator.Map(allModes, ["<Esc>", "<C-[>"],
vimperator.events.onEscape,
{
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."
}
));
addDefaultMap(new vimperator.Map(anyNoninsertMode, [":"],
addDefaultMap(new vimperator.Map(noninsertModes, [":"],
function () { vimperator.commandline.open(":", "", vimperator.modes.EX); },
{
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."
}
));
addDefaultMap(new vimperator.Map(anymode, ["<C-q>"],
addDefaultMap(new vimperator.Map(allModes, ["<C-q>"],
function () { vimperator.modes.passAllKeys = true; },
{
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>."
}
));
addDefaultMap(new vimperator.Map(anymode, ["<C-v>"],
addDefaultMap(new vimperator.Map(allModes, ["<C-v>"],
function () { vimperator.modes.passNextKey = true; },
{
shortHelp: "Pass through next key",
@@ -397,7 +367,7 @@ vimperator.Mappings = function () //{{{
help: "Stops loading the current web page."
}
));
addDefaultMap(new vimperator.Map(anymode, ["<Nop>"],
addDefaultMap(new vimperator.Map(allModes, ["<Nop>"],
function () { return; },
{
shortHelp: "Do nothing",

View File

@@ -154,6 +154,17 @@ vimperator.modes = (function () //{{{
MENU: 1 << 18, // a popupmenu is active
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)
{
this.set(vimperator.modes.NORMAL, vimperator.modes.NONE, silent);