Prepare new Debian package
[conkeror.git] / modules / command-line.js
blobbc6b204a386fad74c64a63db02d875f3ac7075ab
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 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         if (ctx.window)
93             ctx.buffer = ctx.window.buffers.current;
94         co_call(call_interactively(ctx, command));
95     });
97 command_line_param_handler("l", false, function (path, ctx) {
98         try {
99             load_rc(path, function(path) ctx.command_line.resolveFile(path));
100         } catch (e) { dump_error(e);  }
101     });
103 // note `u' must be called as +u because Mozilla consumes -u
104 command_line_handler("u", false, function (ctx) {
105         // hack to make sure we send this command to a window
106         if (! ctx.window) {
107             ctx.window = get_recent_conkeror_window();
108             ctx.buffer = ctx.window.buffers.current;
109         }
110         co_call(call_interactively(ctx, "universal-argument"));
111     });
113 function handle_command_line (cmdline) {
114     try {
115         this.command_line = [];
116         for (let i = 0, clen = cmdline.length; i < clen; ++i)
117             command_line.push(cmdline.getArgument(i));
119         var suppress_default = false;
120         var suppress_rc = false;
122         var i = 0;
124         /* -q must be the first argument, if it is given */
125         if (cmdline.length > 0 && cmdline.getArgument(0) == "-q") {
126             suppress_rc = true;
127             i++;
128         }
130         var initial_launch = (cmdline.state == cmdline.STATE_INITIAL_LAUNCH);
132         if (initial_launch) {
133             let j = i;
134             while (j + 1 < cmdline.length) {
135                 if (cmdline.getArgument(j) == "-E") {
136                     eval(cmdline.getArgument(j+1));
137                     cmdline.removeArguments(j, j+1);
138                 } else
139                     ++j;
140             }
142             let load_default_modules = get_pref("conkeror.loadDefaultModules");
143             let load_mods = new RegExp("^(" + get_pref("conkeror.loadModules") + ")$");
144             try {
145                 let branch = preferences.getBranch("conkeror.load.");
146                 for each (let m in branch.getChildList("", {})) {
147                     let val;
148                     try {
149                         val = branch.getIntPref(m);
150                     } catch (e) {
151                         dumpln("Error: Preference 'conkeror.load." + m + "' has non-integer value.");
152                     }
153                     if ((val > 0 && (load_default_modules > 0 ||
154                                      ((load_default_modules == 0) && branch.prefHasUserValue(m)))) ||
155                         (val >= 0 && load_mods.test(m)))
156                         require(m + ".js");
157                 }
158             } catch (e) {dump_error(e);}
159         }
161         if (! suppress_rc && initial_launch) {
162             try {
163                 load_rc ();
164             } catch (e) { dump (e + "\n"); }
165         } else if (suppress_rc && ! initial_launch) {
166             dumpln ("w: attempt to suppress load_rc in remote invocation");
167         }
168         var ctx = new interactive_context();
169         ctx.command_line = cmdline;
170         ctx.local = { cwd: cmdline.resolveFile("."),
171                       __proto__: conkeror }
173         for (let clen = cmdline.length; i < clen; ++i) {
174             var arg = cmdline.getArgument(i);
175             if (arg[0] == '-' || arg[0] == '+') {
176                 var arg1 = arg.substring(1);
177                 if (arg1 in command_line_handlers) {
178                     var handler = command_line_handlers[arg1];
179                     if (handler.suppress_default)
180                         suppress_default = true;
181                     if (handler.func) {
182                         if (handler.param) {
183                             i++; // increment the argument counter to skip the parameter
184                             if (i >= cmdline.length) {
185                                 dump("w: ignoring command switch `"+arg+"' because no argument was provided.\n");
186                                 continue;
187                             }
188                             var param = cmdline.getArgument(i);
189                             handler.func(param, ctx);
190                         } else {
191                             handler.func(ctx);
192                         }
193                     }
194                     continue;
195                 } else {
196                     dump("w: unknown command switch `"+arg+"'.\n");
197                 }
198             } else {
199                 // something other than a switch was passed on the command
200                 // line.  suppress the default window, and call the
201                 // user-configurable remoting function on it.
202                 //
203                 suppress_default = true;
204                 url_remoting_fn(arg, ctx);
205             }
206         }
208         // we are greedy and handle all command line arguments.  remove
209         // everything from the command line object, so no other
210         // components can see them.
211         //
212         if (cmdline.length > 0) {
213             cmdline.removeArguments(0, cmdline.length - 1);
214         }
216         // no args were found for url_remoting_fn, and no switches
217         // explicitly suppressed the creation of a default window
218         // (e.g. -batch or -daemon)
219         //
220         if (! suppress_default) {
221             url_remoting_fn(homepage, ctx);
222         }
223     } catch (e) {
224         dumpln("Error processing command line.");
225         dump_error(e);
226     }
227     conkeror_started = true;