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");
36 define_current_buffer_hook("current_buffer_title_change_hook", "buffer_title_change_hook");
37 define_current_buffer_hook("current_buffer_description_change_hook", "buffer_description_change_hook");
38 define_current_buffer_hook("current_buffer_icon_change_hook", "buffer_icon_change_hook");
39 define_current_buffer_hook("current_buffer_scroll_hook", "buffer_scroll_hook");
40 define_current_buffer_hook("current_buffer_dom_content_loaded_hook", "buffer_dom_content_loaded_hook");
43 function buffer_position_before (container, b, i) {
47 function buffer_position_after (container, b, i) {
51 function buffer_position_end (container, b, i) {
52 return container.count;
55 function buffer_position_end_by_type (container, b, i) {
56 // after last buffer of same type
57 var count = container.count;
60 container.get_buffer(p).constructor != b.constructor)
70 define_variable("new_buffer_position", buffer_position_end,
71 "Used to compute the position in the buffer-list at which "+
72 "to insert new buffers which do not have an opener. These "+
73 "include buffers created by typing an url or webjump, or "+
74 "buffers created via command-line remoting. The value "+
75 "should be a number giving the index or a function of three "+
76 "arguments that returns the index at which to insert the "+
77 "new buffer. The first argument is the buffer_container "+
78 "into which the new buffer is being inserted. The second "+
79 "argument is the buffer to be inserted. The third argument "+
80 "is the position of the currently selected buffer. Several "+
81 "such functions are provided, including, buffer_position_before, "+
82 "buffer_position_after, buffer_position_end, and "+
83 "buffer_position_end_by_type.");
85 define_variable("new_buffer_with_opener_position", buffer_position_after,
86 "Used to compute the position in the buffer-list at which "+
87 "to insert new buffers which have an opener in the same "+
88 "window. These include buffers created by following a link "+
89 "or frame, and contextual help buffers. The allowed values "+
90 "are the same as those for `new_buffer_position', except that "+
91 "the second argument passed to the function is the index of "+
92 "the opener instead of the index of the current buffer (often "+
93 "one and the same).");
95 define_variable("bury_buffer_position", null,
96 "Used to compute the position in the buffer-list to move a "+
97 "buried buffer to. A value of null prevents bury-buffer "+
98 "from moving the buffer at all. Other allowed values are "+
99 "the same as those for `new_buffer_position', except that "+
100 "the second argument passed to the function is the index of "+
101 "the new buffer that will be selected after burying the "+
104 define_variable("allow_browser_window_close", true,
105 "If this is set to true, if a content buffer page calls " +
106 "window.close() from JavaScript and is not prevented by the " +
107 "normal Mozilla mechanism that restricts pages from closing " +
108 "a window that was not opened by a script, the buffer will be " +
109 "killed, deleting the window as well if it is the only buffer.");
111 define_keywords("$opener", "$position");
112 function buffer_creator (type) {
113 var args = forward_keywords(arguments);
114 return function (window) {
115 return new type(window, args);
119 function buffer_modality (buffer) {
120 buffer.keymaps.push(default_global_keymap);
123 function buffer (window) {
124 this.constructor_begin();
125 keywords(arguments, $position = this.default_position);
126 this.opener = arguments.$opener;
127 this.window = window;
128 var element = create_XUL(window, "vbox");
129 element.setAttribute("flex", "1");
130 var browser = create_XUL(window, "browser");
131 browser.setAttribute("type", "content");
132 browser.setAttribute("flex", "1");
133 browser.setAttribute("autocompletepopup", "popup_autocomplete");
134 element.appendChild(browser);
135 this.window.buffers.container.appendChild(element);
136 this.window.buffers.insert(this, arguments.$position, this.opener);
137 this.window.buffers.buffer_history.push(this);
138 this.element = element;
139 this.browser = element.firstChild;
140 this.element.conkeror_buffer_object = this;
142 this.local = { __proto__: conkeror };
144 this.enabled_modes = [];
145 this.default_browser_object_classes = {};
149 this.browser.addEventListener("scroll", function (event) {
150 buffer_scroll_hook.run(buffer);
151 }, true /* capture */);
153 this.browser.addEventListener("DOMContentLoaded", function (event) {
154 buffer_dom_content_loaded_hook.run(buffer);
155 }, true /* capture */);
157 this.window.setTimeout(function () { create_buffer_late_hook.run(buffer); }, 0);
159 this.browser.addEventListener("load", function (event) {
160 buffer_loaded_hook.run(buffer);
161 }, true /* capture */);
163 this.browser.addEventListener("DOMWindowClose", function (event) {
164 /* This call to preventDefault is very important; without
165 * it, somehow Mozilla does something bad and as a result
166 * the window loses focus, causing keyboard commands to
168 event.preventDefault();
170 if (allow_browser_window_close)
171 kill_buffer(buffer, true);
174 this.browser.addEventListener("focus", function (event) {
175 if (buffer.focusblocker &&
176 event.target instanceof Ci.nsIDOMHTMLElement &&
177 buffer.focusblocker(buffer, event))
181 buffer.set_input_mode();
184 this.browser.addEventListener("blur", function (event) {
185 buffer.set_input_mode();
188 this.modalities = [buffer_modality];
190 // When create_buffer_early_hook runs, basic buffer properties
191 // will be available, but not the properties subclasses.
192 create_buffer_early_hook.run(this);
194 this.constructor_end();
199 // default_position is the default value for the $position keyword to
200 // the buffer constructor. This property can be set on the prototype
201 // of a subclass in order to override new_buffer_position and
202 // new_buffer_with_opener_position for specific types of buffers.
203 default_position: null,
205 /* Saved focus state */
206 saved_focused_frame: null,
207 saved_focused_element: null,
209 // get title () [must be defined by subclasses]
210 // get name () [must be defined by subclasses]
211 dead: false, /* This is set when the buffer is killed */
216 // The property focusblocker is available for an external module to
217 // put a function on which takes a buffer as its argument and returns
218 // true to block a focus event, or false to let normal processing
219 // occur. Having this one property explicitly handled by the buffer
220 // class allows for otherwise modular focus-blockers.
223 // icon is a string url of an icon to use for this buffer. Setting it
224 // causes buffer_icon_change_hook to be run.
226 get icon () this._icon,
228 if (this._icon != x) {
230 buffer_icon_change_hook.run(this);
236 set_default_message: function (str) {
237 this.default_message = str;
238 if (this == this.window.buffers.current)
239 this.window.minibuffer.set_default_message(str);
242 constructors_running: 0,
244 constructor_begin: function () {
245 this.constructors_running++;
248 constructor_end: function () {
249 if (--this.constructors_running == 0) {
250 create_buffer_hook.run(this);
251 this.set_input_mode();
256 destroy: function () {
260 this.saved_focused_frame = null;
261 this.saved_focused_element = null;
262 // prevent modalities from accessing dead browser
263 this.modalities = [];
266 set_input_mode: function () {
267 if (this != this.window.buffers.current)
270 this.modalities.map(function (m) m(this), this);
271 set_input_mode_hook.run(this);
274 override_keymaps: function (keymaps) {
276 this.keymaps = keymaps;
277 this.set_input_mode = function () {
278 set_input_mode_hook.run(this);
281 delete this.set_input_mode;
282 this.set_input_mode();
285 /* Browser accessors */
286 get top_frame () { return this.browser.contentWindow; },
287 get document () { return this.browser.contentDocument; },
288 get web_navigation () { return this.browser.webNavigation; },
289 get doc_shell () { return this.browser.docShell; },
290 get markup_document_viewer () { return this.browser.markupDocumentViewer; },
291 get current_uri () { return this.browser.currentURI; },
293 is_child_element: function (element) {
294 return (element && this.is_child_frame(element.ownerDocument.defaultView));
297 is_child_frame: function (frame) {
298 return (frame && frame.top == this.top_frame);
301 // This method is like focused_frame, except that if no content
302 // frame actually has focus, this returns null.
303 get focused_frame_or_null () {
304 var frame = this.window.document.commandDispatcher.focusedWindow;
305 if (this.is_child_frame(frame))
310 get focused_frame () {
311 var frame = this.window.document.commandDispatcher.focusedWindow;
312 if (this.is_child_frame(frame))
314 return this.top_frame;
317 get focused_element () {
318 var element = this.window.document.commandDispatcher.focusedElement;
319 if (this.is_child_element(element))
324 get focused_selection_controller () {
325 return this.focused_frame
326 .QueryInterface(Ci.nsIInterfaceRequestor)
327 .getInterface(Ci.nsIWebNavigation)
328 .QueryInterface(Ci.nsIInterfaceRequestor)
329 .getInterface(Ci.nsISelectionDisplay)
330 .QueryInterface(Ci.nsISelectionController);
333 do_command: function (command) {
334 function attempt_command (element, command) {
336 if (element.controllers
337 && (controller = element.controllers.getControllerForCommand(command)) != null
338 && controller.isCommandEnabled(command))
340 controller.doCommand(command);
346 var element = this.focused_element;
347 if (element && attempt_command(element, command))
349 var win = this.focused_frame;
351 if (attempt_command(win, command))
353 if (!win.parent || win == win.parent)
360 function with_current_buffer (buffer, callback) {
361 return callback(new interactive_context(buffer));
364 function check_buffer (obj, type) {
365 if (!(obj instanceof type))
366 throw interactive_error("Buffer has invalid type.");
368 throw interactive_error("Buffer has already been killed.");
372 function caret_enabled (buffer) {
373 return buffer.browser.getAttribute('showcaret');
376 function clear_selection (buffer) {
377 let sel_ctrl = buffer.focused_selection_controller;
379 let sel = sel_ctrl.getSelection(sel_ctrl.SELECTION_NORMAL);
380 if (caret_enabled(buffer)) {
382 sel.collapseToStart();
384 sel.removeAllRanges();
390 function buffer_container (window, create_initial_buffer) {
391 this.window = window;
392 this.container = window.document.getElementById("buffer-container");
393 this.buffer_list = [];
394 this.buffer_history = [];
395 window.buffers = this;
396 create_initial_buffer(window);
398 buffer_container.prototype = {
399 constructor: buffer_container,
401 insert: function (buffer, position, opener) {
402 var i = this.index_of(opener);
403 if (position == null) {
405 position = new_buffer_position;
407 position = new_buffer_with_opener_position;
410 i = this.selected_index || 0;
412 if (position instanceof Function)
413 var p = position(this, buffer, i);
416 this.buffer_list.splice(p, 0, buffer);
418 this.buffer_list.splice(0, 0, buffer);
419 dumpln("Error inserting buffer, inserted at 0.");
425 return this.container.selectedPanel.conkeror_buffer_object;
428 set current (buffer) {
429 var old_value = this.current;
430 if (old_value == buffer)
433 this.buffer_history.splice(this.buffer_history.indexOf(buffer), 1);
434 this.buffer_history.unshift(buffer);
436 this._switch_away_from(this.current);
437 this._switch_to(buffer);
440 select_buffer_hook.run(buffer);
443 _switch_away_from: function (old_value) {
445 old_value.saved_focused_frame = old_value.focused_frame;
446 old_value.saved_focused_element = old_value.focused_element;
448 old_value.browser.setAttribute("type", "content");
451 _switch_to: function (buffer) {
452 // Select new buffer in the XUL deck
453 this.container.selectedPanel = buffer.element;
455 buffer.browser.setAttribute("type", "content-primary");
458 * This next focus call seems to be needed to avoid focus
459 * somehow getting lost (and the keypress handler therefore
460 * not getting called at all) when killing buffers.
464 // Restore focus state
465 buffer.browser.focus();
466 if (buffer.saved_focused_element)
467 set_focus_no_scroll(this.window, buffer.saved_focused_element);
468 else if (buffer.saved_focused_frame)
469 set_focus_no_scroll(this.window, buffer.saved_focused_frame);
471 buffer.saved_focused_element = null;
472 buffer.saved_focused_frame = null;
474 buffer.set_input_mode();
476 this.window.minibuffer.set_default_message(buffer.default_message);
480 return this.buffer_list.length;
483 get_buffer: function (index) {
484 if (index >= 0 && index < this.count)
485 return this.buffer_list[index]
489 get selected_index () {
490 var nodes = this.buffer_list;
491 var count = nodes.length;
492 for (var i = 0; i < count; ++i)
493 if (nodes[i] == this.container.selectedPanel.conkeror_buffer_object)
498 index_of: function (b) {
499 var nodes = this.buffer_list;
500 var count = nodes.length;
501 for (var i = 0; i < count; ++i)
507 get unique_name_list () {
508 var existing_names = new string_hashset();
510 this.for_each(function(b) {
511 var base_name = b.name;
512 var name = base_name;
514 while (existing_names.contains(name)) {
516 name = base_name + "<" + index + ">";
518 existing_names.add(name);
519 bufs.push([name, b]);
524 kill_buffer: function (b) {
527 var count = this.count;
530 var new_buffer = this.buffer_history[0];
532 if (b == new_buffer) {
533 new_buffer = this.buffer_history[1];
536 this._switch_away_from(this.current);
537 // The removeChild call below may trigger events in progress
538 // listeners. This call to `destroy' gives buffer subclasses a
539 // chance to remove such listeners, so that they cannot try to
540 // perform UI actions based upon a xul:browser that no longer
542 var element = b.element;
544 this.container.removeChild(element);
545 this.buffer_list.splice(this.buffer_list.indexOf(b), 1);
546 this.buffer_history.splice(this.buffer_history.indexOf(b), 1);
547 this._switch_to(new_buffer);
549 select_buffer_hook.run(new_buffer);
550 this.buffer_history.splice(this.buffer_history.indexOf(new_buffer), 1);
551 this.buffer_history.unshift(new_buffer);
553 kill_buffer_hook.run(b);
557 bury_buffer: function (b) {
558 var new_buffer = this.buffer_history[0];
560 new_buffer = this.buffer_history[1];
562 throw interactive_error("No other buffer");
563 if (bury_buffer_position != null) {
564 this.buffer_list.splice(this.buffer_list.indexOf(b), 1);
565 this.insert(b, bury_buffer_position, new_buffer);
567 this.buffer_history.splice(this.buffer_history.indexOf(b), 1);
568 this.buffer_history.push(b);
569 this.current = new_buffer;
570 if (bury_buffer_position != null)
571 move_buffer_hook.run(b);
575 for_each: function (f) {
576 var count = this.count;
577 for (var i = 0; i < count; ++i)
578 f(this.get_buffer(i));
582 function buffer_initialize_window_early (window) {
584 * Use content_buffer by default to handle an unusual case where
585 * browser.chromeURI is used perhaps. In general this default
586 * should not be needed.
588 var create_initial_buffer =
589 window.args.initial_buffer_creator || buffer_creator(content_buffer);
590 new buffer_container(window, create_initial_buffer);
593 add_hook("window_initialize_early_hook", buffer_initialize_window_early);
597 * initialize_first_buffer_type is a workaround for a XULRunner bug that
598 * first appeared in version 2.0, manifested as missing scrollbars in the
599 * first buffer of any window. It only affects content-primary browsers,
600 * and the workaround is to initialize the browser as type "content" then
601 * change it to content-primary after a delay.
603 function initialize_first_buffer_type (window) {
604 window.buffers.current.browser.setAttribute("type", "content-primary");
607 add_hook("window_initialize_late_hook", initialize_first_buffer_type);
610 define_buffer_local_hook("buffer_kill_before_hook", RUN_HOOK_UNTIL_FAILURE);
611 function buffer_before_window_close (window) {
612 var bs = window.buffers;
613 var count = bs.count;
614 for (let i = 0; i < count; ++i) {
615 if (!buffer_kill_before_hook.run(bs.get_buffer(i)))
620 add_hook("window_before_close_hook", buffer_before_window_close);
622 function buffer_window_close_handler (window) {
623 var bs = window.buffers;
624 var count = bs.count;
625 for (let i = 0; i < count; ++i) {
626 let b = bs.get_buffer(i);
630 add_hook("window_close_hook", buffer_window_close_handler);
632 /* open/follow targets */
633 const OPEN_CURRENT_BUFFER = 0; // only valid for open if the current
634 // buffer is a content_buffer.
635 const OPEN_NEW_BUFFER = 1;
636 const OPEN_NEW_BUFFER_BACKGROUND = 2;
637 const OPEN_NEW_WINDOW = 3;
639 const FOLLOW_DEFAULT = 4; // for open, implies OPEN_CURRENT_BUFFER
640 const FOLLOW_CURRENT_FRAME = 5; // for open, implies OPEN_CURRENT_BUFFER
642 var TARGET_PROMPTS = [" in current buffer",
644 " in new buffer (background)",
647 " in current frame"];
649 var TARGET_NAMES = ["current buffer",
651 "new buffer (background)",
657 function create_buffer (window, creator, target) {
659 case OPEN_NEW_BUFFER:
660 window.buffers.current = creator(window, null);
662 case OPEN_NEW_BUFFER_BACKGROUND:
663 creator(window, null);
665 case OPEN_NEW_WINDOW:
666 make_window(creator);
669 throw new Error("invalid target");
673 let (queued_buffer_creators = null) {
674 function create_buffer_in_current_window (creator, target, focus_existing) {
675 function process_queued_buffer_creators (window) {
676 for (var i = 0; i < queued_buffer_creators.length; ++i) {
677 var x = queued_buffer_creators[i];
678 create_buffer(window, x[0], x[1]);
680 queued_buffer_creators = null;
683 if (target == OPEN_NEW_WINDOW)
684 throw new Error("invalid target");
685 var window = get_recent_conkeror_window();
689 create_buffer(window, creator, target);
690 } else if (queued_buffer_creators != null) {
691 queued_buffer_creators.push([creator,target]);
693 queued_buffer_creators = [];
694 window = make_window(creator);
695 add_hook.call(window, "window_initialize_late_hook", process_queued_buffer_creators);
704 define_variable("read_buffer_show_icons", false,
705 "Boolean which says whether read_buffer should show buffer "+
706 "icons in the completions list.\nNote, setting this variable "+
707 "alone does not cause favicons or other kinds of icons to be "+
708 "fetched. For that, load the `favicon' (or similar other) "+
711 minibuffer_auto_complete_preferences["buffer"] = true;
712 define_keywords("$default");
713 minibuffer.prototype.read_buffer = function () {
714 var window = this.window;
715 var buffer = this.window.buffers.current;
716 keywords(arguments, $prompt = "Buffer:",
718 $history = "buffer");
719 var completer = all_word_completer(
720 $completions = function (visitor) window.buffers.for_each(visitor),
721 $get_string = function (x) x.description,
722 $get_description = function (x) x.title,
723 $get_icon = (read_buffer_show_icons ?
724 function (x) x.icon : null));
725 var result = yield this.read(
726 $keymap = read_buffer_keymap,
727 $prompt = arguments.$prompt,
728 $history = arguments.$history,
729 $completer = completer,
730 $enable_icons = read_buffer_show_icons,
731 $match_required = true,
732 $auto_complete = "buffer",
733 $auto_complete_initial = true,
734 $auto_complete_delay = 0,
735 $default_completion = arguments.$default);
736 yield co_return(result);
740 function buffer_move_forward (window, count) {
741 var buffers = window.buffers;
742 var index = buffers.selected_index;
743 var buffer = buffers.current
744 var total = buffers.count;
747 var new_index = (index + count) % total;
748 if (new_index == index)
752 buffers.buffer_list.splice(index, 1);
753 buffers.buffer_list.splice(new_index, 0, buffer);
754 move_buffer_hook.run(buffer);
756 interactive("buffer-move-forward",
757 "Move the current buffer forward in the buffer order.",
758 function (I) { buffer_move_forward(I.window, I.p); });
760 interactive("buffer-move-backward",
761 "Move the current buffer backward in the buffer order.",
762 function (I) { buffer_move_forward(I.window, -I.p); });
765 function buffer_next (window, count) {
766 var index = window.buffers.selected_index;
767 var total = window.buffers.count;
769 throw new interactive_error("No other buffer");
770 index = (index + count) % total;
773 window.buffers.current = window.buffers.get_buffer(index);
775 interactive("buffer-next",
776 "Switch to the next buffer.",
777 function (I) { buffer_next(I.window, I.p); });
778 interactive("buffer-previous",
779 "Switch to the previous buffer.",
780 function (I) { buffer_next(I.window, -I.p); });
782 function switch_to_buffer (window, buffer) {
783 if (buffer && !buffer.dead)
784 window.buffers.current = buffer;
786 interactive("switch-to-buffer",
787 "Switch to a buffer specified in the minibuffer.",
791 (yield I.minibuffer.read_buffer(
792 $prompt = "Switch to buffer:",
793 $default = (I.window.buffers.count > 1 ?
794 I.window.buffers.buffer_history[1] :
798 define_variable("can_kill_last_buffer", true,
799 "If this is set to true, kill-buffer can kill the last "+
800 "remaining buffer, and close the window.");
802 function kill_other_buffers (buffer) {
805 var bs = buffer.window.buffers;
807 while ((b = bs.get_buffer(0)) != buffer)
809 var count = bs.count;
811 bs.kill_buffer(bs.get_buffer(1));
813 interactive("kill-other-buffers",
814 "Kill all buffers except current one.\n",
815 function (I) { kill_other_buffers(I.buffer); });
818 function kill_buffer (buffer, force) {
821 var buffers = buffer.window.buffers;
822 if (buffers.count == 1 && buffer == buffers.current) {
823 if (can_kill_last_buffer || force) {
824 delete_window(buffer.window);
827 throw interactive_error("Can't kill last buffer.");
829 buffers.kill_buffer(buffer);
831 interactive("kill-buffer",
832 "Kill a buffer specified in the minibuffer.\n" +
833 "If `can_kill_last_buffer' is set to true, an attempt to kill the "+
834 "last remaining buffer in a window will cause the window to be closed.",
836 kill_buffer((yield I.minibuffer.read_buffer($prompt = "Kill buffer:")));
839 interactive("kill-current-buffer",
840 "Kill the current buffer.\n" +
841 "If `can_kill_last_buffer' is set to true, an attempt to kill the "+
842 "last remaining buffer in a window will cause the window to be closed.",
843 function (I) { kill_buffer(I.buffer); });
845 interactive("read-buffer-kill-buffer",
846 "Kill the current selected buffer in the completions list "+
847 "in a read buffer minibuffer interaction.",
849 var s = I.window.minibuffer.current_state;
850 var i = s.selected_completion_index;
851 var c = s.completions;
854 kill_buffer(c.get_value(i));
855 s.completer.refresh();
856 s.handle_input(I.window.minibuffer);
859 interactive("bury-buffer",
860 "Bury the current buffer.\n Put the current buffer at the end of " +
861 "the buffer list, so that it is the least likely buffer to be " +
862 "selected by `switch-to-buffer'.",
863 function (I) { I.window.buffers.bury_buffer(I.buffer); });
865 function change_directory (buffer, dir) {
866 if (buffer.page != null)
867 delete buffer.page.local.cwd;
868 buffer.local.cwd = make_file(dir);
870 interactive("change-directory",
871 "Change the current directory of the selected buffer.",
875 (yield I.minibuffer.read_existing_directory_path(
876 $prompt = "Change to directory:",
877 $initial_value = make_file(I.local.cwd).path)));
880 interactive("shell-command", null,
882 var cwd = I.local.cwd;
883 var cmd = (yield I.minibuffer.read_shell_command($cwd = cwd));
884 yield shell_command(cmd, $cwd = cwd);
889 * selection_is_embed_p is used to test whether the unfocus command can
890 * unfocus an element, even though there is a selection. This happens
891 * when the focused element is an html:embed.
893 function selection_is_embed_p (sel, focused_element) {
894 if (sel.rangeCount == 1) {
896 var r = sel.getRangeAt(0);
897 var a = r.startContainer.childNodes[r.startOffset];
898 if ((a instanceof Ci.nsIDOMHTMLEmbedElement ||
899 a instanceof Ci.nsIDOMHTMLObjectElement) &&
900 a == focused_element)
910 * unfocus is a high-level command for unfocusing hyperlinks, inputs,
911 * frames, iframes, plugins, and also clearing the selection.
913 define_buffer_local_hook("unfocus_hook");
914 function unfocus (window, buffer) {
915 // 1. if there is a selection, clear it.
916 var selc = buffer.focused_selection_controller;
918 var sel = selc.getSelection(selc.SELECTION_NORMAL);
919 var active = ! sel.isCollapsed;
920 var embed_p = selection_is_embed_p(sel, buffer.focused_element);
921 clear_selection(buffer);
922 if (active && !embed_p) {
923 window.minibuffer.message("cleared selection");
927 // 2. if there is a focused element, unfocus it.
928 if (buffer.focused_element) {
929 buffer.focused_element.blur();
930 // if an element in a detached fragment has focus, blur() will
931 // not work, and we need to take more drastic measures. the
932 // action taken was found through experiment, so it is possibly
933 // not the most concise way to unfocus such an element.
934 if (buffer.focused_element) {
935 buffer.element.focus();
936 buffer.top_frame.focus();
938 window.minibuffer.message("unfocused element");
941 // 3. if an iframe has focus, we must blur it.
942 if (buffer.focused_frame_or_null &&
943 buffer.focused_frame_or_null.frameElement)
945 buffer.focused_frame_or_null.frameElement.blur();
947 // 4. return focus to top-frame from subframes and plugins.
948 buffer.top_frame.focus();
949 buffer.top_frame.focus(); // needed to get focus back from plugins
950 window.minibuffer.message("refocused top frame");
951 // give page-modes an opportunity to set focus specially
952 unfocus_hook.run(buffer);
954 interactive("unfocus",
955 "Unfocus is a high-level command for unfocusing hyperlinks, inputs, "+
956 "frames, iframes, plugins, and also for clearing the selection.\n"+
957 "The action that it takes is based on precedence. If there is a "+
958 "focused hyperlink or input, it will unfocus that. Otherwise, if "+
959 "there is a selection, it will clear the selection. Otherwise, it "+
960 "will return focus to the top frame from a focused frame, iframe, "+
961 "or plugin. In the case of plugins, since they steal keyboard "+
962 "control away from Conkeror, the normal way to unfocus them is "+
963 "to use command-line remoting externally: conkeror -batch -f "+
964 "unfocus. Page-modes also have an opportunity to alter the default"+
965 "focus via the hook, `focus_hook'.",
967 unfocus(I.window, I.buffer);
971 function for_each_buffer (f) {
972 for_each_window(function (w) { w.buffers.for_each(f); });
980 define_buffer_local_hook("buffer_mode_change_hook");
981 define_current_buffer_hook("current_buffer_mode_change_hook", "buffer_mode_change_hook");
983 define_keywords("$display_name", "$doc");
984 function buffer_mode (name, enable, disable) {
986 this.name = name.replace("-","_","g");
987 this.hyphen_name = name.replace("_","-","g");
989 this._enable = enable;
991 this._disable = disable;
992 this.display_name = arguments.$display_name;
993 this.doc = arguments.$doc;
994 this.enable_hook = this.name + "_enable_hook";
995 this.disable_hook = this.name + "_disable_hook";
997 buffer_mode.prototype = {
998 constructor: buffer_mode,
1006 enable: function (buffer) {
1009 this._enable(buffer);
1011 buffer.enabled_modes.push(this.name);
1012 if (conkeror[this.enable_hook])
1013 conkeror[this.enable_hook].run(buffer);
1014 buffer_mode_change_hook.run(buffer);
1017 disable: function (buffer) {
1020 this._disable(buffer);
1022 var i = buffer.enabled_modes.indexOf(this.name);
1024 buffer.enabled_modes.splice(i, 1);
1025 if (conkeror[this.disable_hook])
1026 conkeror[this.disable_hook].run(buffer);
1027 buffer_mode_change_hook.run(buffer);
1031 define_keywords("$constructor");
1032 function define_buffer_mode (name, enable, disable) {
1033 keywords(arguments, $constructor = buffer_mode, $doc = null);
1034 var constructor = arguments.$constructor;
1035 var m = new constructor(name, enable, disable, forward_keywords(arguments));
1036 name = m.name; // normalized
1038 define_buffer_local_hook(m.enable_hook);
1039 define_buffer_local_hook(m.disable_hook);
1040 interactive(m.hyphen_name,
1043 var enabledp = (I.buffer.enabled_modes.indexOf(name) > -1);
1045 m.disable(I.buffer);
1048 I.minibuffer.message(m.hyphen_name + (enabledp ? " disabled" : " enabled"));
1051 ignore_function_for_get_caller_source_code_reference("define_buffer_mode");
1055 * Mode Display in Minibuffer
1058 function minibuffer_mode_indicator (window) {
1059 this.window = window;
1060 var element = create_XUL(window, "label");
1061 element.setAttribute("id", "minibuffer-mode-indicator");
1062 element.setAttribute("class", "mode-text-widget");
1063 window.document.getElementById("minibuffer").appendChild(element);
1064 this.element = element;
1065 this.hook_function = method_caller(this, this.update);
1066 add_hook.call(window, "select_buffer_hook", this.hook_function);
1067 add_hook.call(window, "current_buffer_mode_change_hook", this.hook_function);
1070 minibuffer_mode_indicator.prototype = {
1071 constructor: minibuffer_mode_indicator,
1074 hook_function: null,
1075 update: function () {
1076 var buffer = this.window.buffers.current;
1077 var str = buffer.enabled_modes.map(
1079 return (conkeror[x].display_name || null);
1080 }).filter(function (x) x != null).join(" ");
1081 this.element.value = str;
1083 uninstall: function () {
1084 remove_hook.call(this.window, "select_buffer_hook", this.hook_function);
1085 remove_hook.call(this.window, "current_buffer_mode_change_hook", this.hook_function);
1086 this.element.parentNode.removeChild(this.element);
1089 define_global_window_mode("minibuffer_mode_indicator", "window_initialize_hook");
1090 minibuffer_mode_indicator_mode(true);
1094 * minibuffer-keymaps-display
1096 function minibuffer_keymaps_display_update (buffer) {
1097 var element = buffer.window.document
1098 .getElementById("keymaps-display");
1100 var str = buffer.keymaps.reduce(
1101 function (acc, kmap) {
1102 if (kmap.display_name)
1103 acc.push(kmap.display_name);
1106 if (element.value != str)
1107 element.value = str;
1111 function minibuffer_keymaps_display_initialize (window) {
1112 var element = create_XUL(window, "label");
1113 element.setAttribute("id", "keymaps-display");
1114 element.setAttribute("class", "mode-text-widget");
1115 element.setAttribute("value", "");
1116 var mb = window.document.getElementById("minibuffer");
1117 mb.appendChild(element);
1120 define_global_mode("minibuffer_keymaps_display_mode",
1121 function enable () {
1122 add_hook("window_initialize_hook", minibuffer_keymaps_display_initialize);
1123 add_hook("set_input_mode_hook", minibuffer_keymaps_display_update);
1124 for_each_window(minibuffer_keymaps_display_initialize);
1126 function disable () {
1127 remove_hook("window_initialize_hook", minibuffer_keymaps_display_initialize);
1128 remove_hook("set_input_mode_hook", minibuffer_keymaps_display_update);
1129 for_each_window(function (w) {
1130 var element = w.document
1131 .getElementById("keymaps-display");
1133 element.parentNode.removeChild(element);
1137 minibuffer_keymaps_display_mode(true);
1141 * minibuffer-keymaps-highlight
1143 function minibuffer_keymaps_highlight_update (buffer) {
1144 var mb = buffer.window.document.getElementById("minibuffer");
1145 if (buffer.keymaps.some(function (k) k.notify))
1146 dom_add_class(mb, "highlight");
1148 dom_remove_class(mb, "highlight");
1151 define_global_mode("minibuffer_keymaps_highlight_mode",
1152 function enable () {
1153 add_hook("set_input_mode_hook", minibuffer_keymaps_highlight_update);
1155 function disable () {
1156 remove_hook("set_input_mode_hook", minibuffer_keymaps_highlight_update);
1157 for_each_window(function (w) {
1158 var mb = w.document.getElementById("minibuffer");
1160 dom_remove_class("highlight");
1164 minibuffer_keymaps_highlight_mode(true);