new-tabs: fix bug in tab_bar_kill_buffer
[conkeror.git] / modules / minibuffer.js
blob883efa4f5259d6ad49eb2903f25ebb3f75861ffc
1 /**
2  * (C) Copyright 2007-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 /**
12  * minibuffer_state: abstact base class for minibuffer states.
13  */
14 function minibuffer_state (minibuffer, keymap) {
15     this.minibuffer = minibuffer;
16     this.keymaps = [default_base_keymap, keymap];
18 minibuffer_state.prototype = {
19     constructor: minibuffer_state,
20     load: function () {},
21     unload: function () {},
22     destroy: function () {}
26 /**
27  * minibuffer_message_state: base class for minibuffer states which do not
28  * use the input element, but still may use a keymap.
29  */
30 function minibuffer_message_state (minibuffer, keymap, message, cleanup_function) {
31     minibuffer_state.call(this, minibuffer, keymap);
32     this._message = message;
33     this.cleanup_function = cleanup_function;
35 minibuffer_message_state.prototype = {
36     constructor: minibuffer_message_state,
37     __proto__: minibuffer_state.prototype,
38     _message: null,
39     get message () { return this._message; },
40     set message (x) {
41         this.minibuffer._restore_normal_state();
42         this.minibuffer._show(this._message);
43     },
44     load: function () {
45         minibuffer_state.prototype.load.call(this);
46         this.minibuffer._show(this.message);
47     },
48     cleanup_function: null,
49     destroy: function () {
50         if (this.cleanup_function)
51             this.cleanup_function();
52         minibuffer_state.prototype.destroy.call(this);
53     }
57 /**
58  * minibuffer_input_state: base class for minibuffer states which use the
59  * input element.
60  */
61 function minibuffer_input_state (minibuffer, keymap, prompt, input, selection_start, selection_end) {
62     minibuffer_state.call(this, minibuffer, keymap);
63     this.prompt = prompt;
64     if (input)
65         this.input = input;
66     else
67         this.input = "";
68     if (selection_start)
69         this.selection_start = selection_start;
70     else
71         this.selection_start = 0;
72     if (selection_end)
73         this.selection_end = selection_end;
74     else
75         this.selection_end = this.selection_start;
76     this.minibuffer.window.input.begin_recursion();
78 minibuffer_input_state.prototype = {
79     constructor: minibuffer_input_state,
80     __proto__: minibuffer_state.prototype,
81     mark_active: false,
82     load: function () {
83         minibuffer_state.prototype.load.call(this);
84         this.minibuffer.ignore_input_events = true;
85         this.minibuffer._input_text = this.input;
86         this.minibuffer.ignore_input_events = false;
87         this.minibuffer.prompt = this.prompt;
88         this.minibuffer._set_selection(this.selection_start,
89                                        this.selection_end);
90     },
91     unload: function () {
92         this.input = this.minibuffer._input_text;
93         this.prompt = this.minibuffer.prompt;
94         this.selection_start = this.minibuffer._selection_start;
95         this.selection_end = this.minibuffer._selection_end;
96         minibuffer_state.prototype.unload.call(this);
97     },
98     destroy: function () {
99         this.minibuffer.window.input.end_recursion();
100         minibuffer_state.prototype.destroy.call(this);
101     }
106  * The parameter `args' is an object specifying the arguments for
107  * basic_minibuffer_state.  The following properties of args must/may
108  * be set:
110  * prompt:            [required]
112  * initial_value:     [optional] specifies the initial text
114  * select:            [optional] specifies to select the initial text if set to non-null
115  */
116 define_keywords("$keymap", "$prompt", "$initial_value", "$select");
117 function basic_minibuffer_state (minibuffer) {
118     keywords(arguments, $keymap = minibuffer_base_keymap);
119     var initial_value = arguments.$initial_value || "";
120     var sel_start, sel_end;
121     if (arguments.$select) {
122         sel_start = 0;
123         sel_end = initial_value.length;
124     } else {
125         sel_start = sel_end = initial_value.length;
126     }
127     minibuffer_input_state.call(this, minibuffer, arguments.$keymap,
128                                 arguments.$prompt, initial_value,
129                                 sel_start, sel_end);
131 basic_minibuffer_state.prototype = {
132     constructor: basic_minibuffer_state,
133     __proto__: minibuffer_input_state.prototype
137 define_variable("minibuffer_input_mode_show_message_timeout", 1000,
138     "Time duration (in milliseconds) to flash minibuffer messages while in "+
139     "minibuffer input mode.");
142 function minibuffer (window) {
143     this.element = window.document.getElementById("minibuffer");
144     this.output_element = window.document.getElementById("minibuffer-message");
145     this.input_prompt_element = window.document.getElementById("minibuffer-prompt");
146     this.input_element = window.document.getElementById("minibuffer-input");
147     var m = this;
148     this.input_element.inputField.addEventListener("blur",
149         function () {
150             if (m.active && m._input_mode_enabled && !m._showing_message) {
151                 window.setTimeout(function () {
152                         m.input_element.inputField.focus();
153                     }, 0);
154             }
155         }, false);
156     function dispatch_handle_input () {
157         if (m.ignore_input_events || !m._input_mode_enabled)
158             return;
159         var s = m.current_state;
160         if (s && s.handle_input)
161             s.handle_input(m);
162     }
163     this.input_element.addEventListener("input", dispatch_handle_input, true);
164     this.input_element.watch("value",
165         function (prop, oldval, newval) {
166             if (newval != oldval &&
167                 !m.ignore_input_events)
168             {
169                 call_after_timeout(dispatch_handle_input, 0);
170             }
171             return newval;
172         });
173     // Ensure that the input area will have focus if a message is
174     // currently being flashed so that the default handler for key
175     // events will properly add text to the input area.
176     window.addEventListener("keydown",
177         function (e) {
178             if (m._input_mode_enabled && m._showing_message)
179                 m._restore_normal_state();
180         }, true);
181     this.window = window;
182     this.last_message = "";
183     this.states = [];
185 minibuffer.prototype = {
186     constructor: minibuffer,
187     get _selection_start () { return this.input_element.selectionStart; },
188     get _selection_end () { return this.input_element.selectionEnd; },
189     get _input_text () { return this.input_element.value; },
190     set _input_text (text) { this.input_element.value = text; },
191     get prompt () { return this.input_prompt_element.value; },
192     set prompt (s) { this.input_prompt_element.value = s; },
194     set_input_state: function (x) {
195         this._input_text = x[0];
196         this._set_selection(x[1], x[2]);
197     },
199     _set_selection: function (start, end) {
200         if (start == null)
201             start = this._input_text.length;
202         if (end == null)
203             end = this._input_text.length;
204         this.input_element.setSelectionRange(start,end);
205     },
207     /* Saved focus state */
208     saved_focused_frame: null,
209     saved_focused_element: null,
211     default_message: "",
213     current_message: null,
215     /* This method will display the specified string in the
216      * minibuffer, without recording it in any log/Messages buffer. */
217     show: function (str, force) {
218         if (!this.active || force) {
219             this.current_message = str;
220             this._show(str);
221         }
222     },
224     _show: function (str) {
225         if (this.last_message != str) {
226             this.output_element.value = str;
227             this.last_message = str;
228         }
229     },
231     message: function (str) {
232         /* TODO: add the message to a *Messages* buffer, and/or
233          * possibly dump them to the console. */
234         if (str == "")
235             this.clear();
236         else {
237             this.show(str, true /* force */);
238             if (this.active)
239                 this._flash_temporary_message();
240         }
241     },
243     clear: function () {
244         this.current_message = null;
245         if (!this.active)
246             this._show(this.default_message);
247     },
249     set_default_message: function (str) {
250         this.default_message = str;
251         if (this.current_message == null)
252             this._show(str);
253     },
255     get current_state () {
256         if (! this.states[0])
257             return null;
258         return this.states[this.states.length - 1];
259     },
261     push_state: function (state) {
262         this._save_state();
263         this.states.push(state);
264         this._restore_state();
265     },
267     pop_state: function () {
268         this.current_state.destroy();
269         this.states.pop();
270         this._restore_state();
271     },
273     pop_all: function () {
274         var state;
275         while ((state = this.current_state)) {
276             state.destroy();
277             this.states.pop();
278         }
279     },
281     //XXX: breaking stack discipline can cause incorrect
282     //     input recursion termination
283     remove_state: function (state) {
284         var i = this.states.indexOf(state);
285         if (i == -1)
286             return;
287         var was_current = (i == (this.states.length - 1));
288         state.destroy();
289         this.states.splice(i, 1);
290         if (was_current)
291             this._restore_state();
292     },
294     _input_mode_enabled: false,
296     active: false,
298     /* If _input_mode_enabled is true, this is set to indicate that
299      * the message area is being temporarily shown instead of the
300      * input box. */
301     _showing_message: false,
303     _message_timer_ID: null,
305     /* This must only be called if _input_mode_enabled is true */
306     //XXX: if it must only be called if _input_mode_enabled is true, then
307     //     why does it have an else condition for handling
308     //     minibuffer_message_state states?
309     _restore_normal_state: function () {
310         if (this._showing_message) {
311             this.window.clearTimeout(this._message_timer_ID);
312             this._message_timer_ID = null;
313             this._showing_message = false;
315             if (this._input_mode_enabled)
316                 this._switch_to_input_mode();
317             else
318                 // assumes that anything other than an input state is a
319                 // minibuffer_message_state.
320                 this._show(this.current_state._message);
321         }
322     },
324     /* This must only be called if _input_mode_enabled is true */
325     _flash_temporary_message: function () {
326         if (this._showing_message)
327             this.window.clearTimeout(this._message_timer_ID);
328         else {
329             this._showing_message = true;
330             if (this._input_mode_enabled)
331                 this._switch_to_message_mode();
332         }
333         var obj = this;
334         this._message_timer_ID = this.window.setTimeout(function () {
335             obj._restore_normal_state();
336         }, minibuffer_input_mode_show_message_timeout);
337     },
339     _switch_to_input_mode: function () {
340         this.element.setAttribute("minibuffermode", "input");
341         this.input_element.inputField.focus();
342     },
344     _switch_to_message_mode: function () {
345         this.element.setAttribute("minibuffermode", "message");
346     },
348     _restore_state: function () {
349         var s = this.current_state;
350         if (s) {
351             if (!this.active) {
352                 this.saved_focused_frame = this.window.document.commandDispatcher.focusedWindow;
353                 this.saved_focused_element = this.window.document.commandDispatcher.focusedElement;
354             }
355             s.load();
356             this.active = true;
357         } else {
358             if (this.active) {
359                 this.active = false;
360                 this.window.buffers.current.browser.focus();
361                 if (this.saved_focused_element && this.saved_focused_element.focus)
362                     set_focus_no_scroll(this.window, this.saved_focused_element);
363                 else if (this.saved_focused_frame)
364                     set_focus_no_scroll(this.window, this.saved_focused_frame);
365                 this.saved_focused_element = null;
366                 this.saved_focused_frame = null;
367                 this._show(this.current_message || this.default_message);
368             }
369         }
370         if (this._showing_message) {
371             this.window.clearTimeout(this._message_timer_ID);
372             this._message_timer_ID = null;
373             this._showing_message = false;
374         }
375         var want_input_mode = s instanceof minibuffer_input_state;
376         var in_input_mode = this._input_mode_enabled && !this._showing_message;
377         if (want_input_mode && !in_input_mode)
378             this._switch_to_input_mode();
379         else if (!want_input_mode && in_input_mode)
380             this._switch_to_message_mode();
381         this._input_mode_enabled = want_input_mode;
382     },
384     _save_state: function () {
385         var s = this.current_state;
386         if (s)
387             s.unload();
388     },
390     insert_before: function (element) {
391         this.element.parentNode.insertBefore(element, this.element);
392     }
396 function minibuffer_initialize_window (window) {
397     window.minibuffer = new minibuffer(window);
399 add_hook("window_initialize_early_hook", minibuffer_initialize_window);
402 function minibuffer_window_close_handler (window) {
403     window.minibuffer.pop_all();
405 add_hook("window_close_hook", minibuffer_window_close_handler);
408 /* Note: This is concise, but doesn't seem to be useful in practice,
409  * because nothing can be done with the state alone. */
410 minibuffer.prototype.check_state = function (type) {
411     var s = this.current_state;
412     if (!(s instanceof type))
413         throw new Error("Invalid minibuffer state.");
414     return s;
417 minibuffer.prototype.show_wait_message = function (initial_message, cleanup_function) {
418     var s = new minibuffer_message_state(this, minibuffer_message_keymap, initial_message, cleanup_function);
419     this.push_state(s);
420     return s;
423 minibuffer.prototype.wait_for = function (message, coroutine) {
424     var cc = yield CONTINUATION;
425     var done = false;
426     var s = this.show_wait_message(message, function () { if (!done) cc.throw(abort()); });
427     var result;
428     try {
429         result = yield coroutine;
430     } finally {
431         done = true;
432         this.remove_state(s);
433     }
434     yield co_return(result);
438 // This should only be used for minibuffer states where it makes
439 // sense.  In particular, it should not be used if additional cleanup
440 // must be done.
441 function minibuffer_abort (window) {
442     var m = window.minibuffer;
443     var s = m.current_state;
444     if (s == null)
445         throw "Invalid minibuffer state";
446     m.pop_state();
447     input_sequence_abort.call(window);
449 interactive("minibuffer-abort", null, function (I) { minibuffer_abort(I.window); });
452 provide("minibuffer");