1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
|
// Copyright (c) 2006-2008 by Martin Stubenschrott <stubenschrott@vimperator.org>
// Copyright (c) 2007-2011 by Doug Kearns <dougkearns@gmail.com>
// Copyright (c) 2008-2011 by Kris Maglione <maglione.k at Gmail>
//
// This work is licensed for reuse under an MIT license. Details are
// given in the LICENSE.txt file included with this file.
"use strict";
/** @scope modules */
/**
* A class representing key mappings. Instances are created by the
* {@link Mappings} class.
*
* @param {number[]} modes The modes in which this mapping is active.
* @param {string[]} keys The key sequences which are bound to
* *action*.
* @param {string} description A short one line description of the key mapping.
* @param {function} action The action invoked by each key sequence.
* @param {Object} extraInfo An optional extra configuration hash. The
* following properties are supported.
* arg - see {@link Map#arg}
* count - see {@link Map#count}
* motion - see {@link Map#motion}
* noremap - see {@link Map#noremap}
* rhs - see {@link Map#rhs}
* silent - see {@link Map#silent}
* @optional
* @private
*/
var Map = Class("Map", {
init: function (modes, keys, description, action, extraInfo) {
modes = Array.concat(modes).map(function (m) isObject(m) ? m.mask : m);
if (!modes.every(util.identity))
throw Error("Invalid modes");
this.id = ++Map.id;
this.modes = modes;
this._keys = keys;
this.action = action;
this.description = description;
if (Object.freeze)
Object.freeze(this.modes);
if (extraInfo)
update(this, extraInfo);
},
name: Class.memoize(function () this.names[0]),
/** @property {string[]} All of this mapping's names (key sequences). */
names: Class.memoize(function () this._keys.map(events.closure.canonicalKeys)),
get toStringParams() [this.modes.map(function (m) m.name), this.names.map(String.quote)],
/** @property {number} A unique ID for this mapping. */
id: null,
/** @property {number[]} All of the modes for which this mapping applies. */
modes: null,
/** @property {function (number)} The function called to execute this mapping. */
action: null,
/** @property {string} This mapping's description, as shown in :listkeys. */
description: "",
/** @property {boolean} Whether this mapping accepts an argument. */
arg: false,
/** @property {boolean} Whether this mapping accepts a count. */
count: false,
/**
* @property {boolean} Whether the mapping accepts a motion mapping
* as an argument.
*/
motion: false,
/** @property {boolean} Whether the RHS of the mapping should expand mappings recursively. */
noremap: false,
/** @property {boolean} Whether any output from the mapping should be echoed on the command line. */
silent: false,
/** @property {string} The literal RHS expansion of this mapping. */
rhs: null,
/**
* @property {boolean} Specifies whether this is a user mapping. User
* mappings may be created by plugins, or directly by users. Users and
* plugin authors should create only user mappings.
*/
user: false,
/**
* Returns whether this mapping can be invoked by a key sequence matching
* *name*.
*
* @param {string} name The name to query.
* @returns {boolean}
*/
hasName: function (name) this.keys.indexOf(name) >= 0,
get keys() this.names.map(mappings.expandLeader),
/**
* Execute the action for this mapping.
*
* @param {object} args The arguments object for the given mapping.
*/
execute: function (args) {
if (!isObject(args)) // Backwards compatibility :(
args = iter(["motion", "count", "arg", "command"])
.map(function ([i, prop]) [prop, this[i]], arguments)
.toObject();
let self = this;
function repeat() self.action(args)
if (this.names[0] != ".") // FIXME: Kludge.
mappings.repeat = repeat;
if (this.executing)
util.dumpStack("Attempt to execute mapping recursively: " + args.command);
dactyl.assert(!this.executing, "Attempt to execute mapping recursively: " + args.command);
try {
this.executing = true;
var res = repeat();
}
catch (e) {
events.feedingKeys = false;
dactyl.reportError(e, true);
}
finally {
this.executing = false;
}
return res;
}
}, {
id: 0
});
var MapHive = Class("MapHive", {
init: function init(name, description, filter) {
this.name = name;
this.stacks = {};
this.description = description;
this.filter = filter || function (uri) true;
},
get toStringParams() [this.name],
get builtin() mappings.builtinHives.indexOf(this) >= 0,
/**
* Iterates over all mappings present in all of the given *modes*.
*
* @param {[Modes.Mode]} modes The modes for which to return mappings.
*/
iterate: function (modes) {
let stacks = Array.concat(modes).map(this.closure.getStack);
return values(stacks.shift().sort(function (m1, m2) String.localeCompare(m1.name, m2.name))
.filter(function (map) map.rhs &&
stacks.every(function (stack) stack.some(function (m) m.rhs && m.rhs === map.rhs && m.name === map.name))));
},
/**
* Adds a new default key mapping.
*
* @param {number[]} modes The modes that this mapping applies to.
* @param {string[]} keys The key sequences which are bound to *action*.
* @param {string} description A description of the key mapping.
* @param {function} action The action invoked by each key sequence.
* @param {Object} extra An optional extra configuration hash.
* @optional
*/
add: function (modes, keys, description, action, extra) {
extra = extra || {};
let map = Map(modes, keys, description, action, extra);
map.definedAt = Commands.getCaller(Components.stack.caller);
map.hive = this;
if (this.name !== "builtin")
for (let [, name] in Iterator(map.names))
for (let [, mode] in Iterator(map.modes))
this.remove(mode, name);
for (let mode in values(map.modes))
this.getStack(mode).add(map);
return map;
},
/**
* Returns the mapping stack for the given mode.
*
* @param {Modes.Mode} mode The mode to search.
* @returns {[Map]}
*/
getStack: function getStack(mode) {
if (!(mode in this.stacks))
return this.stacks[mode] = MapHive.Stack();
return this.stacks[mode];
},
/**
* Returns the map from *mode* named *cmd*.
*
* @param {Modes.Mode} mode The mode to search.
* @param {string} cmd The map name to match.
* @returns {Map|null}
*/
get: function (mode, cmd) this.getStack(mode).mappings[cmd],
/**
* Returns a count of maps with names starting with but not equal to
* *prefix*.
*
* @param {Modes.Mode} mode The mode to search.
* @param {string} prefix The map prefix string to match.
* @returns {number)
*/
getCandidates: function (mode, prefix) this.getStack(mode).candidates[prefix] || 0,
/**
* Returns whether there is a user-defined mapping *cmd* for the specified
* *mode*.
*
* @param {Modes.Mode} mode The mode to search.
* @param {string} cmd The candidate key mapping.
* @returns {boolean}
*/
has: function (mode, cmd) this.getStack(mode).mappings[cmd] != null,
/**
* Remove the mapping named *cmd* for *mode*.
*
* @param {Modes.Mode} mode The mode to search.
* @param {string} cmd The map name to match.
*/
remove: function (mode, cmd) {
let stack = this.getStack(mode);
for (let [i, map] in array.iterItems(stack)) {
let j = map.names.indexOf(cmd);
if (j >= 0) {
delete stack.states;
map.names.splice(j, 1);
if (map.names.length == 0) // FIX ME.
for (let [mode, stack] in Iterator(this.stacks))
this.stacks[mode] = MapHive.Stack(stack.filter(function (m) m != map));
return;
}
}
},
/**
* Remove all user-defined mappings for *mode*.
*
* @param {Modes.Mode} mode The mode to remove all mappings from.
*/
clear: function (mode) {
this.stacks[mode] = MapHive.Stack([]);
}
}, {
Stack: Class("Stack", Array, {
init: function (ary) {
let self = ary || [];
self.__proto__ = this.__proto__;
return self;
},
__iterator__: function () array.iterValues(this),
get candidates() this.states.candidates,
get mappings() this.states.mappings,
add: function (map) {
this.push(map);
delete this.states;
},
states: Class.memoize(function () {
var states = {
candidates: {},
mappings: {}
};
for (let map in this)
for (let name in values(map.keys)) {
states.mappings[name] = map;
let state = "";
for (let key in events.iterKeys(name)) {
state += key;
if (state !== name)
states.candidates[state] = (states.candidates[state] || 0) + 1;
}
}
return states;
})
})
});
/**
* @instance mappings
*/
var Mappings = Module("mappings", {
init: function () {
this.user = MapHive("user", "User-defined mappings");
this.builtin = MapHive("builtin", "Builtin mappings");
this.builtinHives = array([this.user, this.builtin]);
this.allHives = [this.user, this.builtin];
},
repeat: Modes.boundProperty(),
hives: Class.memoize(function () array(this.allHives.filter(function (h) h.filter(buffer.uri)))),
get userHives() this.allHives.filter(function (h) h !== this.builtin, this),
expandLeader: function (keyString) keyString.replace(/<Leader>/i, options["mapleader"]),
iterate: function (mode) {
let seen = {};
for (let hive in this.hives.iterValues())
for (let map in array(hive.getStack(mode)).iterValues())
if (!set.add(seen, map.name))
yield map;
},
// NOTE: just normal mode for now
/** @property {Iterator(Map)} */
__iterator__: function () this.iterate(modes.NORMAL),
getDefault: deprecated("mappings.builtin.get", function getDefault(mode, cmd) this.builtin.get(mode, cmd)),
getUserIterator: deprecated("mappings.user.iterator", function getUserIterator(modes) this.user.iterator(modes)),
hasMap: deprecated("mappings.user.has", function hasMap(mode, cmd) this.user.has(mode, cmd)),
remove: deprecated("mappings.user.remove", function remove(mode, cmd) this.user.remove(mode, cmd)),
removeAll: deprecated("mappings.user.clear", function removeAll(mode) this.user.clear(mode)),
/**
* Adds a new default key mapping.
*
* @param {number[]} modes The modes that this mapping applies to.
* @param {string[]} keys The key sequences which are bound to *action*.
* @param {string} description A description of the key mapping.
* @param {function} action The action invoked by each key sequence.
* @param {Object} extra An optional extra configuration hash.
* @optional
*/
add: function () {
let map = this.builtin.add.apply(this.builtin, arguments);
map.definedAt = Commands.getCaller(Components.stack.caller);
return map;
},
/**
* Adds a new user-defined key mapping.
*
* @param {number[]} modes The modes that this mapping applies to.
* @param {string[]} keys The key sequences which are bound to *action*.
* @param {string} description A description of the key mapping.
* @param {function} action The action invoked by each key sequence.
* @param {Object} extra An optional extra configuration hash (see
* {@link Map#extraInfo}).
* @optional
*/
addUserMap: function () {
let map = this.user.add.apply(this.user, arguments);
map.definedAt = Commands.getCaller(Components.stack.caller);
return map;
},
addHive: function addHive(name, filter, description) {
this.removeHive(name);
let hive = MapHive(name, description, filter);
this.allHives.unshift(hive);
return hive;
},
removeHive: function removeHive(name, filter) {
let hive = this.getHive(name);
dactyl.assert(!hive || !hive.builtin, "Cannot remove builtin group");
if (hive)
this.allHives.splice(this.allHives.indexOf(hive), 1);
if (io.sourcing && io.sourcing.mapHive == hive)
io.sourcing.mapHive = null;
delete this.hives;
return hive;
},
getHive: function getHive(name) array.nth(this.allHives, function (h) h.name == name, 0) || null,
/**
* Returns the map from *mode* named *cmd*.
*
* @param {number} mode The mode to search.
* @param {string} cmd The map name to match.
* @returns {Map}
*/
get: function get(mode, cmd) this.hives.map(function (h) h.get(mode, cmd)).compact()[0] || null,
/**
* Returns an array of maps with names starting with but not equal to
* *prefix*.
*
* @param {number} mode The mode to search.
* @param {string} prefix The map prefix string to match.
* @returns {Map[]}
*/
getCandidates: function (mode, prefix)
this.hives.map(function (h) h.getCandidates(mode, prefix))
.flatten(),
/**
* Lists all user-defined mappings matching *filter* for the specified
* *modes*.
*
* @param {number[]} modes An array of modes to search.
* @param {string} filter The filter string to match.
*/
list: function (modes, filter, hives) {
hives = hives || mappings.userHives;
let modeSign = "";
modes.filter(function (m) m.char).forEach(function (m) { modeSign += m.char; });
modes.filter(function (m) !m.char).forEach(function (m) { modeSign += " " + m.name; });
modeSign = modeSign.replace(/^ /, "");
function maps(hive) {
let maps = hive.iterate(modes);
if (filter)
maps = [map for (map in maps) if (map.names[0] == filter)];
return maps;
}
let list = <table>
<tr highlight="Title">
<td/>
<td style="padding-right: 1em;">Mode</td>
<td style="padding-right: 1em;">Command</td>
<td style="padding-right: 1em;">Action</td>
</tr>
<col style="min-width: 6em; padding-right: 1em;"/>
{
template.map(hives, function (hive) let (i = 0)
<tr style="height: .5ex;"/> +
template.map(maps(hive), function (map)
template.map(map.names, function (name)
<tr>
<td highlight="Title">{!i++ ? hive.name : ""}</td>
<td>{modeSign}</td>
<td>{name}</td>
<td>{map.rhs || map.action.toSource()}</td>
</tr>)) +
<tr style="height: .5ex;"/>)
}
</table>;
// TODO: Move this to an ItemList to show this automatically
if (list.*.length() === list.text().length())
dactyl.echomsg("No mapping found");
else
commandline.commandOutput(list);
}
}, {
}, {
commands: function () {
function addMapCommands(ch, mapmodes, modeDescription) {
function map(args, noremap) {
let mapmodes = array.uniq(args["-modes"].map(findMode));
let hives = args.explicitOpts["-group"] ? [args["-group"]] : null;
if (!args.length) {
mappings.list(mapmodes, null, hives);
return;
}
let [lhs, rhs] = args;
if (noremap)
args["-builtin"] = true;
if (!rhs) // list the mapping
mappings.list(mapmodes, mappings.expandLeader(lhs), hives);
else {
args["-group"].add(mapmodes, [lhs],
args["-description"],
Command.bindMacro(args, "-keys", function (params) params),
{
arg: args["-arg"],
count: args["-count"],
noremap: args["-builtin"],
persist: !args["-nopersist"],
get rhs() String(this.action),
silent: args["-silent"]
});
}
}
const opts = {
completer: function (context, args) {
let mapmodes = array.uniq(args["-modes"].map(findMode));
if (args.length == 1)
return completion.userMapping(context, mapmodes, args["-group"]);
if (args.length == 2) {
if (args["-javascript"])
return completion.javascript(context);
if (args["-ex"])
return completion.ex(context);
}
},
hereDoc: true,
literal: 1,
options: [
{
names: ["-arg", "-a"],
description: "Accept an argument after the requisite key press",
},
{
names: ["-builtin", "-b"],
description: "Execute this mapping as if there were no user-defined mappings"
},
{
names: ["-count", "-c"],
description: "Accept a count before the requisite key press"
},
{
names: ["-description", "-desc", "-d"],
description: "A description of this mapping",
default: "User-defined mapping",
type: CommandOption.STRING
},
{
names: ["-ex", "-e"],
description: "Execute this mapping as an Ex command rather than keys"
},
groupFlag,
{
names: ["-javascript", "-js", "-j"],
description: "Execute this mapping as JavaScript rather than keys"
},
update({}, modeFlag, {
names: ["-modes", "-mode", "-m"],
type: CommandOption.LIST,
description: "Create this mapping in the given modes",
default: mapmodes || ["n", "v"]
}),
{
names: ["-nopersist", "-n"],
description: "Do not save this mapping to an auto-generated RC file"
},
{
names: ["-silent", "-s", "<silent>", "<Silent>"],
description: "Do not echo any generated keys to the command line"
}
],
serialize: function () {
return this.name != "map" ? [] :
array(mappings.userHives)
.filter(function (h) !h.noPersist)
.map(function (hive) [
{
command: "mapgroup",
bang: true,
arguments: [hive.name, String(hive.filter)].slice(0, hive.name == "user" ? 1 : 2)
}
].concat([
{
command: "map",
options: array([
["-modes", uniqueModes(map.modes)],
["-description", map.description],
map.silent && ["-silent"]])
.filter(util.identity)
.toObject(),
arguments: [map.names[0]],
literalArg: map.rhs,
ignoreDefaults: true
}
for (map in userMappings(hive))
if (map.persist)
]))
.flatten().array;
}
};
function userMappings(hive) {
let seen = {};
for (let stack in values(hive.stacks))
for (let map in array.iterValues(stack))
if (!set.add(seen, map.id))
yield map;
}
modeDescription = modeDescription ? " in " + modeDescription + " mode" : "";
commands.add([ch ? ch + "m[ap]" : "map"],
"Map a key sequence" + modeDescription,
function (args) { map(args, false); },
opts);
commands.add([ch + "no[remap]"],
"Map a key sequence without remapping keys" + modeDescription,
function (args) { map(args, true); },
opts);
commands.add([ch + "mapc[lear]"],
"Remove all mappings" + modeDescription,
function (args) {
let mapmodes = array.uniq(args["-modes"].map(findMode));
mapmodes.forEach(function (mode) {
args["-group"].clear(mode);
});
},
{
argCount: "0",
options: [
groupFlag,
update({}, modeFlag, {
names: ["-modes", "-mode", "-m"],
type: CommandOption.LIST,
description: "Remove all mappings from the given modes",
default: mapmodes || ["n", "v"]
})
]
});
commands.add([ch + "unm[ap]"],
"Remove a mapping" + modeDescription,
function (args) {
let mapmodes = array.uniq(args["-modes"].map(findMode));
let found = false;
for (let [, mode] in Iterator(mapmodes)) {
if (args["-group"].has(mode, args[0])) {
args["-group"].remove(mode, args[0]);
found = true;
}
}
if (!found)
dactyl.echoerr("E31: No such mapping");
},
{
argCount: "1",
completer: opts.completer,
options: [
groupFlag,
update({}, modeFlag, {
names: ["-modes", "-mode", "-m"],
type: CommandOption.LIST,
description: "Remove mapping from the given modes",
default: mapmodes || ["n", "v"]
})
]
});
}
commands.add(["mapg[roup]"],
"Create or select a mapping group",
function (args) {
dactyl.assert(args.length <= 2, "Trailing characters");
if (args.length == 0)
return void completion.listCompleter("mapGroup", "");
let name = Option.dequote(args[0]);
let hive = mappings.getHive(name);
if (args.length == 2) {
dactyl.assert(!hive || args.bang, "Group exists");
let filter = function siteFilter(uri)
siteFilter.filters.every(function (f) f(uri) == f.result);
update(filter, {
toString: function () this.filters.join(","),
filters: Option.splitList(args[1], true).map(function (pattern) {
let [, res, filter] = /^(!?)(.*)/.exec(pattern);
return update(Styles.matchFilter(Option.dequote(filter)), {
result: !res,
toString: function () pattern
});
})
});
hive = mappings.addHive(name, filter, args["-description"]);
if (args["-nopersist"])
hive.noPersist = true;
}
dactyl.assert(hive, "No mapping group: " + name);
dactyl.assert(hive.name != "builtin", "Can't map to builtin hive");
if (io.sourcing)
io.sourcing.mapHive = hive;
},
{
argCount: "*",
bang: true,
completer: function (context, args) {
if (args.length == 1)
completion.mapGroup(context);
else {
Option.splitList(context.filter);
context.advance(Option._splitAt);
context.compare = CompletionContext.Sort.unsorted;
context.completions = [
[buffer.uri.host, "Current Host"],
[buffer.uri.spec, "Current Page"]
];
}
},
keepQuotes: true,
options: [
{
names: ["-description", "-desc", "-d"],
description: "A description of this mapping group",
type: CommandOption.STRING
},
{
names: ["-nopersist", "-n"],
description: "Do not save this mapping group to an auto-generated RC file"
}
]
});
commands.add(["delmapg[roup]"],
"Delete a mapping group",
function (args) {
dactyl.assert(mappings.getHive(args[0]), "No mapping group: " + args[0]);
mappings.removeHive(args[0]);
},
{
argCount: "1",
completer: function (context, args) {
completion.mapGroup(context);
context.filters.push(function ({ item }) !item.builtin);
}
});
let groupFlag = {
names: ["-group", "-g"],
description: "Mapping group to which to add this mapping",
type: ArgType("map-group", function (group) isString(group) ? mappings.getHive(group) : group),
get default() io.sourcing && io.sourcing.mapHive || mappings.user,
completer: function (context) completion.mapGroup(context)
};
let modeFlag = {
names: ["-mode", "-m"],
type: CommandOption.STRING,
validator: function (value) Array.concat(value).every(findMode),
completer: function () [[array.compact([mode.name.toLowerCase().replace(/_/g, "-"), mode.char]), mode.description]
for (mode in values(modes.all))
if (!mode.hidden)],
};
function findMode(name) {
if (name)
for (let mode in values(modes.all))
if (name == mode || name == mode.char
|| String.toLowerCase(name).replace(/-/g, "_") == mode.name.toLowerCase())
return mode;
return null;
}
function uniqueModes(modes) {
let chars = [k for ([k, v] in Iterator(modules.modes.modeChars))
if (v.every(function (mode) modes.indexOf(mode) >= 0))];
return array.uniq(modes.filter(function (m) chars.indexOf(m.char) < 0).concat(chars));
}
commands.add(["feedkeys", "fk"],
"Fake key events",
function (args) { events.feedkeys(args[0] || "", args.bang, false, findMode(args["-mode"])); },
{
argCount: "1",
bang: true,
literal: 0,
options: [
update({}, modeFlag, {
description: "The mode in which to feed the keys"
})
]
});
addMapCommands("", [modes.NORMAL, modes.VISUAL], "");
let args = {
getMode: function (args) findMode(args["-mode"]),
iterate: function (args) {
let mainMode = this.getMode(args);
let seen = {};
for (let mode in values([mainMode].concat(mainMode.bases)))
for (let hive in mappings.hives.iterValues())
for (let map in array.iterValues(hive.getStack(mode)))
for (let name in values(map.names))
if (!set.add(seen, name)) {
yield {
name: name,
columns: [
mode == mainMode ? "" : <span highlight="Object" style="padding-right: 1em;">{mode.name}</span>,
hive.name == "builtin" ? "" : <span highlight="Object" style="padding-right: 1em;">{hive.name}</span>
],
__proto__: map
};
}
},
format: {
description: function (map) (XML.ignoreWhitespace = false, XML.prettyPrinting = false, <>
{options.get("passkeys").has(map.name)
? <span highlight="URLExtra">(passed by {template.helpLink("'passkeys'")})</span>
: <></>}
{template.linkifyHelp(map.description + (map.rhs ? ": " + map.rhs : ""))}
</>),
help: function (map) let (char = array.compact(map.modes.map(function (m) m.char))[0])
char === "n" ? map.name : char ? char + "_" + map.name : "",
headings: ["Command", "Mode", "Group", "Description"]
}
}
dactyl.addUsageCommand({
__proto__: args,
name: ["listk[eys]", "lk"],
description: "List all mappings along with their short descriptions",
options: [
update({}, modeFlag, {
default: "n",
description: "The mode for which to list mappings"
})
]
});
iter.forEach(modes.mainModes, function (mode) {
if (mode.char && !commands.get(mode.char + "listkeys", true))
dactyl.addUsageCommand({
__proto__: args,
name: [mode.char + "listk[eys]", mode.char + "lk"],
iterateIndex: function (args)
let (self = this, prefix = mode.char == "n" ? "" : mode.char + "_")
({ helpTag: prefix + map.name, __proto__: map }
for (map in self.iterate(args))),
description: "List all " + mode.name + " mode mappings along with their short descriptions",
index: mode.char + "-map",
getMode: function (args) mode,
options: []
});
});
for (let mode in modes.mainModes)
if (mode.char && !commands.get(mode.char + "map", true))
addMapCommands(mode.char,
[m.mask for (m in modes.mainModes) if (m.char == mode.char)],
[mode.name.toLowerCase()]);
},
completion: function () {
completion.mapGroup = function mapGroup(context, modes) {
context.title = ["Map group"];
context.keys = { text: "name", description: function (h) h.description || h.filter };
context.completions = mappings.userHives;
};
completion.userMapping = function userMapping(context, modes, hive) {
// FIXME: have we decided on a 'standard' way to handle this clash? --djk
hive = hive || mappings.user;
modes = modes || [modules.modes.NORMAL];
context.keys = { text: function (m) m.names[0], description: function (m) m.description + ": " + m.action };
context.completions = hive.iterate(modes);
};
},
javascript: function () {
JavaScript.setCompleter(this.get,
[
null,
function (context, obj, args) {
let mode = args[0];
return array.flatten([
[[name, map.description] for ([i, name] in Iterator(map.names))]
for (map in mappings.iterate(mode))
]);
}
]);
},
options: function () {
options.add(["mapleader", "ml"],
"Define the replacement keys for the <Leader> pseudo-key",
"string", "\\", {
setter: function (value) {
if (this.hasChanged)
for (let hive in values(mappings.allHives))
for (let stack in values(hive.stacks))
delete stack.states;
return value;
}
});
}
});
// vim: set fdm=marker sw=4 ts=4 et:
|