2 * (C) Copyright 2007-2009 John J. Foerch
3 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
5 * Use, modification, and distribution are subject to the terms specified in the
9 var command_line_handlers = [];
11 define_variable("conkeror_started", false,
12 "True if conkeror has started, false if conkeror is still starting.");
14 define_variable("url_remoting_fn", load_url_in_new_window,
15 "The function given as the value of this variable gets called for "+
16 "each datum given on the command-line not a switch or the value of "+
17 "a switch. Such data are typically urls. Some typical values are "+
18 "load_url_in_new_window (default), load_url_in_new_buffer, or "+
19 "load_url_in_current_buffer.");
22 * load_url_in_new_window is a function intended for use as
23 * a value of `url_remoting_fn'. Every url given on the
24 * command line will be loaded in a new window.
26 function load_url_in_new_window (url, ctx) {
27 make_window(buffer_creator(content_buffer,
33 * load_url_in_new_buffer is a function intended for use as
34 * a value of `url_remoting_fn'. Every url given on the
35 * command line will be loaded in a new buffer in the most
36 * recently used window, or a new window if none exist.
38 function load_url_in_new_buffer (url, ctx) {
39 create_buffer_in_current_window(
40 buffer_creator(content_buffer,
43 OPEN_NEW_BUFFER, true /* focus the new window */);
47 * load_url_in_new_buffer_background is a function intended for use as a
48 * value of `url_remoting_fn'. Every url given on the command line will
49 * be loaded in a new background buffer in the most recently used window,
50 * or a new window if none exist.
52 function load_url_in_new_buffer_background (url, ctx) {
53 create_buffer_in_current_window(
54 buffer_creator(content_buffer,
57 OPEN_NEW_BUFFER_BACKGROUND, true /* focus the new window */);
61 * load_url_in_current_buffer is a function intended for use
62 * as a value of `url_remoting_fn'. Every url given on the
63 * command line will be loaded in the current buffer of the
64 * most recently used window. This makes it useful for only
65 * one url at a time. When there are no conkeror windows
66 * open, the url will be loaded in a new window.
68 function load_url_in_current_buffer (url, ctx) {
69 var win = get_recent_conkeror_window();
71 browser_object_follow(win.buffers.current, OPEN_CURRENT_BUFFER, url);
73 load_url_in_new_window(url, ctx);
77 function command_line_handler (name, suppress_default, handler) {
78 command_line_handlers[name] = { suppress_default: suppress_default, func: handler };
81 function command_line_param_handler (name, suppress_default, handler) {
82 command_line_handlers[name] = { suppress_default: suppress_default,
87 command_line_handler("batch", true);
88 command_line_param_handler("e", true, function (expr, ctx) {
92 command_line_param_handler("E", false, function (expr, ctx) {});
94 command_line_param_handler("chrome", true, function (uri, ctx) {
96 make_chrome_window(uri);
97 } catch (e) { dump_error(e); }
99 command_line_param_handler("q", false, function () {
100 dumpln ("w: -q may only be used as the first argument.");
103 command_line_param_handler("f", true, function (command, ctx) {
104 // hack to make sure we send this command to a window
105 ctx.window = get_recent_conkeror_window();
107 ctx.buffer = ctx.window.buffers.current;
108 co_call(call_interactively(ctx, command));
111 command_line_param_handler("l", false, function (path, ctx) {
113 load(ctx.command_line.resolveFile(path));
119 // note `u' must be called as +u because Mozilla consumes -u
120 command_line_handler("u", false, function (ctx) {
121 // hack to make sure we send this command to a window
123 ctx.window = get_recent_conkeror_window();
124 ctx.buffer = ctx.window.buffers.current;
126 co_call(call_interactively(ctx, "universal-argument"));
129 function handle_command_line (cmdline) {
131 this.command_line = [];
132 for (let i = 0, clen = cmdline.length; i < clen; ++i)
133 command_line.push(cmdline.getArgument(i));
135 var suppress_default = false;
136 var suppress_rc = false;
140 /* -q must be the first argument, if it is given */
141 if (cmdline.length > 0 && cmdline.getArgument(0) == "-q") {
146 var initial_launch = (cmdline.state == cmdline.STATE_INITIAL_LAUNCH);
148 if (initial_launch) {
150 while (j + 1 < cmdline.length) {
151 if (cmdline.getArgument(j) == "-E") {
152 eval(cmdline.getArgument(j+1));
153 cmdline.removeArguments(j, j+1);
158 let load_default_modules = get_pref("conkeror.loadDefaultModules");
159 let load_mods = new RegExp("^(" + get_pref("conkeror.loadModules") + ")$");
161 let branch = preferences.getBranch("conkeror.load.");
162 for each (let m in branch.getChildList("", {})) {
165 val = branch.getIntPref(m);
167 dumpln("Error: Preference 'conkeror.load." + m + "' has non-integer value.");
169 if ((val > 0 && (load_default_modules > 0 ||
170 ((load_default_modules == 0) && branch.prefHasUserValue(m)))) ||
171 (val >= 0 && load_mods.test(m)))
174 } catch (e) {dump_error(e);}
177 if (! suppress_rc && initial_launch) {
183 } else if (suppress_rc && ! initial_launch) {
184 dumpln("w: attempt to suppress loading of rc in remote invocation");
186 var ctx = new interactive_context();
187 ctx.command_line = cmdline;
188 ctx.local = { __proto__: conkeror,
189 // see https://bugzilla.mozilla.org/show_bug.cgi?id=476326
190 cwd: cmdline.resolveURI(".").QueryInterface(Ci.nsIFileURL).file };
192 for (let clen = cmdline.length; i < clen; ++i) {
193 var arg = cmdline.getArgument(i);
194 if (arg[0] == '-' || arg[0] == '+') {
195 var arg1 = arg.substring(1);
196 if (arg1 in command_line_handlers) {
197 var handler = command_line_handlers[arg1];
198 if (handler.suppress_default)
199 suppress_default = true;
202 i++; // increment the argument counter to skip the parameter
203 if (i >= cmdline.length) {
204 dump("w: ignoring command switch `"+arg+"' because no argument was provided.\n");
207 var param = cmdline.getArgument(i);
208 handler.func(param, ctx);
215 dump("w: unknown command switch `"+arg+"'.\n");
218 // something other than a switch was passed on the command
219 // line. suppress the default window, and call the
220 // user-configurable remoting function on it.
222 suppress_default = true;
223 url_remoting_fn(arg, ctx);
227 // we are greedy and handle all command line arguments. remove
228 // everything from the command line object, so no other
229 // components can see them.
231 if (cmdline.length > 0) {
232 cmdline.removeArguments(0, cmdline.length - 1);
235 // no args were found for url_remoting_fn, and no switches
236 // explicitly suppressed the creation of a default window
237 // (e.g. -batch or -daemon)
239 if (! suppress_default) {
240 url_remoting_fn(homepage, ctx);
243 dumpln("Error processing command line.");
246 conkeror_started = true;
249 provide("command-line");