Add initial OpenSearch search engine support
[conkeror.git] / modules / command-line.js
blob61668f66567c4025153a174b32a58380d64b3124
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             let load_mods = new RegExp("^(" + get_pref("conkeror.loadModules") + ")$");
78             try {
79                 let branch = preferences.getBranch("conkeror.load.");
80                 for each (let m in branch.getChildList("", {})) {
81                     let val;
82                     try {
83                         val = branch.getIntPref(m);
84                     } catch (e) {
85                         dumpln("Error: Preference 'conkeror.load." + m + "' has non-integer value.");
86                     }
87                     if ((val > 0 && (load_default_modules > 0 ||
88                                      ((load_default_modules == 0) && branch.prefHasUserValue(m)))) ||
89                         (val >= 0 && load_mods.test(m)))
90                         require(m + ".js");
91                 }
92             } catch (e) {dump_error(e);}
93         }
95         if (! suppress_rc && initial_launch)
96         {
97             try {
98                 load_rc ();
99             } catch (e) { dump (e + "\n"); }
100         } else if (suppress_rc && ! initial_launch) {
101             dumpln ("w: attempt to suppress load_rc in remote invocation");
102         }
103         var ctx = {}; // command-line processing context
105         for (; i < cmdline.length; ++i)
106         {
107             var arg = cmdline.getArgument(i);
108             if (arg[0] == '-') {
109                 var arg1 = arg.substring(1);
110                 if (arg1 in command_line_handlers) {
111                     var handler = command_line_handlers[arg1];
112                     if (handler.suppress_default)
113                         suppress_default = true;
114                     if (handler.func) {
115                         if (handler.param) {
116                             i++; // increment the argument counter to skip the parameter
117                             if (i >= cmdline.length) {
118                                 dump ("w: ignoring command switch `"+arg+"' because no argument was provided.\n");
119                                 continue;
120                             }
121                             var param = cmdline.getArgument (i);
122                             handler.func(param, ctx);
123                         } else {
124                             handler.func(ctx);
125                         }
126                     }
127                     continue;
128                 }
129             }
131             // something other than a switch was passed on the command
132             // line.  suppress the default window, and call the
133             // user-configurable remoting function on it.
134             //
135             suppress_default = true;
136             url_remoting_fn (arg, ctx);
137         }
139         // we are greedy and handle all command line arguments.  remove
140         // everything from the command line object, so no other
141         // components can see them.
142         //
143         if (cmdline.length > 0) {
144             cmdline.removeArguments(0, cmdline.length - 1);
145         }
147         // no args were found for url_remoting_fn, and no switches
148         // explicitly suppressed the creation of a default window
149         // (e.g. -batch or -daemon)
150         //
151         if (! suppress_default) {
152             url_remoting_fn(homepage, ctx);
153         }
154     } catch (e) {
155         dumpln("Error processing command line.");
156         dump_error(e);
157     }