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