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