]> git.donarmstrong.com Git - dactyl.git/blob - common/modules/commands.jsm
1036f20cf09b7bf2263bf27bdaa9dfe1441daf90
[dactyl.git] / common / modules / commands.jsm
1 // Copyright (c) 2006-2008 by Martin Stubenschrott <stubenschrott@vimperator.org>
2 // Copyright (c) 2007-2011 by Doug Kearns <dougkearns@gmail.com>
3 // Copyright (c) 2008-2011 by Kris Maglione <maglione.k at Gmail>
4 //
5 // This work is licensed for reuse under an MIT license. Details are
6 // given in the LICENSE.txt file included with this file.
7 "use strict";
8
9 try {
10
11 Components.utils.import("resource://dactyl/bootstrap.jsm");
12 defineModule("commands", {
13     exports: ["ArgType", "Command", "Commands", "CommandOption", "Ex", "commands"],
14     require: ["contexts", "messages", "util"],
15     use: ["config", "options", "services", "template"]
16 }, this);
17
18 /**
19  * A structure representing the options available for a command.
20  *
21  * Do NOT create instances of this class yourself, use the helper method
22  * {@see Commands#add} instead
23  *
24  * @property {[string]} names An array of option names. The first name
25  *     is the canonical option name.
26  * @property {number} type The option's value type. This is one of:
27  *         (@link CommandOption.NOARG),
28  *         (@link CommandOption.STRING),
29  *         (@link CommandOption.BOOL),
30  *         (@link CommandOption.INT),
31  *         (@link CommandOption.FLOAT),
32  *         (@link CommandOption.LIST),
33  *         (@link CommandOption.ANY)
34  * @property {function} validator A validator function
35  * @property {function (CompletionContext, object)} completer A list of
36  *    completions, or a completion function which will be passed a
37  *    {@link CompletionContext} and an object like that returned by
38  *    {@link commands.parseArgs} with the following additional keys:
39  *      completeOpt - The name of the option currently being completed.
40  * @property {boolean} multiple Whether this option can be specified multiple times
41  * @property {string} description A description of the option
42  * @property {object} default The option's default value
43  */
44
45 var CommandOption = Struct("names", "type", "validator", "completer", "multiple", "description", "default");
46 CommandOption.defaultValue("description", function () "");
47 CommandOption.defaultValue("type", function () CommandOption.NOARG);
48 CommandOption.defaultValue("multiple", function () false);
49
50 var ArgType = Struct("description", "parse");
51 update(CommandOption, {
52     /**
53      * @property {object} The option argument is unspecified. Any argument
54      *     is accepted and caller is responsible for parsing the return
55      *     value.
56      * @final
57      */
58     ANY: 0,
59
60     /**
61      * @property {object} The option doesn't accept an argument.
62      * @final
63      */
64     NOARG: ArgType("no arg",  function (arg) !arg || null),
65     /**
66      * @property {object} The option accepts a boolean argument.
67      * @final
68      */
69     BOOL: ArgType("boolean", function parseBoolArg(val) Commands.parseBool(val)),
70     /**
71      * @property {object} The option accepts a string argument.
72      * @final
73      */
74     STRING: ArgType("string", function (val) val),
75     /**
76      * @property {object} The option accepts an integer argument.
77      * @final
78      */
79     INT: ArgType("int", function parseIntArg(val) parseInt(val)),
80     /**
81      * @property {object} The option accepts a float argument.
82      * @final
83      */
84     FLOAT: ArgType("float", function parseFloatArg(val) parseFloat(val)),
85     /**
86      * @property {object} The option accepts a string list argument.
87      *     E.g. "foo,bar"
88      * @final
89      */
90     LIST: ArgType("list", function parseListArg(arg, quoted) Option.splitList(quoted))
91 });
92
93 /**
94  * A class representing Ex commands. Instances are created by
95  * the {@link Commands} class.
96  *
97  * @param {string[]} specs The names by which this command can be invoked.
98  *     These are specified in the form "com[mand]" where "com" is a unique
99  *     command name prefix.
100  * @param {string} description A short one line description of the command.
101  * @param {function} action The action invoked by this command when executed.
102  * @param {Object} extraInfo An optional extra configuration hash. The
103  *     following properties are supported.
104  *         argCount    - see {@link Command#argCount}
105  *         bang        - see {@link Command#bang}
106  *         completer   - see {@link Command#completer}
107  *         count       - see {@link Command#count}
108  *         domains     - see {@link Command#domains}
109  *         heredoc     - see {@link Command#heredoc}
110  *         literal     - see {@link Command#literal}
111  *         options     - see {@link Command#options}
112  *         privateData - see {@link Command#privateData}
113  *         serialize   - see {@link Command#serialize}
114  *         subCommand  - see {@link Command#subCommand}
115  * @optional
116  * @private
117  */
118 var Command = Class("Command", {
119     init: function init(specs, description, action, extraInfo) {
120         specs = Array.concat(specs); // XXX
121         let parsedSpecs = extraInfo.parsedSpecs || Command.parseSpecs(specs);
122
123         this.specs = specs;
124         this.shortNames = array.compact(parsedSpecs.map(function (n) n[1]));
125         this.longNames = parsedSpecs.map(function (n) n[0]);
126         this.name = this.longNames[0];
127         this.names = array.flatten(parsedSpecs);
128         this.description = description;
129         this.action = action;
130
131         if (extraInfo)
132             update(this, extraInfo);
133         if (this.options)
134             this.options = this.options.map(CommandOption.fromArray, CommandOption);
135         for each (let option in this.options)
136             option.localeName = ["command", this.name, option.names[0]];
137     },
138
139     get toStringParams() [this.name, this.hive.name],
140
141     get identifier() this.hive.prefix + this.name,
142
143     get helpTag() ":" + this.name,
144
145     get lastCommand() this._lastCommand || commandline.command,
146     set lastCommand(val) { this._lastCommand = val; },
147
148     /**
149      * Execute this command.
150      *
151      * @param {Args} args The Args object passed to {@link #action}.
152      * @param {Object} modifiers Any modifiers to be passed to {@link #action}.
153      */
154     execute: function execute(args, modifiers) {
155         const { dactyl } = this.modules;
156
157         let context = args.context;
158         if (this.deprecated && !set.add(this.complained, context ? context.file : "[Command Line]")) {
159             let loc = contexts.context ? context.file + ":" + context.line + ": " : "";
160             dactyl.echoerr(loc + ":" + this.name + " is deprecated" +
161                            (isString(this.deprecated) ? ": " + this.deprecated : ""));
162         }
163
164         modifiers = modifiers || {};
165
166         if (args.count != null && !this.count)
167             throw FailedAssertion(_("command.noRange"));
168         if (args.bang && !this.bang)
169             throw FailedAssertion(_("command.noBang"));
170
171         return !dactyl.trapErrors(function exec() {
172             let extra = this.hive.argsExtra(args);
173             for (let k in properties(extra))
174                 if (!(k in args))
175                     Object.defineProperty(args, k, Object.getOwnPropertyDescriptor(extra, k));
176
177             if (this.always)
178                 this.always(args, modifiers);
179
180             if (!context || !context.noExecute)
181                 this.action(args, modifiers);
182         }, this);
183     },
184
185     /**
186      * Returns whether this command may be invoked via *name*.
187      *
188      * @param {string} name The candidate name.
189      * @returns {boolean}
190      */
191     hasName: function hasName(name) this.parsedSpecs.some(
192         function ([long, short]) name.indexOf(short) == 0 && long.indexOf(name) == 0),
193
194     /**
195      * A helper function to parse an argument string.
196      *
197      * @param {string} args The argument string to parse.
198      * @param {CompletionContext} complete A completion context.
199      *     Non-null when the arguments are being parsed for completion
200      *     purposes.
201      * @param {Object} extra Extra keys to be spliced into the
202      *     returned Args object.
203      * @returns {Args}
204      * @see Commands#parseArgs
205      */
206     parseArgs: function parseArgs(args, complete, extra) this.modules.commands.parseArgs(args, {
207         __proto__: this,
208         complete: complete,
209         extra: extra
210     }),
211
212     complained: Class.memoize(function () ({})),
213
214     /**
215      * @property {string[]} All of this command's name specs. e.g., "com[mand]"
216      */
217     specs: null,
218     /** @property {string[]} All of this command's short names, e.g., "com" */
219     shortNames: null,
220     /**
221      * @property {string[]} All of this command's long names, e.g., "command"
222      */
223     longNames: null,
224
225     /** @property {string} The command's canonical name. */
226     name: null,
227     /** @property {string[]} All of this command's long and short names. */
228     names: null,
229
230     /** @property {string} This command's description, as shown in :listcommands */
231     description: Messages.Localized(""),
232     /**
233      * @property {function (Args)} The function called to execute this command.
234      */
235     action: null,
236     /**
237      * @property {string} This command's argument count spec.
238      * @see Commands#parseArguments
239      */
240     argCount: 0,
241     /**
242      * @property {function (CompletionContext, Args)} This command's completer.
243      * @see CompletionContext
244      */
245     completer: null,
246     /** @property {boolean} Whether this command accepts a here document. */
247     hereDoc: false,
248     /**
249      * @property {boolean} Whether this command may be called with a bang,
250      *     e.g., :com!
251      */
252     bang: false,
253     /**
254      * @property {boolean} Whether this command may be called with a count,
255      *     e.g., :12bdel
256      */
257     count: false,
258     /**
259      * @property {function(args)} A function which should return a list
260      *     of domains referenced in the given args. Used in determining
261      *     whether to purge the command from history when clearing
262      *     private data.
263      */
264     domains: function (args) [],
265     /**
266      * @property {boolean} At what index this command's literal arguments
267      *     begin. For instance, with a value of 2, all arguments starting with
268      *     the third are parsed as a single string, with all quoting characters
269      *     passed literally. This is especially useful for commands which take
270      *     key mappings or Ex command lines as arguments.
271      */
272     literal: null,
273     /**
274      * @property {Array} The options this command takes.
275      * @see Commands@parseArguments
276      */
277     options: [],
278
279     optionMap: Class.memoize(function () array(this.options)
280                 .map(function (opt) opt.names.map(function (name) [name, opt]))
281                 .flatten().toObject()),
282
283     newArgs: function newArgs(base) {
284         let res = [];
285         update(res, base);
286         res.__proto__ = this.argsPrototype;
287         return res;
288     },
289
290     argsPrototype: Class.memoize(function argsPrototype() {
291         let res = update([], {
292                 __iterator__: function AP__iterator__() array.iterItems(this),
293
294                 command: this,
295
296                 explicitOpts: Class.memoize(function () ({})),
297
298                 has: function AP_has(opt) set.has(this.explicitOpts, opt) || typeof opt === "number" && set.has(this, opt),
299
300                 get literalArg() this.command.literal != null && this[this.command.literal] || "",
301
302                 // TODO: string: Class.memoize(function () { ... }),
303
304                 verify: function verify() {
305                     if (this.command.argCount) {
306                         util.assert((this.length > 0 || !/^[1+]$/.test(this.command.argCount)) &&
307                                     (this.literal == null || !/[1+]/.test(this.command.argCount) || /\S/.test(this.literalArg || "")),
308                                      _("error.argumentRequired"));
309
310                         util.assert((this.length == 0 || this.command.argCount !== "0") &&
311                                     (this.length <= 1 || !/^[01?]$/.test(this.command.argCount)),
312                                     _("error.trailing"));
313                     }
314                 }
315         });
316
317         this.options.forEach(function (opt) {
318             if (opt.default !== undefined)
319                 Object.defineProperty(res, opt.names[0],
320                                       Object.getOwnPropertyDescriptor(opt, "default") ||
321                                           { configurable: true, enumerable: true, get: function () opt.default });
322         });
323
324         return res;
325     }),
326
327     /**
328      * @property {boolean|function(args)} When true, invocations of this
329      *     command may contain private data which should be purged from
330      *     saved histories when clearing private data. If a function, it
331      *     should return true if an invocation with the given args
332      *     contains private data
333      */
334     privateData: true,
335     /**
336      * @property {function} Should return an array of *Object*s suitable to be
337      *     passed to {@link Commands#commandToString}, one for each past
338      *     invocation which should be restored on subsequent @dactyl startups.
339      */
340     serialize: null,
341     serialGroup: 50,
342     /**
343      * @property {number} If this command takes another ex command as an
344      *     argument, the index of that argument. Used in determining whether to
345      *     purge the command from history when clearing private data.
346      */
347     subCommand: null,
348     /**
349      * @property {boolean} Specifies whether this is a user command. User
350      *     commands may be created by plugins, or directly by users, and,
351      *     unlike basic commands, may be overwritten. Users and plugin authors
352      *     should create only user commands.
353      */
354     user: false,
355     /**
356      * @property {string} For commands defined via :command, contains the Ex
357      *     command line to be executed upon invocation.
358      */
359     replacementText: null
360 }, {
361     // TODO: do we really need more than longNames as a convenience anyway?
362     /**
363      *  Converts command name abbreviation specs of the form
364      *  'shortname[optional-tail]' to short and long versions:
365      *      ["abc[def]", "ghijkl"] ->  [["abcdef", "abc"], ["ghijlk"]]
366      *
367      *  @param {Array} specs An array of command name specs to parse.
368      *  @returns {Array}
369      */
370     parseSpecs: function parseSpecs(specs) {
371         return specs.map(function (spec) {
372             let [, head, tail] = /([^[]+)(?:\[(.*)])?/.exec(spec);
373             return tail ? [head + tail, head] : [head];
374         });
375     }
376 });
377
378 // Prototype.
379 var Ex = Module("Ex", {
380     Local: function Local(dactyl, modules, window) ({
381         get commands() modules.commands,
382         get context() modules.contexts.context
383     }),
384
385     _args: function E_args(cmd, args) {
386         args = Array.slice(args);
387
388         let res = cmd.newArgs({ context: this.context });
389         if (isObject(args[0]))
390             for (let [k, v] in Iterator(args.shift()))
391                 if (k == "!")
392                     res.bang = v;
393                 else if (k == "#")
394                     res.count = v;
395                 else {
396                     let opt = cmd.optionMap["-" + k];
397                     let val = opt.type && opt.type.parse(v);
398                     util.assert(val != null && (typeof val !== "number" || !isNaN(val)),
399                                 _("option.noSuch", k));
400                     Class.replaceProperty(args, opt.names[0], val);
401                     args.explicitOpts[opt.names[0]] = val;
402                 }
403         for (let [i, val] in array.iterItems(args))
404             res[i] = String(val);
405         return res;
406     },
407
408     _complete: function E_complete(cmd) let (self = this)
409         function _complete(context, func, obj, args) {
410             args = self._args(cmd, args);
411             args.completeArg = args.length - 1;
412             if (cmd.completer && args.length)
413                 return cmd.completer(context, args);
414         },
415
416     _run: function E_run(name) {
417         const self = this;
418         let cmd = this.commands.get(name);
419         util.assert(cmd, _("command.noSuch"));
420
421         return update(function exCommand(options) {
422             let args = self._args(cmd, arguments);
423             args.verify();
424             return cmd.execute(args);
425         }, {
426             dactylCompleter: self._complete(cmd)
427         });
428     },
429
430     __noSuchMethod__: function __noSuchMethod__(meth, args) this._run(meth).apply(this, args)
431 });
432
433 var CommandHive = Class("CommandHive", Contexts.Hive, {
434     init: function init(group) {
435         init.supercall(this, group);
436         this._map = {};
437         this._list = [];
438     },
439
440     /** @property {Iterator(Command)} @private */
441     __iterator__: function __iterator__() array.iterValues(this._list.sort(function (a, b) a.name > b.name)),
442
443     /** @property {string} The last executed Ex command line. */
444     repeat: null,
445
446     /**
447      * Adds a new command to the builtin hive. Accessible only to core
448      * dactyl code. Plugins should use group.commands.add instead.
449      *
450      * @param {string[]} names The names by which this command can be
451      *     invoked. The first name specified is the command's canonical
452      *     name.
453      * @param {string} description A description of the command.
454      * @param {function} action The action invoked by this command.
455      * @param {Object} extra An optional extra configuration hash.
456      * @optional
457      */
458     add: function add(names, description, action, extra, replace) {
459         const { commands, contexts } = this.modules;
460
461         extra = extra || {};
462         if (!extra.definedAt)
463             extra.definedAt = contexts.getCaller(Components.stack.caller);
464
465         extra.hive = this;
466         extra.parsedSpecs = Command.parseSpecs(names);
467
468         let names = array.flatten(extra.parsedSpecs);
469         let name = names[0];
470
471         util.assert(!names.some(function (name) name in commands.builtin._map),
472                     _("command.cantReplace", name));
473
474         util.assert(replace || names.every(function (name) !(name in this._map), this),
475                     _("command.wontReplace", name));
476
477         for (let name in values(names)) {
478             ex.__defineGetter__(name, function () this._run(name));
479             if (name in this._map)
480                 this.remove(name);
481         }
482
483         let self = this;
484         let closure = function () self._map[name];
485
486         memoize(this._map, name, function () commands.Command(names, description, action, extra));
487         memoize(this._list, this._list.length, closure);
488         for (let alias in values(names.slice(1)))
489             memoize(this._map, alias, closure);
490
491         return name;
492     },
493
494     _add: function _add(names, description, action, extra, replace) {
495         const { contexts } = this.modules;
496
497         extra = extra || {};
498         extra.definedAt = contexts.getCaller(Components.stack.caller.caller);
499         return this.add.apply(this, arguments);
500     },
501
502     /**
503      * Clear all commands.
504      * @returns {Command}
505      */
506     clear: function clear() {
507         util.assert(this.group.modifiable, _("command.cantDelete"));
508         this._map = {};
509         this._list = [];
510     },
511
512     /**
513      * Returns the command with matching *name*.
514      *
515      * @param {string} name The name of the command to return. This can be
516      *     any of the command's names.
517      * @param {boolean} full If true, only return a command if one of
518      *     its names matches *name* exactly.
519      * @returns {Command}
520      */
521     get: function get(name, full) this._map[name]
522             || !full && array.nth(this._list, function (cmd) cmd.hasName(name), 0)
523             || null,
524
525     /**
526      * Remove the user-defined command with matching *name*.
527      *
528      * @param {string} name The name of the command to remove. This can be
529      *     any of the command's names.
530      */
531     remove: function remove(name) {
532         util.assert(this.group.modifiable, _("command.cantDelete"));
533
534         let cmd = this.get(name);
535         this._list = this._list.filter(function (c) c !== cmd);
536         for (let name in values(cmd.names))
537             delete this._map[name];
538     }
539 });
540
541 /**
542  * @instance commands
543  */
544 var Commands = Module("commands", {
545     lazyInit: true,
546
547     Local: function Local(dactyl, modules, window) let ({ Group, contexts } = modules) ({
548         init: function init() {
549             this.Command = Class("Command", Command, { modules: modules });
550             update(this, {
551                 hives: contexts.Hives("commands", Class("CommandHive", CommandHive, { modules: modules })),
552                 user: contexts.hives.commands.user,
553                 builtin: contexts.hives.commands.builtin
554             });
555         },
556
557         get context() contexts.context,
558
559         get readHeredoc() modules.io.readHeredoc,
560
561         get allHives() contexts.allGroups.commands,
562
563         get userHives() this.allHives.filter(function (h) h !== this.builtin, this),
564
565         /**
566          * Executes an Ex command script.
567          *
568          * @param {string} string A string containing the commands to execute.
569          * @param {object} tokens An optional object containing tokens to be
570          *      interpolated into the command string.
571          * @param {object} args Optional arguments object to be passed to
572          *      command actions.
573          * @param {object} context An object containing information about
574          *      the file that is being or has been sourced to obtain the
575          *      command string.
576          */
577         execute: function execute(string, tokens, silent, args, context) {
578             contexts.withContext(context || this.context || { file: "[Command Line]", line: 1 },
579                                  function (context) {
580                 modules.io.withSavedValues(["readHeredoc"], function () {
581                     this.readHeredoc = function readHeredoc(end) {
582                         let res = [];
583                         contexts.context.line++;
584                         while (++i < lines.length) {
585                             if (lines[i] === end)
586                                 return res.join("\n");
587                             res.push(lines[i]);
588                         }
589                         util.assert(false, _("command.eof", end));
590                     };
591
592                     args = update({}, args || {});
593
594                     if (tokens && !callable(string))
595                         string = util.compileMacro(string, true);
596                     if (callable(string))
597                         string = string(tokens || {});
598
599                     let lines = string.split(/\r\n|[\r\n]/);
600                     let startLine = context.line;
601
602                     for (var i = 0; i < lines.length && !context.finished; i++) {
603                         // Deal with editors from Silly OSs.
604                         let line = lines[i].replace(/\r$/, "");
605
606                         context.line = startLine + i;
607
608                         // Process escaped new lines
609                         while (i < lines.length && /^\s*\\/.test(lines[i + 1]))
610                             line += "\n" + lines[++i].replace(/^\s*\\/, "");
611
612                         try {
613                             dactyl.execute(line, args);
614                         }
615                         catch (e) {
616                             if (!silent) {
617                                 e.message = context.file + ":" + context.line + ": " + e.message;
618                                 dactyl.reportError(e, true);
619                             }
620                         }
621                     }
622                 });
623             });
624         },
625
626         /**
627          * Displays a list of user-defined commands.
628          */
629         list: function list() {
630             const { commandline, completion } = this.modules;
631             function completerToString(completer) {
632                 if (completer)
633                     return [k for ([k, v] in Iterator(config.completers)) if (completer == completion.closure[v])][0] || "custom";
634                 return "";
635             }
636
637             if (!this.userHives.some(function (h) h._list.length))
638                 dactyl.echomsg(_("command.none"));
639             else
640                 commandline.commandOutput(
641                     <table>
642                         <tr highlight="Title">
643                             <td/>
644                             <td style="padding-right: 1em;"></td>
645                             <td style="padding-right: 1ex;">Name</td>
646                             <td style="padding-right: 1ex;">Args</td>
647                             <td style="padding-right: 1ex;">Range</td>
648                             <td style="padding-right: 1ex;">Complete</td>
649                             <td style="padding-right: 1ex;">Definition</td>
650                         </tr>
651                         <col style="min-width: 6em; padding-right: 1em;"/>
652                         {
653                             template.map(this.userHives, function (hive) let (i = 0)
654                                 <tr style="height: .5ex;"/> +
655                                 template.map(hive, function (cmd)
656                                     template.map(cmd.names, function (name)
657                                     <tr>
658                                         <td highlight="Title">{!i++ ? hive.name : ""}</td>
659                                         <td>{cmd.bang ? "!" : " "}</td>
660                                         <td>{cmd.name}</td>
661                                         <td>{cmd.argCount}</td>
662                                         <td>{cmd.count ? "0c" : ""}</td>
663                                         <td>{completerToString(cmd.completer)}</td>
664                                         <td>{cmd.replacementText || "function () { ... }"}</td>
665                                     </tr>)) +
666                                 <tr style="height: .5ex;"/>)
667                         }
668                     </table>);
669         }
670     }),
671
672     /**
673      * @property Indicates that no count was specified for this
674      *     command invocation.
675      * @final
676      */
677     COUNT_NONE: null,
678     /**
679      * @property {number} Indicates that the full buffer range (1,$) was
680      *     specified for this command invocation.
681      * @final
682      */
683     // FIXME: this isn't a count at all
684     COUNT_ALL: -2, // :%...
685
686     /** @property {Iterator(Command)} @private */
687     iterator: function iterator() iter.apply(null, this.hives.array)
688                               .sort(function (a, b) a.serialGroup - b.serialGroup || a.name > b.name)
689                               .iterValues(),
690
691     /** @property {string} The last executed Ex command line. */
692     repeat: null,
693
694     add: function add() {
695         let group = this.builtin;
696         if (!util.isDactyl(Components.stack.caller)) {
697             deprecated.warn(add, "commands.add", "group.commands.add");
698             group = this.user;
699         }
700
701         return group._add.apply(group, arguments);
702     },
703     addUserCommand: deprecated("group.commands.add", { get: function addUserCommand() this.user.closure._add }),
704     getUserCommands: deprecated("iter(group.commands)", function getUserCommands() iter(this.user).toArray()),
705     removeUserCommand: deprecated("group.commands.remove", { get: function removeUserCommand() this.user.closure.remove }),
706
707     /**
708      * Returns the specified command invocation object serialized to
709      * an executable Ex command string.
710      *
711      * @param {Object} args The command invocation object.
712      * @returns {string}
713      */
714     commandToString: function commandToString(args) {
715         let res = [args.command + (args.bang ? "!" : "")];
716
717         let defaults = {};
718         if (args.ignoreDefaults)
719             defaults = array(this.options).map(function (opt) [opt.names[0], opt.default])
720                                           .toObject();
721
722         for (let [opt, val] in Iterator(args.options || {})) {
723             if (val != null && defaults[opt] === val)
724                 continue;
725             let chr = /^-.$/.test(opt) ? " " : "=";
726             if (isArray(val))
727                 opt += chr + Option.stringify.stringlist(val);
728             else if (val != null)
729                 opt += chr + Commands.quote(val);
730             res.push(opt);
731         }
732         for (let [, arg] in Iterator(args.arguments || []))
733             res.push(Commands.quote(arg));
734
735         let str = args.literalArg;
736         if (str)
737             res.push(!/\n/.test(str) ? str :
738                      this.hereDoc && false ? "<<EOF\n" + String.replace(str, /\n$/, "") + "\nEOF"
739                                            : String.replace(str, /\n/g, "\n" + res[0].replace(/./g, " ").replace(/.$/, "\\")));
740         return res.join(" ");
741     },
742
743     /**
744      * Returns the command with matching *name*.
745      *
746      * @param {string} name The name of the command to return. This can be
747      *     any of the command's names.
748      * @returns {Command}
749      */
750     get: function get(name, full) iter(this.hives).map(function ([i, hive]) hive.get(name, full))
751                                                   .nth(util.identity, 0),
752
753     /**
754      * Returns true if a command invocation contains a URL referring to the
755      * domain *host*.
756      *
757      * @param {string} command
758      * @param {string} host
759      * @returns {boolean}
760      */
761     hasDomain: function hasDomain(command, host) {
762         try {
763             for (let [cmd, args] in this.subCommands(command))
764                 if (Array.concat(cmd.domains(args)).some(function (domain) util.isSubdomain(domain, host)))
765                     return true;
766         }
767         catch (e) {
768             util.reportError(e);
769         }
770         return false;
771     },
772
773     /**
774      * Returns true if a command invocation contains private data which should
775      * be cleared when purging private data.
776      *
777      * @param {string} command
778      * @returns {boolean}
779      */
780     hasPrivateData: function hasPrivateData(command) {
781         for (let [cmd, args] in this.subCommands(command))
782             if (cmd.privateData)
783                 return !callable(cmd.privateData) || cmd.privateData(args);
784         return false;
785     },
786
787     // TODO: should it handle comments?
788     //     : it might be nice to be able to specify that certain quoting
789     //     should be disabled E.g. backslash without having to resort to
790     //     using literal etc.
791     //     : error messages should be configurable or else we can ditch
792     //     Vim compatibility but it actually gives useful messages
793     //     sometimes rather than just "Invalid arg"
794     //     : I'm not sure documenting the returned object here, and
795     //     elsewhere, as type Args rather than simply Object makes sense,
796     //     especially since it is further augmented for use in
797     //     Command#action etc.
798     /**
799      * Parses *str* for options and plain arguments.
800      *
801      * The returned *Args* object is an augmented array of arguments.
802      * Any key/value pairs of *extra* will be available and the
803      * following additional properties:
804      *     -opt       - the value of the option -opt if specified
805      *     string     - the original argument string *str*
806      *     literalArg - any trailing literal argument
807      *
808      * Quoting rules:
809      *     '-quoted strings   - only ' and \ itself are escaped
810      *     "-quoted strings   - also ", \n and \t are translated
811      *     non-quoted strings - everything is taken literally apart from "\
812      *                          " and "\\"
813      *
814      * @param {string} str The Ex command-line string to parse. E.g.
815      *     "-x=foo -opt=bar arg1 arg2"
816      * @param {[CommandOption]} options The options accepted. These are specified
817      *      as an array of {@link CommandOption} structures.
818      * @param {string} argCount The number of arguments accepted.
819      *            "0": no arguments
820      *            "1": exactly one argument
821      *            "+": one or more arguments
822      *            "*": zero or more arguments (default if unspecified)
823      *            "?": zero or one arguments
824      * @param {boolean} allowUnknownOptions Whether unspecified options
825      *     should cause an error.
826      * @param {number} literal The index at which any literal arg begins.
827      *     See {@link Command#literal}.
828      * @param {CompletionContext} complete The relevant completion context
829      *     when the args are being parsed for completion.
830      * @param {Object} extra Extra keys to be spliced into the returned
831      *     Args object.
832      * @returns {Args}
833      */
834     parseArgs: function parseArgs(str, params) {
835         const self = this;
836
837         function getNextArg(str, _keepQuotes) {
838             if (arguments.length < 2)
839                 _keepQuotes = keepQuotes;
840
841             if (str.substr(0, 2) === "<<" && hereDoc) {
842                 let arg = /^<<(\S*)/.exec(str)[1];
843                 let count = arg.length + 2;
844                 if (complete)
845                     return [count, "", ""];
846                 return [count, self.readHeredoc(arg), ""];
847             }
848
849             let [count, arg, quote] = Commands.parseArg(str, null, _keepQuotes);
850             if (quote == "\\" && !complete)
851                 return [, , , "Trailing \\"];
852             if (quote && !complete)
853                 return [, , , "E114: Missing quote: " + quote];
854             return [count, arg, quote];
855         }
856
857         try {
858
859             var { allowUnknownOptions, argCount, complete, extra, hereDoc, literal, options, keepQuotes } = params || {};
860
861             if (!options)
862                 options = [];
863
864             if (!argCount)
865                 argCount = "*";
866
867             var args = params.newArgs ? params.newArgs() : [];
868             args.string = str; // for access to the unparsed string
869
870             // FIXME!
871             for (let [k, v] in Iterator(extra || []))
872                 args[k] = v;
873
874             // FIXME: best way to specify these requirements?
875             var onlyArgumentsRemaining = allowUnknownOptions || options.length == 0; // after a -- has been found
876             var arg = null;
877             var i = 0;
878             var completeOpts;
879
880             // XXX
881             let matchOpts = function matchOpts(arg) {
882                 // Push possible option matches into completions
883                 if (complete && !onlyArgumentsRemaining)
884                     completeOpts = options.filter(function (opt) opt.multiple || !set.has(args, opt.names[0]));
885             };
886             let resetCompletions = function resetCompletions() {
887                 completeOpts = null;
888                 args.completeArg = null;
889                 args.completeOpt = null;
890                 args.completeFilter = null;
891                 args.completeStart = i;
892                 args.quote = Commands.complQuote[""];
893             };
894             if (complete) {
895                 resetCompletions();
896                 matchOpts("");
897                 args.completeArg = 0;
898             }
899
900             let fail = function fail(error) {
901                 if (complete)
902                     complete.message = error;
903                 else
904                     util.assert(false, error);
905             };
906
907             outer:
908             while (i < str.length || complete) {
909                 var argStart = i;
910                 let re = /\s*/gy;
911                 re.lastIndex = i;
912                 i += re.exec(str)[0].length;
913
914                 if (str[i] == "|") {
915                     args.string = str.slice(0, i);
916                     args.trailing = str.slice(i + 1);
917                     break;
918                 }
919                 if (i == str.length && !complete)
920                     break;
921
922                 if (complete)
923                     resetCompletions();
924
925                 var sub = str.substr(i);
926                 if ((!onlyArgumentsRemaining) && /^--(\s|$)/.test(sub)) {
927                     onlyArgumentsRemaining = true;
928                     i += 2;
929                     continue;
930                 }
931
932                 var optname = "";
933                 if (!onlyArgumentsRemaining) {
934                     for (let [, opt] in Iterator(options)) {
935                         for (let [, optname] in Iterator(opt.names)) {
936                             if (sub.indexOf(optname) == 0) {
937                                 let count = 0;
938                                 let invalid = false;
939                                 let arg, quote, quoted;
940
941                                 let sep = sub[optname.length];
942                                 let argString = sub.substr(optname.length + 1);
943                                 if (sep == "=" || /\s/.test(sep) && opt.type != CommandOption.NOARG) {
944                                     [count, quoted, quote, error] = getNextArg(argString, true);
945                                     arg = Option.dequote(quoted);
946                                     util.assert(!error, error);
947
948                                     // if we add the argument to an option after a space, it MUST not be empty
949                                     if (sep != "=" && !quote && arg.length == 0)
950                                         arg = null;
951
952                                     count++; // to compensate the "=" character
953                                 }
954                                 else if (!/\s/.test(sep) && sep != undefined) // this isn't really an option as it has trailing characters, parse it as an argument
955                                     invalid = true;
956
957                                 let context = null;
958                                 if (!complete && quote)
959                                     fail(_("command.invalidOptArg", optname, argString));
960
961                                 if (!invalid) {
962                                     if (complete && !/[\s=]/.test(sep))
963                                         matchOpts(sub);
964
965                                     if (complete && count > 0) {
966                                         args.completeStart += optname.length + 1;
967                                         args.completeOpt = opt;
968                                         args.completeFilter = arg;
969                                         args.quote = Commands.complQuote[quote] || Commands.complQuote[""];
970                                     }
971                                     if (!complete || arg != null) {
972                                         if (opt.type) {
973                                             let orig = arg;
974                                             arg = opt.type.parse(arg, quoted);
975
976                                             if (complete && isArray(arg)) {
977                                                 args.completeFilter = arg[arg.length - 1] || "";
978                                                 args.completeStart += orig.length - args.completeFilter.length;
979                                             }
980
981                                             if (arg == null || (typeof arg == "number" && isNaN(arg))) {
982                                                 if (!complete || orig != "" || args.completeStart != str.length)
983                                                     fail(_("command.invalidOptTypeArg", opt.type.description, optname, argString));
984                                                 if (complete)
985                                                     complete.highlight(args.completeStart, count - 1, "SPELLCHECK");
986                                             }
987                                         }
988
989                                         // we have a validator function
990                                         if (typeof opt.validator == "function") {
991                                             if (opt.validator(arg, quoted) == false && (arg || !complete)) {
992                                                 fail(_("command.invalidOptArg", optname, argString));
993                                                 if (complete) // Always true.
994                                                     complete.highlight(args.completeStart, count - 1, "SPELLCHECK");
995                                             }
996                                         }
997                                     }
998
999                                     if (arg != null || opt.type == CommandOption.NOARG) {
1000                                         // option allowed multiple times
1001                                         if (opt.multiple)
1002                                             args[opt.names[0]] = (args[opt.names[0]] || []).concat(arg);
1003                                         else
1004                                             Class.replaceProperty(args, opt.names[0], opt.type == CommandOption.NOARG || arg);
1005
1006                                         args.explicitOpts[opt.names[0]] = args[opt.names[0]];
1007                                     }
1008
1009                                     i += optname.length + count;
1010                                     if (i == str.length)
1011                                         break outer;
1012                                     continue outer;
1013                                 }
1014                                 // if it is invalid, just fall through and try the next argument
1015                             }
1016                         }
1017                     }
1018                 }
1019
1020                 matchOpts(sub);
1021
1022                 if (complete)
1023                     if (argCount == "0" || args.length > 0 && (/[1?]/.test(argCount)))
1024                         complete.highlight(i, sub.length, "SPELLCHECK");
1025
1026                 if (args.length === literal) {
1027                     if (complete)
1028                         args.completeArg = args.length;
1029
1030                     let re = /(?:\s*(?=\n)|\s*)([^]*)/gy;
1031                     re.lastIndex = argStart || 0;
1032                     sub = re.exec(str)[1];
1033
1034                     // Hack.
1035                     if (sub.substr(0, 2) === "<<" && hereDoc)
1036                         let ([count, arg] = getNextArg(sub)) {
1037                             sub = arg + sub.substr(count);
1038                         }
1039
1040                     args.push(sub);
1041                     args.quote = null;
1042                     break;
1043                 }
1044
1045                 // if not an option, treat this token as an argument
1046                 let [count, arg, quote, error] = getNextArg(sub);
1047                 util.assert(!error, error);
1048
1049                 if (complete) {
1050                     args.quote = Commands.complQuote[quote] || Commands.complQuote[""];
1051                     args.completeFilter = arg || "";
1052                 }
1053                 else if (count == -1)
1054                     fail(_("command.parsing", arg));
1055                 else if (!onlyArgumentsRemaining && sub[0] === "-")
1056                     fail(_("command.invalidOpt", arg));
1057
1058                 if (arg != null)
1059                     args.push(arg);
1060                 if (complete)
1061                     args.completeArg = args.length - 1;
1062
1063                 i += count;
1064                 if (count <= 0 || i == str.length)
1065                     break;
1066             }
1067
1068             if (complete && args.trailing == null) {
1069                 if (args.completeOpt) {
1070                     let opt = args.completeOpt;
1071                     let context = complete.fork(opt.names[0], args.completeStart);
1072                     let arg = args.explicitOpts[opt.names[0]];
1073                     context.filter = args.completeFilter;
1074
1075                     if (isArray(arg))
1076                         context.filters.push(function (item) arg.indexOf(item.text) === -1);
1077
1078                     if (typeof opt.completer == "function")
1079                         var compl = opt.completer(context, args);
1080                     else
1081                         compl = opt.completer || [];
1082
1083                     context.title = [opt.names[0]];
1084                     context.quote = args.quote;
1085                     if (compl)
1086                         context.completions = compl;
1087                 }
1088                 complete.advance(args.completeStart);
1089                 complete.keys = {
1090                     text: "names",
1091                     description: function (opt) messages.get(["command", params.name, "options", opt.names[0], "description"].join("."), opt.description)
1092                 };
1093                 complete.title = ["Options"];
1094                 if (completeOpts)
1095                     complete.completions = completeOpts;
1096             }
1097
1098             if (args.verify)
1099                 args.verify();
1100
1101             return args;
1102         }
1103         catch (e if complete && e instanceof FailedAssertion) {
1104             complete.message = e;
1105             return args;
1106         }
1107     },
1108
1109     nameRegexp: util.regexp(<![CDATA[
1110             [^
1111                 0-9
1112                 <forbid>
1113             ]
1114             [^ <forbid> ]*
1115         ]]>, "gx", {
1116         forbid: util.regexp(String.replace(<![CDATA[
1117             U0000-U002c // U002d -
1118             U002e-U002f
1119             U003a-U0040 // U0041-U005a a-z
1120             U005b-U0060 // U0061-U007a A-Z
1121             U007b-U00bf
1122             U02b0-U02ff // Spacing Modifier Letters
1123             U0300-U036f // Combining Diacritical Marks
1124             U1dc0-U1dff // Combining Diacritical Marks Supplement
1125             U2000-U206f // General Punctuation
1126             U20a0-U20cf // Currency Symbols
1127             U20d0-U20ff // Combining Diacritical Marks for Symbols
1128             U2400-U243f // Control Pictures
1129             U2440-U245f // Optical Character Recognition
1130             U2500-U257f // Box Drawing
1131             U2580-U259f // Block Elements
1132             U2700-U27bf // Dingbats
1133             Ufe20-Ufe2f // Combining Half Marks
1134             Ufe30-Ufe4f // CJK Compatibility Forms
1135             Ufe50-Ufe6f // Small Form Variants
1136             Ufe70-Ufeff // Arabic Presentation Forms-B
1137             Uff00-Uffef // Halfwidth and Fullwidth Forms
1138             Ufff0-Uffff // Specials
1139         ]]>, /U/g, "\\u"), "x")
1140     }),
1141
1142     validName: Class.memoize(function validName() util.regexp("^" + this.nameRegexp.source + "$")),
1143
1144     commandRegexp: Class.memoize(function commandRegexp() util.regexp(<![CDATA[
1145             ^
1146             (?P<spec>
1147                 (?P<prespace> [:\s]*)
1148                 (?P<count>    (?:\d+ | %)? )
1149                 (?P<fullCmd>
1150                     (?: (?P<group>   <name>) : )?
1151                     (?P<cmd>      (?:<name> | !)? ))
1152                 (?P<bang>     !?)
1153                 (?P<space>    \s*)
1154             )
1155             (?P<args>
1156                 (?:. | \n)*?
1157             )?
1158             $
1159         ]]>, "x", {
1160             name: this.nameRegexp
1161         })),
1162
1163     /**
1164      * Parses a complete Ex command.
1165      *
1166      * The parsed string is returned as an Array like
1167      * [count, command, bang, args]:
1168      *     count   - any count specified
1169      *     command - the Ex command name
1170      *     bang    - whether the special "bang" version was called
1171      *     args    - the commands full argument string
1172      * E.g. ":2foo! bar" -> [2, "foo", true, "bar"]
1173      *
1174      * @param {string} str The Ex command line string.
1175      * @returns {Array}
1176      */
1177     // FIXME: why does this return an Array rather than Object?
1178     parseCommand: function parseCommand(str) {
1179         // remove comments
1180         str.replace(/\s*".*$/, "");
1181
1182         let matches = this.commandRegexp.exec(str);
1183         if (!matches)
1184             return [];
1185
1186         let { spec, count, group, cmd, bang, space, args } = matches;
1187         if (!cmd && bang)
1188             [cmd, bang] = [bang, cmd];
1189
1190         if (!cmd || args && args[0] != "|" && !(space || cmd == "!"))
1191             return [];
1192
1193         // parse count
1194         if (count)
1195             count = count == "%" ? this.COUNT_ALL : parseInt(count, 10);
1196         else
1197             count = this.COUNT_NONE;
1198
1199         return [count, cmd, !!bang, args || "", spec.length, group];
1200     },
1201
1202     parseCommands: function parseCommands(str, complete) {
1203         const { contexts } = this.modules;
1204         do {
1205             let [count, cmd, bang, args, len, group] = commands.parseCommand(str);
1206             if (!group)
1207                 var command = this.get(cmd || "");
1208             else if (group = contexts.getGroup(group, "commands"))
1209                 command = group.get(cmd || "");
1210
1211             if (command == null) {
1212                 yield [null, { commandString: str }];
1213                 return;
1214             }
1215
1216             if (complete) {
1217                 complete.fork(command.name);
1218                 var context = complete.fork("args", len);
1219             }
1220
1221             if (!complete || /(\w|^)[!\s]/.test(str))
1222                 args = command.parseArgs(args, context, { count: count, bang: bang });
1223             else
1224                 args = this.parseArgs(args, { extra: { count: count, bang: bang } });
1225             args.context = this.context;
1226             args.commandName = cmd;
1227             args.commandString = str.substr(0, len) + args.string;
1228             str = args.trailing;
1229             yield [command, args];
1230             if (args.break)
1231                 break;
1232         }
1233         while (str);
1234     },
1235
1236     subCommands: function subCommands(command) {
1237         let commands = [command];
1238         while (command = commands.shift())
1239             try {
1240                 for (let [command, args] in this.parseCommands(command)) {
1241                     if (command) {
1242                         yield [command, args];
1243                         if (command.subCommand && args[command.subCommand])
1244                             commands.push(args[command.subCommand]);
1245                     }
1246                 }
1247             }
1248             catch (e) {}
1249     },
1250
1251     /** @property */
1252     get complQuote() Commands.complQuote,
1253
1254     /** @property */
1255     get quoteArg() Commands.quoteArg // XXX: better somewhere else?
1256
1257 }, {
1258     // returns [count, parsed_argument]
1259     parseArg: function parseArg(str, sep, keepQuotes) {
1260         let arg = "";
1261         let quote = null;
1262         let len = str.length;
1263
1264         function fixEscapes(str) str.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4}|(.))/g, function (m, n1) n1 || m);
1265
1266         // Fix me.
1267         if (isString(sep))
1268             sep = RegExp(sep);
1269         sep = sep != null ? sep : /\s/;
1270         let re1 = RegExp("^" + (sep.source === "" ? "(?!)" : sep.source));
1271         let re2 = RegExp(/^()((?:[^\\S"']|\\.)+)((?:\\$)?)/.source.replace("S", sep.source));
1272
1273         while (str.length && !re1.test(str)) {
1274             let res;
1275             if ((res = re2.exec(str)))
1276                 arg += keepQuotes ? res[0] : res[2].replace(/\\(.)/g, "$1");
1277             else if ((res = /^(")((?:[^\\"]|\\.)*)("?)/.exec(str)))
1278                 arg += keepQuotes ? res[0] : JSON.parse(fixEscapes(res[0]) + (res[3] ? "" : '"'));
1279             else if ((res = /^(')((?:[^']|'')*)('?)/.exec(str)))
1280                 arg += keepQuotes ? res[0] : res[2].replace("''", "'", "g");
1281             else
1282                 break;
1283
1284             if (!res[3])
1285                 quote = res[1];
1286             if (!res[1])
1287                 quote = res[3];
1288             str = str.substr(res[0].length);
1289         }
1290
1291         return [len - str.length, arg, quote];
1292     },
1293
1294     quote: function quote(str) Commands.quoteArg[
1295         /[\b\f\n\r\t]/.test(str)   ? '"' :
1296         /[\s"'\\]|^$|^-/.test(str) ? "'"
1297                                    : ""](str)
1298 }, {
1299     completion: function initCompletion(dactyl, modules, window) {
1300         const { completion, contexts } = modules;
1301
1302         completion.command = function command(context, group) {
1303             context.title = ["Command"];
1304             context.keys = { text: "longNames", description: "description" };
1305             if (group)
1306                 context.generate = function () group._list;
1307             else
1308                 context.generate = function () modules.commands.hives.map(function (h) h._list).flatten();
1309         };
1310
1311         // provides completions for ex commands, including their arguments
1312         completion.ex = function ex(context) {
1313             const { commands } = modules;
1314
1315             // if there is no space between the command name and the cursor
1316             // then get completions of the command name
1317             for (var [command, args] in commands.parseCommands(context.filter, context))
1318                 if (args.trailing)
1319                     context.advance(args.commandString.length + 1);
1320             if (!args)
1321                 args = { commandString: context.filter };
1322
1323             let match = commands.commandRegexp.exec(args.commandString);
1324             if (!match)
1325                 return;
1326
1327             if (match.group)
1328                 context.advance(match.group.length + 1);
1329
1330             context.advance(match.prespace.length + match.count.length);
1331             if (!(match.bang || match.space)) {
1332                 context.fork("", 0, this, "command", match.group && contexts.getGroup(match.group, "commands"));
1333                 return;
1334             }
1335
1336             // dynamically get completions as specified with the command's completer function
1337             context.highlight();
1338             if (!command) {
1339                 context.message = "No such command: " + match.cmd;
1340                 context.highlight(0, match.cmd.length, "SPELLCHECK");
1341                 return;
1342             }
1343
1344             let cmdContext = context.fork(command.name, match.fullCmd.length + match.bang.length + match.space.length);
1345             try {
1346                 if (!cmdContext.waitingForTab) {
1347                     if (!args.completeOpt && command.completer && args.completeStart != null) {
1348                         cmdContext.advance(args.completeStart);
1349                         cmdContext.quote = args.quote;
1350                         cmdContext.filter = args.completeFilter;
1351                         command.completer.call(command, cmdContext, args);
1352                     }
1353                 }
1354             }
1355             catch (e) {
1356                 util.reportError(e);
1357             }
1358         };
1359
1360         completion.userCommand = function userCommand(context, group) {
1361             context.title = ["User Command", "Definition"];
1362             context.keys = { text: "name", description: "replacementText" };
1363             context.completions = group || modules.commands.user;
1364         };
1365     },
1366
1367     commands: function initCommands(dactyl, modules, window) {
1368         const { commands, contexts } = modules;
1369
1370         // TODO: Vim allows commands to be defined without {rep} if there are {attr}s
1371         // specified - useful?
1372         commands.add(["com[mand]"],
1373             "List or define commands",
1374             function (args) {
1375                 let cmd = args[0];
1376
1377                 util.assert(!cmd || cmd.split(",").every(commands.validName.closure.test),
1378                             _("command.invalidName", cmd));
1379
1380                 if (!args.literalArg)
1381                     commands.list();
1382                 else {
1383                     util.assert(args["-group"].modifiable,
1384                                 _("group.cantChangeBuiltin", _("command.commands")));
1385
1386                     let completer  = args["-complete"];
1387                     let completerFunc = null; // default to no completion for user commands
1388
1389                     if (completer) {
1390                         if (/^custom,/.test(completer)) {
1391                             completer = completer.substr(7);
1392
1393                             let context = update({}, contexts.context || {});
1394                             completerFunc = function (context) {
1395                                 try {
1396                                     var result = contextswithSavedValues(["context"], function () {
1397                                         contexts.context = context;
1398                                         return dactyl.userEval(completer);
1399                                     });
1400                                 }
1401                                 catch (e) {
1402                                     dactyl.echo(":" + this.name + " ...");
1403                                     dactyl.echoerr(_("command.unknownCompleter", completer));
1404                                     dactyl.log(e);
1405                                     return undefined;
1406                                 }
1407                                 if (callable(result))
1408                                     return result.apply(this, Array.slice(arguments));
1409                                 else
1410                                     return context.completions = result;
1411                             };
1412                         }
1413                         else
1414                             completerFunc = function (context) modules.completion.closure[config.completers[completer]](context);
1415                     }
1416
1417                     let added = args["-group"].add(cmd.split(","),
1418                                     args["-description"],
1419                                     contexts.bindMacro(args, "-ex",
1420                                         function makeParams(args, modifiers) ({
1421                                             args: {
1422                                                 __proto__: args,
1423                                                 toString: function () this.string,
1424                                             },
1425                                             bang:  this.bang && args.bang ? "!" : "",
1426                                             count: this.count && args.count
1427                                         })),
1428                                     {
1429                                         argCount: args["-nargs"],
1430                                         bang: args["-bang"],
1431                                         count: args["-count"],
1432                                         completer: completerFunc,
1433                                         literal: args["-literal"],
1434                                         persist: !args["-nopersist"],
1435                                         replacementText: args.literalArg,
1436                                         context: contexts.context && update({}, contexts.context)
1437                                     }, args.bang);
1438
1439                     if (!added)
1440                         dactyl.echoerr(_("command.exists"));
1441                 }
1442             }, {
1443                 bang: true,
1444                 completer: function (context, args) {
1445                     const { completion } = modules;
1446                     if (args.completeArg == 0)
1447                         completion.userCommand(context, args["-group"]);
1448                     else
1449                         args["-javascript"] ? completion.javascript(context) : completion.ex(context);
1450                 },
1451                 hereDoc: true,
1452                 options: [
1453                     { names: ["-bang", "-b"],  description: "Command may be followed by a !" },
1454                     { names: ["-count", "-c"], description: "Command may be preceded by a count" },
1455                     {
1456                         // TODO: "E180: invalid complete value: " + arg
1457                         names: ["-complete", "-C"],
1458                         description: "The argument completion function",
1459                         completer: function (context) [[k, ""] for ([k, v] in Iterator(config.completers))],
1460                         type: CommandOption.STRING,
1461                         validator: function (arg) arg in config.completers || /^custom,/.test(arg),
1462                     },
1463                     {
1464                         names: ["-description", "-desc", "-d"],
1465                         description: "A user-visible description of the command",
1466                         default: "User-defined command",
1467                         type: CommandOption.STRING
1468                     },
1469                     contexts.GroupFlag("commands"),
1470                     {
1471                         names: ["-javascript", "-js", "-j"],
1472                         description: "Execute the definition as JavaScript rather than Ex commands"
1473                     },
1474                     {
1475                         names: ["-literal", "-l"],
1476                         description: "Process the nth ignoring any quoting or meta characters",
1477                         type: CommandOption.INT
1478                     },
1479                     {
1480                         names: ["-nargs", "-a"],
1481                         description: "The allowed number of arguments",
1482                         completer: [["0", "No arguments are allowed (default)"],
1483                                     ["1", "One argument is allowed"],
1484                                     ["*", "Zero or more arguments are allowed"],
1485                                     ["?", "Zero or one argument is allowed"],
1486                                     ["+", "One or more arguments are allowed"]],
1487                         default: "0",
1488                         type: CommandOption.STRING,
1489                         validator: function (arg) /^[01*?+]$/.test(arg)
1490                     },
1491                     {
1492                         names: ["-nopersist", "-n"],
1493                         description: "Do not save this command to an auto-generated RC file"
1494                     }
1495                 ],
1496                 literal: 1,
1497
1498                 serialize: function () array(commands.userHives)
1499                     .filter(function (h) h.persist)
1500                     .map(function (hive) [
1501                         {
1502                             command: this.name,
1503                             bang: true,
1504                             options: iter([v, typeof cmd[k] == "boolean" ? null : cmd[k]]
1505                                           // FIXME: this map is expressed multiple times
1506                                           for ([k, v] in Iterator({
1507                                               argCount: "-nargs",
1508                                               bang: "-bang",
1509                                               count: "-count",
1510                                               description: "-description"
1511                                           }))
1512                                           if (cmd[k])).toObject(),
1513                             arguments: [cmd.name],
1514                             literalArg: cmd.action,
1515                             ignoreDefaults: true
1516                         }
1517                         for (cmd in hive) if (cmd.persist)
1518                     ], this)
1519                     .flatten().array
1520             });
1521
1522         commands.add(["delc[ommand]"],
1523             "Delete the specified user-defined command",
1524             function (args) {
1525                 util.assert(args.bang ^ !!args[0], _("error.argumentOrBang"));
1526                 let name = args[0];
1527
1528                 if (args.bang)
1529                     args["-group"].clear();
1530                 else if (args["-group"].get(name))
1531                     args["-group"].remove(name);
1532                 else
1533                     dactyl.echoerr(_("command.noSuchUser", name));
1534             }, {
1535                 argCount: "?",
1536                 bang: true,
1537                 completer: function (context, args) modules.completion.userCommand(context, args["-group"]),
1538                 options: [contexts.GroupFlag("commands")]
1539             });
1540
1541         commands.add(["comp[letions]"],
1542             "List the completion results for a given command substring",
1543             function (args) { modules.completion.listCompleter("ex", args[0]); },
1544             {
1545                 argCount: "1",
1546                 completer: function (context, args) modules.completion.ex(context),
1547                 literal: 0
1548             });
1549
1550         dactyl.addUsageCommand({
1551             name: ["listc[ommands]", "lc"],
1552             description: "List all Ex commands along with their short descriptions",
1553             index: "ex-cmd",
1554             iterate: function (args) commands.iterator().map(function (cmd) ({
1555                 __proto__: cmd,
1556                 columns: [
1557                     cmd.hive == commands.builtin ? "" : <span highlight="Object" style="padding-right: 1em;">{cmd.hive.name}</span>
1558                 ]
1559             })),
1560             iterateIndex: function (args) let (tags = services["dactyl:"].HELP_TAGS)
1561                 this.iterate(args).filter(function (cmd) cmd.hive === commands.builtin || set.has(cmd.helpTag)),
1562             format: {
1563                 headings: ["Command", "Group", "Description"],
1564                 description: function (cmd) template.linkifyHelp(cmd.description + (cmd.replacementText ? ": " + cmd.action : "")),
1565                 help: function (cmd) ":" + cmd.name
1566             }
1567         });
1568
1569         commands.add(["y[ank]"],
1570             "Yank the output of the given command to the clipboard",
1571             function (args) {
1572                 let cmd = /^:/.test(args[0]) ? args[0] : ":echo " + args[0];
1573
1574                 let res = modules.commandline.withOutputToString(commands.execute, commands, cmd);
1575
1576                 dactyl.clipboardWrite(res);
1577
1578                 let lines = res.split("\n").length;
1579                 dactyl.echomsg("Yanked " + lines + " line" + (lines == 1 ? "" : "s"));
1580             },
1581             {
1582                 completer: function (context) modules.completion[/^:/.test(context.filter) ? "ex" : "javascript"](context),
1583                 literal: 0
1584             });
1585     },
1586     javascript: function initJavascript(dactyl, modules, window) {
1587         const { JavaScript, commands } = modules;
1588
1589         JavaScript.setCompleter([commands.user.get, commands.user.remove],
1590                                 [function () [[c.names, c.description] for (c in this)]]);
1591         JavaScript.setCompleter([commands.get],
1592                                 [function () [[c.names, c.description] for (c in this.iterator())]]);
1593     },
1594     mappings: function initMappings(dactyl, modules, window) {
1595         const { commands, mappings, modes } = modules;
1596
1597         mappings.add([modes.COMMAND],
1598             ["@:"], "Repeat the last Ex command",
1599             function (args) {
1600                 if (commands.repeat) {
1601                     for (let i in util.interruptibleRange(0, Math.max(args.count, 1), 100))
1602                         dactyl.execute(commands.repeat);
1603                 }
1604                 else
1605                     dactyl.echoerr(_("command.noPrevious"));
1606             },
1607             { count: true });
1608     }
1609 });
1610
1611 (function () {
1612
1613     Commands.quoteMap = {
1614         "\n": "\\n",
1615         "\t": "\\t",
1616     };
1617     function quote(q, list, map) {
1618         map = map || Commands.quoteMap;
1619         let re = RegExp("[" + list + "]", "g");
1620         function quote(str) q + String.replace(str, re, function ($0) $0 in map ? map[$0] : ("\\" + $0)) + q;
1621         quote.list = list;
1622         return quote;
1623     };
1624
1625     Commands.quoteArg = {
1626         '"': quote('"', '\n\t"\\\\'),
1627         "'": quote("'", "'", { "'": "''" }),
1628         "":  quote("",  "|\\\\\\s'\"")
1629     };
1630     Commands.complQuote = {
1631         '"': ['"', quote("", Commands.quoteArg['"'].list), '"'],
1632         "'": ["'", quote("", Commands.quoteArg["'"].list), "'"],
1633         "":  ["", Commands.quoteArg[""], ""]
1634     };
1635
1636     Commands.parseBool = function (arg) {
1637         if (/^(true|1|on)$/i.test(arg))
1638             return true;
1639         if (/^(false|0|off)$/i.test(arg))
1640             return false;
1641         return NaN;
1642     };
1643 })();
1644
1645 endModule();
1646
1647 } catch(e){ if (!e.stack) e = Error(e); dump(e.fileName+":"+e.lineNumber+": "+e+"\n" + e.stack); }
1648
1649 // vim: set fdm=marker sw=4 ts=4 et ft=javascript: