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