From 6c7105128f0ce7229ae7c5658b578b386d1c4783 Mon Sep 17 00:00:00 2001 From: John Foerch Date: Fri, 23 Oct 2009 12:02:44 -0400 Subject: [PATCH] keymap_lookup: take array of keymaps keymap_lookup now expects to be passed an array of keymaps instead of just one keymap. This will allow keymap inheritances to be built dynamically at runtime as an alternative to defining them only statically with the $parent parameter to define_keymap. This capability is a step toward being able to have modes which install extra keymaps without having explicit dependencies one particular input-modes. A cleaner implementation of page-mode keymaps will become possible, as well as an implementation of caret-mode (and other modes which install keymaps) that does not conflict with page-modes. --- modules/global-overlay-keymap.js | 2 +- modules/input.js | 4 ++-- modules/keymap.js | 18 ++++++++++++------ tests/simple/keymap.js | 13 +++++++++++++ 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/modules/global-overlay-keymap.js b/modules/global-overlay-keymap.js index f8a005d..5f5f194 100644 --- a/modules/global-overlay-keymap.js +++ b/modules/global-overlay-keymap.js @@ -12,7 +12,7 @@ var global_overlay_keymap = new keymap(); function global_overlay_keymap_handler (window, I, true_event) { - var binding = keymap_lookup(global_overlay_keymap, I.combo, I.event); + var binding = keymap_lookup([global_overlay_keymap], I.combo, I.event); if (!binding) return false; if (!binding.fallthrough) diff --git a/modules/input.js b/modules/input.js index 15f5bfe..de230fc 100644 --- a/modules/input.js +++ b/modules/input.js @@ -165,8 +165,8 @@ sequence: var overlay_keymap = I.overlay_keymap; - var binding = (overlay_keymap && keymap_lookup(overlay_keymap, combo, event)) || - keymap_lookup(keymap, combo, event); + var binding = (overlay_keymap && keymap_lookup([overlay_keymap], combo, event)) || + keymap_lookup([keymap], combo, event); // kill event for any unbound key, or any bound key which // is not marked fallthrough diff --git a/modules/keymap.js b/modules/keymap.js index 45fdac4..f37d651 100644 --- a/modules/keymap.js +++ b/modules/keymap.js @@ -223,8 +223,10 @@ function format_binding_sequence (seq) { } -function keymap_lookup (kmap, combo, event) { - do { +function keymap_lookup (keymaps, combo, event) { + var i = keymaps.length - 1; + var kmap = keymaps[i]; + while (true) { // first check regular bindings var bindings = kmap.bindings; var bind = bindings[combo]; @@ -239,9 +241,13 @@ function keymap_lookup (kmap, combo, event) { return bind; } } - kmap = kmap.parent; - } while (kmap); - return null; + if (kmap.parent) + kmap = kmap.parent; + else if (i > 0) + kmap = keymaps[--i]; + else + return null; + } } @@ -567,7 +573,7 @@ invalid_key_binding.prototype = { function read_key_binding_key (window, state, event) { var combo = format_key_combo(event); - var binding = keymap_lookup(state.target_keymap, combo, event); + var binding = keymap_lookup([state.target_keymap], combo, event); state.key_sequence.push(combo); diff --git a/tests/simple/keymap.js b/tests/simple/keymap.js index ebb9912..3e4fd5c 100644 --- a/tests/simple/keymap.js +++ b/tests/simple/keymap.js @@ -61,3 +61,16 @@ walnut_run({ assert_not(test_keymap.bindings["C-a"]); } }); + +walnut_run({ + setup: function () { + define_keymap("test_keymap"); + define_key(test_keymap, "C-a", "foo"); + }, + teardown: function () { + delete conkeror.test_keymap; + }, + test_keymap_lookup_1: function () { + assert(keymap_lookup([test_keymap], "C-a")); + } +}); -- 2.11.4.GIT