Add generic label mechanism
[conkeror.git] / modules / command-line.js
blobeb90d071045bbc288451eb2985d7b301a2499c95
1 /**
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
6 * COPYING file.
7 **/
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;
43 if (win = get_recent_conkeror_window()) {
44 open_in_browser(win.buffers.current, OPEN_CURRENT_BUFFER, url);
45 } else {
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,
58 param: true,
59 func: handler };
62 command_line_handler("batch", true);
63 command_line_param_handler("e", true, function (expr, ctx) {
64 eval(expr);
65 });
67 command_line_param_handler("E", false, function (expr, ctx) {});
69 command_line_param_handler("chrome", true, function (uri, ctx) {
70 try {
71 make_chrome_window(uri);
72 } catch (e) { dump_error(e); }
73 });
74 command_line_param_handler("q", false, function () {
75 dumpln ("w: -q may only be used as the first argument.");
76 });
78 command_line_param_handler("cwd", false, function (dir, ctx) {
79 if (ctx.config == null)
80 ctx.config = {};
81 ctx.config.cwd = dir;
82 });
84 command_line_param_handler("f", true, function (command) {
85 var ctx = {
86 window: window_watcher.activeWindow
88 call_interactively(ctx, command);
89 });
91 function handle_command_line(cmdline)
93 try {
94 var suppress_default = false;
95 var suppress_rc = false;
97 var i = 0;
99 /* -q must be the first argument, if it is given */
100 if (cmdline.length > 0 && cmdline.getArgument(0) == "-q")
102 suppress_rc = true;
103 i++;
106 var initial_launch = (cmdline.state == cmdline.STATE_INITIAL_LAUNCH);
108 if (initial_launch) {
109 let j = i;
110 while (j + 1 < cmdline.length) {
111 if (cmdline.getArgument(j) == "-E") {
112 eval(cmdline.getArgument(j+1));
113 cmdline.removeArguments(j, j+1);
114 } else
115 ++j;
118 let load_default_modules = get_pref("conkeror.loadDefaultModules");
119 let load_mods = new RegExp("^(" + get_pref("conkeror.loadModules") + ")$");
120 try {
121 let branch = preferences.getBranch("conkeror.load.");
122 for each (let m in branch.getChildList("", {})) {
123 let val;
124 try {
125 val = branch.getIntPref(m);
126 } catch (e) {
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)))
132 require(m + ".js");
134 } catch (e) {dump_error(e);}
137 if (! suppress_rc && initial_launch)
139 try {
140 load_rc ();
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);
150 if (arg[0] == '-') {
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;
156 if (handler.func) {
157 if (handler.param) {
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");
161 continue;
163 var param = cmdline.getArgument (i);
164 handler.func(param, ctx);
165 } else {
166 handler.func(ctx);
169 continue;
170 } else {
171 dump ("w: unknown command switch `"+arg+"'.\n");
173 } else {
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);
198 } catch (e) {
199 dumpln("Error processing command line.");
200 dump_error(e);