google-search-results.js: also add binding for return
[conkeror.git] / modules / minibuffer-read-option.js
blobcccac21235b9dd59324e49c261c28103c1da7743
1 require("minibuffer-read.js");
3 function or_string(options) {
4     return options.slice(0,options.length-1).join(", ") + " or " + options[options.length - 1];
7 define_keywords("$options");
8 minibuffer.prototype.read_explicit_option = function () {
9     keywords(arguments);
10     var options = arguments.$options;
11     var options_string = or_string(options);
12     var result = yield this.read(forward_keywords(arguments),
13         $prompt = arguments.$prompt + " (" + options_string + ")",
14         $validator = function (x, m) {
15             if (options.indexOf(x) == -1) {
16                 m.message("Please answer " + options_string + ".");
17                 m._input_text = "";
18                 m._set_selection();
19                 return false;
20             }
21             return true;
22         });
23     yield co_return(result);
26 minibuffer.prototype.read_yes_or_no = function () {
27     keywords(arguments);
28     var result = yield this.read_explicit_option(forward_keywords(arguments), $options = ["yes", "no"]);
29     yield co_return(result == "yes");
32 function single_character_options_minibuffer_state(continuation) {
33     keywords(arguments);
34     this.continuation = continuation;
35     this.options = arguments.$options;
36     minibuffer_input_state.call(this, single_character_options_minibuffer_keymap, arguments.$prompt);
38 single_character_options_minibuffer_state.prototype = {
39     __proto__: minibuffer_input_state.prototype,
40     destroy: function () {
41         if (this.continuation)
42             this.continuation.throw(abort());
43     }
45 function single_character_options_enter_character(window, s, event) {
46     var ch = String.fromCharCode(event.charCode);
47     if (s.options.indexOf(ch) != -1) {
48         var c = s.continuation;
49         delete s.continuation;
50         window.minibuffer.pop_state();
51         if (c)
52             c(ch);
53         return;
54     }
55     var str = "Please answer " + or_string(s.options) + ".";
56     window.minibuffer.message(str);
59 interactive("single-character-options-enter-character",
60             function (I) {
61                 single_character_options_enter_character(
62                     I.window,
63                     I.minibuffer.check_state(single_character_options_minibuffer_state),
64                     I.event);
65             });
67 minibuffer.prototype.read_single_character_option = function () {
68     keywords(arguments);
69     var s = new single_character_options_minibuffer_state((yield CONTINUATION), forward_keywords(arguments));
70     this.push_state(s);
71     var result = yield SUSPEND;
72     yield co_return(result);