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_current_buffer is a function intended for use
48 * as a value of `url_remoting_fn'. Every url given on the
49 * command line will be loaded in the current buffer of the
50 * most recently used window. This makes it useful for only
51 * one url at a time. When there are no conkeror windows
52 * open, the url will be loaded in a new window.
54 function load_url_in_current_buffer (url, ctx) {
55 var win = get_recent_conkeror_window();
57 browser_object_follow(win.buffers.current, OPEN_CURRENT_BUFFER, url);
59 load_url_in_new_window(url, ctx);
63 function command_line_handler (name, suppress_default, handler) {
64 command_line_handlers[name] = { suppress_default: suppress_default, func: handler };
67 function command_line_param_handler (name, suppress_default, handler) {
68 command_line_handlers[name] = { suppress_default: suppress_default,
73 command_line_handler("batch", true);
74 command_line_param_handler("e", true, function (expr, ctx) {
78 command_line_param_handler("E", false, function (expr, ctx) {});
80 command_line_param_handler("chrome", true, function (uri, ctx) {
82 make_chrome_window(uri);
83 } catch (e) { dump_error(e); }
85 command_line_param_handler("q", false, function () {
86 dumpln ("w: -q may only be used as the first argument.");
89 command_line_param_handler("f", true, function (command, ctx) {
90 // hack to make sure we send this command to a window
91 ctx.window = get_recent_conkeror_window();
93 ctx.buffer = ctx.window.buffers.current;
94 co_call(call_interactively(ctx, command));
97 command_line_param_handler("l", false, function (path, ctx) {
99 load_rc(path, function(path) ctx.command_line.resolveFile(path));
100 } catch (e) { dump_error(e); }
103 // note `u' must be called as +u because Mozilla consumes -u
104 command_line_handler("u", false, function (ctx) {
105 // hack to make sure we send this command to a window
107 ctx.window = get_recent_conkeror_window();
108 ctx.buffer = ctx.window.buffers.current;
110 co_call(call_interactively(ctx, "universal-argument"));
113 function handle_command_line (cmdline) {
115 this.command_line = [];
116 for (let i = 0, clen = cmdline.length; i < clen; ++i)
117 command_line.push(cmdline.getArgument(i));
119 var suppress_default = false;
120 var suppress_rc = false;
124 /* -q must be the first argument, if it is given */
125 if (cmdline.length > 0 && cmdline.getArgument(0) == "-q") {
130 var initial_launch = (cmdline.state == cmdline.STATE_INITIAL_LAUNCH);
132 if (initial_launch) {
134 while (j + 1 < cmdline.length) {
135 if (cmdline.getArgument(j) == "-E") {
136 eval(cmdline.getArgument(j+1));
137 cmdline.removeArguments(j, j+1);
142 let load_default_modules = get_pref("conkeror.loadDefaultModules");
143 let load_mods = new RegExp("^(" + get_pref("conkeror.loadModules") + ")$");
145 let branch = preferences.getBranch("conkeror.load.");
146 for each (let m in branch.getChildList("", {})) {
149 val = branch.getIntPref(m);
151 dumpln("Error: Preference 'conkeror.load." + m + "' has non-integer value.");
153 if ((val > 0 && (load_default_modules > 0 ||
154 ((load_default_modules == 0) && branch.prefHasUserValue(m)))) ||
155 (val >= 0 && load_mods.test(m)))
158 } catch (e) {dump_error(e);}
161 if (! suppress_rc && initial_launch) {
164 } catch (e) { dump (e + "\n"); }
165 } else if (suppress_rc && ! initial_launch) {
166 dumpln ("w: attempt to suppress load_rc in remote invocation");
168 var ctx = new interactive_context();
169 ctx.command_line = cmdline;
170 ctx.local = { cwd: cmdline.resolveFile("."),
171 __proto__: conkeror }
173 for (let clen = cmdline.length; i < clen; ++i) {
174 var arg = cmdline.getArgument(i);
175 if (arg[0] == '-' || arg[0] == '+') {
176 var arg1 = arg.substring(1);
177 if (arg1 in command_line_handlers) {
178 var handler = command_line_handlers[arg1];
179 if (handler.suppress_default)
180 suppress_default = true;
183 i++; // increment the argument counter to skip the parameter
184 if (i >= cmdline.length) {
185 dump("w: ignoring command switch `"+arg+"' because no argument was provided.\n");
188 var param = cmdline.getArgument(i);
189 handler.func(param, ctx);
196 dump("w: unknown command switch `"+arg+"'.\n");
199 // something other than a switch was passed on the command
200 // line. suppress the default window, and call the
201 // user-configurable remoting function on it.
203 suppress_default = true;
204 url_remoting_fn(arg, ctx);
208 // we are greedy and handle all command line arguments. remove
209 // everything from the command line object, so no other
210 // components can see them.
212 if (cmdline.length > 0) {
213 cmdline.removeArguments(0, cmdline.length - 1);
216 // no args were found for url_remoting_fn, and no switches
217 // explicitly suppressed the creation of a default window
218 // (e.g. -batch or -daemon)
220 if (! suppress_default) {
221 url_remoting_fn(homepage, ctx);
224 dumpln("Error processing command line.");
227 conkeror_started = true;