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
12 /* Generate vk name table */
13 var keycode_to_vk_name = [];
14 var vk_name_to_keycode = {};
15 let (KeyEvent = Ci.nsIDOMKeyEvent,
17 for (var i in KeyEvent) {
18 /* Check if this is a key binding */
19 if (i.substr(0, prefix.length) == prefix) {
20 let name = i.substr(prefix.length).toLowerCase();
21 let code = KeyEvent[i];
22 keycode_to_vk_name[code] = name;
23 vk_name_to_keycode[name] = code;
33 function modifier (in_event_p, set_in_event) {
34 this.in_event_p = in_event_p;
35 this.set_in_event = set_in_event;
39 A: new modifier(function (event) { return event.altKey; },
40 function (event) { event.altKey = true; }),
41 C: new modifier(function (event) { return event.ctrlKey; },
42 function (event) { event.ctrlKey = true; }),
43 M: new modifier(function (event) { return event.metaKey; },
44 function (event) { event.metaKey = true; }),
45 S: new modifier(function (event) {
49 name = keycode_to_vk_name[event.keyCode];
50 return ((name && name.length > 1) ||
51 (event.charCode == 32) ||
52 (event.button != null));
56 function (event) { event.shiftKey = true; })
58 var modifier_order = ['C', 'M', 'S'];
60 // check the platform and guess whether we should treat Alt as Meta
61 if (get_os() == 'Darwin') {
62 // In OS X, alt is a shift-like modifier, in that we
63 // only care about it for non-character events.
64 modifiers.A = new modifier(
69 name = keycode_to_vk_name[event.keyCode];
70 return ((name && name.length > 1) ||
71 (event.charCode == 32) ||
72 (event.button != null));
76 function (event) { event.altKey = true; });
77 modifier_order = ['C', 'M', 'A', 'S'];
79 modifiers.M = modifiers.A;
88 function format_key_combo (event) {
90 for each (var M in modifier_order) {
91 if (modifiers[M].in_event_p(event) ||
92 (event.sticky_modifiers &&
93 event.sticky_modifiers.indexOf(M) != -1))
99 if (event.charCode == 32)
102 combo += String.fromCharCode(event.charCode);
103 } else if (event.keyCode) {
104 combo += keycode_to_vk_name[event.keyCode];
105 } else if (event.button != null) {
106 combo += "mouse" + (event.button + 1);
111 function unformat_key_combo (combo) {
122 var len = combo.length - 2;
124 while (i < len && combo[i+1] == '-') {
126 modifiers[M].set_in_event(event);
129 var key = combo.substring(i);
131 event.charCode = key.charCodeAt(0);
132 else if (key == 'space')
134 else if (vk_name_to_keycode[key])
135 event.keyCode = vk_name_to_keycode[key];
136 else if (key.substring(0, 5) == 'mouse')
137 event.button = parseInt(key.substring(5));
147 define_keywords("$parent", "$help", "$name", "$anonymous");
150 this.parent = arguments.$parent;
152 this.predicate_bindings = [];
153 this.fallthrough = [];
154 this.help = arguments.$help;
155 this.name = arguments.$name;
156 this.anonymous = arguments.$anonymous;
159 function define_keymap (name) {
161 this[name] = new keymap($name = name, forward_keywords(arguments));
167 * Key Match Predicates.
170 function define_key_match_predicate (name, description, predicate) {
171 conkeror[name] = predicate;
172 conkeror[name].name = name;
173 conkeror[name].description = description;
174 return conkeror[name];
177 define_key_match_predicate('match_any_key', 'any key',
178 function (event) true);
180 // should be renamed to match_any_unmodified_character
181 define_key_match_predicate('match_any_unmodified_character', 'any unmodified character',
183 // this predicate can be used for both keypress and keydown
186 return ((event.type == 'keypress' && event.charCode)
187 || event.keyCode > 31)
188 && !modifiers.A.in_event_p(event)
191 && !event.sticky_modifiers;
192 } catch (e) { return false; }
195 define_key_match_predicate('match_checkbox_keys', 'checkbox keys',
197 return event.keyCode == 32
202 //XXX: keycode fallthroughs don't support sticky modifiers
205 define_key_match_predicate('match_text_keys', 'text editing keys',
207 return ((event.type == 'keypress' && event.charCode)
208 || event.keyCode == 13 || event.keyCode > 31)
211 && !modifiers.A.in_event_p(event);
212 //XXX: keycode fallthroughs don't support sticky modifiers
219 function format_key_spec (key) {
220 if (key instanceof Function) {
222 return "<"+key.description+">";
224 return "<"+key.name+">";
225 return "<anonymous match function>";
230 function format_binding_sequence (seq) {
231 return seq.map(function (x) {
232 return format_key_spec(x.key);
237 function keymap_lookup (keymaps, combo, event) {
238 var i = keymaps.length - 1;
239 var kmap = keymaps[i];
242 var bindings = kmap.bindings;
243 var bind = bindings[combo];
247 new_kmaps.unshift(bind.keymap);
253 new_kmaps = [bind.keymap];
257 var pred_binds = kmap.predicate_bindings;
258 for (var j = 0, pblen = pred_binds.length; j < pblen; ++j) {
259 bind = pred_binds[j];
276 function keymap_lookup_fallthrough (keymap, event) {
277 var predicates = keymap.fallthrough;
278 for (var i = 0, plen = predicates.length; i < plen; ++i) {
279 if (predicates[i](event))
286 function for_each_key_binding (keymaps, callback) {
287 var binding_sequence = [];
288 function helper (keymap) {
290 for each (binding in keymap.bindings) {
291 binding_sequence.push(binding);
292 callback(binding_sequence);
294 helper(binding.keymap);
295 binding_sequence.pop();
297 for each (binding in keymap.predicate_bindings) {
298 binding_sequence.push(binding);
299 callback(binding_sequence);
301 helper(binding.keymap);
302 binding_sequence.pop();
305 //outer loop is to go down the parent-chain of keymaps
306 var i = keymaps.length - 1;
307 var keymap = keymaps[i];
311 keymap = keymap.parent;
313 keymap = keymaps[--i];
320 function keymap_lookup_command (keymaps, command) {
322 for_each_key_binding(keymaps, function (bind_seq) {
323 var bind = bind_seq[bind_seq.length - 1];
324 if (bind.command == command)
325 list.push(format_binding_sequence(bind_seq));
336 * $fallthrough, $repeat and $browser_object are as for define_key.
338 * ref is the source code reference of the call to define_key.
340 * kmap is the keymap in which the binding is to be defined.
342 * seq is the key sequence being bound. it may be necessary
343 * to auto-generate new keymaps to accomodate the key sequence.
345 * only one of new_command and new_keymap will be given.
346 * the one that is given is the thing being bound to.
348 define_keywords("$fallthrough", "$repeat", "$browser_object");
349 function define_key_internal (ref, kmap, seq, new_command, new_keymap) {
351 var args = arguments;
352 var last_in_sequence; // flag to indicate the final key combo in the sequence.
353 var key; // current key combo as we iterate through the sequence.
354 var undefine_key = (new_command == null) &&
355 (new_keymap == null) &&
356 (! args.$fallthrough);
358 /* Replace `bind' with the binding specified by (cmd, fallthrough) */
359 function replace_binding (bind) {
360 if (last_in_sequence) {
361 bind.command = new_command;
362 bind.keymap = new_keymap;
363 bind.fallthrough = args.$fallthrough;
364 bind.source_code_reference = ref;
365 bind.repeat = args.$repeat;
366 bind.browser_object = args.$browser_object;
369 throw new Error("Key sequence has a non-keymap in prefix");
374 function make_binding () {
375 if (last_in_sequence) {
377 fallthrough: args.$fallthrough,
378 command: new_command,
380 source_code_reference: ref,
381 repeat: args.$repeat,
382 browser_object: args.$browser_object,
386 kmap = new keymap($anonymous,
387 $name = old_kmap.name + " " + format_key_spec(key));
388 kmap.bound_in = old_kmap;
391 source_code_reference: ref,
392 bound_in: old_kmap };
397 for (var i = 0, slen = seq.length; i < slen; ++i) {
399 last_in_sequence = (i == slen - 1);
401 if (typeof key == "function") { // it's a match predicate
402 // Check if the binding is already present in the keymap
403 var pred_binds = kmap.predicate_bindings;
404 for (var j = 0, pblen = pred_binds.length; j < pblen; j++) {
405 if (pred_binds[j].key == key) {
406 if (last_in_sequence && undefine_key)
407 delete pred_binds[j];
409 replace_binding(pred_binds[j]);
413 pred_binds.push(make_binding());
415 // Check if the binding is already present in the keymap
416 var bindings = kmap.bindings;
417 var binding = bindings[key];
419 if (last_in_sequence && undefine_key)
420 delete bindings[key];
422 replace_binding(binding);
426 bindings[key] = make_binding();
432 * bind SEQ to a keymap or command CMD in keymap KMAP.
434 * If CMD is the special value `fallthrough', it will be bound as a
439 * $fallthrough: specifies that the keypress event will fall through
440 * to gecko. Note, by this method, only keypress will fall through.
441 * Keyup and keydown will still be blocked.
443 * $repeat: (command name) shortcut command to call if a prefix
444 * command key is pressed twice in a row.
446 * $browser_object: (browser object) Override the default
447 * browser-object for the command.
449 define_keywords("$fallthrough", "$repeat", "$browser_object");
450 function define_key (kmap, seq, cmd) {
454 var ref = get_caller_source_code_reference();
456 if (typeof seq == "string" && seq.length > 1)
457 seq = seq.split(" ");
459 if (!(typeof seq == "object") || !(seq instanceof Array))
462 // normalize the order of modifiers in key combos
465 if (typeof(k) == "string")
466 return format_key_combo(unformat_key_combo(k));
471 var new_command = null;
472 var new_keymap = null;
473 if (typeof cmd == "string" || typeof cmd == "function")
475 else if (cmd instanceof keymap)
477 else if (cmd != null)
478 throw new Error("Invalid `cmd' argument: " + cmd);
480 define_key_internal(ref, kmap, seq, new_command, new_keymap,
481 forward_keywords(arguments));
483 } catch (e if (typeof e == "string")) {
484 dumpln("Warning: Error occurred while binding sequence: " + orig_seq);
490 function undefine_key (kmap, seq) {
491 define_key(kmap, seq);
498 * Takes a keymap and a predicate on an event. Fallthroughs defined by
499 * these means will cause all three of keydown, keypress, and keyup to
500 * fall through to gecko, whereas those defined by the $fallthrough
501 * keyword to define_key only affect keypress events.
503 * The limitations of this method are that only the keyCode is available
504 * to the predicate, not the charCode, and keymap inheritance is not
505 * available for these "bindings".
507 function define_fallthrough (keymap, predicate) {
508 keymap.fallthrough.push(predicate);
513 * Minibuffer Read Key Binding
516 define_keymap("key_binding_reader_keymap");
517 define_key(key_binding_reader_keymap, match_any_key, "read-key-binding-key");
519 define_keywords("$keymap");
520 function key_binding_reader (minibuffer, continuation) {
521 keywords(arguments, $prompt = "Describe key:");
522 minibuffer_input_state.call(this, minibuffer, key_binding_reader_keymap, arguments.$prompt);
523 this.continuation = continuation;
524 if (arguments.$keymap)
525 this.target_keymap = arguments.$keymap;
527 this.target_keymap = get_current_keymaps(this.minibuffer.window).slice();
528 this.key_sequence = [];
530 key_binding_reader.prototype = {
531 constructor: key_binding_reader,
532 __proto__: minibuffer_input_state.prototype,
533 destroy: function () {
534 if (this.continuation)
535 this.continuation.throw(abort());
536 minibuffer_input_state.prototype.destroy.call(this);
540 function invalid_key_binding (seq) {
541 var e = new Error(seq.join(" ") + " is undefined");
542 e.key_sequence = seq;
543 e.__proto__ = invalid_key_binding.prototype;
546 invalid_key_binding.prototype = {
547 constructor: invalid_key_binding,
548 __proto__: interactive_error.prototype
551 function read_key_binding_key (window, state, event) {
552 var combo = format_key_combo(event);
553 var binding = keymap_lookup(state.target_keymap, combo, event);
555 state.key_sequence.push(combo);
557 if (binding == null) {
558 var c = state.continuation;
559 delete state.continuation;
560 window.minibuffer.pop_state();
561 c.throw(invalid_key_binding(state.key_sequence));
565 if (binding.constructor == Array) { //keymaps stack
566 window.minibuffer._restore_normal_state();
567 window.minibuffer._input_text = state.key_sequence.join(" ") + " ";
568 state.target_keymap = binding;
572 var c = state.continuation;
573 delete state.continuation;
575 window.minibuffer.pop_state();
578 c([state.key_sequence, binding]);
580 interactive("read-key-binding-key",
581 "Handle a keystroke in the key binding reader minibuffer state.",
583 read_key_binding_key(I.window,
584 I.minibuffer.check_state(key_binding_reader),
588 minibuffer.prototype.read_key_binding = function () {
590 var s = new key_binding_reader(this, (yield CONTINUATION), forward_keywords(arguments));
592 var result = yield SUSPEND;
593 yield co_return(result);