input_handle_command: allow event to be a string
[conkeror/arlinius.git] / modules / minibuffer-read-option.js
blob9fdc2f39a75bc376ef175b6ffb9ef99096df0bb9
1 /**
2  * (C) Copyright 2008 Jeremy Maitin-Shepard
3  * (C) Copyright 2009 John J. Foerch
4  *
5  * Use, modification, and distribution are subject to the terms specified in the
6  * COPYING file.
7 **/
9 require("minibuffer-read.js");
11 define_keywords("$options");
12 minibuffer.prototype.read_explicit_option = function () {
13     keywords(arguments);
14     var options = arguments.$options;
15     var options_string = or_string(options);
16     var result = yield this.read(forward_keywords(arguments),
17         $prompt = arguments.$prompt + " (" + options_string + ")",
18         $validator = function (x, m) {
19             if (options.indexOf(x) == -1) {
20                 m.message("Please answer " + options_string + ".");
21                 m._input_text = "";
22                 m._set_selection();
23                 return false;
24             }
25             return true;
26         });
27     yield co_return(result);
30 minibuffer.prototype.read_yes_or_no = function () {
31     keywords(arguments);
32     var result = yield this.read_explicit_option(forward_keywords(arguments), $options = ["yes", "no"]);
33     yield co_return(result == "yes");
36 function single_character_options_minibuffer_state (window, continuation) {
37     keywords(arguments);
38     this.continuation = continuation;
39     this.options = arguments.$options;
40     minibuffer_input_state.call(this, window, single_character_options_minibuffer_keymap, arguments.$prompt);
42 single_character_options_minibuffer_state.prototype = {
43     __proto__: minibuffer_input_state.prototype,
44     destroy: function (window) {
45         if (this.continuation)
46             this.continuation.throw(abort());
47         minibuffer_input_state.prototype.destroy.call(this, window);
48     }
50 function single_character_options_enter_character(window, s, event) {
51     var ch = String.fromCharCode(event.charCode);
52     if (s.options.indexOf(ch) != -1) {
53         var c = s.continuation;
54         delete s.continuation;
55         window.minibuffer.pop_state();
56         if (c)
57             c(ch);
58         return;
59     }
60     var str = "Please answer " + or_string(s.options) + ".";
61     window.minibuffer.message(str);
64 interactive("single-character-options-enter-character", null,
65             function (I) {
66                 single_character_options_enter_character(
67                     I.window,
68                     I.minibuffer.check_state(single_character_options_minibuffer_state),
69                     I.event);
70             });
72 minibuffer.prototype.read_single_character_option = function () {
73     keywords(arguments);
74     var s = new single_character_options_minibuffer_state(this.window, (yield CONTINUATION), forward_keywords(arguments));
75     this.push_state(s);
76     var result = yield SUSPEND;
77     yield co_return(result);