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
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
|
// 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@gmail.com>
//
// 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 */
var CommandWidgets = Class("CommandWidgets", {
depends: ["statusline"],
init: function init() {
let s = "dactyl-statusline-field-";
XML.ignoreWhitespace = true;
util.overlayWindow(window, {
objects: {
eventTarget: commandline
},
append: <e4x xmlns={XUL} xmlns:dactyl={NS}>
<vbox id={config.commandContainer}>
<vbox class="dactyl-container" hidden="false" collapsed="true">
<iframe class="dactyl-completions" id="dactyl-completions-dactyl-commandline" src="dactyl://content/buffer.xhtml"
contextmenu="dactyl-contextmenu"
flex="1" hidden="false" collapsed="false"
highlight="Events" events="mowEvents" />
</vbox>
<stack orient="horizontal" align="stretch" class="dactyl-container" id="dactyl-container" highlight="CmdLine CmdCmdLine">
<textbox class="plain" id="dactyl-strut" flex="1" crop="end" collapsed="true"/>
<textbox class="plain" id="dactyl-mode" flex="1" crop="end"/>
<textbox class="plain" id="dactyl-message" flex="1" readonly="true"/>
<hbox id="dactyl-commandline" hidden="false" class="dactyl-container" highlight="Normal CmdNormal" collapsed="true">
<label id="dactyl-commandline-prompt" class="dactyl-commandline-prompt plain" flex="0" crop="end" value="" collapsed="true"/>
<textbox id="dactyl-commandline-command" class="dactyl-commandline-command plain" flex="1" type="input" timeout="100"
highlight="Events" />
</hbox>
</stack>
<vbox class="dactyl-container" hidden="false" collapsed="false" highlight="CmdLine">
<textbox id="dactyl-multiline-input" class="plain" flex="1" rows="1" hidden="false" collapsed="true" multiline="true"
highlight="Normal Events" events="multilineInputEvents" />
</vbox>
</vbox>
<stack id="dactyl-statusline-stack">
<hbox id={s + "commandline"} hidden="false" class="dactyl-container" highlight="Normal StatusNormal" collapsed="true">
<label id={s + "commandline-prompt"} class="dactyl-commandline-prompt plain" flex="0" crop="end" value="" collapsed="true"/>
<textbox id={s + "commandline-command"} class="dactyl-commandline-command plain" flex="1" type="text" timeout="100"
highlight="Events" />
</hbox>
</stack>
</e4x>.elements(),
before: <e4x xmlns={XUL} xmlns:dactyl={NS}>
<toolbar id={statusline.statusBar.id}>
<vbox id={"dactyl-completions-" + s + "commandline-container"} class="dactyl-container" hidden="false" collapsed="true">
<iframe class="dactyl-completions" id={"dactyl-completions-" + s + "commandline"} src="dactyl://content/buffer.xhtml"
contextmenu="dactyl-contextmenu" flex="1" hidden="false" collapsed="false"
highlight="Events" events="mowEvents" />
</vbox>
</toolbar>
</e4x>.elements()
});
this.elements = {};
this.addElement({
name: "container",
noValue: true
});
this.addElement({
name: "commandline",
getGroup: function () options.get("guioptions").has("C") ? this.commandbar : this.statusbar,
getValue: function () this.command
});
this.addElement({
name: "strut",
defaultGroup: "Normal",
getGroup: function () this.commandbar,
getValue: function () options.get("guioptions").has("c")
});
this.addElement({
name: "command",
test: function test(stack, prev) stack.pop && !isinstance(prev.main, modes.COMMAND_LINE),
id: "commandline-command",
get: function command_get(elem) {
// The long path is because of complications with the
// completion preview.
try {
return elem.inputField.editor.rootElement.firstChild.textContent;
}
catch (e) {
return elem.value;
}
},
getElement: CommandWidgets.getEditor,
getGroup: function (value) this.activeGroup.commandline,
onChange: function command_onChange(elem, value) {
if (elem.inputField != dactyl.focusedElement)
try {
elem.selectionStart = elem.value.length;
elem.selectionEnd = elem.value.length;
}
catch (e) {}
if (!elem.collapsed)
dactyl.focus(elem);
},
onVisibility: function command_onVisibility(elem, visible) {
if (visible)
dactyl.focus(elem);
}
});
this.addElement({
name: "prompt",
id: "commandline-prompt",
defaultGroup: "CmdPrompt",
getGroup: function () this.activeGroup.commandline
});
this.addElement({
name: "message",
defaultGroup: "Normal",
getElement: CommandWidgets.getEditor,
getGroup: function (value) {
if (this.command && !options.get("guioptions").has("M"))
return this.statusbar;
let statusElem = this.statusbar.message;
if (value && !value[2] && statusElem.editor && statusElem.editor.rootElement.scrollWidth > statusElem.scrollWidth)
return this.commandbar;
return this.activeGroup.mode;
}
});
this.addElement({
name: "mode",
defaultGroup: "ModeMsg",
getGroup: function (value) {
if (!options.get("guioptions").has("M"))
if (this.commandbar.container.clientHeight == 0 ||
value && !this.commandbar.commandline.collapsed)
return this.statusbar;
return this.commandbar;
}
});
this.updateVisibility();
},
addElement: function addElement(obj) {
const self = this;
this.elements[obj.name] = obj;
function get(prefix, map, id) (obj.getElement || util.identity)(map[id] || document.getElementById(prefix + id));
this.active.__defineGetter__(obj.name, function () self.activeGroup[obj.name][obj.name]);
this.activeGroup.__defineGetter__(obj.name, function () self.getGroup(obj.name));
memoize(this.statusbar, obj.name, function () get("dactyl-statusline-field-", statusline.widgets, (obj.id || obj.name)));
memoize(this.commandbar, obj.name, function () get("dactyl-", {}, (obj.id || obj.name)));
if (!(obj.noValue || obj.getValue)) {
Object.defineProperty(this, obj.name, Modes.boundProperty({
test: obj.test,
get: function get_widgetValue() {
let elem = self.getGroup(obj.name, obj.value)[obj.name];
if (obj.value != null)
return [obj.value[0],
obj.get ? obj.get.call(this, elem) : elem.value]
.concat(obj.value.slice(2));
return null;
},
set: function set_widgetValue(val) {
if (val != null && !isArray(val))
val = [obj.defaultGroup || "", val];
obj.value = val;
[this.commandbar, this.statusbar].forEach(function (nodeSet) {
let elem = nodeSet[obj.name];
if (val == null)
elem.value = "";
else {
highlight.highlightNode(elem,
(val[0] != null ? val[0] : obj.defaultGroup)
.split(/\s/).filter(util.identity)
.map(function (g) g + " " + nodeSet.group + g)
.join(" "));
elem.value = val[1];
if (obj.onChange)
obj.onChange.call(this, elem, val);
}
}, this);
this.updateVisibility();
return val;
}
}).init(obj.name));
}
else if (obj.defaultGroup) {
[this.commandbar, this.statusbar].forEach(function (nodeSet) {
let elem = nodeSet[obj.name];
if (elem)
highlight.highlightNode(elem, obj.defaultGroup.split(/\s/)
.map(function (g) g + " " + nodeSet.group + g).join(" "));
});
}
},
getGroup: function getgroup(name, value) {
if (!statusline.visible)
return this.commandbar;
return this.elements[name].getGroup.call(this, arguments.length > 1 ? value : this[name]);
},
updateVisibility: function updateVisibility() {
for (let elem in values(this.elements))
if (elem.getGroup) {
let value = elem.getValue ? elem.getValue.call(this)
: elem.noValue || this[elem.name];
let activeGroup = this.getGroup(elem.name, value);
for (let group in values([this.commandbar, this.statusbar])) {
let meth, node = group[elem.name];
let visible = (value && group === activeGroup);
if (node && !node.collapsed == !visible) {
node.collapsed = !visible;
if (elem.onVisibility)
elem.onVisibility.call(this, node, visible);
}
}
}
// Hack. Collapse hidden elements in the stack.
// Might possibly be better to use a deck and programmatically
// choose which element to select.
function check(node) {
if (util.computedStyle(node).display === "-moz-stack") {
let nodes = Array.filter(node.children, function (n) !n.collapsed && n.boxObject.height);
nodes.forEach(function (node, i) node.style.opacity = (i == nodes.length - 1) ? "" : "0");
}
Array.forEach(node.children, check);
}
[this.commandbar.container, this.statusbar.container].forEach(check);
},
active: Class.memoize(Object),
activeGroup: Class.memoize(Object),
commandbar: Class.memoize(function () ({ group: "Cmd" })),
statusbar: Class.memoize(function () ({ group: "Status" })),
_ready: function _ready(elem) {
return elem.contentDocument.documentURI === elem.getAttribute("src") &&
["viewable", "complete"].indexOf(elem.contentDocument.readyState) >= 0;
},
_whenReady: function _whenReady(id, init) {
let elem = document.getElementById(id);
while (!this._ready(elem))
yield 10;
if (init)
init.call(this, elem);
yield elem;
},
completionContainer: Class.memoize(function () this.completionList.parentNode),
contextMenu: Class.memoize(function () {
["copy", "copylink", "selectall"].forEach(function (tail) {
// some host apps use "hostPrefixContext-copy" ids
let xpath = "//xul:menuitem[contains(@id, '" + "ontext-" + tail + "') and not(starts-with(@id, 'dactyl-'))]";
document.getElementById("dactyl-context-" + tail).style.listStyleImage =
util.computedStyle(util.evaluateXPath(xpath, document).snapshotItem(0)).listStyleImage;
});
return document.getElementById("dactyl-contextmenu");
}),
multilineOutput: Class.memoize(function () this._whenReady("dactyl-multiline-output", function (elem) {
elem.contentWindow.addEventListener("unload", function (event) { event.preventDefault(); }, true);
elem.contentDocument.documentElement.id = "dactyl-multiline-output-top";
elem.contentDocument.body.id = "dactyl-multiline-output-content";
}), true),
multilineInput: Class.memoize(function () document.getElementById("dactyl-multiline-input")),
mowContainer: Class.memoize(function () document.getElementById("dactyl-multiline-output-container"))
}, {
getEditor: function getEditor(elem) {
elem.inputField.QueryInterface(Ci.nsIDOMNSEditableElement);
return elem;
}
});
var CommandMode = Class("CommandMode", {
init: function CM_init() {
this.keepCommand = userContext.hidden_option_command_afterimage;
},
get autocomplete() options["autocomplete"].length,
get command() this.widgets.command[1],
set command(val) this.widgets.command = val,
get prompt() this.widgets.prompt,
set prompt(val) this.widgets.prompt = val,
open: function CM_open(command) {
dactyl.assert(isinstance(this.mode, modes.COMMAND_LINE),
/*L*/"Not opening command line in non-command-line mode.");
this.messageCount = commandline.messageCount;
modes.push(this.mode, this.extendedMode, this.closure);
this.widgets.active.commandline.collapsed = false;
this.widgets.prompt = this.prompt;
this.widgets.command = command || "";
this.input = this.widgets.active.command.inputField;
if (this.historyKey)
this.history = CommandLine.History(this.input, this.historyKey, this);
if (this.complete)
this.completions = CommandLine.Completions(this.input, this);
if (this.completions && command && commandline.commandSession === this)
this.completions.autocompleteTimer.flush(true);
},
get active() this === commandline.commandSession,
get holdFocus() this.widgets.active.command.inputField,
get mappingSelf() this,
get widgets() commandline.widgets,
enter: function CM_enter(stack) {
commandline.commandSession = this;
if (stack.pop && commandline.command) {
this.onChange(commandline.command);
if (this.completions && stack.pop)
this.completions.complete(true, false);
}
},
leave: function CM_leave(stack) {
if (!stack.push) {
commandline.commandSession = null;
this.input.dactylKeyPress = undefined;
if (this.completions)
this.completions.cleanup();
if (this.history)
this.history.save();
this.resetCompletions();
commandline.hideCompletions();
modes.delay(function () {
if (!this.keepCommand || commandline.silent || commandline.quiet)
commandline.hide();
this[this.accepted ? "onSubmit" : "onCancel"](commandline.command);
if (commandline.messageCount === this.messageCount)
commandline.clearMessage();
}, this);
}
},
events: {
input: function CM_onInput(event) {
if (this.completions) {
this.resetCompletions();
this.completions.autocompleteTimer.tell(false);
}
this.onChange(commandline.command);
},
keyup: function CM_onKeyUp(event) {
let key = events.toString(event);
if (/-?Tab>$/.test(key) && this.completions)
this.completions.tabTimer.flush();
}
},
keepCommand: false,
onKeyPress: function CM_onKeyPress(events) {
if (this.completions)
this.completions.previewClear();
return true; /* Pass event */
},
onCancel: function (value) {},
onChange: function (value) {},
onHistory: function (value) {},
onSubmit: function (value) {},
resetCompletions: function CM_resetCompletions() {
if (this.completions) {
this.completions.context.cancelAll();
this.completions.wildIndex = -1;
this.completions.previewClear();
}
if (this.history)
this.history.reset();
},
});
var CommandExMode = Class("CommandExMode", CommandMode, {
get mode() modes.EX,
historyKey: "command",
prompt: ["Normal", ":"],
complete: function CEM_complete(context) {
context.fork("ex", 0, completion, "ex");
},
onSubmit: function CEM_onSubmit(command) {
contexts.withContext({ file: /*L*/"[Command Line]", line: 1 },
function _onSubmit() {
io.withSavedValues(["readHeredoc"], function _onSubmit() {
this.readHeredoc = commandline.readHeredoc;
commands.repeat = command;
dactyl.execute(command);
});
});
}
});
var CommandPromptMode = Class("CommandPromptMode", CommandMode, {
init: function init(prompt, params) {
this.prompt = isArray(prompt) ? prompt : ["Question", prompt];
update(this, params);
init.supercall(this);
},
complete: function CPM_complete(context) {
if (this.completer)
context.forkapply("prompt", 0, this, "completer", Array.slice(arguments, 1));
},
get mode() modes.PROMPT
});
/**
* This class is used for prompting of user input and echoing of messages.
*
* It consists of a prompt and command field be sure to only create objects of
* this class when the chrome is ready.
*/
var CommandLine = Module("commandline", {
init: function init() {
const self = this;
this._callbacks = {};
memoize(this, "_store", function () storage.newMap("command-history", { store: true, privateData: true }));
for (let name in values(["command", "search"]))
if (storage.exists("history-" + name)) {
let ary = storage.newArray("history-" + name, { store: true, privateData: true });
this._store.set(name, [v for ([k, v] in ary)]);
ary.delete();
this._store.changed();
}
this._messageHistory = { //{{{
_messages: [],
get messages() {
let max = options["messages"];
// resize if 'messages' has changed
if (this._messages.length > max)
this._messages = this._messages.splice(this._messages.length - max);
return this._messages;
},
get length() this._messages.length,
clear: function clear() {
this._messages = [];
},
filter: function filter(fn, self) {
this._messages = this._messages.filter(fn, self);
},
add: function add(message) {
if (!message)
return;
if (this._messages.length >= options["messages"])
this._messages.shift();
this._messages.push(update({
timestamp: Date.now()
}, message));
}
}; //}}}
},
signals: {
"browser.locationChange": function (webProgress, request, uri) {
this.clear();
}
},
/**
* Determines whether the command line should be visible.
*
* @returns {boolean}
*/
get commandVisible() !!this.commandSession,
/**
* Ensure that the multiline input widget is the correct size.
*/
_autosizeMultilineInputWidget: function _autosizeMultilineInputWidget() {
let lines = this.widgets.multilineInput.value.split("\n").length - 1;
this.widgets.multilineInput.setAttribute("rows", Math.max(lines, 1));
},
HL_NORMAL: "Normal",
HL_ERRORMSG: "ErrorMsg",
HL_MODEMSG: "ModeMsg",
HL_MOREMSG: "MoreMsg",
HL_QUESTION: "Question",
HL_INFOMSG: "InfoMsg",
HL_WARNINGMSG: "WarningMsg",
HL_LINENR: "LineNr",
FORCE_MULTILINE : 1 << 0,
FORCE_SINGLELINE : 1 << 1,
DISALLOW_MULTILINE : 1 << 2, // If an echo() should try to use the single line
// but output nothing when the MOW is open; when also
// FORCE_MULTILINE is given, FORCE_MULTILINE takes precedence
APPEND_TO_MESSAGES : 1 << 3, // Add the string to the message history.
ACTIVE_WINDOW : 1 << 4, // Only echo in active window.
get completionContext() this._completions.context,
_silent: false,
get silent() this._silent,
set silent(val) {
this._silent = val;
this.quiet = this.quiet;
},
_quite: false,
get quiet() this._quiet,
set quiet(val) {
this._quiet = val;
["commandbar", "statusbar"].forEach(function (nodeSet) {
Array.forEach(this.widgets[nodeSet].commandline.children, function (node) {
node.style.opacity = this._quiet || this._silent ? "0" : "";
}, this);
}, this);
},
widgets: Class.memoize(function () CommandWidgets()),
runSilently: function runSilently(func, self) {
this.withSavedValues(["silent"], function () {
this.silent = true;
func.call(self);
});
},
get completionList() {
let node = this.widgets.active.commandline;
if (this.commandSession && this.commandSession.completionList)
node = document.getElementById(this.commandSession.completionList);
if (!node.completionList) {
let elem = document.getElementById("dactyl-completions-" + node.id);
util.waitFor(bind(this.widgets._ready, null, elem));
node.completionList = ItemList(elem.id);
}
return node.completionList;
},
hideCompletions: function hideCompletions() {
for (let nodeSet in values([this.widgets.statusbar, this.widgets.commandbar]))
if (nodeSet.commandline.completionList)
nodeSet.commandline.completionList.visible = false;
},
_lastClearable: Modes.boundProperty(),
messages: Modes.boundProperty(),
multilineInputVisible: Modes.boundProperty({
set: function set_miwVisible(value) { this.widgets.multilineInput.collapsed = !value; }
}),
get command() {
if (this.commandVisible && this.widgets.command)
return commands.lastCommand = this.widgets.command[1];
return commands.lastCommand;
},
set command(val) {
if (this.commandVisible && (modes.extended & modes.EX))
return this.widgets.command = val;
return commands.lastCommand = val;
},
clear: function clear(scroll) {
if (!scroll || Date.now() - this._lastEchoTime > 5000)
this.clearMessage();
this._lastEchoTime = 0;
if (!this.commandSession) {
this.widgets.command = null;
this.hideCompletions();
}
if (modes.main == modes.OUTPUT_MULTILINE && !mow.isScrollable(1))
modes.pop();
if (!modes.have(modes.OUTPUT_MULTILINE))
mow.visible = false;
},
clearMessage: function clearMessage() {
if (this.widgets.message && this.widgets.message[1] === this._lastClearable)
this.widgets.message = null;
},
/**
* Displays the multi-line output of a command, preceded by the last
* executed ex command string.
*
* @param {XML} xml The output as an E4X XML object.
*/
commandOutput: function commandOutput(xml) {
XML.ignoreWhitespace = XML.prettyPrinting = false;
if (this.command)
this.echo(<><div xmlns={XHTML}>:{this.command}</div>
{xml}</>, this.HIGHLIGHT_NORMAL, this.FORCE_MULTILINE);
else
this.echo(xml, this.HIGHLIGHT_NORMAL, this.FORCE_MULTILINE);
this.command = null;
},
/**
* Hides the command line, and shows any status messages that
* are under it.
*/
hide: function hide() {
this.widgets.command = null;
},
/**
* Display a message in the command-line area.
*
* @param {string} str
* @param {string} highlightGroup
* @param {boolean} forceSingle If provided, don't let over-long
* messages move to the MOW.
*/
_echoLine: function echoLine(str, highlightGroup, forceSingle, silent) {
this.widgets.message = str ? [highlightGroup, str, forceSingle] : null;
dactyl.triggerObserver("echoLine", str, highlightGroup, null, forceSingle);
if (!this.commandVisible)
this.hide();
let field = this.widgets.active.message.inputField;
if (field.value && !forceSingle && field.editor.rootElement.scrollWidth > field.scrollWidth) {
this.widgets.message = null;
mow.echo(<span highlight="Message">{str}</span>, highlightGroup, true);
}
},
_lastEcho: null,
/**
* Output the given string onto the command line. With no flags, the
* message will be shown in the status line if it's short enough to
* fit, and contains no new lines, and isn't XML. Otherwise, it will be
* shown in the MOW.
*
* @param {string} str
* @param {string} highlightGroup The Highlight group for the
* message.
* @default "Normal"
* @param {number} flags Changes the behavior as follows:
* commandline.APPEND_TO_MESSAGES - Causes message to be added to the
* messages history, and shown by :messages.
* commandline.FORCE_SINGLELINE - Forbids the command from being
* pushed to the MOW if it's too long or of there are already
* status messages being shown.
* commandline.DISALLOW_MULTILINE - Cancels the operation if the MOW
* is already visible.
* commandline.FORCE_MULTILINE - Forces the message to appear in
* the MOW.
*/
messageCount: 0,
echo: function echo(data, highlightGroup, flags) {
// dactyl.echo uses different order of flags as it omits the highlight group, change commandline.echo argument order? --mst
if (this._silent || !this.widgets)
return;
this.messageCount++;
highlightGroup = highlightGroup || this.HL_NORMAL;
if (flags & this.APPEND_TO_MESSAGES) {
let message = isObject(data) ? data : { message: data };
// Make sure the memoized message property is an instance property.
message.message;
this._messageHistory.add(update({ highlight: highlightGroup }, message));
data = message.message;
}
if ((flags & this.ACTIVE_WINDOW) &&
window != services.windowWatcher.activeWindow &&
services.windowWatcher.activeWindow.dactyl)
return;
if ((flags & this.DISALLOW_MULTILINE) && !this.widgets.mowContainer.collapsed)
return;
let single = flags & (this.FORCE_SINGLELINE | this.DISALLOW_MULTILINE);
let action = this._echoLine;
if ((flags & this.FORCE_MULTILINE) || (/\n/.test(data) || !isinstance(data, [_, "String"])) && !(flags & this.FORCE_SINGLELINE))
action = mow.closure.echo;
if (single)
this._lastEcho = null;
else {
if (this.widgets.message && this.widgets.message[1] == this._lastEcho)
mow.echo(<span highlight="Message">{this._lastEcho}</span>,
this.widgets.message[0], true);
if (action === this._echoLine && !(flags & this.FORCE_MULTILINE)
&& !(dactyl.fullyInitialized && this.widgets.mowContainer.collapsed)) {
highlightGroup += " Message";
action = mow.closure.echo;
}
this._lastEcho = (action == this._echoLine) && data;
}
this._lastClearable = action === this._echoLine && String(data);
this._lastEchoTime = (flags & this.FORCE_SINGLELINE) && Date.now();
if (action)
action.call(this, data, highlightGroup, single);
},
_lastEchoTime: 0,
/**
* Prompt the user. Sets modes.main to COMMAND_LINE, which the user may
* pop at any time to close the prompt.
*
* @param {string} prompt The input prompt to use.
* @param {function(string)} callback
* @param {Object} extra
* @... {function} onChange - A function to be called with the current
* input every time it changes.
* @... {function(CompletionContext)} completer - A completion function
* for the user's input.
* @... {string} promptHighlight - The HighlightGroup used for the
* prompt. @default "Question"
* @... {string} default - The initial value that will be returned
* if the user presses <CR> straightaway. @default ""
*/
input: function _input(prompt, callback, extra) {
extra = extra || {};
CommandPromptMode(prompt, update({ onSubmit: callback }, extra)).open();
},
readHeredoc: function readHeredoc(end) {
let args;
commandline.inputMultiline(end, function (res) { args = res; });
util.waitFor(function () args !== undefined);
return args;
},
/**
* Get a multi-line input from a user, up to but not including the line
* which matches the given regular expression. Then execute the
* callback with that string as a parameter.
*
* @param {string} end
* @param {function(string)} callback
*/
// FIXME: Buggy, especially when pasting.
inputMultiline: function inputMultiline(end, callback) {
let cmd = this.command;
let self = {
end: "\n" + end + "\n",
callback: callback
};
modes.push(modes.INPUT_MULTILINE, null, {
holdFocus: true,
leave: function leave() {
if (!self.done)
self.callback(null);
},
mappingSelf: self
});
if (cmd != false)
this._echoLine(cmd, this.HL_NORMAL);
// save the arguments, they are needed in the event handler onKeyPress
this.multilineInputVisible = true;
this.widgets.multilineInput.value = "";
this._autosizeMultilineInputWidget();
this.timeout(function () { dactyl.focus(this.widgets.multilineInput); }, 10);
},
get commandMode() this.commandSession && isinstance(modes.main, modes.COMMAND_LINE),
events: update(
iter(CommandMode.prototype.events).map(
function ([event, handler]) [
event, function (event) {
if (this.commandMode)
handler.call(this.commandSession, event);
}
]).toObject(),
{
focus: function onFocus(event) {
if (!this.commandSession
&& event.originalTarget === this.widgets.active.command.inputField) {
event.target.blur();
dactyl.beep();
}
}
}
),
get mowEvents() mow.events,
/**
* Multiline input events, they will come straight from
* #dactyl-multiline-input in the XUL.
*
* @param {Event} event
*/
multilineInputEvents: {
blur: function onBlur(event) {
if (modes.main == modes.INPUT_MULTILINE)
this.timeout(function () {
dactyl.focus(this.widgets.multilineInput.inputField);
});
},
input: function onInput(event) {
this._autosizeMultilineInputWidget();
}
},
updateOutputHeight: deprecated("mow.resize", function updateOutputHeight(open, extra) mow.resize(open, extra)),
withOutputToString: function withOutputToString(fn, self) {
dactyl.registerObserver("echoLine", observe, true);
dactyl.registerObserver("echoMultiline", observe, true);
let output = [];
function observe(str, highlight, dom) {
output.push(dom && !isString(str) ? dom : str);
}
this.savingOutput = true;
dactyl.trapErrors.apply(dactyl, [fn, self].concat(Array.slice(arguments, 2)));
this.savingOutput = false;
return output.map(function (elem) elem instanceof Node ? util.domToString(elem) : elem)
.join("\n");
}
}, {
/**
* A class for managing the history of an input field.
*
* @param {HTMLInputElement} inputField
* @param {string} mode The mode for which we need history.
*/
History: Class("History", {
init: function init(inputField, mode, session) {
this.mode = mode;
this.input = inputField;
this.reset();
this.session = session;
},
get store() commandline._store.get(this.mode, []),
set store(ary) { commandline._store.set(this.mode, ary); },
/**
* Reset the history index to the first entry.
*/
reset: function reset() {
this.index = null;
},
/**
* Save the last entry to the permanent store. All duplicate entries
* are removed and the list is truncated, if necessary.
*/
save: function save() {
if (events.feedingKeys)
return;
let str = this.input.value;
if (/^\s*$/.test(str))
return;
this.store = this.store.filter(function (line) (line.value || line) != str);
try {
this.store.push({ value: str, timestamp: Date.now()*1000, privateData: this.checkPrivate(str) });
}
catch (e) {
dactyl.reportError(e);
}
this.store = this.store.slice(-options["history"]);
},
/**
* @property {function} Returns whether a data item should be
* considered private.
*/
checkPrivate: function checkPrivate(str) {
// Not really the ideal place for this check.
if (this.mode == "command")
return commands.hasPrivateData(str);
return false;
},
/**
* Replace the current input field value.
*
* @param {string} val The new value.
*/
replace: function replace(val) {
this.input.dactylKeyPress = undefined;
if (this.completions)
this.completions.previewClear();
this.input.value = val;
this.session.onHistory(val);
},
/**
* Move forward or backward in history.
*
* @param {boolean} backward Direction to move.
* @param {boolean} matchCurrent Search for matches starting
* with the current input value.
*/
select: function select(backward, matchCurrent) {
// always reset the tab completion if we use up/down keys
if (this.session.completions)
this.session.completions.reset();
let diff = backward ? -1 : 1;
if (this.index == null) {
this.original = this.input.value;
this.index = this.store.length;
}
// search the history for the first item matching the current
// command-line string
while (true) {
this.index += diff;
if (this.index < 0 || this.index > this.store.length) {
this.index = Math.constrain(this.index, 0, this.store.length);
dactyl.beep();
// I don't know why this kludge is needed. It
// prevents the caret from moving to the end of
// the input field.
if (this.input.value == "") {
this.input.value = " ";
this.input.value = "";
}
break;
}
let hist = this.store[this.index];
// user pressed DOWN when there is no newer history item
if (!hist)
hist = this.original;
else
hist = (hist.value || hist);
if (!matchCurrent || hist.substr(0, this.original.length) == this.original) {
this.replace(hist);
break;
}
}
}
}),
/**
* A class for tab completions on an input field.
*
* @param {Object} input
*/
Completions: Class("Completions", {
init: function init(input, session) {
this.context = CompletionContext(input.QueryInterface(Ci.nsIDOMNSEditableElement).editor);
this.context.onUpdate = this.closure._reset;
this.editor = input.editor;
this.input = input;
this.session = session;
this.selected = null;
this.wildmode = options.get("wildmode");
this.wildtypes = this.wildmode.value;
this.itemList = commandline.completionList;
this.itemList.setItems(this.context);
dactyl.registerObserver("events.doneFeeding", this.closure.onDoneFeeding, true);
this.autocompleteTimer = Timer(200, 500, function autocompleteTell(tabPressed) {
if (events.feedingKeys && !tabPressed)
this.ignoredCount++;
else if (this.session.autocomplete) {
this.itemList.visible = true;
this.complete(true, false);
}
}, this);
this.tabTimer = Timer(0, 0, function tabTell(event) {
this.tab(event.shiftKey, event.altKey && options["altwildmode"]);
}, this);
},
cleanup: function () {
dactyl.unregisterObserver("events.doneFeeding", this.closure.onDoneFeeding);
this.previewClear();
this.tabTimer.reset();
this.autocompleteTimer.reset();
this.itemList.visible = false;
this.input.dactylKeyPress = undefined;
},
ignoredCount: 0,
onDoneFeeding: function onDoneFeeding() {
if (this.ignoredCount)
this.autocompleteTimer.flush(true);
this.ignoredCount = 0;
},
UP: {},
DOWN: {},
PAGE_UP: {},
PAGE_DOWN: {},
RESET: null,
lastSubstring: "",
get completion() {
let str = commandline.command;
return str.substring(this.prefix.length, str.length - this.suffix.length);
},
set completion(completion) {
this.previewClear();
// Change the completion text.
// The second line is a hack to deal with some substring
// preview corner cases.
let value = this.prefix + completion + this.suffix;
commandline.widgets.active.command.value = value;
this.editor.selection.focusNode.textContent = value;
// Reset the caret to one position after the completion.
this.caret = this.prefix.length + completion.length;
this._caret = this.caret;
this.input.dactylKeyPress = undefined;
},
get caret() this.editor.selection.getRangeAt(0).startOffset,
set caret(offset) {
this.editor.selection.getRangeAt(0).setStart(this.editor.rootElement.firstChild, offset);
this.editor.selection.getRangeAt(0).setEnd(this.editor.rootElement.firstChild, offset);
},
get start() this.context.allItems.start,
get items() this.context.allItems.items,
get substring() this.context.longestAllSubstring,
get wildtype() this.wildtypes[this.wildIndex] || "",
complete: function complete(show, tabPressed) {
this.session.ignoredCount = 0;
this.context.reset();
this.context.tabPressed = tabPressed;
this.session.complete(this.context);
if (!this.session.active)
return;
this.context.updateAsync = true;
this.reset(show, tabPressed);
this.wildIndex = 0;
this._caret = this.caret;
},
haveType: function haveType(type)
this.wildmode.checkHas(this.wildtype, type == "first" ? "" : type),
preview: function preview() {
this.previewClear();
if (this.wildIndex < 0 || this.suffix || !this.items.length)
return;
let substring = "";
switch (this.wildtype.replace(/.*:/, "")) {
case "":
substring = this.items[0].result;
break;
case "longest":
if (this.items.length > 1) {
substring = this.substring;
break;
}
// Fallthrough
case "full":
let item = this.items[this.selected != null ? this.selected + 1 : 0];
if (item)
substring = item.result;
break;
}
// Don't show 1-character substrings unless we've just hit backspace
if (substring.length < 2 && this.lastSubstring.indexOf(substring) !== 0)
return;
this.lastSubstring = substring;
let value = this.completion;
if (util.compareIgnoreCase(value, substring.substr(0, value.length)))
return;
substring = substring.substr(value.length);
this.removeSubstring = substring;
let node = util.xmlToDom(<span highlight="Preview">{substring}</span>,
document);
let start = this.caret;
this.editor.insertNode(node, this.editor.rootElement, 1);
this.caret = start;
},
previewClear: function previewClear() {
let node = this.editor.rootElement.firstChild;
if (node && node.nextSibling) {
try {
this.editor.deleteNode(node.nextSibling);
}
catch (e) {
node.nextSibling.textContent = "";
}
}
else if (this.removeSubstring) {
let str = this.removeSubstring;
let cmd = commandline.widgets.active.command.value;
if (cmd.substr(cmd.length - str.length) == str)
commandline.widgets.active.command.value = cmd.substr(0, cmd.length - str.length);
}
delete this.removeSubstring;
},
reset: function reset(show) {
this.wildIndex = -1;
this.prefix = this.context.value.substring(0, this.start);
this.value = this.context.value.substring(this.start, this.caret);
this.suffix = this.context.value.substring(this.caret);
if (show) {
this.itemList.reset();
if (this.haveType("list"))
this.itemList.visible = true;
this.selected = null;
this.wildIndex = 0;
}
this.preview();
},
_reset: function _reset() {
let value = this.editor.selection.focusNode.textContent;
this.prefix = value.substring(0, this.start);
this.value = value.substring(this.start, this.caret);
this.suffix = value.substring(this.caret);
this.itemList.reset();
this.itemList.selectItem(this.selected);
this.preview();
},
select: function select(idx) {
switch (idx) {
case this.UP:
if (this.selected == null)
idx = -2;
else
idx = this.selected - 1;
break;
case this.DOWN:
if (this.selected == null)
idx = 0;
else
idx = this.selected + 1;
break;
case this.RESET:
idx = null;
break;
default:
idx = Math.constrain(idx, 0, this.items.length - 1);
break;
}
if (idx == -1 || this.items.length && idx >= this.items.length || idx == null) {
// Wrapped. Start again.
this.selected = null;
this.completion = this.value;
}
else {
// Wait for contexts to complete if necessary.
// FIXME: Need to make idx relative to individual contexts.
let list = this.context.contextList;
if (idx == -2)
list = list.slice().reverse();
let n = 0;
try {
this.waiting = true;
for (let [, context] in Iterator(list)) {
let done = function done() !(idx >= n + context.items.length || idx == -2 && !context.items.length);
util.waitFor(function () !context.incomplete || done());
if (done())
break;
n += context.items.length;
}
}
finally {
this.waiting = false;
}
// See previous FIXME. This will break if new items in
// a previous context come in.
if (idx < 0)
idx = this.items.length - 1;
if (this.items.length == 0)
return;
this.selected = idx;
this.completion = this.items[idx].result;
}
this.itemList.selectItem(idx);
},
tabs: [],
tab: function tab(reverse, wildmode) {
this.autocompleteTimer.flush();
this.ignoredCount = 0;
if (this._caret != this.caret)
this.reset();
this._caret = this.caret;
// Check if we need to run the completer.
if (this.context.waitingForTab || this.wildIndex == -1)
this.complete(true, true);
this.tabs.push([reverse, wildmode || options["wildmode"]]);
if (this.waiting)
return;
while (this.tabs.length) {
[reverse, this.wildtypes] = this.tabs.shift();
this.wildIndex = Math.min(this.wildIndex, this.wildtypes.length - 1);
switch (this.wildtype.replace(/.*:/, "")) {
case "":
this.select(0);
break;
case "longest":
if (this.items.length > 1) {
if (this.substring && this.substring.length > this.completion.length)
this.completion = this.substring;
break;
}
// Fallthrough
case "full":
this.select(reverse ? this.UP : this.DOWN);
break;
}
if (this.haveType("list"))
this.itemList.visible = true;
this.wildIndex++;
this.preview();
if (this.selected == null)
statusline.progress = "";
else
statusline.progress = _("completion.matchIndex", this.selected + 1, this.items.length);
}
if (this.items.length == 0)
dactyl.beep();
}
}),
/**
* Evaluate a JavaScript expression and return a string suitable
* to be echoed.
*
* @param {string} arg
* @param {boolean} useColor When true, the result is a
* highlighted XML object.
*/
echoArgumentToString: function (arg, useColor) {
if (!arg)
return "";
arg = dactyl.userEval(arg);
if (isObject(arg))
arg = util.objectToString(arg, useColor);
else
arg = String(arg);
return arg;
}
}, {
commands: function init_commands() {
[
{
name: "ec[ho]",
description: "Echo the expression",
action: dactyl.echo
},
{
name: "echoe[rr]",
description: "Echo the expression as an error message",
action: dactyl.echoerr
},
{
name: "echom[sg]",
description: "Echo the expression as an informational message",
action: dactyl.echomsg
}
].forEach(function (command) {
commands.add([command.name],
command.description,
function (args) {
command.action(CommandLine.echoArgumentToString(args[0] || "", true));
}, {
completer: function (context) completion.javascript(context),
literal: 0
});
});
commands.add(["mes[sages]"],
"Display previously shown messages",
function () {
// TODO: are all messages single line? Some display an aggregation
// of single line messages at least. E.g. :source
if (commandline._messageHistory.length == 1) {
let message = commandline._messageHistory.messages[0];
commandline.echo(message.message, message.highlight, commandline.FORCE_SINGLELINE);
}
else if (commandline._messageHistory.length > 1) {
XML.ignoreWhitespace = false;
commandline.commandOutput(
template.map(commandline._messageHistory.messages, function (message)
<div highlight={message.highlight + " Message"}>{message.message}</div>));
}
},
{ argCount: "0" });
commands.add(["messc[lear]"],
"Clear the message history",
function () { commandline._messageHistory.clear(); },
{ argCount: "0" });
commands.add(["sil[ent]"],
"Run a command silently",
function (args) {
commandline.runSilently(function () commands.execute(args[0] || "", null, true));
}, {
completer: function (context) completion.ex(context),
literal: 0,
subCommand: 0
});
},
modes: function initModes() {
modes.addMode("COMMAND_LINE", {
char: "c",
description: "Active when the command line is focused",
insert: true,
ownsFocus: true,
get mappingSelf() commandline.commandSession
});
// this._extended modes, can include multiple modes, and even main modes
modes.addMode("EX", {
description: "Ex command mode, active when the command line is open for Ex commands",
bases: [modes.COMMAND_LINE]
});
modes.addMode("PROMPT", {
description: "Active when a prompt is open in the command line",
bases: [modes.COMMAND_LINE]
});
modes.addMode("INPUT_MULTILINE", {
bases: [modes.INSERT]
});
},
mappings: function init_mappings() {
mappings.add([modes.COMMAND],
[":"], "Enter Command Line mode",
function () { CommandExMode().open(""); });
mappings.add([modes.INPUT_MULTILINE],
["<Return>", "<C-j>", "<C-m>"], "Begin a new line",
function ({ self }) {
let text = "\n" + commandline.widgets.multilineInput
.value.substr(0, commandline.widgets.multilineInput.selectionStart)
+ "\n";
let index = text.indexOf(self.end);
if (index >= 0) {
self.done = true;
text = text.substring(1, index);
modes.pop();
return function () self.callback.call(commandline, text);
}
return Events.PASS;
});
let bind = function bind()
mappings.add.apply(mappings, [[modes.COMMAND_LINE]].concat(Array.slice(arguments)))
// Any "non-keyword" character triggers abbreviation expansion
// TODO: Add "<CR>" and "<Tab>" to this list
// At the moment, adding "<Tab>" breaks tab completion. Adding
// "<CR>" has no effect.
// TODO: Make non-keyword recognition smarter so that there need not
// be two lists of the same characters (one here and a regexp in
// mappings.js)
bind(["<Space>", '"', "'"], "Expand command line abbreviation",
function ({ self }) {
self.resetCompletions();
editor.expandAbbreviation(modes.COMMAND_LINE);
return Events.PASS;
});
bind(["<Return>", "<C-j>", "<C-m>"], "Accept the current input",
function ({ self }) {
let command = commandline.command;
self.accepted = true;
return function () { modes.pop(); };
});
[
[["<Up>", "<A-p>"], "previous matching", true, true],
[["<S-Up>", "<C-p>", "<PageUp>"], "previous", true, false],
[["<Down>", "<A-n>"], "next matching", false, true],
[["<S-Down>", "<C-n>", "<PageDown>"], "next", false, false]
].forEach(function ([keys, desc, up, search]) {
bind(keys, "Recall the " + desc + " command line from the history list",
function ({ self }) {
dactyl.assert(self.history);
self.history.select(up, search);
});
});
bind(["<A-Tab>", "<Tab>"], "Select the next matching completion item",
function ({ keypressEvents, self }) {
dactyl.assert(self.completions);
self.completions.tabTimer.tell(keypressEvents[0]);
});
bind(["<A-S-Tab>", "<S-Tab>"], "Select the previous matching completion item",
function ({ keypressEvents, self }) {
dactyl.assert(self.completions);
self.completions.tabTimer.tell(keypressEvents[0]);
});
bind(["<BS>", "<C-h>"], "Delete the previous character",
function () {
if (!commandline.command)
modes.pop();
else
return Events.PASS;
});
bind(["<C-]>", "<C-5>"], "Expand command line abbreviation",
function () { editor.expandAbbreviation(modes.COMMAND_LINE); });
},
options: function init_options() {
options.add(["history", "hi"],
"Number of Ex commands and search patterns to store in the command-line history",
"number", 500,
{ validator: function (value) value >= 0 });
options.add(["maxitems"],
"Maximum number of completion items to display at once",
"number", 20,
{ validator: function (value) value >= 1 });
options.add(["messages", "msgs"],
"Number of messages to store in the :messages history",
"number", 100,
{ validator: function (value) value >= 0 });
},
sanitizer: function init_sanitizer() {
sanitizer.addItem("commandline", {
description: "Command-line and search history",
persistent: true,
action: function (timespan, host) {
let store = commandline._store;
for (let [k, v] in store) {
if (k == "command")
store.set(k, v.filter(function (item)
!(timespan.contains(item.timestamp) && (!host || commands.hasDomain(item.value, host)))));
else if (!host)
store.set(k, v.filter(function (item) !timespan.contains(item.timestamp)));
}
}
});
// Delete history-like items from the commandline and messages on history purge
sanitizer.addItem("history", {
action: function (timespan, host) {
commandline._store.set("command",
commandline._store.get("command", []).filter(function (item)
!(timespan.contains(item.timestamp) && (host ? commands.hasDomain(item.value, host)
: item.privateData))));
commandline._messageHistory.filter(function (item) !timespan.contains(item.timestamp * 1000) ||
!item.domains && !item.privateData ||
host && (!item.domains || !item.domains.some(function (d) util.isSubdomain(d, host))));
}
});
sanitizer.addItem("messages", {
description: "Saved :messages",
action: function (timespan, host) {
commandline._messageHistory.filter(function (item) !timespan.contains(item.timestamp * 1000) ||
host && (!item.domains || !item.domains.some(function (d) util.isSubdomain(d, host))));
}
});
}
});
/**
* The list which is used for the completion box (and QuickFix window in
* future).
*
* @param {string} id The id of the iframe which will display the list. It
* must be in its own container element, whose height it will update as
* necessary.
*/
var ItemList = Class("ItemList", {
init: function init(id) {
this._completionElements = [];
var iframe = document.getElementById(id);
this._doc = iframe.contentDocument;
this._win = iframe.contentWindow;
this._container = iframe.parentNode;
this._doc.documentElement.id = id + "-top";
this._doc.body.id = id + "-content";
this._doc.body.className = iframe.className + "-content";
this._doc.body.appendChild(this._doc.createTextNode(""));
this._doc.body.style.borderTop = "1px solid black"; // FIXME: For cases where completions/MOW are shown at once, or ls=0. Should use :highlight.
this._items = null;
this._startIndex = -1; // The index of the first displayed item
this._endIndex = -1; // The index one *after* the last displayed item
this._selIndex = -1; // The index of the currently selected element
this._div = null;
this._divNodes = {};
this._minHeight = 0;
},
_dom: function _dom(xml, map) util.xmlToDom(xml instanceof XML ? xml : <>{xml}</>, this._doc, map),
_autoSize: function _autoSize() {
if (!this._div)
return;
if (this._container.collapsed)
this._div.style.minWidth = document.getElementById("dactyl-commandline").scrollWidth + "px";
this._minHeight = Math.max(this._minHeight,
this._win.scrollY + this._divNodes.completions.getBoundingClientRect().bottom);
if (this._container.collapsed)
this._div.style.minWidth = "";
// FIXME: Belongs elsewhere.
mow.resize(false, Math.max(0, this._minHeight - this._container.height));
this._container.height = this._minHeight;
this._container.height -= mow.spaceNeeded;
mow.resize(false);
this.timeout(function () {
this._container.height -= mow.spaceNeeded;
});
},
_getCompletion: function _getCompletion(index) this._completionElements.snapshotItem(index - this._startIndex),
_init: function _init() {
this._div = this._dom(
<div class="ex-command-output" highlight="Normal" style="white-space: nowrap">
<div highlight="Completions" key="noCompletions"><span highlight="Title">{_("completion.noCompletions")}</span></div>
<div key="completions"/>
<div highlight="Completions">
{
template.map(util.range(0, options["maxitems"] * 2), function (i)
<div highlight="CompItem NonText">
<li>~</li>
</div>)
}
</div>
</div>, this._divNodes);
this._doc.body.replaceChild(this._div, this._doc.body.firstChild);
util.scrollIntoView(this._div, true);
this._items.contextList.forEach(function init_eachContext(context) {
delete context.cache.nodes;
if (!context.items.length && !context.message && !context.incomplete)
return;
context.cache.nodes = [];
this._dom(<div key="root" highlight="CompGroup">
<div highlight="Completions">
{ context.createRow(context.title || [], "CompTitle") }
</div>
<div highlight="CompTitleSep"/>
<div key="message" highlight="CompMsg"/>
<div key="up" highlight="CompLess"/>
<div key="items" highlight="Completions"/>
<div key="waiting" highlight="CompMsg">{ItemList.WAITING_MESSAGE}</div>
<div key="down" highlight="CompMore"/>
</div>, context.cache.nodes);
this._divNodes.completions.appendChild(context.cache.nodes.root);
}, this);
this.timeout(this._autoSize);
},
/**
* Uses the entries in "items" to fill the listbox and does incremental
* filling to speed up things.
*
* @param {number} offset Start at this index and show options["maxitems"].
*/
_fill: function _fill(offset) {
XML.ignoreWhiteSpace = false;
let diff = offset - this._startIndex;
if (this._items == null || offset == null || diff == 0 || offset < 0)
return false;
this._startIndex = offset;
this._endIndex = Math.min(this._startIndex + options["maxitems"], this._items.allItems.items.length);
let haveCompletions = false;
let off = 0;
let end = this._startIndex + options["maxitems"];
function getRows(context) {
function fix(n) Math.constrain(n, 0, len);
let len = context.items.length;
let start = off;
end -= !!context.message + context.incomplete;
off += len;
let s = fix(offset - start), e = fix(end - start);
return [s, e, context.incomplete && e >= offset && off - 1 < end];
}
this._items.contextList.forEach(function fill_eachContext(context) {
let nodes = context.cache.nodes;
if (!nodes)
return;
haveCompletions = true;
let root = nodes.root;
let items = nodes.items;
let [start, end, waiting] = getRows(context);
if (context.message) {
nodes.message.textContent = "";
nodes.message.appendChild(this._dom(context.message));
}
nodes.message.style.display = context.message ? "block" : "none";
nodes.waiting.style.display = waiting ? "block" : "none";
nodes.up.style.opacity = "0";
nodes.down.style.display = "none";
for (let [i, row] in Iterator(context.getRows(start, end, this._doc)))
nodes[i] = row;
for (let [i, row] in array.iterItems(nodes)) {
if (!row)
continue;
let display = (i >= start && i < end);
if (display && row.parentNode != items) {
do {
var next = nodes[++i];
if (next && next.parentNode != items)
next = null;
}
while (!next && i < end)
items.insertBefore(row, next);
}
else if (!display && row.parentNode == items)
items.removeChild(row);
}
if (context.items.length == 0)
return;
nodes.up.style.opacity = (start == 0) ? "0" : "1";
if (end != context.items.length)
nodes.down.style.display = "block";
else
nodes.up.style.display = "block";
if (start == end) {
nodes.up.style.display = "none";
nodes.down.style.display = "none";
}
}, this);
this._divNodes.noCompletions.style.display = haveCompletions ? "none" : "block";
this._completionElements = util.evaluateXPath("//xhtml:div[@dactyl:highlight='CompItem']", this._doc);
return true;
},
clear: function clear() { this.setItems(); this._doc.body.innerHTML = ""; },
get visible() !this._container.collapsed,
set visible(val) this._container.collapsed = !val,
reset: function reset(brief) {
this._startIndex = this._endIndex = this._selIndex = -1;
this._div = null;
if (!brief)
this.selectItem(-1);
},
// if @param selectedItem is given, show the list and select that item
setItems: function setItems(newItems, selectedItem) {
if (this._selItem > -1)
this._getCompletion(this._selItem).removeAttribute("selected");
if (this._container.collapsed) {
this._minHeight = 0;
this._container.height = 0;
}
this._startIndex = this._endIndex = this._selIndex = -1;
this._items = newItems;
this.reset(true);
if (typeof selectedItem == "number") {
this.selectItem(selectedItem);
this.visible = true;
}
},
// select index, refill list if necessary
selectItem: function selectItem(index) {
//let now = Date.now();
if (this._div == null)
this._init();
let sel = this._selIndex;
let len = this._items.allItems.items.length;
let newOffset = this._startIndex;
let maxItems = options["maxitems"];
let contextLines = Math.min(3, parseInt((maxItems - 1) / 2));
if (index == -1 || index == null || index == len) { // wrapped around
if (this._selIndex < 0)
newOffset = 0;
this._selIndex = -1;
index = -1;
}
else {
if (index <= this._startIndex + contextLines)
newOffset = index - contextLines;
if (index >= this._endIndex - contextLines)
newOffset = index + contextLines - maxItems + 1;
newOffset = Math.min(newOffset, len - maxItems);
newOffset = Math.max(newOffset, 0);
this._selIndex = index;
}
if (sel > -1)
this._getCompletion(sel).removeAttribute("selected");
this._fill(newOffset);
if (index >= 0) {
this._getCompletion(index).setAttribute("selected", "true");
if (this._container.height != 0)
util.scrollIntoView(this._getCompletion(index));
}
//if (index == 0)
// this.start = now;
//if (index == Math.min(len - 1, 100))
// util.dump({ time: Date.now() - this.start });
},
onKeyPress: function onKeyPress(event) false
}, {
WAITING_MESSAGE: _("completion.generating")
});
// vim: set fdm=marker sw=4 ts=4 et:
|