croft-software 0.1.942

VSCode-style TUI written in Rust
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
use anyhow::{Context, Result};
use plist::{Dictionary, Value};
use std::path::{Path, PathBuf};

pub const ITERM2_PLIST_REL: &str = "Library/Preferences/com.googlecode.iterm2.plist";
const CMD_SHIFT_F_KEY: &str = "0x46-0x120000-0x3";
const CMD_SHIFT_F_HEX: &str = "0x1b 0x5b 0x37 0x30 0x3b 0x31 0x30 0x75";
const CMD_F_KEY: &str = "0x66-0x100000-0x3";
const CMD_F_HEX: &str = "0x1b 0x5b 0x31 0x30 0x32 0x3b 0x39 0x75";
const CMD_R_KEY: &str = "0x72-0x100000-0xf";
const CMD_R_HEX: &str = "0x1b 0x5b 0x31 0x31 0x34 0x3b 0x39 0x75";
/// `Cmd+Opt+R` -> "Reveal in Finder" (Explorer). Modifier mask 0x180000
/// (Cmd 0x100000 | Opt 0x80000), `kVK_ANSI_R` = 0xf. CSI-u `ESC [ 114 ; 11 u`
/// (114 = 'r'; modbyte 11 = 1 + Alt(2) + Super(8)) so crossterm decodes it as
/// ALT|SUPER, disjoint from plain Cmd+R (Rename). macOS does not bind this
/// chord to a default menu item, so no NSUserKeyEquivalents relocation is
/// needed for the forwarder to win.
const CMD_OPT_R_KEY: &str = "0x72-0x180000-0xf";
const CMD_OPT_R_HEX: &str = "0x1b 0x5b 0x31 0x31 0x34 0x3b 0x31 0x31 0x75";
/// `Cmd+Opt+C` -> "Copy Path" (editor tab). Modifier mask 0x180000
/// (Cmd 0x100000 | Opt 0x80000), `kVK_ANSI_C` = 0x8. CSI-u `ESC [ 99 ; 11 u`
/// (99 = 'c'; modbyte 11 = 1 + Alt(2) + Super(8)) so crossterm decodes it as
/// ALT|SUPER, disjoint from plain Cmd+C (Copy) and `Cmd+Opt+Shift+C` (Copy
/// Relative Path, which carries Shift). macOS does not bind this chord to a
/// default menu item, so no NSUserKeyEquivalents relocation is needed.
const CMD_OPT_C_KEY: &str = "0x63-0x180000-0x8";
const CMD_OPT_C_HEX: &str = "0x1b 0x5b 0x39 0x39 0x3b 0x31 0x31 0x75";
/// `Cmd+Opt+M` -> "View: Toggle Minimap". Modifier mask 0x180000
/// (Cmd 0x100000 | Opt 0x80000), `kVK_ANSI_M` = 0x2e. CSI-u `ESC [ 109 ; 11 u`
/// (109 = 'm'; modbyte 11 = 1 + Alt(2) + Super(8)) so crossterm decodes it as
/// ALT|SUPER. macOS does not bind this chord to a default menu item, so no
/// NSUserKeyEquivalents relocation is needed; the forwarder claims it.
const CMD_OPT_M_KEY: &str = "0x6d-0x180000-0x2e";
const CMD_OPT_M_HEX: &str = "0x1b 0x5b 0x31 0x30 0x39 0x3b 0x31 0x31 0x75";
/// `Cmd+Opt+F` -> "Replace in File" (the editor find bar expanded with the
/// replace row). Modifier mask 0x180000 (Cmd 0x100000 | Opt 0x80000),
/// `kVK_ANSI_F` = 0x03. CSI-u `ESC [ 102 ; 11 u` (102 = 'f'; modbyte 11 =
/// 1 + Alt(2) + Super(8)) so crossterm decodes it as ALT|SUPER. iTerm2
/// binds this chord to "Password Manager" by default (MainMenu.xib,
/// verified 2026-07-04), so the NSUserKeyEquivalents relocation below
/// moves that item to Cmd+Opt+Ctrl+F for the forwarder to claim the bare
/// chord.
const CMD_OPT_F_KEY: &str = "0x66-0x180000-0x3";
const CMD_OPT_F_HEX: &str = "0x1b 0x5b 0x31 0x30 0x32 0x3b 0x31 0x31 0x75";
/// `Cmd+Opt+Return` -> Replace All in the editor find bar. Return reports
/// character 0xd, mask 0x180000, `kVK_Return` = 0x24. CSI-u
/// `ESC [ 13 ; 11 u` (modbyte 11 = 1 + Alt(2) + Super(8)) decodes to
/// `Enter + ALT | SUPER`. No default iTerm2 menu item claims this chord
/// (MainMenu.xib has no Option+Command Return equivalent, verified
/// 2026-07-04), so no relocation is needed; the forwarder claims it.
const CMD_OPT_ENTER_KEY: &str = "0xd-0x180000-0x24";
const CMD_OPT_ENTER_HEX: &str = "0x1b 0x5b 0x31 0x33 0x3b 0x31 0x31 0x75";
const CMD_SLASH_KEY: &str = "0x2f-0x100000-0x2c";
const CMD_SLASH_HEX: &str = "0x1b 0x5b 0x34 0x37 0x3b 0x39 0x75";
/// `Cmd+.` (period) for Quick Fix (VS Code's `editor.action.quickFix`).
/// character = 0x2e ('.'), modifiers = 0x100000 (Cmd), virtualKeyCode = 0x2f
/// (kVK_ANSI_Period). HEX is the CSI-u sequence `ESC [ 46 ; 9 u` (codepoint 46
/// = '.', modbyte 9 = 1 + Cmd(8)). iTerm2 does not bind Cmd+. to a menu item,
/// so no NSUserKeyEquivalents relocation is needed; the forwarder claims it.
const CMD_DOT_KEY: &str = "0x2e-0x100000-0x2f";
const CMD_DOT_HEX: &str = "0x1b 0x5b 0x34 0x36 0x3b 0x39 0x75";
/// `Cmd+Shift+Return`. Serialized identifier follows iTerm2's
/// iTermKeystroke format `0x<char>-0x<modifiers>-0x<virtualKeyCode>`:
/// character = 0xd (CR, the same unmodified value Return reports under
/// Shift), modifiers = 0x120000 (Cmd 0x100000 | Shift 0x20000),
/// virtualKeyCode = 0x24 (kVK_Return). Without this entry iTerm2 simply
/// drops the chord on the floor, which is why croft never saw it.
const CMD_SHIFT_ENTER_KEY: &str = "0xd-0x120000-0x24";
/// CSI-u sequence `ESC [ 13 ; 10 u` = Enter (codepoint 13) with kitty
/// modifier byte 10 (= 1 base + Shift(1) + Super(8)). Crossterm parses
/// this back into `KeyEvent { code: Enter, modifiers: SHIFT | SUPER }`,
/// which the Explorer's plain Enter handler already routes through
/// `tree.activate()` to toggle expand/collapse on folders.
const CMD_SHIFT_ENTER_HEX: &str = "0x1b 0x5b 0x31 0x33 0x3b 0x31 0x30 0x75";
const CMD_V_KEY: &str = "0x76-0x100000-0x9";
/// GlobalKeyMap keys + CSI-u payloads for the five Mac-style Cmd+letter
/// chords croft uses across panes (terminal copy / source-control
/// select-all / editor save / editor cut / editor undo). iTerm2's
/// `iTermApplication.sendEvent:` checks `GlobalKeyMap` ahead of the
/// NSResponder chain, so a forwarder here intercepts Cmd+letter before
/// AppKit's default copy:/cut:/selectAll:/undo: bindings consume it.
/// Each payload is `ESC [ <codepoint> ; 9 u`, where 9 = 1 base + Super(8)
/// in kitty's CSI-u modifier byte, which crossterm decodes into a
/// `KeyEvent { code: Char(letter), modifiers: SUPER }` that croft's
/// terminal/editor/tree handlers already accept.
const CMD_A_KEY: &str = "0x61-0x100000-0x0";
const CMD_A_HEX: &str = "0x1b 0x5b 0x39 0x37 0x3b 0x39 0x75";
const CMD_C_KEY: &str = "0x63-0x100000-0x8";
const CMD_C_HEX: &str = "0x1b 0x5b 0x39 0x39 0x3b 0x39 0x75";
const CMD_S_KEY: &str = "0x73-0x100000-0x1";
const CMD_S_HEX: &str = "0x1b 0x5b 0x31 0x31 0x35 0x3b 0x39 0x75";
const CMD_X_KEY: &str = "0x78-0x100000-0x7";
const CMD_X_HEX: &str = "0x1b 0x5b 0x31 0x32 0x30 0x3b 0x39 0x75";
const CMD_Z_KEY: &str = "0x7a-0x100000-0x6";
const CMD_Z_HEX: &str = "0x1b 0x5b 0x31 0x32 0x32 0x3b 0x39 0x75";
// Cmd+Shift+Z: editor redo (undo's sibling). Codepoint 'Z' (0x5a), Cmd+Shift
// mask 0x120000, vkcode 0x6; CSI-u `ESC [ 90 ; 10 u`.
const CMD_SHIFT_Z_KEY: &str = "0x5a-0x120000-0x6";
const CMD_SHIFT_Z_HEX: &str = "0x1b 0x5b 0x39 0x30 0x3b 0x31 0x30 0x75";
/// Vim-style chord starts and goto-bottom that the editor consumes.
/// CSI-u `ESC [ <codepoint> ; 9 u` for Cmd+letter and
/// `ESC [ <shifted-glyph> ; 10 u` for Cmd+Shift+letter; modifier byte
/// 9 = 1 base + Super(8), 10 adds Shift(1).
const CMD_D_KEY: &str = "0x64-0x100000-0x2";
const CMD_D_HEX: &str = "0x1b 0x5b 0x31 0x30 0x30 0x3b 0x39 0x75";
const CMD_G_KEY: &str = "0x67-0x100000-0x5";
const CMD_G_HEX: &str = "0x1b 0x5b 0x31 0x30 0x33 0x3b 0x39 0x75";
/// `Cmd+K` — leader for croft's `Cmd+K`-prefixed chords (Color Theme, Close to
/// the Right, Select for / Compare with Selected, Close All). iTerm2 binds
/// `Cmd+K` to Edit > Clear Buffer by default, so the NSUserKeyEquivalents
/// relocation below moves Clear Buffer to `Cmd+Opt+K` for this forwarder to
/// win. `kVK_ANSI_K` = 0x28, CSI-u `ESC [ 107 ; 9 u` (107 = 'k', modifier
/// byte 9 = 1 base + Super(8)).
const CMD_K_KEY: &str = "0x6b-0x100000-0x28";
const CMD_K_HEX: &str = "0x1b 0x5b 0x31 0x30 0x37 0x3b 0x39 0x75";
/// iTerm2's Edit > "Clear Buffer" menu item (bound to Cmd+K by default) and the
/// chord it is relocated to so croft can claim the bare Cmd+K leader. `@~k` =
/// Cmd+Opt+K (`@` Cmd, `~` Opt).
const CLEAR_BUFFER_MENU_KEY: &str = "Clear Buffer";
const CLEAR_BUFFER_MENU_EQUIV: &str = "@~k";
const CMD_Y_KEY: &str = "0x79-0x100000-0x10";
const CMD_Y_HEX: &str = "0x1b 0x5b 0x31 0x32 0x31 0x3b 0x39 0x75";
const CMD_O_KEY: &str = "0x6f-0x100000-0x1f";
const CMD_O_HEX: &str = "0x1b 0x5b 0x31 0x31 0x31 0x3b 0x39 0x75";
/// `Cmd+E` — toggle native modal (vim) editing in the editor pane (croft's
/// `is_vim_toggle_key`). iTerm2 inherits AppKit's standard Edit > Find
/// submenu, which binds the bare chord to "Use Selection for Find"; the
/// NSUserKeyEquivalents override below relocates that item to Cmd+Opt+E so
/// this GlobalKeyMap forwarder fires instead, exactly as Cmd+F (a sibling in
/// the same submenu) is handled. Codepoint 'e' (0x65 = 101), virtualKeyCode
/// `kVK_ANSI_E` = 0xe, modifier mask 0x100000 (Cmd). CSI-u `ESC [ 101 ; 9 u`
/// (modifier byte 9 = 1 base + Super(8)), which crossterm decodes back to
/// `KeyEvent { code: Char('e'), modifiers: SUPER }`.
const CMD_E_KEY: &str = "0x65-0x100000-0xe";
const CMD_E_HEX: &str = "0x1b 0x5b 0x31 0x30 0x31 0x3b 0x39 0x75";
/// AppKit / iTerm2 menu item that owns bare Cmd+E (Edit > Find > Use
/// Selection for Find) and the chord it is relocated to so croft can claim
/// Cmd+E. Cmd+Opt+E keeps the find-from-selection action reachable.
const USE_SELECTION_FOR_FIND_MENU_KEY: &str = "Use Selection for Find";
const USE_SELECTION_FOR_FIND_MENU_EQUIV: &str = "@~e";
const CMD_SHIFT_G_KEY: &str = "0x47-0x120000-0x5";
const CMD_SHIFT_G_HEX: &str = "0x1b 0x5b 0x37 0x31 0x3b 0x31 0x30 0x75";
const CMD_SHIFT_O_KEY: &str = "0x4f-0x120000-0x1f";
const CMD_SHIFT_O_HEX: &str = "0x1b 0x5b 0x37 0x39 0x3b 0x31 0x30 0x75";
/// `Cmd+Shift+E` — jump to Explorer sidebar from any pane. Codepoint 'E'
/// (0x45 = 69), virtualKeyCode `kVK_ANSI_E` = 0x0e, modifier byte 10 =
/// 1 base + Shift(1) + Super(8). CSI-u payload `ESC [ 69 ; 10 u`.
const CMD_SHIFT_E_KEY: &str = "0x45-0x120000-0xe";
const CMD_SHIFT_E_HEX: &str = "0x1b 0x5b 0x36 0x39 0x3b 0x31 0x30 0x75";
/// `Cmd+Shift+S` — jump to Source Control. Codepoint 'S' (0x53 = 83),
/// virtualKeyCode `kVK_ANSI_S` = 0x01. CSI-u `ESC [ 83 ; 10 u`.
const CMD_SHIFT_S_KEY: &str = "0x53-0x120000-0x1";
const CMD_SHIFT_S_HEX: &str = "0x1b 0x5b 0x38 0x33 0x3b 0x31 0x30 0x75";
/// `Cmd+Shift+D` — jump to Run and Debug. iTerm2 binds the bare chord to
/// "Split Horizontally with Same Profile"; the NSUserKeyEquivalents
/// override below relocates that menu item to Cmd+Opt+Shift+D so this
/// forwarder fires instead. Codepoint 'D' (0x44 = 68), virtualKeyCode
/// `kVK_ANSI_D` = 0x02. CSI-u `ESC [ 68 ; 10 u`.
const CMD_SHIFT_D_KEY: &str = "0x44-0x120000-0x2";
const CMD_SHIFT_D_HEX: &str = "0x1b 0x5b 0x36 0x38 0x3b 0x31 0x30 0x75";
/// `Cmd+Shift+R` — jump to Remote sidebar. Codepoint 'R' (0x52 = 82),
/// virtualKeyCode `kVK_ANSI_R` = 0x0f. CSI-u `ESC [ 82 ; 10 u`.
const CMD_SHIFT_R_KEY: &str = "0x52-0x120000-0xf";
const CMD_SHIFT_R_HEX: &str = "0x1b 0x5b 0x38 0x32 0x3b 0x31 0x30 0x75";
/// `Cmd+Shift+X` — jump to the Extensions sidebar view. Matches VS Code's
/// "View: Show Extensions". Cmd+Shift+X is unbound by any default macOS / iTerm2
/// menu item (verified 2026-06-20), so like Cmd+Shift+S no NSUserKeyEquivalents
/// relocation is needed; the GlobalKeyMap forwarder simply claims the chord.
/// Codepoint 'X' (0x58 = 88), virtualKeyCode `kVK_ANSI_X` = 0x07, modifier mask
/// 0x120000 (Cmd+Shift). CSI-u `ESC [ 88 ; 10 u` (modifier byte 10 = 1 base +
/// Shift(1) + Super(8)), which crossterm decodes back to
/// `KeyEvent { code: Char('X'), modifiers: SHIFT | SUPER }`, accepted
/// case-insensitively by `is_extensions_jump_key`.
const CMD_SHIFT_X_KEY: &str = "0x58-0x120000-0x7";
const CMD_SHIFT_X_HEX: &str = "0x1b 0x5b 0x38 0x38 0x3b 0x31 0x30 0x75";
/// `Cmd+Shift+V` — Markdown: Toggle Preview (the VS Code default chord).
/// iTerm2 binds the bare chord to Edit > Paste Special > "Paste Selection";
/// the NSUserKeyEquivalents override below relocates that item to
/// Cmd+Opt+Shift+V so this forwarder fires instead. Codepoint 'V' (0x56 =
/// 86), virtualKeyCode `kVK_ANSI_V` = 0x09. CSI-u `ESC [ 86 ; 10 u`.
const CMD_SHIFT_V_KEY: &str = "0x56-0x120000-0x9";
const CMD_SHIFT_V_HEX: &str = "0x1b 0x5b 0x38 0x36 0x3b 0x31 0x30 0x75";
/// `Cmd+Shift+L` — disconnect a remote session and drop back into the local
/// croft. Codepoint 'L' (0x4c = 76), virtualKeyCode `kVK_ANSI_L` = 0x25.
/// CSI-u `ESC [ 76 ; 10 u`. Forwarded defensively so AppKit / iTerm2 cannot
/// consume the chord before the remote croft's handler sees it.
const CMD_SHIFT_L_KEY: &str = "0x4c-0x120000-0x25";
const CMD_SHIFT_L_HEX: &str = "0x1b 0x5b 0x37 0x36 0x3b 0x31 0x30 0x75";
/// `Cmd+Shift+N` — Explorer "New Folder". Codepoint 'N' (0x4e = 78),
/// virtualKeyCode `kVK_ANSI_N` = 0x2d. CSI-u `ESC [ 78 ; 10 u`. Forwarded
/// defensively so AppKit / iTerm2 cannot consume the chord at the menu
/// layer before croft's Explorer-pane handler sees it.
const CMD_SHIFT_N_KEY: &str = "0x4e-0x120000-0x2d";
const CMD_SHIFT_N_HEX: &str = "0x1b 0x5b 0x37 0x38 0x3b 0x31 0x30 0x75";
/// `Cmd+Shift+T` — focus the Terminal pane from any pane. iTerm2 binds
/// the bare chord to "Restore Closed Session"; the NSUserKeyEquivalents
/// override below relocates that menu item to Cmd+Opt+Shift+T so this
/// forwarder fires instead. Codepoint 'T' (0x54 = 84), virtualKeyCode
/// `kVK_ANSI_T` = 0x11. CSI-u `ESC [ 84 ; 10 u`.
const CMD_SHIFT_T_KEY: &str = "0x54-0x120000-0x11";
const CMD_SHIFT_T_HEX: &str = "0x1b 0x5b 0x38 0x34 0x3b 0x31 0x30 0x75";
/// `Cmd+Shift+K` — Delete Line (VS Code's binding), the new home of the
/// delete-line action after `Cmd+D` moved to Add Selection to Next Match.
/// Cmd+Shift+K is unbound by default macOS / iTerm2, so no NSUserKeyEquivalents
/// relocation is needed; the GlobalKeyMap forwarder simply claims it. Codepoint
/// 'K' (0x4b = 75), virtualKeyCode `kVK_ANSI_K` = 0x28. CSI-u `ESC [ 75 ; 10 u`
/// (modifier byte 10 = 1 base + Shift(1) + Super(8)).
const CMD_SHIFT_K_KEY: &str = "0x4b-0x120000-0x28";
const CMD_SHIFT_K_HEX: &str = "0x1b 0x5b 0x37 0x35 0x3b 0x31 0x30 0x75";
/// `Cmd+Shift+B` — Tasks: Run Build Task (VS Code's binding, croft's
/// `is_run_build_task_key`). iTerm2 binds the same chord to the "Show
/// Toolbelt" menu item (MainMenu.xib id 1297: keyEquivalent="B", default
/// command mask); the NSUserKeyEquivalents override below relocates it to
/// Cmd+Ctrl+B so this forwarder wins. Codepoint 'B' (0x42 = 66),
/// virtualKeyCode `kVK_ANSI_B` = 0x0b. CSI-u `ESC [ 66 ; 10 u`
/// (modifier byte 10 = 1 base + Shift(1) + Super(8)).
const CMD_SHIFT_B_KEY: &str = "0x42-0x120000-0xb";
const CMD_SHIFT_B_HEX: &str = "0x1b 0x5b 0x36 0x36 0x3b 0x31 0x30 0x75";
/// `Cmd+T` — open another terminal next to the active one (croft's
/// `is_terminal_split_key`). iTerm2 binds the bare chord to the "New Tab"
/// menu item (MainMenu.xib: `keyEquivalent="t"`); the NSUserKeyEquivalents
/// override below relocates it to Cmd+Ctrl+T so this forwarder wins.
/// Codepoint 't' (0x74 = 116), virtualKeyCode `kVK_ANSI_T` = 0x11. CSI-u
/// `ESC [ 116 ; 9 u`, modifier byte 9 = 1 base + Super(8), which crossterm
/// decodes back to `KeyEvent { code: Char('t'), modifiers: SUPER }`.
const CMD_T_KEY: &str = "0x74-0x100000-0x11";
const CMD_T_HEX: &str = "0x1b 0x5b 0x31 0x31 0x36 0x3b 0x39 0x75";
/// iTerm2's "New Tab" menu item title (MainMenu.xib id 867) and the chord
/// it is relocated to so croft can claim the bare Cmd+T. `@^t` = Cmd+Ctrl+T,
/// unbound by default and clear of the Cmd+Opt+T alternate ("New Tab Next
/// to Current Tab"), keeps the iTerm2 action reachable.
const NEW_TAB_MENU_KEY: &str = "New Tab";
const NEW_TAB_MENU_EQUIV: &str = "@^t";
/// `Cmd+[` / `Cmd+]` — cycle to the previous / next terminal (croft's
/// `is_terminal_cycle_back_key` / `is_terminal_cycle_key`). iTerm2 binds
/// the bare chords to "Previous Pane" / "Next Pane" (MainMenu.xib ids
/// 1251/1250, Command-only); the NSUserKeyEquivalents overrides below
/// relocate them to Cmd+Opt+[ / Cmd+Opt+] so these forwarders win.
/// Brackets are normal keys, so they use the 3-part `char-modifiers-keycode`
/// form (like Cmd+T), not the 2-part function-key form arrows use. '[' =
/// 0x5b, virtualKeyCode `kVK_ANSI_LeftBracket` = 0x21; ']' = 0x5d, vkc
/// `kVK_ANSI_RightBracket` = 0x1e. CSI-u `ESC [ 91 ; 9 u` / `ESC [ 93 ; 9 u`
/// (modifier byte 9 = 1 base + Super(8)), which crossterm decodes to
/// `Char('[' / ']') + SUPER`. Arrows are unusable for cycling: Ctrl+arrows
/// are eaten by the macOS Spaces shortcuts, Option+arrows are shell
/// word-motion, and Cmd+arrows are reserved by the user.
const CMD_LBRACKET_KEY: &str = "0x5b-0x100000-0x21";
const CMD_LBRACKET_HEX: &str = "0x1b 0x5b 0x39 0x31 0x3b 0x39 0x75";
const CMD_RBRACKET_KEY: &str = "0x5d-0x100000-0x1e";
const CMD_RBRACKET_HEX: &str = "0x1b 0x5b 0x39 0x33 0x3b 0x39 0x75";
/// `Cmd+\` -> split the editor into two side-by-side columns (VS Code's
/// `workbench.action.splitEditor`, croft's `is_editor_split_key`). Normal
/// key, so the 3-part `char-modifiers-keycode` form like Cmd+T: '\' = 0x5c,
/// Cmd modifier mask 0x100000, `kVK_ANSI_Backslash` = 0x2a. CSI-u
/// `ESC [ 92 ; 9 u` (modifier byte 9 = 1 base + Super(8)), which crossterm
/// decodes to `Char('\\') + SUPER`. Cmd+\ is not bound to any default
/// macOS / iTerm2 menu item, so no NSUserKeyEquivalents relocation is needed.
const CMD_BACKSLASH_KEY: &str = "0x5c-0x100000-0x2a";
const CMD_BACKSLASH_HEX: &str = "0x1b 0x5b 0x39 0x32 0x3b 0x39 0x75";
/// `Cmd+Shift+\` -> Go to Bracket (VS Code's `editor.action.jumpToBracket`,
/// croft's `is_goto_bracket_key`). Same `\` key as Cmd+\ above but with Shift
/// added to the modifier mask (Super 0x100000 | Shift 0x20000 = 0x120000).
/// CSI-u `ESC [ 92 ; 10 u` (modifier byte 10 = 1 base + Shift(1) + Super(8)),
/// which crossterm decodes to `Char('\\') + SHIFT | SUPER`. Not bound to any
/// default macOS / iTerm2 menu item, so no NSUserKeyEquivalents relocation.
const CMD_SHIFT_BACKSLASH_KEY: &str = "0x5c-0x120000-0x2a";
const CMD_SHIFT_BACKSLASH_HEX: &str = "0x1b 0x5b 0x39 0x32 0x3b 0x31 0x30 0x75";
/// `Cmd+Opt+\` -> Select to Bracket (VS Code's `editor.action.selectToBracket`,
/// croft's `is_select_to_bracket_key`). The `\` key with Cmd+Opt (Super
/// 0x100000 | Alt 0x80000 = 0x180000). CSI-u `ESC [ 92 ; 11 u` (modifier byte
/// 11 = 1 base + Alt(2) + Super(8)) -> `Char('\\') + ALT | SUPER`.
const CMD_OPT_BACKSLASH_KEY: &str = "0x5c-0x180000-0x2a";
const CMD_OPT_BACKSLASH_HEX: &str = "0x1b 0x5b 0x39 0x32 0x3b 0x31 0x31 0x75";
/// `Cmd+Opt+Shift+<letter>` for editor commands VS Code leaves unbound, so
/// croft binds them (tenet: everything has a shortcut). Modifier mask Super
/// 0x100000 | Alt 0x80000 | Shift 0x20000 = 0x1a0000; CSI-u modifier byte 12 =
/// 1 base + Shift(1) + Alt(2) + Super(8), which crossterm decodes to
/// `Char + SHIFT | ALT | SUPER`. `S` = Convert Indentation to Spaces, `T` =
/// Convert Indentation to Tabs, `N` = Trim Final Newlines.
const CMD_OPT_SHIFT_S_KEY: &str = "0x53-0x1a0000-0x1";
const CMD_OPT_SHIFT_S_HEX: &str = "0x1b 0x5b 0x38 0x33 0x3b 0x31 0x32 0x75";
const CMD_OPT_SHIFT_T_KEY: &str = "0x54-0x1a0000-0x11";
const CMD_OPT_SHIFT_T_HEX: &str = "0x1b 0x5b 0x38 0x34 0x3b 0x31 0x32 0x75";
const CMD_OPT_SHIFT_N_KEY: &str = "0x4e-0x1a0000-0x2d";
const CMD_OPT_SHIFT_N_HEX: &str = "0x1b 0x5b 0x37 0x38 0x3b 0x31 0x32 0x75";
// Same `Cmd+Opt+Shift+<letter>` tier (modifier byte 12) for the formerly
// palette-only editor commands, so none ships without an accelerator:
// `J` = Join Lines, `U`/`L` = Transform to Upper/Lower case, `C` = Copy
// Relative Path (Title Case keeps its palette entry but no chord, matching VS
// Code), `A`/`D` = Sort Lines Ascending/Descending, `W` = Trim Trailing Whitespace.
const CMD_OPT_SHIFT_J_KEY: &str = "0x4a-0x1a0000-0x26";
const CMD_OPT_SHIFT_J_HEX: &str = "0x1b 0x5b 0x37 0x34 0x3b 0x31 0x32 0x75";
const CMD_OPT_SHIFT_U_KEY: &str = "0x55-0x1a0000-0x20";
const CMD_OPT_SHIFT_U_HEX: &str = "0x1b 0x5b 0x38 0x35 0x3b 0x31 0x32 0x75";
const CMD_OPT_SHIFT_L_KEY: &str = "0x4c-0x1a0000-0x25";
const CMD_OPT_SHIFT_L_HEX: &str = "0x1b 0x5b 0x37 0x36 0x3b 0x31 0x32 0x75";
const CMD_OPT_SHIFT_C_KEY: &str = "0x43-0x1a0000-0x8";
const CMD_OPT_SHIFT_C_HEX: &str = "0x1b 0x5b 0x36 0x37 0x3b 0x31 0x32 0x75";
const CMD_OPT_SHIFT_A_KEY: &str = "0x41-0x1a0000-0x0";
const CMD_OPT_SHIFT_A_HEX: &str = "0x1b 0x5b 0x36 0x35 0x3b 0x31 0x32 0x75";
const CMD_OPT_SHIFT_D_KEY: &str = "0x44-0x1a0000-0x2";
const CMD_OPT_SHIFT_D_HEX: &str = "0x1b 0x5b 0x36 0x38 0x3b 0x31 0x32 0x75";
const CMD_OPT_SHIFT_W_KEY: &str = "0x57-0x1a0000-0xd";
const CMD_OPT_SHIFT_W_HEX: &str = "0x1b 0x5b 0x38 0x37 0x3b 0x31 0x32 0x75";
// `F` = Format Document (VS Code's `Shift+Alt+F`), CSI-u `ESC [ 70 ; 12 u`.
const CMD_OPT_SHIFT_F_KEY: &str = "0x46-0x1a0000-0x3";
const CMD_OPT_SHIFT_F_HEX: &str = "0x1b 0x5b 0x37 0x30 0x3b 0x31 0x32 0x75";
/// `Cmd+Opt+Left` / `Cmd+Opt+Right` -> move focus to the left / right
/// editor group while split (croft's `is_focus_group_left_key` /
/// `is_focus_group_right_key`). Arrows use the 2-part function-key form
/// `0x<unicode>-0x<modmask>`: `NSLeftArrowFunctionKey` = 0xf702,
/// `NSRightArrowFunctionKey` = 0xf703, Cmd+Opt mask 0x180000 (Cmd 0x100000
/// | Opt 0x80000). Payload is the legacy modified-arrow sequence
/// `ESC [ 1 ; 11 (D|C)` (modifier byte 11 = 1 base + Alt(2) + Super(8)),
/// which crossterm decodes to `KeyCode::Left/Right + ALT | SUPER` - the
/// same ALT|SUPER mask proven by `CMD_OPT_R` above, disjoint from the bare
/// `Opt+Left/Right` word-motion the shell consumes.
const CMD_OPT_LEFT_KEY: &str = "0xf702-0x180000";
const CMD_OPT_LEFT_HEX: &str = "0x1b 0x5b 0x31 0x3b 0x31 0x31 0x44";
const CMD_OPT_RIGHT_KEY: &str = "0xf703-0x180000";
const CMD_OPT_RIGHT_HEX: &str = "0x1b 0x5b 0x31 0x3b 0x31 0x31 0x43";
/// `Cmd+Opt+Up` / `Cmd+Opt+Down` -> Add Cursor Above / Below (VS Code
/// multi-cursor). Mirrors the Cmd+Opt+Left/Right forwarders above:
/// `NSUpArrowFunctionKey` = 0xf700, `NSDownArrowFunctionKey` = 0xf701, Cmd+Opt
/// mask 0x180000. Payload `ESC [ 1 ; 11 (A|B)` decodes to `KeyCode::Up/Down +
/// ALT | SUPER`, which `handle_editor_key` routes to `add_cursor_above/below`.
const CMD_OPT_UP_KEY: &str = "0xf700-0x180000";
const CMD_OPT_UP_HEX: &str = "0x1b 0x5b 0x31 0x3b 0x31 0x31 0x41";
const CMD_OPT_DOWN_KEY: &str = "0xf701-0x180000";
const CMD_OPT_DOWN_HEX: &str = "0x1b 0x5b 0x31 0x3b 0x31 0x31 0x42";
/// `Cmd+Shift+P` -> Command Palette. Mirrors the `Cmd+Shift+F` forwarder:
/// `kVK_ANSI_P` = 0x23, char 'P' = 0x50, modifier mask 0x120000 (Cmd | Shift).
/// CSI-u `ESC [ 80 ; 10 u` (codepoint 80 = 'P', modifier 10 = Super(8) +
/// Shift(1) + 1), which crossterm decodes to `Char('p') + SUPER | SHIFT`.
const CMD_SHIFT_P_KEY: &str = "0x50-0x120000-0x23";
const CMD_SHIFT_P_HEX: &str = "0x1b 0x5b 0x38 0x30 0x3b 0x31 0x30 0x75";
/// iTerm2 menu items that own bare `Cmd+[` / `Cmd+]`, relocated so the
/// bracket forwarders above reach croft. Cmd+Opt+[ / Cmd+Opt+] keep pane
/// navigation reachable.
const PREV_PANE_MENU_KEY: &str = "Previous Pane";
const PREV_PANE_MENU_EQUIV: &str = "@~[";
const NEXT_PANE_MENU_KEY: &str = "Next Pane";
const NEXT_PANE_MENU_EQUIV: &str = "@~]";
/// GlobalKeyMap arrow keys an earlier croft build hijacked for cycling
/// (both the 2-part form iTerm2 matches and the dead 3-part form). Cycling
/// now lives on Cmd+[ / Cmd+], so these are removed on apply to hand
/// Cmd+Left/Right back to iTerm2's defaults.
const CMD_ARROW_CLEANUP_KEYS: &[&str] = &[
    "0xf702-0x300000",
    "0xf703-0x300000",
    "0xf702-0x300000-0x7b",
    "0xf703-0x300000-0x7c",
];
/// `Ctrl+Shift+J` — toggle "maximize terminal" so the editor / welcome
/// pane collapses and the terminal fills the right column. Codepoint 'J'
/// (0x4a = 74), modifier mask 0x60000 = NSEventModifierFlagControl(0x40000)
/// + NSEventModifierFlagShift(0x20000), virtualKeyCode `kVK_ANSI_J` = 0x26.
///
/// CSI-u `ESC [ 74 ; 6 u` where modifier byte 6 = 1 base + Shift(1) +
/// Control(4). Crossterm decodes it back to
/// `KeyEvent { code: Char('J'), modifiers: CONTROL | SHIFT }`, which
/// `is_terminal_maximize_key` accepts case-insensitively. Forwarded
/// defensively to mirror the Cmd-chord pattern even though Ctrl+Shift+J
/// is not bound by AppKit / iTerm2 menus today, so a future iTerm2 build
/// that adds a default cannot silently swallow the chord.
const CTRL_SHIFT_J_KEY: &str = "0x4a-0x60000-0x26";
const CTRL_SHIFT_J_HEX: &str = "0x1b 0x5b 0x37 0x34 0x3b 0x36 0x75";
/// `Cmd+F12` -> editor "Go to Implementations" (VS Code's real macOS binding).
/// Function keys use the 2-part `0x<unicode>-0x<modmask>` iTermKeystroke form
/// (like the arrow-cleanup keys), not the 3-part letter form: `NSF12FunctionKey`
/// = 0xf70f, Cmd modifier mask 0x100000. The bare F12 family (plain / Shift /
/// Ctrl) reaches croft as escape sequences without a forwarder, but Cmd+F12 is
/// captured by macOS, so this forwarder is required.
///
/// Payload is the legacy modified-function-key sequence `ESC [ 24 ; 9 ~`
/// (`24` = F12, modifier byte 9 = 1 base + Super(8)), which crossterm decodes
/// back to `KeyEvent { code: F(12), modifiers: SUPER }` via the same
/// `parse_csi_special_key_code` path that already delivers Shift+F12 as
/// `ESC [ 24 ; 2 ~`. Cmd+F12 is not bound to any default macOS / iTerm2 menu
/// item, so no NSUserKeyEquivalents relocation is needed.
const CMD_F12_KEY: &str = "0xf70f-0x100000";
const CMD_F12_HEX: &str = "0x1b 0x5b 0x32 0x34 0x3b 0x39 0x7e";
/// `Ctrl+Shift+F12` -> editor "Go to Declaration". VS Code leaves Declaration
/// unbound, so croft keeps it in the F12 navigation family; it moved here off
/// the bare `Shift+F12` it once held when Go to References (VS Code's real
/// `Shift+F12`) was added. `NSF12FunctionKey` = 0xf70f, modifier mask 0x60000 =
/// NSEventModifierFlagControl(0x40000) + NSEventModifierFlagShift(0x20000).
///
/// Payload is the legacy modified-function-key sequence `ESC [ 24 ; 6 ~`
/// (`24` = F12, modifier byte 6 = 1 base + Shift(1) + Control(4)), which
/// crossterm decodes back to `KeyEvent { code: F(12), modifiers: SHIFT |
/// CONTROL }` via the same `parse_csi_special_key_code` path that delivers the
/// rest of the family. Unlike `Cmd+F12`, macOS does not reserve this chord, so
/// iTerm2 would already emit these exact bytes natively; the forwarder is
/// installed defensively (mirroring `Ctrl+Shift+J`) so a future iTerm2 default
/// cannot silently swallow the chord. No NSUserKeyEquivalents relocation needed.
const CTRL_SHIFT_F12_KEY: &str = "0xf70f-0x60000";
const CTRL_SHIFT_F12_HEX: &str = "0x1b 0x5b 0x32 0x34 0x3b 0x36 0x7e";
/// `Cmd+B` — toggle the primary side bar (croft's `is_sidebar_toggle_key`),
/// matching VS Code's "View: Toggle Primary Side Bar" macOS default. iTerm2
/// does not bind the bare chord to any default menu item (newer builds only
/// *suggest* Cmd+B as an opt-in leader key, which the user must enable), so
/// like Cmd+F12 no NSUserKeyEquivalents relocation is needed for this
/// forwarder to win. Codepoint 'b' (0x62 = 98), virtualKeyCode `kVK_ANSI_B`
/// = 0x0b, modifier mask 0x100000 (Cmd). CSI-u `ESC [ 98 ; 9 u` (modifier
/// byte 9 = 1 base + Super(8)), which crossterm decodes back to
/// `KeyEvent { code: Char('b'), modifiers: SUPER }`.
const CMD_B_KEY: &str = "0x62-0x100000-0xb";
const CMD_B_HEX: &str = "0x1b 0x5b 0x39 0x38 0x3b 0x39 0x75";
/// `Cmd+P`: VS Code-style Quick Open file finder. macOS binds Cmd+P to the
/// standard File > Print menu item across virtually every app (iTerm2
/// included), so AppKit catches the chord at the menu layer before
/// iTerm2's `GlobalKeyMap` is consulted. The NSUserKeyEquivalents
/// override below repoints File > Print at Cmd+Opt+P so this
/// GlobalKeyMap forwarder can fire.
/// Encoding: 'p' (codepoint 0x70 = 112) with Cmd, virtualKeyCode
/// `kVK_ANSI_P` = 0x23.
const CMD_P_KEY: &str = "0x70-0x100000-0x23";
/// CSI-u `ESC [ 112 ; 9 u` (= 0x1b 0x5b '1' '1' '2' ';' '9' 'u') so
/// crossterm decodes it back to `KeyEvent { code: Char('p'), modifiers: SUPER }`.
const CMD_P_HEX: &str = "0x1b 0x5b 0x31 0x31 0x32 0x3b 0x39 0x75";
/// Menu-item title macOS / iTerm2 expose for File > Print.
const PRINT_MENU_KEY: &str = "Print";
/// Cmd+Opt+P. Unbound by default in iTerm2; gives the user a path back to
/// the Print dialog when they need it without sacrificing the file finder.
const PRINT_MENU_EQUIV: &str = "@~p";
/// `Cmd+W` — close the active editor tab (croft's `is_close_tab_key`). iTerm2's
/// File menu binds the bare chord to "Close" (`closeCurrentSession:`), which
/// closes the current session and quits iTerm2 when it is the last one; that
/// menu key-equivalent is resolved by AppKit ahead of iTerm2's GlobalKeyMap,
/// so without relocating it the chord never reaches croft. Codepoint 'w'
/// (0x77 = 119), virtualKeyCode `kVK_ANSI_W` = 0xd, modifier mask 0x100000
/// (Cmd). CSI-u `ESC [ 119 ; 9 u` (modifier byte 9 = 1 base + Super(8)), which
/// crossterm decodes back to `KeyEvent { code: Char('w'), modifiers: SUPER }`.
const CMD_W_KEY: &str = "0x77-0x100000-0xd";
const CMD_W_HEX: &str = "0x1b 0x5b 0x31 0x31 0x39 0x3b 0x39 0x75";
/// iTerm2's File > "Close" menu item title (MainMenu.xib id 1184) and the chord
/// it is relocated to so croft can claim the bare Cmd+W. Cmd+Opt+W (`@~w`) is
/// already "Close All Panes in Tab" and Cmd+Shift+W is "Close Terminal Window",
/// so the close-session action moves to Cmd+Ctrl+W (`@^w`), unbound by default
/// and mirroring the `New Tab -> @^t` relocation.
const CLOSE_SESSION_MENU_KEY: &str = "Close";
const CLOSE_SESSION_MENU_EQUIV: &str = "@^w";
/// Cmd+0..Cmd+9 forward as CSI-u so the editor's vim chord can use them
/// as count digits (e.g. `Cmd+5 Cmd+g g` jumps to line 5). Without these,
/// iTerm2 catches Cmd+digit for its own "Select Tab N" action and croft
/// never sees the keystroke. Mac virtual key codes for the number row
/// are non-contiguous: `kVK_ANSI_1=0x12 … kVK_ANSI_5=0x17`, with 0 last
/// at `kVK_ANSI_0=0x1d`.
const CMD_DIGIT_CHORDS: &[(&str, &str)] = &[
    ("0x30-0x100000-0x1d", "0x1b 0x5b 0x34 0x38 0x3b 0x39 0x75"),
    ("0x31-0x100000-0x12", "0x1b 0x5b 0x34 0x39 0x3b 0x39 0x75"),
    ("0x32-0x100000-0x13", "0x1b 0x5b 0x35 0x30 0x3b 0x39 0x75"),
    ("0x33-0x100000-0x14", "0x1b 0x5b 0x35 0x31 0x3b 0x39 0x75"),
    ("0x34-0x100000-0x15", "0x1b 0x5b 0x35 0x32 0x3b 0x39 0x75"),
    ("0x35-0x100000-0x17", "0x1b 0x5b 0x35 0x33 0x3b 0x39 0x75"),
    ("0x36-0x100000-0x16", "0x1b 0x5b 0x35 0x34 0x3b 0x39 0x75"),
    ("0x37-0x100000-0x1a", "0x1b 0x5b 0x35 0x35 0x3b 0x39 0x75"),
    ("0x38-0x100000-0x1c", "0x1b 0x5b 0x35 0x36 0x3b 0x39 0x75"),
    ("0x39-0x100000-0x19", "0x1b 0x5b 0x35 0x37 0x3b 0x39 0x75"),
];
/// Top-level plist key that disables iTerm2's mouse-reporting-frustration
/// banner. Backed by iTermAdvancedSettingsModel's
/// `noSyncNeverAskAboutMouseReportingFrustration` property whose plist
/// storage key is PascalCase per `DEFINE_SETTABLE_BOOL`.
const MOUSE_REPORTING_FRUSTRATION_KEY: &str = "NoSyncNeverAskAboutMouseReportingFrustration";
/// `Cmd+Shift+/`. Character is the *shifted* glyph `?` (0x3f), modifiers
/// are Cmd+Shift (0x120000), virtualKeyCode is `kVK_ANSI_Slash` (0x2c).
/// macOS reserves this chord for the Help-menu Search field (Apple
/// writes it as Cmd+?). The `NSUserKeyEquivalents` override below
/// repoints "Show Help Menu" away from Cmd+?, freeing the chord so this
/// GlobalKeyMap forwarder can fire.
const CMD_SHIFT_SLASH_KEY: &str = "0x3f-0x120000-0x2c";
/// CSI-u sequence `ESC [ 63 ; 10 u` = '?' (codepoint 63) with kitty
/// modifier byte 10 (= 1 base + Shift(1) + Super(8)). Crossterm decodes
/// this back to `KeyEvent { code: Char('?'), modifiers: SHIFT | SUPER }`,
/// which `is_tree_make_parent_root_key` accepts via its `Char('?')`
/// branch.
const CMD_SHIFT_SLASH_HEX: &str = "0x1b 0x5b 0x36 0x33 0x3b 0x31 0x30 0x75";
const FIND_GLOBALLY_MENU_EQUIV: &str = "@~^f";
const FIND_MENU_EQUIV: &str = "@~f";
/// macOS calls the Help-menu Cmd+? Search shortcut "Show Help Menu" in
/// its keyboard-shortcuts UI; that is the menu-item title NSUserKeyEquivalents
/// recognizes. Setting it here in iTerm2's plist overrides the system
/// binding for iTerm2 only.
const HELP_MENU_KEY: &str = "Show Help Menu";
/// Cmd+Opt+? (i.e., Cmd+Opt+Shift+/). Picked because it is unbound by
/// default on macOS and Opt being held neutralizes croft's Explorer
/// predicate (which rejects ALT), so the chord can no longer fall back
/// into croft's parent-folder action either.
const HELP_MENU_EQUIV: &str = "@~?";

