Expand the .deb nightly build script to allow binary-only builds, too
[conkeror/arlinius.git] / modules / buffer.js
blob51be63fe1e5559e505bfab48ed4426b0150326d9
1 /**
2  * (C) Copyright 2004-2007 Shawn Betts
3  * (C) Copyright 2007-2009 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) {
13     define_buffer_local_hook(hook_name);
14     add_hook(existing_hook, function (buffer) {
15             if (!buffer.window.buffers || buffer != buffer.window.buffers.current)
16                 return;
17             var hook = conkeror[hook_name];
18             hook.run.apply(hook, Array.prototype.slice.call(arguments));
19         });
22 define_buffer_local_hook("buffer_title_change_hook");
23 define_buffer_local_hook("buffer_description_change_hook");
24 define_buffer_local_hook("select_buffer_hook");
25 define_buffer_local_hook("create_buffer_early_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");
38 define_keywords("$element", "$opener");
39 function buffer_creator (type) {
40     var args = forward_keywords(arguments);
41     return function (window, element) {
42         return new type(window, element, args);
43     };
46 define_variable("allow_browser_window_close", true,
47     "If this is set to true, if a content buffer page calls " +
48     "window.close() from JavaScript and is not prevented by the " +
49     "normal Mozilla mechanism that restricts pages from closing " +
50     "a window that was not opened by a script, the buffer will be " +
51     "killed, deleting the window as well if it is the only buffer.");
53 function buffer (window, element) {
54     this.constructor_begin();
55     keywords(arguments);
56     this.opener = arguments.$opener;
57     this.window = window;
58     if (element == null) {
59         element = create_XUL(window, "vbox");
60         element.setAttribute("flex", "1");
61         var browser = create_XUL(window, "browser");
62         browser.setAttribute("type", "content");
63         browser.setAttribute("flex", "1");
64         browser.setAttribute("autocompletepopup", "popup_autocomplete");
65         element.appendChild(browser);
66         this.window.buffers.container.appendChild(element);
67     } else {
68         /* Manually set up session history.
69          *
70          * This is needed because when constructor for the XBL binding
71          * (mozilla/toolkit/content/widgets/browser.xml#browser) for
72          * the initial browser element of the window is called, the
73          * docShell is not yet initialized and setting up the session
74          * history will fail.  To work around this problem, we do as
75          * tabbrowser.xml (Firefox) does and set the initial browser
76          * to have the disablehistory=true attribute, and then repeat
77          * the work that would normally be done in the XBL
78          * constructor.
79          */
81         // This code is taken from mozilla/browser/base/content/browser.js
82         let browser = element.firstChild;
83         browser.webNavigation.sessionHistory =
84             Cc["@mozilla.org/browser/shistory;1"].createInstance(Ci.nsISHistory);
85         observer_service.addObserver(browser, "browser:purge-session-history", false);
87         // remove the disablehistory attribute so the browser cleans up, as
88         // though it had done this work itself
89         browser.removeAttribute("disablehistory");
91         // enable global history
92         browser.docShell.QueryInterface(Ci.nsIDocShellHistory).useGlobalHistory = true;
93     }
94     this.window.buffers.buffer_list.push(this);
95     this.element = element;
96     this.browser = element.firstChild;
97     this.element.conkeror_buffer_object = this;
99     this.local = { __proto__ : conkeror };
100     this.page = null;
101     this.enabled_modes = [];
102     this.default_browser_object_classes = {};
104     var buffer = this;
106     this.browser.addEventListener("scroll", function (event) {
107             buffer_scroll_hook.run(buffer);
108         }, true /* capture */);
110     this.browser.addEventListener("DOMContentLoaded", function (event) {
111             buffer_dom_content_loaded_hook.run(buffer);
112         }, true /* capture */);
114     this.browser.addEventListener("load", function (event) {
115             buffer_loaded_hook.run(buffer);
116         }, true /* capture */);
118     this.browser.addEventListener("DOMWindowClose", function (event) {
119             /* This call to preventDefault is very important; without
120              * it, somehow Mozilla does something bad and as a result
121              * the window loses focus, causing keyboard commands to
122              * stop working. */
123             event.preventDefault();
125             if (allow_browser_window_close)
126                 kill_buffer(buffer, true);
127         }, true);
129     this.browser.addEventListener("focus", function (event) {
130         if (buffer.focusblocker &&
131             event.target instanceof Ci.nsIDOMHTMLElement &&
132             buffer.focusblocker(buffer, event))
133         {
134             event.target.blur();
135         } else
136             buffer.set_input_mode();
137     }, true);
138     this.modalities = [];
140     this.override_keymaps = [];
142     // When create_buffer_hook_early runs, basic buffer properties
143     // will be available, but not the properties subclasses.
144     create_buffer_early_hook.run(this);
146     this.constructor_end();
149 buffer.prototype = {
150     /* Saved focus state */
151     saved_focused_frame: null,
152     saved_focused_element: null,
153     on_switch_to: null,
154     on_switch_away: null,
155     // get title ()   [must be defined by subclasses]
156     // get name ()    [must be defined by subclasses]
157     dead: false, /* This is set when the buffer is killed */
159     keymaps: null,
160     mark_active: false,
162     // The property focusblocker is available for an external module to
163     // put a function on which takes a buffer as its argument and returns
164     // true to block a focus event, or false to let normal processing
165     // occur.  Having this one property explicitly handled by the buffer
166     // class allows for otherwise modular focus-blockers.
167     focusblocker: null,
169     default_message: "",
171     set_default_message: function (str) {
172         this.default_message = str;
173         if (this == this.window.buffers.current)
174             this.window.minibuffer.set_default_message(str);
175     },
177     constructors_running: 0,
179     constructor_begin : function () {
180         this.constructors_running++;
181     },
183     constructor_end: function () {
184         if (--this.constructors_running == 0) {
185             create_buffer_hook.run(this);
186             this.set_input_mode();
187             delete this.opener;
188         }
189     },
191     destroy: function () {},
193     set_input_mode: function () {
194         if (this.input_mode)
195             conkeror[this.input_mode](this, false);
196         this.keymaps = [];
197         this.modalities.map(function (m) m(this), this);
198     },
200     /* Browser accessors */
201     get top_frame () { return this.browser.contentWindow; },
202     get document () { return this.browser.contentDocument; },
203     get web_navigation () { return this.browser.webNavigation; },
204     get doc_shell () { return this.browser.docShell; },
205     get markup_document_viewer () { return this.browser.markupDocumentViewer; },
206     get current_uri () { return this.browser.currentURI; },
208     is_child_element: function (element) {
209         return (element && this.is_child_frame(element.ownerDocument.defaultView));
210     },
212     is_child_frame: function (frame) {
213         return (frame && frame.top == this.top_frame);
214     },
216     // This method is like focused_frame, except that if no content
217     // frame actually has focus, this returns null.
218     get focused_frame_or_null () {
219         var frame = this.window.document.commandDispatcher.focusedWindow;
220         var top = this.top_frame;
221         if (this.is_child_frame(frame))
222             return frame;
223         return null;
224     },
226     get focused_frame () {
227         var frame = this.window.document.commandDispatcher.focusedWindow;
228         var top = this.top_frame;
229         if (this.is_child_frame(frame))
230             return frame;
231         return this.top_frame;
232     },
234     get focused_element () {
235         var element = this.window.document.commandDispatcher.focusedElement;
236         if (this.is_child_element(element))
237             return element;
238         return null;
239     },
241     do_command: function (command) {
242         function attempt_command (element, command) {
243             var controller;
244             if (element.controllers
245                 && (controller = element.controllers.getControllerForCommand(command)) != null
246                 && controller.isCommandEnabled(command))
247             {
248                 controller.doCommand(command);
249                 return true;
250             }
251             return false;
252         }
254         var element = this.focused_element;
255         if (element && attempt_command(element, command))
256             return;
257         var win = this.focused_frame;
258         do  {
259             if (attempt_command(win, command))
260                 return;
261             if (!win.parent || win == win.parent)
262                 break;
263             win = win.parent;
264         } while (true);
265     },
267     handle_kill: function () {
268         this.dead = true;
269         this.browser = null;
270         this.element = null;
271         this.saved_focused_frame = null;
272         this.saved_focused_element = null;
273         kill_buffer_hook.run(this);
274     }
277 function with_current_buffer (buffer, callback) {
278     return callback(new interactive_context(buffer));
281 function check_buffer (obj, type) {
282     if (!(obj instanceof type))
283         throw interactive_error("Buffer has invalid type.");
284     if (obj.dead)
285         throw interactive_error("Buffer has already been killed.");
286     return obj;
289 function buffer_container (window, create_initial_buffer) {
290     this.window = window;
291     this.container = window.document.getElementById("buffer-container");
292     this.buffer_list = [];
293     window.buffers = this;
294     create_initial_buffer(window, this.container.firstChild);
297 buffer_container.prototype = {
298     constructor: buffer_container,
300     get current () {
301         return this.container.selectedPanel.conkeror_buffer_object;
302     },
304     set current (buffer) {
305         var old_value = this.current;
306         if (old_value == buffer)
307             return;
309         this.buffer_list.splice(this.buffer_list.indexOf(buffer), 1);
310         this.buffer_list.unshift(buffer);
312         this._switch_away_from(this.current);
313         this._switch_to(buffer);
315         // Run hooks
316         select_buffer_hook.run(buffer);
317     },
319     _switch_away_from: function (old_value) {
320         // Save focus state
321         old_value.saved_focused_frame = old_value.focused_frame;
322         old_value.saved_focused_element = old_value.focused_element;
324         old_value.browser.setAttribute("type", "content");
325     },
327     _switch_to: function (buffer) {
328         // Select new buffer in the XUL deck
329         this.container.selectedPanel = buffer.element;
331         buffer.browser.setAttribute("type", "content-primary");
333         /**
334          * This next focus call seems to be needed to avoid focus
335          * somehow getting lost (and the keypress handler therefore
336          * not getting called at all) when killing buffers.
337          */
338         this.window.focus();
340         // Restore focus state
341         if (buffer.saved_focused_element)
342             set_focus_no_scroll(this.window, buffer.saved_focused_element);
343         else if (buffer.saved_focused_frame)
344             set_focus_no_scroll(this.window, buffer.saved_focused_frame);
346         buffer.saved_focused_element = null;
347         buffer.saved_focused_frame = null;
349         this.window.minibuffer.set_default_message(buffer.default_message);
350     },
352     get count () {
353         return this.container.childNodes.length;
354     },
356     get_buffer: function (index) {
357         if (index >= 0 && index < this.count)
358             return this.container.childNodes.item(index).conkeror_buffer_object;
359         return null;
360     },
362     get selected_index () {
363         var nodes = this.container.childNodes;
364         var count = nodes.length;
365         for (var i = 0; i < count; ++i)
366             if (nodes.item(i) == this.container.selectedPanel)
367                 return i;
368         return null;
369     },
371     index_of: function (b) {
372         var nodes = this.container.childNodes;
373         var count = nodes.length;
374         for (var i = 0; i < count; ++i)
375             if (nodes.item(i) == b.element)
376                 return i;
377         return null;
378     },
380     get unique_name_list () {
381         var existing_names = new string_hashset();
382         var bufs = [];
383         this.for_each(function(b) {
384                 var base_name = b.name;
385                 var name = base_name;
386                 var index = 1;
387                 while (existing_names.contains(name))
388                 {
389                     ++index;
390                     name = base_name + "<" + index + ">";
391                 }
392                 existing_names.add(name);
393                 bufs.push([name, b]);
394             });
395         return bufs;
396     },
398     kill_buffer: function (b) {
399         if (b.dead)
400             return true;
401         var count = this.count;
402         if (count <= 1)
403             return false;
404         var new_buffer = this.buffer_list[0];
405         var changed = false;
406         if (b == new_buffer) {
407             new_buffer = this.buffer_list[1];
408             changed = true;
409         }
410         this._switch_away_from(this.current);
411         // The removeChild call below may trigger events in progress
412         // listeners.  This call to `destroy' gives buffer subclasses a
413         // chance to remove such listeners, so that they cannot try to
414         // perform UI actions based upon a xul:browser that no longer
415         // exists.
416         b.destroy();
417         this.container.removeChild(b.element);
418         this.buffer_list.splice(this.buffer_list.indexOf(b), 1);
419         this._switch_to(new_buffer);
420         if (changed) {
421             select_buffer_hook.run(new_buffer);
422             this.buffer_list.splice(this.buffer_list.indexOf(new_buffer), 1);
423             this.buffer_list.unshift(new_buffer);
424         }
425         b.handle_kill();
426         return true;
427     },
429     bury_buffer: function (b) {
430         var new_buffer = this.buffer_list[0];
431         if (b == new_buffer)
432             new_buffer = this.buffer_list[1];
433         this.buffer_list.splice(this.buffer_list.indexOf(b), 1);
434         this.buffer_list.push(b);
435         this.current = new_buffer;
436         return true;
437     },
439     for_each: function (f) {
440         var count = this.count;
441         for (var i = 0; i < count; ++i)
442             f(this.get_buffer(i));
443     }
446 function buffer_initialize_window_early (window) {
447     /**
448      * Use content_buffer by default to handle an unusual case where
449      * browser.chromeURI is used perhaps.  In general this default
450      * should not be needed.
451      */
452     var create_initial_buffer =
453         window.args.initial_buffer_creator || buffer_creator(content_buffer);
454     new buffer_container(window, create_initial_buffer);
457 add_hook("window_initialize_early_hook", buffer_initialize_window_early);
460 define_buffer_local_hook("buffer_kill_before_hook", RUN_HOOK_UNTIL_FAILURE);
461 function buffer_before_window_close (window) {
462     var bs = window.buffers;
463     var count = bs.count;
464     for (let i = 0; i < count; ++i) {
465         if (!buffer_kill_before_hook.run(bs.get_buffer(i)))
466             return false;
467     }
468     return true;
470 add_hook("window_before_close_hook", buffer_before_window_close);
472 function buffer_window_close_handler (window) {
473     var bs = window.buffers;
474     var count = bs.count;
475     for (let i = 0; i < count; ++i) {
476         let b = bs.get_buffer(i);
477         b.handle_kill();
478     }
480 add_hook("window_close_hook", buffer_window_close_handler);
482 /* open/follow targets */
483 const OPEN_CURRENT_BUFFER = 0; // only valid for open if the current
484                                // buffer is a content_buffer.
485 const OPEN_NEW_BUFFER = 1;
486 const OPEN_NEW_BUFFER_BACKGROUND = 2;
487 const OPEN_NEW_WINDOW = 3;
489 const FOLLOW_DEFAULT = 4; // for open, implies OPEN_CURRENT_BUFFER
490 const FOLLOW_CURRENT_FRAME = 5; // for open, implies OPEN_CURRENT_BUFFER
492 var TARGET_PROMPTS = [" in current buffer",
493                       " in new buffer",
494                       " in new buffer (background)",
495                       " in new window",
496                       "",
497                       " in current frame"];
499 var TARGET_NAMES = ["current buffer",
500                     "new buffer",
501                     "new buffer (background)",
502                     "new window",
503                     "default",
504                     "current frame"];
507 function create_buffer (window, creator, target) {
508     switch (target) {
509     case OPEN_NEW_BUFFER:
510         window.buffers.current = creator(window, null);
511         break;
512     case OPEN_NEW_BUFFER_BACKGROUND:
513         creator(window, null);
514         break;
515     case OPEN_NEW_WINDOW:
516         make_window(creator);
517         break;
518     default:
519         throw new Error("invalid target");
520     }
523 let (queued_buffer_creators = null) {
524     function create_buffer_in_current_window (creator, target, focus_existing) {
525         function process_queued_buffer_creators (window) {
526             for (var i = 0; i < queued_buffer_creators.length; ++i) {
527                 var x = queued_buffer_creators[i];
528                 create_buffer(window, x[0], x[1]);
529             }
530             queued_buffer_creators = null;
531         }
533         if (target == OPEN_NEW_WINDOW)
534             throw new Error("invalid target");
535         var window = get_recent_conkeror_window();
536         if (window) {
537             if (focus_existing)
538                 window.focus();
539             create_buffer(window, creator, target);
540         } else if (queued_buffer_creators != null) {
541             queued_buffer_creators.push([creator,target]);
542         } else {
543             queued_buffer_creators = [];
544             window = make_window(creator);
545             add_hook.call(window, "window_initialize_late_hook", process_queued_buffer_creators);
546         }
547     }
552  * Read Buffer
553  */
554 minibuffer_auto_complete_preferences["buffer"] = true;
555 define_keywords("$default");
556 minibuffer.prototype.read_buffer = function () {
557     var window = this.window;
558     var buffer = this.window.buffers.current;
559     keywords(arguments, $prompt = "Buffer:",
560              $default = buffer,
561              $history = "buffer");
562     var completer = all_word_completer(
563         $completions = function (visitor) window.buffers.for_each(visitor),
564         $get_string = function (x) x.description,
565         $get_description = function (x) x.title);
566     var result = yield this.read(
567         $keymap = read_buffer_keymap,
568         $prompt = arguments.$prompt,
569         $history = arguments.$history,
570         $completer = completer,
571         $match_required = true,
572         $auto_complete = "buffer",
573         $auto_complete_initial = true,
574         $auto_complete_delay = 0,
575         $default_completion = arguments.$default);
576     yield co_return(result);
580 interactive("buffer-reset-input-mode",
581     "Force a reset of the input mode.  Used by quote-next.",
582     function (I) {
583         I.buffer.set_input_mode();
584     });
587 function buffer_next (window, count) {
588     var index = window.buffers.selected_index;
589     var total = window.buffers.count;
590     if (total == 1)
591         throw new interactive_error("No other buffer");
592     index = (index + count) % total;
593     if (index < 0)
594         index += total;
595     window.buffers.current = window.buffers.get_buffer(index);
597 interactive("buffer-next",
598     "Switch to the next buffer.",
599     function (I) { buffer_next(I.window, I.p); });
600 interactive("buffer-previous",
601     "Switch to the previous buffer.",
602     function (I) { buffer_next(I.window, -I.p); });
604 function switch_to_buffer (window, buffer) {
605     if (buffer && !buffer.dead)
606         window.buffers.current = buffer;
608 interactive("switch-to-buffer",
609     "Switch to a buffer specified in the minibuffer.",
610     function (I) {
611         switch_to_buffer(
612             I.window,
613             (yield I.minibuffer.read_buffer(
614                 $prompt = "Switch to buffer:",
615                 $default = (I.window.buffers.count > 1 ?
616                             I.window.buffers.buffer_list[1] :
617                             I.buffer))));
618     });
620 define_variable("can_kill_last_buffer", true,
621     "If this is set to true, kill-buffer can kill the last "+
622     "remaining buffer, and close the window.");
624 function kill_other_buffers (buffer) {
625     if (!buffer)
626         return;
627     var bs = buffer.window.buffers;
628     var b;
630     while ((b = bs.get_buffer(0)) != buffer)
631         bs.kill_buffer(b);
632     var count = bs.count;
633     while (--count)
634         bs.kill_buffer(bs.get_buffer(1));
636 interactive("kill-other-buffers",
637     "Kill all buffers except current one.\n",
638     function (I) { kill_other_buffers(I.buffer); });
641 function kill_buffer (buffer, force) {
642     if (!buffer)
643         return;
644     var buffers = buffer.window.buffers;
645     if (buffers.count == 1 && buffer == buffers.current) {
646         if (can_kill_last_buffer || force) {
647             delete_window(buffer.window);
648             return;
649         } else
650             throw interactive_error("Can't kill last buffer.");
651     }
652     buffers.kill_buffer(buffer);
654 interactive("kill-buffer",
655     "Kill a buffer specified in the minibuffer.\n" +
656     "If `can_kill_last_buffer' is set to true, an attempt to kill the "+
657     "last remaining buffer in a window will cause the window to be closed.",
658     function (I) {
659         kill_buffer((yield I.minibuffer.read_buffer($prompt = "Kill buffer:")));
660     });
662 interactive("kill-current-buffer",
663     "Kill the current buffer.\n" +
664     "If `can_kill_last_buffer' is set to true, an attempt to kill the "+
665     "last remaining buffer in a window will cause the window to be closed.",
666     function (I) { kill_buffer(I.buffer); });
668 interactive("read-buffer-kill-buffer",
669     "Kill the current selected buffer in the completions list "+
670     "in a read buffer minibuffer interaction.",
671     function (I) {
672         var s = I.window.minibuffer.current_state;
673         var i = s.selected_completion_index;
674         var c = s.completions;
675         if (i == -1)
676             return;
677         kill_buffer(c.get_value(i));
678         s.completer.refresh();
679         s.handle_input(I.window.minibuffer);
680     });
682 interactive("bury-buffer",
683     "Bury the current buffer.\n Put the current buffer at the end of " +
684     "the buffer list, so that it is the least likely buffer to be " +
685     "selected by `switch-to-buffer'.",
686     function (I) { I.window.buffers.bury_buffer(I.buffer); });
688 function change_directory (buffer, dir) {
689     if (buffer.page != null)
690         delete buffer.page.local.cwd;
691     buffer.local.cwd = make_file(dir);
693 interactive("change-directory",
694     "Change the current directory of the selected buffer.",
695     function (I) {
696         change_directory(
697             I.buffer,
698             (yield I.minibuffer.read_existing_directory_path(
699                 $prompt = "Change to directory:",
700                 $initial_value = make_file(I.local.cwd).path)));
701     });
703 interactive("shell-command", null,
704     function (I) {
705         var cwd = I.local.cwd;
706         var cmd = (yield I.minibuffer.read_shell_command($cwd = cwd));
707         yield shell_command(cmd, $cwd = cwd);
708     });
712  * unfocus is a high-level command for unfocusing hyperlinks, inputs,
713  * frames, iframes, plugins, and also clearing the selection.
714  */
715 define_buffer_local_hook("unfocus_hook");
716 function unfocus (window, buffer) {
717     // 1. if there is a selection, clear it.
718     var selc = getFocusedSelCtrl(buffer);
719     if (selc && selc.getSelection(selc.SELECTION_NORMAL).isCollapsed == false) {
720         clear_selection(buffer);
721         window.minibuffer.message("cleared selection");
722         return;
723     }
724     // 2. if there is a focused element, unfocus it.
725     if (buffer.focused_element) {
726         buffer.focused_element.blur();
727         // if an element in a detached fragment has focus, blur() will
728         // not work, and we need to take more drastic measures.  the
729         // action taken was found through experiment, so it is possibly
730         // not the most concise way to unfocus such an element.
731         if (buffer.focused_element) {
732             buffer.element.focus();
733             buffer.top_frame.focus();
734         }
735         window.minibuffer.message("unfocused element");
736         return;
737     }
738     // 3. return focus to top-frame from subframes and plugins.
739     buffer.top_frame.focus();
740     buffer.top_frame.focus(); // needed to get focus back from plugins
741     window.minibuffer.message("refocused top frame");
742     // give page-modes an opportunity to set focus specially
743     unfocus_hook.run(buffer);
745 interactive("unfocus",
746     "Unfocus is a high-level command for unfocusing hyperlinks, inputs, "+
747     "frames, iframes, plugins, and also for clearing the selection.  "+
748     "The action that it takes is based on precedence.  If there is a "+
749     "focused hyperlink or input, it will unfocus that.  Otherwise, if "+
750     "there is a selection, it will clear the selection.  Otherwise, it "+
751     "will return focus to the top frame from a focused frame, iframe, "+
752     "or plugin.  In the case of plugins, since they steal keyboard "+
753     "control away from Conkeror, the normal way to unfocus them is "+
754     "to use command-line remoting externally: conkeror -batch -f "+
755     "unfocus.  Page-modes also have an opportunity to alter the default"+
756     "focus via the hook, `focus_hook'.",
757     function (I) {
758         unfocus(I.window, I.buffer);
759     });
762 function for_each_buffer (f) {
763     for_each_window(function (w) { w.buffers.for_each(f); });
768  * BUFFER MODES
769  */
771 var mode_functions = {};
772 var mode_display_names = {};
774 define_buffer_local_hook("buffer_mode_change_hook");
775 define_current_buffer_hook("current_buffer_mode_change_hook", "buffer_mode_change_hook");
777 define_keywords("$display_name", "$class", "$enable", "$disable", "$doc");
778 function define_buffer_mode (name) {
779     keywords(arguments);
781     var hyphen_name = name.replace("_","-","g");
782     var display_name = arguments.$display_name;
783     var mode_class = arguments.$class;
784     var enable = arguments.$enable;
785     var disable = arguments.$disable;
787     mode_display_names[name] = display_name;
789     var can_disable;
791     if (disable == false) {
792         can_disable = false;
793         disable = null;
794     } else
795         can_disable = true;
797     var state = (mode_class != null) ? mode_class : (name + "_enabled");
798     var enable_hook_name = name + "_enable_hook";
799     var disable_hook_name = name + "_disable_hook";
800     define_buffer_local_hook(enable_hook_name);
801     define_buffer_local_hook(disable_hook_name);
803     var change_hook_name = null;
805     if (mode_class) {
806         mode_functions[name] = { enable: enable,
807                                  disable: disable,
808                                  mode_class: mode_class,
809                                  disable_hook_name: disable_hook_name };
810         change_hook_name = mode_class + "_change_hook";
811         define_buffer_local_hook(change_hook_name);
812     }
814     function func (buffer, arg) {
815         var old_state = buffer[state];
816         var cur_state = (old_state == name);
817         var new_state = (arg == null) ? !cur_state : (arg > 0);
818         if ((new_state == cur_state) || (!can_disable && !new_state))
819             // perhaps show a message if (!can_disable && !new_state)
820             // to tell the user that this mode cannot be disabled.  do
821             // we have any existing modes that would benefit by it?
822             return null;
823         if (new_state) {
824             if (mode_class && old_state != null)  {
825                 // Another buffer-mode of our same mode-class is
826                 // enabled.  Buffer-modes within a mode-class are
827                 // mutually exclusive, so turn the old one off.
828                 buffer.enabled_modes.splice(buffer.enabled_modes.indexOf(old_state), 1);
829                 let x = mode_functions[old_state];
830                 let y = x.disable;
831                 if (y) y(buffer);
832                 conkeror[x.disable_hook_name].run(buffer);
833             }
834             buffer[state] = name;
835             if (enable)
836                 enable(buffer);
837             conkeror[enable_hook_name].run(buffer);
838             buffer.enabled_modes.push(name);
839         } else {
840             buffer.enabled_modes.splice(buffer.enabled_modes.indexOf(name), 1);
841             disable(buffer);
842             conkeror[disable_hook_name].run(buffer);
843             buffer[state] = null;
844         }
845         if (change_hook_name)
846             conkeror[change_hook_name].run(buffer, buffer[state]);
847         buffer_mode_change_hook.run(buffer);
848         return new_state;
849     }
851     conkeror[name] = func;
852     interactive(hyphen_name, arguments.$doc, function (I) {
853         var arg = I.P;
854         var new_state = func(I.buffer, arg && univ_arg_to_number(arg));
855         I.minibuffer.message(hyphen_name + (new_state ? " enabled" : " disabled"));
856     });
858 ignore_function_for_get_caller_source_code_reference("define_buffer_mode");
861 function minibuffer_mode_indicator (window) {
862     this.window = window;
863     var element = create_XUL(window, "label");
864     element.setAttribute("id", "minibuffer-mode-indicator");
865     element.collapsed = true;
866     element.setAttribute("class", "minibuffer");
867     window.document.getElementById("minibuffer").appendChild(element);
868     this.element = element;
869     this.hook_func = method_caller(this, this.update);
870     add_hook.call(window, "select_buffer_hook", this.hook_func);
871     add_hook.call(window, "current_buffer_mode_change_hook", this.hook_func);
872     this.update();
874 minibuffer_mode_indicator.prototype = {
875     update: function () {
876         var buf = this.window.buffers.current;
877         var modes = buf.enabled_modes;
878         var str = modes.map(
879             function (x) {
880                 let y = mode_display_names[x];
881                 if (y)
882                     return "[" + y + "]";
883                 else
884                     return null;
885             }).filter(function (x) x != null).join(" ");
886         this.element.collapsed = (str.length == 0);
887         this.element.value = str;
888     },
889     uninstall: function () {
890         remove_hook.call(window, "select_buffer_hook", this.hook_fun);
891         remove_hook.call(window, "current_buffer_mode_change_hook", this.hook_fun);
892         this.element.parentNode.removeChild(this.element);
893     }
895 define_global_window_mode("minibuffer_mode_indicator", "window_initialize_hook");
896 minibuffer_mode_indicator_mode(true);
901  * INPUT MODES
902  */
903 define_current_buffer_hook("current_buffer_input_mode_change_hook", "input_mode_change_hook");
904 define_keywords("$display_name", "$doc");
905 function define_input_mode (base_name, keymap_name) {
906     keywords(arguments);
907     var name = base_name + "_input_mode";
908     define_buffer_mode(name,
909                        $class = "input_mode",
910                        $enable = function (buffer) {
911                            check_buffer(buffer, content_buffer);
912                            buffer.keymaps.push(conkeror[keymap_name]);
913                        },
914                        $disable = function (buffer) {
915                            var i = buffer.keymaps.indexOf(conkeror[keymap_name]);
916                            if (i > -1)
917                                buffer.keymaps.splice(i, 1);
918                        },
919                        forward_keywords(arguments));
921 ignore_function_for_get_caller_source_code_reference("define_input_mode");
924 function minibuffer_input_mode_indicator (window) {
925     this.window = window;
926     this.hook_func = method_caller(this, this.update);
927     add_hook.call(window, "select_buffer_hook", this.hook_func);
928     add_hook.call(window, "current_buffer_input_mode_change_hook", this.hook_func);
929     this.update();
932 minibuffer_input_mode_indicator.prototype = {
933     update: function () {
934         var buf = this.window.buffers.current;
935         var mode = buf.input_mode;
936         var classname = mode ? ("minibuffer-" + buf.input_mode.replace("_","-","g")) : "";
937         this.window.minibuffer.element.className = classname;
938     },
939     uninstall: function () {
940         remove_hook.call(window, "select_buffer_hook", this.hook_func);
941         remove_hook.call(window, "current_buffer_input_mode_change_hook", this.hook_func);
942     }
945 define_global_window_mode("minibuffer_input_mode_indicator", "window_initialize_hook");
946 minibuffer_input_mode_indicator_mode(true);