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) {
43 if (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_handler("uu", false, function (ctx) {
91 ctx.window = window_watcher.activeWindow;
92 call_interactively(ctx, "universal-argument");
95 function handle_command_line(cmdline)
98 var suppress_default = false;
99 var suppress_rc = false;
103 /* -q must be the first argument, if it is given */
104 if (cmdline.length > 0 && cmdline.getArgument(0) == "-q")
110 var initial_launch = (cmdline.state == cmdline.STATE_INITIAL_LAUNCH);
112 if (initial_launch) {
114 while (j + 1 < cmdline.length) {
115 if (cmdline.getArgument(j) == "-E") {
116 eval(cmdline.getArgument(j+1));
117 cmdline.removeArguments(j, j+1);
122 let load_default_modules = get_pref("conkeror.loadDefaultModules");
123 let load_mods = new RegExp("^(" + get_pref("conkeror.loadModules") + ")$");
125 let branch = preferences.getBranch("conkeror.load.");
126 for each (let m in branch.getChildList("", {})) {
129 val = branch.getIntPref(m);
131 dumpln("Error: Preference 'conkeror.load." + m + "' has non-integer value.");
133 if ((val > 0 && (load_default_modules > 0 ||
134 ((load_default_modules == 0) && branch.prefHasUserValue(m)))) ||
135 (val >= 0 && load_mods.test(m)))
138 } catch (e) {dump_error(e);}
141 if (! suppress_rc && initial_launch)
145 } catch (e) { dump (e + "\n"); }
146 } else if (suppress_rc && ! initial_launch) {
147 dumpln ("w: attempt to suppress load_rc in remote invocation");
149 var ctx = {}; // command-line processing context
151 for (; i < cmdline.length; ++i)
153 var arg = cmdline.getArgument(i);
155 var arg1 = arg.substring(1);
156 if (arg1 in command_line_handlers) {
157 var handler = command_line_handlers[arg1];
158 if (handler.suppress_default)
159 suppress_default = true;
162 i++; // increment the argument counter to skip the parameter
163 if (i >= cmdline.length) {
164 dump ("w: ignoring command switch `"+arg+"' because no argument was provided.\n");
167 var param = cmdline.getArgument (i);
168 handler.func(param, ctx);
175 dump ("w: unknown command switch `"+arg+"'.\n");
178 // something other than a switch was passed on the command
179 // line. suppress the default window, and call the
180 // user-configurable remoting function on it.
182 suppress_default = true;
183 url_remoting_fn (arg, ctx);
187 // we are greedy and handle all command line arguments. remove
188 // everything from the command line object, so no other
189 // components can see them.
191 if (cmdline.length > 0) {
192 cmdline.removeArguments(0, cmdline.length - 1);
195 // no args were found for url_remoting_fn, and no switches
196 // explicitly suppressed the creation of a default window
197 // (e.g. -batch or -daemon)
199 if (! suppress_default) {
200 url_remoting_fn(homepage, ctx);
203 dumpln("Error processing command line.");