2 * (C) Copyright 2004-2007 Shawn Betts
3 * (C) Copyright 2007-2008 John J. Foerch
4 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
6 * Use, modification, and distribution are subject to the terms specified in the
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)
18 var hook = conkeror[hook_name];
19 hook.run.apply(hook, Array.prototype.slice.call(arguments));
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;
42 this.cwd = default_directory.path;
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);
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);
66 this.configuration = new buffer_configuration(arguments.$configuration);
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);
78 /* Manually set up session history.
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
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;
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 = {};
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
131 event.preventDefault();
133 if (allow_browser_window_close)
134 kill_buffer(buffer, true);
137 this.constructor_end();
141 /* Saved focus state */
142 saved_focused_frame : null,
143 saved_focused_element : 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 : "",
153 if (x in this.local_variables)
154 return this.local_variables[x];
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);
164 constructors_running : 0,
166 constructor_begin : function () {
167 this.constructors_running++;
170 constructor_end : function () {
171 if (--this.constructors_running == 0)
172 create_buffer_hook.run(this);
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)
188 return (element && this.is_child_frame(element.ownerDocument.defaultView));
191 is_child_frame : function (frame)
193 return (frame && frame.top == this.top_frame);
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))
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))
211 return this.top_frame;
214 get focused_element () {
215 var element = this.window.document.commandDispatcher.focusedElement;
216 if (this.is_child_element(element))
221 do_command : function (command)
223 function attempt_command(element, command)
226 if (element.controllers
227 && (controller = element.controllers.getControllerForCommand(command)) != null
228 && controller.isCommandEnabled(command))
230 controller.doCommand(command);
236 var element = this.focused_element;
237 if (element && attempt_command(element, command))
239 var win = this.focused_frame;
241 if (attempt_command(win, command))
243 if (!win.parent || win == win.parent)
249 handle_kill : function () {
253 this.saved_focused_frame = null;
254 this.saved_focused_element = null;
255 kill_buffer_hook.run(this);
259 function check_buffer(obj, type) {
260 if (!(obj instanceof type))
261 throw interactive_error("Buffer has invalid type.");
263 throw interactive_error("Buffer has already been killed.");
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,
281 return this.container.selectedPanel.conkeror_buffer_object;
284 set current (buffer) {
285 var old_value = this.current;
286 if (old_value == buffer)
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);
296 select_buffer_hook.run(buffer);
299 _switch_away_from : function (old_value) {
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");
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");
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.
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);
333 return this.container.childNodes.length;
336 get_buffer : function (index) {
337 if (index >= 0 && index < this.count)
338 return this.container.childNodes.item(index).conkeror_buffer_object;
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)
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)
360 get unique_name_list () {
361 var existing_names = new string_hashset();
363 this.for_each(function(b) {
364 var base_name = b.name;
365 var name = base_name;
367 while (existing_names.contains(name))
370 name = base_name + "<" + index + ">";
372 existing_names.add(name);
373 bufs.push([name, b]);
378 kill_buffer : function (b) {
381 var count = this.count;
384 var new_buffer = this.buffer_list[0];
386 if (b == new_buffer) {
387 new_buffer = this.buffer_list[1];
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);
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);
403 for_each : function (f) {
404 var count = this.count;
405 for (var i = 0; i < count; ++i)
406 f(this.get_buffer(i));
410 function buffer_initialize_window_early(window)
413 * Use content_buffer by default to handle an unusual case where
414 * browser.chromeURI is used perhaps. In general this default
415 * should not be needed.
418 var create_initial_buffer
419 = window.args.initial_buffer_creator || buffer_creator(content_buffer);
420 new buffer_container(window, create_initial_buffer);
423 add_hook("window_initialize_early_hook", buffer_initialize_window_early);
426 define_buffer_local_hook("buffer_kill_before_hook", RUN_HOOK_UNTIL_FAILURE);
427 function buffer_before_window_close(window)
429 var bs = window.buffers;
430 var count = bs.count;
431 for (let i = 0; i < count; ++i) {
432 if (!buffer_kill_before_hook.run(bs.get_buffer(i)))
437 add_hook("window_before_close_hook", buffer_before_window_close);
439 function buffer_window_close_handler(window)
441 var bs = window.buffers;
442 var count = bs.count;
443 for (let i = 0; i < count; ++i) {
444 let b = bs.get_buffer(i);
448 add_hook("window_close_hook", buffer_window_close_handler);
450 /* open/follow targets */
451 const OPEN_CURRENT_BUFFER = 0; // only valid for open if the current
452 // buffer is a content_buffer; for
453 // follow, equivalent to
455 const OPEN_NEW_BUFFER = 1;
456 const OPEN_NEW_BUFFER_BACKGROUND = 2;
457 const OPEN_NEW_WINDOW = 3;
459 const FOLLOW_DEFAULT = 4; // for open, implies OPEN_CURRENT_BUFFER
460 const FOLLOW_CURRENT_FRAME = 5; // for open, implies OPEN_CURRENT_BUFFER
461 const FOLLOW_TOP_FRAME = 6; // for open, implies OPEN_CURRENT_BUFFER
463 var TARGET_PROMPTS = [" in current buffer",
465 " in new buffer (background)",
471 var TARGET_NAMES = ["current buffer",
473 "new buffer (background)",
480 function browse_target_prompt(target, prefix) {
483 return prefix + TARGET_PROMPTS[target] + ":";
487 var default_browse_targets = {};
488 default_browse_targets["follow"] = [FOLLOW_DEFAULT, OPEN_NEW_BUFFER, OPEN_NEW_WINDOW];
489 default_browse_targets["follow-top"] = [FOLLOW_TOP_FRAME, FOLLOW_CURRENT_FRAME];
491 interactive_context.prototype.browse_target = function (action) {
492 var prefix = this.prefix_argument;
493 var targets = action;
494 while (typeof(targets) == "string")
495 targets = default_browse_targets[targets];
496 if (prefix == null || typeof(prefix) != "object")
500 while (num >= 4 && index + 1 < targets.length) {
504 return targets[index];
507 function create_buffer(window, creator, target) {
509 case OPEN_NEW_BUFFER:
510 window.buffers.current = creator(window, null);
512 case OPEN_NEW_BUFFER_BACKGROUND:
513 creator(window, null);
515 case OPEN_NEW_WINDOW:
516 make_window(creator);
519 throw new Error("invalid target");
523 var queued_buffer_creators = null;
524 function _process_queued_buffer_creators(window) {
525 for (var i = 0; i < queued_buffer_creators.length; ++i) {
526 var x = queued_buffer_creators[i];
527 create_buffer(window, x[0], x[1]);
529 queued_buffer_creators = null;
531 function create_buffer_in_current_window(creator, target, focus_existing) {
532 if (target == OPEN_NEW_WINDOW)
533 throw new Error("invalid target");
534 var window = get_recent_conkeror_window();
538 create_buffer(window, creator, target);
539 } else if (queued_buffer_creators != null) {
540 queued_buffer_creators.push([creator,target]);
542 queued_buffer_creators = [];
543 window = make_window(creator);
544 add_hook.call(window, "window_initialize_late_hook", _process_queued_buffer_creators);
548 minibuffer_auto_complete_preferences["buffer"] = true;
549 define_keywords("$default");
550 minibuffer.prototype.read_buffer = function () {
551 var window = this.window;
552 var buffer = this.window.buffers.current;
553 keywords(arguments, $prompt = "Buffer:",
555 $history = "buffer");
556 var completer = all_word_completer(
557 $completions = function (visitor) window.buffers.for_each(visitor),
558 $get_string = function (x) x.description,
559 $get_description = function (x) x.title);
560 var result = yield this.read(
561 $prompt = arguments.$prompt,
562 $history = arguments.$history,
563 $completer = completer,
564 $match_required = true,
565 $auto_complete = "buffer",
566 $auto_complete_initial = true,
567 $auto_complete_delay = 0,
568 $default_completion = arguments.$default);
569 yield co_return(result);
572 interactive_context.prototype.__defineGetter__("cwd", function () this.buffer.cwd);
574 function buffer_next (window, count)
576 var index = window.buffers.selected_index;
577 var total = window.buffers.count;
578 index = (index + count) % total;
581 window.buffers.current = window.buffers.get_buffer(index);
583 interactive("buffer-next",
584 "Switch to the next buffer.",
585 function (I) {buffer_next(I.window, I.p);});
586 interactive("buffer-previous",
587 "Switch to the previous buffer.",
588 function (I) {buffer_next(I.window, -I.p);});
590 function switch_to_buffer (window, buffer)
592 if (buffer && !buffer.dead)
593 window.buffers.current = buffer;
595 interactive("switch-to-buffer",
596 "Switch to a buffer specified in the minibuffer.",
600 (yield I.minibuffer.read_buffer(
601 $prompt = "Switch to buffer:",
602 $default = (I.window.buffers.count > 1 ?
603 I.window.buffers.buffer_list[1] :
608 define_variable("can_kill_last_buffer", true,
609 "If this is set to true, kill-buffer can kill the last remaining buffer, and close the window.");
611 function kill_buffer(buffer, force)
615 var buffers = buffer.window.buffers;
616 if (buffers.count == 1 && buffer == buffers.current) {
617 if (can_kill_last_buffer || force) {
618 delete_window(buffer.window);
622 throw interactive_error("Can't kill last buffer.");
624 buffers.kill_buffer(buffer);
626 interactive("kill-buffer",
627 "Kill a buffer specified in the minibuffer.\n" +
628 "If `can_kill_last_buffer' is set to true, an attempt to kill the last remaining " +
629 "buffer in a window will cause the window to be closed.",
630 function (I) {kill_buffer((yield I.minibuffer.read_buffer($prompt = "Kill buffer:")))});
632 interactive("kill-current-buffer",
633 "Kill the current buffer.\n" +
634 "If `can_kill_last_buffer' is set to true, an attempt to kill the last remaining " +
635 "buffer in a window will cause the window to be closed.",
636 function (I) {kill_buffer(I.buffer)});
638 function change_directory(buffer, dir) {
639 buffer.configuration.cwd = dir;
641 interactive("change-current-directory",
642 "Change the current directory of the selected buffer.",
646 (yield I.minibuffer.read_existing_directory_path(
647 $prompt = "New current directory:",
648 $initial_value = I.cwd)));
651 interactive("shell-command", function (I) {
653 var cmd = (yield I.minibuffer.read_shell_command($cwd = cwd));
654 yield shell_command(cmd, $cwd = cwd);
657 function unfocus(buffer)
659 var elem = buffer.focused_element;
664 var win = buffer.focused_frame;
665 if (win != buffer.top_frame)
667 buffer.top_frame.focus();
669 interactive("unfocus", function (I) {unfocus(I.buffer)});
671 require_later("content-buffer.js");
673 var mode_functions = {};
675 var mode_display_names = {};
677 define_buffer_local_hook("buffer_mode_change_hook");
678 define_current_buffer_hook("current_buffer_mode_change_hook", "buffer_mode_change_hook");
680 define_keywords("$class", "$enable", "$disable", "$doc");
681 function define_buffer_mode(name, display_name) {
684 var hyphen_name = name.replace("_","-","g");
685 var mode_class = arguments.$class;
686 var enable = arguments.$enable;
687 var disable = arguments.$disable;
689 mode_display_names[name] = display_name;
693 if (disable == false) {
699 var state = (mode_class != null) ? mode_class : (name + "_enabled");
700 var enable_hook_name = name + "_enable_hook";
701 var disable_hook_name = name + "_disable_hook";
702 define_buffer_local_hook(enable_hook_name);
703 define_buffer_local_hook(disable_hook_name);
705 var change_hook_name = null;
708 mode_functions[name] = {enable: enable,
710 mode_class: mode_class,
711 disable_hook_name: disable_hook_name};
712 change_hook_name = mode_class + "_change_hook";
713 define_buffer_local_hook(change_hook_name);
716 function func(buffer, arg) {
717 var old_state = buffer[state];
718 var cur_state = (old_state == name);
719 var new_state = (arg == null) ? !cur_state : (arg > 0);
720 if ((new_state == cur_state) || (!can_disable && !new_state))
723 if (mode_class && old_state != null) {
724 buffer.enabled_modes.splice(buffer.enabled_modes.indexOf(old_state), 1);
725 let x = mode_functions[old_state];
728 conkeror[x.disable_hook_name].run(buffer);
730 buffer[state] = name;
733 conkeror[enable_hook_name].run(buffer);
734 buffer.enabled_modes.push(name);
736 buffer.enabled_modes.splice(buffer.enabled_modes.indexOf(name), 1);
738 conkeror[disable_hook_name].run(buffer);
739 buffer[state] = null;
741 if (change_hook_name)
742 conkeror[change_hook_name].run(buffer, buffer[state]);
743 buffer_mode_change_hook.run(buffer);
746 conkeror[name] = func;
747 interactive(hyphen_name, arguments.$doc, function (I) {
749 var new_state = func(I.buffer, arg && univ_arg_to_number(arg));
750 I.minibuffer.message(hyphen_name + (new_state ? " enabled" : " disabled"));
753 ignore_function_for_get_caller_source_code_reference("define_buffer_mode");
756 function minibuffer_mode_indicator(window) {
757 this.window = window;
758 var element = create_XUL(window, "label");
759 element.setAttribute("id", "minibuffer-mode-indicator");
760 element.collapsed = true;
761 element.setAttribute("class", "minibuffer");
762 window.document.getElementById("minibuffer").appendChild(element);
763 this.element = element;
764 this.hook_func = method_caller(this, this.update);
765 add_hook.call(window, "select_buffer_hook", this.hook_func);
766 add_hook.call(window, "current_buffer_mode_change_hook", this.hook_func);
769 minibuffer_mode_indicator.prototype = {
770 update : function () {
771 var buf = this.window.buffers.current;
772 var modes = buf.enabled_modes;
773 var str = modes.map( function (x) {
774 let y = mode_display_names[x];
776 return "[" + y + "]";
779 } ).filter( function (x) x != null ).join(" ");
780 this.element.collapsed = (str.length == 0);
781 this.element.value = str;
783 uninstall : function () {
784 remove_hook.call(window, "select_buffer_hook", this.hook_fun);
785 remove_hook.call(window, "current_buffer_mode_change_hook", this.hook_fun);
786 this.element.parentNode.removeChild(this.element);
789 define_global_window_mode("minibuffer_mode_indicator", "window_initialize_hook");
790 minibuffer_mode_indicator_mode(true);