// (H) Pointer key earlier croft versions wrote to route Cmd+click to Go to
// Definition via a bracketed-paste sentinel; that transport never fired under
// croft's mouse reporting. Mouse reports carry no Cmd bit at all, so
// go-to-definition keys on Ctrl+click and ALT belongs to the editor's
// multi-cursor. Re-running setup purges any stale copy.
const STALE_GOTO_DEF_POINTER_KEY: &str = "Button,0,1,c,";

/// CSI-u Send-Hex payloads exposed to the crate for the Ghostty keybind
/// generator (`crate::ghostty`). The byte sequences are defined exactly once,
/// in the const block above, as the single source of truth for what each croft
/// chord puts on the wire. Ghostty emits the identical bytes through its `csi:`
/// keybind action, so croft receives byte-for-byte the same input under Ghostty
/// as it does under iTerm2's GlobalKeyMap forwarders. A descendant module may
/// read its parent's private items, so these `pub(crate)` aliases copy the
/// private payloads without widening any original const's visibility.
pub(crate) mod payloads {
    pub(crate) const CMD_A_HEX: &str = super::CMD_A_HEX;
    pub(crate) const CMD_B_HEX: &str = super::CMD_B_HEX;
    pub(crate) const CMD_BACKSLASH_HEX: &str = super::CMD_BACKSLASH_HEX;
    pub(crate) const CMD_SHIFT_BACKSLASH_HEX: &str = super::CMD_SHIFT_BACKSLASH_HEX;
    pub(crate) const CMD_OPT_BACKSLASH_HEX: &str = super::CMD_OPT_BACKSLASH_HEX;
    pub(crate) const CMD_OPT_SHIFT_S_HEX: &str = super::CMD_OPT_SHIFT_S_HEX;
    pub(crate) const CMD_OPT_SHIFT_T_HEX: &str = super::CMD_OPT_SHIFT_T_HEX;
    pub(crate) const CMD_OPT_SHIFT_N_HEX: &str = super::CMD_OPT_SHIFT_N_HEX;
    pub(crate) const CMD_OPT_SHIFT_J_HEX: &str = super::CMD_OPT_SHIFT_J_HEX;
    pub(crate) const CMD_OPT_SHIFT_U_HEX: &str = super::CMD_OPT_SHIFT_U_HEX;
    pub(crate) const CMD_OPT_SHIFT_L_HEX: &str = super::CMD_OPT_SHIFT_L_HEX;
    pub(crate) const CMD_OPT_SHIFT_C_HEX: &str = super::CMD_OPT_SHIFT_C_HEX;
    pub(crate) const CMD_OPT_SHIFT_A_HEX: &str = super::CMD_OPT_SHIFT_A_HEX;
    pub(crate) const CMD_OPT_SHIFT_D_HEX: &str = super::CMD_OPT_SHIFT_D_HEX;
    pub(crate) const CMD_OPT_SHIFT_W_HEX: &str = super::CMD_OPT_SHIFT_W_HEX;
    pub(crate) const CMD_OPT_SHIFT_F_HEX: &str = super::CMD_OPT_SHIFT_F_HEX;
    pub(crate) const CMD_C_HEX: &str = super::CMD_C_HEX;
    pub(crate) const CMD_D_HEX: &str = super::CMD_D_HEX;
    pub(crate) const CMD_DOT_HEX: &str = super::CMD_DOT_HEX;
    pub(crate) const CMD_DIGIT_CHORDS: &[(&str, &str)] = super::CMD_DIGIT_CHORDS;
    pub(crate) const CMD_E_HEX: &str = super::CMD_E_HEX;
    pub(crate) const CMD_F12_HEX: &str = super::CMD_F12_HEX;
    pub(crate) const CMD_F_HEX: &str = super::CMD_F_HEX;
    pub(crate) const CMD_G_HEX: &str = super::CMD_G_HEX;
    pub(crate) const CMD_K_HEX: &str = super::CMD_K_HEX;
    pub(crate) const CMD_LBRACKET_HEX: &str = super::CMD_LBRACKET_HEX;
    pub(crate) const CMD_O_HEX: &str = super::CMD_O_HEX;
    pub(crate) const CMD_OPT_LEFT_HEX: &str = super::CMD_OPT_LEFT_HEX;
    pub(crate) const CMD_OPT_R_HEX: &str = super::CMD_OPT_R_HEX;
    pub(crate) const CMD_OPT_C_HEX: &str = super::CMD_OPT_C_HEX;
    pub(crate) const CMD_OPT_M_HEX: &str = super::CMD_OPT_M_HEX;
    pub(crate) const CMD_OPT_F_HEX: &str = super::CMD_OPT_F_HEX;
    pub(crate) const CMD_OPT_ENTER_HEX: &str = super::CMD_OPT_ENTER_HEX;
    pub(crate) const CMD_OPT_RIGHT_HEX: &str = super::CMD_OPT_RIGHT_HEX;
    pub(crate) const CMD_OPT_UP_HEX: &str = super::CMD_OPT_UP_HEX;
    pub(crate) const CMD_OPT_DOWN_HEX: &str = super::CMD_OPT_DOWN_HEX;
    pub(crate) const CMD_SHIFT_P_HEX: &str = super::CMD_SHIFT_P_HEX;
    pub(crate) const CMD_P_HEX: &str = super::CMD_P_HEX;
    pub(crate) const CMD_R_HEX: &str = super::CMD_R_HEX;
    pub(crate) const CMD_RBRACKET_HEX: &str = super::CMD_RBRACKET_HEX;
    pub(crate) const CMD_S_HEX: &str = super::CMD_S_HEX;
    pub(crate) const CMD_SHIFT_B_HEX: &str = super::CMD_SHIFT_B_HEX;
    pub(crate) const CMD_SHIFT_D_HEX: &str = super::CMD_SHIFT_D_HEX;
    pub(crate) const CMD_SHIFT_E_HEX: &str = super::CMD_SHIFT_E_HEX;
    pub(crate) const CMD_SHIFT_ENTER_HEX: &str = super::CMD_SHIFT_ENTER_HEX;
    pub(crate) const CMD_SHIFT_F_HEX: &str = super::CMD_SHIFT_F_HEX;
    pub(crate) const CMD_SHIFT_G_HEX: &str = super::CMD_SHIFT_G_HEX;
    pub(crate) const CMD_SHIFT_L_HEX: &str = super::CMD_SHIFT_L_HEX;
    pub(crate) const CMD_SHIFT_N_HEX: &str = super::CMD_SHIFT_N_HEX;
    pub(crate) const CMD_SHIFT_O_HEX: &str = super::CMD_SHIFT_O_HEX;
    pub(crate) const CMD_SHIFT_R_HEX: &str = super::CMD_SHIFT_R_HEX;
    pub(crate) const CMD_SHIFT_S_HEX: &str = super::CMD_SHIFT_S_HEX;
    pub(crate) const CMD_SHIFT_SLASH_HEX: &str = super::CMD_SHIFT_SLASH_HEX;
    pub(crate) const CMD_SHIFT_T_HEX: &str = super::CMD_SHIFT_T_HEX;
    pub(crate) const CMD_SHIFT_V_HEX: &str = super::CMD_SHIFT_V_HEX;
    pub(crate) const CMD_SHIFT_X_HEX: &str = super::CMD_SHIFT_X_HEX;
    pub(crate) const CMD_SLASH_HEX: &str = super::CMD_SLASH_HEX;
    pub(crate) const CMD_T_HEX: &str = super::CMD_T_HEX;
    pub(crate) const CMD_W_HEX: &str = super::CMD_W_HEX;
    pub(crate) const CMD_X_HEX: &str = super::CMD_X_HEX;
    pub(crate) const CMD_Y_HEX: &str = super::CMD_Y_HEX;
    pub(crate) const CMD_Z_HEX: &str = super::CMD_Z_HEX;
    pub(crate) const CMD_SHIFT_Z_HEX: &str = super::CMD_SHIFT_Z_HEX;
    pub(crate) const CTRL_SHIFT_F12_HEX: &str = super::CTRL_SHIFT_F12_HEX;
    pub(crate) const CTRL_SHIFT_J_HEX: &str = super::CTRL_SHIFT_J_HEX;
}

/// PostScript name iTerm2 stores in `Normal Font` and `Non Ascii Font`.
/// Format is "<PostScriptName> <size>".
pub fn primary_font_value(font_ps: &str, size: u32) -> String {
    format!("{font_ps} {size}")
}

#[derive(Debug, thiserror::Error)]
pub enum ITerm2Error {
    #[error("iTerm2 plist not found at {0}; install and launch iTerm2 first")]
    PlistMissing(PathBuf),
    #[error("iTerm2 plist has no `Default Bookmark Guid` (the default profile)")]
    NoDefaultGuid,
    #[error("iTerm2 plist has no profile matching the default GUID `{0}`")]
    NoMatchingProfile(String),
    #[error("iTerm2 plist top level is not a dictionary")]
    NotADictionary,
    #[error("`New Bookmarks` is missing or not an array")]
    NoBookmarksArray,
}

/// Apply font settings to the *default profile* in an iTerm2 plist value.
/// Pure function: no I/O, mutates the value in place.
pub fn apply_font_settings(
    plist: &mut Value,
    primary_font_ps: &str,
    nonascii_font_ps: &str,
    size: u32,
) -> Result<(), ITerm2Error> {
    let dict = plist
        .as_dictionary_mut()
        .ok_or(ITerm2Error::NotADictionary)?;

    let profile = default_profile_mut(dict)?;

    set_string(
        profile,
        "Normal Font",
        primary_font_value(primary_font_ps, size),
    );
    set_string(
        profile,
        "Non Ascii Font",
        primary_font_value(nonascii_font_ps, size),
    );
    profile.insert("Use Non-ASCII Font".into(), Value::Boolean(true));
    profile.insert("Non-ASCII Anti Aliased".into(), Value::Boolean(true));
    Ok(())
}

