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>
5 // This work is licensed for reuse under an MIT license. Details are
6 // given in the LICENSE.txt file included with this file.
12 * A class representing key mappings. Instances are created by the
13 * {@link Mappings} class.
15 * @param {[Modes.Mode]} modes The modes in which this mapping is active.
16 * @param {[string]} keys The key sequences which are bound to
18 * @param {string} description A short one line description of the key mapping.
19 * @param {function} action The action invoked by each key sequence.
20 * @param {Object} extraInfo An optional extra configuration hash. The
21 * following properties are supported.
22 * arg - see {@link Map#arg}
23 * count - see {@link Map#count}
24 * motion - see {@link Map#motion}
25 * noremap - see {@link Map#noremap}
26 * rhs - see {@link Map#rhs}
27 * silent - see {@link Map#silent}
31 var Map = Class("Map", {
32 init: function (modes, keys, description, action, extraInfo) {
37 this.description = description;
39 Object.freeze(this.modes);
42 this.update(extraInfo);
45 name: Class.memoize(function () this.names[0]),
47 /** @property {[string]} All of this mapping's names (key sequences). */
48 names: Class.memoize(function () this._keys.map(function (k) events.canonicalKeys(k))),
50 get toStringParams() [this.modes.map(function (m) m.name), this.names.map(String.quote)],
52 get identifier() [this.modes[0].name, this.hive.prefix + this.names[0]].join("."),
54 /** @property {number} A unique ID for this mapping. */
56 /** @property {[Modes.Mode]} All of the modes for which this mapping applies. */
58 /** @property {function (number)} The function called to execute this mapping. */
60 /** @property {string} This mapping's description, as shown in :listkeys. */
61 description: Messages.Localized(""),
63 /** @property {boolean} Whether this mapping accepts an argument. */
65 /** @property {boolean} Whether this mapping accepts a count. */
68 * @property {boolean} Whether the mapping accepts a motion mapping
72 /** @property {boolean} Whether the RHS of the mapping should expand mappings recursively. */
74 /** @property {boolean} Whether any output from the mapping should be echoed on the command line. */
76 /** @property {string} The literal RHS expansion of this mapping. */
79 * @property {boolean} Specifies whether this is a user mapping. User
80 * mappings may be created by plugins, or directly by users. Users and
81 * plugin authors should create only user mappings.
86 * Returns whether this mapping can be invoked by a key sequence matching
89 * @param {string} name The name to query.
92 hasName: function (name) this.keys.indexOf(name) >= 0,
94 get keys() array.flatten(this.names.map(mappings.closure.expand)),
97 * Execute the action for this mapping.
99 * @param {object} args The arguments object for the given mapping.
101 execute: function (args) {
102 if (!isObject(args)) // Backwards compatibility :(
103 args = iter(["motion", "count", "arg", "command"])
104 .map(function ([i, prop]) [prop, this[i]], arguments)
107 args = update({ context: contexts.context },
108 this.hive.argsExtra(args),
112 function repeat() self.action(args)
113 if (this.names[0] != ".") // FIXME: Kludge.
114 mappings.repeat = repeat;
117 util.dumpStack(_("map.recursive", args.command));
118 dactyl.assert(!this.executing, _("map.recursive", args.command));
121 this.executing = true;
125 events.feedingKeys = false;
126 dactyl.reportError(e, true);
129 this.executing = false;
138 var MapHive = Class("MapHive", Contexts.Hive, {
139 init: function init(group) {
140 init.supercall(this, group);
145 * Iterates over all mappings present in all of the given *modes*.
147 * @param {[Modes.Mode]} modes The modes for which to return mappings.
149 iterate: function (modes) {
150 let stacks = Array.concat(modes).map(this.closure.getStack);
151 return values(stacks.shift().sort(function (m1, m2) String.localeCompare(m1.name, m2.name))
152 .filter(function (map) map.rhs &&
153 stacks.every(function (stack) stack.some(function (m) m.rhs && m.rhs === map.rhs && m.name === map.name))));
157 * Adds a new key mapping.
159 * @param {[Modes.Mode]} modes The modes that this mapping applies to.
160 * @param {[string]} keys The key sequences which are bound to *action*.
161 * @param {string} description A description of the key mapping.
162 * @param {function} action The action invoked by each key sequence.
163 * @param {Object} extra An optional extra configuration hash.
166 add: function (modes, keys, description, action, extra) {
169 modes = Array.concat(modes);
170 if (!modes.every(util.identity))
171 throw TypeError(/*L*/"Invalid modes: " + modes);
173 let map = Map(modes, keys, description, action, extra);
174 map.definedAt = contexts.getCaller(Components.stack.caller);
177 if (this.name !== "builtin")
178 for (let [, name] in Iterator(map.names))
179 for (let [, mode] in Iterator(map.modes))
180 this.remove(mode, name);
182 for (let mode in values(map.modes))
183 this.getStack(mode).add(map);
188 * Returns the mapping stack for the given mode.
190 * @param {Modes.Mode} mode The mode to search.
193 getStack: function getStack(mode) {
194 if (!(mode in this.stacks))
195 return this.stacks[mode] = MapHive.Stack();
196 return this.stacks[mode];
200 * Returns the map from *mode* named *cmd*.
202 * @param {Modes.Mode} mode The mode to search.
203 * @param {string} cmd The map name to match.
204 * @returns {Map|null}
206 get: function (mode, cmd) this.getStack(mode).mappings[cmd],
209 * Returns a count of maps with names starting with but not equal to
212 * @param {Modes.Mode} mode The mode to search.
213 * @param {string} prefix The map prefix string to match.
216 getCandidates: function (mode, prefix) this.getStack(mode).candidates[prefix] || 0,
219 * Returns whether there is a user-defined mapping *cmd* for the specified
222 * @param {Modes.Mode} mode The mode to search.
223 * @param {string} cmd The candidate key mapping.
226 has: function (mode, cmd) this.getStack(mode).mappings[cmd] != null,
229 * Remove the mapping named *cmd* for *mode*.
231 * @param {Modes.Mode} mode The mode to search.
232 * @param {string} cmd The map name to match.
234 remove: function (mode, cmd) {
235 let stack = this.getStack(mode);
236 for (let [i, map] in array.iterItems(stack)) {
237 let j = map.names.indexOf(cmd);
240 map.names.splice(j, 1);
241 if (map.names.length == 0) // FIX ME.
242 for (let [mode, stack] in Iterator(this.stacks))
243 this.stacks[mode] = MapHive.Stack(stack.filter(function (m) m != map));
250 * Remove all user-defined mappings for *mode*.
252 * @param {Modes.Mode} mode The mode to remove all mappings from.
254 clear: function (mode) {
255 this.stacks[mode] = MapHive.Stack([]);
258 Stack: Class("Stack", Array, {
259 init: function (ary) {
260 let self = ary || [];
261 self.__proto__ = this.__proto__;
265 __iterator__: function () array.iterValues(this),
267 get candidates() this.states.candidates,
268 get mappings() this.states.mappings,
270 add: function (map) {
275 states: Class.memoize(function () {
281 for (let map in this)
282 for (let name in values(map.keys)) {
283 states.mappings[name] = map;
285 for (let key in events.iterKeys(name)) {
288 states.candidates[state] = (states.candidates[state] || 0) + 1;
299 var Mappings = Module("mappings", {
303 repeat: Modes.boundProperty(),
305 get allHives() contexts.allGroups.mappings,
307 get userHives() this.allHives.filter(function (h) h !== this.builtin, this),
309 expandLeader: function expandLeader(keyString) keyString.replace(/<Leader>/i, function () options["mapleader"]),
311 prefixes: Class.memoize(function () {
312 let list = Array.map("CASM", function (s) s + "-");
314 return iter(util.range(0, 1 << list.length)).map(function (mask)
315 list.filter(function (p, i) mask & (1 << i)).join("")).toArray().concat("*-");
318 expand: function expand(keys) {
319 keys = keys.replace(/<leader>/i, options["mapleader"]);
320 if (!/<\*-/.test(keys))
323 return util.debrace(events.iterKeys(keys).map(function (key) {
324 if (/^<\*-/.test(key))
325 return ["<", this.prefixes, key.slice(3)];
327 }, this).flatten().array).map(function (k) events.canonicalKeys(k));
330 iterate: function (mode) {
332 for (let hive in this.hives.iterValues())
333 for (let map in array(hive.getStack(mode)).iterValues())
334 if (!Set.add(seen, map.name))
338 // NOTE: just normal mode for now
339 /** @property {Iterator(Map)} */
340 __iterator__: function () this.iterate(modes.NORMAL),
342 getDefault: deprecated("mappings.builtin.get", function getDefault(mode, cmd) this.builtin.get(mode, cmd)),
343 getUserIterator: deprecated("mappings.user.iterator", function getUserIterator(modes) this.user.iterator(modes)),
344 hasMap: deprecated("group.mappings.has", function hasMap(mode, cmd) this.user.has(mode, cmd)),
345 remove: deprecated("group.mappings.remove", function remove(mode, cmd) this.user.remove(mode, cmd)),
346 removeAll: deprecated("group.mappings.clear", function removeAll(mode) this.user.clear(mode)),
349 * Adds a new default key mapping.
351 * @param {[Modes.Mode]} modes The modes that this mapping applies to.
352 * @param {[string]} keys The key sequences which are bound to *action*.
353 * @param {string} description A description of the key mapping.
354 * @param {function} action The action invoked by each key sequence.
355 * @param {Object} extra An optional extra configuration hash.
358 add: function add() {
359 let group = this.builtin;
360 if (!util.isDactyl(Components.stack.caller)) {
361 deprecated.warn(add, "mappings.add", "group.mappings.add");
365 let map = group.add.apply(group, arguments);
366 map.definedAt = contexts.getCaller(Components.stack.caller);
371 * Adds a new user-defined key mapping.
373 * @param {[Modes.Mode]} modes The modes that this mapping applies to.
374 * @param {[string]} keys The key sequences which are bound to *action*.
375 * @param {string} description A description of the key mapping.
376 * @param {function} action The action invoked by each key sequence.
377 * @param {Object} extra An optional extra configuration hash (see
378 * {@link Map#extraInfo}).
381 addUserMap: deprecated("group.mappings.add", function addUserMap() {
382 let map = this.user.add.apply(this.user, arguments);
383 map.definedAt = contexts.getCaller(Components.stack.caller);
388 * Returns the map from *mode* named *cmd*.
390 * @param {Modes.Mode} mode The mode to search.
391 * @param {string} cmd The map name to match.
394 get: function get(mode, cmd) this.hives.map(function (h) h.get(mode, cmd)).compact()[0] || null,
397 * Returns an array of maps with names starting with but not equal to
400 * @param {Modes.Mode} mode The mode to search.
401 * @param {string} prefix The map prefix string to match.
404 getCandidates: function (mode, prefix)
405 this.hives.map(function (h) h.getCandidates(mode, prefix))
409 * Lists all user-defined mappings matching *filter* for the specified
410 * *modes* in the specified *hives*.
412 * @param {[Modes.Mode]} modes An array of modes to search.
413 * @param {string} filter The filter string to match. @optional
414 * @param {[MapHive]} hives The map hives to list. @optional
416 list: function (modes, filter, hives) {
417 let modeSign = modes.map(function (m) m.char || "").join("")
418 + modes.map(function (m) !m.char ? " " + m.name : "").join("");
419 modeSign = modeSign.replace(/^ /, "");
421 hives = (hives || mappings.userHives).map(function (h) [h, maps(h)])
422 .filter(function ([h, m]) m.length);
424 function maps(hive) {
425 let maps = iter.toArray(hive.iterate(modes));
427 maps = maps.filter(function (m) m.names[0] === filter);
432 <tr highlight="Title">
434 <td style="padding-right: 1em;">{_("title.Mode")}</td>
435 <td style="padding-right: 1em;">{_("title.Command")}</td>
436 <td style="padding-right: 1em;">{_("title.Action")}</td>
438 <col style="min-width: 6em; padding-right: 1em;"/>
440 template.map(hives, function ([hive, maps]) let (i = 0)
441 <tr style="height: .5ex;"/> +
442 template.map(maps, function (map)
443 template.map(map.names, function (name)
445 <td highlight="Title">{!i++ ? hive.name : ""}</td>
448 <td>{map.rhs || map.action.toSource()}</td>
450 <tr style="height: .5ex;"/>)
454 // TODO: Move this to an ItemList to show this automatically
455 if (list.*.length() === list.text().length() + 2)
456 dactyl.echomsg(_("map.none"));
458 commandline.commandOutput(list);
462 contexts: function initContexts(dactyl, modules, window) {
463 update(Mappings.prototype, {
464 hives: contexts.Hives("mappings", MapHive),
465 user: contexts.hives.mappings.user,
466 builtin: contexts.hives.mappings.builtin
469 commands: function initCommands(dactyl, modules, window) {
470 function addMapCommands(ch, mapmodes, modeDescription) {
471 function map(args, noremap) {
472 let mapmodes = array.uniq(args["-modes"].map(findMode));
473 let hives = args.explicitOpts["-group"] ? [args["-group"]] : null;
476 mappings.list(mapmodes, null, hives);
480 if (args[1] && !/^<nop>$/i.test(args[1])
481 && !args["-count"] && !args["-ex"] && !args["-javascript"]
482 && mapmodes.every(function (m) m.count))
483 args[1] = "<count>" + args[1];
485 let [lhs, rhs] = args;
487 args["-builtin"] = true;
489 if (!rhs) // list the mapping
490 mappings.list(mapmodes, mappings.expandLeader(lhs), hives);
492 util.assert(args["-group"].modifiable,
493 _("map.builtinImmutable"));
495 args["-group"].add(mapmodes, [lhs],
496 args["-description"],
497 contexts.bindMacro(args, "-keys", function (params) params),
500 count: args["-count"] || !(args["-ex"] || args["-javascript"]),
501 noremap: args["-builtin"],
502 persist: !args["-nopersist"],
503 get rhs() String(this.action),
504 silent: args["-silent"]
511 completer: function (context, args) {
512 let mapmodes = array.uniq(args["-modes"].map(findMode));
513 if (args.length == 1)
514 return completion.userMapping(context, mapmodes, args["-group"]);
515 if (args.length == 2) {
516 if (args["-javascript"])
517 return completion.javascript(context);
519 return completion.ex(context);
526 names: ["-arg", "-a"],
527 description: "Accept an argument after the requisite key press",
530 names: ["-builtin", "-b"],
531 description: "Execute this mapping as if there were no user-defined mappings"
534 names: ["-count", "-c"],
535 description: "Accept a count before the requisite key press"
538 names: ["-description", "-desc", "-d"],
539 description: "A description of this mapping",
540 default: /*L*/"User-defined mapping",
541 type: CommandOption.STRING
544 names: ["-ex", "-e"],
545 description: "Execute this mapping as an Ex command rather than keys"
547 contexts.GroupFlag("mappings"),
549 names: ["-javascript", "-js", "-j"],
550 description: "Execute this mapping as JavaScript rather than keys"
552 update({}, modeFlag, {
553 names: ["-modes", "-mode", "-m"],
554 type: CommandOption.LIST,
555 description: "Create this mapping in the given modes",
556 default: mapmodes || ["n", "v"]
559 names: ["-nopersist", "-n"],
560 description: "Do not save this mapping to an auto-generated RC file"
563 names: ["-silent", "-s", "<silent>", "<Silent>"],
564 description: "Do not echo any generated keys to the command line"
567 serialize: function () {
568 return this.name != "map" ? [] :
569 array(mappings.userHives)
570 .filter(function (h) h.persist)
571 .map(function (hive) [
575 hive.name !== "user" && ["-group", hive.name],
576 ["-modes", uniqueModes(map.modes)],
577 ["-description", map.description],
578 map.silent && ["-silent"]])
580 .filter(util.identity)
582 arguments: [map.names[0]],
586 for (map in userMappings(hive))
592 function userMappings(hive) {
594 for (let stack in values(hive.stacks))
595 for (let map in array.iterValues(stack))
596 if (!Set.add(seen, map.id))
600 modeDescription = modeDescription ? " in " + modeDescription + " mode" : "";
601 commands.add([ch ? ch + "m[ap]" : "map"],
602 "Map a key sequence" + modeDescription,
603 function (args) { map(args, false); },
606 commands.add([ch + "no[remap]"],
607 "Map a key sequence without remapping keys" + modeDescription,
608 function (args) { map(args, true); },
611 commands.add([ch + "unm[ap]"],
612 "Remove a mapping" + modeDescription,
614 util.assert(args["-group"].modifiable, _("map.builtinImmutable"));
616 util.assert(args.bang ^ !!args[0], _("error.argumentOrBang"));
618 let mapmodes = array.uniq(args["-modes"].map(findMode));
621 for (let mode in values(mapmodes))
623 args["-group"].clear(mode);
624 else if (args["-group"].has(mode, args[0])) {
625 args["-group"].remove(mode, args[0]);
629 if (!found && !args.bang)
630 dactyl.echoerr(_("map.noSuch", args[0]));
636 completer: opts.completer,
638 contexts.GroupFlag("mappings"),
639 update({}, modeFlag, {
640 names: ["-modes", "-mode", "-m"],
641 type: CommandOption.LIST,
642 description: "Remove mapping from the given modes",
643 default: mapmodes || ["n", "v"]
650 names: ["-mode", "-m"],
651 type: CommandOption.STRING,
652 validator: function (value) Array.concat(value).every(findMode),
653 completer: function () [[array.compact([mode.name.toLowerCase().replace(/_/g, "-"), mode.char]), mode.description]
654 for (mode in values(modes.all))
658 function findMode(name) {
660 for (let mode in values(modes.all))
661 if (name == mode || name == mode.char
662 || String.toLowerCase(name).replace(/-/g, "_") == mode.name.toLowerCase())
666 function uniqueModes(modes) {
667 let chars = [k for ([k, v] in Iterator(modules.modes.modeChars))
668 if (v.every(function (mode) modes.indexOf(mode) >= 0))];
669 return array.uniq(modes.filter(function (m) chars.indexOf(m.char) < 0)
670 .map(function (m) m.name.toLowerCase())
674 commands.add(["feedkeys", "fk"],
676 function (args) { events.feedkeys(args[0] || "", args.bang, false, findMode(args["-mode"])); },
682 update({}, modeFlag, {
683 description: "The mode in which to feed the keys"
688 addMapCommands("", [modes.NORMAL, modes.VISUAL], "");
690 for (let mode in modes.mainModes)
691 if (mode.char && !commands.get(mode.char + "map", true))
692 addMapCommands(mode.char,
693 [m.mask for (m in modes.mainModes) if (m.char == mode.char)],
697 getMode: function (args) findMode(args["-mode"]),
698 iterate: function (args, mainOnly) {
699 let modes = [this.getMode(args)];
701 modes = modes[0].allBases;
704 // Bloody hell. --Kris
705 for (let [i, mode] in Iterator(modes))
706 for (let hive in mappings.hives.iterValues())
707 for (let map in array.iterValues(hive.getStack(mode)))
708 for (let name in values(map.names))
709 if (!Set.add(seen, name)) {
713 i === 0 ? "" : <span highlight="Object" style="padding-right: 1em;">{mode.name}</span>,
714 hive == mappings.builtin ? "" : <span highlight="Object" style="padding-right: 1em;">{hive.name}</span>
721 description: function (map) (XML.ignoreWhitespace = false, XML.prettyPrinting = false, <>
722 {options.get("passkeys").has(map.name)
723 ? <span highlight="URLExtra">(passed by {template.helpLink("'passkeys'")})</span>
725 {template.linkifyHelp(map.description + (map.rhs ? ": " + map.rhs : ""))}
727 help: function (map) let (char = array.compact(map.modes.map(function (m) m.char))[0])
728 char === "n" ? map.name : char ? char + "_" + map.name : "",
729 headings: ["Command", "Mode", "Group", "Description"]
733 dactyl.addUsageCommand({
735 name: ["listk[eys]", "lk"],
736 description: "List all mappings along with their short descriptions",
738 update({}, modeFlag, {
740 description: "The mode for which to list mappings"
745 iter.forEach(modes.mainModes, function (mode) {
746 if (mode.char && !commands.get(mode.char + "listkeys", true))
747 dactyl.addUsageCommand({
749 name: [mode.char + "listk[eys]", mode.char + "lk"],
750 iterateIndex: function (args)
751 let (self = this, prefix = /^[bCmn]$/.test(mode.char) ? "" : mode.char + "_",
752 tags = services["dactyl:"].HELP_TAGS)
753 ({ helpTag: prefix + map.name, __proto__: map }
754 for (map in self.iterate(args, true))
755 if (map.hive === mappings.builtin || Set.has(tags, prefix + map.name))),
756 description: "List all " + mode.displayName + " mode mappings along with their short descriptions",
757 index: mode.char + "-map",
758 getMode: function (args) mode,
763 completion: function initCompletion(dactyl, modules, window) {
764 completion.userMapping = function userMapping(context, modes_, hive) {
765 hive = hive || mappings.user;
766 modes_ = modes_ || [modes.NORMAL];
767 context.keys = { text: function (m) m.names[0], description: function (m) m.description + ": " + m.action };
768 context.completions = hive.iterate(modes_);
771 javascript: function initJavascript(dactyl, modules, window) {
772 JavaScript.setCompleter([mappings.get, mappings.builtin.get],
775 function (context, obj, args) [[m.names, m.description] for (m in this.iterate(args[0]))]
778 options: function initOptions(dactyl, modules, window) {
779 options.add(["mapleader", "ml"],
780 "Define the replacement keys for the <Leader> pseudo-key",
782 setter: function (value) {
784 for (let hive in values(mappings.allHives))
785 for (let stack in values(hive.stacks))
793 // vim: set fdm=marker sw=4 ts=4 et: