2 * (C) Copyright 2007 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("url_remoting_fn", load_url_in_new_window);
13 function load_url_in_new_window(url, ctx) {
14 make_window(buffer_creator(content_buffer, $load = url, $configuration = ctx.config));
17 function load_url_in_new_buffer(url, ctx) {
18 create_buffer_in_current_window(buffer_creator(content_buffer, $load = url, $configuration = ctx.config),
19 OPEN_NEW_BUFFER, true /* focus the new window */);
22 function command_line_handler(name, suppress_default, handler)
24 command_line_handlers[name] = { suppress_default: suppress_default, func: handler };
27 function command_line_param_handler(name, suppress_default, handler)
29 command_line_handlers[name] = { suppress_default: suppress_default,
34 command_line_handler("batch", true);
35 command_line_param_handler("e", false, function (expr, ctx) {
39 command_line_param_handler("E", false, function (expr, ctx) {});
41 command_line_param_handler("chrome", true, function (uri, ctx) {
43 make_chrome_window(uri);
44 } catch (e) { dump_error(e); }
46 command_line_param_handler("q", false, function () {
47 dumpln ("w: -q may only be used as the first argument.");
50 command_line_param_handler("cwd", false, function (dir, ctx) {
51 if (ctx.config == null)
57 function handle_command_line(cmdline)
60 var suppress_default = false;
61 var suppress_rc = false;
65 /* -q must be the first argument, if it is given */
66 if (cmdline.length > 0 && cmdline.getArgument(0) == "-q")
72 var initial_launch = (cmdline.state == cmdline.STATE_INITIAL_LAUNCH);
76 while (j + 1 < cmdline.length) {
77 if (cmdline.getArgument(j) == "-E") {
78 eval(cmdline.getArgument(j+1));
79 cmdline.removeArguments(j, j+1);
84 let load_default_modules = get_pref("conkeror.loadDefaultModules");
85 let load_mods = new RegExp("^(" + get_pref("conkeror.loadModules") + ")$");
87 let branch = preferences.getBranch("conkeror.load.");
88 for each (let m in branch.getChildList("", {})) {
91 val = branch.getIntPref(m);
93 dumpln("Error: Preference 'conkeror.load." + m + "' has non-integer value.");
95 if ((val > 0 && (load_default_modules > 0 ||
96 ((load_default_modules == 0) && branch.prefHasUserValue(m)))) ||
97 (val >= 0 && load_mods.test(m)))
100 } catch (e) {dump_error(e);}
103 if (! suppress_rc && initial_launch)
107 } catch (e) { dump (e + "\n"); }
108 } else if (suppress_rc && ! initial_launch) {
109 dumpln ("w: attempt to suppress load_rc in remote invocation");
111 var ctx = {}; // command-line processing context
113 for (; i < cmdline.length; ++i)
115 var arg = cmdline.getArgument(i);
117 var arg1 = arg.substring(1);
118 if (arg1 in command_line_handlers) {
119 var handler = command_line_handlers[arg1];
120 if (handler.suppress_default)
121 suppress_default = true;
124 i++; // increment the argument counter to skip the parameter
125 if (i >= cmdline.length) {
126 dump ("w: ignoring command switch `"+arg+"' because no argument was provided.\n");
129 var param = cmdline.getArgument (i);
130 handler.func(param, ctx);
139 // something other than a switch was passed on the command
140 // line. suppress the default window, and call the
141 // user-configurable remoting function on it.
143 suppress_default = true;
144 url_remoting_fn (arg, ctx);
147 // we are greedy and handle all command line arguments. remove
148 // everything from the command line object, so no other
149 // components can see them.
151 if (cmdline.length > 0) {
152 cmdline.removeArguments(0, cmdline.length - 1);
155 // no args were found for url_remoting_fn, and no switches
156 // explicitly suppressed the creation of a default window
157 // (e.g. -batch or -daemon)
159 if (! suppress_default) {
160 url_remoting_fn(homepage, ctx);
163 dumpln("Error processing command line.");