isearch-backspace, isearch-done: docstrings
[conkeror.git] / modules / command-line.js
blob01fb1887cac4ba9d062b033f8ad0acb003076c45
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_new_buffer_background is a function intended for use as a
48  * value of `url_remoting_fn'.  Every url given on the command line will
49  * be loaded in a new background buffer in the most recently used window,
50  * or a new window if none exist.
51  */
52 function load_url_in_new_buffer_background (url, ctx) {
53     create_buffer_in_current_window(
54         buffer_creator(content_buffer,
55                        $opener = ctx,
56                        $load = url),
57         OPEN_NEW_BUFFER_BACKGROUND, true /* focus the new window */);
61  * load_url_in_current_buffer is a function intended for use
62  * as a value of `url_remoting_fn'.  Every url given on the
63  * command line will be loaded in the current buffer of the
64  * most recently used window.  This makes it useful for only
65  * one url at a time.  When there are no conkeror windows
66  * open, the url will be loaded in a new window.
67  */
68 function load_url_in_current_buffer (url, ctx) {
69     var win = get_recent_conkeror_window();
70     if (win) {
71         browser_object_follow(win.buffers.current, OPEN_CURRENT_BUFFER, url);
72     } else {
73         load_url_in_new_window(url, ctx);
74     }
77 function command_line_handler (name, suppress_default, handler) {
78     command_line_handlers[name] = { suppress_default: suppress_default, func: handler };
81 function command_line_param_handler (name, suppress_default, handler) {
82     command_line_handlers[name] = { suppress_default: suppress_default,
83                                     param: true,
84                                     func: handler };
87 command_line_handler("batch", true);
88 command_line_param_handler("e", true, function (expr, ctx) {
89         eval(expr);
90     });
92 command_line_param_handler("E", false, function (expr, ctx) {});
94 command_line_param_handler("chrome", true, function (uri, ctx) {
95         try {
96             make_chrome_window(uri);
97         } catch (e) { dump_error(e); }
98     });
99 command_line_param_handler("q", false, function () {
100         dumpln ("w: -q may only be used as the first argument.");
101     });
103 command_line_param_handler("f", true, function (command, ctx) {
104         // hack to make sure we send this command to a window
105         ctx.window = get_recent_conkeror_window();
106         if (ctx.window)
107             ctx.buffer = ctx.window.buffers.current;
108         co_call(call_interactively(ctx, command));
109     });
111 command_line_param_handler("l", false, function (path, ctx) {
112         try {
113             load(ctx.command_line.resolveFile(path));
114         } catch (e) {
115             dump_error(e);
116         }
117     });
119 // note `u' must be called as +u because Mozilla consumes -u
120 command_line_handler("u", false, function (ctx) {
121         // hack to make sure we send this command to a window
122         if (! ctx.window) {
123             ctx.window = get_recent_conkeror_window();
124             ctx.buffer = ctx.window.buffers.current;
125         }
126         co_call(call_interactively(ctx, "universal-argument"));
127     });
129 function handle_command_line (cmdline) {
130     try {
131         this.command_line = [];
132         for (let i = 0, clen = cmdline.length; i < clen; ++i)
133             command_line.push(cmdline.getArgument(i));
135         var suppress_default = false;
136         var suppress_rc = false;
138         var i = 0;
140         /* -q must be the first argument, if it is given */
141         if (cmdline.length > 0 && cmdline.getArgument(0) == "-q") {
142             suppress_rc = true;
143             i++;
144         }
146         var initial_launch = (cmdline.state == cmdline.STATE_INITIAL_LAUNCH);
148         if (initial_launch) {
149             let j = i;
150             while (j + 1 < cmdline.length) {
151                 if (cmdline.getArgument(j) == "-E") {
152                     eval(cmdline.getArgument(j+1));
153                     cmdline.removeArguments(j, j+1);
154                 } else
155                     ++j;
156             }
158             let load_default_modules = get_pref("conkeror.loadDefaultModules");
159             let load_mods = new RegExp("^(" + get_pref("conkeror.loadModules") + ")$");
160             try {
161                 let branch = preferences.getBranch("conkeror.load.");
162                 for each (let m in branch.getChildList("", {})) {
163                     let val;
164                     try {
165                         val = branch.getIntPref(m);
166                     } catch (e) {
167                         dumpln("Error: Preference 'conkeror.load." + m + "' has non-integer value.");
168                     }
169                     if ((val > 0 && (load_default_modules > 0 ||
170                                      ((load_default_modules == 0) && branch.prefHasUserValue(m)))) ||
171                         (val >= 0 && load_mods.test(m)))
172                         require(m + ".js");
173                 }
174             } catch (e) {dump_error(e);}
175         }
177         if (! suppress_rc && initial_launch) {
178             try {
179                 load_rc();
180             } catch (e) {
181                 dump_error(e);
182             }
183         } else if (suppress_rc && ! initial_launch) {
184             dumpln("w: attempt to suppress loading of rc in remote invocation");
185         }
186         var ctx = new interactive_context();
187         ctx.command_line = cmdline;
188         ctx.local = { __proto__: conkeror,
189                       // see https://bugzilla.mozilla.org/show_bug.cgi?id=476326
190                       cwd: cmdline.resolveURI(".").QueryInterface(Ci.nsIFileURL).file };
192         for (let clen = cmdline.length; i < clen; ++i) {
193             var arg = cmdline.getArgument(i);
194             if (arg[0] == '-' || arg[0] == '+') {
195                 var arg1 = arg.substring(1);
196                 if (arg1 in command_line_handlers) {
197                     var handler = command_line_handlers[arg1];
198                     if (handler.suppress_default)
199                         suppress_default = true;
200                     if (handler.func) {
201                         if (handler.param) {
202                             i++; // increment the argument counter to skip the parameter
203                             if (i >= cmdline.length) {
204                                 dump("w: ignoring command switch `"+arg+"' because no argument was provided.\n");
205                                 continue;
206                             }
207                             var param = cmdline.getArgument(i);
208                             handler.func(param, ctx);
209                         } else {
210                             handler.func(ctx);
211                         }
212                     }
213                     continue;
214                 } else {
215                     dump("w: unknown command switch `"+arg+"'.\n");
216                 }
217             } else {
218                 // something other than a switch was passed on the command
219                 // line.  suppress the default window, and call the
220                 // user-configurable remoting function on it.
221                 //
222                 suppress_default = true;
223                 url_remoting_fn(arg, ctx);
224             }
225         }
227         // we are greedy and handle all command line arguments.  remove
228         // everything from the command line object, so no other
229         // components can see them.
230         //
231         if (cmdline.length > 0) {
232             cmdline.removeArguments(0, cmdline.length - 1);
233         }
235         // no args were found for url_remoting_fn, and no switches
236         // explicitly suppressed the creation of a default window
237         // (e.g. -batch or -daemon)
238         //
239         if (! suppress_default) {
240             url_remoting_fn(homepage, ctx);
241         }
242     } catch (e) {
243         dumpln("Error processing command line.");
244         dump_error(e);
245     }
246     conkeror_started = true;
249 provide("command-line");