2 * (C) Copyright 2004-2007 Shawn Betts
3 * (C) Copyright 2007-2010 John J. Foerch
4 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
6 * Use, modification, and distribution are subject to the terms specified in the
14 require("interactive.js");
16 define_variable("key_bindings_ignore_capslock", false,
17 "When true, the case of characters in key bindings will be based "+
18 "only on whether shift was pressed--upper-case if yes, lower-case if "+
19 "no. Effectively, this overrides the capslock key. This option has "+
20 "no effect on ordinary typing in input fields.");
22 define_variable("keyboard_key_sequence_help_timeout", 0,
23 "Delay (in millseconds) before the current key sequence prefix is "+
24 "displayed in the minibuffer.");
28 * event_clone is used to make a copy of an event which is safe to keep
29 * references to, because it will not reference any DOM trees directly
32 * A pertinent question would be, is this needed? Are there instances
33 * in Conkeror when an event reference is stored across a navigation
34 * boundary or buffer/window closing?
36 function event_clone (event) {
37 this.type = event.type;
38 this.keyCode = event.keyCode;
39 this.charCode = event.charCode;
40 this.ctrlKey = event.ctrlKey;
41 this.metaKey = event.metaKey;
42 this.altKey = event.altKey;
43 this.shiftKey = event.shiftKey;
44 this.sticky_modifiers = event.sticky_modifiers;
49 * event_kill stops an event from being processed by any further handlers.
51 function event_kill (event) {
52 event.preventDefault();
53 event.stopPropagation();
58 * command_event is a special event type that tells input_handle_sequence
59 * to run the given command.
61 function command_event (command) {
62 this.type = "command";
63 this.command = command;
68 * input_state makes an object that holds the state of a single key sequence.
69 * As a small measure of efficiency, these objects get recycled from one
70 * sequence to the next.
72 function input_state () {
73 this.fallthrough = {};
75 input_state.prototype = {
76 constructor: input_state,
83 * input_stack is a stack of input_states, which is to say a stack of
84 * recursed sequences. input recursion happens, for example, when a
85 * minibuffer read takes place in the middle of another sequence.
87 function input_stack () {
88 this.push(new input_state());
90 input_stack.prototype = {
91 constructor: input_stack,
92 __proto__: Array.prototype,
95 help_displayed: false,
97 toString: function () {
98 return "[input_stack ("+this.length+")]";
101 return this[this.length - 1];
103 begin_recursion: function () {
104 this.push(new input_state());
106 end_recursion: function () {
112 function input_help_timer_clear (window) {
113 if (window.input.help_timer != null) {
114 timer_cancel(window.input.help_timer);
115 window.input.help_timer = null;
120 function input_show_partial_sequence (window, I) {
121 if (window.input.help_displayed)
122 window.minibuffer.show(I.key_sequence.join(" "));
124 window.input.help_timer = call_after_timeout(function () {
125 window.minibuffer.show(I.key_sequence.join(" "));
126 window.input.help_displayed = true;
127 window.input.help_timer = null;
128 }, keyboard_key_sequence_help_timeout);
133 define_window_local_hook("keypress_hook", RUN_HOOK_UNTIL_SUCCESS,
134 "A run-until-success hook available for special keypress "+
135 "handling. Handlers receive as arguments the window, an "+
136 "interactive context, and the real keypress event. The "+
137 "handler is responsible for stopping event propagation, if "+
142 * get_current_keymaps returns the keymap stack for the current focus
143 * context of the given window. This is the top-level keymap stack, not
144 * the stack that represents any on-going key sequence.
146 function get_current_keymaps (window) {
147 var m = window.minibuffer;
148 var s = m.current_state;
149 if (m.active && s.keymaps)
151 return window.buffers.current.keymaps;
156 * input_handle_sequence is the main handler for all event types which
157 * can be part of a sequence. It is a coroutine procedure which gets
158 * started and resumed by various EventListeners, some of which have
159 * additional, special tasks.
161 function input_handle_sequence (event) {
164 var state = window.input.current;
165 state.continuation = yield CONTINUATION;
166 var I = new interactive_context(window.buffers.current);
168 I.sticky_modifiers = 0;
169 var keymaps = get_current_keymaps(window);
172 switch (event.type) {
174 //try the fallthrough predicates in our current keymap
175 if (keymap_lookup_fallthrough(keymaps[keymaps.length - 1], event)) {
176 //XXX: need to take account of modifers, too!
177 state.fallthrough[event.keyCode] = true;
182 window.minibuffer.clear();
183 window.input.help_displayed = false;
184 input_help_timer_clear(window);
187 var clone = new event_clone(event);
188 clone.sticky_modifiers = I.sticky_modifiers;
189 I.sticky_modifiers = 0;
190 if (key_bindings_ignore_capslock && clone.charCode) {
191 let c = String.fromCharCode(clone.charCode);
193 clone.charCode = c.toUpperCase().charCodeAt(0);
195 clone.charCode = c.toLowerCase().charCodeAt(0);
198 // make the combo string
199 var combo = format_key_combo(clone);
200 var canabort = I.key_sequence.push(combo) > 1;
204 // make active keymaps visible to commands
207 if (keypress_hook.run(window, I, event))
210 var overlay_keymap = I.overlay_keymap;
213 (canabort && keymap_lookup([sequence_abort_keymap], combo, event)) ||
214 (overlay_keymap && keymap_lookup([overlay_keymap], combo, event)) ||
215 keymap_lookup(keymaps, combo, event) ||
216 keymap_lookup([sequence_help_keymap], combo, event);
218 // kill event for any unbound key, or any bound key which
219 // is not marked fallthrough
220 if (!binding || !binding.fallthrough)
224 if (binding.browser_object != null)
225 I.binding_browser_object = binding.browser_object;
226 if (binding.constructor == Array) {
228 input_show_partial_sequence(window, I);
229 } else if (binding.command) {
230 let command = binding.command;
231 if (I.repeat == command)
232 command = binding.repeat;
233 yield call_interactively(I, command);
234 if (typeof command == "string" &&
235 interactive_commands.get(command).prefix)
237 keymaps = get_current_keymaps(window); //back to top keymap
238 input_show_partial_sequence(window, I);
245 break sequence; //reachable by keypress fallthroughs
248 window.minibuffer.message(I.key_sequence.join(" ") + " is undefined");
253 let (command = event.command) {
254 window.input.help_displayed = false;
255 input_help_timer_clear(window);
256 window.minibuffer.clear();
257 yield call_interactively(I, command);
258 if (! interactive_commands.get(command).prefix)
263 // should we expect more events?
265 event = yield SUSPEND;
271 delete state.continuation;
276 function input_handle_keydown (event) {
277 if (event.keyCode == 0 ||
278 event.keyCode == vk_name_to_keycode.shift ||
279 event.keyCode == vk_name_to_keycode.control ||
280 event.keyCode == vk_name_to_keycode.alt ||
281 event.keyCode == vk_name_to_keycode.caps_lock)
282 return event_kill(event);
284 var state = window.input.current;
285 if (state.continuation)
286 state.continuation(event);
288 co_call(input_handle_sequence.call(window, event));
292 function input_handle_keypress (event) {
293 if (event.keyCode == 0 && event.charCode == 0 ||
294 event.keyCode == vk_name_to_keycode.caps_lock)
295 return event_kill(event);
297 var state = window.input.current;
298 if (state.continuation)
299 state.continuation(event);
301 co_call(input_handle_sequence.call(window, event));
305 function input_handle_keyup (event) {
306 if (event.keyCode == 0 ||
307 event.keyCode == vk_name_to_keycode.shift ||
308 event.keyCode == vk_name_to_keycode.control ||
309 event.keyCode == vk_name_to_keycode.alt ||
310 event.keyCode == vk_name_to_keycode.caps_lock)
311 return event_kill(event);
313 var state = window.input.current;
314 if (state.fallthrough[event.keyCode])
315 delete state.fallthrough[event.keyCode];
321 // handler for command_event special events
322 function input_handle_command (event) {
324 var state = window.input.current;
325 if (typeof event == 'string')
326 event = new command_event(event);
327 if (state.continuation)
328 state.continuation(event);
330 co_call(input_handle_sequence.call(window, event));
334 // handler for special abort event
335 function input_sequence_abort (message) {
337 window.input.help_displayed = false;
338 input_help_timer_clear(window);
339 window.minibuffer.clear();
341 window.minibuffer.show(message);
342 delete window.input.current.continuation;
346 function input_initialize_window (window) {
347 window.input = new input_stack();
348 //window.addEventListener("keydown", input_handle_keydown, true);
349 window.addEventListener("keypress", input_handle_keypress, true);
350 //window.addEventListener("keyup", input_handle_keyup, true);
351 //TO-DO: mousedown, mouseup, click, dblclick
354 add_hook("window_initialize_hook", input_initialize_window);
357 interactive("sequence-abort",
358 "Abort an ongoing key sequence.",
359 function (I) { I.minibuffer.message("abort sequence"); });