/// Apply the iTerm2-side pieces needed for Croft's macOS keyboard gestures.
/// Installs the Cmd+Shift+F search shortcut globally, frees the matching
/// menu equivalent so macOS doesn't eat it for "Find Globally...", and
/// scrubs any legacy Cmd+V or Paste-menu remappings that older croft
/// versions wrote in. Cmd+V is intentionally **not** bound: leaving it on
/// the default Edit menu shortcut routes through iTerm2's native Paste
/// action, which emits a bracketed-paste sequence carrying the local
/// clipboard. That works identically in local and SSH'd croft sessions
/// (croft handles `Event::Paste`); intercepting Cmd+V as a key event
/// instead — the previous design — broke paste over SSH because the
/// remote process has no path to the local Mac clipboard.
pub fn apply_croft_key_settings(plist: &mut Value) -> Result<(), ITerm2Error> {
    let dict = plist
        .as_dictionary_mut()
        .ok_or(ITerm2Error::NotADictionary)?;

    let menu = dict_entry_mut(dict, "NSUserKeyEquivalents");
    set_string(
        menu,
        "Find Globally...",
        FIND_GLOBALLY_MENU_EQUIV.to_string(),
    );
    // Relocate iTerm's "Find" menu item off Cmd+F so the GlobalKeyMap
    // binding below wins reliably. Users that still want iTerm's
    // in-pane find can use Cmd+Opt+F.
    set_string(menu, "Find...", FIND_MENU_EQUIV.to_string());
    // Reclaim Cmd+Shift+/ from the macOS Help-menu Search field.
    // AppKit binds Cmd+? to the Help menu at the app level, ahead of
    // iTerm2's GlobalKeyMap; without this override the chord opens
    // Help instead of reaching croft. Pointing "Show Help Menu" at
    // Cmd+Opt+? leaves Help reachable on a chord croft does not use.
    set_string(menu, HELP_MENU_KEY, HELP_MENU_EQUIV.to_string());
    // Relocate iTerm2's Edit menu items off the standard Cmd+letter
    // shortcuts so croft's terminal-pane Cmd+C (copy via OSC 52),
    // editor Cmd+S / Cmd+X / Cmd+Z, and Source Control / editor Cmd+A
    // can all reach their handlers. Without this, even after stripping
    // the profile-level Send-Hex bindings below, AppKit's standard
    // Edit menu would still claim the chord at the menu layer.
    set_string(menu, "Copy", "@~c".to_string());
    set_string(menu, "Cut", "@~x".to_string());
    set_string(menu, "Select All", "@~a".to_string());
    set_string(menu, "Undo", "@~z".to_string());
    menu.remove("Paste");
    // Relocate iTerm2 menu items that would otherwise catch the editor's
    // vim chords at the menu-bar layer. Each is moved to Cmd+Opt+<key>
    // so the original action stays reachable, but the bare Cmd+<key>
    // chord is freed for the GlobalKeyMap forwarder below.
    set_string(
        menu,
        "Split Vertically with Same Profile",
        "@~d".to_string(),
    );
    set_string(
        menu,
        "Split Horizontally with Same Profile",
        "@~D".to_string(),
    );
    set_string(menu, "Find Next", "@~g".to_string());
    set_string(menu, "Find Previous", "@~G".to_string());
    set_string(menu, "Jump to Selection", "@~y".to_string());
    // Relocate File > Print off Cmd+P so croft's Quick Open finder can
    // receive the chord. Without this AppKit's app-level Print menu
    // binding opens the macOS Print dialog before iTerm2's GlobalKeyMap
    // is consulted. Cmd+Opt+P keeps Print reachable on a chord croft
    // does not use.
    set_string(menu, PRINT_MENU_KEY, PRINT_MENU_EQUIV.to_string());
    // Relocate iTerm2's File > "Close" (closeCurrentSession:) off bare Cmd+W so
    // croft's editor close-tab chord reaches the app instead of closing the
    // iTerm2 session (which quits iTerm2 when it is the last one). The menu
    // key-equivalent layer is consulted before GlobalKeyMap, so the forwarder
    // alone is not enough; the menu item must move. Cmd+Opt+W is already
    // iTerm2's "Close All Panes in Tab" and Cmd+Shift+W is "Close Terminal
    // Window", so the close-session action moves to Cmd+Ctrl+W, unbound by
    // default and reachable.
    set_string(
        menu,
        CLOSE_SESSION_MENU_KEY,
        CLOSE_SESSION_MENU_EQUIV.to_string(),
    );
    // Relocate AppKit's Edit > Find > "Use Selection for Find" off Cmd+E so
    // croft's native-modal (vim) toggle can receive the chord. Like Cmd+F in
    // the same submenu, the bare chord is otherwise claimed at the menu layer
    // before iTerm2's GlobalKeyMap is consulted. Cmd+Opt+E keeps the
    // find-from-selection action reachable on a chord croft does not use.
    set_string(
        menu,
        USE_SELECTION_FOR_FIND_MENU_KEY,
        USE_SELECTION_FOR_FIND_MENU_EQUIV.to_string(),
    );
    // Relocate iTerm2's Edit > "Clear Buffer" off bare Cmd+K so croft's Cmd+K
    // chord leader reaches the app. The menu key-equivalent layer is consulted
    // before GlobalKeyMap, so the forwarder alone is not enough; the item must
    // move. Cmd+Opt+K keeps Clear Buffer reachable on a chord croft does not use.
    set_string(
        menu,
        CLEAR_BUFFER_MENU_KEY,
        CLEAR_BUFFER_MENU_EQUIV.to_string(),
    );
    // Relocate iTerm2's "Restore Closed Session" off Cmd+Shift+T so
    // croft's terminal-focus chord can claim it. Cmd+Opt+Shift+T keeps
    // the iTerm2 action reachable on a chord croft does not use.
    set_string(menu, "Restore Closed Session", "@~T".to_string());
    // Relocate Edit > Paste Special > "Paste Selection" off Cmd+Shift+V so
    // croft's Markdown preview toggle can claim it. Cmd+Opt+Shift+V keeps
    // the iTerm2 action reachable on a chord croft does not use.
    set_string(menu, "Paste Selection", "@~V".to_string());
    // Relocate iTerm2's "Password Manager" off Cmd+Opt+F so croft's
    // Replace in File chord can claim it. Cmd+Opt+Ctrl+F keeps the
    // password manager reachable on a chord croft does not use.
    set_string(menu, "Password Manager", "@~^f".to_string());
    // Relocate iTerm2's "Show Toolbelt" off Cmd+Shift+B so croft's Run
    // Build Task chord can claim it. Cmd+Ctrl+B keeps the toolbelt
    // reachable on a chord croft does not use.
    set_string(menu, "Show Toolbelt", "@^b".to_string());
    // Relocate iTerm2's "New Tab" off Cmd+T so croft's new-terminal chord
    // can claim it. Cmd+Ctrl+T keeps the iTerm2 action reachable and stays
    // clear of the Cmd+Opt+T "New Tab Next to Current Tab" alternate.
    set_string(menu, NEW_TAB_MENU_KEY, NEW_TAB_MENU_EQUIV.to_string());
    // Relocate iTerm2's "Previous Pane" / "Next Pane" off Cmd+[ / Cmd+] so
    // croft's terminal-cycle chords can claim them. Cmd+Opt+[ / Cmd+Opt+]
    // keep pane navigation reachable.
    set_string(menu, PREV_PANE_MENU_KEY, PREV_PANE_MENU_EQUIV.to_string());
    set_string(menu, NEXT_PANE_MENU_KEY, NEXT_PANE_MENU_EQUIV.to_string());
    // iTerm2's Window menu binds Cmd+1..Cmd+9 to Select Tab. Move each
    // to Cmd+Opt+digit so croft can capture Cmd+digit as a vim count.
    for (i, label) in [
        "Select Tab 1",
        "Select Tab 2",
        "Select Tab 3",
        "Select Tab 4",
        "Select Tab 5",
        "Select Tab 6",
        "Select Tab 7",
        "Select Tab 8",
        "Select Tab 9",
    ]
    .iter()
    .enumerate()
    {
        set_string(menu, label, format!("@~{}", i + 1));
    }

    // Sweep the user-keybinding forwarders recorded by the PREVIOUS run
    // before any inserts: a chord deleted from keybindings.json must
    // release its Cmd chord in iTerm2 (a leftover entry keeps typing
    // escape bytes into every iTerm2 session forever, with no croft UI to
    // undo it), and sweeping first means a stale key that collides with a
    // built-in simply gets re-inserted below.
    remove_stale_user_forwarders(dict);
    let global = dict_entry_mut(dict, "GlobalKeyMap");
    global.insert(CMD_SHIFT_F_KEY.into(), send_hex_action(CMD_SHIFT_F_HEX, 0));
    // Explorer shortcuts: forward Cmd+F / Cmd+R / Cmd+/ to croft as
    // CSI-u sequences. Croft handles them only when the Explorer pane
    // is focused; elsewhere the keys are passed through as raw input,
    // which means giving up iTerm's own actions on those chords
    // (Find / Clear Buffer) while croft is running. The user agreed.
    global.insert(CMD_F_KEY.into(), send_hex_action(CMD_F_HEX, 0));
    global.insert(CMD_R_KEY.into(), send_hex_action(CMD_R_HEX, 0));
    global.insert(CMD_SLASH_KEY.into(), send_hex_action(CMD_SLASH_HEX, 0));
    global.insert(
        CMD_SHIFT_SLASH_KEY.into(),
        send_hex_action(CMD_SHIFT_SLASH_HEX, 0),
    );
    global.insert(
        CMD_SHIFT_ENTER_KEY.into(),
        send_hex_action(CMD_SHIFT_ENTER_HEX, 0),
    );
    // Mac-style Cmd+letter chords: forward each as a CSI-u sequence so
    // AppKit's NSResponder defaults (copy: / cut: / selectAll: / undo:)
    // don't consume them at the textview layer. Without these, even
    // with the Edit menu items relocated via NSUserKeyEquivalents above,
    // Cmd+C still never reaches croft because PTYTextView still answers
    // copy: from the default key bindings dictionary.
    for (key, hex) in [
        (CMD_A_KEY, CMD_A_HEX),
        (CMD_C_KEY, CMD_C_HEX),
        (CMD_S_KEY, CMD_S_HEX),
        (CMD_X_KEY, CMD_X_HEX),
        (CMD_Z_KEY, CMD_Z_HEX),
        (CMD_SHIFT_Z_KEY, CMD_SHIFT_Z_HEX),
        (CMD_D_KEY, CMD_D_HEX),
        (CMD_G_KEY, CMD_G_HEX),
        (CMD_K_KEY, CMD_K_HEX),
        (CMD_Y_KEY, CMD_Y_HEX),
        (CMD_O_KEY, CMD_O_HEX),
        (CMD_E_KEY, CMD_E_HEX),
        (CMD_P_KEY, CMD_P_HEX),
        (CMD_W_KEY, CMD_W_HEX),
        (CMD_SHIFT_G_KEY, CMD_SHIFT_G_HEX),
        (CMD_SHIFT_O_KEY, CMD_SHIFT_O_HEX),
        (CMD_SHIFT_E_KEY, CMD_SHIFT_E_HEX),
        (CMD_SHIFT_S_KEY, CMD_SHIFT_S_HEX),
        (CMD_SHIFT_D_KEY, CMD_SHIFT_D_HEX),
        (CMD_SHIFT_R_KEY, CMD_SHIFT_R_HEX),
        (CMD_SHIFT_X_KEY, CMD_SHIFT_X_HEX),
        (CMD_SHIFT_V_KEY, CMD_SHIFT_V_HEX),
        (CMD_SHIFT_L_KEY, CMD_SHIFT_L_HEX),
        (CMD_SHIFT_N_KEY, CMD_SHIFT_N_HEX),
        (CMD_SHIFT_T_KEY, CMD_SHIFT_T_HEX),
        (CMD_SHIFT_K_KEY, CMD_SHIFT_K_HEX),
        (CMD_SHIFT_B_KEY, CMD_SHIFT_B_HEX),
        (CMD_T_KEY, CMD_T_HEX),
        (CMD_LBRACKET_KEY, CMD_LBRACKET_HEX),
        (CMD_RBRACKET_KEY, CMD_RBRACKET_HEX),
        (CMD_OPT_R_KEY, CMD_OPT_R_HEX),
        (CMD_OPT_C_KEY, CMD_OPT_C_HEX),
        (CMD_OPT_M_KEY, CMD_OPT_M_HEX),
        (CMD_OPT_F_KEY, CMD_OPT_F_HEX),
        (CMD_OPT_ENTER_KEY, CMD_OPT_ENTER_HEX),
        (CTRL_SHIFT_J_KEY, CTRL_SHIFT_J_HEX),
        (CMD_F12_KEY, CMD_F12_HEX),
        (CTRL_SHIFT_F12_KEY, CTRL_SHIFT_F12_HEX),
        (CMD_B_KEY, CMD_B_HEX),
        (CMD_BACKSLASH_KEY, CMD_BACKSLASH_HEX),
        (CMD_SHIFT_BACKSLASH_KEY, CMD_SHIFT_BACKSLASH_HEX),
        (CMD_OPT_BACKSLASH_KEY, CMD_OPT_BACKSLASH_HEX),
        (CMD_OPT_SHIFT_S_KEY, CMD_OPT_SHIFT_S_HEX),
        (CMD_OPT_SHIFT_T_KEY, CMD_OPT_SHIFT_T_HEX),
        (CMD_OPT_SHIFT_N_KEY, CMD_OPT_SHIFT_N_HEX),
        (CMD_OPT_SHIFT_J_KEY, CMD_OPT_SHIFT_J_HEX),
        (CMD_OPT_SHIFT_U_KEY, CMD_OPT_SHIFT_U_HEX),
        (CMD_OPT_SHIFT_L_KEY, CMD_OPT_SHIFT_L_HEX),
        (CMD_OPT_SHIFT_C_KEY, CMD_OPT_SHIFT_C_HEX),
        (CMD_OPT_SHIFT_A_KEY, CMD_OPT_SHIFT_A_HEX),
        (CMD_OPT_SHIFT_D_KEY, CMD_OPT_SHIFT_D_HEX),
        (CMD_OPT_SHIFT_W_KEY, CMD_OPT_SHIFT_W_HEX),
        (CMD_OPT_SHIFT_F_KEY, CMD_OPT_SHIFT_F_HEX),
        (CMD_OPT_LEFT_KEY, CMD_OPT_LEFT_HEX),
        (CMD_OPT_RIGHT_KEY, CMD_OPT_RIGHT_HEX),
        (CMD_OPT_UP_KEY, CMD_OPT_UP_HEX),
        (CMD_OPT_DOWN_KEY, CMD_OPT_DOWN_HEX),
        (CMD_SHIFT_P_KEY, CMD_SHIFT_P_HEX),
        (CMD_DOT_KEY, CMD_DOT_HEX),
    ] {
        global.insert(key.into(), send_hex_action(hex, 0));
    }
    for (key, hex) in CMD_DIGIT_CHORDS {
        global.insert((*key).into(), send_hex_action(hex, 0));
    }
    global.remove(CMD_V_KEY);
    for key in CMD_ARROW_CLEANUP_KEYS {
        global.remove(key);
    }
    // User keybindings: forward each reserved-Cmd chord the same way the
    // built-ins are, so a rebind in keybindings.json actually reaches croft in
    // iTerm2. Added last so a user chord that collides with a built-in wins.
    insert_user_keymap_forwarders(
        dict,
        &crate::keymap::Keymap::load(&crate::keymap::keybindings_path()),
    );

    dict.insert(MOUSE_REPORTING_FRUSTRATION_KEY.into(), Value::Boolean(true));

    let bookmarks = dict
        .get_mut("New Bookmarks")
        .and_then(|v| v.as_array_mut())
        .ok_or(ITerm2Error::NoBookmarksArray)?;
    for profile in bookmarks.iter_mut().filter_map(|v| v.as_dictionary_mut()) {
        if let Some(Value::Dictionary(profile_keys)) = profile.get_mut("Keyboard Map") {
            profile_keys.remove(CMD_V_KEY);
        }
    }

    let drop_pointer_actions =
        if let Some(Value::Dictionary(pointer)) = dict.get_mut("PointerActions") {
            pointer.remove(STALE_GOTO_DEF_POINTER_KEY);
            pointer.is_empty()
        } else {
            false
        };
    if drop_pointer_actions {
        dict.remove("PointerActions");
    }

    Ok(())
}

