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