2 * (C) Copyright 2004-2007 Shawn Betts
3 * (C) Copyright 2007-2012 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) {
13 define_buffer_local_hook(hook_name);
14 add_hook(existing_hook, function (buffer) {
15 if (!buffer.window.buffers || buffer != buffer.window.buffers.current)
17 var hook = conkeror[hook_name];
18 hook.run.apply(hook, Array.prototype.slice.call(arguments));
22 define_buffer_local_hook("buffer_title_change_hook");
23 define_buffer_local_hook("buffer_description_change_hook");
24 define_buffer_local_hook("buffer_icon_change_hook");
25 define_buffer_local_hook("select_buffer_hook");
26 define_buffer_local_hook("create_buffer_early_hook");
27 define_buffer_local_hook("create_buffer_late_hook");
28 define_buffer_local_hook("create_buffer_hook");
29 define_buffer_local_hook("kill_buffer_hook");
30 define_buffer_local_hook("move_buffer_hook");
31 define_buffer_local_hook("buffer_scroll_hook");
32 define_buffer_local_hook("buffer_dom_content_loaded_hook");
33 define_buffer_local_hook("buffer_loaded_hook");
34 define_buffer_local_hook("set_input_mode_hook");
35 define_buffer_local_hook("zoom_hook");
37 define_current_buffer_hook("current_buffer_title_change_hook", "buffer_title_change_hook");
38 define_current_buffer_hook("current_buffer_description_change_hook", "buffer_description_change_hook");
39 define_current_buffer_hook("current_buffer_icon_change_hook", "buffer_icon_change_hook");
40 define_current_buffer_hook("current_buffer_scroll_hook", "buffer_scroll_hook");
41 define_current_buffer_hook("current_buffer_dom_content_loaded_hook", "buffer_dom_content_loaded_hook");
42 define_current_buffer_hook("current_buffer_zoom_hook", "zoom_hook");
45 function buffer_position_before (container, b, i) {
49 function buffer_position_after (container, b, i) {
53 function buffer_position_end (container, b, i) {
54 return container.count;
57 function buffer_position_end_by_type (container, b, i) {
58 // after last buffer of same type
59 var count = container.count;
62 container.get_buffer(p).constructor != b.constructor)
72 define_variable("new_buffer_position", buffer_position_end,
73 "Used to compute the position in the buffer-list at which "+
74 "to insert new buffers which do not have an opener. These "+
75 "include buffers created by typing an url or webjump, or "+
76 "buffers created via command-line remoting. The value "+
77 "should be a number giving the index or a function of three "+
78 "arguments that returns the index at which to insert the "+
79 "new buffer. The first argument is the buffer_container "+
80 "into which the new buffer is being inserted. The second "+
81 "argument is the buffer to be inserted. The third argument "+
82 "is the position of the currently selected buffer. Several "+
83 "such functions are provided, including, buffer_position_before, "+
84 "buffer_position_after, buffer_position_end, and "+
85 "buffer_position_end_by_type.");
87 define_variable("new_buffer_with_opener_position", buffer_position_after,
88 "Used to compute the position in the buffer-list at which "+
89 "to insert new buffers which have an opener in the same "+
90 "window. These include buffers created by following a link "+
91 "or frame, and contextual help buffers. The allowed values "+
92 "are the same as those for `new_buffer_position', except that "+
93 "the second argument passed to the function is the index of "+
94 "the opener instead of the index of the current buffer (often "+
95 "one and the same).");
97 define_variable("bury_buffer_position", null,
98 "Used to compute the position in the buffer-list to move a "+
99 "buried buffer to. A value of null prevents bury-buffer "+
100 "from moving the buffer at all. Other allowed values are "+
101 "the same as those for `new_buffer_position', except that "+
102 "the second argument passed to the function is the index of "+
103 "the new buffer that will be selected after burying the "+
106 define_variable("allow_browser_window_close", true,
107 "If this is set to true, if a content buffer page calls " +
108 "window.close() from JavaScript and is not prevented by the " +
109 "normal Mozilla mechanism that restricts pages from closing " +
110 "a window that was not opened by a script, the buffer will be " +
111 "killed, deleting the window as well if it is the only buffer.");
113 define_keywords("$opener", "$position");
114 function buffer_creator (type) {
115 var args = forward_keywords(arguments);
116 return function (window) {
117 return new type(window, args);
121 function buffer_modality (buffer) {
122 buffer.keymaps.push(default_global_keymap);
125 function buffer (window) {
126 this.constructor_begin();
127 keywords(arguments, $position = this.default_position);
128 this.opener = arguments.$opener;
129 this.window = window;
130 var element = create_XUL(window, "vbox");
131 element.setAttribute("flex", "1");
132 var browser = create_XUL(window, "browser");
133 browser.setAttribute("type", "content");
134 browser.setAttribute("flex", "1");
135 browser.setAttribute("autocompletepopup", "popup_autocomplete");
136 element.appendChild(browser);
137 this.window.buffers.container.appendChild(element);
138 this.window.buffers.insert(this, arguments.$position, this.opener);
139 this.window.buffers.buffer_history.push(this);
140 this.element = element;
141 this.browser = element.firstChild;
142 this.element.conkeror_buffer_object = this;
144 this.local = { __proto__: conkeror };
146 this.enabled_modes = [];
147 this.default_browser_object_classes = {};
151 this.browser.addEventListener("scroll", function (event) {
152 buffer_scroll_hook.run(buffer);
153 }, true /* capture */);
155 this.browser.addEventListener("DOMContentLoaded", function (event) {
156 buffer_dom_content_loaded_hook.run(buffer);
157 }, true /* capture */);
159 this.window.setTimeout(function () { create_buffer_late_hook.run(buffer); }, 0);
161 this.browser.addEventListener("load", function (event) {
162 if (event.target == buffer.document)
163 buffer_loaded_hook.run(buffer);
164 }, true /* capture */);
166 this.browser.addEventListener("DOMWindowClose", function (event) {
167 /* This call to preventDefault is very important; without
168 * it, somehow Mozilla does something bad and as a result
169 * the window loses focus, causing keyboard commands to
171 event.preventDefault();
173 if (allow_browser_window_close)
174 kill_buffer(buffer, true);
177 this.browser.addEventListener("focus", function (event) {
178 if (buffer.focusblocker &&
179 event.target instanceof Ci.nsIDOMHTMLElement &&
180 buffer.focusblocker(buffer, event))
184 buffer.set_input_mode();
187 this.browser.addEventListener("blur", function (event) {
188 buffer.set_input_mode();
191 this.modalities = [buffer_modality];
193 // When create_buffer_early_hook runs, basic buffer properties
194 // will be available, but not the properties subclasses.
195 create_buffer_early_hook.run(this);
197 this.constructor_end();
201 toString: function () "#<buffer>",
203 // default_position is the default value for the $position keyword to
204 // the buffer constructor. This property can be set on the prototype
205 // of a subclass in order to override new_buffer_position and
206 // new_buffer_with_opener_position for specific types of buffers.
207 default_position: null,
209 /* Saved focus state */
210 saved_focused_frame: null,
211 saved_focused_element: null,
213 // get title () [must be defined by subclasses]
214 // get name () [must be defined by subclasses]
215 dead: false, /* This is set when the buffer is killed */
220 // The property focusblocker is available for an external module to
221 // put a function on which takes a buffer as its argument and returns
222 // true to block a focus event, or false to let normal processing
223 // occur. Having this one property explicitly handled by the buffer
224 // class allows for otherwise modular focus-blockers.
227 // icon is a string url of an icon to use for this buffer. Setting it
228 // causes buffer_icon_change_hook to be run.
230 get icon () this._icon,
232 if (this._icon != x) {
234 buffer_icon_change_hook.run(this);
240 set_default_message: function (str) {
241 this.default_message = str;
242 if (this == this.window.buffers.current)
243 this.window.minibuffer.set_default_message(str);
246 constructors_running: 0,
248 constructor_begin: function () {
249 this.constructors_running++;
252 constructor_end: function () {
253 if (--this.constructors_running == 0) {
254 create_buffer_hook.run(this);
255 this.set_input_mode();
260 destroy: function () {
264 this.saved_focused_frame = null;
265 this.saved_focused_element = null;
266 // prevent modalities from accessing dead browser
267 this.modalities = [];
270 set_input_mode: function () {
271 if (this != this.window.buffers.current)
274 this.modalities.map(function (m) m(this), this);
275 set_input_mode_hook.run(this);
278 override_keymaps: function (keymaps) {
280 this.keymaps = keymaps;
281 this.set_input_mode = function () {
282 set_input_mode_hook.run(this);
285 delete this.set_input_mode;
286 this.set_input_mode();
289 /* Browser accessors */
290 get top_frame () { return this.browser.contentWindow; },
291 get document () { return this.browser.contentDocument; },
292 get web_navigation () { return this.browser.webNavigation; },
293 get doc_shell () { return this.browser.docShell; },
294 get markup_document_viewer () { return this.browser.markupDocumentViewer; },
295 get current_uri () { return this.browser.currentURI; },
297 is_child_element: function (element) {
298 return (element && this.is_child_frame(element.ownerDocument.defaultView));
301 is_child_frame: function (frame) {
302 return (frame && frame.top == this.top_frame);
305 // This method is like focused_frame, except that if no content
306 // frame actually has focus, this returns null.
307 get focused_frame_or_null () {
308 var frame = this.window.document.commandDispatcher.focusedWindow;
309 if (this.is_child_frame(frame))
314 get focused_frame () {
315 var frame = this.window.document.commandDispatcher.focusedWindow;
316 if (this.is_child_frame(frame))
318 return this.top_frame;
321 get focused_element () {
322 var element = this.window.document.commandDispatcher.focusedElement;
323 if (this.is_child_element(element))
328 get focused_selection_controller () {
329 return this.focused_frame
330 .QueryInterface(Ci.nsIInterfaceRequestor)
331 .getInterface(Ci.nsIWebNavigation)
332 .QueryInterface(Ci.nsIInterfaceRequestor)
333 .getInterface(Ci.nsISelectionDisplay)
334 .QueryInterface(Ci.nsISelectionController);
337 do_command: function (command) {
338 function attempt_command (element, command) {
340 if (element.controllers
341 && (controller = element.controllers.getControllerForCommand(command)) != null
342 && controller.isCommandEnabled(command))
344 controller.doCommand(command);
350 var element = this.focused_element;
351 if (element && attempt_command(element, command))
353 var win = this.focused_frame;
355 if (attempt_command(win, command))
357 if (!win.parent || win == win.parent)
364 function with_current_buffer (buffer, callback) {
365 return callback(new interactive_context(buffer));
368 function check_buffer (obj, type) {
369 if (!(obj instanceof type))
370 throw interactive_error("Buffer has invalid type.");
372 throw interactive_error("Buffer has already been killed.");
376 function caret_enabled (buffer) {
377 return buffer.browser.getAttribute('showcaret');
380 function clear_selection (buffer) {
381 let sel_ctrl = buffer.focused_selection_controller;
383 let sel = sel_ctrl.getSelection(sel_ctrl.SELECTION_NORMAL);
384 if (caret_enabled(buffer)) {
386 sel.collapseToStart();
388 sel.removeAllRanges();
394 function buffer_container (window, create_initial_buffer) {
395 this.window = window;
396 this.container = window.document.getElementById("buffer-container");
397 this.buffer_list = [];
398 this.buffer_history = [];
399 window.buffers = this;
400 create_initial_buffer(window);
402 buffer_container.prototype = {
403 constructor: buffer_container,
404 toString: function () "#<buffer_container>",
406 insert: function (buffer, position, opener) {
407 var i = this.index_of(opener);
408 if (position == null) {
410 position = new_buffer_position;
412 position = new_buffer_with_opener_position;
415 i = this.selected_index || 0;
417 if (position instanceof Function)
418 var p = position(this, buffer, i);
421 this.buffer_list.splice(p, 0, buffer);
423 this.buffer_list.splice(0, 0, buffer);
424 dumpln("Error inserting buffer, inserted at 0.");
430 return this.container.selectedPanel.conkeror_buffer_object;
433 set current (buffer) {
434 var old_value = this.current;
435 if (old_value == buffer)
438 this.buffer_history.splice(this.buffer_history.indexOf(buffer), 1);
439 this.buffer_history.unshift(buffer);
441 this._switch_away_from(this.current);
442 this._switch_to(buffer);
445 select_buffer_hook.run(buffer);
448 _switch_away_from: function (old_value) {
450 old_value.saved_focused_frame = old_value.focused_frame;
451 old_value.saved_focused_element = old_value.focused_element;
453 old_value.browser.setAttribute("type", "content");
456 _switch_to: function (buffer) {
457 // Select new buffer in the XUL deck
458 this.container.selectedPanel = buffer.element;
460 buffer.browser.setAttribute("type", "content-primary");
463 * This next focus call seems to be needed to avoid focus
464 * somehow getting lost (and the keypress handler therefore
465 * not getting called at all) when killing buffers.
469 // Restore focus state
470 buffer.browser.focus();
471 if (buffer.saved_focused_element)
472 set_focus_no_scroll(this.window, buffer.saved_focused_element);
473 else if (buffer.saved_focused_frame)
474 set_focus_no_scroll(this.window, buffer.saved_focused_frame);
476 buffer.saved_focused_element = null;
477 buffer.saved_focused_frame = null;
479 buffer.set_input_mode();
481 this.window.minibuffer.set_default_message(buffer.default_message);
485 return this.buffer_list.length;
488 get_buffer: function (index) {
489 if (index >= 0 && index < this.count)
490 return this.buffer_list[index]
494 get selected_index () {
495 var nodes = this.buffer_list;
496 var count = nodes.length;
497 for (var i = 0; i < count; ++i)
498 if (nodes[i] == this.container.selectedPanel.conkeror_buffer_object)
503 index_of: function (b) {
504 var nodes = this.buffer_list;
505 var count = nodes.length;
506 for (var i = 0; i < count; ++i)
512 get unique_name_list () {
513 var existing_names = {};
515 this.for_each(function(b) {
516 var base_name = b.name;
517 var name = base_name;
519 while (existing_names[name]) {
521 name = base_name + "<" + index + ">";
523 existing_names[name] = true;
524 bufs.push([name, b]);
529 kill_buffer: function (b) {
532 var count = this.count;
535 var new_buffer = this.buffer_history[0];
537 if (b == new_buffer) {
538 new_buffer = this.buffer_history[1];
541 this._switch_away_from(this.current);
542 // The removeChild call below may trigger events in progress
543 // listeners. This call to `destroy' gives buffer subclasses a
544 // chance to remove such listeners, so that they cannot try to
545 // perform UI actions based upon a xul:browser that no longer
547 var element = b.element;
549 this.container.removeChild(element);
550 this.buffer_list.splice(this.buffer_list.indexOf(b), 1);
551 this.buffer_history.splice(this.buffer_history.indexOf(b), 1);
552 this._switch_to(new_buffer);
554 select_buffer_hook.run(new_buffer);
555 this.buffer_history.splice(this.buffer_history.indexOf(new_buffer), 1);
556 this.buffer_history.unshift(new_buffer);
558 kill_buffer_hook.run(b);
562 bury_buffer: function (b) {
563 var new_buffer = this.buffer_history[0];
565 new_buffer = this.buffer_history[1];
567 throw interactive_error("No other buffer");
568 if (bury_buffer_position != null) {
569 this.buffer_list.splice(this.buffer_list.indexOf(b), 1);
570 this.insert(b, bury_buffer_position, new_buffer);
572 this.buffer_history.splice(this.buffer_history.indexOf(b), 1);
573 this.buffer_history.push(b);
574 this.current = new_buffer;
575 if (bury_buffer_position != null)
576 move_buffer_hook.run(b);
580 unbury_buffer: function (b) {
581 var c = this.current;
582 if (bury_buffer_position != null) {
583 this.buffer_list.splice(this.buffer_list.indexOf(b), 1);
584 this.buffer_list.splice(this.buffer_list.indexOf(c), 0, b);
586 this.buffer_history.splice(this.buffer_history.indexOf(b), 1);
587 this.buffer_history.unshift(b);
589 if (bury_buffer_position != null)
590 move_buffer_hook.run(b);
594 for_each: function (f) {
595 var count = this.count;
596 for (var i = 0; i < count; ++i)
597 f(this.get_buffer(i));
601 function buffer_initialize_window_early (window) {
603 * Use content_buffer by default to handle an unusual case where
604 * browser.chromeURI is used perhaps. In general this default
605 * should not be needed.
607 var create_initial_buffer =
608 window.args.initial_buffer_creator || buffer_creator(content_buffer);
609 new buffer_container(window, create_initial_buffer);
612 add_hook("window_initialize_early_hook", buffer_initialize_window_early);
616 * initialize_first_buffer_type is a workaround for a XULRunner bug that
617 * first appeared in version 2.0, manifested as missing scrollbars in the
618 * first buffer of any window. It only affects content-primary browsers,
619 * and the workaround is to initialize the browser as type "content" then
620 * change it to content-primary after a delay.
622 function initialize_first_buffer_type (window) {
623 window.buffers.current.browser.setAttribute("type", "content-primary");
626 add_hook("window_initialize_late_hook", initialize_first_buffer_type);
629 define_buffer_local_hook("buffer_kill_before_hook", RUN_HOOK_UNTIL_FAILURE);
630 function buffer_before_window_close (window) {
631 var bs = window.buffers;
632 var count = bs.count;
633 for (let i = 0; i < count; ++i) {
634 if (!buffer_kill_before_hook.run(bs.get_buffer(i)))
639 add_hook("window_before_close_hook", buffer_before_window_close);
641 function buffer_window_close_handler (window) {
642 var bs = window.buffers;
643 var count = bs.count;
644 for (let i = 0; i < count; ++i) {
645 let b = bs.get_buffer(i);
649 add_hook("window_close_hook", buffer_window_close_handler);
651 /* open/follow targets */
652 const OPEN_CURRENT_BUFFER = 0; // only valid for open if the current
653 // buffer is a content_buffer.
654 const OPEN_NEW_BUFFER = 1;
655 const OPEN_NEW_BUFFER_BACKGROUND = 2;
656 const OPEN_NEW_WINDOW = 3;
658 const FOLLOW_DEFAULT = 4; // for open, implies OPEN_CURRENT_BUFFER
659 const FOLLOW_CURRENT_FRAME = 5; // for open, implies OPEN_CURRENT_BUFFER
661 var TARGET_PROMPTS = [" in current buffer",
663 " in new buffer (background)",
666 " in current frame"];
668 var TARGET_NAMES = ["current buffer",
670 "new buffer (background)",
676 function create_buffer (window, creator, target) {
678 case OPEN_NEW_BUFFER:
679 window.buffers.current = creator(window, null);
681 case OPEN_NEW_BUFFER_BACKGROUND:
682 creator(window, null);
684 case OPEN_NEW_WINDOW:
685 make_window(creator);
688 throw new Error("invalid target");
692 let (queued_buffer_creators = null) {
693 function create_buffer_in_current_window (creator, target, focus_existing) {
694 function process_queued_buffer_creators (window) {
695 for (var i = 0; i < queued_buffer_creators.length; ++i) {
696 var x = queued_buffer_creators[i];
697 create_buffer(window, x[0], x[1]);
699 queued_buffer_creators = null;
702 if (target == OPEN_NEW_WINDOW)
703 throw new Error("invalid target");
704 var window = get_recent_conkeror_window();
708 create_buffer(window, creator, target);
709 } else if (queued_buffer_creators != null) {
710 queued_buffer_creators.push([creator,target]);
712 queued_buffer_creators = [];
713 window = make_window(creator);
714 add_hook.call(window, "window_initialize_late_hook", process_queued_buffer_creators);
723 define_variable("read_buffer_show_icons", false,
724 "Boolean which says whether read_buffer should show buffer "+
725 "icons in the completions list.\nNote, setting this variable "+
726 "alone does not cause favicons or other kinds of icons to be "+
727 "fetched. For that, load the `favicon' (or similar other) "+
730 minibuffer_auto_complete_preferences["buffer"] = true;
731 define_keywords("$buffers", "$default");
732 minibuffer.prototype.read_buffer = function () {
733 var window = this.window;
734 keywords(arguments, $prompt = "Buffer:",
735 $buffers = function (visitor) window.buffers.for_each(visitor),
736 $default = window.buffers.current,
737 $history = "buffer");
738 var completer = all_word_completer(
739 $completions = arguments.$buffers,
740 $get_string = function (x) x.description,
741 $get_description = function (x) x.title,
742 $get_icon = (read_buffer_show_icons ?
743 function (x) x.icon : null));
744 var result = yield this.read(
745 $keymap = read_buffer_keymap,
746 $prompt = arguments.$prompt,
747 $history = arguments.$history,
748 $completer = completer,
749 $enable_icons = read_buffer_show_icons,
750 $match_required = true,
751 $auto_complete = "buffer",
752 $auto_complete_initial = true,
753 $auto_complete_delay = 0,
754 $default_completion = arguments.$default);
755 yield co_return(result);
759 function buffer_move_forward (window, count) {
760 var buffers = window.buffers;
761 var index = buffers.selected_index;
762 var buffer = buffers.current
763 var total = buffers.count;
766 var new_index = (index + count) % total;
767 if (new_index == index)
771 buffers.buffer_list.splice(index, 1);
772 buffers.buffer_list.splice(new_index, 0, buffer);
773 move_buffer_hook.run(buffer);
775 interactive("buffer-move-forward",
776 "Move the current buffer forward in the buffer order.",
777 function (I) { buffer_move_forward(I.window, I.p); });
779 interactive("buffer-move-backward",
780 "Move the current buffer backward in the buffer order.",
781 function (I) { buffer_move_forward(I.window, -I.p); });
784 function buffer_next (window, count) {
785 var index = window.buffers.selected_index;
786 var total = window.buffers.count;
788 throw new interactive_error("No other buffer");
789 index = (index + count) % total;
792 window.buffers.current = window.buffers.get_buffer(index);
794 interactive("buffer-next",
795 "Switch to the next buffer.",
796 function (I) { buffer_next(I.window, I.p); });
797 interactive("buffer-previous",
798 "Switch to the previous buffer.",
799 function (I) { buffer_next(I.window, -I.p); });
801 function switch_to_buffer (window, buffer) {
802 if (buffer && !buffer.dead)
803 window.buffers.current = buffer;
805 interactive("switch-to-buffer",
806 "Prompt for a buffer and switch to it.",
810 (yield I.minibuffer.read_buffer(
811 $prompt = "Switch to buffer:",
812 $default = (I.window.buffers.count > 1 ?
813 I.window.buffers.buffer_history[1] :
817 define_variable("can_kill_last_buffer", true,
818 "When true, kill-buffer can kill the last buffer in a window, "+
819 "and close the window.");
821 function kill_other_buffers (buffer) {
824 var bs = buffer.window.buffers;
826 while ((b = bs.get_buffer(0)) != buffer)
828 var count = bs.count;
830 bs.kill_buffer(bs.get_buffer(1));
832 interactive("kill-other-buffers",
833 "Kill all buffers except current one.\n",
834 function (I) { kill_other_buffers(I.buffer); });
837 function kill_buffer (buffer, force) {
840 var buffers = buffer.window.buffers;
841 if (buffers.count == 1 && buffer == buffers.current) {
842 if (can_kill_last_buffer || force) {
843 delete_window(buffer.window);
846 throw interactive_error("Can't kill last buffer.");
848 buffers.kill_buffer(buffer);
850 interactive("kill-buffer",
851 "Kill a buffer specified in the minibuffer.\n" +
852 "If `can_kill_last_buffer' is set to true, an attempt to kill the "+
853 "last remaining buffer in a window will cause the window to be closed.",
855 kill_buffer((yield I.minibuffer.read_buffer($prompt = "Kill buffer:")));
858 interactive("kill-current-buffer",
859 "Kill the current buffer.\n" +
860 "If `can_kill_last_buffer' is set to true, an attempt to kill the "+
861 "last remaining buffer in a window will cause the window to be closed.",
862 function (I) { kill_buffer(I.buffer); });
864 interactive("read-buffer-kill-buffer",
865 "Kill the current selected buffer in the completions list "+
866 "in a read buffer minibuffer interaction.",
868 var s = I.window.minibuffer.current_state;
869 var i = s.selected_completion_index;
870 var c = s.completions;
873 kill_buffer(c.get_value(i));
874 s.completer.refresh();
875 s.handle_input(I.window.minibuffer);
878 interactive("bury-buffer",
879 "Bury the current buffer.\nPut the current buffer at the end of " +
880 "the buffer list, so that it is the least likely buffer to be " +
881 "selected by `switch-to-buffer'.",
882 function (I) { I.window.buffers.bury_buffer(I.buffer); });
884 interactive("unbury-buffer",
885 "Unbury the buffer lowest in the buffer-history list.\n"+
886 "With universal argument, prompt for a buffer. When "+
887 "`bury_buffer_position` is non-null, move the buffer "+
888 "to the current position in the buffer list.",
890 var buffers = I.window.buffers;
891 if (I.prefix_argument)
892 var b = yield I.minibuffer.read_buffer(
893 $prompt = "Switch to buffer:",
894 $buffers = function (visitor) {
895 var count = buffers.count;
896 for (var i = count - 1; i >= 0; --i)
897 visitor(buffers.buffer_history[i]);
899 $default = buffers.buffer_history[buffers.count - 1]);
901 b = buffers.buffer_history[buffers.count - 1];
902 buffers.unbury_buffer(b);
905 function change_directory (buffer, dir) {
906 if (buffer.page != null)
907 delete buffer.page.local.cwd;
908 buffer.local.cwd = make_file(dir);
910 interactive("change-directory",
911 "Change the current directory of the selected buffer.",
915 (yield I.minibuffer.read_existing_directory_path(
916 $prompt = "Change to directory:",
917 $initial_value = make_file(I.local.cwd).path)));
920 interactive("shell-command", null,
922 var cwd = I.local.cwd;
923 var cmd = (yield I.minibuffer.read_shell_command($cwd = cwd));
924 yield shell_command(cmd, $cwd = cwd);
929 * selection_is_embed_p is used to test whether the unfocus command can
930 * unfocus an element, even though there is a selection. This happens
931 * when the focused element is an html:embed.
933 function selection_is_embed_p (sel, focused_element) {
934 if (sel.rangeCount == 1) {
936 var r = sel.getRangeAt(0);
937 var a = r.startContainer.childNodes[r.startOffset];
938 if ((a instanceof Ci.nsIDOMHTMLEmbedElement ||
939 a instanceof Ci.nsIDOMHTMLObjectElement) &&
940 a == focused_element)
950 * unfocus is a high-level command for unfocusing hyperlinks, inputs,
951 * frames, iframes, plugins, and also clearing the selection.
953 define_buffer_local_hook("unfocus_hook");
954 function unfocus (window, buffer) {
955 // 1. if there is a selection, clear it.
956 var selc = buffer.focused_selection_controller;
958 var sel = selc.getSelection(selc.SELECTION_NORMAL);
959 var active = ! sel.isCollapsed;
960 var embed_p = selection_is_embed_p(sel, buffer.focused_element);
961 clear_selection(buffer);
962 if (active && !embed_p) {
963 window.minibuffer.message("cleared selection");
967 // 2. if there is a focused element, unfocus it.
968 if (buffer.focused_element) {
969 buffer.focused_element.blur();
970 // if an element in a detached fragment has focus, blur() will
971 // not work, and we need to take more drastic measures. the
972 // action taken was found through experiment, so it is possibly
973 // not the most concise way to unfocus such an element.
974 if (buffer.focused_element) {
975 buffer.element.focus();
976 buffer.top_frame.focus();
978 window.minibuffer.message("unfocused element");
981 // 3. if an iframe has focus, we must blur it.
982 if (buffer.focused_frame_or_null &&
983 buffer.focused_frame_or_null.frameElement)
985 buffer.focused_frame_or_null.frameElement.blur();
987 // 4. return focus to top-frame from subframes and plugins.
988 buffer.top_frame.focus();
989 buffer.top_frame.focus(); // needed to get focus back from plugins
990 window.minibuffer.message("refocused top frame");
991 // give page-modes an opportunity to set focus specially
992 unfocus_hook.run(buffer);
994 interactive("unfocus",
995 "Unfocus is a high-level command for unfocusing hyperlinks, inputs, "+
996 "frames, iframes, plugins, and also for clearing the selection.\n"+
997 "The action that it takes is based on precedence. If there is a "+
998 "focused hyperlink or input, it will unfocus that. Otherwise, if "+
999 "there is a selection, it will clear the selection. Otherwise, it "+
1000 "will return focus to the top frame from a focused frame, iframe, "+
1001 "or plugin. In the case of plugins, since they steal keyboard "+
1002 "control away from Conkeror, the normal way to unfocus them is "+
1003 "to use command-line remoting externally: conkeror -batch -f "+
1004 "unfocus. Page-modes also have an opportunity to alter the default"+
1005 "focus via the hook, `focus_hook'.",
1007 unfocus(I.window, I.buffer);
1011 function for_each_buffer (f) {
1012 for_each_window(function (w) { w.buffers.for_each(f); });
1020 define_buffer_local_hook("buffer_mode_change_hook");
1021 define_current_buffer_hook("current_buffer_mode_change_hook", "buffer_mode_change_hook");
1023 define_keywords("$display_name", "$doc");
1024 function buffer_mode (name, enable, disable) {
1025 keywords(arguments);
1026 this.name = name.replace("-","_","g");
1027 this.hyphen_name = name.replace("_","-","g");
1029 this._enable = enable;
1031 this._disable = disable;
1032 this.display_name = arguments.$display_name;
1033 this.doc = arguments.$doc;
1034 this.enable_hook = this.name + "_enable_hook";
1035 this.disable_hook = this.name + "_disable_hook";
1037 buffer_mode.prototype = {
1038 constructor: buffer_mode,
1046 enable: function (buffer) {
1049 this._enable(buffer);
1051 buffer.enabled_modes.push(this.name);
1052 if (conkeror[this.enable_hook])
1053 conkeror[this.enable_hook].run(buffer);
1054 buffer_mode_change_hook.run(buffer);
1057 disable: function (buffer) {
1060 this._disable(buffer);
1062 var i = buffer.enabled_modes.indexOf(this.name);
1064 buffer.enabled_modes.splice(i, 1);
1065 if (conkeror[this.disable_hook])
1066 conkeror[this.disable_hook].run(buffer);
1067 buffer_mode_change_hook.run(buffer);
1071 define_keywords("$constructor");
1072 function define_buffer_mode (name, enable, disable) {
1073 keywords(arguments, $constructor = buffer_mode, $doc = null);
1074 var constructor = arguments.$constructor;
1075 var m = new constructor(name, enable, disable, forward_keywords(arguments));
1076 name = m.name; // normalized
1078 define_buffer_local_hook(m.enable_hook);
1079 define_buffer_local_hook(m.disable_hook);
1080 interactive(m.hyphen_name,
1083 var enabledp = (I.buffer.enabled_modes.indexOf(name) > -1);
1085 m.disable(I.buffer);
1088 I.minibuffer.message(m.hyphen_name + (enabledp ? " disabled" : " enabled"));
1091 ignore_function_for_get_caller_source_code_reference("define_buffer_mode");
1095 * Mode Display in Minibuffer
1098 function minibuffer_mode_indicator (window) {
1099 this.window = window;
1100 var element = create_XUL(window, "label");
1101 element.setAttribute("id", "minibuffer-mode-indicator");
1102 element.setAttribute("class", "mode-text-widget");
1103 window.document.getElementById("minibuffer").appendChild(element);
1104 this.element = element;
1105 this.hook_function = method_caller(this, this.update);
1106 add_hook.call(window, "select_buffer_hook", this.hook_function);
1107 add_hook.call(window, "current_buffer_mode_change_hook", this.hook_function);
1110 minibuffer_mode_indicator.prototype = {
1111 constructor: minibuffer_mode_indicator,
1114 hook_function: null,
1115 update: function () {
1116 var buffer = this.window.buffers.current;
1117 var str = buffer.enabled_modes.map(
1119 return (conkeror[x].display_name || null);
1120 }).filter(function (x) x != null).join(" ");
1121 this.element.value = str;
1123 uninstall: function () {
1124 remove_hook.call(this.window, "select_buffer_hook", this.hook_function);
1125 remove_hook.call(this.window, "current_buffer_mode_change_hook", this.hook_function);
1126 this.element.parentNode.removeChild(this.element);
1129 define_global_window_mode("minibuffer_mode_indicator", "window_initialize_hook");
1130 minibuffer_mode_indicator_mode(true);
1134 * minibuffer-keymaps-display
1136 function minibuffer_keymaps_display_update (buffer) {
1137 var element = buffer.window.document
1138 .getElementById("keymaps-display");
1140 var str = buffer.keymaps.reduce(
1141 function (acc, kmap) {
1142 if (kmap.display_name)
1143 acc.push(kmap.display_name);
1146 if (element.value != str)
1147 element.value = str;
1151 function minibuffer_keymaps_display_initialize (window) {
1152 var element = create_XUL(window, "label");
1153 element.setAttribute("id", "keymaps-display");
1154 element.setAttribute("class", "mode-text-widget");
1155 element.setAttribute("value", "");
1156 var mb = window.document.getElementById("minibuffer");
1157 mb.appendChild(element);
1160 define_global_mode("minibuffer_keymaps_display_mode",
1161 function enable () {
1162 add_hook("window_initialize_hook", minibuffer_keymaps_display_initialize);
1163 add_hook("set_input_mode_hook", minibuffer_keymaps_display_update);
1164 for_each_window(minibuffer_keymaps_display_initialize);
1166 function disable () {
1167 remove_hook("window_initialize_hook", minibuffer_keymaps_display_initialize);
1168 remove_hook("set_input_mode_hook", minibuffer_keymaps_display_update);
1169 for_each_window(function (w) {
1170 var element = w.document
1171 .getElementById("keymaps-display");
1173 element.parentNode.removeChild(element);
1177 minibuffer_keymaps_display_mode(true);
1181 * minibuffer-keymaps-highlight
1183 function minibuffer_keymaps_highlight_update (buffer) {
1184 var mb = buffer.window.document.getElementById("minibuffer");
1185 if (buffer.keymaps.some(function (k) k.notify))
1186 dom_add_class(mb, "highlight");
1188 dom_remove_class(mb, "highlight");
1191 define_global_mode("minibuffer_keymaps_highlight_mode",
1192 function enable () {
1193 add_hook("set_input_mode_hook", minibuffer_keymaps_highlight_update);
1195 function disable () {
1196 remove_hook("set_input_mode_hook", minibuffer_keymaps_highlight_update);
1197 for_each_window(function (w) {
1198 var mb = w.document.getElementById("minibuffer");
1200 dom_remove_class("highlight");
1204 minibuffer_keymaps_highlight_mode(true);