fn set_string(dict: &mut Dictionary, key: &str, value: String) {
    dict.insert(key.into(), Value::String(value));
}

fn default_profile_mut(dict: &mut Dictionary) -> Result<&mut Dictionary, ITerm2Error> {
    let default_guid = dict
        .get("Default Bookmark Guid")
        .and_then(|v| v.as_string())
        .ok_or(ITerm2Error::NoDefaultGuid)?
        .to_string();

    let bookmarks = dict
        .get_mut("New Bookmarks")
        .and_then(|v| v.as_array_mut())
        .ok_or(ITerm2Error::NoBookmarksArray)?;

    bookmarks
        .iter_mut()
        .filter_map(|v| v.as_dictionary_mut())
        .find(|d| d.get("Guid").and_then(|g| g.as_string()) == Some(&default_guid))
        .ok_or(ITerm2Error::NoMatchingProfile(default_guid))
}

fn dict_entry_mut<'a>(dict: &'a mut Dictionary, key: &str) -> &'a mut Dictionary {
    if !matches!(dict.get(key), Some(Value::Dictionary(_))) {
        dict.insert(key.into(), Value::Dictionary(Dictionary::new()));
    }
    dict.get_mut(key)
        .and_then(|v| v.as_dictionary_mut())
        .expect("dictionary value was just inserted")
}

/// Root-plist ledger of the user-keybinding forwarders installed by the
/// previous `setup-iterm2` run (key → hex text), so the next run can remove
/// entries whose chords have since left keybindings.json.
const USER_FORWARDER_LEDGER_KEY: &str = "Croft User Keymap Forwarders";

/// Remove every `GlobalKeyMap` forwarder the previous run recorded in the
/// ledger, but only while its value is still the exact forwarder croft
/// installed - an entry the user has since rewired in iTerm2 prefs is
/// theirs now and must survive. Runs before ANY forwarder inserts, so a
/// stale user chord that collides with a built-in loses only its user
/// entry, not the built-in.
fn remove_stale_user_forwarders(dict: &mut Dictionary) {
    let stale: Vec<(String, String)> = dict
        .get(USER_FORWARDER_LEDGER_KEY)
        .and_then(|v| v.as_dictionary())
        .map(|d| {
            d.iter()
                .filter_map(|(k, v)| Some((k.clone(), v.as_string()?.to_string())))
                .collect()
        })
        .unwrap_or_default();
    if stale.is_empty() {
        return;
    }
    let global = dict_entry_mut(dict, "GlobalKeyMap");
    for (key, hex) in &stale {
        if global.get(key) == Some(&send_hex_action(hex, 0)) {
            global.remove(key);
        }
    }
}

/// Install a `GlobalKeyMap` "Send Hex Code" forwarder for every user-bound
/// chord that needs one (see [`crate::keymap::Chord::iterm2_forwarder`]),
/// and record key → hex in the root-plist ledger for the next run's
/// stale sweep.
fn insert_user_keymap_forwarders(dict: &mut Dictionary, keymap: &crate::keymap::Keymap) {
    let mut installed = Dictionary::new();
    let global = dict_entry_mut(dict, "GlobalKeyMap");
    for chord in keymap.chords() {
        if let Some((key, hex)) = chord.iterm2_forwarder() {
            installed.insert(key.clone(), Value::String(hex.clone()));
            global.insert(key, send_hex_action(&hex, 0));
        }
    }
    dict.insert(
        USER_FORWARDER_LEDGER_KEY.into(),
        Value::Dictionary(installed),
    );
}

fn send_hex_action(text: &str, apply_mode: i64) -> Value {
    let mut action = Dictionary::new();
    action.insert("Action".into(), Value::Integer(11.into()));
    action.insert("Apply Mode".into(), Value::Integer(apply_mode.into()));
    action.insert("Escaping".into(), Value::Integer(2.into()));
    action.insert("Text".into(), Value::String(text.to_string()));
    action.insert("Version".into(), Value::Integer(2.into()));
    Value::Dictionary(action)
}

/// Load → mutate → save the iTerm2 plist on disk.
#[cfg(test)]
pub fn install_font_settings(
    plist_path: &Path,
    primary_font_ps: &str,
    nonascii_font_ps: &str,
    size: u32,
) -> Result<()> {
    if !plist_path.exists() {
        return Err(ITerm2Error::PlistMissing(plist_path.to_path_buf()).into());
    }
    let mut value: Value = Value::from_file(plist_path)
        .with_context(|| format!("reading {}", plist_path.display()))?;
    apply_font_settings(&mut value, primary_font_ps, nonascii_font_ps, size)?;
    value
        .to_file_binary(plist_path)
        .with_context(|| format!("writing {}", plist_path.display()))?;
    Ok(())
}

