Add generic label mechanism
[conkeror.git] / modules / global-overlay-keymap.js
blobc49edc670760670ecfc0f70bcdb9c7ad99553963
1 /**
2 * (C) Copyright 2008 Jeremy Maitin-Shepard
4 * Use, modification, and distribution are subject to the terms specified in the
5 * COPYING file.
6 **/
8 require("keyboard.js");
10 var global_overlay_keymap = new keymap();
12 function global_overlay_keymap_handler(window, ctx, true_event) {
13 var binding = lookup_key_binding(global_overlay_keymap, ctx.event);
14 if (!binding)
15 return false;
16 if (!binding.fallthrough)
18 true_event.preventDefault();
19 true_event.stopPropagation();
22 if (binding.command)
23 call_interactively(ctx, binding.command);
25 return true;
28 define_global_mode("global_overlay_keymap_mode",
29 function () {
30 add_hook("key_press_hook", global_overlay_keymap_handler);
32 function () {
33 remove_hook("key_press_hook", global_overlay_keymap_handler);
34 });
36 function define_key_alias(typed_key, generated_key) {
37 typed_key = kbd(typed_key);
38 generated_key = kbd(generated_key);
39 var formatted = format_key_spec(generated_key[0]);
40 var name = "generate-key-event:" + formatted;
41 interactive(name,
42 "Generate a fake key press event for the key: " + formatted,
43 function (I) {
44 send_key_as_event(I.window, I.buffer.focused_element, generated_key);
45 });
46 define_key(global_overlay_keymap, typed_key, name);
47 global_overlay_keymap_mode(true);
49 ignore_function_for_get_caller_source_code_reference("define_key_alias");
52 function define_sticky_modifier(typed_key, modifiers) {
53 typed_key = kbd(typed_key);
54 var mod_str = "";
55 for (var i = 0; i < modifier_names.length; ++i)
57 if (modifiers & (1 << i)) {
58 if (mod_str.length > 0)
59 mod_str = mod_str + "-";
60 mod_str = mod_str + modifier_names[i];
63 if (mod_str.length == 0)
64 throw new Error("Invalid modifiers: " + modifiers);
65 var name = "sticky-modifiers:" + mod_str;
66 interactive(name, "Set sticky modifiers: " + mod_str, function (I) {
67 I.sticky_modifiers |= modifiers;
68 });
69 define_key(global_overlay_keymap, typed_key, name);
70 global_overlay_keymap_mode(true);
72 ignore_function_for_get_caller_source_code_reference("define_sticky_modifier");