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