summaryrefslogtreecommitdiff
path: root/common/content/dactyl.js
blob: b8e70e7af78db9b657c4c04a8756fd7fdd60b12f (plain)
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
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
// Copyright (c) 2006-2008 by Martin Stubenschrott <stubenschrott@vimperator.org>
// Copyright (c) 2007-2009 by Doug Kearns <dougkearns@gmail.com>
// Copyright (c) 2008-2010 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 */

default xml namespace = XHTML;
XML.ignoreWhitespace = false;
XML.prettyPrinting = false;

var plugins = { __proto__: modules };
var userContext = newContext(modules);

var EVAL_ERROR = "__dactyl_eval_error";
var EVAL_RESULT = "__dactyl_eval_result";
var EVAL_STRING = "__dactyl_eval_string";

var Dactyl = Module("dactyl", XPCOM(Ci.nsISupportsWeakReference, ModuleBase), {
    init: function () {
        window.dactyl = this;
        // cheap attempt at compatibility
        let prop = { get: deprecated("Please use dactyl instead", function liberator() dactyl) };
        Object.defineProperty(window, "liberator", prop);
        Object.defineProperty(modules, "liberator", prop);
        this.commands = {};
        this.indices = {};
        this.modules = modules;
        this.observers = {};
        util.addObserver(this);

        this.commands["dactyl.help"] = function (event) {
            let elem = event.originalTarget;
            dactyl.help(elem.getAttribute("tag") || elem.textContent);
        };
    },

    cleanup: function () {
        delete window.dactyl;
        delete window.liberator;
    },

    destroy: function () {
        autocommands.trigger("LeavePre", {});
        storage.saveAll();
        dactyl.triggerObserver("shutdown", null);
        util.dump("All dactyl modules destroyed\n");
        autocommands.trigger("Leave", {});
    },

    observe: {
        "dactyl-cleanup": function () {
            let modules = dactyl.modules;

            for (let name in values(Object.getOwnPropertyNames(modules).reverse())) {
                let mod = Object.getOwnPropertyDescriptor(modules, name).value;
                if (mod instanceof ModuleBase) {
                    if ("cleanup" in mod)
                        mod.cleanup();
                    if ("destroy" in mod)
                        mod.destroy();
                }
            }

            for (let name in values(Object.getOwnPropertyNames(modules).reverse()))
                try {
                    delete modules[name];
                }
                catch (e) {}
        }
    },

    /** @property {string} The name of the current user profile. */
    profileName: Class.memoize(function () {
        // NOTE: services.profile.selectedProfile.name doesn't return
        // what you might expect. It returns the last _actively_ selected
        // profile (i.e. via the Profile Manager or -P option) rather than the
        // current profile. These will differ if the current process was run
        // without explicitly selecting a profile.

        let dir = services.directory.get("ProfD", Ci.nsIFile);
        for (let prof in iter(services.profile.profiles))
            if (prof.QueryInterface(Ci.nsIToolkitProfile).rootDir.path === dir.path)
                return prof.name;
        return "unknown";
    }),

    /**
     * @property {number} The current main mode.
     * @see modes#mainModes
     */
    mode: deprecated("Please use modes.main instead", {
        get: function mode() modes.main,
        set: function mode(val) modes.main = val
    }),

    get menuItems() Dactyl.getMenuItems(),

    // Global constants
    CURRENT_TAB: [],
    NEW_TAB: [],
    NEW_BACKGROUND_TAB: [],
    NEW_WINDOW: [],

    forceNewTab: false,
    forceNewWindow: false,

    version: deprecated("Please use config.version instead", { get: function version() config.version }),

    /**
     * @property {Object} The map of command-line options. These are
     *     specified in the argument to the host application's -{config.name}
     *     option. E.g. $ firefox -pentadactyl '+u=/tmp/rcfile ++noplugin'
     *     Supported options:
     *         +u RCFILE   Use RCFILE instead of .pentadactylrc.
     *         ++noplugin  Don't load plugins.
     *     These two can be specified multiple times:
     *         ++cmd CMD   Execute an Ex command before initialization.
     *         +c CMD      Execute an Ex command after initialization.
     */
    commandLineOptions: {
        /** @property Whether plugin loading should be prevented. */
        noPlugins: false,
        /** @property An RC file to use rather than the default. */
        rcFile: null,
        /** @property An Ex command to run before any initialization is performed. */
        preCommands: null,
        /** @property An Ex command to run after all initialization has been performed. */
        postCommands: null
    },

    registerObserver: function (type, callback, weak) {
        if (!(type in this.observers))
            this.observers[type] = [];
        this.observers[type].push(weak ? Cu.getWeakReference(callback) : { get: function () callback });
    },

    unregisterObserver: function (type, callback) {
        if (type in this.observers)
            this.observers[type] = this.observers[type].filter(function (c) c.get() != callback);
    },

    // TODO: "zoom": if the zoom value of the current buffer changed
    triggerObserver: function (type) {
        let args = Array.slice(arguments, 1);
        if (type in this.observers)
            this.observers[type] = this.observers[type].filter(function (callback) {
                if (callback.get()) {
                    callback.get().apply(null, args);
                    return true;
                }
            });
    },

    addUsageCommand: function (params) {
        commands.add(params.name, params.description,
            function (args) {
                let results = array(params.iterate(args))
                    .sort(function (a, b) String.localeCompare(a.name, b.name));

                let filters = args.map(function (arg) RegExp("\\b" + util.regexp.escape(arg) + "\\b", "i"));
                if (filters.length)
                    results = results.filter(function (item) filters.every(function (re) re.test(item.name + item.description)));

                commandline.commandOutput(
                    template.usage(results, params.format));
            },
            {
                argCount: "*",
                completer: function (context, args) {
                    context.keys.text = util.identity;
                    context.keys.description = function () seen[this.text] + " matching items";
                    let seen = {};
                    context.completions = array(item.description.toLowerCase().split(/[()\s]+/)
                                                for (item in params.iterate(args)))
                        .flatten().filter(function (w) /^\w[\w-_']+$/.test(w))
                        .map(function (k) {
                            seen[k] = (seen[k] || 0) + 1;
                            return k;
                        }).uniq();
                },
                options: params.options || []
            });
        if (params.index)
            this.indices[params.index] = function () {
                for (let obj in (params.iterateIndex || params.iterate)())
                    if (obj.helpTag in services["dactyl:"].HELP_TAGS)
                        yield dactyl.generateHelp(obj, null, null, true);
            }
    },

    /**
     * Triggers the application bell to notify the user of an error. The
     * bell may be either audible or visual depending on the value of the
     * 'visualbell' option.
     */
    beep: function () {
        if (options["visualbell"]) {
            let elems = {
                bell: document.getElementById("dactyl-bell"),
                strut: document.getElementById("dactyl-bell-strut")
            }
            if (!elems.bell)
                util.overlayWindow(window, {
                    objects: elems,
                    prepend: <>
                        <window id={document.documentElement.id} xmlns={XUL}>
                            <hbox style="display: none" highlight="Bell" id="dactyl-bell" key="bell"/>
                        </window>
                    </>,
                    append: <>
                        <window id={document.documentElement.id} xmlns={XUL}>
                            <hbox style="display: none" highlight="Bell" id="dactyl-bell-strut" key="strut"/>
                        </window>
                    </>
                }, elems);

            elems.bell.style.height = window.innerHeight + "px";
            elems.strut.style.marginBottom = -window.innerHeight + "px";
            elems.strut.style.display = elems.bell.style.display = "";

            util.timeout(function () { elems.strut.style.display = elems.bell.style.display = "none"; }, 20);
        }
        else {
            let soundService = Cc["@mozilla.org/sound;1"].getService(Ci.nsISound);
            soundService.beep();
        }
    },

    /**
     * Reads a string from the system clipboard.
     *
     * This is same as Firefox's readFromClipboard function, but is needed for
     * apps like Thunderbird which do not provide it.
     *
     * @returns {string}
     */
    clipboardRead: function clipboardRead(getClipboard) {
        let str = null;

        try {
            const clipboard = Cc["@mozilla.org/widget/clipboard;1"].getService(Ci.nsIClipboard);
            const transferable = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);

            transferable.addDataFlavor("text/unicode");

            if (!getClipboard && clipboard.supportsSelectionClipboard())
                clipboard.getData(transferable, clipboard.kSelectionClipboard);
            else
                clipboard.getData(transferable, clipboard.kGlobalClipboard);

            let data = {};
            let dataLen = {};

            transferable.getTransferData("text/unicode", data, dataLen);

            if (data) {
                data = data.value.QueryInterface(Ci.nsISupportsString);
                str = data.data.substring(0, dataLen.value / 2);
            }
        }
        catch (e) {}
        return str;
    },

    /**
     * Copies a string to the system clipboard. If *verbose* is specified the
     * copied string is also echoed to the command line.
     *
     * @param {string} str
     * @param {boolean} verbose
     */
    clipboardWrite: function clipboardWrite(str, verbose) {
        const clipboardHelper = Cc["@mozilla.org/widget/clipboardhelper;1"].getService(Ci.nsIClipboardHelper);
        clipboardHelper.copyString(str);

        if (verbose) {
            let message = { message: "Yanked " + str };
            try {
                message.domains = [util.newURI(str).host];
            }
            catch (e) {};
            dactyl.echomsg(message);
        }
    },

    dump: deprecated("Use util.dump instead",
                     { get: function dump() util.closure.dump }),
    dumpStack: deprecated("Use util.dumpStack instead",
                          { get: function dumpStack() util.closure.dumpStack }),

    /**
     * Outputs a plain message to the command line.
     *
     * @param {string} str The message to output.
     * @param {number} flags These control the multi-line message behavior.
     *     See {@link CommandLine#echo}.
     */
    echo: function echo(str, flags) {
        commandline.echo(str, commandline.HL_NORMAL, flags);
    },

    // TODO: Vim replaces unprintable characters in echoerr/echomsg
    /**
     * Outputs an error message to the command line.
     *
     * @param {string} str The message to output.
     * @param {number} flags These control the multi-line message behavior.
     *     See {@link CommandLine#echo}.
     */
    echoerr: function echoerr(str, flags) {
        flags |= commandline.APPEND_TO_MESSAGES;

        if (isinstance(str, ["Error", "Exception"]))
            dactyl.reportError(str);
        if (typeof str == "object" && "echoerr" in str)
            str = str.echoerr;
        else if (isinstance(str, ["Error"]))
            str = <>{str.fileName.replace(/^.*? -> /, "")}: {str.lineNumber}: {str}</>;

        if (options["errorbells"])
            dactyl.beep();

        commandline.echo(str, commandline.HL_ERRORMSG, flags);
    },

    // TODO: add proper level constants
    /**
     * Outputs an information message to the command line.
     *
     * @param {string} str The message to output.
     * @param {number} verbosity The messages log level (0 - 15). Only
     *     messages with verbosity less than or equal to the value of the
     *     *verbosity* option will be output.
     * @param {number} flags These control the multi-line message behavior.
     *     See {@link CommandLine#echo}.
     */
    echomsg: function (str, verbosity, flags) {
        flags |= commandline.APPEND_TO_MESSAGES;

        if (verbosity == null)
            verbosity = 0; // verbosity level is exclusionary

        if (options["verbose"] >= verbosity)
            commandline.echo(str, commandline.HL_INFOMSG, flags);
    },

    /**
     * Loads and executes the script referenced by *uri* in the scope of the
     * *context* object.
     *
     * @param {string} uri The URI of the script to load. Should be a local
     *     chrome:, file:, or resource: URL.
     * @param {Object} context The context object into which the script
     *     should be loaded.
     */
    loadScript: function (uri, context) {
        services.subscriptLoader.loadSubScript(uri, context, File.defaultEncoding);
    },

    userEval: function (str, context, fileName, lineNumber) {
        if (fileName == null)
            if (io.sourcing)
                ({ file: fileName, line: lineNumber }) = io.sourcing;
            else if (String.indexOf(commandline.command, str) > -1)
                [fileName, lineNumber] = ["[Command Line]", 1];

        if (!context)
            context = userContext;
        if (window.isPrototypeOf(modules))
            return Cu.evalInSandbox(str, context, "1.8", fileName, lineNumber);
        return Cu.evalInSandbox("with (window) { with (modules) { this.eval(" + str.quote() + ") } }", context, "1.8", fileName, lineNumber);
    },

    /**
     * Acts like the Function builtin, but the code executes in the
     * userContext global.
     */
    userFunc: function () {
        return this.userEval(
            "(function userFunction(" + Array.slice(arguments, 0, -1).join(", ") + ")" +
            " { " + arguments[arguments.length - 1] + " })");
    },

    /**
     * Execute an Ex command string. E.g. ":zoom 300".
     *
     * @param {string} str The command to execute.
     * @param {Object} modifiers Any modifiers to be passed to
     *     {@link Command#action}.
     * @param {boolean} silent Whether the command should be echoed on the
     *     command line.
     */
    execute: function (str, modifiers, silent) {
        // skip comments and blank lines
        if (/^\s*("|$)/.test(str))
            return;

        modifiers = modifiers || {};

        if (!silent)
            commandline.command = str.replace(/^\s*:\s*/, "");
        let res = true;
        for (let [command, args] in commands.parseCommands(str.replace(/^'(.*)'$/, "$1"))) {
            if (command === null)
                throw FailedAssertion("E492: Not a " + config.appName + " command: " + args.commandString);

            res = res && command.execute(args, modifiers);
        }
        return res;
    },

    focus: function focus(elem, flags) {
        flags = flags || services.focus.FLAG_BYMOUSE;
        try {
            if (elem instanceof Document)
                elem = elem.defaultView;
            if (elem instanceof Window)
                services.focus.focusedWindow = elem;
            else
                services.focus.setFocus(elem, flags);
        }
        catch (e) {
            util.dump(elem);
            util.reportError(e);
        }
    },

    /**
     * Focuses the content window.
     *
     * @param {boolean} clearFocusedElement Remove focus from any focused
     *     element.
     */
    focusContent: function focusContent(clearFocusedElement) {
        if (window != services.focus.activeWindow)
            return;

        let win = document.commandDispatcher.focusedWindow;
        let elem = config.mainWidget || content;
        // TODO: make more generic
        try {
            if (this.has("mail") && !config.isComposeWindow) {
                let i = gDBView.selection.currentIndex;
                if (i == -1 && gDBView.rowCount >= 0)
                    i = 0;
                gDBView.selection.select(i);
            }
            else {
                let frame = buffer.focusedFrame;
                if (frame && frame.top == content && !Editor.getEditor(frame))
                    elem = frame;
            }
        }
        catch (e) {}

        if (clearFocusedElement) {
            if (dactyl.focusedElement)
                dactyl.focusedElement.blur();
            if (win && Editor.getEditor(win)) {
                win.blur();
                if (win.frameElement)
                    util.dump("blur(" + util.objectToString(win.frameElement) + ")");
                if (win.frameElement)
                    win.frameElement.blur();
            }
        }

        if (elem instanceof Window && Editor.getEditor(elem))
            elem = window;

        if (elem && elem != dactyl.focusedElement)
            dactyl.focus(elem);
     },

    /** @property {Element} The currently focused element. */
    get focusedElement() services.focus.getFocusedElementForWindow(window, true, {}),
    set focusedElement(elem) dactyl.focus(elem),

    /**
     * Returns whether this Dactyl extension supports *feature*.
     *
     * @param {string} feature The feature name.
     * @returns {boolean}
     */
    has: function (feature) config.features.indexOf(feature) >= 0,

    /**
     * Returns the URL of the specified help *topic* if it exists.
     *
     * @param {string} topic The help topic to lookup.
     * @param {boolean} unchunked Whether to search the unchunked help page.
     * @returns {string}
     */
    findHelp: function (topic, unchunked) {
        if (!unchunked && topic in services["dactyl:"].FILE_MAP)
            return topic;
        unchunked = !!unchunked;
        let items = completion._runCompleter("help", topic, null, unchunked).items;
        let partialMatch = null;

        function format(item) item.description + "#" + encodeURIComponent(item.text);

        for (let [i, item] in Iterator(items)) {
            if (item.text == topic)
                return format(item);
            else if (!partialMatch && topic)
                partialMatch = item;
        }

        if (partialMatch)
            return format(partialMatch);
        return null;
    },

    /**
     * @private
     */
    initDocument: function initDocument(doc) {
        if (doc.location.protocol === "dactyl:") {
            dactyl.initHelp();
            config.styleHelp();
        }
    },

    /**
     * @private
     * Initialize the help system.
     */
    initHelp: function (force) {
        if (!force && !this.helpInitialized) {
            if ("noscriptOverlay" in window) {
                noscriptOverlay.safeAllow("chrome-data:", true, false);
                noscriptOverlay.safeAllow("dactyl:", true, false);
            }

            let namespaces = ["locale-local", "locale"];
            services["dactyl:"].init({});

            let tagMap = services["dactyl:"].HELP_TAGS;
            let fileMap = services["dactyl:"].FILE_MAP;
            let overlayMap = services["dactyl:"].OVERLAY_MAP;

            // Find help and overlay files with the given name.
            function findHelpFile(file) {
                let result = [];
                for (let [, namespace] in Iterator(namespaces)) {
                    let url = ["dactyl://", namespace, "/", file, ".xml"].join("");
                    let res = util.httpGet(url);
                    if (res) {
                        if (res.responseXML.documentElement.localName == "document")
                            fileMap[file] = url;
                        if (res.responseXML.documentElement.localName == "overlay")
                            overlayMap[file] = url;
                        result.push(res.responseXML);
                    }
                }
                return result;
            }
            // Find the tags in the document.
            function addTags(file, doc) {
                for (let elem in util.evaluateXPath("//@tag|//dactyl:tags/text()|//dactyl:tag/text()", doc))
                    for (let tag in values((elem.value || elem.textContent).split(/\s+/)))
                        tagMap[tag] = file;
            }

            // Scrape the list of help files from all.xml
            // Manually process main and overlay files, since XSLTProcessor and
            // XMLHttpRequest don't allow access to chrome documents.
            tagMap["all"] = tagMap["all.xml"] = "all";
            let files = findHelpFile("all").map(function (doc)
                    [f.value for (f in util.evaluateXPath("//dactyl:include/@href", doc))]);

            // Scrape the tags from the rest of the help files.
            array.flatten(files).forEach(function (file) {
                tagMap[file + ".xml"] = file;
                findHelpFile(file).forEach(function (doc) {
                    addTags(file, doc);
                });
            });

            // Process plugin help entries.
            XML.ignoreWhiteSpace = false;
            XML.prettyPrinting = false;

            let body = XML();
            for (let [, context] in Iterator(plugins.contexts))
                if (context && context.INFO instanceof XML) {
                    let info = context.INFO;
                    if (info.*.@lang.length()) {
                        let langs = set(String(a) for each (a in info.*.@lang));
                        let lang = [window.navigator.language,
                                    window.navigator.language.replace(/-.*/, ""),
                                    "en", "en-US", info.*.@lang[0]
                                   ].filter(function (l) set.has(langs, l))[0];

                        info.* = info.*.(function::attribute("lang").length() == 0 || @lang == lang);

                        for each (let elem in info.NS::info)
                            for each (let attr in ["@name", "@summary", "@href"])
                                if (elem[attr].length())
                                    info[attr] = elem[attr];
                    }
                    body += <h2 xmlns={NS.uri} tag={context.INFO.@name + '-plugin'}>{context.INFO.@summary}</h2> +
                        context.INFO;
                }

            let help =
                '<?xml version="1.0"?>\n' +
                '<?xml-stylesheet type="text/xsl" href="chrome://dactyl/content/help.xsl"?>\n' +
                '<!DOCTYPE document SYSTEM "chrome://dactyl/content/dactyl.dtd">\n' +
                unescape(encodeURI( // UTF-8 handling hack.
                <document xmlns={NS}
                    name="plugins" title={config.appName + " Plugins"}>
                    <h1 tag="using-plugins">Using Plugins</h1>
                    <toc start="2"/>

                    {body}
                </document>.toXMLString()));
            fileMap["plugins"] = function () ['text/xml;charset=UTF-8', help];

            default xml namespace = NS;

            overlayMap["index"] = function () ['text/xml;charset=UTF-8',
                '<?xml version="1.0"?>\n' +
                '<overlay xmlns="' + NS + '">\n' +
                unescape(encodeURI( // UTF-8 handling hack.
                template.map(dactyl.indices, function ([name, iter])
                    <dl insertafter={name + "-index"}>{
                        template.map(iter(), util.identity)
                    }</dl>, <>{"\n\n"}</>))) +
                '\n</overlay>'];

            addTags("plugins", util.httpGet("dactyl://help/plugins").responseXML);
            this.helpInitialized = true;
        }
    },

    stringifyXML: function (xml) {
        XML.prettyPrinting = false;
        XML.ignoreWhitespace = false;
        return UTF8(xml.toXMLString());
    },

    exportHelp: JavaScript.setCompleter(function (path) {
        const FILE = io.File(path);
        const PATH = FILE.leafName.replace(/\..*/, "") + "/";
        const TIME = Date.now();

        if (!FILE.exists() && (/\/$/.test(path) && !/\./.test(FILE.leafName)))
            FILE.create(FILE.DIRECTORY_TYPE, octal(755));

        dactyl.initHelp();
        if (FILE.isDirectory()) {
            function addDataEntry(file, data) FILE.child(file).write(data);
            function addURIEntry(file, uri) addDataEntry(file, util.httpGet(uri).responseText);
        }
        else {
            var zip = services.ZipWriter();
            zip.open(FILE, File.MODE_CREATE | File.MODE_WRONLY | File.MODE_TRUNCATE);
            function addURIEntry(file, uri)
                zip.addEntryChannel(PATH + file, TIME, 9,
                    services.io.newChannel(uri, null, null), false);
            function addDataEntry(file, data) // Unideal to an extreme.
                addURIEntry(file, "data:text/plain;charset=UTF-8," + encodeURI(data));
        }

        let empty = set("area base basefont br col frame hr img input isindex link meta param"
                            .split(" "));

        let chromeFiles = {};
        let styles = {};
        for (let [file, ] in Iterator(services["dactyl:"].FILE_MAP)) {
            dactyl.open("dactyl://help/" + file);
            dactyl.modules.events.waitForPageLoad();
            let data = [
                '<?xml version="1.0" encoding="UTF-8"?>\n',
                '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"\n',
                '          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n'
            ];
            function fix(node) {
                switch(node.nodeType) {
                    case Node.ELEMENT_NODE:
                        if (isinstance(node, [HTMLBaseElement]))
                            return;

                        data.push("<"); data.push(node.localName);
                        if (node instanceof HTMLHtmlElement)
                            data.push(" xmlns=" + XHTML.uri.quote());

                        for (let { name, value } in array.iterValues(node.attributes)) {
                            if (name == "dactyl:highlight") {
                                set.add(styles, value);
                                name = "class";
                                value = "hl-" + value;
                            }
                            if (name == "href") {
                                value = node.href;
                                if (value.indexOf("dactyl://help-tag/") == 0) {
                                    let uri = services.io.newChannel(value, null, null).originalURI;
                                    value = uri.spec == value ? "javascript:;" : uri.path.substr(1);
                                }
                                if (!/^#|[\/](#|$)|^[a-z]+:/.test(value))
                                    value = value.replace(/(#|$)/, ".xhtml$1");
                            }
                            if (name == "src" && value.indexOf(":") > 0) {
                                chromeFiles[value] = value.replace(/.*\//, "");
                                value = value.replace(/.*\//, "");
                            }
                            data.push(" ");
                            data.push(name);
                            data.push('="');
                            data.push(<>{value}</>.toXMLString());
                            data.push('"');
                        }
                        if (node.localName in empty)
                            data.push(" />");
                        else {
                            data.push(">");
                            if (node instanceof HTMLHeadElement)
                                data.push(<link rel="stylesheet" type="text/css" href="help.css"/>.toXMLString());
                            Array.map(node.childNodes, fix);
                            data.push("</"); data.push(node.localName); data.push(">");
                        }
                        break;
                    case Node.TEXT_NODE:
                        data.push(<>{node.textContent}</>.toXMLString());
                }
            }
            fix(content.document.documentElement);
            addDataEntry(file + ".xhtml", data.join(""));
        }

        let data = [h for (h in highlight) if (set.has(styles, h.class) || /^Help/.test(h.class))]
            .map(function (h) h.selector
                               .replace(/^\[.*?=(.*?)\]/, ".hl-$1")
                               .replace(/html\|/g, "") + "\t" + "{" + h.cssText + "}")
            .join("\n");
        addDataEntry("help.css", data.replace(/chrome:[^ ")]+\//g, ""));

        addDataEntry("tag-map.json", JSON.stringify(services["dactyl:"].HELP_TAGS));

        let m, re = /(chrome:[^ ");]+\/)([^ ");]+)/g;
        while ((m = re.exec(data)))
            chromeFiles[m[0]] = m[2];

        for (let [uri, leaf] in Iterator(chromeFiles))
            addURIEntry(leaf, uri);

        if (zip)
            zip.close();
    }, [function (context, args) completion.file(context)]),

    /**
     * Generates a help entry and writes it to the clipboard.
     *
     * @param {Command|Map|Option} obj A dactyl *Command*, *Map* or *Option*
     *     object
     * @param {XMLList} extraHelp Extra help text beyond the description.
     * @returns {string}
     */
    generateHelp: function generateHelp(obj, extraHelp, str, specOnly) {
        default xml namespace = "";

        let link, tag, spec;
        link = tag = spec = util.identity;
        let args = null;

        if (obj instanceof Command) {
            tag = spec = function (cmd) <>:{cmd}</>;
            link = function (cmd) <ex>:{cmd}</ex>;
            args = obj.parseArgs("", CompletionContext(str || ""));
            spec = function (cmd) cmd + (obj.bang ? <oa>!</oa> : <></>);
        }
        else if (obj instanceof Map && obj.count) {
            spec = function (map) <><oa>count</oa>{map}</>;
            link = function (map) let (res = /^<(.*?)>(.*?)/.exec(map))
                res ? <k name={res[1]}>{res[2]}</k> : <k>{map}</k>;
        }
        else if (obj instanceof Option) {
            tag = spec = function (opt) <>'{opt}'</>;
            link = function (opt) <o>{opt}</o>;
        }

        XML.prettyPrinting = false;
        XML.ignoreWhitespace = false;
        default xml namespace = NS;

        // E4X has its warts.
        let br = <>
                    </>;

        let res = <res>
                <dt>{link(obj.name)}</dt> <dd>{
                    template.linkifyHelp(obj.description ? obj.description.replace(/\.$/, "") : "", true)
                }</dd></res>;
        if (specOnly)
            return res.*;

        res.* += <>
            <item>
                <tags>{template.map(obj.names.slice().reverse(), tag, " ")}</tags>
                <spec>{
                    spec(template.highlightRegexp((obj.specs || obj.names)[0],
                                                  /\[(.*?)\]/g,
                                                  function (m, n0) <oa>{n0}</oa>))
                }</spec>{
                !obj.type ? "" : <>
                <type>{obj.type}</type>
                <default>{obj.stringDefaultValue}</default></>}
                <description>{
                    obj.description ? br + <p>{template.linkifyHelp(obj.description.replace(/\.?$/, "."), true)}</p> : "" }{
                        extraHelp ? br + extraHelp : "" }{
                        !(extraHelp || obj.description) ? br + <p>Sorry, no help available.</p> : "" }
                </description>
            </item></>;

        function add(ary) {
            res.item.description.* += br +
                let (br = br + <>    </>)
                    <><dl>{ br + template.map(ary, function ([a, b]) <><dt>{a}</dt> <dd>{b}</dd></>, br) }
                    </dl>
                </>;
        }

        if (obj.completer)
            add(completion._runCompleter(obj.completer, "", null, args).items
                          .map(function (i) [i.text, i.description]));

        if (obj.options && obj.options.some(function (o) o.description))
            add(obj.options.filter(function (o) o.description)
                   .map(function (o) [
                        o.names[0],
                        <>{o.description}{
                            o.names.length == 1 ? "" :
                                <> (short name: {
                                    template.map(o.names.slice(1), function (n) <em>{n}</em>, <>, </>)
                                })</>
                        }</>
                    ]));
        return res.*.toXMLString()
                  .replace(' xmlns="' + NS + '"', "", "g")
                  .replace(/^ {12}|[ \t]+$/gm, "")
                  .replace(/^\s*\n|\n\s*$/g, "") + "\n";
    },

    /**
     * Opens the help page containing the specified *topic* if it exists.
     *
     * @param {string} topic The help topic to open.
     * @param {boolean} unchunked Whether to use the unchunked help page.
     * @returns {string}
     */
    help: function (topic, unchunked) {
        dactyl.initHelp();
        if (!topic) {
            let helpFile = unchunked ? "all" : options["helpfile"];

            if (helpFile in services["dactyl:"].FILE_MAP)
                dactyl.open("dactyl://help/" + helpFile, { from: "help" });
            else
                dactyl.echomsg("Sorry, help file " + helpFile.quote() + " not found");
            return;
        }

        let page = this.findHelp(topic, unchunked);
        dactyl.assert(page != null, "E149: Sorry, no help for " + topic);

        dactyl.open("dactyl://help/" + page, { from: "help" });
    },

    /**
     * The map of global variables.
     *
     * These are set and accessed with the "g:" prefix.
     */
    _globalVariables: {},
    globalVariables: deprecated("Please use the options system instead", {
        get: function globalVariables() this._globalVariables
    }),

    loadPlugins: function (args) {
        function sourceDirectory(dir) {
            dactyl.assert(dir.isReadable(), "E484: Can't open file " + dir.path);

            dactyl.log("Sourcing plugin directory: " + dir.path + "...", 3);

            let loadplugins = options.get("loadplugins");
            if (args)
                loadplugins = { __proto__: loadplugins, value: args.map(Option.parseRegexp) }

            dir.readDirectory(true).forEach(function (file) {
                if (file.isFile() && loadplugins.getKey(file.path) && !(file.path in dactyl.pluginFiles)) {
                    try {
                        io.source(file.path, false);
                        dactyl.pluginFiles[file.path] = true;
                    }
                    catch (e) {
                        dactyl.reportError(e);
                    }
                }
                else if (file.isDirectory())
                    sourceDirectory(file);
            });
        }

        let dirs = io.getRuntimeDirectories("plugins");

        if (dirs.length == 0) {
            dactyl.log("No user plugin directory found", 3);
            return;
        }

        dactyl.echomsg('Searching for "plugins/**/*.{js,' + config.fileExtension + '}" in '
                            + [dir.path.replace(/.plugins$/, "") for ([, dir] in Iterator(dirs))]
                                .join(",").quote(), 2);

        dirs.forEach(function (dir) {
            dactyl.echomsg("Searching for " + (dir.path + "/**/*.{js," + config.fileExtension + "}").quote(), 3);
            sourceDirectory(dir);
        });
    },

    // TODO: add proper level constants
    /**
     * Logs a message to the JavaScript error console. Each message has an
     * associated log level. Only messages with a log level less than or equal
     * to *level* will be printed. If *msg* is an object, it is pretty printed.
     *
     * @param {string|Object} msg The message to print.
     * @param {number} level The logging level 0 - 15.
     */
    log: function (msg, level) {
        let verbose = prefs.get("extensions.dactyl.loglevel", 0);

        if (!level || level <= verbose) {
            if (isObject(msg))
                msg = util.objectToString(msg, false);

            services.console.logStringMessage(config.name + ": " + msg);
        }
    },

    /**
     * Opens one or more URLs. Returns true when load was initiated, or
     * false on error.
     *
     * @param {string|Array} urls A representation of the URLs to open. May be
     *     either a string, which will be passed to
     *     {@see Dactyl#parseURLs}, or an array in the same format as
     *     would be returned by the same.
     * @param {object} params A set of parameters specifying how to open the
     *     URLs. The following properties are recognized:
     *
     *      • background   If true, new tabs are opened in the background.
     *
     *      • from         The designation of the opener, as appears in
     *                     'activate' and 'newtab' options. If present,
     *                     the newtab option provides the default 'where'
     *                     parameter, and the value of the 'activate'
     *                     parameter is inverted if 'background' is true.
     *
     *      • where        One of CURRENT_TAB, NEW_TAB, or NEW_WINDOW
     *
     *      As a deprecated special case, the where parameter may be provided
     *      by itself, in which case it is transformed into { where: params }.
     *
     * @param {boolean} force Don't prompt whether to open more than 20
     *     tabs.
     * @returns {boolean}
     */
    open: function (urls, params, force) {
        if (typeof urls == "string")
            urls = dactyl.parseURLs(urls);

        if (urls.length > 20 && !force)
            return commandline.input("This will open " + urls.length + " new tabs. Would you like to continue? (yes/[no]) ",
                function (resp) {
                    if (resp && resp.match(/^y(es)?$/i))
                        dactyl.open(urls, params, true);
                });

        params = params || {};
        if (isArray(params))
            params = { where: params };

        let flags = 0;
        for (let [opt, flag] in Iterator({ replace: "REPLACE_HISTORY", hide: "BYPASS_HISTORY" }))
            flags |= params[opt] && Ci.nsIWebNavigation["LOAD_FLAGS_" + flag];

        let where = params.where || dactyl.CURRENT_TAB;
        let background = ("background" in params) ? params.background
                                                  : params.where == dactyl.NEW_BACKGROUND_TAB;

        if (params.from && dactyl.has("tabs")) {
            if (!params.where && options.get("newtab").has(params.from))
                where = dactyl.NEW_TAB;
            background ^= !options.get("activate").has(params.from);
        }

        if (urls.length == 0)
            return;

        let browser = config.browser;
        function open(urls, where) {
            try {
                let url = Array.concat(urls)[0];
                let postdata = Array.concat(urls)[1];

                // decide where to load the first url
                switch (where) {
                case dactyl.CURRENT_TAB:
                    browser.loadURIWithFlags(url, flags, null, null, postdata);
                    return browser.contentWindow;

                case dactyl.NEW_TAB:
                    if (!dactyl.has("tabs"))
                        return open(urls, dactyl.NEW_WINDOW);

                    return prefs.withContext(function () {
                        prefs.set("browser.tabs.loadInBackground", true);
                        return browser.loadOneTab(url, null, null, postdata, background).linkedBrowser.contentDocument;
                    });

                case dactyl.NEW_WINDOW:
                    let win = window.openDialog(document.documentURI, "_blank", "chrome,all,dialog=no");
                    win.loadURI(url, null, postdata);
                    browser = win.getBrowser();
                    return win.content;
                }
            }
            catch (e) {}
            // Unfortunately, failed page loads throw exceptions and
            // cause a lot of unwanted noise. This solution means that
            // any genuine errors go unreported.
        }

        if (dactyl.forceNewTab)
            where = dactyl.NEW_TAB;
        else if (dactyl.forceNewWindow)
            where = dactyl.NEW_WINDOW;
        else if (!where)
            where = dactyl.CURRENT_TAB;

        return urls.map(function (url) {
            let res = open(url, where);
            where = dactyl.NEW_TAB;
            background = true;
            return res;
        });
    },

    pluginFiles: {},

    get plugins() plugins,

    /**
     * Quit the host application, no matter how many tabs/windows are open.
     *
     * @param {boolean} saveSession If true the current session will be
     *     saved and restored when the host application is restarted.
     * @param {boolean} force Forcibly quit irrespective of whether all
     *    windows could be closed individually.
     */
    quit: function (saveSession, force) {
        if (!force &&
            prefs.withContext(function () {
                prefs.set("browser.warnOnQuit", false);
                return !canQuitApplication();
            }))
            return;

        let pref = "browser.startup.page";
        prefs.save(pref);
        if (saveSession)
            prefs.safeSet(pref, 3);
        if (!saveSession && prefs.get(pref) >= 2)
            prefs.safeSet(pref, 1);

        services.appStartup.quit(Ci.nsIAppStartup[force ? "eForceQuit" : "eAttemptQuit"]);
    },

    /**
     * Returns an array of URLs parsed from *str*.
     *
     * Given a string like 'google bla, www.osnews.com' return an array
     * ['www.google.com/search?q=bla', 'www.osnews.com']
     *
     * @param {string} str
     * @returns {string[]}
     */
    stringToURLArray: deprecated("Please use dactyl.parseURLs instead", "parseURLs"),
    parseURLs: function parseURLs(str) {
        let urls;

        if (options["urlseparator"])
            urls = util.splitLiteral(str, RegExp("\\s*" + options["urlseparator"] + "\\s*"));
        else
            urls = [str];

        return urls.map(function (url) {
            if (/^(\.{0,2}|~)(\/|$)/.test(url)) {
                try {
                    // Try to find a matching file.
                    let file = io.File(url);
                    if (file.exists() && file.isReadable())
                        return services.io.newFileURI(file).spec;
                }
                catch (e) {}
            }

            // strip each 'URL' - makes things simpler later on
            url = url.trim();

            // Look for a valid protocol
            let proto = url.match(/^([-\w]+):/);
            if (proto && Cc["@mozilla.org/network/protocol;1?name=" + proto[1]])
                // Handle as URL, but remove spaces. Useful for copied/'p'asted URLs.
                return url.replace(/\s*\n+\s*/g, "");

            // Ok, not a valid proto. If it looks like URL-ish (foo.com/bar),
            // let Gecko figure it out.
            if (/^[a-zA-Z0-9-.]+(?:\/|$)/.test(url) && /[.\/]/.test(url) && !/\s/.test(url) || /^[a-zA-Z0-9-.]+:\d+(?:\/|$)/.test(url))
                return url;

            // TODO: it would be clearer if the appropriate call to
            // getSearchURL was made based on whether or not the first word was
            // indeed an SE alias rather than seeing if getSearchURL can
            // process the call usefully and trying again if it fails

            // check for a search engine match in the string, then try to
            // search for the whole string in the default engine
            let searchURL = bookmarks.getSearchURL(url, false) || bookmarks.getSearchURL(url, true);
            if (searchURL)
                return searchURL;

            // Hmm. No defsearch? Let the host app deal with it, then.
            return url;
        });
    },

    get assert() util.assert,

    /**
     * Traps errors in the called function, possibly reporting them.
     *
     * @param {function} func The function to call
     * @param {object} self The 'this' object for the function.
     */
    trapErrors: function trapErrors(func, self) {
        try {
            return func.apply(self || this, Array.slice(arguments, 2));
        }
        catch (e) {
            dactyl.reportError(e, true);
            return e;
        }
    },

    /**
     * Reports an error to both the console and the host application's
     * Error Console.
     *
     * @param {Object} error The error object.
     */
    reportError: function reportError(error, echo) {
        if (error instanceof FailedAssertion || error.message === "Interrupted") {
            if (error.message)
                dactyl.echoerr(template.linkifyHelp(error.message));
            else
                dactyl.beep();
            return;
        }
        if (error.result == Cr.NS_BINDING_ABORTED)
            return;
        if (echo)
            dactyl.echoerr(error);
        util.reportError(error);
    },

    /**
     * Restart the host application.
     */
    restart: function () {
        if (!canQuitApplication())
            return;

        services.appStartup.quit(Ci.nsIAppStartup.eAttemptQuit | Ci.nsIAppStartup.eRestart);
    },

    /**
     * Parses a Dactyl command-line string i.e. the value of the
     * -dactyl command-line option.
     *
     * @param {string} cmdline The string to parse for command-line
     *     options.
     * @returns {Object}
     * @see Commands#parseArgs
     */
    parseCommandLine: function (cmdline) {
        try {
            return commands.get("rehash").parseArgs(cmdline);
        }
        catch (e) {
            dactyl.reportError(e, true);
            return [];
        }
    },

    wrapCallback: function (callback, self) {
        self = self || this;
        let save = ["forceNewTab", "forceNewWindow"];
        let saved = save.map(function (p) dactyl[p]);
        return function wrappedCallback() {
            let args = arguments;
            return dactyl.withSavedValues(save, function () {
                saved.forEach(function (p, i) dactyl[save[i]] = p);
                try {
                    return callback.apply(self, args);
                }
                catch (e) {
                    dactyl.reportError(e, true);
                }
            });
        }
    },

    /**
     * @property {Window[]} Returns an array of all the host application's
     *     open windows.
     */
    get windows() [win for (win in iter(services.windowMediator.getEnumerator("navigator:browser")))],

}, {
    // initially hide all GUI elements, they are later restored unless the user
    // has :set go= or something similar in his config
    hideGUI: function () {
        let guioptions = config.guioptions;
        for (let option in guioptions) {
            guioptions[option].forEach(function (elem) {
                try {
                    document.getElementById(elem).collapsed = true;
                }
                catch (e) {}
            });
        }
    },

    // TODO: move this
    getMenuItems: function () {
        function addChildren(node, parent) {
            for (let [, item] in Iterator(node.childNodes)) {
                if (item.childNodes.length == 0 && item.localName == "menuitem"
                    && !/rdf:http:/.test(item.getAttribute("label"))) { // FIXME
                    item.fullMenuPath = parent + item.getAttribute("label");
                    items.push(item);
                }
                else {
                    let path = parent;
                    if (item.localName == "menu")
                        path += item.getAttribute("label") + ".";
                    addChildren(item, path);
                }
            }
        }

        let items = [];
        addChildren(document.getElementById(config.guioptions["m"][1]), "");
        return items;
    }
}, {
    // Only general options are added here, which are valid for all Dactyl extensions
    options: function () {
        options.add(["errorbells", "eb"],
            "Ring the bell when an error message is displayed",
            "boolean", false);

        options.add(["exrc", "ex"],
            "Enable automatic sourcing of an RC file in the current directory at startup",
            "boolean", false);

        options.add(["fullscreen", "fs"],
            "Show the current window fullscreen",
            "boolean", false, {
                setter: function (value) window.fullScreen = value,
                getter: function () window.fullScreen
            });

        const groups = [
            {
                opts: {
                    c: ["Always show the command line, even when empty"],
                    C: ["Always show the command line outside of the status line"],
                    M: ["Always show messages outside of the status line"]
                },
                setter: function (opts) {
                    commandline.widgets.updateVisibility();
                }
            },
            {
                opts: update({
                    s: ["Status bar", [statusline.statusBar.id]]
                }, config.guioptions),
                setter: function (opts) {
                    for (let [opt, [, ids]] in Iterator(this.opts)) {
                        ids.map(function (id) document.getElementById(id))
                           .forEach(function (elem) {
                            if (elem)
                                elem.collapsed = (opts.indexOf(opt) == -1);
                        });
                    }
                }
            },
            {
                opts: {
                    r: ["Right Scrollbar", "vertical"],
                    l: ["Left Scrollbar", "vertical"],
                    b: ["Bottom Scrollbar", "horizontal"]
                },
                setter: function (opts) {
                    let dir = ["horizontal", "vertical"].filter(
                        function (dir) !Array.some(opts,
                            function (o) this.opts[o] && this.opts[o][1] == dir, this),
                        this);
                    let class_ = dir.map(function (dir) "html|html > xul|scrollbar[orient=" + dir + "]");

                    styles.system.add("scrollbar", "*",
                                      class_.length ? class_.join(", ") + " { visibility: collapse !important; }" : "",
                                      true);

                    prefs.safeSet("layout.scrollbar.side", opts.indexOf("l") >= 0 ? 3 : 2,
                                  "See 'guioptions' scrollbar flags.");
                },
                validator: function (opts) Option.validIf(!(opts.indexOf("l") >= 0 && opts.indexOf("r") >= 0),
                                                          UTF8("Only one of ‘l’ or ‘r’ allowed"))
            },
            {
                feature: "tabs",
                opts: {
                    n: ["Tab number", highlight.selector("TabNumber")],
                    N: ["Tab number over icon", highlight.selector("TabIconNumber")]
                },
                setter: function (opts) {
                    let classes = [v[1] for ([k, v] in Iterator(this.opts)) if (opts.indexOf(k) < 0)];

                    styles.system.add("taboptions", "chrome://*",
                                      classes.length ? classes.join(",") + "{ display: none; }" : "");

                    if (!dactyl.has("Gecko2")) {
                        tabs.tabBinding.enabled = Array.some(opts, function (k) k in this.opts, this);
                        tabs.updateTabCount();
                    }
                    if (config.tabbrowser.tabContainer._positionPinnedTabs)
                        config.tabbrowser.tabContainer._positionPinnedTabs();
                }
            }
        ].filter(function (group) !group.feature || dactyl.has(group.feature));

        options.add(["guioptions", "go"],
            "Show or hide certain GUI elements like the menu or toolbar",
            "charlist", config.defaults.guioptions || "", {

                // FIXME: cleanup
                cleanupValue: config.cleanups.guioptions ||
                    "r" + [k for ([k, v] in iter(groups[1].opts))
                           if (!document.getElementById(v[1][0]).collapsed)].join(""),

                completer: function (context)
                    array(groups).map(function (g) [[k, v[0]] for ([k, v] in Iterator(g.opts))]).flatten(),
                setter: function (value) {
                    for (let group in values(groups))
                        group.setter(value);
                    events.checkFocus();
                    return value;
                },
                validator: function (val) Option.validateCompleter.call(this, val) &&
                        groups.every(function (g) !g.validator || g.validator(val))
            });

        options.add(["helpfile", "hf"],
            "Name of the main help file",
            "string", "intro");

        options.add(["loadplugins", "lpl"],
            "A regexp list that defines which plugins are loaded at startup and via :loadplugins",
            "regexplist", "'\\.(js|" + config.fileExtension + ")$'");

        options.add(["titlestring"],
            "The string shown at the end of the window title",
            "string", config.defaults.titlestring || config.host,
            {
                setter: function (value) {
                    let win = document.documentElement;
                    function updateTitle(old, current) {
                        document.title = document.title.replace(RegExp("(.*)" + util.regexp.escape(old)), "$1" + current);
                    }

                    // TODO: remove this FF3.5 test when we no longer support 3.0
                    //     : make this a config feature
                    if (services.privateBrowsing) {
                        let oldValue = win.getAttribute("titlemodifier_normal");
                        let suffix = win.getAttribute("titlemodifier_privatebrowsing").substr(oldValue.length);

                        win.setAttribute("titlemodifier_normal", value);
                        win.setAttribute("titlemodifier_privatebrowsing", value + suffix);

                        if (services.privateBrowsing.privateBrowsingEnabled) {
                            updateTitle(oldValue + suffix, value + suffix);
                            return value;
                        }
                    }

                    updateTitle(win.getAttribute("titlemodifier"), value);
                    win.setAttribute("titlemodifier", value);

                    return value;
                }
            });

        options.add(["urlseparator", "urlsep", "us"],
            "The regular expression used to separate multiple URLs in :open and friends",
            "string", "\\|",
            { validator: function (value) RegExp(value) });

        options.add(["verbose", "vbs"],
            "Define which info messages are displayed",
            "number", 1,
            { validator: function (value) Option.validIf(value >= 0 && value <= 15, "Value must be between 0 and 15") });

        options.add(["visualbell", "vb"],
            "Use visual bell instead of beeping on errors",
            "boolean", false,
            {
                setter: function (value) {
                    prefs.safeSet("accessibility.typeaheadfind.enablesound", !value,
                                  "See 'visualbell' option");
                    return value;
                }
            });
    },

    mappings: function () {
        mappings.add(modes.all, ["<F1>"],
            "Open the introductory help page",
            function () { dactyl.help(); });

        mappings.add(modes.all, ["<A-F1>"],
            "Open the single unchunked help page",
            function () { ex.helpall(); });

        if (dactyl.has("session"))
            mappings.add([modes.NORMAL], ["ZQ"],
                "Quit and don't save the session",
                function () { dactyl.quit(false); });

        mappings.add([modes.NORMAL], ["ZZ"],
            "Quit and save the session",
            function () { dactyl.quit(true); });
    },

    commands: function () {
        commands.add(["addo[ns]"],
            "Manage available Extensions and Themes",
            function () {
                dactyl.open("chrome://mozapps/content/extensions/extensions.xul",
                    { from: "addons" });
            },
            { argCount: "0" });

        commands.add(["dia[log]"],
            "Open a " + config.appName + " dialog",
            function (args) {
                let dialog = args[0];

                dactyl.assert(dialog in config.dialogs, "E475: Invalid argument: " + dialog);
                dactyl.assert(!config.dialogs[dialog][2] || config.dialogs[dialog][2](),
                              "Dialog " + dialog + " not available");
                try {
                    config.dialogs[dialog][1]();
                }
                catch (e) {
                    dactyl.echoerr("Error opening " + dialog.quote() + ": " + (e.message || e));
                }
            }, {
                argCount: "1",
                bang: true,
                completer: function (context) {
                    context.ignoreCase = true;
                    return completion.dialog(context);
                }
            });

        commands.add(["em[enu]"],
            "Execute the specified menu item from the command line",
            function (args) {
                let arg = args[0] || "";
                let items = Dactyl.getMenuItems();

                dactyl.assert(items.some(function (i) i.fullMenuPath == arg),
                    "E334: Menu not found: " + arg);

                for (let [, item] in Iterator(items)) {
                    if (item.fullMenuPath == arg)
                        item.doCommand();
                }
            }, {
                argCount: "1",
                completer: function (context) completion.menuItem(context),
                literal: 0
            });

        commands.add(["exe[cute]"],
            "Execute the argument as an Ex command",
            function (args) {
                try {
                    let cmd = dactyl.userEval(args[0] || "");
                    dactyl.execute(cmd, null, true);
                }
                catch (e) {
                    dactyl.echoerr(e);
                }
            }, {
                completer: function (context) completion.javascript(context),
                literal: 0
            });

        ///////////////////////////////////////////////////////////////////////////

        const addonErrors = array.toObject([
            [AddonManager.ERROR_NETWORK_FAILURE, "A network error occurred"],
            [AddonManager.ERROR_INCORRECT_HASH,  "The downloaded file did not match the expected hash"],
            [AddonManager.ERROR_CORRUPT_FILE,    "The file appears to be corrupt"],
            [AddonManager.ERROR_FILE_ACCESS,     "There was an error accessing the filesystem"]]);

        function listener(action, event)
            function addonListener(install) {
                dactyl[install.error ? "echoerr" : "echomsg"](
                    "Add-on " + action + " " + event + ": " + (install.name || install.sourceURI.spec) +
                    (install.error ? ": " + addonErrors[install.error] : ""));
            }
        const addonListener = {
            onNewInstall:      function (install) {},
            onExternalInstall: function (addon, existingAddon, needsRestart) {},
            onDownloadStarted:   listener("download", "started"),
            onDownloadEnded:     listener("download", "complete"),
            onDownloadCancelled: listener("download", "cancelled"),
            onDownloadFailed:    listener("download", "failed"),
            onDownloadProgress:  function (install) {},
            onInstallStarted:   function (install) {},
            onInstallEnded:     listener("installation", "complete"),
            onInstallCancelled: listener("installation", "cancelled"),
            onInstallFailed:    listener("installation", "failed")
        };

        const updateAddons = Class("UpgradeListener", {
            init: function init(addons) {
                dactyl.assert(!addons.length || addons[0].findUpdates,
                              "Not available on " + config.host + " " + services.runtime.version);
                this.remaining = addons;
                this.upgrade = [];
                dactyl.echomsg("Checking updates for addons: " + addons.map(function (a) a.name).join(", "));
                for (let addon in values(addons))
                    addon.findUpdates(this, AddonManager.UPDATE_WHEN_USER_REQUESTED, null, null);
            },
            addonListener: {
                __proto__: addonListener,
                onDownloadStarted: function () {},
                onDownloadEnded: function () {}
            },
            onUpdateAvailable: function (addon, install) {
                this.upgrade.push(addon);
                install.addListener(this.addonListener);
                install.install();
            },
            onUpdateFinished: function (addon, error) {
                this.remaining = this.remaining.filter(function (a) a != addon);
                if (!this.remaining.length)
                    dactyl.echomsg(
                        this.upgrade.length
                            ? "Installing updates for addons: " + this.upgrade.map(function (i) i.name).join(", ")
                            : "No addon updates found");
            }
        });

        ///////////////////////////////////////////////////////////////////////////

        function callResult(method) {
            let args = Array.slice(arguments, 1);
            return function (result) { result[method].apply(result, args); };
        }

        commands.add(["exta[dd]"],
            "Install an extension",
            function (args) {
                let url  = args[0];
                let file = io.File(url);
                function install(addonInstall) {
                    addonInstall.addListener(addonListener);
                    addonInstall.install();
                }

                if (!file.exists())
                    AddonManager.getInstallForURL(url,   install, "application/x-xpinstall");
                else if (file.isReadable() && file.isFile())
                    AddonManager.getInstallForFile(file, install, "application/x-xpinstall");
                else if (file.isDirectory())
                    dactyl.echoerr("Cannot install a directory: " + file.path.quote());
                else
                    dactyl.echoerr("E484: Can't open file " + file.path);
            }, {
                argCount: "1",
                completer: function (context) {
                    context.filters.push(function ({ item }) item.isDirectory() || /\.xpi$/.test(item.leafName));
                    completion.file(context);
                },
                literal: 0
            });

        // TODO: handle extension dependencies
        [
            {
                name: "extde[lete]",
                description: "Uninstall an extension",
                action: callResult("uninstall"),
                perm: "uninstall"
            },
            {
                name: "exte[nable]",
                description: "Enable an extension",
                action: function (addon) addon.userDisabled = false,
                filter: function ({ item }) item.userDisabled,
                perm: "enable"
            },
            {
                name: "extd[isable]",
                description: "Disable an extension",
                action: function (addon) addon.userDisabled = true,
                filter: function ({ item }) !item.userDisabled,
                perm: "disable"
            },
            {
                name: "extr[ehash]",
                description: "Reload an extension",
                action: function (addon) {
                    dactyl.assert(dactyl.has("Gecko2"), "This command is not useful in this version of " + config.host);
                    util.timeout(function () {
                        addon.userDisabled = true;
                        addon.userDisabled = false;
                    });
                },
                get filter() {
                    let ids = set(keys(JSON.parse(prefs.get("extensions.bootstrappedAddons", "{}"))));
                    return function ({ item }) !item.userDisabled && set.has(ids, item.id);
                },
                perm: "disable"
            },
            {
                name: "extt[oggle]",
                description: "Toggle an extension's enabled status",
                action: function (addon) addon.userDisabled = !addon.userDisabled
            },
            {
                name: "extu[pdate]",
                description: "Update an extension",
                actions: updateAddons,
                perm: "upgrade"
            }
        ].forEach(function (command) {
            let perm = command.perm && AddonManager["PERM_CAN_" + command.perm.toUpperCase()];
            function ok(addon) !perm || addon.permissions & perm;
            commands.add([command.name],
                command.description,
                function (args) {
                    let name = args[0];
                    if (args.bang)
                        dactyl.assert(!name, "E488: Trailing characters");
                    else
                        dactyl.assert(name, "E471: Argument required");

                    AddonManager.getAddonsByTypes(["extension"], dactyl.wrapCallback(function (list) {
                        if (!args.bang) {
                            list = list.filter(function (extension) extension.name == name);
                            if (list.length == 0)
                                return void dactyl.echoerr("E475: Invalid argument: " + name);
                            if (!list.every(ok))
                                return void dactyl.echoerr("Permission denied");
                        }
                        if (command.actions)
                            command.actions(list);
                        else
                            list.forEach(command.action);
                    }));
                }, {
                    argCount: "?", // FIXME: should be "1"
                    bang: true,
                    completer: function (context) {
                        completion.extension(context);
                        context.filters.push(function ({ item }) ok(item));
                        if (command.filter)
                            context.filters.push(command.filter);
                    },
                    literal: 0
                });
        });

        commands.add(["exto[ptions]", "extp[references]"],
            "Open an extension's preference dialog",
            function (args) {
                AddonManager.getAddonsByTypes(["extension"], dactyl.wrapCallback(function (list) {
                    list = list.filter(function (extension) extension.name == args[0]);
                    if (!list.length || !list[0].optionsURL)
                        dactyl.echoerr("E474: Invalid argument");
                    else if (args.bang)
                        window.openDialog(list[0].optionsURL, "_blank", "chrome");
                    else
                        dactyl.open(list[0].optionsURL, { from: "extoptions" });
                }));
            }, {
                argCount: "1",
                bang: true,
                completer: function (context) {
                    completion.extension(context);
                    context.filters.push(function ({ item }) item.isActive && item.optionsURL);
                },
                literal: 0
            });

        commands.add(["extens[ions]", "exts"],
            "List available extensions",
            function (args) {
                function addonExtra(e) {
                    let extra;
                    if (e.pendingOperations & AddonManager.PENDING_UNINSTALL)
                        extra = ["Disabled", "uninstalled"];
                    else if (e.pendingOperations & AddonManager.PENDING_DISABLE)
                        extra = ["Disabled", "disabled"];
                    else if (e.pendingOperations & AddonManager.PENDING_INSTALL)
                        extra = ["Enabled", "installed"];
                    else if (e.pendingOperations & AddonManager.PENDING_ENABLE)
                        extra = ["Enabled", "enabled"];
                    else if (e.pendingOperations & AddonManager.PENDING_UPGRADE)
                        extra = ["Enabled", "upgraded"];
                    if (extra)
                        return <>&#xa0;(<span highlight={extra[0]}>{extra[1]}</span>
                                        &#xa0;on restart)</>;
                    return <></>;
                }
                AddonManager.getAddonsByTypes(["extension"], function (extensions) {
                    if (args[0])
                        extensions = extensions.filter(function (extension) extension.name.indexOf(args[0]) >= 0);
                    extensions.sort(function (a, b) String.localeCompare(a.name, b.name));

                    if (extensions.length > 0)
                        commandline.commandOutput(
                            template.tabular(["Name", "Version", "Status", "Description"], [],
                                ([template.icon({ icon: e.iconURL }, e.name),
                                  e.version,
                                  (e.isActive ? <span highlight="Enabled">enabled</span>
                                              : <span highlight="Disabled">disabled</span>) +
                                  addonExtra(e),
                                  e.description]
                                for ([, e] in Iterator(extensions)))));
                    else if (filter)
                        dactyl.echoerr("Exxx: No extension matching " + filter.quote());
                    else
                        dactyl.echoerr("No extensions installed");
                });
            },
            { argCount: "?" });

        [
            {
                name: "h[elp]",
                description: "Open the introductory help page"
            }, {
                name: "helpa[ll]",
                description: "Open the single unchunked help page"
            }
        ].forEach(function (command) {
            let unchunked = command.name == "helpa[ll]";

            commands.add([command.name],
                command.description,
                function (args) {
                    dactyl.assert(!args.bang, "E478: Don't panic!");
                    dactyl.help(args.literalArg, unchunked);
                }, {
                    argCount: "?",
                    bang: true,
                    completer: function (context) completion.help(context, unchunked),
                    literal: 0
                });
        });

        commands.add(["javas[cript]", "js"],
            "Evaluate a JavaScript string",
            function (args) {
                if (args.bang) // open JavaScript console
                    dactyl.open("chrome://global/content/console.xul",
                        { from: "javascript" });
                else
                    dactyl.userEval(args[0]);
            }, {
                bang: true,
                completer: function (context) completion.javascript(context),
                hereDoc: true,
                literal: 0
            });

        commands.add(["loadplugins", "lpl"],
            "Load all plugins immediately",
            function (args) {
                dactyl.loadPlugins(args.length ? args : null);
            },
            {
                argCount: "*",
                keepQuotes: true,
                serialGroup: 10,
                serialize: function ()  [
                    {
                        command: this.name,
                        literalArg: options["loadplugins"].join(" ")
                    }
                ]
            });

        commands.add(["norm[al]"],
            "Execute Normal mode commands",
            function (args) { events.feedkeys(args[0], args.bang, false, modes.NORMAL); },
            {
                argCount: "1",
                bang: true,
                literal: 0
            });

        commands.add(["q[uit]"],
            dactyl.has("tabs") ? "Quit current tab" : "Quit application",
            function (args) {
                if (dactyl.has("tabs") && tabs.remove(tabs.getTab(), 1, false))
                    return;
                else if (dactyl.windows.length > 1)
                    window.close();
                else
                    dactyl.quit(false, args.bang);
            }, {
                argCount: "0",
                bang: true
            });

        commands.add(["reh[ash]"],
            "Reload the " + config.appName + " add-on",
            function (args) { util.rehash(args); },
            {
                argCount: "0",
                options: [
                    {
                        names: ["+u"],
                        description: "The initialization file to execute at startup",
                        type: CommandOption.STRING
                    },
                    {
                        names: ["++noplugin"],
                        description: "Do not automatically load plugins"
                    },
                    {
                        names: ["++cmd"],
                        description: "Ex commands to execute prior to initialization",
                        type: CommandOption.STRING,
                        multiple: true
                    },
                    {
                        names: ["+c"],
                        description: "Ex commands to execute after initialization",
                        type: CommandOption.STRING,
                        multiple: true
                    }
                ]
            });

        commands.add(["res[tart]"],
            "Force " + config.appName + " to restart",
            function () { dactyl.restart(); });

        var toolbox = document.getElementById("navigator-toolbox");
        if (toolbox) {
            function findToolbar(name) util.evaluateXPath(
                "./*[@toolbarname=" + util.escapeString(name, "'") + "]",
                document, toolbox).snapshotItem(0);

            let toolbarCommand = function (names, desc, action, filter) {
                commands.add(names, desc,
                    function (args) {
                        let toolbar = findToolbar(args[0] || "");
                        dactyl.assert(toolbar, "E474: Invalid argument");
                        action(toolbar);
                        events.checkFocus();
                    }, {
                        argcount: "1",
                        completer: function (context) {
                            completion.toolbar(context);
                            if (filter)
                                context.filters.push(filter);
                        },
                        literal: 0
                    });
            };

            toolbarCommand(["toolbars[how]", "tbs[how]"], "Show the named toolbar",
                function (toolbar) toolbar.collapsed = false,
                function (item) item.item.collapsed);
            toolbarCommand(["toolbarh[ide]", "tbh[ide]"], "Hide the named toolbar",
                function (toolbar) toolbar.collapsed = true,
                function (item) !item.item.collapsed);
            toolbarCommand(["toolbart[oggle]", "tbt[oggle]"], "Toggle the named toolbar",
                function (toolbar) toolbar.collapsed = !toolbar.collapsed);
        }

        commands.add(["time"],
            "Profile a piece of code or run a command multiple times",
            function (args) {
                let count = args.count;
                let special = args.bang;
                args = args[0] || "";

                if (args[0] == ":")
                    var method = function () commands.execute(args, null, true);
                else
                    method = dactyl.userFunc(args);

                try {
                    if (count > 1) {
                        let each, eachUnits, totalUnits;
                        let total = 0;

                        for (let i in util.interruptibleRange(0, count, 500)) {
                            let now = Date.now();
                            method();
                            total += Date.now() - now;
                        }

                        if (special)
                            return;

                        if (total / count >= 100) {
                            each = total / 1000.0 / count;
                            eachUnits = "sec";
                        }
                        else {
                            each = total / count;
                            eachUnits = "msec";
                        }

                        if (total >= 100) {
                            total = total / 1000.0;
                            totalUnits = "sec";
                        }
                        else
                            totalUnits = "msec";

                        commandline.commandOutput(
                                <table>
                                    <tr highlight="Title" align="left">
                                        <th colspan="3">Code execution summary</th>
                                    </tr>
                                    <tr><td>&#xa0;&#xa0;Executed:</td><td align="right"><span class="times-executed">{count}</span></td><td>times</td></tr>
                                    <tr><td>&#xa0;&#xa0;Average time:</td><td align="right"><span class="time-average">{each.toFixed(2)}</span></td><td>{eachUnits}</td></tr>
                                    <tr><td>&#xa0;&#xa0;Total time:</td><td align="right"><span class="time-total">{total.toFixed(2)}</span></td><td>{totalUnits}</td></tr>
                                </table>);
                    }
                    else {
                        let beforeTime = Date.now();
                        method();

                        if (special)
                            return;

                        let afterTime = Date.now();

                        if (afterTime - beforeTime >= 100)
                            dactyl.echo("Total time: " + ((afterTime - beforeTime) / 1000.0).toFixed(2) + " sec");
                        else
                            dactyl.echo("Total time: " + (afterTime - beforeTime) + " msec");
                    }
                }
                catch (e) {
                    dactyl.echoerr(e);
                }
            }, {
                argCount: "+",
                bang: true,
                completer: function (context) {
                    if (/^:/.test(context.filter))
                        return completion.ex(context);
                    else
                        return completion.javascript(context);
                },
                count: true,
                hereDoc: true,
                literal: 0,
                subCommand: 0
            });

        commands.add(["verb[ose]"],
            "Execute a command with 'verbose' set",
            function (args) {
                let vbs = options.get("verbose");
                let value = vbs.value;
                let setFrom = vbs.setFrom;

                try {
                    vbs.set(args.count || 1);
                    vbs.setFrom = null;
                    dactyl.execute(args[0] || "", null, true);
                }
                finally {
                    vbs.set(value);
                    vbs.setFrom = setFrom;
                }
            }, {
                argCount: "+",
                completer: function (context) completion.ex(context),
                count: true,
                literal: 0,
                subCommand: 0
            });

        commands.add(["ve[rsion]"],
            "Show version information",
            function (args) {
                if (args.bang)
                    dactyl.open("about:");
                else
                    commandline.commandOutput(<>
                        {config.appName} {config.version} running on:<br/>{navigator.userAgent}
                    </>);
            }, {
                argCount: "0",
                bang: true
            });

    },

    completion: function () {
        completion.dialog = function dialog(context) {
            context.title = ["Dialog"];
            context.filters.push(function ({ item }) !item[2] || item[2]());
            context.completions = [[k, v[0], v[2]] for ([k, v] in Iterator(config.dialogs))];
        };

        completion.extension = function extension(context) {
            context.title = ["Extension"];
            context.anchored = false;
            context.keys = { text: "name", description: "description", icon: "iconURL" },
            context.generate = function () {
                context.incomplete = true;
                AddonManager.getAddonsByTypes(["extension"], function (addons) {
                    context.incomplete = false;
                    context.completions = addons;
                });
            };
        };

        completion.help = function help(context, unchunked) {
            dactyl.initHelp();
            context.title = ["Help"];
            context.anchored = false;
            context.completions = services["dactyl:"].HELP_TAGS;
            if (unchunked)
                context.keys = { text: 0, description: function () "all" };
        };

        completion.menuItem = function menuItem(context) {
            context.title = ["Menu Path", "Label"];
            context.anchored = false;
            context.keys = { text: "fullMenuPath", description: function (item) item.getAttribute("label") };
            context.completions = dactyl.menuItems;
        };

        var toolbox = document.getElementById("navigator-toolbox");
        completion.toolbar = function toolbar(context) {
            context.title = ["Toolbar"];
            context.keys = { text: function (item) item.getAttribute("toolbarname"), description: function () "" };
            context.completions = util.evaluateXPath("./*[@toolbarname]", document, toolbox);
        };

        completion.window = function window(context) {
            context.title = ["Window", "Title"];
            context.keys = { text: function (win) dactyl.windows.indexOf(win) + 1, description: function (win) win.document.title };
            context.completions = dactyl.windows;
        };
    },
    load: function () {
        dactyl.triggerObserver("load");

        dactyl.log("All modules loaded", 3);

        try {
            if (services.fuel)
                var args = services.fuel.storage.get("dactyl.commandlineArgs", null);
            if (!args) {
                let commandline = services.commandLineHandler.optionValue;
                if (commandline)
                    args = dactyl.parseCommandLine(commandline);
            }
            if (args) {
                dactyl.commandLineOptions.rcFile = args["+u"];
                dactyl.commandLineOptions.noPlugins = "++noplugin" in args;
                dactyl.commandLineOptions.postCommands = args["+c"];
                dactyl.commandLineOptions.preCommands = args["++cmd"];
                util.dump("Processing command-line option: " + args.string);
            }
        }
        catch (e) {
            dactyl.echoerr("Parsing command line options: " + e);
        }

        dactyl.log("Command-line options: " + util.objectToString(dactyl.commandLineOptions), 3);

        // first time intro message
        const firstTime = "extensions." + config.name + ".firsttime";
        if (prefs.get(firstTime, true)) {
            dactyl.timeout(function () {
                this.withSavedValues(["forceNewTab"], function () {
                    this.forceNewTab = true;
                    this.help();
                    prefs.set(firstTime, false);
                });
            }, 1000);
        }

        // TODO: we should have some class where all this guioptions stuff fits well
        // Dactyl.hideGUI();

        if (dactyl.commandLineOptions.preCommands)
            dactyl.commandLineOptions.preCommands.forEach(function (cmd) {
                dactyl.execute(cmd);
            });

        // finally, read the RC file and source plugins
        // make sourcing asynchronous, otherwise commands that open new tabs won't work
        util.timeout(function () {
            let init = services.environment.get(config.idName + "_INIT");
            let rcFile = io.getRCFile("~");

            if (dactyl.userEval('typeof document') === "undefined")
                jsmodules.__proto__ = XPCSafeJSObjectWrapper(window);

            try {
                if (dactyl.commandLineOptions.rcFile) {
                    let filename = dactyl.commandLineOptions.rcFile;
                    if (!/^(NONE|NORC)$/.test(filename))
                        io.source(io.File(filename).path, false); // let io.source handle any read failure like Vim
                }
                else {
                    if (init)
                        dactyl.execute(init);
                    else {
                        if (rcFile) {
                            io.source(rcFile.path, false);
                            services.environment.set("MY_" + config.idName + "RC", rcFile.path);
                        }
                        else
                            dactyl.log("No user RC file found", 3);
                    }

                    if (options["exrc"] && !dactyl.commandLineOptions.rcFile) {
                        let localRCFile = io.getRCFile(io.cwd);
                        if (localRCFile && !localRCFile.equals(rcFile))
                            io.source(localRCFile.path, false);
                    }
                }

                if (dactyl.commandLineOptions.rcFile == "NONE" || dactyl.commandLineOptions.noPlugins)
                    options["loadplugins"] = false;

                if (options["loadplugins"])
                    dactyl.loadPlugins();
            }
            catch (e) {
                dactyl.reportError(e, true);
            }

            // after sourcing the initialization files, this function will set
            // all gui options to their default values, if they have not been
            // set before by any RC file
            for (let option in values(options.needInit))
                option.initValue();

            if (dactyl.commandLineOptions.postCommands)
                dactyl.commandLineOptions.postCommands.forEach(function (cmd) {
                    dactyl.execute(cmd);
                });

            dactyl.fullyInitialized = true;
            dactyl.triggerObserver("enter", null);
            autocommands.trigger("Enter", {});
        }, 0);

        statusline.update();
        dactyl.log(config.appName + " fully initialized", 0);
        dactyl.initialized = true;
    }
});

// vim: set fdm=marker sw=4 ts=4 et: