Add facilities for querying information about installed extensions
[conkeror.git] / modules / command-line.js
blob80348cc431e93e541a913cd3414e9bdc23d587c0
1 var command_line_handlers = [];
3 define_variable("url_remoting_fn", load_url_in_new_window);
5 function load_url_in_new_window(url, ctx) {
6     make_window(buffer_creator(content_buffer, $load = url, $configuration = ctx.config));
9 function load_url_in_new_buffer(url, ctx) {
10     create_buffer_in_current_window(buffer_creator(content_buffer, $load = url, $configuration = ctx.config),
11                                     OPEN_NEW_BUFFER, true /* focus the new window */);
14 function command_line_handler(name, suppress_default, handler)
16     command_line_handlers[name] = { suppress_default: suppress_default, func: handler };
19 function command_line_param_handler(name, suppress_default, handler)
21     command_line_handlers[name] = { suppress_default: suppress_default,
22                                     param: true,
23                                     func: handler };
26 command_line_handler("batch", true);
27 command_line_param_handler("e", false, function (expr, ctx) {
28         eval(expr);
29     });
31 command_line_param_handler("E", false, function (expr, ctx) {});
33 command_line_param_handler("chrome", true, function (uri, ctx) {
34         try {
35             make_chrome_window(uri);
36         } catch (e) { dump_error(e); }
37     });
38 command_line_param_handler("q", false, function () {
39         dumpln ("w: -q may only be used as the first argument.");
40     });
42 command_line_param_handler("cwd", false, function (dir, ctx) {
43         if (ctx.config == null)
44             ctx.config = {};
45         ctx.config.cwd = dir;
46     });
49 function handle_command_line(cmdline)
51     try {
52         var suppress_default = false;
53         var suppress_rc = false;
55         var i = 0;
57         /* -q must be the first argument, if it is given */
58         if (cmdline.length > 0 && cmdline.getArgument(0) == "-q")
59         {
60             suppress_rc = true;
61             i++;
62         }
64         var initial_launch = (cmdline.state == cmdline.STATE_INITIAL_LAUNCH);
66         if (initial_launch) {
67             let j = i;
68             while (j + 1 < cmdline.length) {
69                 if (cmdline.getArgument(j) == "-E") {
70                     eval(cmdline.getArgument(j+1));
71                     cmdline.removeArguments(j, j+1);
72                 } else
73                     ++j;
74             }
76             let load_default_modules = get_pref("conkeror.loadDefaultModules");
77             try {
78                 let branch = preferences.getBranch("conkeror.load.");
79                 for each (let m in branch.getChildList("", {})) {
80                     try {
81                         let val = branch.getBoolPref(m);
82                         if (val && (load_default_modules || branch.prefHasUserValue(m)))
83                             require(m + ".js");
84                     } catch (e) {
85                         dumpln("Error: Preference 'conkeror.load." + m + "' has non-boolean value.");
86                     }
87                 }
88             } catch (e) {dump_error(e);}
89         }
91         if (! suppress_rc && initial_launch)
92         {
93             try {
94                 load_rc ();
95             } catch (e) { dump (e + "\n"); }
96         } else if (suppress_rc && ! initial_launch) {
97             dumpln ("w: attempt to suppress load_rc in remote invocation");
98         }
99         var ctx = {}; // command-line processing context
101         for (; i < cmdline.length; ++i)
102         {
103             var arg = cmdline.getArgument(i);
104             if (arg[0] == '-') {
105                 var arg1 = arg.substring(1);
106                 if (arg1 in command_line_handlers) {
107                     var handler = command_line_handlers[arg1];
108                     if (handler.suppress_default)
109                         suppress_default = true;
110                     if (handler.func) {
111                         if (handler.param) {
112                             i++; // increment the argument counter to skip the parameter
113                             if (i >= cmdline.length) {
114                                 dump ("w: ignoring command switch `"+arg+"' because no argument was provided.\n");
115                                 continue;
116                             }
117                             var param = cmdline.getArgument (i);
118                             handler.func(param, ctx);
119                         } else {
120                             handler.func(ctx);
121                         }
122                     }
123                     continue;
124                 }
125             }
127             // something other than a switch was passed on the command
128             // line.  suppress the default window, and call the
129             // user-configurable remoting function on it.
130             //
131             suppress_default = true;
132             url_remoting_fn (arg, ctx);
133         }
135         // we are greedy and handle all command line arguments.  remove
136         // everything from the command line object, so no other
137         // components can see them.
138         //
139         if (cmdline.length > 0) {
140             cmdline.removeArguments(0, cmdline.length - 1);
141         }
143         // no args were found for url_remoting_fn, and no switches
144         // explicitly suppressed the creation of a default window
145         // (e.g. -batch or -daemon)
146         //
147         if (! suppress_default) {
148             url_remoting_fn(homepage, ctx);
149         }
150     } catch (e) {
151         dumpln("Error processing command line.");
152         dump_error(e);
153     }