spawn-process: Better error information in case of timeout
[conkeror.git] / modules / minibuffer-read-option.js
blob0bdf363798feee07e79a0820fb5ea0a32f5bff5c
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) {
37     keywords(arguments);
38     minibuffer_input_state.call(this, minibuffer, single_character_options_minibuffer_keymap, arguments.$prompt);
39     this.deferred = Promise.defer();
40     this.promise = make_simple_cancelable(this.deferred);
41     this.options = arguments.$options;
43 single_character_options_minibuffer_state.prototype = {
44     constructor: single_character_options_minibuffer_state,
45     __proto__: minibuffer_input_state.prototype,
46     destroy: function () {
47         this.promise.cancel();
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         s.deferred.resolve(ch);
55         window.minibuffer.pop_state();
56         return;
57     }
58     var str = "Please answer " + or_string(s.options) + ".";
59     window.minibuffer.message(str);
62 interactive("single-character-options-enter-character", null,
63             function (I) {
64                 single_character_options_enter_character(
65                     I.window,
66                     I.minibuffer.check_state(single_character_options_minibuffer_state),
67                     I.event);
68             });
70 minibuffer.prototype.read_single_character_option = function () {
71     keywords(arguments);
72     var s = new single_character_options_minibuffer_state(this, forward_keywords(arguments));
73     this.push_state(s);
74     yield co_return(yield s.promise);
77 provide("minibuffer-read-options");