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 open_in_browser(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
) {
86 window
: window_watcher
.activeWindow
88 call_interactively(ctx
, command
);
91 function handle_command_line(cmdline
)
94 var suppress_default
= false;
95 var suppress_rc
= false;
99 /* -q must be the first argument, if it is given */
100 if (cmdline
.length
> 0 && cmdline
.getArgument(0) == "-q")
106 var initial_launch
= (cmdline
.state
== cmdline
.STATE_INITIAL_LAUNCH
);
108 if (initial_launch
) {
110 while (j
+ 1 < cmdline
.length
) {
111 if (cmdline
.getArgument(j
) == "-E") {
112 eval(cmdline
.getArgument(j
+1));
113 cmdline
.removeArguments(j
, j
+1);
118 let load_default_modules
= get_pref("conkeror.loadDefaultModules");
119 let load_mods
= new RegExp("^(" + get_pref("conkeror.loadModules") + ")$");
121 let branch
= preferences
.getBranch("conkeror.load.");
122 for each (let m
in branch
.getChildList("", {})) {
125 val
= branch
.getIntPref(m
);
127 dumpln("Error: Preference 'conkeror.load." + m
+ "' has non-integer value.");
129 if ((val
> 0 && (load_default_modules
> 0 ||
130 ((load_default_modules
== 0) && branch
.prefHasUserValue(m
)))) ||
131 (val
>= 0 && load_mods
.test(m
)))
134 } catch (e
) {dump_error(e
);}
137 if (! suppress_rc
&& initial_launch
)
141 } catch (e
) { dump (e
+ "\n"); }
142 } else if (suppress_rc
&& ! initial_launch
) {
143 dumpln ("w: attempt to suppress load_rc in remote invocation");
145 var ctx
= {}; // command-line processing context
147 for (; i
< cmdline
.length
; ++i
)
149 var arg
= cmdline
.getArgument(i
);
151 var arg1
= arg
.substring(1);
152 if (arg1
in command_line_handlers
) {
153 var handler
= command_line_handlers
[arg1
];
154 if (handler
.suppress_default
)
155 suppress_default
= true;
158 i
++; // increment the argument counter to skip the parameter
159 if (i
>= cmdline
.length
) {
160 dump ("w: ignoring command switch `"+arg
+"' because no argument was provided.\n");
163 var param
= cmdline
.getArgument (i
);
164 handler
.func(param
, ctx
);
171 dump ("w: unknown command switch `"+arg
+"'.\n");
174 // something other than a switch was passed on the command
175 // line. suppress the default window, and call the
176 // user-configurable remoting function on it.
178 suppress_default
= true;
179 url_remoting_fn (arg
, ctx
);
183 // we are greedy and handle all command line arguments. remove
184 // everything from the command line object, so no other
185 // components can see them.
187 if (cmdline
.length
> 0) {
188 cmdline
.removeArguments(0, cmdline
.length
- 1);
191 // no args were found for url_remoting_fn, and no switches
192 // explicitly suppressed the creation of a default window
193 // (e.g. -batch or -daemon)
195 if (! suppress_default
) {
196 url_remoting_fn(homepage
, ctx
);
199 dumpln("Error processing command line.");