create_buffer() expects a window as its first argument, not a buffer.
[conkeror.git] / modules / buffer.js
blob314a83987fe36a82e662a3b52dcc96239287a88e
1 /**
2  * (C) Copyright 2004-2007 Shawn Betts
3  * (C) Copyright 2007-2008 John J. Foerch
4  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
5  *
6  * Use, modification, and distribution are subject to the terms specified in the
7  * COPYING file.
8 **/
10 var define_buffer_local_hook = local_hook_definer("window");
12 function define_current_buffer_hook(hook_name, existing_hook)
14     define_buffer_local_hook(hook_name);
15     add_hook(existing_hook, function (buffer) {
16             if (!buffer.window.buffers || buffer != buffer.window.buffers.current)
17                 return;
18             var hook = conkeror[hook_name];
19             hook.run.apply(hook, Array.prototype.slice.call(arguments));
20         });
23 define_buffer_local_hook("buffer_title_change_hook");
24 define_buffer_local_hook("buffer_description_change_hook");
25 define_buffer_local_hook("select_buffer_hook");
26 define_buffer_local_hook("create_buffer_hook");
27 define_buffer_local_hook("kill_buffer_hook");
28 define_buffer_local_hook("buffer_scroll_hook");
29 define_buffer_local_hook("buffer_dom_content_loaded_hook");
30 define_buffer_local_hook("buffer_loaded_hook");
32 define_current_buffer_hook("current_buffer_title_change_hook", "buffer_title_change_hook");
33 define_current_buffer_hook("current_buffer_description_change_hook", "buffer_description_change_hook");
34 define_current_buffer_hook("current_buffer_scroll_hook", "buffer_scroll_hook");
35 define_current_buffer_hook("current_buffer_dom_content_loaded_hook", "buffer_dom_content_loaded_hook");
37 function buffer_configuration(existing_configuration) {
38     if (existing_configuration != null) {
39         this.cwd = existing_configuration.cwd;
40     }
41     else {
42         this.cwd = default_directory.path;
43     }
46 define_keywords("$configuration", "$element");
47 function buffer_creator(type) {
48     var args = forward_keywords(arguments);
49     return function (window, element) {
50         return new type(window, element, args);
51     }
54 define_variable("allow_browser_window_close", true,
55                      "If this is set to true, if a content buffer page calls " +
56                      "window.close() from JavaScript and is not prevented by the " +
57                      "normal Mozilla mechanism that restricts pages from closing " +
58                      "a window that was not opened by a script, the buffer will be " +
59                      "killed, deleting the window as well if it is the only buffer.");
61 function buffer(window, element)
63     this.constructor_begin();
64     keywords(arguments, $configuration = null);
65     this.window = window;
66     this.configuration = new buffer_configuration(arguments.$configuration);
67     if (element == null)
68     {
69         element = create_XUL(window, "vbox");
70         element.setAttribute("flex", "1");
71         var browser = create_XUL(window, "browser");
72         browser.setAttribute("type", "content");
73         browser.setAttribute("flex", "1");
74         browser.setAttribute("autocompletepopup", "popup_autocomplete");
75         element.appendChild(browser);
76         this.window.buffers.container.appendChild(element);
77     } else {
78         /* Manually set up session history.
79          *
80          * This is needed because when constructor for the XBL binding
81          * (mozilla/toolkit/content/widgets/browser.xml#browser) for
82          * the initial browser element of the window is called, the
83          * docShell is not yet initialized and setting up the session
84          * history will fail.  To work around this problem, we do as
85          * tabbrowser.xml (Firefox) does and set the initial browser
86          * to have the disablehistory=true attribute, and then repeat
87          * the work that would normally be done in the XBL
88          * constructor.
89          */
91         // This code is taken from mozilla/browser/base/content/browser.js
92         let browser = element.firstChild;
93         browser.webNavigation.sessionHistory =
94             Cc["@mozilla.org/browser/shistory;1"].createInstance(Ci.nsISHistory);
95         observer_service.addObserver(browser, "browser:purge-session-history", false);
97         // remove the disablehistory attribute so the browser cleans up, as
98         // though it had done this work itself
99         browser.removeAttribute("disablehistory");
101         // enable global history
102         browser.docShell.QueryInterface(Ci.nsIDocShellHistory).useGlobalHistory = true;
103     }
104     this.window.buffers.buffer_list.push(this);
105     this.element = element;
106     this.browser = element.firstChild;
107     this.element.conkeror_buffer_object = this;
109     this.enabled_modes = [];
110     this.local_variables = {};
112     var buffer = this;
114     this.browser.addEventListener("scroll", function (event) {
115             buffer_scroll_hook.run(buffer);
116         }, true /* capture */, false /* ignore untrusted events */);
118     this.browser.addEventListener("DOMContentLoaded", function (event) {
119             buffer_dom_content_loaded_hook.run(buffer);
120         }, true /* capture */, false /*ignore untrusted events */);
122     this.browser.addEventListener("load", function (event) {
123             buffer_loaded_hook.run(buffer);
124         }, true /* capture */, false /*ignore untrusted events */);
126     this.browser.addEventListener("DOMWindowClose", function (event) {
127             /* This call to preventDefault is very important; without
128              * it, somehow Mozilla does something bad and as a result
129              * the window loses focus, causing keyboard commands to
130              * stop working. */
131             event.preventDefault();
133             if (allow_browser_window_close)
134                 kill_buffer(buffer, true);
135         }, true);
137     this.constructor_end();
140 buffer.prototype = {
141     /* Saved focus state */
142     saved_focused_frame : null,
143     saved_focused_element : null,
144     on_switch_to : null,
145     on_switch_away : null,
146     // get title ()   [must be defined by subclasses]
147     // get name ()    [must be defined by subclasses]
148     dead : false, /* This is set when the buffer is killed */
150     default_message : "",
152     get : function (x) {
153         if (x in this.local_variables)
154             return this.local_variables[x];
155         return conkeror[x];
156     },
158     set_default_message : function (str) {
159         this.default_message = str;
160         if (this == this.window.buffers.current)
161             this.window.minibuffer.set_default_message(str);
162     },
164     constructors_running : 0,
166     constructor_begin : function () {
167         this.constructors_running++;
168     },
170     constructor_end : function () {
171         if (--this.constructors_running == 0)
172             create_buffer_hook.run(this);
173     },
175     /* General accessors */
176     get cwd () { return this.configuration.cwd; },
178     /* Browser accessors */
179     get top_frame () { return this.browser.contentWindow; },
180     get document () { return this.browser.contentDocument; },
181     get web_navigation () { return this.browser.webNavigation; },
182     get doc_shell () { return this.browser.docShell; },
183     get markup_document_viewer () { return this.browser.markupDocumentViewer; },
184     get current_URI () { return this.browser.currentURI; },
186     is_child_element : function (element)
187     {
188         return (element && this.is_child_frame(element.ownerDocument.defaultView));
189     },
191     is_child_frame : function (frame)
192     {
193         return (frame && frame.top == this.top_frame);
194     },
196     // This method is like focused_frame, except that if no content
197     // frame actually has focus, this returns null.
198     get focused_frame_or_null () {
199         var frame = this.window.document.commandDispatcher.focusedWindow;
200         var top = this.top_frame;
201         if (this.is_child_frame(frame))
202             return frame;
203         return null;
204     },
206     get focused_frame () {
207         var frame = this.window.document.commandDispatcher.focusedWindow;
208         var top = this.top_frame;
209         if (this.is_child_frame(frame))
210             return frame;
211         return this.top_frame;
212     },
214     get focused_element () {
215         var element = this.window.document.commandDispatcher.focusedElement;
216         if (this.is_child_element(element))
217             return element;
218         return null;
219     },
221     do_command : function (command)
222     {
223         function attempt_command(element, command)
224         {
225             var controller;
226             if (element.controllers
227                 && (controller = element.controllers.getControllerForCommand(command)) != null
228                 && controller.isCommandEnabled(command))
229             {
230                 controller.doCommand(command);
231                 return true;
232             }
233             return false;
234         }
236         var element = this.focused_element;
237         if (element && attempt_command(element, command))
238             return;
239         var win = this.focused_frame;
240         do  {
241             if (attempt_command(win, command))
242                 return;
243             if (!win.parent || win == win.parent)
244                 break;
245             win = win.parent;
246         } while (true);
247     },
249     handle_kill : function () {
250         this.dead = true;
251         this.browser = null;
252         this.element = null;
253         this.saved_focused_frame = null;
254         this.saved_focused_element = null;
255         kill_buffer_hook.run(this);
256     }
259 function check_buffer(obj, type) {
260     if (!(obj instanceof type))
261         throw interactive_error("Buffer has invalid type.");
262     if (obj.dead)
263         throw interactive_error("Buffer has already been killed.");
264     return obj;
267 function buffer_container(window, create_initial_buffer)
269     this.window = window;
270     this.container = window.document.getElementById("buffer-container");
271     this.buffer_list = [];
272     window.buffers = this;
274     create_initial_buffer(window, this.container.firstChild);
277 buffer_container.prototype = {
278     constructor : buffer_container,
280     get current () {
281         return this.container.selectedPanel.conkeror_buffer_object;
282     },
284     set current (buffer) {
285         var old_value = this.current;
286         if (old_value == buffer)
287             return;
289         this.buffer_list.splice(this.buffer_list.indexOf(buffer), 1);
290         this.buffer_list.unshift(buffer);
292         this._switch_away_from(this.current);
293         this._switch_to(buffer);
295         // Run hooks
296         select_buffer_hook.run(buffer);
297     },
299     _switch_away_from : function (old_value) {
300         // Save focus state
301         old_value.saved_focused_frame = old_value.focused_frame;
302         old_value.saved_focused_element = old_value.focused_element;
304         old_value.browser.setAttribute("type", "content");
305     },
307     _switch_to : function (buffer) {
308         // Select new buffer in the XUL deck
309         this.container.selectedPanel = buffer.element;
311         buffer.browser.setAttribute("type", "content-primary");
313         /**
314          * This next focus call seems to be needed to avoid focus
315          * somehow getting lost (and the keypress handler therefore
316          * not getting called at all) when killing buffers.
317          */
318         this.window.focus();
320         // Restore focus state
321         if (buffer.saved_focused_element)
322             set_focus_no_scroll(this.window, buffer.saved_focused_element);
323         else if (buffer.saved_focused_frame)
324             set_focus_no_scroll(this.window, buffer.saved_focused_frame);
326         buffer.saved_focused_element = null;
327         buffer.saved_focused_frame = null;
329         this.window.minibuffer.set_default_message(buffer.default_message);
330     },
332     get count () {
333         return this.container.childNodes.length;
334     },
336     get_buffer : function (index) {
337         if (index >= 0 && index < this.count)
338             return this.container.childNodes.item(index).conkeror_buffer_object;
339         return null;
340     },
342     get selected_index () {
343         var nodes = this.container.childNodes;
344         var count = nodes.length;
345         for (var i = 0; i < count; ++i)
346             if (nodes.item(i) == this.container.selectedPanel)
347                 return i;
348         return null;
349     },
351     index_of : function (b) {
352         var nodes = this.container.childNodes;
353         var count = nodes.length;
354         for (var i = 0; i < count; ++i)
355             if (nodes.item(i) == b.element)
356                 return i;
357         return null;
358     },
360     get unique_name_list () {
361         var existing_names = new string_hashset();
362         var bufs = [];
363         this.for_each(function(b) {
364                 var base_name = b.name;
365                 var name = base_name;
366                 var index = 1;
367                 while (existing_names.contains(name))
368                 {
369                     ++index;
370                     name = base_name + "<" + index + ">";
371                 }
372                 existing_names.add(name);
373                 bufs.push([name, b]);
374             });
375         return bufs;
376     },
378     kill_buffer : function (b) {
379         if (b.dead)
380             return true;
381         var count = this.count;
382         if (count <= 1)
383             return false;
384         var new_buffer = this.buffer_list[0];
385         var changed = false;
386         if (b == new_buffer) {
387             new_buffer = this.buffer_list[1];
388             changed = true;
389         }
390         this._switch_away_from(this.current);
391         this.container.removeChild(b.element);
392         this.buffer_list.splice(this.buffer_list.indexOf(b), 1);
393         this._switch_to(new_buffer);
394         if (changed) {
395             select_buffer_hook.run(new_buffer);
396             this.buffer_list.splice(this.buffer_list.indexOf(new_buffer), 1);
397             this.buffer_list.unshift(new_buffer);
398         }
399         b.handle_kill();
400         return true;
401     },
403     bury_buffer : function(b) {
404       var new_buffer = this.buffer_list[0];
405       if (b == new_buffer) new_buffer = this.buffer_list[1];
406       this.buffer_list.splice(this.buffer_list.indexOf(b), 1);
407       this.buffer_list.push(b);
408       this.current = new_buffer;
409       return true;
410     },
412     for_each : function (f) {
413         var count = this.count;
414         for (var i = 0; i < count; ++i)
415             f(this.get_buffer(i));
416     }
419 function buffer_initialize_window_early(window)
421     /**
422      * Use content_buffer by default to handle an unusual case where
423      * browser.chromeURI is used perhaps.  In general this default
424      * should not be needed.
425      */
427     var create_initial_buffer
428         = window.args.initial_buffer_creator || buffer_creator(content_buffer);
429     new buffer_container(window, create_initial_buffer);
432 add_hook("window_initialize_early_hook", buffer_initialize_window_early);
435 define_buffer_local_hook("buffer_kill_before_hook", RUN_HOOK_UNTIL_FAILURE);
436 function buffer_before_window_close(window)
438     var bs = window.buffers;
439     var count = bs.count;
440     for (let i = 0; i < count; ++i) {
441         if (!buffer_kill_before_hook.run(bs.get_buffer(i)))
442             return false;
443     }
444     return true;
446 add_hook("window_before_close_hook", buffer_before_window_close);
448 function buffer_window_close_handler(window)
450     var bs = window.buffers;
451     var count = bs.count;
452     for (let i = 0; i < count; ++i) {
453         let b = bs.get_buffer(i);
454         b.handle_kill();
455     }
457 add_hook("window_close_hook", buffer_window_close_handler);
459 /* open/follow targets */
460 const OPEN_CURRENT_BUFFER = 0; // only valid for open if the current
461                                // buffer is a content_buffer; for
462                                // follow, equivalent to
463                                // FOLLOW_TOP_FRAME.
464 const OPEN_NEW_BUFFER = 1;
465 const OPEN_NEW_BUFFER_BACKGROUND = 2;
466 const OPEN_NEW_WINDOW = 3;
468 const FOLLOW_DEFAULT = 4; // for open, implies OPEN_CURRENT_BUFFER
469 const FOLLOW_CURRENT_FRAME = 5; // for open, implies OPEN_CURRENT_BUFFER
470 const FOLLOW_TOP_FRAME = 6; // for open, implies OPEN_CURRENT_BUFFER
472 var TARGET_PROMPTS = [" in current buffer",
473                       " in new buffer",
474                       " in new buffer (background)",
475                       " in new window",
476                       "",
477                       " in current frame",
478                       " in top frame"];
480 var TARGET_NAMES = ["current buffer",
481                     "new buffer",
482                     "new buffer (background)",
483                     "new window",
484                     "default",
485                     "current frame",
486                     "top frame"];
489 function browse_target_prompt(target, prefix) {
490     if (prefix == null)
491         prefix = "Open URL";
492     return prefix + TARGET_PROMPTS[target] + ":";
496 var default_browse_targets = {};
497 default_browse_targets["follow"] = [FOLLOW_DEFAULT, OPEN_NEW_BUFFER, OPEN_NEW_WINDOW];
498 default_browse_targets["follow-top"] = [FOLLOW_TOP_FRAME, FOLLOW_CURRENT_FRAME];
500 interactive_context.prototype.browse_target = function (action) {
501     var prefix = this.prefix_argument;
502     var targets = action;
503     while (typeof(targets) == "string")
504         targets = default_browse_targets[targets];
505     if (prefix == null || typeof(prefix) != "object")
506         return targets[0];
507     var num = prefix[0];
508     var index = 0;
509     while (num >= 4 && index + 1 < targets.length) {
510         num = num / 4;
511         index++;
512     }
513     return targets[index];
516 function create_buffer(window, creator, target) {
517     switch (target) {
518     case OPEN_NEW_BUFFER:
519         window.buffers.current = creator(window, null);
520         break;
521     case OPEN_NEW_BUFFER_BACKGROUND:
522         creator(window, null);
523         break;
524     case OPEN_NEW_WINDOW:
525         make_window(creator);
526         break;
527     default:
528         throw new Error("invalid target");
529     }
532 var queued_buffer_creators = null;
533 function _process_queued_buffer_creators(window) {
534     for (var i = 0; i < queued_buffer_creators.length; ++i) {
535         var x = queued_buffer_creators[i];
536         create_buffer(window, x[0], x[1]);
537     }
538     queued_buffer_creators = null;
540 function create_buffer_in_current_window(creator, target, focus_existing) {
541     if (target == OPEN_NEW_WINDOW)
542         throw new Error("invalid target");
543     var window = get_recent_conkeror_window();
544     if (window) {
545         if (focus_existing)
546             window.focus();
547         create_buffer(window, creator, target);
548     } else if (queued_buffer_creators != null) {
549         queued_buffer_creators.push([creator,target]);
550     } else {
551         queued_buffer_creators = [];
552         window = make_window(creator);
553         add_hook.call(window, "window_initialize_late_hook", _process_queued_buffer_creators);
554     }
557 minibuffer_auto_complete_preferences["buffer"] = true;
558 define_keywords("$default");
559 minibuffer.prototype.read_buffer = function () {
560     var window = this.window;
561     var buffer = this.window.buffers.current;
562     keywords(arguments, $prompt = "Buffer:",
563              $default = buffer,
564              $history = "buffer");
565     var completer = all_word_completer(
566         $completions = function (visitor) window.buffers.for_each(visitor),
567         $get_string = function (x) x.description,
568         $get_description = function (x) x.title);
569     var result = yield this.read(
570         $prompt = arguments.$prompt,
571         $history = arguments.$history,
572         $completer = completer,
573         $match_required = true,
574         $auto_complete = "buffer",
575         $auto_complete_initial = true,
576         $auto_complete_delay = 0,
577         $default_completion = arguments.$default);
578     yield co_return(result);
581 interactive_context.prototype.__defineGetter__("cwd", function () this.buffer.cwd);
583 function buffer_next (window, count)
585     var index = window.buffers.selected_index;
586     var total = window.buffers.count;
587     index = (index + count) % total;
588     if (index < 0)
589         index += total;
590     window.buffers.current = window.buffers.get_buffer(index);
592 interactive("buffer-next",
593             "Switch to the next buffer.",
594             function (I) {buffer_next(I.window, I.p);});
595 interactive("buffer-previous",
596             "Switch to the previous buffer.",
597             function (I) {buffer_next(I.window, -I.p);});
599 function switch_to_buffer (window, buffer)
601     if (buffer && !buffer.dead)
602         window.buffers.current = buffer;
604 interactive("switch-to-buffer",
605             "Switch to a buffer specified in the minibuffer.",
606             function (I) {
607                 switch_to_buffer(
608                     I.window,
609                     (yield I.minibuffer.read_buffer(
610                         $prompt = "Switch to buffer:",
611                         $default = (I.window.buffers.count > 1 ?
612                                     I.window.buffers.buffer_list[1] :
613                                     I.buffer)))
614                 )
615             });
617 define_variable("can_kill_last_buffer", true,
618                      "If this is set to true, kill-buffer can kill the last remaining buffer, and close the window.");
620 function kill_buffer(buffer, force)
622     if (!buffer)
623         return;
624     var buffers = buffer.window.buffers;
625     if (buffers.count == 1 && buffer == buffers.current) {
626         if (can_kill_last_buffer || force) {
627             delete_window(buffer.window);
628             return;
629         }
630         else
631             throw interactive_error("Can't kill last buffer.");
632     }
633     buffers.kill_buffer(buffer);
635 interactive("kill-buffer",
636             "Kill a buffer specified in the minibuffer.\n" +
637             "If `can_kill_last_buffer' is set to true, an attempt to kill the last remaining " +
638             "buffer in a window will cause the window to be closed.",
639             function (I) {kill_buffer((yield I.minibuffer.read_buffer($prompt = "Kill buffer:")))});
641 interactive("kill-current-buffer",
642             "Kill the current buffer.\n" +
643             "If `can_kill_last_buffer' is set to true, an attempt to kill the last remaining " +
644             "buffer in a window will cause the window to be closed.",
645             function (I) {kill_buffer(I.buffer)});
647 interactive("bury-buffer",
648             "Bury the current buffer.\n Put the current buffer at the end of " +
649             "the buffer list, so that it is the least likely buffer to be " +
650             "selected by `switch-to-buffer'.",
651             function (I) {I.window.buffers.bury_buffer(I.buffer);});
653 function change_directory(buffer, dir) {
654     buffer.configuration.cwd = dir;
656 interactive("change-current-directory",
657             "Change the current directory of the selected buffer.",
658             function (I) {
659                 change_directory(
660                     I.buffer,
661                     (yield I.minibuffer.read_existing_directory_path(
662                         $prompt = "New current directory:",
663                         $initial_value = I.cwd)));
664             });
666 interactive("shell-command", null, function (I) {
667     var cwd = I.cwd;
668     var cmd = (yield I.minibuffer.read_shell_command($cwd = cwd));
669     yield shell_command(cmd, $cwd = cwd);
672 function unfocus(window, buffer)
674     var elem = buffer.focused_element;
675     if (elem) {
676         elem.blur();
677         return;
678     }
679     var win = buffer.focused_frame;
680     if (win != buffer.top_frame)
681         return;
682     clear_selection(buffer);
683     buffer.top_frame.focus();
685 interactive("unfocus", null, function (I) {
686     unfocus(I.window, I.buffer);
687     I.window.minibuffer.message("unfocus");
690 require_later("content-buffer.js");
692 var mode_functions = {};
694 var mode_display_names = {};
696 define_buffer_local_hook("buffer_mode_change_hook");
697 define_current_buffer_hook("current_buffer_mode_change_hook", "buffer_mode_change_hook");
699 define_keywords("$class", "$enable", "$disable", "$doc");
700 function define_buffer_mode(name, display_name) {
701     keywords(arguments);
703     var hyphen_name = name.replace("_","-","g");
704     var mode_class = arguments.$class;
705     var enable = arguments.$enable;
706     var disable = arguments.$disable;
708     mode_display_names[name] = display_name;
710     var can_disable;
712     if (disable == false) {
713         can_disable = false;
714         disable = null;
715     } else
716         can_disable = true;
718     var state = (mode_class != null) ? mode_class : (name + "_enabled");
719     var enable_hook_name = name + "_enable_hook";
720     var disable_hook_name = name + "_disable_hook";
721     define_buffer_local_hook(enable_hook_name);
722     define_buffer_local_hook(disable_hook_name);
724     var change_hook_name = null;
726     if (mode_class) {
727         mode_functions[name] = {enable: enable,
728                                 disable: disable,
729                                 mode_class: mode_class,
730                                 disable_hook_name: disable_hook_name};
731         change_hook_name = mode_class + "_change_hook";
732         define_buffer_local_hook(change_hook_name);
733     }
735     function func(buffer, arg) {
736         var old_state = buffer[state];
737         var cur_state = (old_state == name);
738         var new_state = (arg == null) ? !cur_state : (arg > 0);
739         if ((new_state == cur_state) || (!can_disable && !new_state))
740             return null;
741         if (new_state) {
742             if (mode_class && old_state != null)  {
743                 buffer.enabled_modes.splice(buffer.enabled_modes.indexOf(old_state), 1);
744                 let x = mode_functions[old_state];
745                 let y = x.disable;
746                 if (y) y(buffer);
747                 conkeror[x.disable_hook_name].run(buffer);
748             }
749             buffer[state] = name;
750             if (enable)
751                 enable(buffer);
752             conkeror[enable_hook_name].run(buffer);
753             buffer.enabled_modes.push(name);
754         } else {
755             buffer.enabled_modes.splice(buffer.enabled_modes.indexOf(name), 1);
756             disable(buffer);
757             conkeror[disable_hook_name].run(buffer);
758             buffer[state] = null;
759         }
760         if (change_hook_name)
761             conkeror[change_hook_name].run(buffer, buffer[state]);
762         buffer_mode_change_hook.run(buffer);
763         return new_state;
764     };
765     conkeror[name] = func;
766     interactive(hyphen_name, arguments.$doc, function (I) {
767         var arg = I.P;
768         var new_state = func(I.buffer, arg && univ_arg_to_number(arg));
769         I.minibuffer.message(hyphen_name + (new_state ? " enabled" : " disabled"));
770     });
772 ignore_function_for_get_caller_source_code_reference("define_buffer_mode");
775 function minibuffer_mode_indicator(window) {
776     this.window = window;
777     var element = create_XUL(window, "label");
778     element.setAttribute("id", "minibuffer-mode-indicator");
779     element.collapsed = true;
780     element.setAttribute("class", "minibuffer");
781     window.document.getElementById("minibuffer").appendChild(element);
782     this.element = element;
783     this.hook_func = method_caller(this, this.update);
784     add_hook.call(window, "select_buffer_hook", this.hook_func);
785     add_hook.call(window, "current_buffer_mode_change_hook", this.hook_func);
786     this.update();
788 minibuffer_mode_indicator.prototype = {
789     update : function () {
790         var buf = this.window.buffers.current;
791         var modes = buf.enabled_modes;
792         var str = modes.map( function (x) {
793             let y = mode_display_names[x];
794             if (y)
795                 return "[" + y + "]";
796             else
797                 return null;
798         } ).filter( function (x) x != null ).join(" ");
799         this.element.collapsed = (str.length == 0);
800         this.element.value = str;
801     },
802     uninstall : function () {
803         remove_hook.call(window, "select_buffer_hook", this.hook_fun);
804         remove_hook.call(window, "current_buffer_mode_change_hook", this.hook_fun);
805         this.element.parentNode.removeChild(this.element);
806     }
808 define_global_window_mode("minibuffer_mode_indicator", "window_initialize_hook");
809 minibuffer_mode_indicator_mode(true);