browser-next-form-field: skip non-visible elements
[conkeror.git] / modules / command-line.js
blobe26a26b986f59e9c9248e9e6459f1d519e7a29c2
1 /**
2  * (C) Copyright 2007 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("url_remoting_fn", load_url_in_new_window);
14  * load_url_in_new_window is a function intended for use as
15  * a value of `url_remoting_fn'.  Every url given on the
16  * command line will be loaded in a new window.
17  */
18 function load_url_in_new_window(url, ctx) {
19     make_window(buffer_creator(content_buffer, $load = url, $configuration = ctx.config));
23  * load_url_in_new_buffer is a function intended for use as
24  * a value of `url_remoting_fn'.  Every url given on the
25  * command line will be loaded in a new buffer in the most
26  * recently used window, or a new window if none exist.
27  */
28 function load_url_in_new_buffer(url, ctx) {
29     create_buffer_in_current_window(buffer_creator(content_buffer, $load = url, $configuration = ctx.config),
30                                     OPEN_NEW_BUFFER, true /* focus the new window */);
34  * load_url_in_current_buffer is a function intended for use
35  * as a value of `url_remoting_fn'.  Every url given on the
36  * command line will be loaded in the current buffer of the
37  * most recently used window.  This makes it useful for only
38  * one url at a time.  When there are no conkeror windows
39  * open, the url will be loaded in a new window.
40  */
41 function  load_url_in_current_buffer(url,ctx) {
42     var win;
43     if (win = get_recent_conkeror_window()) {
44         browser_object_follow(win.buffers.current, OPEN_CURRENT_BUFFER, url);
45     } else {
46         load_url_in_new_window(url, ctx);
47     }
50 function command_line_handler(name, suppress_default, handler)
52     command_line_handlers[name] = { suppress_default: suppress_default, func: handler };
55 function command_line_param_handler(name, suppress_default, handler)
57     command_line_handlers[name] = { suppress_default: suppress_default,
58                                     param: true,
59                                     func: handler };
62 command_line_handler("batch", true);
63 command_line_param_handler("e", true, function (expr, ctx) {
64         eval(expr);
65     });
67 command_line_param_handler("E", false, function (expr, ctx) {});
69 command_line_param_handler("chrome", true, function (uri, ctx) {
70         try {
71             make_chrome_window(uri);
72         } catch (e) { dump_error(e); }
73     });
74 command_line_param_handler("q", false, function () {
75         dumpln ("w: -q may only be used as the first argument.");
76     });
78 command_line_param_handler("cwd", false, function (dir, ctx) {
79         if (ctx.config == null)
80             ctx.config = {};
81         ctx.config.cwd = dir;
82     });
84 command_line_param_handler("f", true, function (command, ctx) {
85         ctx.window = window_watcher.activeWindow;
86         call_interactively(ctx, command);
87     });
89 command_line_handler("uu", false, function (ctx) {
90         if (! ctx.window)
91             ctx.window = window_watcher.activeWindow;
92         call_interactively(ctx, "universal-argument");
93     });
95 function handle_command_line(cmdline)
97     try {
98         var suppress_default = false;
99         var suppress_rc = false;
101         var i = 0;
103         /* -q must be the first argument, if it is given */
104         if (cmdline.length > 0 && cmdline.getArgument(0) == "-q")
105         {
106             suppress_rc = true;
107             i++;
108         }
110         var initial_launch = (cmdline.state == cmdline.STATE_INITIAL_LAUNCH);
112         if (initial_launch) {
113             let j = i;
114             while (j + 1 < cmdline.length) {
115                 if (cmdline.getArgument(j) == "-E") {
116                     eval(cmdline.getArgument(j+1));
117                     cmdline.removeArguments(j, j+1);
118                 } else
119                     ++j;
120             }
122             let load_default_modules = get_pref("conkeror.loadDefaultModules");
123             let load_mods = new RegExp("^(" + get_pref("conkeror.loadModules") + ")$");
124             try {
125                 let branch = preferences.getBranch("conkeror.load.");
126                 for each (let m in branch.getChildList("", {})) {
127                     let val;
128                     try {
129                         val = branch.getIntPref(m);
130                     } catch (e) {
131                         dumpln("Error: Preference 'conkeror.load." + m + "' has non-integer value.");
132                     }
133                     if ((val > 0 && (load_default_modules > 0 ||
134                                      ((load_default_modules == 0) && branch.prefHasUserValue(m)))) ||
135                         (val >= 0 && load_mods.test(m)))
136                         require(m + ".js");
137                 }
138             } catch (e) {dump_error(e);}
139         }
141         if (! suppress_rc && initial_launch)
142         {
143             try {
144                 load_rc ();
145             } catch (e) { dump (e + "\n"); }
146         } else if (suppress_rc && ! initial_launch) {
147             dumpln ("w: attempt to suppress load_rc in remote invocation");
148         }
149         var ctx = {}; // command-line processing context
151         for (; i < cmdline.length; ++i)
152         {
153             var arg = cmdline.getArgument(i);
154             if (arg[0] == '-') {
155                 var arg1 = arg.substring(1);
156                 if (arg1 in command_line_handlers) {
157                     var handler = command_line_handlers[arg1];
158                     if (handler.suppress_default)
159                         suppress_default = true;
160                     if (handler.func) {
161                         if (handler.param) {
162                             i++; // increment the argument counter to skip the parameter
163                             if (i >= cmdline.length) {
164                                 dump ("w: ignoring command switch `"+arg+"' because no argument was provided.\n");
165                                 continue;
166                             }
167                             var param = cmdline.getArgument (i);
168                             handler.func(param, ctx);
169                         } else {
170                             handler.func(ctx);
171                         }
172                     }
173                     continue;
174                 } else {
175                     dump ("w: unknown command switch `"+arg+"'.\n");
176                 }
177             } else {
178                 // something other than a switch was passed on the command
179                 // line.  suppress the default window, and call the
180                 // user-configurable remoting function on it.
181                 //
182                 suppress_default = true;
183                 url_remoting_fn (arg, ctx);
184             }
185         }
187         // we are greedy and handle all command line arguments.  remove
188         // everything from the command line object, so no other
189         // components can see them.
190         //
191         if (cmdline.length > 0) {
192             cmdline.removeArguments(0, cmdline.length - 1);
193         }
195         // no args were found for url_remoting_fn, and no switches
196         // explicitly suppressed the creation of a default window
197         // (e.g. -batch or -daemon)
198         //
199         if (! suppress_default) {
200             url_remoting_fn(homepage, ctx);
201         }
202     } catch (e) {
203         dumpln("Error processing command line.");
204         dump_error(e);
205     }