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,
12 "The function given as the value of this variable gets called for "+
13 "each datum given on the command-line not a switch or the value of "+
14 "a switch. Such data are typically urls. Some typical values are "+
15 "load_url_in_new_window (default), load_url_in_new_buffer, or "+
16 "load_url_in_current_buffer.");
19 * load_url_in_new_window is a function intended for use as
20 * a value of `url_remoting_fn'. Every url given on the
21 * command line will be loaded in a new window.
23 function load_url_in_new_window(url, ctx) {
24 make_window(buffer_creator(content_buffer, $load = url));
28 * load_url_in_new_buffer is a function intended for use as
29 * a value of `url_remoting_fn'. Every url given on the
30 * command line will be loaded in a new buffer in the most
31 * recently used window, or a new window if none exist.
33 function load_url_in_new_buffer(url, ctx) {
34 create_buffer_in_current_window(buffer_creator(content_buffer, $load = url),
35 OPEN_NEW_BUFFER, true /* focus the new window */);
39 * load_url_in_current_buffer is a function intended for use
40 * as a value of `url_remoting_fn'. Every url given on the
41 * command line will be loaded in the current buffer of the
42 * most recently used window. This makes it useful for only
43 * one url at a time. When there are no conkeror windows
44 * open, the url will be loaded in a new window.
46 function load_url_in_current_buffer(url,ctx) {
47 var win = get_recent_conkeror_window();
49 browser_object_follow(win.buffers.current, OPEN_CURRENT_BUFFER, url);
51 load_url_in_new_window(url, ctx);
55 function command_line_handler(name, suppress_default, handler)
57 command_line_handlers[name] = { suppress_default: suppress_default, func: handler };
60 function command_line_param_handler(name, suppress_default, handler)
62 command_line_handlers[name] = { suppress_default: suppress_default,
67 command_line_handler("batch", true);
68 command_line_param_handler("e", true, function (expr, ctx) {
72 command_line_param_handler("E", false, function (expr, ctx) {});
74 command_line_param_handler("chrome", true, function (uri, ctx) {
76 make_chrome_window(uri);
77 } catch (e) { dump_error(e); }
79 command_line_param_handler("q", false, function () {
80 dumpln ("w: -q may only be used as the first argument.");
83 command_line_param_handler("f", true, function (command, ctx) {
84 // hack to make sure we send this command to a window
85 ctx.window = get_recent_conkeror_window();
86 ctx.buffer = ctx.window.buffers.current;
87 call_interactively(ctx, command);
90 command_line_param_handler("l", false, function (path, ctx) {
92 load_rc(ctx.command_line.resolveFile(path).path);
93 } catch (e) { dump_error(e); }
96 // note `u' must be called as +u because Mozilla consumes -u
97 command_line_handler("u", false, function (ctx) {
98 // hack to make sure we send this command to a window
100 ctx.window = get_recent_conkeror_window();
101 ctx.buffer = ctx.window.buffers.current;
103 call_interactively(ctx, "universal-argument");
106 function handle_command_line (cmdline) {
108 var suppress_default = false;
109 var suppress_rc = false;
113 /* -q must be the first argument, if it is given */
114 if (cmdline.length > 0 && cmdline.getArgument(0) == "-q") {
119 var initial_launch = (cmdline.state == cmdline.STATE_INITIAL_LAUNCH);
121 if (initial_launch) {
123 while (j + 1 < cmdline.length) {
124 if (cmdline.getArgument(j) == "-E") {
125 eval(cmdline.getArgument(j+1));
126 cmdline.removeArguments(j, j+1);
131 let load_default_modules = get_pref("conkeror.loadDefaultModules");
132 let load_mods = new RegExp("^(" + get_pref("conkeror.loadModules") + ")$");
134 let branch = preferences.getBranch("conkeror.load.");
135 for each (let m in branch.getChildList("", {})) {
138 val = branch.getIntPref(m);
140 dumpln("Error: Preference 'conkeror.load." + m + "' has non-integer value.");
142 if ((val > 0 && (load_default_modules > 0 ||
143 ((load_default_modules == 0) && branch.prefHasUserValue(m)))) ||
144 (val >= 0 && load_mods.test(m)))
147 } catch (e) {dump_error(e);}
150 if (! suppress_rc && initial_launch) {
153 } catch (e) { dump (e + "\n"); }
154 } else if (suppress_rc && ! initial_launch) {
155 dumpln ("w: attempt to suppress load_rc in remote invocation");
157 var ctx = new interactive_context();
158 ctx.command_line = cmdline;
160 for (; i < cmdline.length; ++i) {
161 var arg = cmdline.getArgument(i);
162 if (arg[0] == '-' || arg[0] == '+') {
163 var arg1 = arg.substring(1);
164 if (arg1 in command_line_handlers) {
165 var handler = command_line_handlers[arg1];
166 if (handler.suppress_default)
167 suppress_default = true;
170 i++; // increment the argument counter to skip the parameter
171 if (i >= cmdline.length) {
172 dump("w: ignoring command switch `"+arg+"' because no argument was provided.\n");
175 var param = cmdline.getArgument(i);
176 handler.func(param, ctx);
183 dump("w: unknown command switch `"+arg+"'.\n");
186 // something other than a switch was passed on the command
187 // line. suppress the default window, and call the
188 // user-configurable remoting function on it.
190 suppress_default = true;
191 url_remoting_fn(arg, ctx);
195 // we are greedy and handle all command line arguments. remove
196 // everything from the command line object, so no other
197 // components can see them.
199 if (cmdline.length > 0) {
200 cmdline.removeArguments(0, cmdline.length - 1);
203 // no args were found for url_remoting_fn, and no switches
204 // explicitly suppressed the creation of a default window
205 // (e.g. -batch or -daemon)
207 if (! suppress_default) {
208 url_remoting_fn(homepage, ctx);
211 dumpln("Error processing command line.");