Further adapt debian webjumps as suggested by John J. Foerch
[conkeror.git] / modules / minibuffer.js
blob6564be35daa6455c49a95f027966236c20d6d9eb
1 /**
2  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
3  * (C) Copyright 2009 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 // This should only be used for minibuffer states where it makes
12 // sense.  In particular, it should not be used if additional cleanup
13 // must be done.
14 function minibuffer_abort (window) {
15     var m = window.minibuffer;
16     var s = m.current_state;
17     if (s == null)
18         throw "Invalid minibuffer state";
19     m.pop_state();
20     input_sequence_abort.call(window);
22 interactive("minibuffer-abort", null, function (I) { minibuffer_abort(I.window); });
24 define_builtin_commands("minibuffer-",
25     function (I, command) {
26         try {
27             var m = I.minibuffer;
28             if (m._input_mode_enabled) {
29                 m._restore_normal_state();
30                 var e = m.input_element;
31                 var c = e.controllers.getControllerForCommand(command);
32                 try {
33                     m.ignore_input_events = true;
34                     if (c && c.isCommandEnabled(command))
35                         c.doCommand(command);
36                 } finally {
37                     m.ignore_input_events = false;
38                 }
39                 var s = m.current_state;
40                 if (s.ran_minibuffer_command)
41                     s.ran_minibuffer_command(m, command);
42             }
43         } catch (e) {
44             /* Ignore exceptions. */
45         }
46     },
47     function (I) { //XXX: need return??
48         I.minibuffer.current_state.mark_active = !I.minibuffer.current_state.mark_active;
49     },
50     function (I) I.minibuffer.current_state.mark_active,
51     false);
54 /**
55  * minibuffer_state: abstact base class for minibuffer states.
56  */
57 function minibuffer_state (keymap, use_input_mode) {
58     this.keymap = keymap;
59     this.use_input_mode = use_input_mode;
61 minibuffer_state.prototype = {
62     load: function () {},
63     unload: function () {},
64     destroy: function () {}
68 function minibuffer_message_state (keymap, message, destroy_function) {
69     minibuffer_state.call(this, keymap, false);
70     this._message = message;
71     if (destroy_function)
72         this.destroy = destroy_function;
74 minibuffer_message_state.prototype = {
75     __proto__: minibuffer_state.prototype,
76     load: function (window) {
77         minibuffer_state.prototype.load.call(this, window);
78         this.window = window;
79     },
80     unload: function (window) {
81         this.window = null;
82         minibuffer_state.prototype.unload.call(this, window);
83     },
84     get message () { return this._message; },
85     set message (x) {
86         if (this.window) {
87             this.window.minibuffer._restore_normal_state();
88             this.window.minibuffer._show(this._message);
89         }
90     }
94 function minibuffer_input_state (window, keymap, prompt, input, selection_start, selection_end) {
95     minibuffer_state.call(this, keymap, true);
96     this.prompt = prompt;
97     if (input)
98         this.input = input;
99     else
100         this.input = "";
101     if (selection_start)
102         this.selection_start = selection_start;
103     else
104         this.selection_start = 0;
105     if (selection_end)
106         this.selection_end = selection_end;
107     else
108         this.selection_end = this.selection_start;
109     window.input.begin_recursion();
111 minibuffer_input_state.prototype = {
112     __proto__: minibuffer_state.prototype,
113     mark_active : false,
114     destroy: function (window) {
115         window.input.end_recursion();
116         minibuffer_state.prototype.destroy.call(this, window);
117     }
122  * The parameter `args' is an object specifying the arguments for
123  * basic_minibuffer_state.  The following properties of args must/may
124  * be set:
126  * prompt:            [required]
128  * initial_value:     [optional] specifies the initial text
130  * select:            [optional] specifies to select the initial text if set to non-null
131  */
132 define_keywords("$keymap", "$prompt", "$initial_value", "$select");
133 function basic_minibuffer_state (window) {
134     keywords(arguments, $keymap = minibuffer_base_keymap);
135     var initial_value = arguments.$initial_value || "";
136     var sel_start, sel_end;
137     if (arguments.$select) {
138         sel_start = 0;
139         sel_end = initial_value.length;
140     } else {
141         sel_start = sel_end = initial_value.length;
142     }
143     minibuffer_input_state.call(this, window, arguments.$keymap,
144                                 arguments.$prompt, initial_value,
145                                 sel_start, sel_end);
147 basic_minibuffer_state.prototype.__proto__ = minibuffer_input_state.prototype;
150 define_variable("minibuffer_input_mode_show_message_timeout", 1000,
151     "Time duration (in milliseconds) to flash minibuffer messages while in "+
152     "minibuffer input mode.");
155 function minibuffer (window) {
156     this.element = window.document.getElementById("minibuffer");
157     this.output_element = window.document.getElementById("minibuffer-message");
158     this.input_prompt_element = window.document.getElementById("minibuffer-prompt");
159     this.input_element = window.document.getElementById("minibuffer-input");
160     var m = this;
161     this.input_element.inputField.addEventListener("blur",
162         function () {
163             if (m.active && m._input_mode_enabled && !m._showing_message) {
164                 window.setTimeout(function () {
165                         m.input_element.inputField.focus();
166                     }, 0);
167             }
168         }, false);
169     this.input_element.addEventListener("input",
170         function (e) {
171             if (m.ignore_input_events || !m._input_mode_enabled)
172                 return;
173             var s = m.current_state;
174             if (s) {
175                 if (s.handle_input)
176                     s.handle_input(m);
177             }
178         }, true);
180     // Ensure that the input area will have focus if a message is
181     // currently being flashed so that the default handler for key
182     // events will properly add text to the input area.
183     window.addEventListener("keydown",
184         function (e) {
185             if (m._input_mode_enabled && m._showing_message)
186                 m._restore_normal_state();
187         }, true);
188     this.window = window;
189     this.last_message = "";
190     this.states = [];
193 minibuffer.prototype = {
194     constructor: minibuffer.constructor,
195     get _selection_start () { return this.input_element.selectionStart; },
196     get _selection_end () { return this.input_element.selectionEnd; },
197     get _input_text () { return this.input_element.value; },
198     set _input_text (text) { this.input_element.value = text; },
199     get prompt () { return this.input_prompt_element.value; },
200     set prompt (s) { this.input_prompt_element.value = s; },
202     set_input_state: function (x) {
203         this._input_text = x[0];
204         this._set_selection(x[1], x[2]);
205     },
207     _set_selection: function (start, end) {
208         if (start == null)
209             start = this._input_text.length;
210         if (end == null)
211             end = this._input_text.length;
212         this.input_element.setSelectionRange(start,end);
213     },
215     /* Saved focus state */
216     saved_focused_frame: null,
217     saved_focused_element: null,
219     default_message: "",
221     current_message: null,
223     /* This method will display the specified string in the
224      * minibuffer, without recording it in any log/Messages buffer. */
225     show: function (str, force) {
226         if (!this.active || force) {
227             this.current_message = str;
228             this._show(str);
229         }
230     },
232     _show: function (str, force) {
233         if (this.last_message != str) {
234             this.output_element.value = str;
235             this.last_message = str;
236         }
237     },
239     message: function (str) {
240         /* TODO: add the message to a *Messages* buffer, and/or
241          * possibly dump them to the console. */
242         this.show(str, true /* force */);
244         if (str.length > 0 && this.active)
245             this._flash_temporary_message();
246     },
248     clear: function () {
249         this.current_message = null;
250         if (!this.active)
251             this._show(this.default_message);
252     },
254     set_default_message: function (str) {
255         this.default_message = str;
256         if (this.current_message == null)
257             this._show(str);
258     },
260     get current_state () {
261         if (this.states.length == 0)
262             return null;
263         return this.states[this.states.length - 1];
264     },
266     push_state: function (state) {
267         this._save_state();
268         this.states.push(state);
269         this._restore_state();
270     },
272     pop_state: function () {
273         this.current_state.destroy(this.window);
274         this.states.pop();
275         this._restore_state();
276     },
278     pop_all: function () {
279         while (this.states.length > 0) {
280             this.current_state.destroy(this.window);
281             this.states.pop();
282         }
283     },
285     remove_state: function (state) {
286         var i = this.states.indexOf(state);
287         if (i == -1)
288             return;
289         var was_current = (i == (this.states.length - 1));
290         state.destroy(this.window);
291         this.states.splice(i, 1);
292         if (was_current)
293             this._restore_state();
294     },
296     _input_mode_enabled: false,
298     active: false,
300     /* If _input_mode_enabled is true, this is set to indicate that
301      * the message area is being temporarily shown instead of the
302      * input box. */
303     _showing_message: false,
305     _message_timer_ID: null,
307     /* This must only be called if _input_mode_enabled is true */
308     //XXX: if it must only be called if _input_mode_enabled is true,
309     //     then why does it have an else condition?
310     _restore_normal_state: function () {
311         if (this._showing_message) {
312             this.window.clearTimeout(this._message_timer_ID);
313             this._message_timer_ID = null;
314             this._showing_message = false;
316             if (this._input_mode_enabled)
317                 this._switch_to_input_mode();
318             else
319                 // assumes that anything other than an input state is a
320                 // minibuffer_message_state.
321                 this._show(this.current_state._message);
322         }
323     },
325     /* This must only be called if _input_mode_enabled is true */
326     _flash_temporary_message: function () {
327         if (this._showing_message)
328             this.window.clearTimeout(this._message_timer_ID);
329         else {
330             this._showing_message = true;
331             if (this._input_mode_enabled)
332                 this._switch_to_message_mode();
333         }
334         var obj = this;
335         this._message_timer_ID = this.window.setTimeout(function () {
336             obj._restore_normal_state();
337         }, minibuffer_input_mode_show_message_timeout);
338     },
340     _switch_to_input_mode: function () {
341         this.element.setAttribute("minibuffermode", "input");
342         this.input_element.inputField.focus();
343     },
345     _switch_to_message_mode: function () {
346         this.element.setAttribute("minibuffermode", "message");
347     },
349     _restore_state: function () {
350         var s = this.current_state;
351         var want_input_mode = false;
352         if (s) {
353             if (!this.active) {
354                 this.saved_focused_frame = this.window.document.commandDispatcher.focusedWindow;
355                 this.saved_focused_element = this.window.document.commandDispatcher.focusedElement;
356             }
357             if (s.use_input_mode) {
358                 want_input_mode = true;
359                 this._input_text = s.input;
360                 this.prompt = s.prompt;
361                 this._set_selection(s.selection_start, s.selection_end);
362             } else {
363                 this._show(s._message);
364             }
365             s.load(this.window);
366             this.window.input.current.override_keymap = s.keymap;
367             this.active = true;
368         } else {
369             if (this.active) {
370                 this.active = false;
371                 this.window.input.current.override_keymap = null;
372                 if (this.saved_focused_element)
373                     set_focus_no_scroll(this.window, this.saved_focused_element);
374                 else if (this.saved_focused_frame)
375                     set_focus_no_scroll(this.window, this.saved_focused_frame);
376                 this.saved_focused_element = null;
377                 this.saved_focused_frame = null;
378                 this._show(this.current_message || this.default_message);
379             }
380         }
381         var in_input_mode = this._input_mode_enabled && !this._showing_message;
382         if (this._showing_message) {
383             this.window.clearTimeout(this._message_timer_ID);
384             this._message_timer_ID = null;
385             this._showing_message = false;
386         }
387         if (want_input_mode && !in_input_mode)
388             this._switch_to_input_mode();
389         else if (!want_input_mode && in_input_mode)
390             this._switch_to_message_mode();
391         this._input_mode_enabled = want_input_mode;
392     },
394     _save_state: function () {
395         var s = this.current_state;
396         if (s) {
397             if (s.use_input_mode) {
398                 s.input = this._input_text;
399                 s.prompt = this.prompt;
400                 s.selection_start = this._selection_start;
401                 s.selection_end = this._selection_end;
402             }
403             s.unload(this.window);
404         }
405     },
407     insert_before: function (element) {
408         this.element.parentNode.insertBefore(element, this.element);
409     }
413 function minibuffer_initialize_window (window) {
414     window.minibuffer = new minibuffer(window);
416 add_hook("window_initialize_early_hook", minibuffer_initialize_window);
419 function minibuffer_window_close_handler (window) {
420     window.minibuffer.pop_all();
422 add_hook("window_close_hook", minibuffer_window_close_handler);
425 /* Note: This is concise, but doesn't seem to be useful in practice,
426  * because nothing can be done with the state alone. */
427 minibuffer.prototype.check_state = function (type) {
428     var s = this.current_state;
429     if (!(s instanceof type))
430         throw new Error("Invalid minibuffer state.");
431     return s;
434 minibuffer.prototype.show_wait_message = function (initial_message, destroy_function) {
435     var s = new minibuffer_message_state(minibuffer_message_keymap, initial_message, destroy_function);
436     this.push_state(s);
437     return s;
440 minibuffer.prototype.wait_for = function (message, coroutine) {
441     var cc = yield CONTINUATION;
442     var done = false;
443     var s = this.show_wait_message(message, function () { if (!done) cc.throw(abort()); });
444     var result;
445     try {
446         result = yield coroutine;
447     } finally {
448         done = true;
449         this.remove_state(s);
450     }
451     yield co_return(result);
454 provide("minibuffer");