gmane page-mode: binding for browser-object-links
[conkeror.git] / modules / command-line.js
blob78302436895a806571a1cb0bbaab11a14c9b06a9
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,
12     "The function given as the value of this variable gets called for "+
13     "each datum given on the command-line not a switch or the value of "+
14     "a switch.  Such data are typically urls.  Some typical values are "+
15     "load_url_in_new_window (default), load_url_in_new_buffer, or "+
16     "load_url_in_current_buffer.");
19  * load_url_in_new_window is a function intended for use as
20  * a value of `url_remoting_fn'.  Every url given on the
21  * command line will be loaded in a new window.
22  */
23 function load_url_in_new_window(url, ctx) {
24     make_window(buffer_creator(content_buffer, $load = url, $configuration = ctx.config));
28  * load_url_in_new_buffer is a function intended for use as
29  * a value of `url_remoting_fn'.  Every url given on the
30  * command line will be loaded in a new buffer in the most
31  * recently used window, or a new window if none exist.
32  */
33 function load_url_in_new_buffer(url, ctx) {
34     create_buffer_in_current_window(buffer_creator(content_buffer, $load = url, $configuration = ctx.config),
35                                     OPEN_NEW_BUFFER, true /* focus the new window */);
39  * load_url_in_current_buffer is a function intended for use
40  * as a value of `url_remoting_fn'.  Every url given on the
41  * command line will be loaded in the current buffer of the
42  * most recently used window.  This makes it useful for only
43  * one url at a time.  When there are no conkeror windows
44  * open, the url will be loaded in a new window.
45  */
46 function  load_url_in_current_buffer(url,ctx) {
47     var win = get_recent_conkeror_window();
48     if (win) {
49         browser_object_follow(win.buffers.current, OPEN_CURRENT_BUFFER, url);
50     } else {
51         load_url_in_new_window(url, ctx);
52     }
55 function command_line_handler(name, suppress_default, handler)
57     command_line_handlers[name] = { suppress_default: suppress_default, func: handler };
60 function command_line_param_handler(name, suppress_default, handler)
62     command_line_handlers[name] = { suppress_default: suppress_default,
63                                     param: true,
64                                     func: handler };
67 command_line_handler("batch", true);
68 command_line_param_handler("e", true, function (expr, ctx) {
69         eval(expr);
70     });
72 command_line_param_handler("E", false, function (expr, ctx) {});
74 command_line_param_handler("chrome", true, function (uri, ctx) {
75         try {
76             make_chrome_window(uri);
77         } catch (e) { dump_error(e); }
78     });
79 command_line_param_handler("q", false, function () {
80         dumpln ("w: -q may only be used as the first argument.");
81     });
83 command_line_param_handler("f", true, function (command, ctx) {
84         ctx.window = window_watcher.activeWindow;
85         call_interactively(ctx, command);
86     });
88 command_line_param_handler("l", false, function (path, ctx) {
89         try {
90             load_rc(ctx.command_line.resolveFile(path).path);
91         } catch (e) { dump_error(e);  }
92     });
94 command_line_handler("uu", false, function (ctx) {
95         if (! ctx.window)
96             ctx.window = window_watcher.activeWindow;
97         call_interactively(ctx, "universal-argument");
98     });
100 function handle_command_line(cmdline)
102     try {
103         var suppress_default = false;
104         var suppress_rc = false;
106         var i = 0;
108         /* -q must be the first argument, if it is given */
109         if (cmdline.length > 0 && cmdline.getArgument(0) == "-q")
110         {
111             suppress_rc = true;
112             i++;
113         }
115         var initial_launch = (cmdline.state == cmdline.STATE_INITIAL_LAUNCH);
117         if (initial_launch) {
118             let j = i;
119             while (j + 1 < cmdline.length) {
120                 if (cmdline.getArgument(j) == "-E") {
121                     eval(cmdline.getArgument(j+1));
122                     cmdline.removeArguments(j, j+1);
123                 } else
124                     ++j;
125             }
127             let load_default_modules = get_pref("conkeror.loadDefaultModules");
128             let load_mods = new RegExp("^(" + get_pref("conkeror.loadModules") + ")$");
129             try {
130                 let branch = preferences.getBranch("conkeror.load.");
131                 for each (let m in branch.getChildList("", {})) {
132                     let val;
133                     try {
134                         val = branch.getIntPref(m);
135                     } catch (e) {
136                         dumpln("Error: Preference 'conkeror.load." + m + "' has non-integer value.");
137                     }
138                     if ((val > 0 && (load_default_modules > 0 ||
139                                      ((load_default_modules == 0) && branch.prefHasUserValue(m)))) ||
140                         (val >= 0 && load_mods.test(m)))
141                         require(m + ".js");
142                 }
143             } catch (e) {dump_error(e);}
144         }
146         if (! suppress_rc && initial_launch)
147         {
148             try {
149                 load_rc ();
150             } catch (e) { dump (e + "\n"); }
151         } else if (suppress_rc && ! initial_launch) {
152             dumpln ("w: attempt to suppress load_rc in remote invocation");
153         }
154         var ctx = {command_line: cmdline,
155                    config: {
156                        cwd: cmdline.resolveFile(".").path
157                    }
158                   }; // command-line processing context
160         for (; i < cmdline.length; ++i)
161         {
162             var arg = cmdline.getArgument(i);
163             if (arg[0] == '-') {
164                 var arg1 = arg.substring(1);
165                 if (arg1 in command_line_handlers) {
166                     var handler = command_line_handlers[arg1];
167                     if (handler.suppress_default)
168                         suppress_default = true;
169                     if (handler.func) {
170                         if (handler.param) {
171                             i++; // increment the argument counter to skip the parameter
172                             if (i >= cmdline.length) {
173                                 dump ("w: ignoring command switch `"+arg+"' because no argument was provided.\n");
174                                 continue;
175                             }
176                             var param = cmdline.getArgument (i);
177                             handler.func(param, ctx);
178                         } else {
179                             handler.func(ctx);
180                         }
181                     }
182                     continue;
183                 } else {
184                     dump ("w: unknown command switch `"+arg+"'.\n");
185                 }
186             } else {
187                 // something other than a switch was passed on the command
188                 // line.  suppress the default window, and call the
189                 // user-configurable remoting function on it.
190                 //
191                 suppress_default = true;
192                 url_remoting_fn (arg, ctx);
193             }
194         }
196         // we are greedy and handle all command line arguments.  remove
197         // everything from the command line object, so no other
198         // components can see them.
199         //
200         if (cmdline.length > 0) {
201             cmdline.removeArguments(0, cmdline.length - 1);
202         }
204         // no args were found for url_remoting_fn, and no switches
205         // explicitly suppressed the creation of a default window
206         // (e.g. -batch or -daemon)
207         //
208         if (! suppress_default) {
209             url_remoting_fn(homepage, ctx);
210         }
211     } catch (e) {
212         dumpln("Error processing command line.");
213         dump_error(e);
214     }