1 var define_buffer_local_hook = local_hook_definer("window");
3 function define_current_buffer_hook(hook_name, existing_hook)
5 define_buffer_local_hook(hook_name);
6 add_hook(existing_hook, function (buffer) {
7 if (!buffer.window.buffers || buffer != buffer.window.buffers.current)
9 var hook = conkeror[hook_name];
10 hook.run.apply(hook, Array.prototype.slice.call(arguments));
14 define_buffer_local_hook("buffer_title_change_hook");
15 define_buffer_local_hook("buffer_description_change_hook");
16 define_buffer_local_hook("select_buffer_hook");
17 define_buffer_local_hook("create_buffer_hook");
18 define_buffer_local_hook("kill_buffer_hook");
19 define_buffer_local_hook("buffer_scroll_hook");
20 define_buffer_local_hook("buffer_dom_content_loaded_hook");
21 define_buffer_local_hook("buffer_loaded_hook");
23 define_current_buffer_hook("current_buffer_title_change_hook", "buffer_title_change_hook");
24 define_current_buffer_hook("current_buffer_description_change_hook", "buffer_description_change_hook");
25 define_current_buffer_hook("current_buffer_scroll_hook", "buffer_scroll_hook");
26 define_current_buffer_hook("current_buffer_dom_content_loaded_hook", "buffer_dom_content_loaded_hook");
28 function buffer_configuration(existing_configuration) {
29 if (existing_configuration != null) {
30 this.cwd = existing_configuration.cwd;
33 this.cwd = default_directory.path;
37 define_keywords("$configuration", "$element");
38 function buffer_creator(type) {
39 var args = forward_keywords(arguments);
40 return function (window, element) {
41 return new type(window, element, args);
45 define_variable("allow_browser_window_close", true,
46 "If this is set to true, if a content buffer page calls " +
47 "window.close() from JavaScript and is not prevented by the " +
48 "normal Mozilla mechanism that restricts pages from closing " +
49 "a window that was not opened by a script, the buffer will be " +
50 "killed, deleting the window as well if it is the only buffer.");
52 function buffer(window, element)
54 this.constructor_begin();
55 keywords(arguments, $configuration = null);
57 this.configuration = new buffer_configuration(arguments.$configuration);
60 element = create_XUL(window, "vbox");
61 element.setAttribute("flex", "1");
62 var browser = create_XUL(window, "browser");
63 browser.setAttribute("type", "content");
64 browser.setAttribute("flex", "1");
65 element.appendChild(browser);
66 this.window.buffers.container.appendChild(element);
68 /* Manually set up session history.
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
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;
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.enabled_modes = [];
100 this.local_variables = {};
104 this.browser.addEventListener("scroll", function (event) {
105 buffer_scroll_hook.run(buffer);
106 }, true /* capture */, false /* ignore untrusted events */);
108 this.browser.addEventListener("DOMContentLoaded", function (event) {
109 buffer_dom_content_loaded_hook.run(buffer);
110 }, true /* capture */, false /*ignore untrusted events */);
112 this.browser.addEventListener("load", function (event) {
113 buffer_loaded_hook.run(buffer);
114 }, true /* capture */, false /*ignore untrusted events */);
116 this.browser.addEventListener("DOMWindowClose", function (event) {
117 /* This call to preventDefault is very important; without
118 * it, somehow Mozilla does something bad and as a result
119 * the window loses focus, causing keyboard commands to
121 event.preventDefault();
123 if (allow_browser_window_close)
124 kill_buffer(buffer, true);
127 this.constructor_end();
131 /* Saved focus state */
132 saved_focused_frame : null,
133 saved_focused_element : null,
135 on_switch_away : null,
136 // get title () [must be defined by subclasses]
137 // get name () [must be defined by subclasses]
138 dead : false, /* This is set when the buffer is killed */
140 default_message : "",
143 if (x in this.local_variables)
144 return this.local_variables[x];
148 set_default_message : function (str) {
149 this.default_message = str;
150 if (this == this.window.buffers.current)
151 this.window.minibuffer.set_default_message(str);
154 constructors_running : 0,
156 constructor_begin : function () {
157 this.constructors_running++;
160 constructor_end : function () {
161 if (--this.constructors_running == 0)
162 create_buffer_hook.run(this);
165 /* General accessors */
166 get cwd () { return this.configuration.cwd; },
168 /* Browser accessors */
169 get top_frame () { return this.browser.contentWindow; },
170 get document () { return this.browser.contentDocument; },
171 get web_navigation () { return this.browser.webNavigation; },
172 get doc_shell () { return this.browser.docShell; },
173 get markup_document_viewer () { return this.browser.markupDocumentViewer; },
174 get current_URI () { return this.browser.currentURI; },
176 is_child_element : function (element)
178 return (element && this.is_child_frame(element.ownerDocument.defaultView));
181 is_child_frame : function (frame)
183 return (frame && frame.top == this.top_frame);
186 // This method is like focused_frame, except that if no content
187 // frame actually has focus, this returns null.
188 get focused_frame_or_null () {
189 var frame = this.window.document.commandDispatcher.focusedWindow;
190 var top = this.top_frame;
191 if (this.is_child_frame(frame))
196 get focused_frame () {
197 var frame = this.window.document.commandDispatcher.focusedWindow;
198 var top = this.top_frame;
199 if (this.is_child_frame(frame))
201 return this.top_frame;
204 get focused_element () {
205 var element = this.window.document.commandDispatcher.focusedElement;
206 if (this.is_child_element(element))
211 do_command : function (command)
213 function attempt_command(element, command)
216 if (element.controllers
217 && (controller = element.controllers.getControllerForCommand(command)) != null
218 && controller.isCommandEnabled(command))
220 controller.doCommand(command);
226 var element = this.focused_element;
227 if (element && attempt_command(element, command))
229 var win = this.focused_frame;
231 if (attempt_command(win, command))
233 if (!win.parent || win == win.parent)
239 handle_kill : function () {
243 this.saved_focused_frame = null;
244 this.saved_focused_element = null;
245 kill_buffer_hook.run(this);
249 function check_buffer(obj, type) {
250 if (!(obj instanceof type))
251 throw interactive_error("Buffer has invalid type.");
253 throw interactive_error("Buffer has already been killed.");
257 function buffer_container(window, create_initial_buffer)
259 this.window = window;
260 this.container = window.document.getElementById("buffer-container");
261 this.buffer_list = [];
262 window.buffers = this;
264 create_initial_buffer(window, this.container.firstChild);
267 buffer_container.prototype = {
268 constructor : buffer_container,
271 return this.container.selectedPanel.conkeror_buffer_object;
274 set current (buffer) {
275 var old_value = this.current;
276 if (old_value == buffer)
279 this.buffer_list.splice(this.buffer_list.indexOf(buffer), 1);
280 this.buffer_list.unshift(buffer);
282 this._switch_away_from(this.current);
283 this._switch_to(buffer);
286 select_buffer_hook.run(buffer);
289 _switch_away_from : function (old_value) {
291 old_value.saved_focused_frame = old_value.focused_frame;
292 old_value.saved_focused_element = old_value.focused_element;
294 old_value.browser.setAttribute("type", "content");
297 _switch_to : function (buffer) {
298 // Select new buffer in the XUL deck
299 this.container.selectedPanel = buffer.element;
301 buffer.browser.setAttribute("type", "content-primary");
304 * This next focus call seems to be needed to avoid focus
305 * somehow getting lost (and the keypress handler therefore
306 * not getting called at all) when killing buffers.
310 // Restore focus state
311 if (buffer.saved_focused_element)
312 set_focus_no_scroll(this.window, buffer.saved_focused_element);
313 else if (buffer.saved_focused_frame)
314 set_focus_no_scroll(this.window, buffer.saved_focused_frame);
316 buffer.saved_focused_element = null;
317 buffer.saved_focused_frame = null;
319 this.window.minibuffer.set_default_message(buffer.default_message);
323 return this.container.childNodes.length;
326 get_buffer : function (index) {
327 if (index >= 0 && index < this.count)
328 return this.container.childNodes.item(index).conkeror_buffer_object;
332 get selected_index () {
333 var nodes = this.container.childNodes;
334 var count = nodes.length;
335 for (var i = 0; i < count; ++i)
336 if (nodes.item(i) == this.container.selectedPanel)
341 index_of : function (b) {
342 var nodes = this.container.childNodes;
343 var count = nodes.length;
344 for (var i = 0; i < count; ++i)
345 if (nodes.item(i) == b.element)
350 get unique_name_list () {
351 var existing_names = new string_hashset();
353 this.for_each(function(b) {
354 var base_name = b.name;
355 var name = base_name;
357 while (existing_names.contains(name))
360 name = base_name + "<" + index + ">";
362 existing_names.add(name);
363 bufs.push([name, b]);
368 kill_buffer : function (b) {
371 var count = this.count;
374 var new_buffer = this.buffer_list[0];
376 if (b == new_buffer) {
377 new_buffer = this.buffer_list[1];
380 this._switch_away_from(this.current);
381 this.container.removeChild(b.element);
382 this.buffer_list.splice(this.buffer_list.indexOf(b), 1);
383 this._switch_to(new_buffer);
385 select_buffer_hook.run(new_buffer);
386 this.buffer_list.splice(this.buffer_list.indexOf(new_buffer), 1);
387 this.buffer_list.unshift(new_buffer);
393 for_each : function (f) {
394 var count = this.count;
395 for (var i = 0; i < count; ++i)
396 f(this.get_buffer(i));
400 function buffer_initialize_window_early(window)
403 * Use content_buffer by default to handle an unusual case where
404 * browser.chromeURI is used perhaps. In general this default
405 * should not be needed.
408 var create_initial_buffer
409 = window.args.initial_buffer_creator || buffer_creator(content_buffer);
410 new buffer_container(window, create_initial_buffer);
413 add_hook("window_initialize_early_hook", buffer_initialize_window_early);
416 define_buffer_local_hook("buffer_kill_before_hook", RUN_HOOK_UNTIL_FAILURE);
417 function buffer_before_window_close(window)
419 var bs = window.buffers;
420 var count = bs.count;
421 for (let i = 0; i < count; ++i) {
422 if (!buffer_kill_before_hook.run(bs.get_buffer(i)))
427 add_hook("window_before_close_hook", buffer_before_window_close);
429 function buffer_window_close_handler(window)
431 var bs = window.buffers;
432 var count = bs.count;
433 for (let i = 0; i < count; ++i) {
434 let b = bs.get_buffer(i);
438 add_hook("window_close_hook", buffer_window_close_handler);
440 /* open/follow targets */
441 const OPEN_CURRENT_BUFFER = 0; // only valid for open if the current
442 // buffer is a content_buffer; for
443 // follow, equivalent to
445 const OPEN_NEW_BUFFER = 1;
446 const OPEN_NEW_BUFFER_BACKGROUND = 2;
447 const OPEN_NEW_WINDOW = 3;
449 const FOLLOW_DEFAULT = 4; // for open, implies OPEN_CURRENT_BUFFER
450 const FOLLOW_CURRENT_FRAME = 5; // for open, implies OPEN_CURRENT_BUFFER
451 const FOLLOW_TOP_FRAME = 6; // for open, implies OPEN_CURRENT_BUFFER
453 var TARGET_PROMPTS = [" in current buffer",
455 " in new buffer (background)",
461 var TARGET_NAMES = ["current buffer",
463 "new buffer (background)",
470 function browse_target_prompt(target, prefix) {
473 return prefix + TARGET_PROMPTS[target] + ":";
477 var default_browse_targets = {};
478 default_browse_targets["open"] = [OPEN_CURRENT_BUFFER, OPEN_NEW_BUFFER, OPEN_NEW_WINDOW];
479 default_browse_targets["follow"] = [FOLLOW_DEFAULT, OPEN_NEW_BUFFER, OPEN_NEW_WINDOW];
480 default_browse_targets["follow-top"] = [FOLLOW_TOP_FRAME, FOLLOW_CURRENT_FRAME];
482 interactive_context.prototype.browse_target = function (action) {
483 var prefix = this.prefix_argument;
484 var targets = action;
485 while (typeof(targets) == "string")
486 targets = default_browse_targets[targets];
487 if (prefix == null || typeof(prefix) != "object")
491 while (num >= 4 && index + 1 < targets.length) {
495 return targets[index];
498 function create_buffer(window, creator, target) {
500 case OPEN_NEW_BUFFER:
501 window.buffers.current = creator(window, null);
503 case OPEN_NEW_BUFFER_BACKGROUND:
504 creator(window, null);
506 case OPEN_NEW_WINDOW:
507 make_window(creator);
510 throw new Error("invalid target");
514 var queued_buffer_creators = null;
515 function _process_queued_buffer_creators(window) {
516 for (var i = 0; i < queued_buffer_creators.length; ++i) {
517 var x = queued_buffer_creators[i];
518 create_buffer(window, x[0], x[1]);
520 queued_buffer_creators = null;
522 function create_buffer_in_current_window(creator, target, focus_existing) {
523 if (target == OPEN_NEW_WINDOW)
524 throw new Error("invalid target");
525 var window = get_recent_conkeror_window();
529 create_buffer(window, creator, target);
530 } else if (queued_buffer_creators != null) {
531 queued_buffer_creators.push([creator,target]);
533 queued_buffer_creators = [];
534 window = make_window(creator);
535 add_hook.call(window, "window_initialize_late_hook", _process_queued_buffer_creators);
539 minibuffer_auto_complete_preferences["buffer"] = true;
540 define_keywords("$default");
541 minibuffer.prototype.read_buffer = function () {
542 var window = this.window;
543 var buffer = this.window.buffers.current;
544 keywords(arguments, $prompt = "Buffer:",
546 $history = "buffer");
547 var completer = all_word_completer(
548 $completions = function (visitor) window.buffers.for_each(visitor),
549 $get_string = function (x) x.description,
550 $get_description = function (x) x.title);
551 var result = yield this.read(
552 $prompt = arguments.$prompt,
553 $history = arguments.$history,
554 $completer = completer,
555 $match_required = true,
556 $auto_complete = "buffer",
557 $auto_complete_initial = true,
558 $auto_complete_delay = 0,
559 $default_completion = arguments.$default);
560 yield co_return(result);
563 interactive_context.prototype.__defineGetter__("cwd", function () this.buffer.cwd);
565 function buffer_next (window, count)
567 var index = window.buffers.selected_index;
568 var total = window.buffers.count;
569 index = (index + count) % total;
572 window.buffers.current = window.buffers.get_buffer(index);
574 interactive("buffer-next",
575 "Switch to the next buffer.",
576 function (I) {buffer_next(I.window, I.p);});
577 interactive("buffer-previous",
578 "Switch to the previous buffer.",
579 function (I) {buffer_next(I.window, -I.p);});
581 function switch_to_buffer (window, buffer)
583 if (buffer && !buffer.dead)
584 window.buffers.current = buffer;
586 interactive("switch-to-buffer",
587 "Switch to a buffer specified in the minibuffer.",
591 (yield I.minibuffer.read_buffer(
592 $prompt = "Switch to buffer:",
593 $default = (I.window.buffers.count > 1 ?
594 I.window.buffers.buffer_list[1] :
599 define_variable("can_kill_last_buffer", true,
600 "If this is set to true, kill-buffer can kill the last remaining buffer, and close the window.");
602 function kill_buffer(buffer, force)
606 var buffers = buffer.window.buffers;
607 if (buffers.count == 1 && buffer == buffers.current) {
608 if (can_kill_last_buffer || force) {
609 delete_window(buffer.window);
613 throw interactive_error("Can't kill last buffer.");
615 buffers.kill_buffer(buffer);
617 interactive("kill-buffer",
618 "Kill a buffer specified in the minibuffer.\n" +
619 "If `can_kill_last_buffer' is set to true, an attempt to kill the last remaining " +
620 "buffer in a window will cause the window to be closed.",
621 function (I) {kill_buffer((yield I.minibuffer.read_buffer($prompt = "Kill buffer:")))});
623 interactive("kill-current-buffer",
624 "Kill the current buffer.\n" +
625 "If `can_kill_last_buffer' is set to true, an attempt to kill the last remaining " +
626 "buffer in a window will cause the window to be closed.",
627 function (I) {kill_buffer(I.buffer)});
629 function change_directory(buffer, dir) {
630 buffer.configuration.cwd = dir;
632 interactive("change-current-directory",
633 "Change the current directory of the selected buffer.",
637 (yield I.minibuffer.read_existing_directory_path(
638 $prompt = "New current directory:",
639 $initial_value = I.cwd)));
642 interactive("shell-command", function (I) {
644 var cmd = (yield I.minibuffer.read_shell_command($cwd = cwd));
645 yield shell_command(cmd, $cwd = cwd);
648 function unfocus(buffer)
650 var elem = buffer.focused_element;
655 var win = buffer.focused_frame;
656 if (win != buffer.top_frame)
658 buffer.top_frame.focus();
660 interactive("unfocus", function (I) {unfocus(I.buffer)});
662 require_later("content-buffer.js");
664 var mode_functions = {};
666 var mode_display_names = {};
668 define_buffer_local_hook("buffer_mode_change_hook");
669 define_current_buffer_hook("current_buffer_mode_change_hook", "buffer_mode_change_hook");
671 define_keywords("$class", "$enable", "$disable", "$doc");
672 function define_buffer_mode(name, display_name) {
675 var hyphen_name = name.replace("_","-","g");
676 var mode_class = arguments.$class;
677 var enable = arguments.$enable;
678 var disable = arguments.$disable;
680 mode_display_names[name] = display_name;
684 if (disable == false) {
690 var state = (mode_class != null) ? mode_class : (name + "_enabled");
691 var enable_hook_name = name + "_enable_hook";
692 var disable_hook_name = name + "_disable_hook";
693 define_buffer_local_hook(enable_hook_name);
694 define_buffer_local_hook(disable_hook_name);
696 var change_hook_name = null;
699 mode_functions[name] = {enable: enable,
701 mode_class: mode_class,
702 disable_hook_name: disable_hook_name};
703 change_hook_name = mode_class + "_change_hook";
704 define_buffer_local_hook(change_hook_name);
707 function func(buffer, arg) {
708 var old_state = buffer[state];
709 var cur_state = (old_state == name);
710 var new_state = (arg == null) ? !cur_state : (arg > 0);
711 if ((new_state == cur_state) || (!can_disable && !new_state))
714 if (mode_class && old_state != null) {
715 buffer.enabled_modes.splice(buffer.enabled_modes.indexOf(old_state), 1);
716 let x = mode_functions[old_state];
719 conkeror[x.disable_hook_name].run(buffer);
721 buffer[state] = name;
724 conkeror[enable_hook_name].run(buffer);
725 buffer.enabled_modes.push(name);
727 buffer.enabled_modes.splice(buffer.enabled_modes.indexOf(name), 1);
729 conkeror[disable_hook_name].run(buffer);
730 buffer[state] = null;
732 if (change_hook_name)
733 conkeror[change_hook_name].run(buffer, buffer[state]);
734 buffer_mode_change_hook.run(buffer);
737 conkeror[name] = func;
738 interactive(hyphen_name, arguments.$doc, function (I) {
740 var new_state = func(I.buffer, arg && univ_arg_to_number(arg));
741 I.minibuffer.message(hyphen_name + (new_state ? " enabled" : " disabled"));
744 ignore_function_for_get_caller_source_code_reference("define_buffer_mode");
747 function minibuffer_mode_indicator(window) {
748 this.window = window;
749 var element = create_XUL(window, "label");
750 element.setAttribute("id", "minibuffer-mode-indicator");
751 element.collapsed = true;
752 element.setAttribute("class", "minibuffer");
753 window.document.getElementById("minibuffer").appendChild(element);
754 this.element = element;
755 this.hook_func = method_caller(this, this.update);
756 add_hook.call(window, "select_buffer_hook", this.hook_func);
757 add_hook.call(window, "current_buffer_mode_change_hook", this.hook_func);
760 minibuffer_mode_indicator.prototype = {
761 update : function () {
762 var buf = this.window.buffers.current;
763 var modes = buf.enabled_modes;
764 var str = modes.map( function (x) {
765 let y = mode_display_names[x];
767 return "[" + y + "]";
770 } ).filter( function (x) x != null ).join(" ");
771 this.element.collapsed = (str.length == 0);
772 this.element.value = str;
774 uninstall : function () {
775 remove_hook.call(window, "select_buffer_hook", this.hook_fun);
776 remove_hook.call(window, "current_buffer_mode_change_hook", this.hook_fun);
777 this.element.parentNode.removeChild(this.element);
780 define_global_window_mode("minibuffer_mode_indicator", "window_initialize_hook");
781 minibuffer_mode_indicator_mode(true);