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 for_each: function (f) {
581 var count = this.count;
582 for (var i = 0; i < count; ++i)
583 f(this.get_buffer(i));
587 function buffer_initialize_window_early (window) {
589 * Use content_buffer by default to handle an unusual case where
590 * browser.chromeURI is used perhaps. In general this default
591 * should not be needed.
593 var create_initial_buffer =
594 window.args.initial_buffer_creator || buffer_creator(content_buffer);
595 new buffer_container(window, create_initial_buffer);
598 add_hook("window_initialize_early_hook", buffer_initialize_window_early);
602 * initialize_first_buffer_type is a workaround for a XULRunner bug that
603 * first appeared in version 2.0, manifested as missing scrollbars in the
604 * first buffer of any window. It only affects content-primary browsers,
605 * and the workaround is to initialize the browser as type "content" then
606 * change it to content-primary after a delay.
608 function initialize_first_buffer_type (window) {
609 window.buffers.current.browser.setAttribute("type", "content-primary");
612 add_hook("window_initialize_late_hook", initialize_first_buffer_type);
615 define_buffer_local_hook("buffer_kill_before_hook", RUN_HOOK_UNTIL_FAILURE);
616 function buffer_before_window_close (window) {
617 var bs = window.buffers;
618 var count = bs.count;
619 for (let i = 0; i < count; ++i) {
620 if (!buffer_kill_before_hook.run(bs.get_buffer(i)))
625 add_hook("window_before_close_hook", buffer_before_window_close);
627 function buffer_window_close_handler (window) {
628 var bs = window.buffers;
629 var count = bs.count;
630 for (let i = 0; i < count; ++i) {
631 let b = bs.get_buffer(i);
635 add_hook("window_close_hook", buffer_window_close_handler);
637 /* open/follow targets */
638 const OPEN_CURRENT_BUFFER = 0; // only valid for open if the current
639 // buffer is a content_buffer.
640 const OPEN_NEW_BUFFER = 1;
641 const OPEN_NEW_BUFFER_BACKGROUND = 2;
642 const OPEN_NEW_WINDOW = 3;
644 const FOLLOW_DEFAULT = 4; // for open, implies OPEN_CURRENT_BUFFER
645 const FOLLOW_CURRENT_FRAME = 5; // for open, implies OPEN_CURRENT_BUFFER
647 var TARGET_PROMPTS = [" in current buffer",
649 " in new buffer (background)",
652 " in current frame"];
654 var TARGET_NAMES = ["current buffer",
656 "new buffer (background)",
662 function create_buffer (window, creator, target) {
664 case OPEN_NEW_BUFFER:
665 window.buffers.current = creator(window, null);
667 case OPEN_NEW_BUFFER_BACKGROUND:
668 creator(window, null);
670 case OPEN_NEW_WINDOW:
671 make_window(creator);
674 throw new Error("invalid target");
678 let (queued_buffer_creators = null) {
679 function create_buffer_in_current_window (creator, target, focus_existing) {
680 function process_queued_buffer_creators (window) {
681 for (var i = 0; i < queued_buffer_creators.length; ++i) {
682 var x = queued_buffer_creators[i];
683 create_buffer(window, x[0], x[1]);
685 queued_buffer_creators = null;
688 if (target == OPEN_NEW_WINDOW)
689 throw new Error("invalid target");
690 var window = get_recent_conkeror_window();
694 create_buffer(window, creator, target);
695 } else if (queued_buffer_creators != null) {
696 queued_buffer_creators.push([creator,target]);
698 queued_buffer_creators = [];
699 window = make_window(creator);
700 add_hook.call(window, "window_initialize_late_hook", process_queued_buffer_creators);
709 define_variable("read_buffer_show_icons", false,
710 "Boolean which says whether read_buffer should show buffer "+
711 "icons in the completions list.\nNote, setting this variable "+
712 "alone does not cause favicons or other kinds of icons to be "+
713 "fetched. For that, load the `favicon' (or similar other) "+
716 minibuffer_auto_complete_preferences["buffer"] = true;
717 define_keywords("$default");
718 minibuffer.prototype.read_buffer = function () {
719 var window = this.window;
720 var buffer = this.window.buffers.current;
721 keywords(arguments, $prompt = "Buffer:",
723 $history = "buffer");
724 var completer = all_word_completer(
725 $completions = function (visitor) window.buffers.for_each(visitor),
726 $get_string = function (x) x.description,
727 $get_description = function (x) x.title,
728 $get_icon = (read_buffer_show_icons ?
729 function (x) x.icon : null));
730 var result = yield this.read(
731 $keymap = read_buffer_keymap,
732 $prompt = arguments.$prompt,
733 $history = arguments.$history,
734 $completer = completer,
735 $enable_icons = read_buffer_show_icons,
736 $match_required = true,
737 $auto_complete = "buffer",
738 $auto_complete_initial = true,
739 $auto_complete_delay = 0,
740 $default_completion = arguments.$default);
741 yield co_return(result);
745 function buffer_move_forward (window, count) {
746 var buffers = window.buffers;
747 var index = buffers.selected_index;
748 var buffer = buffers.current
749 var total = buffers.count;
752 var new_index = (index + count) % total;
753 if (new_index == index)
757 buffers.buffer_list.splice(index, 1);
758 buffers.buffer_list.splice(new_index, 0, buffer);
759 move_buffer_hook.run(buffer);
761 interactive("buffer-move-forward",
762 "Move the current buffer forward in the buffer order.",
763 function (I) { buffer_move_forward(I.window, I.p); });
765 interactive("buffer-move-backward",
766 "Move the current buffer backward in the buffer order.",
767 function (I) { buffer_move_forward(I.window, -I.p); });
770 function buffer_next (window, count) {
771 var index = window.buffers.selected_index;
772 var total = window.buffers.count;
774 throw new interactive_error("No other buffer");
775 index = (index + count) % total;
778 window.buffers.current = window.buffers.get_buffer(index);
780 interactive("buffer-next",
781 "Switch to the next buffer.",
782 function (I) { buffer_next(I.window, I.p); });
783 interactive("buffer-previous",
784 "Switch to the previous buffer.",
785 function (I) { buffer_next(I.window, -I.p); });
787 function switch_to_buffer (window, buffer) {
788 if (buffer && !buffer.dead)
789 window.buffers.current = buffer;
791 interactive("switch-to-buffer",
792 "Switch to a buffer specified in the minibuffer.",
796 (yield I.minibuffer.read_buffer(
797 $prompt = "Switch to buffer:",
798 $default = (I.window.buffers.count > 1 ?
799 I.window.buffers.buffer_history[1] :
803 define_variable("can_kill_last_buffer", true,
804 "If this is set to true, kill-buffer can kill the last "+
805 "remaining buffer, and close the window.");
807 function kill_other_buffers (buffer) {
810 var bs = buffer.window.buffers;
812 while ((b = bs.get_buffer(0)) != buffer)
814 var count = bs.count;
816 bs.kill_buffer(bs.get_buffer(1));
818 interactive("kill-other-buffers",
819 "Kill all buffers except current one.\n",
820 function (I) { kill_other_buffers(I.buffer); });
823 function kill_buffer (buffer, force) {
826 var buffers = buffer.window.buffers;
827 if (buffers.count == 1 && buffer == buffers.current) {
828 if (can_kill_last_buffer || force) {
829 delete_window(buffer.window);
832 throw interactive_error("Can't kill last buffer.");
834 buffers.kill_buffer(buffer);
836 interactive("kill-buffer",
837 "Kill a buffer specified in the minibuffer.\n" +
838 "If `can_kill_last_buffer' is set to true, an attempt to kill the "+
839 "last remaining buffer in a window will cause the window to be closed.",
841 kill_buffer((yield I.minibuffer.read_buffer($prompt = "Kill buffer:")));
844 interactive("kill-current-buffer",
845 "Kill the current buffer.\n" +
846 "If `can_kill_last_buffer' is set to true, an attempt to kill the "+
847 "last remaining buffer in a window will cause the window to be closed.",
848 function (I) { kill_buffer(I.buffer); });
850 interactive("read-buffer-kill-buffer",
851 "Kill the current selected buffer in the completions list "+
852 "in a read buffer minibuffer interaction.",
854 var s = I.window.minibuffer.current_state;
855 var i = s.selected_completion_index;
856 var c = s.completions;
859 kill_buffer(c.get_value(i));
860 s.completer.refresh();
861 s.handle_input(I.window.minibuffer);
864 interactive("bury-buffer",
865 "Bury the current buffer.\n Put the current buffer at the end of " +
866 "the buffer list, so that it is the least likely buffer to be " +
867 "selected by `switch-to-buffer'.",
868 function (I) { I.window.buffers.bury_buffer(I.buffer); });
870 function change_directory (buffer, dir) {
871 if (buffer.page != null)
872 delete buffer.page.local.cwd;
873 buffer.local.cwd = make_file(dir);
875 interactive("change-directory",
876 "Change the current directory of the selected buffer.",
880 (yield I.minibuffer.read_existing_directory_path(
881 $prompt = "Change to directory:",
882 $initial_value = make_file(I.local.cwd).path)));
885 interactive("shell-command", null,
887 var cwd = I.local.cwd;
888 var cmd = (yield I.minibuffer.read_shell_command($cwd = cwd));
889 yield shell_command(cmd, $cwd = cwd);
894 * selection_is_embed_p is used to test whether the unfocus command can
895 * unfocus an element, even though there is a selection. This happens
896 * when the focused element is an html:embed.
898 function selection_is_embed_p (sel, focused_element) {
899 if (sel.rangeCount == 1) {
901 var r = sel.getRangeAt(0);
902 var a = r.startContainer.childNodes[r.startOffset];
903 if ((a instanceof Ci.nsIDOMHTMLEmbedElement ||
904 a instanceof Ci.nsIDOMHTMLObjectElement) &&
905 a == focused_element)
915 * unfocus is a high-level command for unfocusing hyperlinks, inputs,
916 * frames, iframes, plugins, and also clearing the selection.
918 define_buffer_local_hook("unfocus_hook");
919 function unfocus (window, buffer) {
920 // 1. if there is a selection, clear it.
921 var selc = buffer.focused_selection_controller;
923 var sel = selc.getSelection(selc.SELECTION_NORMAL);
924 var active = ! sel.isCollapsed;
925 var embed_p = selection_is_embed_p(sel, buffer.focused_element);
926 clear_selection(buffer);
927 if (active && !embed_p) {
928 window.minibuffer.message("cleared selection");
932 // 2. if there is a focused element, unfocus it.
933 if (buffer.focused_element) {
934 buffer.focused_element.blur();
935 // if an element in a detached fragment has focus, blur() will
936 // not work, and we need to take more drastic measures. the
937 // action taken was found through experiment, so it is possibly
938 // not the most concise way to unfocus such an element.
939 if (buffer.focused_element) {
940 buffer.element.focus();
941 buffer.top_frame.focus();
943 window.minibuffer.message("unfocused element");
946 // 3. if an iframe has focus, we must blur it.
947 if (buffer.focused_frame_or_null &&
948 buffer.focused_frame_or_null.frameElement)
950 buffer.focused_frame_or_null.frameElement.blur();
952 // 4. return focus to top-frame from subframes and plugins.
953 buffer.top_frame.focus();
954 buffer.top_frame.focus(); // needed to get focus back from plugins
955 window.minibuffer.message("refocused top frame");
956 // give page-modes an opportunity to set focus specially
957 unfocus_hook.run(buffer);
959 interactive("unfocus",
960 "Unfocus is a high-level command for unfocusing hyperlinks, inputs, "+
961 "frames, iframes, plugins, and also for clearing the selection.\n"+
962 "The action that it takes is based on precedence. If there is a "+
963 "focused hyperlink or input, it will unfocus that. Otherwise, if "+
964 "there is a selection, it will clear the selection. Otherwise, it "+
965 "will return focus to the top frame from a focused frame, iframe, "+
966 "or plugin. In the case of plugins, since they steal keyboard "+
967 "control away from Conkeror, the normal way to unfocus them is "+
968 "to use command-line remoting externally: conkeror -batch -f "+
969 "unfocus. Page-modes also have an opportunity to alter the default"+
970 "focus via the hook, `focus_hook'.",
972 unfocus(I.window, I.buffer);
976 function for_each_buffer (f) {
977 for_each_window(function (w) { w.buffers.for_each(f); });
985 define_buffer_local_hook("buffer_mode_change_hook");
986 define_current_buffer_hook("current_buffer_mode_change_hook", "buffer_mode_change_hook");
988 define_keywords("$display_name", "$doc");
989 function buffer_mode (name, enable, disable) {
991 this.name = name.replace("-","_","g");
992 this.hyphen_name = name.replace("_","-","g");
994 this._enable = enable;
996 this._disable = disable;
997 this.display_name = arguments.$display_name;
998 this.doc = arguments.$doc;
999 this.enable_hook = this.name + "_enable_hook";
1000 this.disable_hook = this.name + "_disable_hook";
1002 buffer_mode.prototype = {
1003 constructor: buffer_mode,
1011 enable: function (buffer) {
1014 this._enable(buffer);
1016 buffer.enabled_modes.push(this.name);
1017 if (conkeror[this.enable_hook])
1018 conkeror[this.enable_hook].run(buffer);
1019 buffer_mode_change_hook.run(buffer);
1022 disable: function (buffer) {
1025 this._disable(buffer);
1027 var i = buffer.enabled_modes.indexOf(this.name);
1029 buffer.enabled_modes.splice(i, 1);
1030 if (conkeror[this.disable_hook])
1031 conkeror[this.disable_hook].run(buffer);
1032 buffer_mode_change_hook.run(buffer);
1036 define_keywords("$constructor");
1037 function define_buffer_mode (name, enable, disable) {
1038 keywords(arguments, $constructor = buffer_mode, $doc = null);
1039 var constructor = arguments.$constructor;
1040 var m = new constructor(name, enable, disable, forward_keywords(arguments));
1041 name = m.name; // normalized
1043 define_buffer_local_hook(m.enable_hook);
1044 define_buffer_local_hook(m.disable_hook);
1045 interactive(m.hyphen_name,
1048 var enabledp = (I.buffer.enabled_modes.indexOf(name) > -1);
1050 m.disable(I.buffer);
1053 I.minibuffer.message(m.hyphen_name + (enabledp ? " disabled" : " enabled"));
1056 ignore_function_for_get_caller_source_code_reference("define_buffer_mode");
1060 * Mode Display in Minibuffer
1063 function minibuffer_mode_indicator (window) {
1064 this.window = window;
1065 var element = create_XUL(window, "label");
1066 element.setAttribute("id", "minibuffer-mode-indicator");
1067 element.setAttribute("class", "mode-text-widget");
1068 window.document.getElementById("minibuffer").appendChild(element);
1069 this.element = element;
1070 this.hook_function = method_caller(this, this.update);
1071 add_hook.call(window, "select_buffer_hook", this.hook_function);
1072 add_hook.call(window, "current_buffer_mode_change_hook", this.hook_function);
1075 minibuffer_mode_indicator.prototype = {
1076 constructor: minibuffer_mode_indicator,
1079 hook_function: null,
1080 update: function () {
1081 var buffer = this.window.buffers.current;
1082 var str = buffer.enabled_modes.map(
1084 return (conkeror[x].display_name || null);
1085 }).filter(function (x) x != null).join(" ");
1086 this.element.value = str;
1088 uninstall: function () {
1089 remove_hook.call(this.window, "select_buffer_hook", this.hook_function);
1090 remove_hook.call(this.window, "current_buffer_mode_change_hook", this.hook_function);
1091 this.element.parentNode.removeChild(this.element);
1094 define_global_window_mode("minibuffer_mode_indicator", "window_initialize_hook");
1095 minibuffer_mode_indicator_mode(true);
1099 * minibuffer-keymaps-display
1101 function minibuffer_keymaps_display_update (buffer) {
1102 var element = buffer.window.document
1103 .getElementById("keymaps-display");
1105 var str = buffer.keymaps.reduce(
1106 function (acc, kmap) {
1107 if (kmap.display_name)
1108 acc.push(kmap.display_name);
1111 if (element.value != str)
1112 element.value = str;
1116 function minibuffer_keymaps_display_initialize (window) {
1117 var element = create_XUL(window, "label");
1118 element.setAttribute("id", "keymaps-display");
1119 element.setAttribute("class", "mode-text-widget");
1120 element.setAttribute("value", "");
1121 var mb = window.document.getElementById("minibuffer");
1122 mb.appendChild(element);
1125 define_global_mode("minibuffer_keymaps_display_mode",
1126 function enable () {
1127 add_hook("window_initialize_hook", minibuffer_keymaps_display_initialize);
1128 add_hook("set_input_mode_hook", minibuffer_keymaps_display_update);
1129 for_each_window(minibuffer_keymaps_display_initialize);
1131 function disable () {
1132 remove_hook("window_initialize_hook", minibuffer_keymaps_display_initialize);
1133 remove_hook("set_input_mode_hook", minibuffer_keymaps_display_update);
1134 for_each_window(function (w) {
1135 var element = w.document
1136 .getElementById("keymaps-display");
1138 element.parentNode.removeChild(element);
1142 minibuffer_keymaps_display_mode(true);
1146 * minibuffer-keymaps-highlight
1148 function minibuffer_keymaps_highlight_update (buffer) {
1149 var mb = buffer.window.document.getElementById("minibuffer");
1150 if (buffer.keymaps.some(function (k) k.notify))
1151 dom_add_class(mb, "highlight");
1153 dom_remove_class(mb, "highlight");
1156 define_global_mode("minibuffer_keymaps_highlight_mode",
1157 function enable () {
1158 add_hook("set_input_mode_hook", minibuffer_keymaps_highlight_update);
1160 function disable () {
1161 remove_hook("set_input_mode_hook", minibuffer_keymaps_highlight_update);
1162 for_each_window(function (w) {
1163 var mb = w.document.getElementById("minibuffer");
1165 dom_remove_class("highlight");
1169 minibuffer_keymaps_highlight_mode(true);