/// Load → mutate → save every iTerm2 setting Croft needs: font fallback plus
/// Cmd+Shift+F and Search paste behavior.
pub fn install_croft_settings(
    plist_path: &Path,
    primary_font_ps: &str,
    nonascii_font_ps: &str,
    size: u32,
) -> Result<()> {
    if !plist_path.exists() {
        return Err(ITerm2Error::PlistMissing(plist_path.to_path_buf()).into());
    }
    let mut value: Value = Value::from_file(plist_path)
        .with_context(|| format!("reading {}", plist_path.display()))?;
    apply_font_settings(&mut value, primary_font_ps, nonascii_font_ps, size)?;
    apply_croft_key_settings(&mut value)?;
    value
        .to_file_binary(plist_path)
        .with_context(|| format!("writing {}", plist_path.display()))?;
    Ok(())
}

pub fn default_plist_path() -> PathBuf {
    let home = std::env::var_os("HOME")
        .map(PathBuf::from)
        .unwrap_or_default();
    home.join(ITERM2_PLIST_REL)
}

#[cfg(test)]
mod tests {
    use super::*;
    use plist::Value;

    #[test]
    fn user_keymap_chord_is_forwarded_into_the_global_key_map() {
        // A user rebind of an otherwise-unbound Cmd chord must install a CSI-u
        // forwarder, or the chord is eaten by macOS and never reaches croft.
        let mut root = Dictionary::new();
        let km = crate::keymap::Keymap::from_json(
            r#"[{ "key": "cmd+j", "command": "toggle_terminal" }]"#,
        );
        insert_user_keymap_forwarders(&mut root, &km);
        // cmd+j: 'j' = 0x6a, Cmd mask 0x100000, kVK_ANSI_J = 0x26.
        assert!(
            dict_entry_mut(&mut root, "GlobalKeyMap").contains_key("0x6a-0x100000-0x26"),
            "the user's cmd+j binding must be forwarded so it reaches croft in iTerm2"
        );
    }

    /// A chord deleted from keybindings.json must release its Cmd chord in
    /// iTerm2 on the next setup run: the previous run's forwarder otherwise
    /// keeps typing escape bytes into every iTerm2 session forever, with no
    /// croft UI to undo it. The root-plist ledger records what was
    /// installed so the sweep removes exactly that, never a built-in.
    #[test]
    fn a_deleted_user_keybinding_releases_its_iterm2_forwarder() {
        let mut root = Dictionary::new();
        let km1 = crate::keymap::Keymap::from_json(
            r#"[{ "key": "cmd+j", "command": "toggle_terminal" }]"#,
        );
        remove_stale_user_forwarders(&mut root);
        insert_user_keymap_forwarders(&mut root, &km1);
        assert!(
            dict_entry_mut(&mut root, "GlobalKeyMap").contains_key("0x6a-0x100000-0x26"),
            "staging: run 1 installed the forwarder"
        );
        // Next run: the binding is gone from keybindings.json.
        let km2 = crate::keymap::Keymap::from_json("[]");
        remove_stale_user_forwarders(&mut root);
        insert_user_keymap_forwarders(&mut root, &km2);
        assert!(
            !dict_entry_mut(&mut root, "GlobalKeyMap").contains_key("0x6a-0x100000-0x26"),
            "the stale forwarder must be swept, not left hijacking Cmd+J in every session"
        );
    }

    /// The sweep must only remove entries croft still owns. If the user
    /// hand-edited a forwarder's action in iTerm2 after croft installed it,
    /// deleting the chord from keybindings.json must not destroy their edit.
    #[test]
    fn a_user_edited_forwarder_survives_the_stale_sweep() {
        let mut root = Dictionary::new();
        let km1 = crate::keymap::Keymap::from_json(
            r#"[{ "key": "cmd+j", "command": "toggle_terminal" }]"#,
        );
        remove_stale_user_forwarders(&mut root);
        insert_user_keymap_forwarders(&mut root, &km1);
        // The user rewires Cmd+J in iTerm2 prefs to their own action.
        let theirs = send_hex_action("0xdeadbeef", 1);
        dict_entry_mut(&mut root, "GlobalKeyMap")
            .insert("0x6a-0x100000-0x26".into(), theirs.clone());
        // Next run: the binding is gone from keybindings.json.
        let km2 = crate::keymap::Keymap::from_json("[]");
        remove_stale_user_forwarders(&mut root);
        insert_user_keymap_forwarders(&mut root, &km2);
        assert_eq!(
            dict_entry_mut(&mut root, "GlobalKeyMap").get("0x6a-0x100000-0x26"),
            Some(&theirs),
            "the sweep deleted an entry the user had rewired; it may only remove exact croft forwarders"
        );
    }

    fn synth_plist(default_guid: &str, profile_guids: &[&str]) -> Value {
        let mut bookmarks: Vec<Value> = Vec::new();
        for g in profile_guids {
            let mut d = Dictionary::new();
            d.insert("Guid".into(), Value::String((*g).to_string()));
            d.insert("Name".into(), Value::String(format!("Profile {g}")));
            bookmarks.push(Value::Dictionary(d));
        }
        let mut top = Dictionary::new();
        top.insert(
            "Default Bookmark Guid".into(),
            Value::String(default_guid.to_string()),
        );
        top.insert("New Bookmarks".into(), Value::Array(bookmarks));
        Value::Dictionary(top)
    }

