media: scrapers are now independent of page-modes
[conkeror/arlinius.git] / modules / input.js
blob1df229288f90c7fc7cab90d8635791787a8e2026
1 /**
2  * (C) Copyright 2004-2007 Shawn Betts
3  * (C) Copyright 2007-2009 John J. Foerch
4  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
5  *
6  * Use, modification, and distribution are subject to the terms specified in the
7  * COPYING file.
8 **/
10 require("window.js");
11 require("keymap.js");
12 require("interactive.js");
14 define_variable("key_bindings_ignore_capslock", false,
15     "When true, the case of characters in key bindings will be based "+
16     "only on whether shift was pressed--upper-case if yes, lower-case if "+
17     "no.  Effectively, this overrides the capslock key.  This option has "+
18     "no effect on ordinary typing in input fields.");
20 define_variable("keyboard_key_sequence_help_timeout", 0,
21     "Delay (in millseconds) before the current key sequence prefix is "+
22     "displayed in the minibuffer.");
25 /**
26  * event_clone is used to make a copy of an event which is safe to keep
27  * references to, because it will not reference any DOM trees directly
28  * or indirectly.
29  *
30  * A pertinent question would be, is this needed?  Are there instances
31  * in Conkeror when an event reference is stored across a navigation
32  * boundary or buffer/window closing?
33  */
34 function event_clone (event) {
35     this.type = event.type;
36     this.keyCode = event.keyCode;
37     this.charCode = event.charCode;
38     this.ctrlKey = event.ctrlKey;
39     this.metaKey = event.metaKey;
40     this.altKey = event.altKey;
41     this.shiftKey = event.shiftKey;
42     this.sticky_modifiers = event.sticky_modifiers;
46 /**
47  * event_kill stops an event from being processed by any further handlers.
48  */
49 function event_kill (event) {
50     event.preventDefault();
51     event.stopPropagation();
55 /**
56  * command_event is a special event type that tells input_handle_sequence
57  * to run the given command.
58  */
59 function command_event (command) {
60     this.type = "command";
61     this.command = command;
65 /**
66  * input_state makes an object that holds the state of a single key sequence.
67  * As a small measure of efficiency, these objects get recycled from one
68  * sequence to the next.
69  */
70 function input_state (window) {
71     this.window = window;
72     this.fallthrough = {};
74 input_state.prototype = {
75     continuation: null,
77     // If this is non-null, it is used instead of the current buffer's
78     // keymap.  Used for minibuffer.
79     override_keymap: null //this should be stored in the minibuffer state, right?
83 function input_help_timer_clear (window) {
84     if (window.input.help_timer != null) {
85         timer_cancel(window.input.help_timer);
86         window.input.help_timer = null;
87     }
91 function input_show_partial_sequence (window, I) {
92     if (window.input.help_displayed)
93         window.minibuffer.show(I.key_sequence.join(" "));
94     else {
95         window.input.help_timer = call_after_timeout(function () {
96             window.minibuffer.show(I.key_sequence.join(" "));
97             window.input.help_displayed = true;
98             window.input.help_timer = null;
99         }, keyboard_key_sequence_help_timeout);
100     }
104 define_window_local_hook("keypress_hook", RUN_HOOK_UNTIL_SUCCESS,
105     "A run-until-success hook available for special keypress "+
106     "handling.  Handlers receive as arguments the window, an "+
107     "interactive context, and the real keypress event.  The "+
108     "handler is responsible for stopping event propagation, if "+
109     "that is desired.");
112 function get_current_keymaps (window) {
113     if (window.input.current.override_keymap)
114         return [window.input.current.override_keymap];
115     if (window.buffers.current.override_keymaps.length > 0)
116         return window.buffers.current.override_keymaps;
117     return window.buffers.current.keymaps;
122  * input_handle_sequence is the main handler for all event types which
123  * can be part of a sequence.  It is a coroutine procedure which gets
124  * started and resumed by various EventListeners, some of which have
125  * additional, special tasks.
126  */
127 function input_handle_sequence (event) {
128     try {
129         var window = this;
130         var state = window.input.current;
131         state.continuation = yield CONTINUATION;
132         var I = new interactive_context(window.buffers.current);
133         I.key_sequence = [];
134         I.sticky_modifiers = 0;
135         var keymaps = get_current_keymaps(window);
136 sequence:
137         while (true) {
138             switch (event.type) {
139             case "keydown":
140                 //try the fallthrough predicates in our current keymap
141                 if (keymap_lookup_fallthrough(keymaps[keymaps.length - 1], event)) {
142                     //XXX: need to take account of modifers, too!
143                     state.fallthrough[event.keyCode] = true;
144                 } else
145                     event_kill(event);
146                 break;
147             case "keypress":
148                 window.minibuffer.clear();
149                 window.input.help_displayed = false;
150                 input_help_timer_clear(window);
152                 // prepare the clone
153                 var clone = new event_clone(event);
154                 clone.sticky_modifiers = I.sticky_modifiers;
155                 I.sticky_modifiers = 0;
156                 if (key_bindings_ignore_capslock && clone.charCode) {
157                     let c = String.fromCharCode(clone.charCode);
158                     if (clone.shiftKey)
159                         clone.charCode = c.toUpperCase().charCodeAt(0);
160                     else
161                         clone.charCode = c.toLowerCase().charCodeAt(0);
162                 }
164                 // make the combo string
165                 var combo = format_key_combo(clone);
166                 I.key_sequence.push(combo);
167                 I.combo = combo;
168                 I.event = clone;
170                 if (keypress_hook.run(window, I, event))
171                     break;
173                 var overlay_keymap = I.overlay_keymap;
175                 var binding = (overlay_keymap && keymap_lookup([overlay_keymap], combo, event)) ||
176                     keymap_lookup(keymaps, combo, event);
178                 // kill event for any unbound key, or any bound key which
179                 // is not marked fallthrough
180                 if (!binding || !binding.fallthrough)
181                     event_kill(event);
183                 if (binding) {
184                     if (binding.browser_object != null)
185                         I.binding_browser_object = binding.browser_object;
186                     if (binding.constructor == Array) {
187                         keymaps = binding;
188                         input_show_partial_sequence(window, I);
189                     } else if (binding.command) {
190                         let command = binding.command;
191                         if (I.repeat == command)
192                             command = binding.repeat;
193                         yield call_interactively(I, command);
194                         if (typeof command == "string" &&
195                             interactive_commands.get(command).prefix)
196                         {
197                             keymaps = get_current_keymaps(window); //back to top keymap
198                             input_show_partial_sequence(window, I);
199                             if (binding.repeat)
200                                 I.repeat = command;
201                         } else {
202                             break sequence;
203                         }
204                     } else {
205                         break sequence; //reachable by keypress fallthroughs
206                     }
207                 } else {
208                     window.minibuffer.message(I.key_sequence.join(" ") + " is undefined");
209                     break sequence;
210                 }
211                 break;
212             case "command":
213                 let (command = event.command) {
214                     window.input.help_displayed = false;
215                     input_help_timer_clear(window);
216                     window.minibuffer.clear();
217                     yield call_interactively(I, command);
218                     if (! interactive_commands.get(command).prefix)
219                         break sequence;
220                 }
221                 break;
222             }
223             // should we expect more events?
224             event = null;
225             event = yield SUSPEND;
226         }
227     } catch (e) {
228         dump_error(e);
229     } finally {
230         // sequence is done
231         delete state.continuation;
232     }
236 function input_handle_keydown (event) {
237     if (event.keyCode == 0 ||
238         event.keyCode == vk_name_to_keycode.shift ||
239         event.keyCode == vk_name_to_keycode.control ||
240         event.keyCode == vk_name_to_keycode.alt ||
241         event.keyCode == vk_name_to_keycode.caps_lock)
242         return event_kill(event);
243     var window = this;
244     var state = window.input.current;
245     if (state.continuation)
246         state.continuation(event);
247     else
248         co_call(input_handle_sequence.call(window, event));
252 function input_handle_keypress (event) {
253     if (event.keyCode == 0 && event.charCode == 0 ||
254         event.keyCode == vk_name_to_keycode.caps_lock)
255         return event_kill(event);
256     var window = this;
257     var state = window.input.current;
258     if (state.continuation)
259         state.continuation(event);
260     else
261         co_call(input_handle_sequence.call(window, event));
265 function input_handle_keyup (event) {
266     if (event.keyCode == 0 ||
267         event.keyCode == vk_name_to_keycode.shift ||
268         event.keyCode == vk_name_to_keycode.control ||
269         event.keyCode == vk_name_to_keycode.alt ||
270         event.keyCode == vk_name_to_keycode.caps_lock)
271         return event_kill(event);
272     var window = this;
273     var state = window.input.current;
274     if (state.fallthrough[event.keyCode])
275         delete state.fallthrough[event.keyCode];
276     else
277         event_kill(event);
281 // handler for command_event special events
282 function input_handle_command (event) {
283     var window = this;
284     var state = window.input.current;
285     if (state.continuation)
286         state.continuation(event);
287     else
288         co_call(input_handle_sequence.call(window, event));
292 // handler for special abort event
293 function input_sequence_abort (message) {
294     var window = this;
295     window.input.help_displayed = false;
296     input_help_timer_clear(window);
297     window.minibuffer.clear();
298     if (message)
299         window.minibuffer.show(message);
300     delete window.input.current.continuation;
304 function input_initialize_window (window) {
305     window.input = [new input_state(window)]; // a stack of states
306     window.input.__defineGetter__("current", function () {
307         return this[this.length - 1];
308     });
309     window.input.begin_recursion = function () {
310         this.push(new input_state(window));
311     };
312     window.input.end_recursion = function () {
313         this.pop();
314     };
315     window.input.help_timer = null;
316     window.input.help_displayed = false;
317     //window.addEventListener("keydown", input_handle_keydown, true);
318     window.addEventListener("keypress", input_handle_keypress, true);
319     //window.addEventListener("keyup", input_handle_keyup, true);
320     //TO-DO: mousedown, mouseup, click, dblclick
323 add_hook("window_initialize_hook", input_initialize_window);