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);
14 * load_url_in_new_window is a function intended for use as
15 * a value of `url_remoting_fn'. Every url given on the
16 * command line will be loaded in a new window.
18 function load_url_in_new_window(url, ctx) {
19 make_window(buffer_creator(content_buffer, $load = url, $configuration = ctx.config));
23 * load_url_in_new_buffer is a function intended for use as
24 * a value of `url_remoting_fn'. Every url given on the
25 * command line will be loaded in a new buffer in the most
26 * recently used window, or a new window if none exist.
28 function load_url_in_new_buffer(url, ctx) {
29 create_buffer_in_current_window(buffer_creator(content_buffer, $load = url, $configuration = ctx.config),
30 OPEN_NEW_BUFFER, true /* focus the new window */);
34 * load_url_in_current_buffer is a function intended for use
35 * as a value of `url_remoting_fn'. Every url given on the
36 * command line will be loaded in the current buffer of the
37 * most recently used window. This makes it useful for only
38 * one url at a time. When there are no conkeror windows
39 * open, the url will be loaded in a new window.
41 function load_url_in_current_buffer(url,ctx) {
42 var win = get_recent_conkeror_window();
44 browser_object_follow(win.buffers.current, OPEN_CURRENT_BUFFER, url);
46 load_url_in_new_window(url, ctx);
50 function command_line_handler(name, suppress_default, handler)
52 command_line_handlers[name] = { suppress_default: suppress_default, func: handler };
55 function command_line_param_handler(name, suppress_default, handler)
57 command_line_handlers[name] = { suppress_default: suppress_default,
62 command_line_handler("batch", true);
63 command_line_param_handler("e", true, function (expr, ctx) {
67 command_line_param_handler("E", false, function (expr, ctx) {});
69 command_line_param_handler("chrome", true, function (uri, ctx) {
71 make_chrome_window(uri);
72 } catch (e) { dump_error(e); }
74 command_line_param_handler("q", false, function () {
75 dumpln ("w: -q may only be used as the first argument.");
78 command_line_param_handler("cwd", false, function (dir, ctx) {
79 if (ctx.config == null)
84 command_line_param_handler("f", true, function (command, ctx) {
85 ctx.window = window_watcher.activeWindow;
86 call_interactively(ctx, command);
89 command_line_param_handler("l", false, function (path, ctx) {
91 load_rc(ctx.command_line.resolveFile(path).path);
92 } catch (e) { dump_error(e); }
95 command_line_handler("uu", false, function (ctx) {
97 ctx.window = window_watcher.activeWindow;
98 call_interactively(ctx, "universal-argument");
101 function handle_command_line(cmdline)
104 var suppress_default = false;
105 var suppress_rc = false;
109 /* -q must be the first argument, if it is given */
110 if (cmdline.length > 0 && cmdline.getArgument(0) == "-q")
116 var initial_launch = (cmdline.state == cmdline.STATE_INITIAL_LAUNCH);
118 if (initial_launch) {
120 while (j + 1 < cmdline.length) {
121 if (cmdline.getArgument(j) == "-E") {
122 eval(cmdline.getArgument(j+1));
123 cmdline.removeArguments(j, j+1);
128 let load_default_modules = get_pref("conkeror.loadDefaultModules");
129 let load_mods = new RegExp("^(" + get_pref("conkeror.loadModules") + ")$");
131 let branch = preferences.getBranch("conkeror.load.");
132 for each (let m in branch.getChildList("", {})) {
135 val = branch.getIntPref(m);
137 dumpln("Error: Preference 'conkeror.load." + m + "' has non-integer value.");
139 if ((val > 0 && (load_default_modules > 0 ||
140 ((load_default_modules == 0) && branch.prefHasUserValue(m)))) ||
141 (val >= 0 && load_mods.test(m)))
144 } catch (e) {dump_error(e);}
147 if (! suppress_rc && initial_launch)
151 } catch (e) { dump (e + "\n"); }
152 } else if (suppress_rc && ! initial_launch) {
153 dumpln ("w: attempt to suppress load_rc in remote invocation");
155 var ctx = {command_line: cmdline}; // command-line processing context
157 for (; i < cmdline.length; ++i)
159 var arg = cmdline.getArgument(i);
161 var arg1 = arg.substring(1);
162 if (arg1 in command_line_handlers) {
163 var handler = command_line_handlers[arg1];
164 if (handler.suppress_default)
165 suppress_default = true;
168 i++; // increment the argument counter to skip the parameter
169 if (i >= cmdline.length) {
170 dump ("w: ignoring command switch `"+arg+"' because no argument was provided.\n");
173 var param = cmdline.getArgument (i);
174 handler.func(param, ctx);
181 dump ("w: unknown command switch `"+arg+"'.\n");
184 // something other than a switch was passed on the command
185 // line. suppress the default window, and call the
186 // user-configurable remoting function on it.
188 suppress_default = true;
189 url_remoting_fn (arg, ctx);
193 // we are greedy and handle all command line arguments. remove
194 // everything from the command line object, so no other
195 // components can see them.
197 if (cmdline.length > 0) {
198 cmdline.removeArguments(0, cmdline.length - 1);
201 // no args were found for url_remoting_fn, and no switches
202 // explicitly suppressed the creation of a default window
203 // (e.g. -batch or -daemon)
205 if (! suppress_default) {
206 url_remoting_fn(homepage, ctx);
209 dumpln("Error processing command line.");