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

merge improved :noremap

This commit is contained in:
Doug Kearns
2007-11-09 12:58:22 +00:00
parent 61c55f6f1d
commit 8a218d19e3
2 changed files with 46 additions and 84 deletions

View File

@@ -780,11 +780,7 @@ vimperator.Commands = function() //{{{
"Without arguments, displays a list of all variables." "Without arguments, displays a list of all variables."
} }
)); ));
addDefaultCommand(new vimperator.Command(["map"], function map(args, noremap)
// 0 args -> list all maps
// 1 arg -> list the maps starting with args
// 2 args -> map arg1 to arg*
function(args)
{ {
if (!args) if (!args)
{ {
@@ -807,7 +803,7 @@ vimperator.Commands = function() //{{{
if (rhs) if (rhs)
{ {
vimperator.mappings.add(new vimperator.Map(vimperator.modes.NORMAL, [lhs], vimperator.mappings.add(new vimperator.Map(vimperator.modes.NORMAL, [lhs],
function(count) { vimperator.events.feedkeys((count > 1 ? count : "") + rhs); }, function(count) { vimperator.events.feedkeys((count > 1 ? count : "") + rhs, noremap); },
{ flags: vimperator.Mappings.flags.COUNT, rhs: rhs } { flags: vimperator.Mappings.flags.COUNT, rhs: rhs }
)); ));
} }
@@ -816,7 +812,9 @@ vimperator.Commands = function() //{{{
// FIXME: no filtering for now // FIXME: no filtering for now
vimperator.mappings.list(vimperator.modes.NORMAL, lhs); vimperator.mappings.list(vimperator.modes.NORMAL, lhs);
} }
}, }
addDefaultCommand(new vimperator.Command(["map"],
function(args) { map(args, false) },
{ {
usage: ["map {lhs} {rhs}", "map {lhs}", "map"], usage: ["map {lhs} {rhs}", "map {lhs}", "map"],
short_help: "Map the key sequence {lhs} to {rhs}", short_help: "Map the key sequence {lhs} to {rhs}",
@@ -968,56 +966,11 @@ vimperator.Commands = function() //{{{
)); ));
// TODO: remove duplication in :map // TODO: remove duplication in :map
addDefaultCommand(new vimperator.Command(["no[remap]"], addDefaultCommand(new vimperator.Command(["no[remap]"],
// 0 args -> list all maps function(args) { map(args, true) },
// 1 arg -> list the maps starting with args
// 2 args -> map arg1 to arg*
function(args)
{
if (!args)
{
vimperator.mappings.list(vimperator.modes.NORMAL);
return;
}
var matches = args.match(/^([^ ]+)(?:\s+(.+))?$/);
var [lhs, rhs] = [matches[1], matches[2]];
if (rhs)
{
if (/^:/.test(rhs))
{
vimperator.mappings.add(
new vimperator.Map(vimperator.modes.NORMAL, [lhs], function() { vimperator.execute(rhs); }, { rhs: rhs })
);
}
else
{
// NOTE: we currently only allow one normal mode command in {rhs}
var map = vimperator.mappings.getDefaultMap(vimperator.modes.NORMAL, rhs);
// create a Map for {lhs} with the same action as
// {rhs}...until we have feedkeys().
// NOTE: Currently only really intended for static use (e.g.
// from the RC file) since {rhs} is evaluated when the map
// is created not at runtime
if (map)
vimperator.mappings.add(
new vimperator.Map(vimperator.modes.NORMAL, [lhs], map.action, { flags: map.flags, rhs: rhs })
);
else
vimperator.echoerr("E475: Invalid argument: " + "{rhs} must be a existing singular mapping");
}
}
else
{
// FIXME: no filtering for now
vimperator.mappings.list(vimperator.modes.NORMAL, lhs);
}
},
{ {
usage: ["no[remap] {lhs} {rhs}", "no[remap] {lhs}", "no[remap]"], usage: ["no[remap] {lhs} {rhs}", "no[remap] {lhs}", "no[remap]"],
short_help: "Map the key sequence {lhs} to {rhs}", short_help: "Map the key sequence {lhs} to {rhs}",
help: "No remapping of the <code class=\"argument\">{rhs}</code> is performed.<br/>NOTE: :noremap does not yet work as reliably as :map." help: "No remapping of the <code class=\"argument\">{rhs}</code> is performed."
} }
)); ));
addDefaultCommand(new vimperator.Command(["o[pen]", "e[dit]"], addDefaultCommand(new vimperator.Command(["o[pen]", "e[dit]"],

View File

@@ -256,12 +256,14 @@ vimperator.Events = function() //{{{
// //
// @param keys: a string like "2<C-f>" to pass // @param keys: a string like "2<C-f>" to pass
// if you want < to be taken literally, prepend it with a \\ // if you want < to be taken literally, prepend it with a \\
this.feedkeys = function(keys) this.feedkeys = function(keys, noremap)
{ {
var doc = window.document; var doc = window.document;
var view = window.document.defaultView; var view = window.document.defaultView;
var escapeKey = false; // \ to escape some special keys var escapeKey = false; // \ to escape some special keys
noremap = !!noremap;
for (var i = 0; i < keys.length; i++) for (var i = 0; i < keys.length; i++)
{ {
var charCode = keys.charCodeAt(i); var charCode = keys.charCodeAt(i);
@@ -270,7 +272,7 @@ vimperator.Events = function() //{{{
//if (charCode == 92) // the '\' key FIXME: support the escape key //if (charCode == 92) // the '\' key FIXME: support the escape key
if (charCode == 60 && !escapeKey) // the '<' key starts a complex key if (charCode == 60 && !escapeKey) // the '<' key starts a complex key
{ {
var matches = keys.substr(i+1).match(/([CSMAcsma]-)*([^>]+)/); var matches = keys.substr(i + 1).match(/([CSMAcsma]-)*([^>]+)/);
if (matches && matches[2]) if (matches && matches[2])
{ {
if (matches[1]) // check for modifiers if (matches[1]) // check for modifiers
@@ -294,8 +296,10 @@ vimperator.Events = function() //{{{
{ {
charCode = 0; 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; return;
}
i += matches[0].length + 1; i += matches[0].length + 1;
} }
@@ -306,7 +310,8 @@ vimperator.Events = function() //{{{
elem = window.content; elem = window.content;
var evt = doc.createEvent("KeyEvents"); var evt = doc.createEvent("KeyEvents");
evt.initKeyEvent("keypress", true, true, view, ctrl, alt, shift, meta, keyCode, charCode ); evt.initKeyEvent("keypress", true, true, view, ctrl, alt, shift, meta, keyCode, charCode);
evt.noremap = noremap;
elem.dispatchEvent(evt); elem.dispatchEvent(evt);
} }
} }
@@ -600,6 +605,10 @@ vimperator.Events = function() //{{{
var count_str = vimperator.input.buffer.match(/^[0-9]*/)[0]; var count_str = vimperator.input.buffer.match(/^[0-9]*/)[0];
var candidate_command = (vimperator.input.buffer + key).replace(count_str, ""); var candidate_command = (vimperator.input.buffer + key).replace(count_str, "");
var map; var map;
if (event.noremap)
map = vimperator.mappings.getDefaultMap(vimperator.modes.NORMAL, candidate_command);
else
map = vimperator.mappings.get(vimperator.modes.NORMAL, candidate_command);
// counts must be at the start of a complete mapping (10j -> go 10 lines down) // counts must be at the start of a complete mapping (10j -> go 10 lines down)
if ((vimperator.input.buffer + key).match(/^[1-9][0-9]*$/)) if ((vimperator.input.buffer + key).match(/^[1-9][0-9]*$/))
@@ -619,7 +628,7 @@ vimperator.Events = function() //{{{
vimperator.input.pendingArgMap = null; vimperator.input.pendingArgMap = null;
} }
else if (map = vimperator.mappings.get(vimperator.modes.NORMAL, candidate_command)) else if (map)
{ {
vimperator.input.count = parseInt(count_str, 10); vimperator.input.count = parseInt(count_str, 10);
if (isNaN(vimperator.input.count)) if (isNaN(vimperator.input.count))