    fn profile_in<'a>(plist: &'a Value, guid: &str) -> &'a Dictionary {
        plist
            .as_dictionary()
            .unwrap()
            .get("New Bookmarks")
            .unwrap()
            .as_array()
            .unwrap()
            .iter()
            .find(|p| {
                p.as_dictionary()
                    .and_then(|d| d.get("Guid"))
                    .and_then(|v| v.as_string())
                    == Some(guid)
            })
            .unwrap()
            .as_dictionary()
            .unwrap()
    }

    fn dict_in<'a>(dict: &'a Dictionary, key: &str) -> &'a Dictionary {
        dict.get(key).unwrap().as_dictionary().unwrap()
    }

    fn action_text<'a>(dict: &'a Dictionary, key: &str) -> &'a str {
        dict.get(key)
            .unwrap()
            .as_dictionary()
            .unwrap()
            .get("Text")
            .unwrap()
            .as_string()
            .unwrap()
    }

    /// The historical kitty CSI-u Cmd+V escape that older croft versions
    /// installed in iTerm2 plists. Tests use it to seed a "legacy state"
    /// fixture so we can prove `apply_croft_key_settings` cleans it up.
    const LEGACY_CMD_V_HEX: &str = "0x1b 0x5b 0x31 0x31 0x38 0x3b 0x39 0x75";

    fn seed_stale_cmd_v_mappings(plist: &mut Value) {
        let top = plist.as_dictionary_mut().unwrap();
        dict_entry_mut(top, "GlobalKeyMap")
            .insert(CMD_V_KEY.into(), send_hex_action(LEGACY_CMD_V_HEX, 0));
        let bookmarks = top
            .get_mut("New Bookmarks")
            .unwrap()
            .as_array_mut()
            .unwrap();
        for profile in bookmarks.iter_mut().filter_map(|v| v.as_dictionary_mut()) {
            dict_entry_mut(profile, "Keyboard Map")
                .insert(CMD_V_KEY.into(), send_hex_action(LEGACY_CMD_V_HEX, 0));
        }
    }

    #[test]
    fn primary_font_value_concatenates_postscript_name_and_size() {
        assert_eq!(
            primary_font_value("MesloLGSNFM-Regular", 13),
            "MesloLGSNFM-Regular 13"
        );
        assert_eq!(
            primary_font_value("FiraCodeNFM-Reg", 14),
            "FiraCodeNFM-Reg 14"
        );
    }

    #[test]
    fn apply_font_settings_writes_normal_and_nonascii_font() {
        let mut plist = synth_plist("DEFAULT-GUID", &["OTHER-GUID", "DEFAULT-GUID"]);
        apply_font_settings(&mut plist, "MesloLGSNFM-Regular", "SymbolsNFM", 13).unwrap();
        let p = profile_in(&plist, "DEFAULT-GUID");
        assert_eq!(
            p.get("Normal Font").unwrap().as_string(),
            Some("MesloLGSNFM-Regular 13")
        );
        assert_eq!(
            p.get("Non Ascii Font").unwrap().as_string(),
            Some("SymbolsNFM 13")
        );
    }

    #[test]
    fn apply_font_settings_enables_use_non_ascii_font() {
        let mut plist = synth_plist("G1", &["G1"]);
        apply_font_settings(&mut plist, "X-Reg", "Y-Reg", 12).unwrap();
        let p = profile_in(&plist, "G1");
        assert_eq!(
            p.get("Use Non-ASCII Font").unwrap().as_boolean(),
            Some(true)
        );
    }

    #[test]
    fn apply_font_settings_only_touches_default_profile() {
        let mut plist = synth_plist("DEFAULT-GUID", &["OTHER-GUID", "DEFAULT-GUID"]);
        apply_font_settings(&mut plist, "F-Reg", "S-Reg", 13).unwrap();
        let other = profile_in(&plist, "OTHER-GUID");
        assert!(
            other.get("Normal Font").is_none(),
            "other profile should be untouched"
        );
        let defaultp = profile_in(&plist, "DEFAULT-GUID");
        assert!(defaultp.get("Normal Font").is_some());
    }

    #[test]
    fn apply_font_settings_errors_when_no_default_guid() {
        let mut top = Dictionary::new();
        top.insert("New Bookmarks".into(), Value::Array(vec![]));
        let mut plist = Value::Dictionary(top);
        let err = apply_font_settings(&mut plist, "F", "S", 13).unwrap_err();
        assert!(matches!(err, ITerm2Error::NoDefaultGuid));
    }

    #[test]
    fn apply_font_settings_errors_when_default_guid_does_not_match_any_profile() {
        let mut plist = synth_plist("MISSING-GUID", &["A", "B"]);
        let err = apply_font_settings(&mut plist, "F", "S", 13).unwrap_err();
        assert!(matches!(err, ITerm2Error::NoMatchingProfile(g) if g == "MISSING-GUID"));
    }

    #[test]
    fn apply_font_settings_errors_when_top_level_is_not_dict() {
        let mut plist = Value::Array(vec![]);
        let err = apply_font_settings(&mut plist, "F", "S", 13).unwrap_err();
        assert!(matches!(err, ITerm2Error::NotADictionary));
    }

    #[test]
    fn install_font_settings_round_trips_through_disk() {
        let tmp = tempfile::NamedTempFile::new().unwrap();
        let plist = synth_plist("GUID-1", &["GUID-1"]);
        plist.to_file_xml(tmp.path()).unwrap();
        install_font_settings(tmp.path(), "MesloLGSNFM-Regular", "SymbolsNFM", 13).unwrap();
        let reloaded: Value = Value::from_file(tmp.path()).unwrap();
        let p = profile_in(&reloaded, "GUID-1");
        assert_eq!(
            p.get("Normal Font").unwrap().as_string(),
            Some("MesloLGSNFM-Regular 13")
        );
    }

    #[test]
    fn install_font_settings_errors_when_plist_missing() {
        let tmp = tempfile::TempDir::new().unwrap();
        let bogus = tmp.path().join("nonexistent.plist");
        let err = install_font_settings(&bogus, "F", "S", 13).unwrap_err();
        let msg = format!("{err}");
        assert!(
            msg.contains("not found"),
            "expected 'not found' message, got: {msg}"
        );
    }

    #[test]
    fn apply_croft_key_settings_frees_find_menu_shortcut_only() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let menu = dict_in(top, "NSUserKeyEquivalents");
        assert_eq!(
            menu.get("Find Globally...").and_then(|v| v.as_string()),
            Some(FIND_GLOBALLY_MENU_EQUIV)
        );
        assert!(
            menu.get("Paste").is_none(),
            "Paste must remain on its default Cmd+V menu shortcut so iTerm2 fires its native bracketed-paste action when Cmd+V is pressed; remapping it off Cmd+V breaks paste over SSH because the resulting key event has no clipboard reachable from the remote process"
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_letter_chords_as_csi_u_so_iterm_responder_chain_does_not_consume_them()
     {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        for (key, hex, label) in [
            (CMD_A_KEY, CMD_A_HEX, "Cmd+A (select all / multi-select)"),
            (CMD_C_KEY, CMD_C_HEX, "Cmd+C (copy via OSC 52)"),
            (
                CMD_S_KEY,
                CMD_S_HEX,
                "Cmd+S (editor save / source control stage)",
            ),
            (CMD_X_KEY, CMD_X_HEX, "Cmd+X (editor cut)"),
            (CMD_Z_KEY, CMD_Z_HEX, "Cmd+Z (editor undo)"),
            (
                CMD_SHIFT_Z_KEY,
                CMD_SHIFT_Z_HEX,
                "Cmd+Shift+Z (editor redo)",
            ),
        ] {
            assert_eq!(
                action_text(global, key),
                hex,
                "GlobalKeyMap is missing the CSI-u forwarder for {label}; without it, AppKit's NSResponder default key bindings catch the chord (Cmd+C -> copy: on PTYTextView) before croft's terminal handler sees a Char-with-Super key event, which is why Cmd+C silently fails to copy the croft selection even with the NSUserKeyEquivalents Edit-menu relocations in place"
            );
        }
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_dot_for_quick_fix() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CMD_DOT_KEY),
            CMD_DOT_HEX,
            "GlobalKeyMap must forward Cmd+. as a CSI-u sequence so croft's `is_quick_fix_key` fires and opens the Quick Fix menu. Without it the chord never reaches croft and the lightbulb is unreachable by keyboard"
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_e_for_vim_mode_toggle() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CMD_E_KEY),
            CMD_E_HEX,
            "GlobalKeyMap must forward Cmd+E as a CSI-u sequence so croft's `is_vim_toggle_key` fires and toggles native modal editing. Without it, iTerm2's Edit > Find > Use Selection for Find owns the bare chord at the menu layer and croft never sees a Char('e') + SUPER key event, so the toggle is inert"
        );
    }

    #[test]
    fn apply_croft_key_settings_relocates_use_selection_for_find_off_cmd_e() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let menu = dict_in(top, "NSUserKeyEquivalents");
        assert_eq!(
            menu.get(USE_SELECTION_FOR_FIND_MENU_KEY)
                .and_then(|v| v.as_string()),
            Some(USE_SELECTION_FOR_FIND_MENU_EQUIV),
            "iTerm2's Use Selection for Find must be relocated off bare Cmd+E so croft's vim-mode toggle can claim the chord; @~e = Cmd+Opt+E keeps the find-from-selection action reachable on a chord croft does not use"
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_shift_backslash_for_go_to_bracket() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CMD_SHIFT_BACKSLASH_KEY),
            CMD_SHIFT_BACKSLASH_HEX,
            "GlobalKeyMap must forward Cmd+Shift+\\ as a CSI-u sequence so croft's `is_goto_bracket_key` fires and jumps to the matching bracket (VS Code editor.action.jumpToBracket). Without it the chord never reaches croft and Go to Bracket is only reachable from the Command Palette"
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_the_new_editor_command_chords() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        for (key, hex, label) in [
            (
                CMD_OPT_BACKSLASH_KEY,
                CMD_OPT_BACKSLASH_HEX,
                "Cmd+Opt+\\ Select to Bracket",
            ),
            (
                CMD_OPT_SHIFT_S_KEY,
                CMD_OPT_SHIFT_S_HEX,
                "Cmd+Opt+Shift+S Convert Indentation to Spaces",
            ),
            (
                CMD_OPT_SHIFT_T_KEY,
                CMD_OPT_SHIFT_T_HEX,
                "Cmd+Opt+Shift+T Convert Indentation to Tabs",
            ),
            (
                CMD_OPT_SHIFT_N_KEY,
                CMD_OPT_SHIFT_N_HEX,
                "Cmd+Opt+Shift+N Trim Final Newlines",
            ),
        ] {
            assert_eq!(
                action_text(global, key),
                hex,
                "GlobalKeyMap must forward {label} as a CSI-u sequence so the palette command also has a working chord, honoring croft's tenet that every action has a shortcut. Without it the chord never reaches croft and the command is palette-only"
            );
        }
    }

    #[test]
    fn apply_croft_key_settings_forwards_the_formerly_palette_only_editor_chords() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        for (key, hex, label) in [
            (
                CMD_OPT_SHIFT_J_KEY,
                CMD_OPT_SHIFT_J_HEX,
                "Cmd+Opt+Shift+J Join Lines",
            ),
            (
                CMD_OPT_SHIFT_U_KEY,
                CMD_OPT_SHIFT_U_HEX,
                "Cmd+Opt+Shift+U Transform to Uppercase",
            ),
            (
                CMD_OPT_SHIFT_L_KEY,
                CMD_OPT_SHIFT_L_HEX,
                "Cmd+Opt+Shift+L Transform to Lowercase",
            ),
            (
                CMD_OPT_SHIFT_C_KEY,
                CMD_OPT_SHIFT_C_HEX,
                "Cmd+Opt+Shift+C Copy Relative Path",
            ),
            (
                CMD_OPT_SHIFT_A_KEY,
                CMD_OPT_SHIFT_A_HEX,
                "Cmd+Opt+Shift+A Sort Lines Ascending",
            ),
            (
                CMD_OPT_SHIFT_D_KEY,
                CMD_OPT_SHIFT_D_HEX,
                "Cmd+Opt+Shift+D Sort Lines Descending",
            ),
            (
                CMD_OPT_SHIFT_W_KEY,
                CMD_OPT_SHIFT_W_HEX,
                "Cmd+Opt+Shift+W Trim Trailing Whitespace",
            ),
            (
                CMD_OPT_SHIFT_F_KEY,
                CMD_OPT_SHIFT_F_HEX,
                "Cmd+Opt+Shift+F Format Document",
            ),
        ] {
            assert_eq!(
                action_text(global, key),
                hex,
                "GlobalKeyMap must forward {label} as a CSI-u sequence so this command has a working chord and no longer ships palette-only, honoring croft's tenet that every action has a shortcut"
            );
        }
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_b_for_sidebar_toggle() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CMD_B_KEY),
            CMD_B_HEX,
            "GlobalKeyMap must forward Cmd+B as a CSI-u sequence so croft's `is_sidebar_toggle_key` fires and toggles the primary side bar, matching VS Code's Cmd+B. Without it the chord never reaches croft and the side bar can only be toggled with the raw Ctrl+B control byte"
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_w_for_close_tab() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CMD_W_KEY),
            CMD_W_HEX,
            "GlobalKeyMap must forward Cmd+W as a CSI-u sequence so croft's `is_close_tab_key` fires and closes the active editor tab. Without it, iTerm2's File > Close (closeCurrentSession:) owns the bare chord at the menu layer and the keystroke closes the iTerm2 session, quitting iTerm2 when it is the last one, instead of reaching croft"
        );
    }

    #[test]
    fn apply_croft_key_settings_relocates_close_off_cmd_w() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let menu = dict_in(top, "NSUserKeyEquivalents");
        let equiv = menu
            .get(CLOSE_SESSION_MENU_KEY)
            .and_then(|v| v.as_string())
            .filter(|s| !s.is_empty());
        assert!(
            equiv.is_some(),
            "iTerm2's File > Close must be relocated off bare Cmd+W (a non-empty NSUserKeyEquivalents entry) so croft's close-tab forwarder reaches the app instead of the menu layer closing the session"
        );
        assert_eq!(
            equiv,
            Some(CLOSE_SESSION_MENU_EQUIV),
            "the relocation chord must be Cmd+Ctrl+W (@^w): Cmd+Opt+W is already iTerm2's \"Close All Panes in Tab\" (MainMenu.xib id 635) and Cmd+Shift+W is \"Close Terminal Window\" (id 598), so neither can be reused"
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_editor_vim_chord_starts_and_count_digits() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        for (key, hex, label) in [
            (CMD_D_KEY, CMD_D_HEX, "Cmd+D (vim dd chord start)"),
            (CMD_G_KEY, CMD_G_HEX, "Cmd+G (vim gg chord start)"),
            (CMD_Y_KEY, CMD_Y_HEX, "Cmd+Y (vim yy chord start)"),
            (CMD_O_KEY, CMD_O_HEX, "Cmd+O (open line below)"),
            (
                CMD_SHIFT_G_KEY,
                CMD_SHIFT_G_HEX,
                "Cmd+Shift+G (goto bottom)",
            ),
            (
                CMD_SHIFT_O_KEY,
                CMD_SHIFT_O_HEX,
                "Cmd+Shift+O (open line above)",
            ),
        ] {
            assert_eq!(
                action_text(global, key),
                hex,
                "GlobalKeyMap missing CSI-u forwarder for {label}; without it, iTerm2 swallows the chord at the menu/responder layer (e.g. Cmd+D = Split Pane) and the editor's chord state never advances"
            );
        }
        for (key, hex) in CMD_DIGIT_CHORDS {
            assert_eq!(
                action_text(global, key),
                *hex,
                "GlobalKeyMap missing CSI-u forwarder for Cmd+digit {key}; without it, iTerm2 catches Cmd+digit for Select Tab N and the editor's vim count chord cannot start with a leading digit"
            );
        }
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_shift_n_for_explorer_new_folder() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CMD_SHIFT_N_KEY),
            CMD_SHIFT_N_HEX,
            "GlobalKeyMap must forward Cmd+Shift+N as a CSI-u sequence so the Explorer's New Folder chord reaches croft regardless of any AppKit / iTerm2 menu binding that might otherwise consume it at the menu layer"
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_shift_sidebar_jumps_as_csi_u() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        for (key, hex, label) in [
            (
                CMD_SHIFT_E_KEY,
                CMD_SHIFT_E_HEX,
                "Cmd+Shift+E (jump to Explorer)",
            ),
            (
                CMD_SHIFT_S_KEY,
                CMD_SHIFT_S_HEX,
                "Cmd+Shift+S (jump to Source Control)",
            ),
            (
                CMD_SHIFT_D_KEY,
                CMD_SHIFT_D_HEX,
                "Cmd+Shift+D (jump to Run and Debug)",
            ),
            (
                CMD_SHIFT_R_KEY,
                CMD_SHIFT_R_HEX,
                "Cmd+Shift+R (jump to Remote)",
            ),
            (
                CMD_SHIFT_L_KEY,
                CMD_SHIFT_L_HEX,
                "Cmd+Shift+L (disconnect remote, drop to local)",
            ),
        ] {
            assert_eq!(
                action_text(global, key),
                hex,
                "GlobalKeyMap must forward {label} as a CSI-u sequence so the sidebar-jump chord reaches croft; without it, AppKit / iTerm2 menu bindings (most notably Cmd+Shift+D = Split Horizontally) would swallow the chord at the menu layer"
            );
        }
    }

    #[test]
    fn apply_croft_key_settings_forwards_ctrl_shift_j_for_terminal_maximize() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CTRL_SHIFT_J_KEY),
            CTRL_SHIFT_J_HEX,
            "GlobalKeyMap must forward Ctrl+Shift+J as a CSI-u sequence so the maximize-terminal chord reaches croft as Char('J') + CONTROL+SHIFT regardless of any future AppKit / iTerm2 default that might bind the chord and swallow it at the menu layer"
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_f12_for_go_to_implementations() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CMD_F12_KEY),
            CMD_F12_HEX,
            "GlobalKeyMap must forward Cmd+F12 as the legacy modified-function-key sequence ESC[24;9~ so the editor's Go to Implementations chord reaches croft as F(12) + SUPER; unlike the bare-F12 family (plain / Shift / Ctrl) which passes through untouched, Cmd+F12 is captured by macOS and never reaches croft without this forwarder"
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_ctrl_shift_f12_for_go_to_declaration() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CTRL_SHIFT_F12_KEY),
            CTRL_SHIFT_F12_HEX,
            "GlobalKeyMap must forward Ctrl+Shift+F12 as the legacy modified-function-key sequence ESC[24;6~ so the editor's Go to Declaration chord reaches croft as F(12) + SHIFT + CONTROL; Declaration moved here off the bare Shift+F12 it once held when Go to References took that VS Code default, and the forwarder is installed defensively so a future iTerm2 default cannot swallow the chord"
        );
    }

    #[test]
    fn apply_croft_key_settings_relocates_split_horizontally_menu_off_cmd_shift_d() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let menu = dict_in(top, "NSUserKeyEquivalents");
        assert_eq!(
            menu.get("Split Horizontally with Same Profile")
                .and_then(|v| v.as_string()),
            Some("@~D"),
            "iTerm2's Split Horizontally with Same Profile must be relocated off Cmd+Shift+D so croft's Run-and-Debug jump can claim the chord; @~D = Cmd+Opt+Shift+D keeps the iTerm2 split reachable on a chord croft does not use"
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_shift_t_for_terminal_focus() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CMD_SHIFT_T_KEY),
            CMD_SHIFT_T_HEX,
            "GlobalKeyMap must forward Cmd+Shift+T as a CSI-u sequence so croft's `is_terminal_focus_key` fires from any pane. Encoding: 'T' (codepoint 0x54 = 84) with kitty modifier byte 10 = 1 base + Shift(1) + Super(8), giving `ESC [ 84 ; 10 u`"
        );
    }

    #[test]
    fn apply_croft_key_settings_relocates_restore_closed_session_menu_off_cmd_shift_t() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let menu = dict_in(top, "NSUserKeyEquivalents");
        assert_eq!(
            menu.get("Restore Closed Session")
                .and_then(|v| v.as_string()),
            Some("@~T"),
            "iTerm2's Restore Closed Session must be relocated off Cmd+Shift+T so croft's terminal-focus chord can claim it; @~T = Cmd+Opt+Shift+T keeps the iTerm2 action reachable on a chord croft does not use"
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_shift_v_for_markdown_preview() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CMD_SHIFT_V_KEY),
            CMD_SHIFT_V_HEX,
            "GlobalKeyMap must forward Cmd+Shift+V as a CSI-u sequence so croft's `is_markdown_preview_key` toggles the rendered Markdown preview. Encoding: 'V' (codepoint 0x56 = 86) with kitty modifier byte 10 = 1 base + Shift(1) + Super(8), giving `ESC [ 86 ; 10 u`"
        );
    }

    #[test]
    fn apply_croft_key_settings_relocates_paste_selection_menu_off_cmd_shift_v() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let menu = dict_in(top, "NSUserKeyEquivalents");
        assert_eq!(
            menu.get("Paste Selection").and_then(|v| v.as_string()),
            Some("@~V"),
            "iTerm2's Paste Special > Paste Selection must be relocated off Cmd+Shift+V so croft's Markdown preview toggle can claim it; @~V = Cmd+Opt+Shift+V keeps the iTerm2 action reachable on a chord croft does not use"
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_t_for_new_terminal() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CMD_T_KEY),
            CMD_T_HEX,
            "GlobalKeyMap must forward Cmd+T as a CSI-u sequence so croft's `is_terminal_split_key` fires and opens a new terminal. Encoding: 't' (codepoint 0x74 = 116) with kitty modifier byte 9 = 1 base + Super(8), giving `ESC [ 116 ; 9 u`"
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_opt_r_for_reveal_in_finder() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CMD_OPT_R_KEY),
            CMD_OPT_R_HEX,
            "GlobalKeyMap must forward Cmd+Opt+R as a CSI-u sequence so croft's `is_reveal_in_finder_key` fires and reveals the selected entry (or active editor file) in Finder. Encoding: 'r' (codepoint 0x72 = 114) with kitty modifier byte 11 = 1 base + Alt(2) + Super(8), giving `ESC [ 114 ; 11 u` (decoded as ALT|SUPER, disjoint from plain Cmd+R = Rename)"
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_opt_c_for_copy_path() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CMD_OPT_C_KEY),
            CMD_OPT_C_HEX,
            "GlobalKeyMap must forward Cmd+Opt+C as a CSI-u sequence so croft's `is_copy_path_key` fires and copies the active file's path. Encoding: 'c' (codepoint 0x63 = 99) with kitty modifier byte 11 = 1 base + Alt(2) + Super(8), giving `ESC [ 99 ; 11 u` (decoded as ALT|SUPER, disjoint from plain Cmd+C = Copy)"
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_opt_f_for_replace_in_file() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CMD_OPT_F_KEY),
            CMD_OPT_F_HEX,
            "GlobalKeyMap must forward Cmd+Opt+F as a CSI-u sequence so croft's `is_editor_replace_key` fires and opens the find bar with the replace row. Encoding: 'f' (codepoint 0x66 = 102) with kitty modifier byte 11 = 1 base + Alt(2) + Super(8), giving `ESC [ 102 ; 11 u` (decoded as ALT|SUPER, disjoint from plain Cmd+F = Find)"
        );
        let menu = dict_in(top, "NSUserKeyEquivalents");
        assert_eq!(
            menu.get("Password Manager").and_then(|v| v.as_string()),
            Some("@~^f"),
            "iTerm2 binds Cmd+Opt+F to Password Manager by default (MainMenu.xib); it must be relocated to Cmd+Opt+Ctrl+F so the bare chord reaches croft"
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_shift_b_for_run_build_task() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CMD_SHIFT_B_KEY),
            CMD_SHIFT_B_HEX,
            "GlobalKeyMap must forward Cmd+Shift+B as a CSI-u sequence so croft's `is_run_build_task_key` fires. Encoding: 'B' (codepoint 0x42 = 66) with kitty modifier byte 10 = 1 base + Shift(1) + Super(8), giving `ESC [ 66 ; 10 u`"
        );
        let menu = dict_in(top, "NSUserKeyEquivalents");
        assert_eq!(
            menu.get("Show Toolbelt").and_then(|v| v.as_string()),
            Some("@^b"),
            "iTerm2 binds Cmd+Shift+B to Show Toolbelt by default (MainMenu.xib id 1297); it must be relocated to Cmd+Ctrl+B so the bare chord reaches croft"
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_opt_enter_for_replace_all() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CMD_OPT_ENTER_KEY),
            CMD_OPT_ENTER_HEX,
            "GlobalKeyMap must forward Cmd+Opt+Return as a CSI-u sequence so the find bar's Replace All fires. Encoding: Enter (codepoint 13) with kitty modifier byte 11 = 1 base + Alt(2) + Super(8), giving `ESC [ 13 ; 11 u`"
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_backslash_for_editor_split() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CMD_BACKSLASH_KEY),
            CMD_BACKSLASH_HEX,
            "GlobalKeyMap must forward Cmd+\\ as a CSI-u sequence so croft's `is_editor_split_key` fires and splits the editor side by side. Encoding: '\\' (codepoint 0x5c = 92) with kitty modifier byte 9 = 1 base + Super(8), giving `ESC [ 92 ; 9 u`"
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_opt_arrows_for_editor_group_focus() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CMD_OPT_LEFT_KEY),
            CMD_OPT_LEFT_HEX,
            "GlobalKeyMap must forward Cmd+Opt+Left as `ESC [ 1 ; 11 D` so croft's `is_focus_group_left_key` moves focus to the left editor group. Modifier byte 11 = 1 base + Alt(2) + Super(8), decoded as Left + ALT | SUPER, disjoint from the bare Opt+Left word-motion"
        );
        assert_eq!(
            action_text(global, CMD_OPT_RIGHT_KEY),
            CMD_OPT_RIGHT_HEX,
            "GlobalKeyMap must forward Cmd+Opt+Right as `ESC [ 1 ; 11 C` so croft's `is_focus_group_right_key` moves focus to the right editor group"
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_opt_up_down_for_multi_cursor() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CMD_OPT_UP_KEY),
            CMD_OPT_UP_HEX,
            "GlobalKeyMap must forward Cmd+Opt+Up as `ESC [ 1 ; 11 A` so croft adds a cursor above; modifier byte 11 decodes to Up + ALT | SUPER"
        );
        assert_eq!(
            action_text(global, CMD_OPT_DOWN_KEY),
            CMD_OPT_DOWN_HEX,
            "GlobalKeyMap must forward Cmd+Opt+Down as `ESC [ 1 ; 11 B` so croft adds a cursor below"
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_shift_p_for_command_palette() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CMD_SHIFT_P_KEY),
            CMD_SHIFT_P_HEX,
            "GlobalKeyMap must forward Cmd+Shift+P as `ESC [ 80 ; 10 u` so croft's `is_command_palette_key` opens the Command Palette; modifier byte 10 = 1 base + Shift(1) + Super(8), decoded as Char('p') + SUPER | SHIFT"
        );
    }

    #[test]
    fn apply_croft_key_settings_relocates_new_tab_menu_off_cmd_t() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let menu = dict_in(top, "NSUserKeyEquivalents");
        assert_eq!(
            menu.get(NEW_TAB_MENU_KEY).and_then(|v| v.as_string()),
            Some(NEW_TAB_MENU_EQUIV),
            "iTerm2's New Tab must be relocated off Cmd+T so croft's new-terminal chord can claim it; @^t = Cmd+Ctrl+T keeps the iTerm2 action reachable on a chord croft does not use"
        );
    }

    #[test]
    fn apply_croft_key_settings_purges_stale_goto_definition_pointer_binding() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        {
            let top = plist.as_dictionary_mut().unwrap();
            let pa = dict_entry_mut(top, "PointerActions");
            let mut ctx = Dictionary::new();
            ctx.insert(
                "Action".into(),
                Value::String("kContextMenuPointerAction".into()),
            );
            pa.insert("Button,1,1,,".into(), Value::Dictionary(ctx));
            let mut goto = Dictionary::new();
            goto.insert(
                "Action".into(),
                Value::String("kSendHexCodePointerAction".into()),
            );
            pa.insert("Button,0,1,c,".into(), Value::Dictionary(goto));
        }
        apply_croft_key_settings(&mut plist).unwrap();
        let pointer = dict_in(plist.as_dictionary().unwrap(), "PointerActions");
        assert!(
            pointer.get("Button,0,1,c,").is_none(),
            "re-running setup must purge the dead Cmd+click sentinel binding earlier croft versions wrote"
        );
        assert!(
            pointer.get("Button,1,1,,").is_some(),
            "unrelated pointer actions such as the right-click context menu must survive the purge"
        );
    }

    #[test]
    fn apply_croft_key_settings_does_not_synthesize_pointer_actions_when_absent() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        assert!(
            plist
                .as_dictionary()
                .unwrap()
                .get("PointerActions")
                .is_none(),
            "with no PointerActions in the plist, setup must not create one (an empty dict would suppress iTerm2's built-in right-click menu)"
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_brackets_for_terminal_cycle() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        for (key, hex, label) in [
            (
                CMD_LBRACKET_KEY,
                CMD_LBRACKET_HEX,
                "Cmd+[ (previous terminal)",
            ),
            (CMD_RBRACKET_KEY, CMD_RBRACKET_HEX, "Cmd+] (next terminal)"),
        ] {
            assert_eq!(
                action_text(global, key),
                hex,
                "GlobalKeyMap must forward {label} as a CSI-u sequence so croft's terminal-cycle predicate fires as Char('[' / ']') + SUPER. Arrows cannot be used: Ctrl+arrows are eaten by macOS Spaces, Option+arrows are shell word-motion, Cmd+arrows are reserved by the user"
            );
        }
    }

    #[test]
    fn apply_croft_key_settings_relocates_pane_nav_menus_off_cmd_brackets() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let menu = dict_in(top, "NSUserKeyEquivalents");
        for (item, expected) in [
            (PREV_PANE_MENU_KEY, PREV_PANE_MENU_EQUIV),
            (NEXT_PANE_MENU_KEY, NEXT_PANE_MENU_EQUIV),
        ] {
            assert_eq!(
                menu.get(item).and_then(|v| v.as_string()),
                Some(expected),
                "iTerm2's {item} must be relocated off bare Cmd+[ / Cmd+] so croft's terminal-cycle chord can claim it; Cmd+Opt+brackets keep pane navigation reachable"
            );
        }
    }

    #[test]
    fn apply_croft_key_settings_drops_earlier_cmd_arrow_cycle_overrides() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        {
            let top = plist.as_dictionary_mut().unwrap();
            let global = dict_entry_mut(top, "GlobalKeyMap");
            for key in CMD_ARROW_CLEANUP_KEYS {
                global.insert((*key).into(), send_hex_action("0x99", 0));
            }
        }
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        for key in CMD_ARROW_CLEANUP_KEYS {
            assert!(
                global.get(key).is_none(),
                "an earlier croft build hijacked {key} for cycling; now that cycling lives on Cmd+[ / Cmd+], that override must be removed so Cmd+Left/Right return to iTerm2's defaults"
            );
        }
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_p_as_csi_u_so_iterm_print_menu_does_not_eat_quick_open()
     {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CMD_P_KEY),
            CMD_P_HEX,
            "GlobalKeyMap must forward Cmd+P as a CSI-u sequence so croft's Quick Open file finder fires. Without it, AppKit's app-level File > Print binding opens the macOS Print dialog before iTerm2 forwards anything to the TUI — exactly what the user reported on 2026-05-13."
        );
    }

    #[test]
    fn apply_croft_key_settings_relocates_print_menu_off_cmd_p_so_appkit_does_not_steal_quick_open()
    {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let menu = dict_in(top, "NSUserKeyEquivalents");
        assert_eq!(
            menu.get(PRINT_MENU_KEY).and_then(|v| v.as_string()),
            Some(PRINT_MENU_EQUIV),
            "NSUserKeyEquivalents must repoint File > Print at Cmd+Opt+P; otherwise AppKit's app-level Print binding catches Cmd+P at the menu layer and the macOS Print dialog opens instead of croft's Quick Open finder. Cmd+Opt+P keeps Print reachable on a chord croft does not use."
        );
    }

    #[test]
    fn apply_croft_key_settings_relocates_iterm_menu_items_off_vim_chord_letters() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let menu = dict_in(top, "NSUserKeyEquivalents");
        for (item, expected) in [
            ("Split Vertically with Same Profile", "@~d"),
            ("Find Next", "@~g"),
            ("Find Previous", "@~G"),
            ("Select Tab 1", "@~1"),
            ("Select Tab 9", "@~9"),
        ] {
            assert_eq!(
                menu.get(item).and_then(|v| v.as_string()),
                Some(expected),
                "iTerm2 menu item {item} must be relocated off its default Cmd-chord, otherwise the menu bar catches the chord before the GlobalKeyMap forwarder fires"
            );
        }
    }

    #[test]
    fn apply_croft_key_settings_silences_iterm_mouse_reporting_frustration_banner() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        assert_eq!(
            top.get(MOUSE_REPORTING_FRUSTRATION_KEY)
                .and_then(|v| v.as_boolean()),
            Some(true),
            "iTerm2's iTermMouseReportingFrustrationDetector watches raw Cmd+C keyDown and pops the 'Looks like you're trying to copy to the pasteboard...' banner whenever mouse reporting is on and iTerm2 has no selection (which is the steady state under croft, since croft owns the mouse). The advanced setting NoSyncNeverAskAboutMouseReportingFrustration suppresses that detector entirely."
        );
    }

    #[test]
    fn apply_croft_key_settings_relocates_edit_menu_items_off_cmd_letter_so_iterm_does_not_steal_them_back()
     {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let menu = dict_in(top, "NSUserKeyEquivalents");
        for (item, expected) in [
            ("Copy", "@~c"),
            ("Cut", "@~x"),
            ("Select All", "@~a"),
            ("Undo", "@~z"),
        ] {
            assert_eq!(
                menu.get(item).and_then(|v| v.as_string()),
                Some(expected),
                "iTerm2's Edit > {item} menu item must be relocated off its default Cmd-letter shortcut, otherwise once the profile-level Send-Hex bindings are stripped the menu shortcut would still claim the chord and croft would never see the keystroke"
            );
        }
    }

    #[test]
    fn apply_croft_key_settings_relocates_help_menu_off_cmd_shift_slash() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let menu = dict_in(top, "NSUserKeyEquivalents");
        assert_eq!(
            menu.get(HELP_MENU_KEY).and_then(|v| v.as_string()),
            Some(HELP_MENU_EQUIV),
            "Cmd+Shift+/ is reserved by macOS as Cmd+? for the Help menu's Search field; AppKit captures the chord at the app level before iTerm2's GlobalKeyMap is consulted. Re-pointing the 'Show Help Menu' NSUserKeyEquivalents item at Cmd+Opt+? frees the chord so the GlobalKeyMap CSI-u forwarder below can forward it to croft."
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_shift_slash_as_csi_u_so_explorer_make_parent_root_fires()
     {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CMD_SHIFT_SLASH_KEY),
            CMD_SHIFT_SLASH_HEX,
            "GlobalKeyMap must forward Cmd+Shift+/ as a CSI-u sequence so croft's `is_tree_make_parent_root_key` predicate fires from the Explorer pane. Encoding: '?' (shifted '/', codepoint 0x3f = 63) with kitty modifier byte 10 = 1+Shift(1)+Super(8), giving `ESC [ 63 ; 10 u`."
        );
    }

    #[test]
    fn apply_croft_key_settings_forwards_cmd_shift_enter_as_csi_u_so_iterm_does_not_swallow_it() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(
            action_text(global, CMD_SHIFT_ENTER_KEY),
            CMD_SHIFT_ENTER_HEX,
            "Cmd+Shift+Return must be hex-bound at the iTerm2 level: with no binding, iTerm2 never forwards the chord to the TUI, so croft never sees the keystroke. The CSI-u payload encodes Enter (codepoint 13) with kitty modifier byte 10 = 1+Shift(1)+Super(8), which crossterm decodes back to KeyEvent {{ code: Enter, modifiers: SHIFT|SUPER }}."
        );
    }

    #[test]
    fn apply_croft_key_settings_installs_global_search_only() {
        let mut plist = synth_plist("GUID-1", &["GUID-1", "GUID-2"]);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert_eq!(action_text(global, CMD_SHIFT_F_KEY), CMD_SHIFT_F_HEX);
        assert!(
            global.get(CMD_V_KEY).is_none(),
            "Cmd+V must not be hex-bound at the iTerm2 level; intercepting it as a key event prevents the terminal's native paste from emitting a bracketed-paste sequence, which is the only clipboard path that works over SSH"
        );
        for guid in ["GUID-1", "GUID-2"] {
            let profile = profile_in(&plist, guid);
            let cmd_v_in_profile = profile
                .get("Keyboard Map")
                .and_then(|v| v.as_dictionary())
                .and_then(|d| d.get(CMD_V_KEY));
            assert!(
                cmd_v_in_profile.is_none(),
                "profile-level Cmd+V binding must not exist (whether the Keyboard Map dict is absent or just missing this key) so every profile defers to iTerm2's native paste action"
            );
        }
    }

    #[test]
    fn apply_croft_key_settings_clears_legacy_cmd_v_bindings() {
        let mut plist = synth_plist("GUID-1", &["GUID-1", "GUID-2"]);
        seed_stale_cmd_v_mappings(&mut plist);
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let global = dict_in(top, "GlobalKeyMap");
        assert!(
            global.get(CMD_V_KEY).is_none(),
            "re-running setup must remove the legacy GlobalKeyMap Cmd+V hex binding installed by older croft versions"
        );
        for guid in ["GUID-1", "GUID-2"] {
            let profile = profile_in(&plist, guid);
            let profile_keys = dict_in(profile, "Keyboard Map");
            assert!(
                profile_keys.get(CMD_V_KEY).is_none(),
                "re-running setup must remove the legacy profile-level Cmd+V hex binding"
            );
        }
    }

    #[test]
    fn apply_croft_key_settings_clears_legacy_paste_menu_remap() {
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        {
            let top = plist.as_dictionary_mut().unwrap();
            let menu = dict_entry_mut(top, "NSUserKeyEquivalents");
            set_string(menu, "Paste", "@~^v".to_string());
        }
        apply_croft_key_settings(&mut plist).unwrap();
        let top = plist.as_dictionary().unwrap();
        let menu = dict_in(top, "NSUserKeyEquivalents");
        assert!(
            menu.get("Paste").is_none(),
            "re-running setup must remove the legacy Paste -> Cmd+Opt+Ctrl+V menu remap that older croft versions installed; the menu must fall back to the default Cmd+V shortcut so the native paste action fires"
        );
    }

    #[test]
    fn install_croft_settings_round_trips_fonts_and_clears_cmd_v_through_disk() {
        let tmp = tempfile::NamedTempFile::new().unwrap();
        let mut plist = synth_plist("GUID-1", &["GUID-1"]);
        seed_stale_cmd_v_mappings(&mut plist);
        plist.to_file_xml(tmp.path()).unwrap();
        install_croft_settings(tmp.path(), "MesloLGSNFM-Regular", "SymbolsNFM", 13).unwrap();
        let reloaded: Value = Value::from_file(tmp.path()).unwrap();
        let profile = profile_in(&reloaded, "GUID-1");
        assert_eq!(
            profile.get("Normal Font").unwrap().as_string(),
            Some("MesloLGSNFM-Regular 13")
        );
        let top = reloaded.as_dictionary().unwrap();
        assert!(
            dict_in(top, "GlobalKeyMap").get(CMD_V_KEY).is_none(),
            "round-trip: Cmd+V binding must not survive on disk after a fresh setup"
        );
        let profile_keys = dict_in(profile, "Keyboard Map");
        assert!(
            profile_keys.get(CMD_V_KEY).is_none(),
            "round-trip: profile-level Cmd+V binding must not survive on disk after a fresh setup"
        );
    }
}