diff --git a/content/commands.js b/content/commands.js index 50f6b9fb..c9ab6316 100644 --- a/content/commands.js +++ b/content/commands.js @@ -1202,43 +1202,44 @@ vimperator.Commands = function() //{{{ function(args) { vimperator.editor.removeAllAbbreviations("i"); }, { short_help: "Remove all abbreviations for Insert mode" } )); - addDefaultCommand(new vimperator.Command(["map"], - // 0 args -> list all maps - // 1 arg -> list the maps starting with args - // 2 args -> map arg1 to arg* - function(args) + // 0 args -> list all maps + // 1 arg -> list the maps starting with args + // 2 args -> map arg1 to arg* + function map(args, noremap) + { + if (!args) { - if (!args) - { - vimperator.mappings.list(vimperator.modes.NORMAL); - return; - } + vimperator.mappings.list(vimperator.modes.NORMAL); + return; + } - var matches = args.match(/^([^\s]+)(?:\s+(.+))?$/); - var [lhs, rhs] = [matches[1], matches[2]]; - var leader_reg = //i; + var matches = args.match(/^([^\s]+)(?:\s+(.+))?$/); + var [lhs, rhs] = [matches[1], matches[2]]; + var leader_reg = //i; - if (leader_reg.test(lhs)) - { - var leader_ref = vimperator.variableReference("mapleader"); - var leader = leader_ref[0] ? leader_ref[0][leader_ref[1]] : "\\"; + if (leader_reg.test(lhs)) + { + var leader_ref = vimperator.variableReference("mapleader"); + var leader = leader_ref[0] ? leader_ref[0][leader_ref[1]] : "\\"; - lhs = lhs.replace(leader_reg, leader); - } + lhs = lhs.replace(leader_reg, leader); + } - if (rhs) - { - vimperator.mappings.add(new vimperator.Map([vimperator.modes.NORMAL], [lhs], - function(count) { vimperator.events.feedkeys((count > 1 ? count : "") + rhs); }, - { flags: vimperator.Mappings.flags.COUNT, rhs: rhs } - )); - } - else - { - // FIXME: no filtering for now - vimperator.mappings.list(vimperator.modes.NORMAL, lhs); - } - }, + if (rhs) + { + vimperator.mappings.add(new vimperator.Map([vimperator.modes.NORMAL], [lhs], + function(count) { vimperator.events.feedkeys((count > 1 ? count : "") + rhs, noremap); }, + { flags: vimperator.Mappings.flags.COUNT, rhs: rhs } + )); + } + else + { + // FIXME: no filtering for now + vimperator.mappings.list(vimperator.modes.NORMAL, lhs); + } + } + addDefaultCommand(new vimperator.Command(["map"], + function(args) { map(args, false) }, { usage: ["map {lhs} {rhs}", "map {lhs}", "map"], short_help: "Map the key sequence {lhs} to {rhs}", @@ -1397,56 +1398,12 @@ vimperator.Commands = function() //{{{ )); // TODO: remove duplication in :map addDefaultCommand(new vimperator.Command(["no[remap]"], - // 0 args -> list all maps - // 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 new 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); - } - }, + function(args) { map(args, true) }, { usage: ["no[remap] {lhs} {rhs}", "no[remap] {lhs}", "no[remap]"], short_help: "Map the key sequence {lhs} to {rhs}", - help: "No remapping of the {rhs} is performed.
NOTE: :noremap does not yet work as reliably as :map." + help: "No remapping of the {rhs} is performed.
" + + "NOTE: :noremap does not yet work as reliably as :map." } )); addDefaultCommand(new vimperator.Command(["o[pen]", "e[dit]"], diff --git a/content/events.js b/content/events.js index abd74bb2..bf3cde7c 100644 --- a/content/events.js +++ b/content/events.js @@ -268,12 +268,14 @@ vimperator.Events = function() //{{{ // // @param keys: a string like "2" to pass // 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 view = window.document.defaultView; var escapeKey = false; // \ to escape some special keys + noremap = !!noremap; + for (var i = 0; i < keys.length; i++) { var charCode = keys.charCodeAt(i); @@ -282,7 +284,7 @@ vimperator.Events = function() //{{{ //if (charCode == 92) // the '\' key FIXME: support the escape 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[1]) // check for modifiers @@ -306,8 +308,10 @@ vimperator.Events = function() //{{{ { charCode = 0; } - else //an invalid key like was found, stop propagation here (like vim) + else //an invalid key like was found, stop propagation here (like Vim) + { return; + } i += matches[0].length + 1; } @@ -318,7 +322,8 @@ vimperator.Events = function() //{{{ elem = window.content; 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); } } @@ -636,6 +641,10 @@ vimperator.Events = function() //{{{ var count_str = vimperator.input.buffer.match(/^[0-9]*/)[0]; var candidate_command = (vimperator.input.buffer + key).replace(count_str, ""); var map; + if (event.noremap) + map = vimperator.mappings.getDefaultMap(vimperator.mode, candidate_command); + else + map = vimperator.mappings.get(vimperator.mode, candidate_command); // 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]*$/)) @@ -655,7 +664,7 @@ vimperator.Events = function() //{{{ vimperator.input.pendingArgMap = null; } - else if (map = vimperator.mappings.get(vimperator.mode, candidate_command)) + else if (map) { vimperator.input.count = parseInt(count_str, 10); if (isNaN(vimperator.input.count))