2 * (C) Copyright 2004-2007 Shawn Betts
3 * (C) Copyright 2007-2008 John J. Foerch
4 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
6 * Use, modification, and distribution are subject to the terms specified in the
10 require("content-buffer.js");
12 define_hook("quit_hook");
16 var appStartup = Cc["@mozilla.org/toolkit/app-startup;1"]
17 .getService(Ci.nsIAppStartup);
18 appStartup.quit(appStartup.eAttemptQuit);
24 interactive("confirm-quit",
25 "Quit Conkeror with confirmation",
27 let result = yield I.window.minibuffer.read_single_character_option(
28 $prompt = "Quit Conkeror? (y/n)",
29 $options = ["y", "n"]);
34 function show_conkeror_version (window) {
35 window.minibuffer.message(conkeror.version);
37 interactive("conkeror-version",
38 "Show version information for Conkeror.",
39 function (I) { show_conkeror_version(I.window); });
40 interactive("version",
41 "Show version information for Conkeror.",
44 /* FIXME: maybe this should be supported for non-browser buffers */
45 function scroll_horiz_complete (buffer, n) {
46 var w = buffer.focused_frame;
47 w.scrollTo (n > 0 ? w.scrollMaxX : 0, w.scrollY);
49 interactive("scroll-beginning-of-line",
50 "Scroll the current frame all the way to the left.",
51 function (I) { scroll_horiz_complete(I.buffer, -1); });
53 interactive("scroll-end-of-line",
54 "Scroll the current frame all the way to the right.",
55 function (I) { scroll_horiz_complete(I.buffer, 1); });
57 function delete_window (window) {
58 window.window.close();
60 interactive("delete-window",
61 "Delete the current window.",
62 function (I) { delete_window(I.window); });
64 interactive("jsconsole",
65 "Open the JavaScript console.",
66 "find-url-new-buffer",
67 $browser_object = "chrome://global/content/console.xul");
70 * Given a callback func and an interactive context I, call func, passing either
71 * a focused field, or the minibuffer's input element if the minibuffer is
72 * active. Afterward, call `ensure_index_is_visible' on the field. See
73 * `paste_x_primary_selection' and `open_line' for examples.
75 function call_on_focused_field (I, func) {
76 var m = I.window.minibuffer;
77 var s = m.current_state;
78 if (m._input_mode_enabled) {
79 m._restore_normal_state();
80 var e = m.input_element;
82 var e = I.buffer.focused_element;
84 ensure_index_is_visible(I.window, e, e.selectionStart);
85 if (s && s.handle_input)
90 * Replace the current region with modifier(selection). Deactivates region and
91 * sets point to the end of the inserted text, unless keep_point is true, in
92 * which case the point will be left at the beginning of the inserted text.
94 function modify_region (field, modifier, keep_point) {
96 modifier(field.value.substring(field.selectionStart, field.selectionEnd+1));
97 var point = field.selectionStart;
99 field.value.substr(0, field.selectionStart) + replacement +
100 field.value.substr(field.selectionEnd);
101 if (!keep_point) point += replacement.length;
102 field.setSelectionRange(point, point);
105 function paste_x_primary_selection (field) {
106 modify_region(field, function (str) read_from_x_primary_selection());
108 interactive("paste-x-primary-selection",
109 "Insert the contents of the X primary selection into the selected field or "+
110 "minibuffer. Deactivates the region if it is active, and leaves the point "+
111 "after the inserted text.",
112 function (I) call_on_focused_field(I, paste_x_primary_selection));
114 function open_line (field) {
115 modify_region(field, function() "\n", true);
118 interactive("open-line",
119 "If there is an active region, replace is with a newline, otherwise just "+
120 "insert a newline. In both cases leave point before the inserted newline.",
121 function (I) call_on_focused_field(I, open_line));
123 function transpose_chars (field) {
124 var value = field.value;
125 var caret = field.selectionStart; // Caret position.
126 var length = value.length;
128 // If we have less than two character in the field or if we are at the
129 // beginning of the field, do nothing.
130 if (length <= 2 || caret == 0)
133 // If we are at the end of the field, switch places on the two last
134 // characters. TODO: This should happen at the end of every line, not only
135 // at the end of the field.
139 // Do the transposing.
140 field.value = switch_subarrays(value, caret - 1, caret, caret, caret + 1);
142 // Increment the caret position. If this is not done, the caret is left at
143 // the end of the field as a result of the replacing of contents.
144 field.selectionStart = caret + 1;
145 field.selectionEnd = caret + 1;
148 interactive("transpose-chars",
149 "Interchange characters around point, moving forward one character.",
150 function (I) call_on_focused_field(I, transpose_chars));
152 function meta_x (buffer, prefix, command, browser_object) {
153 var I = new interactive_context(buffer);
154 I.prefix_argument = prefix;
155 I.browser_object = browser_object;
156 call_interactively(I, command);
158 interactive("execute-extended-command",
159 "Execute a Conkeror command specified in the minibuffer.",
162 var boc = I.browser_object;
165 prompt += ' ['+boc.name+']';
166 if (prefix !== null && prefix !== undefined) {
167 if (typeof prefix == "object")
168 prompt += prefix[0] == 4 ? " C-u" : " "+prefix[0];
170 prompt += " "+prefix;
172 meta_x(I.buffer, I.P,
173 (yield I.minibuffer.read_command(
174 $prompt = "M-x" + prompt)),
178 /// built in commands
179 // see: http://www.xulplanet.com/tutorials/xultu/commandupdate.html
181 // Performs a command on a browser buffer content area
184 define_builtin_commands(
186 function (I, command) {
187 var buffer = I.buffer;
189 buffer.do_command(command);
191 /* Ignore exceptions */
195 I.buffer.mark_active = !I.buffer.mark_active;
197 function (I) I.buffer.mark_active,
201 define_builtin_commands(
203 function (I, command) {
204 var buffer = I.buffer;
206 buffer.do_command(command);
208 /* Ignore exceptions */
212 I.buffer.mark_active = !I.buffer.mark_active;
214 function (I) I.buffer.mark_active,
217 function get_link_text () {
218 var e = document.commandDispatcher.focusedElement;
219 if (e && e.getAttribute("href")) {
220 return e.getAttribute("href");
227 function copy_email_address (loc)
229 // Copy the comma-separated list of email addresses only.
230 // There are other ways of embedding email addresses in a mailto:
231 // link, but such complex parsing is beyond us.
232 var qmark = loc.indexOf( "?" );
235 if ( qmark > 7 ) { // 7 == length of "mailto:"
236 addresses = loc.substring( 7, qmark );
238 addresses = loc.substr( 7 );
241 //XXX: the original code, which we got from firefox, unescapes the string
242 // using the current character set. To do this in conkeror, we
243 // *should* use an interactive method that gives us the character set,
244 // rather than fetching it by side-effect.
246 // // Let's try to unescape it using a character set
247 // // in case the address is not ASCII.
249 // var characterSet = this.target.ownerDocument.characterSet;
250 // const textToSubURI = Components.classes["@mozilla.org/intl/texttosuburi;1"]
251 // .getService(Components.interfaces.nsITextToSubURI);
252 // addresses = textToSubURI.unEscapeURIForUI(characterSet, addresses);
258 writeToClipboard(addresses);
259 message("Copied '" + addresses + "'");
261 interactive("copy-email-address", copy_email_address, ['focused_link_url']);
264 /* FIXME: fix this command */
266 interactive("source",
267 "Load a JavaScript file.",
268 function (fo) { load_rc (fo.path); }, [['f', function (a) { return "Source File: "; }, null, "source"]]);
270 function reinit (window) {
274 window.minibuffer.message("Loaded: " + path);
276 window.minibuffer.message("Failed to load: "+path);
280 interactive("reinit",
281 "Reload the Conkeror rc file.",
282 function (I) { reinit(I.window); });
284 interactive("help-page", "Open the Conkeror help page.",
285 "find-url-new-buffer",
286 $browser_object = "chrome://conkeror-help/content/help.html");
288 interactive("help-with-tutorial", "Open the Conkeror tutorial.",
289 "find-url-new-buffer",
290 $browser_object = "chrome://conkeror-help/content/tutorial.html");
292 function univ_arg_to_number (prefix, default_value) {
293 if (prefix == null) {
294 if (default_value == null)
297 return default_value;
299 if (typeof prefix == "object")
304 function eval_expression (window, s) {
305 // eval in the global scope.
307 // In addition, the following variables are available:
309 var buffer = window.buffers.current;
310 var result = eval(s);
311 if (result !== undefined) {
312 window.minibuffer.message(String(result));
315 interactive("eval-expression",
316 "Evaluate JavaScript statements.",
320 (yield I.minibuffer.read($prompt = "Eval:",
321 $history = "eval-expression",
322 $completer = javascript_completer(I.buffer))));
326 function show_extension_manager () {
327 return conkeror.window_watcher.openWindow(
329 "chrome://mozapps/content/extensions/extensions.xul?type=extensions",
331 "resizable=yes,dialog=no",
334 interactive("extensions",
335 "Open the extensions manager in a new window.",
336 show_extension_manager);
338 function print_buffer (buffer) {
339 buffer.top_frame.print();
341 interactive("print-buffer",
342 "Print the currently loaded page.",
343 function (I) { print_buffer(I.buffer); });
345 function view_partial_source (window, charset, selection) {
347 charset = "charset=" + charset;
348 window.window.openDialog("chrome://global/content/viewPartialSource.xul",
349 "_blank", "scrollbars,resizable,chrome,dialog=no",
350 null, charset, selection, 'selection');
352 //interactive ('view-partial-source', view_partial_source, I.current_window, I.content_charset, I.content_selection);
355 function view_mathml_source (window, charset, target) {
357 charset = "charset=" + charset;
358 window.window.openDialog("chrome://global/content/viewPartialSource.xul",
359 "_blank", "scrollbars,resizable,chrome,dialog=no",
360 null, charset, target, 'mathml');
364 function send_key_as_event (window, element, combo) {
365 var split = unformat_key_combo(combo);
366 var event = window.document.createEvent("KeyboardEvent");
379 return element.dispatchEvent (event);
381 return window.dispatchEvent (event);
384 interactive ("send-ret",
387 send_key_as_event(I.window, I.buffer.focused_element, "return");
390 function ensure_content_focused (buffer) {
391 var foc = buffer.focused_frame_or_null;
393 buffer.top_frame.focus();
395 interactive("ensure-content-focused", "Ensure that the content document has focus.",
396 function (I) { ensure_content_focused(I.buffer); });
398 function network_set_online_status (status) {
400 io_service.manageOfflineStatus = false;
401 io_service.offline = status;
404 interactive("network-go-online", "Work online.",
405 function (I) { network_set_online_status (true); });
406 interactive("network-go-offline", "Work offline.",
407 function (I) { network_set_online_status (false); });
410 interactive("submit-form",
411 "Submit the form to which the focused element belongs.",
413 var el = I.buffer.focused_element.parentNode;
414 while (el && el.tagName != "FORM")
421 * Browser Object Commands
423 interactive("follow", null,
424 alternates(follow, follow_new_buffer, follow_new_window),
425 $browser_object = browser_object_links);
427 interactive("follow-top", null,
428 alternates(follow_current_buffer, follow_current_frame),
429 $browser_object = browser_object_frames,
432 interactive("follow-new-buffer",
433 "Follow a link in a new buffer",
434 alternates(follow_new_buffer, follow_new_window),
435 $browser_object = browser_object_links,
438 interactive("follow-new-buffer-background",
439 "Follow a link in a new buffer in the background",
440 alternates(follow_new_buffer_background, follow_new_window),
441 $browser_object = browser_object_links,
444 interactive("follow-new-window",
445 "Follow a link in a new window",
447 $browser_object = browser_object_links,
450 interactive("follow-current",
451 "Follow current object, normally the focused element.",
452 alternates(follow, follow_new_buffer, follow_new_window),
453 $browser_object = browser_object_focused_element);
455 interactive("follow-current-new-buffer",
456 "Follow current object, normally the focused element, in "+
458 alternates(follow_new_buffer, follow_new_window),
459 $browser_object = browser_object_focused_element,
462 interactive("follow-current-new-buffer-background",
463 "Follow current object, normally the focused element, in "+
464 "a new background buffer.",
465 alternates(follow_new_buffer_background, follow_new_window),
466 $browser_object = browser_object_focused_element,
469 interactive("follow-current-new-window",
470 "Follow current object, normally the focused element, in "+
473 $browser_object = browser_object_focused_element,
476 interactive("find-url", "Open a URL in the current buffer",
477 alternates(follow_current_buffer, follow_new_buffer, follow_new_window),
478 $browser_object = browser_object_url);
480 interactive("find-url-new-buffer",
481 "Open a URL in a new buffer",
482 alternates(follow_new_buffer, follow_new_window),
483 $browser_object = browser_object_url,
484 $prompt = "Find url");
486 interactive("find-url-new-window", "Open a URL in a new window",
488 $browser_object = browser_object_url,
489 $prompt = "Find url");
491 interactive("find-alternate-url", "Edit the current URL in the minibuffer",
494 define_browser_object_class(
495 "alternate-url", null, null,
496 function (I, prompt) {
497 check_buffer(I.buffer, content_buffer);
498 var result = yield I.buffer.window.minibuffer.read_url(
500 $initial_value = I.buffer.display_URI_string);
501 yield co_return(result);
503 $prompt = "Find url");
506 interactive("go-up", "Go to the parent directory of the current URL",
508 $browser_object = browser_object_up_url);
510 interactive("go-home",
511 "Go to the homepage in the current buffer.", "follow",
512 $browser_object = function () { return homepage; });
514 interactive("make-window",
515 "Make a new window with the homepage.",
517 $browser_object = function () { return homepage; });
519 interactive("focus", null,
521 var element = yield read_browser_object(I);
522 browser_element_focus(I.buffer, element);
524 $browser_object = browser_object_frames);
526 interactive("save", null, function (I) {
527 var element = yield read_browser_object(I);
529 var spec = load_spec(element);
532 panel = create_info_panel(I.window, "download-panel",
534 element_get_operation_label(element, "Saving"),
535 load_spec_uri_string(spec)],
536 ["mime-type", "Mime type:", load_spec_mime_type(spec)]]);
539 var file = yield I.minibuffer.read_file_check_overwrite(
540 $prompt = "Save as:",
541 $initial_value = suggest_save_path_from_file_name(suggest_file_name(spec), I.buffer),
552 $browser_object = browser_object_links);
555 interactive("copy", null,
557 var element = yield read_browser_object(I);
558 browser_element_copy(I.buffer, element);
560 $browser_object = browser_object_links);
562 interactive("paste-url", "Open a URL from the clipboard in the current buffer.",
563 alternates(follow_current_buffer, follow_new_buffer, follow_new_window),
564 $browser_object = browser_object_pasteurl);
566 interactive("paste-url-new-buffer", "Open a URL from the clipboard in a new buffer.",
567 alternates(follow_new_buffer, follow_new_window),
568 $browser_object = browser_object_pasteurl);
570 interactive("paste-url-new-window", "Open a URL from the clipboard in a new window.",
572 $browser_object = browser_object_pasteurl);
574 interactive("view-source", null,
575 alternates(view_source, view_source_new_buffer, view_source_new_window),
576 $browser_object = browser_object_frames);
578 interactive("shell-command-on-url", null, function (I) {
579 var cwd = I.local.cwd;
580 var element = yield read_browser_object(I);
581 var spec = load_spec(element);
583 var uri = load_spec_uri_string(spec);
586 panel = create_info_panel(I.window, "download-panel",
588 element_get_operation_label(element, "Running on", "URI"),
589 load_spec_uri_string(spec)],
590 ["mime-type", "Mime type:", load_spec_mime_type(spec)]]);
593 var cmd = yield I.minibuffer.read_shell_command(
595 $initial_value = load_spec_default_shell_command(spec));
600 shell_command_with_argument_blind(cmd, uri, $cwd = cwd);
602 $browser_object = browser_object_url,
603 $prompt = "Shell command");
606 interactive("shell-command-on-file", null, function (I) {
607 var cwd = I.local.cwd;
608 var element = yield read_browser_object(I);
610 var spec = load_spec(element);
612 var uri = load_spec_uri_string(spec);
615 panel = create_info_panel(I.window, "download-panel",
617 element_get_operation_label(element, "Running on"),
618 load_spec_uri_string(spec)],
619 ["mime-type", "Mime type:", load_spec_mime_type(spec)]]);
622 var cmd = yield I.minibuffer.read_shell_command(
624 $initial_value = load_spec_default_shell_command(spec));
629 yield browser_element_shell_command(I.buffer, element, cmd, cwd);
631 $browser_object = browser_object_links,
632 $prompt = "Shell command");
634 interactive("bookmark", null, function (I) {
635 var element = yield read_browser_object(I);
636 var spec = load_spec(element);
637 var uri_string = load_spec_uri_string(spec);
639 panel = create_info_panel(I.window, "bookmark-panel",
641 element_get_operation_label(element, "Bookmarking"),
644 var title = yield I.minibuffer.read($prompt = "Bookmark with title:", $initial_value = load_spec_title(spec) || "");
648 add_bookmark(uri_string, title);
649 I.minibuffer.message("Added bookmark: " + uri_string + " - " + title);
651 $browser_object = browser_object_frames);
653 interactive("save-page", null, function (I) {
654 check_buffer(I.buffer, content_buffer);
655 var element = yield read_browser_object(I);
656 var spec = load_spec(element);
657 if (!load_spec_document(spec))
658 throw interactive_error("Element is not associated with a document.");
659 var suggested_path = suggest_save_path_from_file_name(suggest_file_name(spec), I.buffer);
662 panel = create_info_panel(I.window, "download-panel",
664 element_get_operation_label(element, "Saving"),
665 load_spec_uri_string(spec)],
666 ["mime-type", "Mime type:", load_spec_mime_type(spec)]]);
669 var file = yield I.minibuffer.read_file_check_overwrite(
670 $prompt = "Save page as:",
672 $initial_value = suggested_path);
677 save_uri(spec, file, $buffer = I.buffer);
679 $browser_object = browser_object_frames);
681 interactive("save-page-as-text", null, function (I) {
682 check_buffer(I.buffer, content_buffer);
683 var element = yield read_browser_object(I);
684 var spec = load_spec(element);
686 if (!(doc = load_spec_document(spec)))
687 throw interactive_error("Element is not associated with a document.");
688 var suggested_path = suggest_save_path_from_file_name(suggest_file_name(spec, "txt"), I.buffer);
691 panel = create_info_panel(I.window, "download-panel",
693 element_get_operation_label(element, "Saving", "as text"),
694 load_spec_uri_string(spec)],
695 ["mime-type", "Mime type:", load_spec_mime_type(spec)]]);
698 var file = yield I.minibuffer.read_file_check_overwrite(
699 $prompt = "Save page as text:",
701 $initial_value = suggested_path);
706 save_document_as_text(doc, file, $buffer = I.buffer);
708 $browser_object = browser_object_frames);
710 interactive("save-page-complete", null, function (I) {
711 check_buffer(I.buffer, content_buffer);
712 var element = yield read_browser_object(I);
713 var spec = load_spec(element);
715 if (!(doc = load_spec_document(spec)))
716 throw interactive_error("Element is not associated with a document.");
717 var suggested_path = suggest_save_path_from_file_name(suggest_file_name(spec), I.buffer);
720 panel = create_info_panel(I.window, "download-panel",
722 element_get_operation_label(element, "Saving complete"),
723 load_spec_uri_string(spec)],
724 ["mime-type", "Mime type:", load_spec_mime_type(spec)]]);
727 var file = yield I.minibuffer.read_file_check_overwrite(
728 $prompt = "Save page complete:",
730 $initial_value = suggested_path);
731 // FIXME: use proper read function
732 var dir = yield I.minibuffer.read_file(
733 $prompt = "Data Directory:",
735 $initial_value = file.path + ".support");
740 save_document_complete(doc, file, dir, $buffer = I.buffer);
742 $browser_object = browser_object_frames);
745 function view_as_mime_type (I, target) {
747 var element = yield read_browser_object(I);
748 var spec = load_spec(element);
751 target = FOLLOW_CURRENT_FRAME;
753 if (!can_override_mime_type_for_uri(load_spec_uri(spec)))
754 throw interactive_error("Overriding the MIME type is not currently supported for non-HTTP URLs.");
758 var mime_type = load_spec_mime_type(spec);
759 panel = create_info_panel(I.window, "download-panel",
761 element_get_operation_label(element, "View in browser"),
762 load_spec_uri_string(spec)],
763 ["mime-type", "Mime type:", load_spec_mime_type(spec)]]);
767 let suggested_type = mime_type;
768 if (gecko_viewable_mime_type_list.indexOf(suggested_type) == -1)
769 suggested_type = "text/plain";
770 mime_type = yield I.minibuffer.read_gecko_viewable_mime_type(
771 $prompt = "View internally as",
772 $initial_value = suggested_type,
774 override_mime_type_for_next_load(load_spec_uri(spec), mime_type);
775 browser_object_follow(I.buffer, target, spec);
780 function view_as_mime_type_new_buffer (I) {
781 yield view_as_mime_type(I, OPEN_NEW_BUFFER);
783 function view_as_mime_type_new_window (I) {
784 yield view_as_mime_type(I, OPEN_NEW_WINDOW);
786 interactive("view-as-mime-type",
787 "Display a browser object in the browser using the specified MIME type.",
788 alternates(view_as_mime_type,
789 view_as_mime_type_new_buffer,
790 view_as_mime_type_new_window));
793 interactive("reload-with-charset",
796 var forced_charset = yield I.minibuffer.read($prompt = "Charset:");
797 reload(I.buffer, false, null, forced_charset);