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
11 var command_line_handlers = [];
13 define_variable("conkeror_started", false,
14 "True if conkeror has started, false if conkeror is still starting.");
16 define_variable("url_remoting_fn", load_url_in_new_window,
17 "The function given as the value of this variable gets called for "+
18 "each datum given on the command-line not a switch or the value of "+
19 "a switch. Such data are typically urls. Some typical values are "+
20 "load_url_in_new_window (default), load_url_in_new_buffer, or "+
21 "load_url_in_current_buffer.");
24 * load_url_in_new_window is a function intended for use as
25 * a value of `url_remoting_fn'. Every url given on the
26 * command line will be loaded in a new window.
28 function load_url_in_new_window (url, ctx) {
29 make_window(buffer_creator(content_buffer,
35 * load_url_in_new_buffer is a function intended for use as
36 * a value of `url_remoting_fn'. Every url given on the
37 * command line will be loaded in a new buffer in the most
38 * recently used window, or a new window if none exist.
40 function load_url_in_new_buffer (url, ctx) {
41 create_buffer_in_current_window(
42 buffer_creator(content_buffer,
45 OPEN_NEW_BUFFER, true /* focus the new window */);
49 * load_url_in_current_buffer is a function intended for use
50 * as a value of `url_remoting_fn'. Every url given on the
51 * command line will be loaded in the current buffer of the
52 * most recently used window. This makes it useful for only
53 * one url at a time. When there are no conkeror windows
54 * open, the url will be loaded in a new window.
56 function load_url_in_current_buffer (url, ctx) {
57 var win = get_recent_conkeror_window();
59 browser_object_follow(win.buffers.current, OPEN_CURRENT_BUFFER, url);
61 load_url_in_new_window(url, ctx);
65 function command_line_handler (name, suppress_default, handler) {
66 command_line_handlers[name] = { suppress_default: suppress_default, func: handler };
69 function command_line_param_handler (name, suppress_default, handler) {
70 command_line_handlers[name] = { suppress_default: suppress_default,
75 command_line_handler("batch", true);
76 command_line_param_handler("e", true, function (expr, ctx) {
80 command_line_param_handler("E", false, function (expr, ctx) {});
82 command_line_param_handler("chrome", true, function (uri, ctx) {
84 make_chrome_window(uri);
85 } catch (e) { dump_error(e); }
87 command_line_param_handler("q", false, function () {
88 dumpln ("w: -q may only be used as the first argument.");
91 command_line_param_handler("f", true, function (command, ctx) {
92 // hack to make sure we send this command to a window
93 ctx.window = get_recent_conkeror_window();
95 ctx.buffer = ctx.window.buffers.current;
96 co_call(call_interactively(ctx, command));
99 command_line_param_handler("l", false, function (path, ctx) {
101 load(ctx.command_line.resolveFile(path));
107 // note `u' must be called as +u because Mozilla consumes -u
108 command_line_handler("u", false, function (ctx) {
109 // hack to make sure we send this command to a window
111 ctx.window = get_recent_conkeror_window();
112 ctx.buffer = ctx.window.buffers.current;
114 co_call(call_interactively(ctx, "universal-argument"));
117 function handle_command_line (cmdline) {
119 this.command_line = [];
120 for (let i = 0, clen = cmdline.length; i < clen; ++i)
121 command_line.push(cmdline.getArgument(i));
123 var suppress_default = false;
124 var suppress_rc = false;
128 /* -q must be the first argument, if it is given */
129 if (cmdline.length > 0 && cmdline.getArgument(0) == "-q") {
134 var initial_launch = (cmdline.state == cmdline.STATE_INITIAL_LAUNCH);
136 if (initial_launch) {
138 while (j + 1 < cmdline.length) {
139 if (cmdline.getArgument(j) == "-E") {
140 eval(cmdline.getArgument(j+1));
141 cmdline.removeArguments(j, j+1);
146 let load_default_modules = get_pref("conkeror.loadDefaultModules");
147 let load_mods = new RegExp("^(" + get_pref("conkeror.loadModules") + ")$");
149 let branch = preferences.getBranch("conkeror.load.");
150 for each (let m in branch.getChildList("", {})) {
153 val = branch.getIntPref(m);
155 dumpln("Error: Preference 'conkeror.load." + m + "' has non-integer value.");
157 if ((val > 0 && (load_default_modules > 0 ||
158 ((load_default_modules == 0) && branch.prefHasUserValue(m)))) ||
159 (val >= 0 && load_mods.test(m)))
162 } catch (e) {dump_error(e);}
165 if (! suppress_rc && initial_launch) {
171 } else if (suppress_rc && ! initial_launch) {
172 dumpln("w: attempt to suppress loading of rc in remote invocation");
174 var ctx = new interactive_context();
175 ctx.command_line = cmdline;
176 ctx.local = { cwd: cmdline.resolveFile("."),
177 __proto__: conkeror }
179 for (let clen = cmdline.length; i < clen; ++i) {
180 var arg = cmdline.getArgument(i);
181 if (arg[0] == '-' || arg[0] == '+') {
182 var arg1 = arg.substring(1);
183 if (arg1 in command_line_handlers) {
184 var handler = command_line_handlers[arg1];
185 if (handler.suppress_default)
186 suppress_default = true;
189 i++; // increment the argument counter to skip the parameter
190 if (i >= cmdline.length) {
191 dump("w: ignoring command switch `"+arg+"' because no argument was provided.\n");
194 var param = cmdline.getArgument(i);
195 handler.func(param, ctx);
202 dump("w: unknown command switch `"+arg+"'.\n");
205 // something other than a switch was passed on the command
206 // line. suppress the default window, and call the
207 // user-configurable remoting function on it.
209 suppress_default = true;
210 url_remoting_fn(arg, ctx);
214 // we are greedy and handle all command line arguments. remove
215 // everything from the command line object, so no other
216 // components can see them.
218 if (cmdline.length > 0) {
219 cmdline.removeArguments(0, cmdline.length - 1);
222 // no args were found for url_remoting_fn, and no switches
223 // explicitly suppressed the creation of a default window
224 // (e.g. -batch or -daemon)
226 if (! suppress_default) {
227 url_remoting_fn(homepage, ctx);
230 dumpln("Error processing command line.");
233 conkeror_started = true;
236 provide("command-line");