Support Debian's upcoming xulrunner-1.9.1 packages
[conkeror.git] / modules / command-line.js
blob97348ec9f8d2c034afe1e6ce3c311367d9365a1a
1 /**
2  * (C) Copyright 2007 John J. Foerch
3  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
4  *
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("conkeror_started", false,
12     "True if conkeror has started, false if conkeror is still starting.");
14 define_variable("url_remoting_fn", load_url_in_new_window,
15     "The function given as the value of this variable gets called for "+
16     "each datum given on the command-line not a switch or the value of "+
17     "a switch.  Such data are typically urls.  Some typical values are "+
18     "load_url_in_new_window (default), load_url_in_new_buffer, or "+
19     "load_url_in_current_buffer.");
22  * load_url_in_new_window is a function intended for use as
23  * a value of `url_remoting_fn'.  Every url given on the
24  * command line will be loaded in a new window.
25  */
26 function load_url_in_new_window (url, ctx) {
27     make_window(buffer_creator(content_buffer,
28                                $opener = ctx,
29                                $load = url));
33  * load_url_in_new_buffer is a function intended for use as
34  * a value of `url_remoting_fn'.  Every url given on the
35  * command line will be loaded in a new buffer in the most
36  * recently used window, or a new window if none exist.
37  */
38 function load_url_in_new_buffer (url, ctx) {
39     create_buffer_in_current_window(
40         buffer_creator(content_buffer,
41                        $opener = ctx,
42                        $load = url),
43         OPEN_NEW_BUFFER, true /* focus the new window */);
47  * load_url_in_current_buffer is a function intended for use
48  * as a value of `url_remoting_fn'.  Every url given on the
49  * command line will be loaded in the current buffer of the
50  * most recently used window.  This makes it useful for only
51  * one url at a time.  When there are no conkeror windows
52  * open, the url will be loaded in a new window.
53  */
54 function load_url_in_current_buffer (url, ctx) {
55     var win = get_recent_conkeror_window();
56     if (win) {
57         browser_object_follow(win.buffers.current, OPEN_CURRENT_BUFFER, url);
58     } else {
59         load_url_in_new_window(url, ctx);
60     }
63 function command_line_handler (name, suppress_default, handler) {
64     command_line_handlers[name] = { suppress_default: suppress_default, func: handler };
67 function command_line_param_handler (name, suppress_default, handler) {
68     command_line_handlers[name] = { suppress_default: suppress_default,
69                                     param: true,
70                                     func: handler };
73 command_line_handler("batch", true);
74 command_line_param_handler("e", true, function (expr, ctx) {
75         eval(expr);
76     });
78 command_line_param_handler("E", false, function (expr, ctx) {});
80 command_line_param_handler("chrome", true, function (uri, ctx) {
81         try {
82             make_chrome_window(uri);
83         } catch (e) { dump_error(e); }
84     });
85 command_line_param_handler("q", false, function () {
86         dumpln ("w: -q may only be used as the first argument.");
87     });
89 command_line_param_handler("f", true, function (command, ctx) {
90         // hack to make sure we send this command to a window
91         ctx.window = get_recent_conkeror_window();
92         ctx.buffer = ctx.window.buffers.current;
93         call_interactively(ctx, command);
94     });
96 command_line_param_handler("l", false, function (path, ctx) {
97         try {
98             load_rc(ctx.command_line.resolveFile(path).path);
99         } catch (e) { dump_error(e);  }
100     });
102 // note `u' must be called as +u because Mozilla consumes -u
103 command_line_handler("u", false, function (ctx) {
104         // hack to make sure we send this command to a window
105         if (! ctx.window) {
106             ctx.window = get_recent_conkeror_window();
107             ctx.buffer = ctx.window.buffers.current;
108         }
109         call_interactively(ctx, "universal-argument");
110     });
112 function handle_command_line (cmdline) {
113     try {
114         this.command_line = [];
115         for (let i = 0; i < cmdline.length; ++i)
116             command_line.push(cmdline.getArgument(i));
118         var suppress_default = false;
119         var suppress_rc = false;
121         var i = 0;
123         /* -q must be the first argument, if it is given */
124         if (cmdline.length > 0 && cmdline.getArgument(0) == "-q") {
125             suppress_rc = true;
126             i++;
127         }
129         var initial_launch = (cmdline.state == cmdline.STATE_INITIAL_LAUNCH);
131         if (initial_launch) {
132             let j = i;
133             while (j + 1 < cmdline.length) {
134                 if (cmdline.getArgument(j) == "-E") {
135                     eval(cmdline.getArgument(j+1));
136                     cmdline.removeArguments(j, j+1);
137                 } else
138                     ++j;
139             }
141             let load_default_modules = get_pref("conkeror.loadDefaultModules");
142             let load_mods = new RegExp("^(" + get_pref("conkeror.loadModules") + ")$");
143             try {
144                 let branch = preferences.getBranch("conkeror.load.");
145                 for each (let m in branch.getChildList("", {})) {
146                     let val;
147                     try {
148                         val = branch.getIntPref(m);
149                     } catch (e) {
150                         dumpln("Error: Preference 'conkeror.load." + m + "' has non-integer value.");
151                     }
152                     if ((val > 0 && (load_default_modules > 0 ||
153                                      ((load_default_modules == 0) && branch.prefHasUserValue(m)))) ||
154                         (val >= 0 && load_mods.test(m)))
155                         require(m + ".js");
156                 }
157             } catch (e) {dump_error(e);}
158         }
160         if (! suppress_rc && initial_launch) {
161             try {
162                 load_rc ();
163             } catch (e) { dump (e + "\n"); }
164         } else if (suppress_rc && ! initial_launch) {
165             dumpln ("w: attempt to suppress load_rc in remote invocation");
166         }
167         var ctx = new interactive_context();
168         ctx.command_line = cmdline;
169         ctx.local = { cwd: cmdline.resolveFile("."),
170                       __proto__: conkeror }
172         for (; i < cmdline.length; ++i) {
173             var arg = cmdline.getArgument(i);
174             if (arg[0] == '-' || arg[0] == '+') {
175                 var arg1 = arg.substring(1);
176                 if (arg1 in command_line_handlers) {
177                     var handler = command_line_handlers[arg1];
178                     if (handler.suppress_default)
179                         suppress_default = true;
180                     if (handler.func) {
181                         if (handler.param) {
182                             i++; // increment the argument counter to skip the parameter
183                             if (i >= cmdline.length) {
184                                 dump("w: ignoring command switch `"+arg+"' because no argument was provided.\n");
185                                 continue;
186                             }
187                             var param = cmdline.getArgument(i);
188                             handler.func(param, ctx);
189                         } else {
190                             handler.func(ctx);
191                         }
192                     }
193                     continue;
194                 } else {
195                     dump("w: unknown command switch `"+arg+"'.\n");
196                 }
197             } else {
198                 // something other than a switch was passed on the command
199                 // line.  suppress the default window, and call the
200                 // user-configurable remoting function on it.
201                 //
202                 suppress_default = true;
203                 url_remoting_fn(arg, ctx);
204             }
205         }
207         // we are greedy and handle all command line arguments.  remove
208         // everything from the command line object, so no other
209         // components can see them.
210         //
211         if (cmdline.length > 0) {
212             cmdline.removeArguments(0, cmdline.length - 1);
213         }
215         // no args were found for url_remoting_fn, and no switches
216         // explicitly suppressed the creation of a default window
217         // (e.g. -batch or -daemon)
218         //
219         if (! suppress_default) {
220             url_remoting_fn(homepage, ctx);
221         }
222     } catch (e) {
223         dumpln("Error processing command line.");
224         dump_error(e);
225     }
226     conkeror_started = true;