new module to enable editing and deleting of bookmarks
[conkeror/arlinius.git] / modules / minibuffer-read-option.js
blobdcdf71bdbfb7e251323ab29e12bf1f831c548be0
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 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 (minibuffer, continuation) {
37     keywords(arguments);
38     minibuffer_input_state.call(this, minibuffer, single_character_options_minibuffer_keymap, arguments.$prompt);
39     this.continuation = continuation;
40     this.options = arguments.$options;
42 single_character_options_minibuffer_state.prototype = {
43     constructor: single_character_options_minibuffer_state,
44     __proto__: minibuffer_input_state.prototype,
45     destroy: function () {
46         if (this.continuation)
47             this.continuation.throw(abort());
48         minibuffer_input_state.prototype.destroy.call(this);
49     }
51 function single_character_options_enter_character (window, s, event) {
52     var ch = String.fromCharCode(event.charCode);
53     if (s.options.indexOf(ch) != -1) {
54         var c = s.continuation;
55         delete s.continuation;
56         window.minibuffer.pop_state();
57         if (c)
58             c(ch);
59         return;
60     }
61     var str = "Please answer " + or_string(s.options) + ".";
62     window.minibuffer.message(str);
65 interactive("single-character-options-enter-character", null,
66             function (I) {
67                 single_character_options_enter_character(
68                     I.window,
69                     I.minibuffer.check_state(single_character_options_minibuffer_state),
70                     I.event);
71             });
73 minibuffer.prototype.read_single_character_option = function () {
74     keywords(arguments);
75     var s = new single_character_options_minibuffer_state(this, (yield CONTINUATION), forward_keywords(arguments));
76     this.push_state(s);
77     var result = yield SUSPEND;
78     yield co_return(result);
81 provide("minibuffer-read-options");