browser-next-form-field: skip non-visible elements
[conkeror.git] / modules / buffer.js
blob0e02d6372d6bdc687fc278009e1b83edbb074e4d
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 = {};
111     this.default_browser_object_classes = {};
113     var buffer = this;
115     this.browser.addEventListener("scroll", function (event) {
116             buffer_scroll_hook.run(buffer);
117         }, true /* capture */, false /* ignore untrusted events */);
119     this.browser.addEventListener("DOMContentLoaded", function (event) {
120             buffer_dom_content_loaded_hook.run(buffer);
121         }, true /* capture */, false /*ignore untrusted events */);
123     this.browser.addEventListener("load", function (event) {
124             buffer_loaded_hook.run(buffer);
125         }, true /* capture */, false /*ignore untrusted events */);
127     this.browser.addEventListener("DOMWindowClose", function (event) {
128             /* This call to preventDefault is very important; without
129              * it, somehow Mozilla does something bad and as a result
130              * the window loses focus, causing keyboard commands to
131              * stop working. */
132             event.preventDefault();
134             if (allow_browser_window_close)
135                 kill_buffer(buffer, true);
136         }, true);
138     this.constructor_end();
141 buffer.prototype = {
142     /* Saved focus state */
143     saved_focused_frame : null,
144     saved_focused_element : null,
145     on_switch_to : null,
146     on_switch_away : null,
147     // get title ()   [must be defined by subclasses]
148     // get name ()    [must be defined by subclasses]
149     dead : false, /* This is set when the buffer is killed */
151     default_message : "",
153     get : function (x) {
154         if (x in this.local_variables)
155             return this.local_variables[x];
156         return conkeror[x];
157     },
159     set_default_message : function (str) {
160         this.default_message = str;
161         if (this == this.window.buffers.current)
162             this.window.minibuffer.set_default_message(str);
163     },
165     constructors_running : 0,
167     constructor_begin : function () {
168         this.constructors_running++;
169     },
171     constructor_end : function () {
172         if (--this.constructors_running == 0)
173             create_buffer_hook.run(this);
174     },
176     /* General accessors */
177     get cwd () { return this.configuration.cwd; },
179     /* Browser accessors */
180     get top_frame () { return this.browser.contentWindow; },
181     get document () { return this.browser.contentDocument; },
182     get web_navigation () { return this.browser.webNavigation; },
183     get doc_shell () { return this.browser.docShell; },
184     get markup_document_viewer () { return this.browser.markupDocumentViewer; },
185     get current_URI () { return this.browser.currentURI; },
187     is_child_element : function (element)
188     {
189         return (element && this.is_child_frame(element.ownerDocument.defaultView));
190     },
192     is_child_frame : function (frame)
193     {
194         return (frame && frame.top == this.top_frame);
195     },
197     // This method is like focused_frame, except that if no content
198     // frame actually has focus, this returns null.
199     get focused_frame_or_null () {
200         var frame = this.window.document.commandDispatcher.focusedWindow;
201         var top = this.top_frame;
202         if (this.is_child_frame(frame))
203             return frame;
204         return null;
205     },
207     get focused_frame () {
208         var frame = this.window.document.commandDispatcher.focusedWindow;
209         var top = this.top_frame;
210         if (this.is_child_frame(frame))
211             return frame;
212         return this.top_frame;
213     },
215     get focused_element () {
216         var element = this.window.document.commandDispatcher.focusedElement;
217         if (this.is_child_element(element))
218             return element;
219         return null;
220     },
222     do_command : function (command)
223     {
224         function attempt_command(element, command)
225         {
226             var controller;
227             if (element.controllers
228                 && (controller = element.controllers.getControllerForCommand(command)) != null
229                 && controller.isCommandEnabled(command))
230             {
231                 controller.doCommand(command);
232                 return true;
233             }
234             return false;
235         }
237         var element = this.focused_element;
238         if (element && attempt_command(element, command))
239             return;
240         var win = this.focused_frame;
241         do  {
242             if (attempt_command(win, command))
243                 return;
244             if (!win.parent || win == win.parent)
245                 break;
246             win = win.parent;
247         } while (true);
248     },
250     handle_kill : function () {
251         this.dead = true;
252         this.browser = null;
253         this.element = null;
254         this.saved_focused_frame = null;
255         this.saved_focused_element = null;
256         kill_buffer_hook.run(this);
257     }
260 function check_buffer(obj, type) {
261     if (!(obj instanceof type))
262         throw interactive_error("Buffer has invalid type.");
263     if (obj.dead)
264         throw interactive_error("Buffer has already been killed.");
265     return obj;
268 function buffer_container(window, create_initial_buffer)
270     this.window = window;
271     this.container = window.document.getElementById("buffer-container");
272     this.buffer_list = [];
273     window.buffers = this;
275     create_initial_buffer(window, this.container.firstChild);
278 buffer_container.prototype = {
279     constructor : buffer_container,
281     get current () {
282         return this.container.selectedPanel.conkeror_buffer_object;
283     },
285     set current (buffer) {
286         var old_value = this.current;
287         if (old_value == buffer)
288             return;
290         this.buffer_list.splice(this.buffer_list.indexOf(buffer), 1);
291         this.buffer_list.unshift(buffer);
293         this._switch_away_from(this.current);
294         this._switch_to(buffer);
296         // Run hooks
297         select_buffer_hook.run(buffer);
298     },
300     _switch_away_from : function (old_value) {
301         // Save focus state
302         old_value.saved_focused_frame = old_value.focused_frame;
303         old_value.saved_focused_element = old_value.focused_element;
305         old_value.browser.setAttribute("type", "content");
306     },
308     _switch_to : function (buffer) {
309         // Select new buffer in the XUL deck
310         this.container.selectedPanel = buffer.element;
312         buffer.browser.setAttribute("type", "content-primary");
314         /**
315          * This next focus call seems to be needed to avoid focus
316          * somehow getting lost (and the keypress handler therefore
317          * not getting called at all) when killing buffers.
318          */
319         this.window.focus();
321         // Restore focus state
322         if (buffer.saved_focused_element)
323             set_focus_no_scroll(this.window, buffer.saved_focused_element);
324         else if (buffer.saved_focused_frame)
325             set_focus_no_scroll(this.window, buffer.saved_focused_frame);
327         buffer.saved_focused_element = null;
328         buffer.saved_focused_frame = null;
330         this.window.minibuffer.set_default_message(buffer.default_message);
331     },
333     get count () {
334         return this.container.childNodes.length;
335     },
337     get_buffer : function (index) {
338         if (index >= 0 && index < this.count)
339             return this.container.childNodes.item(index).conkeror_buffer_object;
340         return null;
341     },
343     get selected_index () {
344         var nodes = this.container.childNodes;
345         var count = nodes.length;
346         for (var i = 0; i < count; ++i)
347             if (nodes.item(i) == this.container.selectedPanel)
348                 return i;
349         return null;
350     },
352     index_of : function (b) {
353         var nodes = this.container.childNodes;
354         var count = nodes.length;
355         for (var i = 0; i < count; ++i)
356             if (nodes.item(i) == b.element)
357                 return i;
358         return null;
359     },
361     get unique_name_list () {
362         var existing_names = new string_hashset();
363         var bufs = [];
364         this.for_each(function(b) {
365                 var base_name = b.name;
366                 var name = base_name;
367                 var index = 1;
368                 while (existing_names.contains(name))
369                 {
370                     ++index;
371                     name = base_name + "<" + index + ">";
372                 }
373                 existing_names.add(name);
374                 bufs.push([name, b]);
375             });
376         return bufs;
377     },
379     kill_buffer : function (b) {
380         if (b.dead)
381             return true;
382         var count = this.count;
383         if (count <= 1)
384             return false;
385         var new_buffer = this.buffer_list[0];
386         var changed = false;
387         if (b == new_buffer) {
388             new_buffer = this.buffer_list[1];
389             changed = true;
390         }
391         this._switch_away_from(this.current);
392         this.container.removeChild(b.element);
393         this.buffer_list.splice(this.buffer_list.indexOf(b), 1);
394         this._switch_to(new_buffer);
395         if (changed) {
396             select_buffer_hook.run(new_buffer);
397             this.buffer_list.splice(this.buffer_list.indexOf(new_buffer), 1);
398             this.buffer_list.unshift(new_buffer);
399         }
400         b.handle_kill();
401         return true;
402     },
404     bury_buffer : function(b) {
405       var new_buffer = this.buffer_list[0];
406       if (b == new_buffer) new_buffer = this.buffer_list[1];
407       this.buffer_list.splice(this.buffer_list.indexOf(b), 1);
408       this.buffer_list.push(b);
409       this.current = new_buffer;
410       return true;
411     },
413     for_each : function (f) {
414         var count = this.count;
415         for (var i = 0; i < count; ++i)
416             f(this.get_buffer(i));
417     }
420 function buffer_initialize_window_early(window)
422     /**
423      * Use content_buffer by default to handle an unusual case where
424      * browser.chromeURI is used perhaps.  In general this default
425      * should not be needed.
426      */
428     var create_initial_buffer
429         = window.args.initial_buffer_creator || buffer_creator(content_buffer);
430     new buffer_container(window, create_initial_buffer);
433 add_hook("window_initialize_early_hook", buffer_initialize_window_early);
436 define_buffer_local_hook("buffer_kill_before_hook", RUN_HOOK_UNTIL_FAILURE);
437 function buffer_before_window_close(window)
439     var bs = window.buffers;
440     var count = bs.count;
441     for (let i = 0; i < count; ++i) {
442         if (!buffer_kill_before_hook.run(bs.get_buffer(i)))
443             return false;
444     }
445     return true;
447 add_hook("window_before_close_hook", buffer_before_window_close);
449 function buffer_window_close_handler(window)
451     var bs = window.buffers;
452     var count = bs.count;
453     for (let i = 0; i < count; ++i) {
454         let b = bs.get_buffer(i);
455         b.handle_kill();
456     }
458 add_hook("window_close_hook", buffer_window_close_handler);
460 /* open/follow targets */
461 const OPEN_CURRENT_BUFFER = 0; // only valid for open if the current
462                                // buffer is a content_buffer; for
463                                // follow, equivalent to
464                                // FOLLOW_TOP_FRAME.
465 const OPEN_NEW_BUFFER = 1;
466 const OPEN_NEW_BUFFER_BACKGROUND = 2;
467 const OPEN_NEW_WINDOW = 3;
469 const FOLLOW_DEFAULT = 4; // for open, implies OPEN_CURRENT_BUFFER
470 const FOLLOW_CURRENT_FRAME = 5; // for open, implies OPEN_CURRENT_BUFFER
471 const FOLLOW_TOP_FRAME = 6; // for open, implies OPEN_CURRENT_BUFFER
473 var TARGET_PROMPTS = [" in current buffer",
474                       " in new buffer",
475                       " in new buffer (background)",
476                       " in new window",
477                       "",
478                       " in current frame",
479                       " in top frame"];
481 var TARGET_NAMES = ["current buffer",
482                     "new buffer",
483                     "new buffer (background)",
484                     "new window",
485                     "default",
486                     "current frame",
487                     "top frame"];
490 function create_buffer(window, creator, target) {
491     switch (target) {
492     case OPEN_NEW_BUFFER:
493         window.buffers.current = creator(window, null);
494         break;
495     case OPEN_NEW_BUFFER_BACKGROUND:
496         creator(window, null);
497         break;
498     case OPEN_NEW_WINDOW:
499         make_window(creator);
500         break;
501     default:
502         throw new Error("invalid target");
503     }
506 var queued_buffer_creators = null;
507 function _process_queued_buffer_creators(window) {
508     for (var i = 0; i < queued_buffer_creators.length; ++i) {
509         var x = queued_buffer_creators[i];
510         create_buffer(window, x[0], x[1]);
511     }
512     queued_buffer_creators = null;
514 function create_buffer_in_current_window(creator, target, focus_existing) {
515     if (target == OPEN_NEW_WINDOW)
516         throw new Error("invalid target");
517     var window = get_recent_conkeror_window();
518     if (window) {
519         if (focus_existing)
520             window.focus();
521         create_buffer(window, creator, target);
522     } else if (queued_buffer_creators != null) {
523         queued_buffer_creators.push([creator,target]);
524     } else {
525         queued_buffer_creators = [];
526         window = make_window(creator);
527         add_hook.call(window, "window_initialize_late_hook", _process_queued_buffer_creators);
528     }
531 minibuffer_auto_complete_preferences["buffer"] = true;
532 define_keywords("$default");
533 minibuffer.prototype.read_buffer = function () {
534     var window = this.window;
535     var buffer = this.window.buffers.current;
536     keywords(arguments, $prompt = "Buffer:",
537              $default = buffer,
538              $history = "buffer");
539     var completer = all_word_completer(
540         $completions = function (visitor) window.buffers.for_each(visitor),
541         $get_string = function (x) x.description,
542         $get_description = function (x) x.title);
543     var result = yield this.read(
544         $prompt = arguments.$prompt,
545         $history = arguments.$history,
546         $completer = completer,
547         $match_required = true,
548         $auto_complete = "buffer",
549         $auto_complete_initial = true,
550         $auto_complete_delay = 0,
551         $default_completion = arguments.$default);
552     yield co_return(result);
555 interactive_context.prototype.__defineGetter__("cwd", function () this.buffer.cwd);
557 function buffer_next (window, count)
559     var index = window.buffers.selected_index;
560     var total = window.buffers.count;
561     index = (index + count) % total;
562     if (index < 0)
563         index += total;
564     window.buffers.current = window.buffers.get_buffer(index);
566 interactive("buffer-next",
567             "Switch to the next buffer.",
568             function (I) {buffer_next(I.window, I.p);});
569 interactive("buffer-previous",
570             "Switch to the previous buffer.",
571             function (I) {buffer_next(I.window, -I.p);});
573 function switch_to_buffer (window, buffer)
575     if (buffer && !buffer.dead)
576         window.buffers.current = buffer;
578 interactive("switch-to-buffer",
579             "Switch to a buffer specified in the minibuffer.",
580             function (I) {
581                 switch_to_buffer(
582                     I.window,
583                     (yield I.minibuffer.read_buffer(
584                         $prompt = "Switch to buffer:",
585                         $default = (I.window.buffers.count > 1 ?
586                                     I.window.buffers.buffer_list[1] :
587                                     I.buffer))));
588             });
590 define_variable("can_kill_last_buffer", true,
591                 "If this is set to true, kill-buffer can kill the last "+
592                 "remaining buffer, and close the window.");
594 function kill_other_buffers(buffer)
596     if (!buffer)
597         return;
598     var bs = buffer.window.buffers;
599     var b;
601     while ((b = bs.get_buffer(0)) != buffer)
602             bs.kill_buffer(b);
603     var count = bs.count;
604     while (--count)
605             bs.kill_buffer(bs.get_buffer(1));
607 interactive("kill-other-buffers",
608             "Kill all buffers except current one.\n",
609             function (I) { kill_other_buffers(I.buffer); });
612 function kill_buffer(buffer, force)
614     if (!buffer)
615         return;
616     var buffers = buffer.window.buffers;
617     if (buffers.count == 1 && buffer == buffers.current) {
618         if (can_kill_last_buffer || force) {
619             delete_window(buffer.window);
620             return;
621         }
622         else
623             throw interactive_error("Can't kill last buffer.");
624     }
625     buffers.kill_buffer(buffer);
627 interactive("kill-buffer",
628             "Kill a buffer specified in the minibuffer.\n" +
629             "If `can_kill_last_buffer' is set to true, an attempt to kill the last remaining " +
630             "buffer in a window will cause the window to be closed.",
631             function (I) { kill_buffer((yield I.minibuffer.read_buffer($prompt = "Kill buffer:"))); });
633 interactive("kill-current-buffer",
634             "Kill the current buffer.\n" +
635             "If `can_kill_last_buffer' is set to true, an attempt to kill the last remaining " +
636             "buffer in a window will cause the window to be closed.",
637             function (I) { kill_buffer(I.buffer); });
639 interactive("bury-buffer",
640             "Bury the current buffer.\n Put the current buffer at the end of " +
641             "the buffer list, so that it is the least likely buffer to be " +
642             "selected by `switch-to-buffer'.",
643             function (I) {I.window.buffers.bury_buffer(I.buffer);});
645 function change_directory(buffer, dir) {
646     buffer.configuration.cwd = dir;
648 interactive("change-current-directory",
649             "Change the current directory of the selected buffer.",
650             function (I) {
651                 change_directory(
652                     I.buffer,
653                     (yield I.minibuffer.read_existing_directory_path(
654                         $prompt = "New current directory:",
655                         $initial_value = I.cwd)));
656             });
658 interactive("shell-command", null, function (I) {
659     var cwd = I.cwd;
660     var cmd = (yield I.minibuffer.read_shell_command($cwd = cwd));
661     yield shell_command(cmd, $cwd = cwd);
664 function unfocus(window, buffer)
666     var elem = buffer.focused_element;
667     if (elem) {
668         elem.blur();
669         return;
670     }
671     var win = buffer.focused_frame;
672     if (win != buffer.top_frame)
673         return;
674     clear_selection(buffer);
675     buffer.top_frame.focus();
677 interactive("unfocus", null, function (I) {
678     unfocus(I.window, I.buffer);
679     I.window.minibuffer.message("unfocus");
682 require_later("content-buffer.js");
684 var mode_functions = {};
686 var mode_display_names = {};
688 define_buffer_local_hook("buffer_mode_change_hook");
689 define_current_buffer_hook("current_buffer_mode_change_hook", "buffer_mode_change_hook");
691 define_keywords("$class", "$enable", "$disable", "$doc");
692 function define_buffer_mode(name, display_name) {
693     keywords(arguments);
695     var hyphen_name = name.replace("_","-","g");
696     var mode_class = arguments.$class;
697     var enable = arguments.$enable;
698     var disable = arguments.$disable;
700     mode_display_names[name] = display_name;
702     var can_disable;
704     if (disable == false) {
705         can_disable = false;
706         disable = null;
707     } else
708         can_disable = true;
710     var state = (mode_class != null) ? mode_class : (name + "_enabled");
711     var enable_hook_name = name + "_enable_hook";
712     var disable_hook_name = name + "_disable_hook";
713     define_buffer_local_hook(enable_hook_name);
714     define_buffer_local_hook(disable_hook_name);
716     var change_hook_name = null;
718     if (mode_class) {
719         mode_functions[name] = {enable: enable,
720                                 disable: disable,
721                                 mode_class: mode_class,
722                                 disable_hook_name: disable_hook_name};
723         change_hook_name = mode_class + "_change_hook";
724         define_buffer_local_hook(change_hook_name);
725     }
727     function func(buffer, arg) {
728         var old_state = buffer[state];
729         var cur_state = (old_state == name);
730         var new_state = (arg == null) ? !cur_state : (arg > 0);
731         if ((new_state == cur_state) || (!can_disable && !new_state))
732             return null;
733         if (new_state) {
734             if (mode_class && old_state != null)  {
735                 buffer.enabled_modes.splice(buffer.enabled_modes.indexOf(old_state), 1);
736                 let x = mode_functions[old_state];
737                 let y = x.disable;
738                 if (y) y(buffer);
739                 conkeror[x.disable_hook_name].run(buffer);
740             }
741             buffer[state] = name;
742             if (enable)
743                 enable(buffer);
744             conkeror[enable_hook_name].run(buffer);
745             buffer.enabled_modes.push(name);
746         } else {
747             buffer.enabled_modes.splice(buffer.enabled_modes.indexOf(name), 1);
748             disable(buffer);
749             conkeror[disable_hook_name].run(buffer);
750             buffer[state] = null;
751         }
752         if (change_hook_name)
753             conkeror[change_hook_name].run(buffer, buffer[state]);
754         buffer_mode_change_hook.run(buffer);
755         return new_state;
756     };
757     conkeror[name] = func;
758     interactive(hyphen_name, arguments.$doc, function (I) {
759         var arg = I.P;
760         var new_state = func(I.buffer, arg && univ_arg_to_number(arg));
761         I.minibuffer.message(hyphen_name + (new_state ? " enabled" : " disabled"));
762     });
764 ignore_function_for_get_caller_source_code_reference("define_buffer_mode");
767 function minibuffer_mode_indicator(window) {
768     this.window = window;
769     var element = create_XUL(window, "label");
770     element.setAttribute("id", "minibuffer-mode-indicator");
771     element.collapsed = true;
772     element.setAttribute("class", "minibuffer");
773     window.document.getElementById("minibuffer").appendChild(element);
774     this.element = element;
775     this.hook_func = method_caller(this, this.update);
776     add_hook.call(window, "select_buffer_hook", this.hook_func);
777     add_hook.call(window, "current_buffer_mode_change_hook", this.hook_func);
778     this.update();
780 minibuffer_mode_indicator.prototype = {
781     update : function () {
782         var buf = this.window.buffers.current;
783         var modes = buf.enabled_modes;
784         var str = modes.map( function (x) {
785             let y = mode_display_names[x];
786             if (y)
787                 return "[" + y + "]";
788             else
789                 return null;
790         } ).filter( function (x) x != null ).join(" ");
791         this.element.collapsed = (str.length == 0);
792         this.element.value = str;
793     },
794     uninstall : function () {
795         remove_hook.call(window, "select_buffer_hook", this.hook_fun);
796         remove_hook.call(window, "current_buffer_mode_change_hook", this.hook_fun);
797         this.element.parentNode.removeChild(this.element);
798     }
800 define_global_window_mode("minibuffer_mode_indicator", "window_initialize_hook");
801 minibuffer_mode_indicator_mode(true);