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, $configuration = ctx.config));
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, $configuration = ctx.config),
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 ctx.window = window_watcher.activeWindow;
85 call_interactively(ctx, command);
88 command_line_param_handler("l", false, function (path, ctx) {
90 load_rc(ctx.command_line.resolveFile(path).path);
91 } catch (e) { dump_error(e); }
94 command_line_handler("uu", false, function (ctx) {
96 ctx.window = window_watcher.activeWindow;
97 call_interactively(ctx, "universal-argument");
100 function handle_command_line(cmdline)
103 var suppress_default = false;
104 var suppress_rc = false;
108 /* -q must be the first argument, if it is given */
109 if (cmdline.length > 0 && cmdline.getArgument(0) == "-q")
115 var initial_launch = (cmdline.state == cmdline.STATE_INITIAL_LAUNCH);
117 if (initial_launch) {
119 while (j + 1 < cmdline.length) {
120 if (cmdline.getArgument(j) == "-E") {
121 eval(cmdline.getArgument(j+1));
122 cmdline.removeArguments(j, j+1);
127 let load_default_modules = get_pref("conkeror.loadDefaultModules");
128 let load_mods = new RegExp("^(" + get_pref("conkeror.loadModules") + ")$");
130 let branch = preferences.getBranch("conkeror.load.");
131 for each (let m in branch.getChildList("", {})) {
134 val = branch.getIntPref(m);
136 dumpln("Error: Preference 'conkeror.load." + m + "' has non-integer value.");
138 if ((val > 0 && (load_default_modules > 0 ||
139 ((load_default_modules == 0) && branch.prefHasUserValue(m)))) ||
140 (val >= 0 && load_mods.test(m)))
143 } catch (e) {dump_error(e);}
146 if (! suppress_rc && initial_launch)
150 } catch (e) { dump (e + "\n"); }
151 } else if (suppress_rc && ! initial_launch) {
152 dumpln ("w: attempt to suppress load_rc in remote invocation");
154 var ctx = {command_line: cmdline,
156 cwd: cmdline.resolveFile(".").path
158 }; // command-line processing context
160 for (; i < cmdline.length; ++i)
162 var arg = cmdline.getArgument(i);
164 var arg1 = arg.substring(1);
165 if (arg1 in command_line_handlers) {
166 var handler = command_line_handlers[arg1];
167 if (handler.suppress_default)
168 suppress_default = true;
171 i++; // increment the argument counter to skip the parameter
172 if (i >= cmdline.length) {
173 dump ("w: ignoring command switch `"+arg+"' because no argument was provided.\n");
176 var param = cmdline.getArgument (i);
177 handler.func(param, ctx);
184 dump ("w: unknown command switch `"+arg+"'.\n");
187 // something other than a switch was passed on the command
188 // line. suppress the default window, and call the
189 // user-configurable remoting function on it.
191 suppress_default = true;
192 url_remoting_fn (arg, ctx);
196 // we are greedy and handle all command line arguments. remove
197 // everything from the command line object, so no other
198 // components can see them.
200 if (cmdline.length > 0) {
201 cmdline.removeArguments(0, cmdline.length - 1);
204 // no args were found for url_remoting_fn, and no switches
205 // explicitly suppressed the creation of a default window
206 // (e.g. -batch or -daemon)
208 if (! suppress_default) {
209 url_remoting_fn(homepage, ctx);
212 dumpln("Error processing command line.");