taut 0.1.0

A Rust GUI library for Windows and Linux that aims to be small, fast and accessible, with zero dependencies and nothing compiled in that your program doesn't use.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
//! Code editor: source code, config files, anything line-based.
//! [`ui.code_box(&mut code, "Source")`](Ui::code_box), where `code` is a [`Code`] the app
//! keeps: the text and the editing state (cursors, undo, folds).
//!
//! Fast at any file size. The text is one `String`, with each line's start offset kept beside
//! it: an edit re-finds the lines it touched and shifts the rest. Only visible rows are
//! described (as [`Ui::rows`] does for a large list), a box per run of one colour. Colouring a
//! line needs what was left open before it (a block comment, a multi-line string): that is kept
//! per line, computed only up to the visible lines, and after an edit recomputed only until it
//! matches the old value again.
//!
//! A language's syntax is plain data ([`Rules`]): keywords, type names, comment and string
//! syntax. A few languages are built in; an app writes rules for others the same way.
use crate::color::Color;
use crate::edit::{in_word, step, word, word_edge};
use crate::event::{Cursor, Key, Mods};
use crate::id::Id;
use crate::layout::{Align, Flow, Length};
use crate::platform::Clipboard;
use crate::text::{Font, cluster};
use crate::theme::Theme;
use crate::ui::{Icon, Response, Stroke, Ui};
use crate::widgets::{CROSS, DOWN, RIGHT};
use std::ops::Range;
use std::time::Duration;

/// Undo depth.
const HISTORY: usize = 500;
/// An edit at more places than this re-indexes every line, not line by line.
const MANY: usize = 64;
/// How many lines away a matching bracket is searched for.
const PAIRED_WITHIN: usize = 2000;
/// Line-end state: inside a block comment.
const IN_COMMENT: u8 = 1;
/// Opening brackets, each with its closing one.
const BRACKETS: [(char, char); 3] = [('(', ')'), ('[', ']'), ('{', '}')];
/// The up arrow, for the find-previous button.
const UP: Icon = Icon::line("M4 9.5 8 5.5 12 9.5");

/// A code token's kind, for its colour.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Sort {
    /// Identifiers, punctuation, and anything else.
    Plain,
    /// A keyword: `fn`, `if`, `return`.
    Word,
    /// A type name: `u32`, `String`.
    Kind,
    /// A number: `42`, `0xff`, `1.5`.
    Number,
    /// A string literal.
    Quoted,
    /// A comment.
    Comment,
}

impl Sort {
    /// Its colour in a theme.
    pub fn color(self, theme: &Theme) -> Color {
        match self {
            Sort::Plain => theme.text,
            Sort::Word => theme.accent,
            Sort::Kind => theme.blue,
            Sort::Number => theme.amber,
            Sort::Quoted => theme.green,
            Sort::Comment => theme.text_faint,
        }
    }
}

/// A string syntax of a language.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Quote {
    /// Opening and closing delimiters. (A backslash escapes the next character.)
    pub marks: [&'static str; 2],
    /// It may span lines. (Else a line end ends it, closed or not.)
    pub lines: bool,
    /// It holds one character, or one escape: `'a'`, `'\n'`. A delimiter not closed right after
    /// is no literal (Rust's `'a`, a lifetime).
    pub one: bool,
}

/// A language's syntax for highlighting, and what indents the next line: plain data, so an app
/// writes rules for its own language the same way.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Rules {
    /// Keywords.
    pub words: &'static [&'static str],
    /// Built-in type names.
    pub kinds: &'static [&'static str],
    /// A capitalized word is a type name too.
    pub capitals: bool,
    /// Line comment start; "" for none.
    pub comment: &'static str,
    /// Block comment start and end; "" for none.
    pub long_comment: [&'static str; 2],
    /// String syntaxes. Where two start alike, the longer first.
    pub quotes: &'static [Quote],
    /// Line-end characters after which Enter indents one more step.
    pub opens: &'static str,
}

impl Rules {
    /// No highlighting: every token is plain.
    pub const PLAIN: Rules = Rules { words: &[], kinds: &[], capitals: false, comment: "", long_comment: ["", ""], quotes: &[], opens: "([{" };
    /// Rust.
    pub const RUST: Rules = Rules {
        words: &[
            "as", "async", "await", "break", "const", "continue", "crate", "dyn", "else", "enum", "extern", "false", "fn", "for", "if", "impl", "in", "let", "loop", "match", "mod", "move", "mut",
            "pub", "ref", "return", "self", "Self", "static", "struct", "super", "trait", "true", "type", "unsafe", "use", "where", "while",
        ],
        kinds: &["bool", "char", "str", "u8", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i64", "i128", "isize", "f32", "f64"],
        capitals: true,
        comment: "//",
        long_comment: ["/*", "*/"],
        quotes: &[Quote { marks: ["\"", "\""], lines: true, one: false }, Quote { marks: ["'", "'"], lines: false, one: true }],
        opens: "([{",
    };
    /// C and C++.
    pub const C: Rules = Rules {
        words: &[
            "auto",
            "break",
            "case",
            "catch",
            "class",
            "const",
            "continue",
            "default",
            "delete",
            "do",
            "else",
            "enum",
            "extern",
            "false",
            "for",
            "goto",
            "if",
            "inline",
            "namespace",
            "new",
            "nullptr",
            "private",
            "protected",
            "public",
            "return",
            "sizeof",
            "static",
            "struct",
            "switch",
            "template",
            "this",
            "throw",
            "true",
            "try",
            "typedef",
            "union",
            "using",
            "virtual",
            "volatile",
            "while",
        ],
        kinds: &["bool", "char", "double", "float", "int", "long", "short", "signed", "size_t", "unsigned", "void"],
        capitals: false,
        comment: "//",
        long_comment: ["/*", "*/"],
        quotes: &[Quote { marks: ["\"", "\""], lines: false, one: false }, Quote { marks: ["'", "'"], lines: false, one: true }],
        opens: "([{",
    };
    /// Python.
    pub const PYTHON: Rules = Rules {
        words: &[
            "and", "as", "assert", "async", "await", "break", "class", "continue", "def", "del", "elif", "else", "except", "False", "finally", "for", "from", "global", "if", "import", "in", "is",
            "lambda", "None", "nonlocal", "not", "or", "pass", "raise", "return", "True", "try", "while", "with", "yield",
        ],
        kinds: &["bool", "bytes", "dict", "float", "int", "list", "object", "set", "str", "tuple"],
        capitals: true,
        comment: "#",
        long_comment: ["", ""],
        quotes: &[
            Quote { marks: ["\"\"\"", "\"\"\""], lines: true, one: false },
            Quote { marks: ["'''", "'''"], lines: true, one: false },
            Quote { marks: ["\"", "\""], lines: false, one: false },
            Quote { marks: ["'", "'"], lines: false, one: false },
        ],
        opens: "([{:",
    };
    /// JavaScript, TypeScript and JSON.
    pub const JAVASCRIPT: Rules = Rules {
        words: &[
            "async",
            "await",
            "break",
            "case",
            "catch",
            "class",
            "const",
            "continue",
            "debugger",
            "default",
            "delete",
            "do",
            "else",
            "export",
            "extends",
            "false",
            "finally",
            "for",
            "function",
            "if",
            "import",
            "in",
            "instanceof",
            "interface",
            "let",
            "new",
            "null",
            "of",
            "return",
            "static",
            "super",
            "switch",
            "this",
            "throw",
            "true",
            "try",
            "type",
            "typeof",
            "undefined",
            "var",
            "void",
            "while",
            "yield",
        ],
        kinds: &["any", "boolean", "never", "number", "object", "string", "unknown"],
        capitals: true,
        comment: "//",
        long_comment: ["/*", "*/"],
        quotes: &[Quote { marks: ["`", "`"], lines: true, one: false }, Quote { marks: ["\"", "\""], lines: false, one: false }, Quote { marks: ["'", "'"], lines: false, one: false }],
        opens: "([{",
    };

    /// Highlights one line. Pushes onto `pieces` each run's end (bytes from the line start) and
    /// kind. `open` is the state before the line (0 at the text's start); returns the state at
    /// its end, for the next line. (This is all a code block shown elsewhere needs:
    /// [`Sort::color`] colours each piece.)
    pub fn line(&self, line: &str, open: u8, pieces: &mut Vec<(usize, Sort)>) -> u8 {
        self.read(line, open, Some(pieces))
    }

    /// Scans a line: returns its end state, and its pieces if asked.
    fn read(&self, line: &str, mut open: u8, mut pieces: Option<&mut Vec<(usize, Sort)>>) -> u8 {
        let mut at = 0;
        while at < line.len() {
            let (end, sort, after) = self.piece(line, at, open, pieces.is_some());
            if let Some(pieces) = pieces.as_deref_mut() {
                join(pieces, end, sort);
            }
            (at, open) = (end, after);
        }
        open
    }

    /// The token of `line` starting at byte `at` in state `open`: its end, kind, and the state
    /// after it. (`named`: words are looked up; else only skipped.)
    fn piece(&self, line: &str, at: usize, open: u8, named: bool) -> (usize, Sort, u8) {
        let rest = &line[at..];
        if open == IN_COMMENT {
            return self.comment_end(line, at);
        }
        if let Some(quote) = self.quotes.get((open as usize).wrapping_sub(2)) {
            return close(rest, quote.marks[1]).map_or((line.len(), Sort::Quoted, if quote.lines { open } else { 0 }), |end| (at + end, Sort::Quoted, 0));
        }
        let first = rest.as_bytes()[0];
        if first.is_ascii_digit() {
            return (at + number(rest), Sort::Number, 0);
        }
        if first == b'_' || first.is_ascii_alphabetic() || first >= 0x80 {
            let end = at + word_end(rest);
            return (end, if named { self.sort_of(&line[at..end]) } else { Sort::Plain }, 0);
        }
        if !self.comment.is_empty() && rest.starts_with(self.comment) {
            return (line.len(), Sort::Comment, 0);
        }
        if !self.long_comment[0].is_empty() && rest.starts_with(self.long_comment[0]) {
            return self.comment_end(line, at + self.long_comment[0].len());
        }
        match self.quotes.iter().position(|quote| rest.starts_with(quote.marks[0])) {
            Some(found) => self.quoted(line, at, found),
            None => (at + self.plain_end(rest), Sort::Plain, 0),
        }
    }

    /// The token a block comment makes from byte `from` of `line`: its end, and whether it is
    /// still open after the line.
    fn comment_end(&self, line: &str, from: usize) -> (usize, Sort, u8) {
        let mark = self.long_comment[1];
        match line[from..].find(mark).filter(|_| !mark.is_empty()) {
            Some(end) => (from + end + mark.len(), Sort::Comment, 0),
            None => (line.len(), Sort::Comment, IN_COMMENT),
        }
    }

    /// The token string syntax `quotes[found]` makes, opening at byte `at` of `line`.
    fn quoted(&self, line: &str, at: usize, found: usize) -> (usize, Sort, u8) {
        let quote = self.quotes[found];
        let from = at + quote.marks[0].len();
        let rest = &line[from..];
        if quote.one {
            // One character, or a backslash escape (up to `\u{10ffff}`), then the closing
            // delimiter: else it was no literal.
            let kept = rest.strip_prefix('\\').map_or(0, |after| 1 + cluster(after));
            let inside = if kept > 0 { rest[kept..].find(quote.marks[1]).filter(|end| *end <= 9).map(|end| kept + end) } else { Some(cluster(rest)) };
            let closed = inside.filter(|inside| *inside > 0 && rest[*inside..].starts_with(quote.marks[1]));
            return closed.map_or((from, Sort::Plain, 0), |inside| (from + inside + quote.marks[1].len(), Sort::Quoted, 0));
        }
        match close(rest, quote.marks[1]) {
            Some(end) => (from + end, Sort::Quoted, 0),
            None => (line.len(), Sort::Quoted, if quote.lines { found as u8 + 2 } else { 0 }),
        }
    }

    /// A word's kind: keyword, type name, or neither.
    fn sort_of(&self, word: &str) -> Sort {
        if self.words.contains(&word) {
            Sort::Word
        } else if self.kinds.contains(&word) || self.capitals && word.starts_with(char::is_uppercase) {
            Sort::Kind
        } else {
            Sort::Plain
        }
    }

    /// The end of the plain run at the start of `rest`: past its first character, up to
    /// anything that could start a word, number, comment or string.
    fn plain_end(&self, rest: &str) -> usize {
        let marks = [self.comment, self.long_comment[0]].into_iter().chain(self.quotes.iter().map(|quote| quote.marks[0]));
        let mut firsts = [0u8; 8];
        let mut count = 0;
        for mark in marks.filter_map(|mark| mark.as_bytes().first()).take(8) {
            (firsts[count], count) = (*mark, count + 1);
        }
        let starts = |byte: &u8| byte.is_ascii_alphanumeric() || *byte == b'_' || *byte >= 0x80 || firsts[..count].contains(byte);
        1 + rest.as_bytes()[1..].iter().position(starts).unwrap_or(rest.len() - 1)
    }
}

/// Appends a piece; merges it with the last if of the same kind.
fn join(pieces: &mut Vec<(usize, Sort)>, end: usize, sort: Sort) {
    match pieces.last_mut() {
        Some(last) if last.1 == sort => last.0 = end,
        _ => pieces.push((end, sort)),
    }
}

/// The end of the first `mark` in `rest`, skipping escaped ones; `None` if there is none.
fn close(rest: &str, mark: &str) -> Option<usize> {
    let (bytes, mark) = (rest.as_bytes(), mark.as_bytes());
    let mut at = 0;
    while at < bytes.len() && !mark.is_empty() {
        if bytes[at..].starts_with(mark) {
            return Some(at + mark.len());
        }
        at += if bytes[at] == b'\\' { 2 } else { 1 };
    }
    None
}

/// The length of the number at the start of `rest`: digits, letters and `_` (`0x1f`, `10u8`,
/// `1_000`), and a point followed by a digit (`1.5`; `1..5` is two).
fn number(rest: &str) -> usize {
    let bytes = rest.as_bytes();
    let goes_on = |at: usize| bytes[at].is_ascii_alphanumeric() || bytes[at] == b'_' || bytes[at] == b'.' && bytes.get(at + 1).is_some_and(u8::is_ascii_digit);
    (1..bytes.len()).find(|at| !goes_on(*at)).unwrap_or(bytes.len())
}

/// The length of the word at the start of `rest`: letters, digits and `_`, any script.
fn word_end(rest: &str) -> usize {
    rest.bytes().position(|byte| !(byte.is_ascii_alphanumeric() || byte == b'_' || byte >= 0x80)).unwrap_or(rest.len())
}

/// The column width of a piece starting at column `from`: one per character, a tab to the next
/// multiple of `tab`.
fn columns(text: &str, from: u32, tab: u32) -> u32 {
    text.chars().fold(from, |column, letter| if letter == '\t' { (column / tab + 1) * tab } else { column + 1 }) - from
}

/// The byte length of a line's leading indent.
fn indent_len(line: &str) -> usize {
    line.len() - line.trim_start_matches([' ', '\t']).len()
}

/// The closing bracket for an opening one.
fn closer(open: char) -> Option<char> {
    BRACKETS.iter().find(|(opens, _)| *opens == open).map(|(_, closes)| *closes)
}

/// Where a text position moves when `edits` are made (each a replaced range and its new text,
/// in order, in pre-edit offsets): along with the text before it. Inside a replaced range, it
/// goes to the end of the replacement.
fn moved(at: usize, edits: &[(Range<usize>, String)]) -> usize {
    let mut shift = 0isize;
    for (range, with) in edits {
        if at < range.start || at == range.start && range.start < range.end {
            break;
        }
        if at < range.end {
            return range.start.saturating_add_signed(shift) + with.len();
        }
        shift += with.len() as isize - range.len() as isize;
    }
    at.saturating_add_signed(shift)
}

/// A stable insertion sort by `key`, for short lists (cursors: usually few).
fn in_order<T>(list: &mut [T], key: impl Fn(&T) -> usize) {
    for at in 1..list.len() {
        let mut to = at;
        while to > 0 && key(&list[to - 1]) > key(&list[to]) {
            list.swap(to - 1, to);
            to -= 1;
        }
    }
}

/// Where a line wraps to fit rows so many columns wide: for each row after the first, its start
/// byte and column. After the last space that fits; with none, before the first character that
/// doesn't.
struct Breaks<'a> {
    letters: std::str::CharIndices<'a>,
    width: u32,
    tab: u32,
    column: u32,
    /// The current row's start, and its last space: each as byte and column.
    from: (usize, u32),
    space: Option<(usize, u32)>,
}

impl<'a> Breaks<'a> {
    fn new(line: &'a str, width: u32, tab: u32) -> Self {
        Breaks { letters: line.char_indices(), width: width.max(1), tab, column: 0, from: (0, 0), space: None }
    }
}

impl Iterator for Breaks<'_> {
    type Item = (usize, u32);
    fn next(&mut self) -> Option<(usize, u32)> {
        for (at, letter) in self.letters.by_ref() {
            let after = if letter == '\t' { (self.column / self.tab + 1) * self.tab } else { self.column + 1 };
            // A space that doesn't fit hangs past the edge: the row breaks after it.
            let over = letter != ' ' && after - self.from.1 > self.width && at > self.from.0;
            let broken = over.then(|| self.space.filter(|(space, _)| *space > self.from.0).unwrap_or((at, self.column)));
            self.space = if letter == ' ' { Some((at + 1, after)) } else { self.space.filter(|_| broken.is_none()) };
            self.column = after;
            if let Some(start) = broken {
                self.from = start;
                return Some(start);
            }
        }
        None
    }
}

/// A cursor and its selection anchor, in bytes: the text between is selected.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct Sel {
    cursor: usize,
    anchor: usize,
    /// The column it keeps while moving up and down rows.
    column: Option<u32>,
}

impl Sel {
    fn at(at: usize) -> Sel {
        Sel { cursor: at, anchor: at, column: None }
    }
    fn range(&self) -> Range<usize> {
        self.cursor.min(self.anchor)..self.cursor.max(self.anchor)
    }
}

/// One change, undone as one: at each of some places (in pre-change offsets) what went and what
/// came; and the cursors before and after.
struct Change {
    edits: Vec<(usize, String, String)>,
    before: Vec<Sel>,
    after: Vec<Sel>,
    /// It was typed, so typing right after it undoes with it.
    typed: bool,
}

/// The find bar: open, and whether replacing too; search and replacement text; match case, and
/// whole words.
#[derive(Default)]
struct Finding {
    open: bool,
    replacing: bool,
    words: String,
    with: String,
    case: bool,
    whole: bool,
    /// Focus goes to its box when next described.
    focus: bool,
    /// The total match count, as of the search and text (as one hash); and the selected match's
    /// index (0 for none), as of the selection's start.
    counted: (u64, usize, (usize, usize)),
}

/// A code editor's state ([`Ui::code_box`]): the text and the editing state. Kept by the app,
/// one per editor, as a [`Table`](crate::Table) is.
///
/// ```
/// use taut::{Code, Rules};
/// let mut code = Code::new("fn main() {}\n", &Rules::RUST);
/// code.wrap = true; // long lines go on to the next row, in place of scrolling sideways
/// assert_eq!(code.text(), "fn main() {}\n");
/// assert_eq!(code.cursor(), (1, 1)); // line and column of the cursor, counted from 1
/// ```
pub struct Code {
    /// Highlighting: [`Rules::RUST`], or the app's own; [`Rules::PLAIN`] highlights nothing.
    pub rules: &'static Rules,
    /// Wraps long lines instead of scrolling sideways.
    pub wrap: bool,
    /// Tab width in columns; also the indent width.
    pub tab: usize,
    /// Tab indents with a tab character instead of spaces.
    pub tabs: bool,
    text: String,
    /// Each line's start byte; each line's width in columns; and the widest, once computed
    /// since the last change.
    starts: Vec<u32>,
    widths: Vec<u32>,
    widest: Option<u32>,
    /// The text uses CRLF line ends.
    returns: bool,
    /// Each line's start state, for highlighting (see `open_at`): correct for lines before
    /// `sure`; and for those before `kept`, from the first line after `edited` whose recomputed
    /// start state matches the old one.
    states: Vec<u8>,
    sure: usize,
    kept: usize,
    edited: usize,
    /// For wrapping, each line's row count at `wrapped_at` columns (0: no wrapping). And where
    /// lines wrap or fold, each line's first row, and the total row count after the last line;
    /// empty when each line is one row.
    wraps: Vec<u16>,
    wrapped_at: u32,
    tops: Vec<u32>,
    tops_stale: bool,
    /// Folded line ranges: first and last line of each, in order. (The line before the first
    /// stands for them.)
    folds: Vec<[u32; 2]>,
    /// The cursors, the primary first.
    sels: Vec<Sel>,
    changes: Vec<Change>,
    /// How many of the last changes are undone now.
    undone: usize,
    /// Counts every change to the text.
    version: u64,
    /// The tab width and rules all of the above was computed with (the app may change them).
    made_for: (usize, Rules),
    finding: Finding,
    /// The go-to-line bar, if open: its text, and whether focus goes to it when next described.
    going: Option<(String, bool)>,
    /// For the box: its id, and its measuring id; its room (width and height); when its cursor
    /// last moved (it blinks from then); whether to scroll the primary cursor into view; the
    /// pointer's click count; and that Escape had nothing to do, so Tab moves on.
    id: Id,
    measured: Id,
    view: [f32; 2],
    moved: Duration,
    reveal: bool,
    clicks: u8,
    escaped: bool,
    /// The bracket pair at the primary cursor, as of a version and position.
    paired: (u64, usize, Option<[usize; 2]>),
    /// The last highlighted line's pieces, which line, and as of which version.
    pieces: Vec<(usize, Sort)>,
    coloured: (u64, usize),
}

impl std::fmt::Debug for Code {
    /// Its length in bytes and lines, and its settings; not the text.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let lines = self.starts.len().max(1);
        f.debug_struct("Code").field("bytes", &self.text.len()).field("lines", &lines).field("wrap", &self.wrap).field("tab", &self.tab).field("tabs", &self.tabs).finish_non_exhaustive()
    }
}

impl Default for Code {
    fn default() -> Self {
        Code::new("", &Rules::PLAIN)
    }
}

impl Code {
    /// The text to edit, and its highlighting rules.
    pub fn new(text: impl Into<String>, rules: &'static Rules) -> Code {
        let mut code = Code {
            rules,
            wrap: false,
            tab: 4,
            tabs: false,
            text: text.into(),
            starts: Vec::new(),
            widths: Vec::new(),
            widest: None,
            returns: false,
            states: Vec::new(),
            sure: 1,
            kept: 1,
            edited: 0,
            wraps: Vec::new(),
            wrapped_at: 0,
            tops: Vec::new(),
            tops_stale: false,
            folds: Vec::new(),
            sels: vec![Sel::at(0)],
            changes: Vec::new(),
            undone: 0,
            version: 0,
            made_for: (4, *rules),
            finding: Finding::default(),
            going: None,
            id: 0,
            measured: 0,
            view: [0.0; 2],
            moved: Duration::ZERO,
            reveal: false,
            clicks: 1,
            escaped: false,
            paired: (u64::MAX, 0, None),
            pieces: Vec::new(),
            coloured: (u64::MAX, 0),
        };
        code.index();
        code
    }

    /// The current text.
    pub fn text(&self) -> &str {
        &self.text
    }
    /// Replaces all the text: the undo history is cleared and the cursor goes to the start.
    pub fn set_text(&mut self, text: impl Into<String>) {
        (self.text, self.sels, self.undone, self.version) = (text.into(), vec![Sel::at(0)], 0, self.version + 1);
        self.changes.clear();
        self.index();
    }
    /// The primary cursor's line and column, both from 1 (column in characters).
    pub fn cursor(&self) -> (usize, usize) {
        let at = self.sels[0].cursor;
        let line = self.line_of(at);
        (line + 1, self.text[self.starts[line] as usize..at].chars().count() + 1)
    }
    /// Puts the cursor at the start of a line (from 1), to be scrolled into view.
    pub fn go_to_line(&mut self, line: usize) {
        let line = line.clamp(1, self.starts.len()) - 1;
        let (start, end) = self.line_ends(line);
        self.sels = vec![Sel::at(start + indent_len(&self.text[start..end]))];
        self.unfold_cursors();
        self.reveal = true;
    }

    // ---- the lines ----

    /// Indexes every line's start and width from scratch: for a new text, or after a change too
    /// large to patch line by line.
    fn index(&mut self) {
        self.starts.clear();
        self.widths.clear();
        let tab = self.tab.max(1) as u32;
        let mut from = 0;
        for (at, _) in self.text.match_indices('\n') {
            self.starts.push(from as u32);
            self.widths.push(columns(self.text[from..at].trim_end_matches('\r'), 0, tab));
            from = at + 1;
        }
        self.starts.push(from as u32);
        self.widths.push(columns(&self.text[from..], 0, tab));
        self.returns = self.text.find('\n').is_some_and(|at| self.text[..at].ends_with('\r'));
        self.states.clear();
        self.states.resize(self.starts.len(), 0);
        (self.sure, self.kept, self.edited, self.widest) = (1, 1, 0, None);
        (self.made_for, self.wrapped_at, self.tops_stale) = ((self.tab, *self.rules), 0, true);
        self.folds.clear();
        self.wraps.clear();
    }

    /// The line containing byte `at`.
    fn line_of(&self, at: usize) -> usize {
        self.starts.partition_point(|start| *start as usize <= at) - 1
    }
    /// Line `line`'s start and end (before its line end).
    fn line_ends(&self, line: usize) -> (usize, usize) {
        let start = self.starts[line] as usize;
        let end = self.starts.get(line + 1).map_or(self.text.len(), |next| *next as usize - 1);
        (start, if self.text[start..end].ends_with('\r') { end - 1 } else { end })
    }
    /// Line `line`'s text, without its line end.
    fn line_text(&self, line: usize) -> &str {
        let (start, end) = self.line_ends(line);
        &self.text[start..end]
    }

    /// Re-indexes after `removed` bytes from `at` were replaced by `added`: the touched lines
    /// are re-found and the rest shifted, with everything kept per line.
    fn relines(&mut self, at: usize, removed: usize, added: usize) {
        let (first, last) = (self.line_of(at), self.line_of(at + removed));
        let shift = added as i64 - removed as i64;
        let end = self.starts.get(last + 1).map_or(self.text.len() as i64, |next| *next as i64 - 1 + shift) as usize;
        let (from, tab) = (self.starts[first] as usize, self.tab.max(1) as u32);
        // The lines now in place of those.
        let mut found: Vec<(u32, u32)> = Vec::new();
        let mut start = from;
        for (at, _) in self.text[from..end].match_indices('\n') {
            found.push((start as u32, columns(self.text[start..from + at].trim_end_matches('\r'), 0, tab)));
            start = from + at + 1;
        }
        found.push((start as u32, columns(self.text[start..end].trim_end_matches('\r'), 0, tab)));
        let new = found.len();
        self.starts.splice(first..=last, found.iter().map(|line| line.0));
        self.widths.splice(first..=last, found.iter().map(|line| line.1));
        for start in &mut self.starts[first + new..] {
            *start = (*start as i64 + shift) as u32;
        }
        self.relined(first, last, new);
    }

    /// Patches the per-line state after lines `first` to `last` became `new` lines.
    fn relined(&mut self, first: usize, last: usize, new: usize) {
        let shift = new as isize - (last + 1 - first) as isize;
        let along = |line: usize| if line > last { line.saturating_add_signed(shift) } else { line };
        self.states.splice(first + 1..=last, std::iter::repeat_n(0, new - 1));
        // Start states are correct up to the first touched line; after the touched lines they
        // may well match again.
        self.edited = if self.sure > self.edited { first + new - 1 } else { along(self.edited).max(first + new - 1) };
        self.kept = if self.kept > last + 1 { along(self.kept) } else { self.kept.min(first + 1) };
        self.sure = self.sure.min(first + 1);
        if self.wrapped_at > 0 {
            let rows: Vec<u16> = (first..first + new).map(|line| self.rows_of(line)).collect();
            self.wraps.splice(first..=last, rows);
        }
        // A fold whose lines, or whose standing line, were touched is unfolded.
        self.folds.retain(|fold| (fold[1] as usize) < first || fold[0] as usize > last + 1);
        for fold in &mut self.folds {
            *fold = fold.map(|line| along(line as usize) as u32);
        }
        (self.widest, self.tops_stale) = (None, true);
    }

    /// Line `line`'s row count when wrapping at `wrapped_at` columns.
    fn rows_of(&self, line: usize) -> u16 {
        Breaks::new(self.line_text(line), self.wrapped_at, self.tab.max(1) as u32).count().saturating_add(1).min(u16::MAX as usize) as u16
    }

    /// Syncs the per-line state with the app's settings, which may have changed: tab width,
    /// rules, and wrapping at `width` columns.
    fn fit(&mut self, width: u32) {
        if self.made_for.0 != self.tab {
            self.index();
        }
        if self.made_for.1 != *self.rules {
            (self.made_for.1, self.sure, self.kept, self.coloured) = (*self.rules, 1, 1, (u64::MAX, 0));
        }
        let wrap = if self.wrap { width.max(8) } else { 0 };
        if wrap == self.wrapped_at {
            return;
        }
        (self.wrapped_at, self.tops_stale) = (wrap, true);
        self.wraps.clear();
        if wrap > 0 {
            self.wraps = (0..self.starts.len()).map(|line| self.rows_of(line)).collect();
        }
    }

    /// The widest line, in columns.
    fn widest(&mut self) -> u32 {
        let widths = &self.widths;
        *self.widest.get_or_insert_with(|| widths.iter().copied().max().unwrap_or(0))
    }

    // ---- rows: what the box shows of the lines ----

    /// Recomputes each line's first row, where it differs from its line number.
    fn tops(&mut self) {
        if !std::mem::take(&mut self.tops_stale) {
            return;
        }
        self.tops.clear();
        if self.wraps.is_empty() && self.folds.is_empty() {
            return;
        }
        let (mut top, mut fold) = (0u32, 0);
        for line in 0..self.starts.len() {
            self.tops.push(top);
            fold += self.folds.get(fold).is_some_and(|run| run[1] < line as u32) as usize;
            let hidden = self.folds.get(fold).is_some_and(|run| run[0] <= line as u32);
            top += if hidden { 0 } else { self.wraps.get(line).map_or(1, |rows| *rows as u32) };
        }
        self.tops.push(top);
    }
    /// The row count.
    fn row_count(&self) -> usize {
        self.tops.last().map_or(self.starts.len(), |rows| *rows as usize)
    }
    /// The row line `line` starts at.
    fn top_of(&self, line: usize) -> usize {
        self.tops.get(line).map_or(line, |top| *top as usize)
    }
    /// The line of row `row`.
    fn line_at(&self, row: usize) -> usize {
        match self.tops.is_empty() {
            true => row.min(self.starts.len() - 1),
            false => self.tops.partition_point(|top| *top as usize <= row).saturating_sub(1).min(self.starts.len() - 1),
        }
    }
    /// Row `row`: its line, its byte range, and its start column.
    fn row_span(&self, row: usize) -> (usize, usize, usize, u32) {
        let line = self.line_at(row);
        let (start, end) = self.line_ends(line);
        if self.wraps.is_empty() {
            return (line, start, end, 0);
        }
        let part = row.saturating_sub(self.top_of(line));
        let mut breaks = Breaks::new(&self.text[start..end], self.wrapped_at, self.tab.max(1) as u32);
        let from = if part == 0 { (0, 0) } else { breaks.nth(part - 1).unwrap_or((end - start, 0)) };
        let to = breaks.next().map_or(end, |next| start + next.0);
        (line, start + from.0, to, from.1)
    }
    /// The row byte `at` shows in, and its column within that row.
    fn place(&self, at: usize) -> (usize, u32) {
        let line = self.line_of(at);
        let start = self.starts[line] as usize;
        let tab = self.tab.max(1) as u32;
        let column = columns(&self.text[start..at], 0, tab);
        if self.wraps.is_empty() {
            return (self.top_of(line), column);
        }
        let end = self.line_ends(line).1;
        let before = Breaks::new(&self.text[start..end], self.wrapped_at, tab).take_while(|(byte, _)| start + byte <= at);
        let (part, from) = before.fold((0, 0), |(part, _), (_, from)| (part + 1, from));
        (self.top_of(line) + part, column - from)
    }
    /// The position in row `row` at `column`, or the row's end. (A row a line wraps after ends
    /// before its last character: the position after it is the next row's.)
    fn at_column(&self, row: usize, column: u32) -> usize {
        let (line, from, to, start) = self.row_span(row);
        let last = to == self.line_ends(line).1;
        let (mut at, mut went, tab) = (from, start, self.tab.max(1) as u32);
        while at < to {
            let next = at + cluster(&self.text[at..to]);
            went += columns(&self.text[at..next], went, tab);
            if went > start + column || next == to && !last {
                break;
            }
            at = next;
        }
        at
    }

    // ---- folding ----

    /// A line's indent depth in columns; `None` for a blank line.
    fn depth(&self, line: usize) -> Option<u32> {
        let text = self.line_text(line);
        let indent = indent_len(text);
        (indent < text.len()).then(|| columns(&text[..indent], 0, self.tab.max(1) as u32))
    }
    /// The last line of the block line `line` starts, if it starts one: the following lines
    /// indented further, up to the last non-blank one. (The next non-blank line is searched
    /// only a few lines on.)
    fn block(&self, line: usize) -> Option<usize> {
        let depth = self.depth(line)?;
        let next = (line + 1..self.starts.len().min(line + 64)).find_map(|next| self.depth(next).map(|deeper| (next, deeper)))?;
        if next.1 <= depth {
            return None;
        }
        let after = (next.0..self.starts.len()).find(|later| self.depth(*later).is_some_and(|deeper| deeper <= depth)).unwrap_or(self.starts.len());
        (line + 1..after).rev().find(|inside| self.depth(*inside).is_some())
    }
    /// Folds the block line `line` starts (with any folds inside), or unfolds it if folded.
    /// Returns whether there was such a block.
    fn fold(&mut self, line: usize) -> bool {
        if let Some(run) = self.folds.iter().position(|run| run[0] as usize == line + 1) {
            self.folds.remove(run);
            self.tops_stale = true;
            return true;
        }
        let Some(last) = self.block(line) else { return false };
        let run = [line as u32 + 1, last as u32];
        self.folds.retain(|inner| inner[1] < run[0] || inner[0] > run[1]);
        let at = self.folds.partition_point(|before| before[0] < run[0]);
        self.folds.insert(at, run);
        self.tops_stale = true;
        // A cursor in the folded lines moves to the end of the standing line.
        let shows = self.line_ends(line).1;
        let inside: Vec<bool> = self.sels.iter().map(|sel| (line + 1..=last).contains(&self.line_of(sel.cursor))).collect();
        for (sel, inside) in self.sels.iter_mut().zip(inside) {
            if inside {
                *sel = Sel::at(shows);
            }
        }
        self.merge();
        true
    }
    /// Unfolds whatever a cursor entered.
    fn unfold_cursors(&mut self) {
        let lines: Vec<usize> = self.sels.iter().map(|sel| self.line_of(sel.cursor)).collect();
        let before = self.folds.len();
        self.folds.retain(|run| !lines.iter().any(|line| (run[0] as usize..=run[1] as usize).contains(line)));
        self.tops_stale |= self.folds.len() != before;
    }

    // ---- colours ----

    /// Line `line`'s start state (see [`Rules::line`]): scanned on from the last known line,
    /// and after an edit only until it matches again.
    fn open_at(&mut self, line: usize) -> u8 {
        while self.sure <= line {
            let at = self.sure;
            let open = self.rules.read(self.line_text(at - 1), self.states[at - 1], None);
            if at > self.edited && at < self.kept && self.states[at] == open {
                self.sure = self.kept;
                continue;
            }
            self.states[at] = open;
            self.sure += 1;
        }
        self.kept = self.kept.max(self.sure);
        self.states[line]
    }
    /// Highlights line `line` into `pieces`, unless it was the last highlighted.
    fn colour(&mut self, line: usize) {
        if self.coloured == (self.version, line) {
            return;
        }
        let open = self.open_at(line);
        self.pieces.clear();
        let (start, end) = self.line_ends(line);
        self.rules.read(&self.text[start..end], open, Some(&mut self.pieces));
        self.coloured = (self.version, line);
    }

    /// The bracket matching the one at the primary cursor (after it, else before), where both
    /// are code, not in a comment or string: both positions. Searched up to `PAIRED_WITHIN`
    /// lines away.
    fn paired(&mut self) -> Option<[usize; 2]> {
        let at = self.sels[0].cursor;
        if self.paired.0 == self.version && self.paired.1 == at {
            return self.paired.2;
        }
        let before = self.text[..at].chars().next_back().map(|letter| (at - letter.len_utf8(), letter));
        let after = self.text[at..].chars().next().map(|letter| (at, letter));
        let found = [after, before].into_iter().flatten().find_map(|(at, letter)| self.partner(at, letter).map(|other| [at, other]));
        self.paired = (self.version, at, found);
        found
    }
    /// The position of the bracket matching `letter` at byte `at`.
    fn partner(&mut self, at: usize, letter: char) -> Option<usize> {
        let (open, close) = *BRACKETS.iter().find(|(open, close)| *open == letter || *close == letter)?;
        let (forward, home) = (letter == open, self.line_of(at));
        if !self.is_code(home, at) {
            return None;
        }
        let mut depth = 0usize;
        let lines: Vec<usize> = if forward { (home..self.starts.len().min(home + PAIRED_WITHIN)).collect() } else { (home.saturating_sub(PAIRED_WITHIN)..=home).rev().collect() };
        for line in lines {
            if let Some(found) = self.pair_in(line, at, [open, close], forward, &mut depth) {
                return Some(found);
            }
        }
        None
    }
    /// Whether byte `at` of line `line` is code: not in a comment or string.
    fn is_code(&mut self, line: usize, at: usize) -> bool {
        self.colour(line);
        let start = self.starts[line] as usize;
        let piece = self.pieces.partition_point(|(end, _)| *end <= at - start);
        self.pieces.get(piece).is_some_and(|(_, sort)| *sort == Sort::Plain)
    }
    /// Scans line `line`'s code for the bracket matching the one at `at`, forward or back,
    /// tracking nesting depth of its kind.
    fn pair_in(&mut self, line: usize, at: usize, [open, close]: [char; 2], forward: bool, depth: &mut usize) -> Option<usize> {
        self.colour(line);
        let start = self.starts[line] as usize;
        let mut letters: Vec<(usize, char)> = Vec::new();
        let mut from = 0;
        for (end, sort) in self.pieces.iter().copied() {
            let piece = &self.text[start + from..start + end];
            letters.extend(piece.char_indices().filter(|(_, letter)| *letter == open || *letter == close).filter(|_| sort == Sort::Plain).map(|(byte, letter)| (start + from + byte, letter)));
            from = end;
        }
        if !forward {
            letters.reverse();
        }
        for (byte, letter) in letters.into_iter().filter(|(byte, _)| if forward { *byte > at } else { *byte < at }) {
            match letter == open && forward || letter == close && !forward {
                true => *depth += 1,
                false if *depth == 0 => return Some(byte),
                false => *depth -= 1,
            }
        }
        None
    }

    // ---- changing the text ----

    /// Makes one change, undone as one: each of `edits` replaces a range of the text (in order,
    /// non-overlapping). With `carets`, a cursor goes into each replacement, that many bytes
    /// in; else cursors move with the text.
    fn change(&mut self, edits: Vec<(Range<usize>, String)>, carets: Option<Vec<usize>>, typed: bool) {
        if edits.iter().all(|(range, with)| range.is_empty() && with.is_empty()) {
            return;
        }
        let before = self.sels.clone();
        let written: Vec<(usize, String, String)> = edits.iter().map(|(range, with)| (range.start, self.text[range.clone()].to_string(), with.clone())).collect();
        self.apply(&edits);
        if let Some(carets) = carets {
            self.sels = self.carets(&edits, &carets);
            self.merge();
        }
        self.changes.truncate(self.changes.len() - self.undone);
        self.undone = 0;
        // Consecutive typed letters undo together, a word at a time.
        let after = self.sels.clone();
        match self.changes.last_mut().filter(|last| typed && last.typed && joins(last, &written)) {
            Some(last) => {
                last.edits[0].2.push_str(&written[0].2);
                last.after = after;
            }
            None => self.changes.push(Change { edits: written, before, after, typed }),
        }
        if self.changes.len() > HISTORY {
            self.changes.remove(0);
        }
    }

    /// The cursors after `edits`: one in each replacement, `carets` bytes into it.
    fn carets(&self, edits: &[(Range<usize>, String)], carets: &[usize]) -> Vec<Sel> {
        let mut shift = 0isize;
        let mut sels = Vec::with_capacity(edits.len());
        for ((range, with), caret) in edits.iter().zip(carets) {
            sels.push(Sel::at(range.start.saturating_add_signed(shift) + caret));
            shift += with.len() as isize - range.len() as isize;
        }
        sels
    }

    /// Applies each replacement (in order, non-overlapping, in current offsets), re-indexes the
    /// lines, and moves the cursors.
    fn apply(&mut self, edits: &[(Range<usize>, String)]) {
        let many = edits.len() > MANY;
        for (range, with) in edits.iter().rev() {
            self.text.replace_range(range.clone(), with);
            if !many {
                self.relines(range.start, range.len(), with.len());
            }
        }
        if many {
            self.index();
        }
        for sel in &mut self.sels {
            (sel.cursor, sel.anchor) = (moved(sel.cursor, edits), moved(sel.anchor, edits));
        }
        self.version += 1;
    }

    /// Undoes the last change, or (`again`) redoes the last undone one.
    fn undo(&mut self, again: bool) {
        let at = if again { self.changes.len() - self.undone } else { self.changes.len().wrapping_sub(self.undone + 1) };
        let Some(change) = self.changes.get(at).filter(|_| !again || self.undone > 0) else { return };
        let mut shift = 0isize;
        let mut edits = Vec::with_capacity(change.edits.len());
        for (place, removed, added) in &change.edits {
            let (from, to) = if again { (removed, added) } else { (added, removed) };
            let start = if again { *place } else { place.saturating_add_signed(shift) };
            edits.push((start..start + from.len(), to.clone()));
            shift += added.len() as isize - removed.len() as isize;
        }
        let sels = if again { change.after.clone() } else { change.before.clone() };
        self.apply(&edits);
        self.sels = sels;
        self.undone = if again { self.undone - 1 } else { self.undone + 1 };
        self.unfold_cursors();
    }

    /// Edits at every cursor as `make` says: the range to replace, its replacement, and the
    /// cursor's offset into it. (Where two cursors' ranges meet, the first's edit wins.)
    fn each(&mut self, typed: bool, make: impl Fn(&Code, &Sel) -> (Range<usize>, String, usize)) {
        let mut made: Vec<(Range<usize>, String, usize)> = self.sels.iter().map(|sel| make(self, sel)).collect();
        in_order(&mut made, |(range, ..)| range.start);
        let (mut edits, mut carets, mut end) = (Vec::with_capacity(made.len()), Vec::with_capacity(made.len()), 0);
        for (nth, (range, with, caret)) in made.into_iter().enumerate() {
            if nth == 0 || range.start >= end {
                end = range.end;
                edits.push((range, with));
                carets.push(caret);
            }
        }
        self.change(edits, Some(carets), typed);
    }

    /// Inserts typed or pasted text at every cursor, replacing each selection. (Its line ends
    /// become the text's own.)
    fn put(&mut self, text: &str, typed: bool) {
        let with: String = text.replace("\r\n", "\n").replace('\r', "\n");
        let with = if self.returns { with.replace('\n', "\r\n") } else { with };
        self.each(typed, |_, sel| (sel.range(), with.clone(), with.len()));
    }

    /// What Enter inserts for a cursor: a line end and the line's indent; after a block opener,
    /// one step more; with the matching closing bracket right after the cursor, that bracket on
    /// its own line below.
    fn line_break(&self, sel: &Sel) -> (Range<usize>, String, usize) {
        let range = sel.range();
        let start = self.starts[self.line_of(range.start)] as usize;
        let typed = &self.text[start..range.start];
        let indent = &typed[..indent_len(typed)];
        let last = typed.trim_end().chars().next_back();
        let ending = if self.returns { "\r\n" } else { "\n" };
        let mut with = format!("{ending}{indent}");
        if !last.is_some_and(|last| self.rules.opens.contains(last)) {
            let caret = with.len();
            return (range, with, caret);
        }
        with.push_str(&self.indent_step(indent.len() as u32));
        let caret = with.len();
        if last.and_then(closer).is_some_and(|close| self.text[range.end..].starts_with(close)) {
            with.push_str(&format!("{ending}{indent}"));
        }
        (range, with, caret)
    }

    /// What Tab inserts at column `column`: a tab, or spaces to the next stop.
    fn indent_step(&self, column: u32) -> String {
        let tab = self.tab.max(1);
        if self.tabs { "\t".into() } else { " ".repeat(tab - column as usize % tab) }
    }

    /// The lines the cursors' selections touch, in order, each once. (A selection ending at a
    /// line's start doesn't take that line.)
    fn picked_lines(&self) -> Vec<usize> {
        let mut lines = Vec::new();
        for range in self.sels.iter().map(Sel::range) {
            let ends_a_line = range.end > range.start && self.starts[self.line_of(range.end)] as usize == range.end;
            let last = self.line_of(if ends_a_line { range.end - 1 } else { range.end });
            lines.extend(self.line_of(range.start)..=last);
        }
        in_order(&mut lines, |line| *line);
        lines.dedup();
        lines
    }

    /// Tab: indents the selected lines, if any selection spans lines; else inserts an indent at
    /// each cursor.
    fn indent(&mut self) {
        let spans = self.sels.iter().any(|sel| self.line_of(sel.cursor) != self.line_of(sel.anchor));
        if !spans {
            return self.each(true, |code, sel| {
                let range = sel.range();
                let start = code.starts[code.line_of(range.start)] as usize;
                let step = code.indent_step(columns(&code.text[start..range.start], 0, code.tab.max(1) as u32));
                let caret = step.len();
                (range, step, caret)
            });
        }
        let edits = self.picked_lines().into_iter().filter(|line| !self.line_text(*line).trim().is_empty()).map(|line| {
            let start = self.starts[line] as usize;
            (start..start, if self.tabs { "\t".to_string() } else { " ".repeat(self.tab.max(1)) })
        });
        let edits = edits.collect();
        self.change(edits, None, false);
    }

    /// Shift+Tab: removes one indent step from each cursor's line.
    fn outdent(&mut self) {
        let edits = self.picked_lines().into_iter().map(|line| {
            let (start, text) = (self.starts[line] as usize, self.line_text(line));
            let spaces = text.bytes().take(self.tab.max(1)).take_while(|byte| *byte == b' ').count();
            (start..start + if text.starts_with('\t') { 1 } else { spaces }, String::new())
        });
        let edits = edits.filter(|(range, _)| !range.is_empty()).collect();
        self.change(edits, None, false);
    }

    /// What Backspace (or `forward`, Delete) removes at a cursor: the selection, else a
    /// character (with Ctrl a word); and in a space indent, back to the last stop.
    fn doomed(&self, sel: &Sel, forward: bool, words: bool) -> Range<usize> {
        let at = sel.cursor;
        if !sel.range().is_empty() {
            return sel.range();
        }
        let start = self.starts[self.line_of(at)] as usize;
        let lead = &self.text[start..at];
        if !forward && !words && !lead.is_empty() && lead.bytes().all(|byte| byte == b' ') {
            let tab = self.tab.max(1);
            return at - ((lead.len() - 1) % tab + 1)..at;
        }
        let other = if words { word(&self.text, at, forward) } else { step(&self.text, at, forward) };
        at.min(other)..at.max(other)
    }

    // ---- the keyboard ----

    /// Handles a key; returns whether the box took it. `page` is the visible row count.
    fn key(&mut self, key: Key, mods: Mods, page: usize, clipboard: &mut dyn Clipboard) -> bool {
        self.escaped = false;
        if key == Key::Escape && mods == Mods::NONE {
            return self.escape();
        }
        if self.edit_key(key, mods, clipboard) || self.bar_key(key, mods) {
            return true;
        }
        if mods.ctrl && mods.alt && matches!(key, Key::Up | Key::Down) {
            return self.add_cursor(key == Key::Up);
        }
        if mods.ctrl && mods.alt && key == Key::Char('f') {
            let line = self.line_of(self.sels[0].cursor);
            return self.fold(line) || self.fold_around(line);
        }
        self.go(key, mods, page)
    }

    /// Escape: closes an open bar, or reduces several cursors to one. With nothing to do it is
    /// not taken, and Tab after it moves focus on.
    fn escape(&mut self) -> bool {
        if self.finding.open || self.going.is_some() {
            (self.finding.open, self.going) = (false, None);
            return true;
        }
        if self.sels.len() > 1 {
            self.sels.truncate(1);
            return true;
        }
        self.escaped = true;
        false
    }

    /// Keys that edit the text or use the clipboard; returns whether this was one.
    fn edit_key(&mut self, key: Key, mods: Mods, clipboard: &mut dyn Clipboard) -> bool {
        let plain = !mods.ctrl && !mods.alt;
        match key {
            Key::Backspace | Key::Delete if !mods.alt => self.each(false, |code, sel| (code.doomed(sel, key == Key::Delete, mods.ctrl), String::new(), 0)),
            Key::Enter if plain => self.each(true, Code::line_break),
            Key::Tab if plain && mods.shift => self.outdent(),
            Key::Tab if plain => self.indent(),
            Key::Char('a') if mods == Mods::CTRL => self.sels = vec![Sel { cursor: self.text.len(), anchor: 0, column: None }],
            Key::Char('c') if mods == Mods::CTRL => self.copy(clipboard),
            Key::Char('x') if mods == Mods::CTRL => {
                self.copy(clipboard);
                self.each(false, |_, sel| (sel.range(), String::new(), 0));
            }
            Key::Char('v') if mods == Mods::CTRL => clipboard.get().iter().for_each(|pasted| self.put(pasted, false)),
            Key::Char('z' | 'y') if mods.ctrl && !mods.alt => self.undo(mods.shift || key == Key::Char('y')),
            _ => return false,
        }
        true
    }

    /// Copies the cursors' selections, a line each: nothing if none select anything.
    fn copy(&self, clipboard: &mut dyn Clipboard) {
        let mut ranges: Vec<Range<usize>> = self.sels.iter().map(Sel::range).filter(|range| !range.is_empty()).collect();
        in_order(&mut ranges, |range| range.start);
        if !ranges.is_empty() {
            clipboard.set(&ranges.into_iter().map(|range| &self.text[range]).collect::<Vec<_>>().join("\n"));
        }
    }

    /// Keys that open the bars or step through matches; returns whether this was one.
    fn bar_key(&mut self, key: Key, mods: Mods) -> bool {
        match key {
            Key::Char('f' | 'h') if mods == Mods::CTRL => self.open_finding(key == Key::Char('h')),
            Key::Char('g') if mods == Mods::CTRL => self.going = Some((String::new(), true)),
            Key::F(3) if !mods.ctrl && !mods.alt => self.find_next(mods.shift),
            _ => return false,
        }
        true
    }

    /// Opens the find bar (and, `replacing`, replace) with the selection in it, if on one line.
    fn open_finding(&mut self, replacing: bool) {
        let picked = &self.text[self.sels[0].range()];
        if !picked.is_empty() && !picked.contains('\n') {
            self.finding.words = picked.to_string();
        }
        (self.finding.open, self.finding.replacing, self.finding.focus) = (true, replacing, true);
    }

    /// Ctrl+Alt+Up/Down: adds a cursor a row above the top one or below the bottom one, at the
    /// primary's column.
    fn add_cursor(&mut self, up: bool) -> bool {
        let edge = self.sels.iter().map(|sel| sel.cursor).fold(if up { usize::MAX } else { 0 }, |edge, at| if up { edge.min(at) } else { edge.max(at) });
        let column = self.sels[0].column.unwrap_or_else(|| self.place(self.sels[0].cursor).1);
        self.tops();
        let (to, _) = self.row_away(edge, if up { -1 } else { 1 }, Some(column));
        if to != edge {
            self.sels.push(Sel { column: Some(column), ..Sel::at(to) });
        }
        true
    }

    /// Cursor movement keys (with Shift, selecting); returns whether this was one.
    fn go(&mut self, key: Key, mods: Mods, page: usize) -> bool {
        if mods.alt || !matches!(key, Key::Left | Key::Right | Key::Up | Key::Down | Key::Home | Key::End | Key::PageUp | Key::PageDown) {
            return false;
        }
        self.tops();
        for at in 0..self.sels.len() {
            let sel = self.sels[at];
            let (to, column) = self.went(&sel, key, mods, page);
            self.sels[at] = Sel { cursor: to, anchor: if mods.shift { sel.anchor } else { to }, column };
        }
        self.merge();
        self.unfold_cursors();
        true
    }

    /// Where a key moves a cursor, and the column it then keeps.
    fn went(&self, sel: &Sel, key: Key, mods: Mods, page: usize) -> (usize, Option<u32>) {
        let (at, picked, text) = (sel.cursor, sel.range(), self.text.as_str());
        let (start, end) = self.line_ends(self.line_of(at));
        let lead = start + indent_len(&text[start..end]);
        let to = match key {
            Key::Left | Key::Right if mods.ctrl => word(text, at, key == Key::Right),
            Key::Left if !mods.shift && !picked.is_empty() => picked.start,
            Key::Right if !mods.shift && !picked.is_empty() => picked.end,
            Key::Left | Key::Right => step(text, at, key == Key::Right),
            Key::Home if mods.ctrl => 0,
            Key::End if mods.ctrl => text.len(),
            // Home goes to the line's first non-blank character, and from there to the line
            // start.
            Key::Home => [lead, start][(at == lead) as usize],
            Key::End => end,
            Key::Up | Key::Down => return self.row_away(at, if key == Key::Up { -1 } else { 1 }, sel.column),
            _ => return self.row_away(at, if key == Key::PageUp { -(page as isize) } else { page as isize }, sel.column),
        };
        (to, None)
    }

    /// The position `rows` up or down from `at` (before the first row: the start; past the
    /// last: the end), at `column`, else at `at`'s column.
    fn row_away(&self, at: usize, rows: isize, column: Option<u32>) -> (usize, Option<u32>) {
        let (row, across) = self.place(at);
        let want = column.unwrap_or(across);
        match row.checked_add_signed(rows) {
            None => (0, Some(want)),
            Some(to) if to >= self.row_count() => (self.text.len(), Some(want)),
            Some(to) => (self.at_column(to, want), Some(want)),
        }
    }

    /// Merges cursors that overlap or touch.
    fn merge(&mut self) {
        let mut at = 1;
        while at < self.sels.len() {
            let (one, other) = (self.sels[at].range(), at);
            let over = (0..at).find(|before| {
                let two = self.sels[*before].range();
                let (low, high) = (one.start.max(two.start), one.end.min(two.end));
                low < high || low == high && (one.is_empty() || two.is_empty())
            });
            let Some(before) = over else {
                at += 1;
                continue;
            };
            let (kept, both) = (self.sels[before], self.sels[before].range().start.min(one.start)..self.sels[before].range().end.max(one.end));
            let forward = kept.cursor >= kept.anchor;
            self.sels[before] = Sel { cursor: if forward { both.end } else { both.start }, anchor: if forward { both.start } else { both.end }, column: kept.column };
            self.sels.remove(other);
        }
    }

    /// Folds the nearest block around line `line` that starts above it.
    fn fold_around(&mut self, line: usize) -> bool {
        let Some(depth) = (0..=line).rev().find_map(|inside| self.depth(inside)) else { return false };
        let outer = (0..line).rev().find(|above| self.depth(*above).is_some_and(|outer| outer < depth));
        outer.is_some_and(|outer| self.fold(outer))
    }

    // ---- the pointer ----

    /// A press at byte `at`, the `clicks`th in a row: one places the cursor, two select the
    /// word, three the line. With Shift the anchor stays; with Alt a cursor is added. Or (not
    /// `first`) the pointer is dragged there.
    fn point(&mut self, at: usize, clicks: u8, first: bool, mods: Mods) {
        if !first {
            self.sels[0].cursor = self.dragged(at);
            return;
        }
        self.clicks = clicks;
        if mods.alt {
            self.sels.insert(0, Sel::at(at));
            return self.merge();
        }
        let anchor = self.sels[0].anchor;
        let picked = match clicks {
            _ if mods.shift => Sel { cursor: at, anchor, column: None },
            2 => Sel { cursor: word_edge(&self.text, at, true), anchor: word_edge(&self.text, at, false), column: None },
            3.. => self.whole_line(at),
            _ => Sel::at(at),
        };
        self.sels = vec![picked];
    }
    /// Where a drag to byte `at` moves the primary cursor: by character, by word after a double
    /// click, by line after a triple one.
    fn dragged(&self, at: usize) -> usize {
        let forward = at >= self.sels[0].anchor;
        match self.clicks {
            2 => word_edge(&self.text, at, forward),
            3.. if forward => self.whole_line(at).cursor,
            3.. => self.starts[self.line_of(at)] as usize,
            _ => at,
        }
    }
    /// The whole line containing byte `at`, with its line end.
    fn whole_line(&self, at: usize) -> Sel {
        let line = self.line_of(at);
        let end = self.starts.get(line + 1).map_or(self.text.len(), |next| *next as usize);
        Sel { cursor: end, anchor: self.starts[line] as usize, column: None }
    }

    // ---- finding ----

    /// The first match starting at or after byte `from` and ending by `to`: case-sensitive or
    /// not, whole word or not.
    fn next_match(&self, from: usize, to: usize) -> Option<Range<usize>> {
        let (words, mut at) = (self.finding.words.as_str(), from);
        while at < to && !words.is_empty() {
            let rest = &self.text[at..to];
            let (start, long) = if self.finding.case { rest.find(words).map(|start| (start, words.len()))? } else { any_case(rest, words)? };
            let found = at + start..at + start + long;
            let alone = !self.finding.whole || !in_word(&self.text, step(&self.text, found.start, false)) || found.start == 0;
            if alone && (!self.finding.whole || !in_word(&self.text, found.end)) {
                return Some(found);
            }
            at = found.start + cluster(&self.text[found.start..]);
        }
        None
    }

    /// Selects the next match after the primary cursor (or before, `back`), wrapping around the
    /// text; and scrolls to it.
    fn find_next(&mut self, back: bool) {
        let picked = self.sels[0].range();
        let found = match back {
            false => self.next_match(picked.end, self.text.len()).or_else(|| self.next_match(0, picked.end)),
            true => self.last_match(picked.start).or_else(|| self.last_match(self.text.len())),
        };
        if let Some(found) = found {
            self.sels = vec![Sel { cursor: found.end, anchor: found.start, column: None }];
            self.unfold_cursors();
            self.reveal = true;
        }
    }
    /// The last match ending by byte `to`.
    fn last_match(&self, to: usize) -> Option<Range<usize>> {
        let mut last = None;
        let mut from = 0;
        while let Some(found) = self.next_match(from, to) {
            from = found.start + cluster(&self.text[found.start..]);
            last = Some(found);
        }
        last
    }

    /// Replaces the selection with the replacement text, if it is a match; then finds the next.
    fn replace_one(&mut self) {
        let picked = self.sels[0].range();
        if !picked.is_empty() && self.next_match(picked.start, picked.end) == Some(picked.clone()) {
            let with = self.finding.with.clone();
            let caret = with.len();
            self.change(vec![(picked, with)], Some(vec![caret]), false);
        }
        self.find_next(false);
    }
    /// Replaces every match, as one change.
    fn replace_all(&mut self) {
        let mut edits = Vec::new();
        let mut from = 0;
        while let Some(found) = self.next_match(from, self.text.len()) {
            from = found.end.max(found.start + 1);
            edits.push((found, self.finding.with.clone()));
        }
        self.change(edits, None, false);
    }

    /// The total match count, and the selected match's index (0 for none). Recounted only when
    /// the search, text or selection changed.
    fn count(&mut self) -> (usize, usize) {
        let picked = self.sels[0].range();
        let key = crate::id::hash((&self.finding.words, self.finding.case, self.finding.whole, self.version));
        if self.finding.counted.0 != key {
            self.finding.counted = (key, self.matches_before(self.text.len()), (0, usize::MAX));
        }
        if self.finding.counted.2.1 != picked.start {
            let place = if self.next_match(picked.start, picked.end) == Some(picked.clone()) { self.matches_before(picked.end) } else { 0 };
            self.finding.counted.2 = (place, picked.start);
        }
        (self.finding.counted.1, self.finding.counted.2.0)
    }
    /// The number of matches ending by byte `to`.
    fn matches_before(&self, to: usize) -> usize {
        let (mut count, mut from) = (0, 0);
        while let Some(found) = self.next_match(from, to) {
            (count, from) = (count + 1, found.end.max(found.start + 1));
        }
        count
    }
}

/// Whether a typed change right after `last` undoes with it: both insert one or more letters at
/// one place, the second right after the first, and the first didn't end a word where the
/// second starts one.
fn joins(last: &Change, edits: &[(usize, String, String)]) -> bool {
    let ([(at, removed, added)], [(was, _, before)]) = (edits, last.edits.as_slice()) else { return false };
    removed.is_empty() && *was + before.len() == *at && (!before.ends_with(char::is_whitespace) || added.starts_with(char::is_whitespace))
}

/// The first case-insensitive match of `needle` in `hay`: its start, and its length in `hay`.
fn any_case(hay: &str, needle: &str) -> Option<(usize, usize)> {
    if needle.is_ascii() {
        let (bytes, wanted) = (hay.as_bytes(), needle.as_bytes());
        let first = wanted[0].to_ascii_lowercase();
        let found = (0..(bytes.len() + 1).saturating_sub(wanted.len())).find(|at| bytes[*at].to_ascii_lowercase() == first && bytes[*at..*at + wanted.len()].eq_ignore_ascii_case(wanted));
        return found.map(|at| (at, wanted.len()));
    }
    hay.char_indices().find_map(|(at, _)| alike(&hay[at..], needle).map(|long| (at, long)))
}

/// The bytes of `hay` matching `needle` at its start, case-insensitive.
fn alike(hay: &str, needle: &str) -> Option<usize> {
    let mut letters = hay.char_indices();
    for wanted in needle.chars() {
        let (_, letter) = letters.next()?;
        if !letter.to_lowercase().eq(wanted.to_lowercase()) {
            return None;
        }
    }
    Some(letters.next().map_or(hay.len(), |(at, _)| at))
}

/// A code editor's metrics, computed each run: its font, row and font line heights, column
/// width, line number gutter width, and the text's x start.
#[derive(Clone, Copy)]
struct Sizes {
    font: Font,
    row: f32,
    line: f32,
    column: f32,
    numbers: f32,
    text: f32,
}

/// Code editor.
impl Ui {
    /// A code editor showing `code`'s text, highlighted by `code.rules`, with line numbers in a
    /// gutter on the left. It fills its room. `hint` is its screen reader name. `changed` is
    /// set when the user edits the text.
    ///
    /// Keys, besides those of every text box: Tab and Shift+Tab indent and outdent the selected
    /// lines (Escape, then Tab, moves focus on); Enter keeps the indent. Ctrl+F finds, Ctrl+H
    /// replaces, F3 and Shift+F3 find next and previous, Ctrl+G goes to a line.
    /// Ctrl+Alt+Up/Down add a cursor, as does Alt+click; Escape leaves one. Ctrl+Alt+F folds or
    /// unfolds the cursor's block, as does a click on the marker by its line number. Files of
    /// any length open, scroll and edit instantly: only what is visible is described and
    /// highlighted.
    pub fn code_box(&mut self, code: &mut Code, hint: &str) -> Response {
        let t = self.theme;
        let sizes = self.code_sizes(code);
        if let Some(room) = self.rooms.get(&code.measured) {
            code.view = [room[0], room[1]];
        }
        code.fit(((code.view[0] - sizes.text - sizes.column) / sizes.column).max(0.0) as u32);
        code.tops();
        let edge = if code.id != 0 && self.focus == code.id { t.accent } else { t.line_strong };
        self.column().grow().min(0.0, 3.0 * sizes.row).fill(t.panel).border(edge, 1.0).radius(t.radius).clip().open();
        let id = self.id("code");
        code.id = id;
        let mut state = self.interact(id);
        let version = code.version;
        if state.focused {
            self.code_input(code, &state, sizes);
        } else if state.hovered {
            self.set_cursor(Cursor::Text);
        }
        self.find_bar(code);
        self.go_bar(code);
        if std::mem::take(&mut code.reveal) {
            self.reveal_code(code, id, sizes);
        }
        self.code_view(code, &state, sizes, hint);
        self.close();
        state.changed = code.version != version;
        self.edit_menu(&state, false);
        state
    }

    /// This run's metrics.
    fn code_sizes(&mut self, code: &Code) -> Sizes {
        let font = self.theme.font.mono();
        let line = self.text.metrics(font, self.scale).height().ceil();
        let column = self.text.measure(" ", font, self.scale, None).max(1.0);
        let digits = (code.starts.len().ilog10() + 1).max(2) as f32;
        let numbers = (digits * column + 26.0).ceil();
        Sizes { font, row: line.max((font.size * 1.45).round()), line, column, numbers, text: numbers + 6.0 }
    }

    /// Feeds pointer and keys to a focused code editor.
    fn code_input(&mut self, code: &mut Code, state: &Response, sizes: Sizes) {
        // A selection asked for by a screen reader, or the app (`select_all`), first.
        if self.edit.id == state.id && std::mem::take(&mut self.edit.reveal) {
            code.sels = vec![Sel { cursor: self.edit.cursor.min(code.text.len()), anchor: self.edit.anchor.min(code.text.len()), column: None }];
        }
        if std::mem::take(&mut self.select_all) {
            code.sels = vec![Sel { cursor: code.text.len(), anchor: 0, column: None }];
        }
        let was = (code.version, code.sels.clone());
        self.code_pointer(code, state, sizes);
        let page = ((code.view[1] / sizes.row) as usize).saturating_sub(1).max(1);
        let Ui { input, clipboard, .. } = self;
        input.strokes.retain(|stroke| match stroke {
            Stroke::Key(key, mods) => !code.key(*key, *mods, page, clipboard.as_mut()),
            Stroke::Text(typed) => {
                code.escaped = false;
                code.put(typed, true);
                false
            }
        });
        if was != (code.version, code.sels.clone()) {
            (code.moved, code.reveal, self.again) = (self.now, true, true);
        }
        // The box stands for the edited text: the context menu, and screen reader selection,
        // use it.
        let primary = code.sels[0];
        (self.edit.id, self.edit.secret, self.edit.cursor, self.edit.anchor) = (state.id, false, primary.cursor, primary.anchor);
        self.takes_tab = if code.escaped { 0 } else { state.id };
    }

    /// The pointer in a code editor: on the text it places the cursor (dragging selects); on
    /// the gutter it selects the line, or on a fold marker folds the block.
    fn code_pointer(&mut self, code: &mut Code, state: &Response, sizes: Sizes) {
        let Some(whole) = self.survey.scrollers.iter().find(|scroller| scroller.id == state.id).map(|scroller| scroller.rect) else { return };
        let Some(p) = self.input.pointer else { return };
        code.tops();
        let offset = self.scrolls.get(&state.id).copied().unwrap_or_default();
        let on_numbers = p.x - whole.x < sizes.numbers;
        if state.hovered {
            self.set_cursor(if on_numbers { Cursor::Arrow } else { Cursor::Text });
        }
        if !state.pressed {
            return;
        }
        let first = self.input.pressed[0] && state.hovered;
        let row = (((p.y - whole.y + offset[1]) / sizes.row).max(0.0) as usize).min(code.row_count() - 1);
        if first && on_numbers {
            return self.number_pressed(code, row, p.x - whole.x > sizes.numbers - 16.0);
        }
        let at = self.code_at_x(code, row, p.x - whole.x + offset[0] - sizes.text, sizes);
        code.point(at, self.input.clicks, first, self.input.mods);
    }

    /// A click on the gutter at row `row`: on the fold marker it folds or unfolds the line's
    /// block; elsewhere it selects the line.
    fn number_pressed(&mut self, code: &mut Code, row: usize, on_mark: bool) {
        let line = code.line_at(row);
        if on_mark && code.fold(line) {
            return;
        }
        let picked = code.whole_line(code.starts[line] as usize);
        code.sels = vec![picked];
        code.clicks = 3;
    }

    /// The position in row `row` nearest `x` units from its text start.
    fn code_at_x(&mut self, code: &Code, row: usize, x: f32, sizes: Sizes) -> usize {
        let (_, from, to, start) = code.row_span(row);
        let (mut at, mut left) = (from, 0.0);
        while at < to {
            let next = at + cluster(&code.text[at..to]);
            let right = self.code_x(code, from, next, start, sizes);
            if x < (left + right) / 2.0 {
                return at;
            }
            (at, left) = (next, right);
        }
        to
    }

    /// The x of byte `at` from its row's text start, the row starting at byte `from` and column
    /// `start`. ASCII characters are one column each, tabs go to their stop, anything else is
    /// measured.
    fn code_x(&mut self, code: &Code, from: usize, at: usize, start: u32, sizes: Sizes) -> f32 {
        self.code_x_on(code, (0.0, start), from, at, sizes).0
    }

    /// The same, continuing from byte `from` at `x` and column `column`: so each piece of a
    /// long row is placed without rescanning from the row start. Returns byte `at`'s x and
    /// column.
    fn code_x_on(&mut self, code: &Code, (mut x, mut column): (f32, u32), from: usize, at: usize, sizes: Sizes) -> (f32, u32) {
        let tab = code.tab.max(1) as u32;
        for (nth, piece) in code.text[from..at].split('\t').enumerate() {
            if nth > 0 {
                let stop = (column / tab + 1) * tab;
                (x, column) = (x + (stop - column) as f32 * sizes.column, stop);
            }
            let wide = if piece.is_ascii() { piece.len() as f32 * sizes.column } else { self.text.measure(piece, sizes.font, self.scale, None) };
            (x, column) = (x + wide, column + piece.chars().count() as u32);
        }
        (x, column)
    }

    /// Scrolls so the primary cursor is in view.
    fn reveal_code(&mut self, code: &mut Code, id: Id, sizes: Sizes) {
        let [wide, high] = code.view;
        if high <= 0.0 {
            return;
        }
        code.unfold_cursors();
        code.tops();
        let at = code.sels[0].cursor;
        let (row, _) = code.place(at);
        let (_, from, _, start) = code.row_span(row);
        let x = sizes.text + self.code_x(code, from, at, start, sizes);
        let mut offset = self.scrolls.get(&id).copied().unwrap_or_default();
        let top = row as f32 * sizes.row;
        offset[1] = offset[1].min(top).max(top + sizes.row - high);
        offset[0] = offset[0].min(x - sizes.text).max(x + 2.0 * sizes.column - wide).max(0.0);
        match offset == [0.0; 2] {
            true => self.scrolls.remove(&id),
            false => self.scrolls.insert(id, offset),
        };
    }

    /// The find and replace bar, while open.
    fn find_bar(&mut self, code: &mut Code) {
        if !code.finding.open {
            return;
        }
        let (t, words) = (self.theme, self.code_words);
        self.column().pad(6.0).gap(6.0).fill(t.raise).open();
        self.wrapping_row().gap(6.0).open();
        self.el().w(Length::Px(200.0)).open();
        if std::mem::take(&mut code.finding.focus) {
            let field = self.next_id();
            self.set_focus(field);
            self.select_all();
        }
        let field = self.text_box(&mut code.finding.words, words[0]);
        self.close();
        let back = self.icon_button(UP, words[9]).clicked;
        let next = self.icon_button(DOWN, words[8]).clicked;
        let (count, place) = code.count();
        self.note(&format!("{place} {} {count}", words[7]));
        self.toggle(&mut code.finding.case, words[2]);
        self.toggle(&mut code.finding.whole, words[3]);
        let shut = self.icon_button(CROSS, words[10]).clicked;
        self.close();
        let with = code.finding.replacing && self.replace_row(code);
        self.close();
        let in_bar = field.focused || with;
        let keyed = |ui: &mut Ui, mods: Mods| in_bar && ui.key(Key::Enter, mods) || ui.key(Key::F(3), mods);
        if field.changed {
            code.sels = vec![Sel::at(code.sels[0].range().start)];
            code.find_next(false);
        } else if back || keyed(self, Mods::SHIFT) {
            code.find_next(true);
        } else if next || keyed(self, Mods::NONE) {
            code.find_next(false);
        }
        if shut || in_bar && self.key(Key::Escape, Mods::NONE) {
            code.finding.open = false;
            self.set_focus(code.id);
        }
    }

    /// The replace row of the bar. Returns whether its box has focus.
    fn replace_row(&mut self, code: &mut Code) -> bool {
        let words = self.code_words;
        self.wrapping_row().gap(6.0).open();
        self.el().w(Length::Px(200.0)).open();
        let field = self.text_box(&mut code.finding.with, words[1]);
        self.close();
        let one = self.button(words[4]).clicked;
        let all = self.button(words[5]).clicked;
        self.close();
        if one {
            code.replace_one();
        }
        if all {
            code.replace_all();
        }
        field.focused
    }

    /// The go-to-line bar, while open.
    fn go_bar(&mut self, code: &mut Code) {
        let Some((mut typed, focus)) = code.going.take() else { return };
        let t = self.theme;
        self.row().pad(6.0).gap(6.0).fill(t.raise).open();
        self.el().w(Length::Px(140.0)).open();
        if focus {
            let field = self.next_id();
            self.set_focus(field);
        }
        let field = self.text_box(&mut typed, self.code_words[6]);
        self.close();
        let shut = self.icon_button(CROSS, self.code_words[10]).clicked;
        self.close();
        let go = field.focused && self.key(Key::Enter, Mods::NONE);
        if go && let Ok(line) = typed.trim().parse::<usize>() {
            code.go_to_line(line);
            code.moved = self.now;
        }
        if go || shut || field.focused && self.key(Key::Escape, Mods::NONE) {
            return self.set_focus(code.id);
        }
        code.going = Some((typed, false));
    }

    /// The scrolling area with the visible rows; and what screen readers get of it.
    fn code_view(&mut self, code: &mut Code, state: &Response, sizes: Sizes, hint: &str) {
        let wide = if code.wrap { 0.0 } else { sizes.text + (code.widest() + 2) as f32 * sizes.column };
        self.el().key("code").grow().scroll().scroll_x().align(Align::Start, Align::Spread).sense().focusable().open();
        // Screen readers get a text box with all its text, the cursor position in characters,
        // and its line of how many.
        if self.listening {
            self.tell_text(state, hint, &code.text);
            let letters = |at: usize| code.text[..at].chars().count() as u32;
            self.tell_caret([letters(code.sels[0].anchor), letters(code.sels[0].cursor)]);
            self.tell_place(code.line_of(code.sels[0].cursor) + 1, code.starts.len());
        }
        self.column().min(wide, 0.0).open();
        code.tops();
        code.measured = self.next_id();
        let caret = state.focused && self.window_focused && self.blink(code.moved);
        let rows = self.rows(code.row_count(), sizes.row);
        let line = code.line_of(code.sels[0].cursor);
        let paired = if state.focused { code.paired() } else { None };
        for row in rows {
            self.code_row(code, row, line, [state.focused, caret], paired, sizes);
        }
        self.close();
        self.close();
        self.close();
    }

    /// One row: its selections, matches and bracket pair, its highlighted text, its cursors,
    /// and over it all, its line number. `line` is the primary cursor's; `shows` says whether
    /// the box has focus, and whether the cursor shows now (it blinks).
    fn code_row(&mut self, code: &mut Code, row: usize, line: usize, shows: [bool; 2], paired: Option<[usize; 2]>, sizes: Sizes) {
        let t = self.theme;
        let (this, from, to, start) = code.row_span(row);
        let ends = to == code.line_ends(this).1;
        self.el().flow(Flow::Layers).fill(if this == line { t.hover } else { Color::NONE }).open();
        // Behind the text: selections, matches, the bracket pair.
        let picked = t.accent.alpha(if shows[0] { 0.35 } else { 0.2 });
        for range in code.sels.iter().map(Sel::range).filter(|range| !range.is_empty() && range.start <= to && range.end > from).collect::<Vec<_>>() {
            let left = self.code_x(code, from, range.start.max(from), start, sizes);
            let right = if range.end > to { self.code_x(code, from, to, start, sizes) + if ends { sizes.column } else { 0.0 } } else { self.code_x(code, from, range.end, start, sizes) };
            self.code_mark(left, right - left, sizes, picked, false);
        }
        let mut found = from;
        while let Some(range) = code.finding.open.then(|| code.next_match(found, to)).flatten() {
            let (left, right) = (self.code_x(code, from, range.start, start, sizes), self.code_x(code, from, range.end, start, sizes));
            self.code_mark(left, right - left, sizes, t.amber.alpha(0.3), false);
            found = range.end.max(range.start + 1);
        }
        for at in paired.into_iter().flatten().filter(|at| (from..to).contains(at)) {
            let left = self.code_x(code, from, at, start, sizes);
            self.code_mark(left, sizes.column, sizes, t.text_dim, true);
        }
        self.code_letters(code, this, [from, to], start, sizes);
        // The cursors: the primary where the input method's window goes, the others as marks.
        for (nth, sel) in code.sels.iter().enumerate().filter(|(_, sel)| sel.cursor >= from && (sel.cursor < to || sel.cursor == to && ends)) {
            let x = sizes.text + self.code_x(code, from, sel.cursor, start, sizes);
            let blinks = shows[0] && shows[1];
            // The primary is a text box only while the box has focus: the focused text box's
            // cursor is watched for blinking.
            match nth {
                0 => _ = self.el().text("", sizes.font, t.text).select(shows[0].then_some([0, 0]), blinks).place((Align::Start, Align::Center)).at(x, 0.0).show(),
                _ => _ = self.el().size(1.0, sizes.line).fill(if blinks { t.text } else { Color::NONE }).place((Align::Start, Align::Center)).at(x, 0.0).show(),
            }
        }
        self.code_numbers(code, this, from == code.starts[this] as usize, line, sizes);
        self.close();
    }

    /// A mark behind a row's text, `left` units from its text start and so wide: filled, or
    /// (`ring`) outlined.
    fn code_mark(&mut self, left: f32, wide: f32, sizes: Sizes, color: Color, ring: bool) {
        let mark = self.el().size(wide.max(1.0), sizes.row).at(sizes.text + left, 0.0);
        match ring {
            true => mark.border(color, 1.0).radius(2.0).show(),
            false => mark.fill(color).show(),
        };
    }

    /// A row's text, from byte `ends[0]` to `ends[1]` of line `line` (the row starting at
    /// column `start`): a box per run of one colour, placed where it starts.
    fn code_letters(&mut self, code: &mut Code, line: usize, [from, to]: [usize; 2], start: u32, sizes: Sizes) {
        let t = self.theme;
        code.colour(line);
        let base = code.starts[line] as usize;
        // Where the last piece went, and its byte: each piece is placed from there.
        let (mut begun, mut went) = (base, (0.0, start, from));
        for nth in 0..code.pieces.len() {
            let (end, sort) = code.pieces[nth];
            let (low, high) = (begun.max(from), (base + end).min(to).max(begun.max(from)));
            begun = base + end;
            // Each piece between tabs on its own: a tab is space, up to its stop.
            for (at, piece) in between_tabs(&code.text, low, high) {
                let (x, column) = self.code_x_on(code, (went.0, went.1), went.2, at, sizes);
                went = (x, column, at);
                self.el().text(piece, sizes.font, sort.color(&t)).select(None, false).place((Align::Start, Align::Center)).at(sizes.text + x, 0.0).show();
            }
        }
    }

    /// The line number, at the row's left however far it is scrolled sideways, over what
    /// scrolls under it; and beside it, for a line starting a block, the fold marker. Only a
    /// line's first row shows these (`first`).
    fn code_numbers(&mut self, code: &Code, line: usize, first: bool, current: usize, sizes: Sizes) {
        let t = self.theme;
        let across = self.scrolls.get(&code.id).map_or(0.0, |offset| offset[0]);
        self.row().size(sizes.numbers, sizes.row).fill(t.panel).at(across, 0.0).pad((0.0, 0.0, 4.0, 0.0)).gap(2.0).align(Align::End, Align::Center).open();
        if first {
            let mut digits = [0u8; 20];
            let shown = number_text(line + 1, &mut digits);
            self.el().text(shown, sizes.font, if line == current { t.text } else { t.text_faint }).show();
        }
        let folded = code.folds.iter().any(|run| run[0] as usize == line + 1);
        match first && (folded || code.block(line).is_some()) {
            true => _ = self.el().icon(if folded { RIGHT } else { DOWN }, 12.0, t.text_faint).show(),
            false => _ = self.el().size(12.0, 12.0).show(),
        }
        self.close();
    }
}

/// The non-empty pieces of `text` from byte `from` to `to` between its tabs, each with its
/// start byte.
fn between_tabs(text: &str, from: usize, to: usize) -> impl Iterator<Item = (usize, &str)> {
    let pieces = text[from..to].split('\t').scan(from, |at, piece| Some((std::mem::replace(at, *at + piece.len() + 1), piece)));
    pieces.filter(|(_, piece)| !piece.is_empty())
}

/// A number written into `digits`, as text.
fn number_text(mut number: usize, digits: &mut [u8; 20]) -> &str {
    let mut at = digits.len();
    loop {
        at -= 1;
        digits[at] = b'0' + (number % 10) as u8;
        number /= 10;
        if number == 0 {
            break;
        }
    }
    std::str::from_utf8(&digits[at..]).unwrap_or("")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::platform::LocalClipboard;

    /// The text with `|` at each cursor and `[` `]` around each selection.
    fn shown(code: &Code) -> String {
        let mut marks: Vec<(usize, char)> = Vec::new();
        for sel in &code.sels {
            match sel.range().is_empty() {
                true => marks.push((sel.cursor, '|')),
                false => marks.extend([(sel.range().start, '['), (sel.range().end, ']')]),
            }
        }
        marks.sort_by(|a, b| b.0.cmp(&a.0).then(b.1.cmp(&a.1)));
        let mut text = code.text.clone();
        for (at, mark) in marks {
            text.insert(at, mark);
        }
        text
    }

    /// Presses keys in a windowless code editor, and types.
    fn keys(code: &mut Code, keys: &[(Key, Mods)]) {
        let mut clipboard = LocalClipboard::default();
        for (key, mods) in keys {
            code.key(*key, *mods, 10, &mut clipboard);
        }
    }
    const NONE: Mods = Mods::NONE;
    const SHIFT: Mods = Mods::SHIFT;
    const CTRL: Mods = Mods::CTRL;

    /// What every line keeps, recomputed from scratch: as it must be after any edit.
    fn same_as_afresh(code: &Code) {
        let afresh = Code::new(code.text.clone(), code.rules);
        assert_eq!((&code.starts, &code.widths), (&afresh.starts, &afresh.widths), "the lines of {:?}", code.text);
    }

    #[test]
    fn a_line_is_coloured_by_the_rules() {
        let mut pieces = Vec::new();
        let open = Rules::RUST.line("let x: u32 = 0x1f; // done", 0, &mut pieces);
        assert_eq!(open, 0);
        let sorts: Vec<(&str, Sort)> = {
            let line = "let x: u32 = 0x1f; // done";
            let mut from = 0;
            pieces.iter().map(|(end, sort)| (&line[std::mem::replace(&mut from, *end)..*end], *sort)).collect()
        };
        assert_eq!(sorts, [("let", Sort::Word), (" x: ", Sort::Plain), ("u32", Sort::Kind), (" = ", Sort::Plain), ("0x1f", Sort::Number), ("; ", Sort::Plain), ("// done", Sort::Comment)]);
        // A multi-line string and a block comment stay open and close on a later line.
        pieces.clear();
        assert_eq!(Rules::RUST.line("let s = \"one", 0, &mut pieces), 2);
        pieces.clear();
        assert_eq!(Rules::RUST.line("two\"; /* three", 2, &mut pieces), IN_COMMENT);
        assert_eq!(pieces, [(4, Sort::Quoted), (6, Sort::Plain), (14, Sort::Comment)]);
        pieces.clear();
        assert_eq!(Rules::RUST.line("four */ x", IN_COMMENT, &mut pieces), 0);
        assert_eq!(pieces, [(7, Sort::Comment), (9, Sort::Plain)]);
        // A character literal is a string, a lifetime is not; an escaped delimiter doesn't
        // close.
        pieces.clear();
        Rules::RUST.line("f::<'a>('\\'', 'b', \"\\\"\")", 0, &mut pieces);
        assert_eq!(pieces, [(8, Sort::Plain), (12, Sort::Quoted), (14, Sort::Plain), (17, Sort::Quoted), (19, Sort::Plain), (23, Sort::Quoted), (24, Sort::Plain)]);
        // Python's triple quotes span lines, its single ones don't.
        pieces.clear();
        assert_eq!(Rules::PYTHON.line("x = \"\"\"doc", 0, &mut pieces), 2);
        assert_eq!(Rules::PYTHON.line("y = 'no end", 0, &mut pieces), 0);
    }

    #[test]
    fn lines_are_kept_right_through_every_edit() {
        let mut code = Code::new("one\ntwo\r\nthree\n\tfour", &Rules::PLAIN);
        same_as_afresh(&code);
        assert_eq!(code.widths, [3, 3, 5, 8]);
        for (at, removed, added) in [(0, 0, "zero\n"), (5, 4, ""), (2, 3, "a\nb\nc"), (0, 0, ""), (4, 6, "\r\n\r\n")] {
            let at = at.min(code.text.len());
            let end = (at + removed).min(code.text.len());
            code.change(vec![(at..end, added.to_string())], None, false);
            same_as_afresh(&code);
        }
        // And a change at more places than are patched one by one.
        let edits: Vec<_> = (0..100).map(|n| (0..0, format!("{n}\n"))).take(1).collect();
        code.change(edits, None, false);
        let many: Vec<(Range<usize>, String)> = (0..code.text.len()).step_by(2).map(|at| (at..at, "\n".to_string())).filter(|(range, _)| code.text.is_char_boundary(range.start)).collect();
        code.change(many, None, false);
        same_as_afresh(&code);
    }

    #[test]
    fn what_is_left_open_is_read_only_as_far_as_needed_and_again_only_until_it_agrees() {
        let mut lines: Vec<String> = (0..1000).map(|n| format!("let x{n} = {n};")).collect();
        lines[10] = "/* a comment".into();
        lines[12] = "that ends */".into();
        let mut code = Code::new(lines.join("\n"), &Rules::RUST);
        assert_eq!(code.open_at(11), IN_COMMENT);
        assert_eq!(code.sure, 12, "no further than asked");
        assert_eq!(code.open_at(900), 0);
        assert_eq!(code.sure, 901);
        // An edit far above: rescanned from there until it matches, and no further.
        let at = code.starts[3] as usize;
        code.change(vec![(at..at, "1".into())], None, false);
        assert_eq!(code.sure, 4);
        assert_eq!(code.open_at(800), 0);
        assert_eq!(code.sure, 901, "it agreed again at once, and kept the rest");
        // An edit opening a comment: everything after it is inside.
        code.change(vec![(at..at, "/*".into())], None, false);
        assert_eq!(code.open_at(5), IN_COMMENT);
        assert_eq!(code.open_at(800), 0, "the comment ends at line 12");
        assert_eq!(code.open_at(11), IN_COMMENT);
        // Each state is what scanning every line from the start gives.
        let mut open = 0;
        for line in 0..code.starts.len() {
            assert_eq!(code.open_at(line), open, "line {line}");
            open = code.rules.read(code.line_text(line), open, None);
        }
    }

    #[test]
    fn enter_keeps_the_indent_and_tab_indents() {
        let mut code = Code::new("fn main() {}", &Rules::RUST);
        code.sels = vec![Sel::at(11)];
        keys(&mut code, &[(Key::Enter, NONE)]);
        assert_eq!(shown(&code), "fn main() {\n    |\n}", "after a bracket that opens, a step further; the closing one under");
        code.put("let x = 1;", true);
        keys(&mut code, &[(Key::Enter, NONE)]);
        assert_eq!(shown(&code), "fn main() {\n    let x = 1;\n    |\n}");
        keys(&mut code, &[(Key::Backspace, NONE)]);
        assert_eq!(shown(&code), "fn main() {\n    let x = 1;\n|\n}", "Backspace in spaces goes back a stop");
        keys(&mut code, &[(Key::Tab, NONE), (Key::Char('a'), CTRL), (Key::Tab, NONE)]);
        assert_eq!(code.text, "    fn main() {\n        let x = 1;\n    \n    }", "Tab indents every line selected, but blank ones");
        keys(&mut code, &[(Key::Tab, SHIFT), (Key::Tab, SHIFT)]);
        assert_eq!(code.text, "fn main() {\nlet x = 1;\n\n}");
        code.tabs = true;
        code.sels = vec![Sel::at(12)];
        keys(&mut code, &[(Key::Tab, NONE)]);
        assert_eq!(shown(&code), "fn main() {\n\t|let x = 1;\n\n}");
        keys(&mut code, &[(Key::Char('z'), CTRL)]);
        assert_eq!(shown(&code), "fn main() {\n|let x = 1;\n\n}");
    }

    #[test]
    fn several_cursors_type_and_move_together() {
        let mut code = Code::new("one\ntwo\nthree", &Rules::PLAIN);
        keys(&mut code, &[(Key::Down, Mods { ctrl: true, alt: true, shift: false }), (Key::Down, Mods { ctrl: true, alt: true, shift: false })]);
        assert_eq!(shown(&code), "|one\n|two\n|three");
        code.put("- ", true);
        assert_eq!(shown(&code), "- |one\n- |two\n- |three");
        keys(&mut code, &[(Key::End, NONE), (Key::Left, SHIFT)]);
        assert_eq!(shown(&code), "- on[e]\n- tw[o]\n- thre[e]");
        keys(&mut code, &[(Key::Char('x'), CTRL), (Key::Char('z'), CTRL)]);
        assert_eq!(shown(&code), "- on[e]\n- tw[o]\n- thre[e]", "undone, the cursors are back as they were");
        keys(&mut code, &[(Key::Home, NONE), (Key::Escape, NONE)]);
        assert_eq!(shown(&code), "|- one\n- two\n- three", "Escape leaves the main cursor: the first");
        // Overlapping cursors merge.
        code.sels = vec![Sel::at(1), Sel::at(2)];
        keys(&mut code, &[(Key::Home, NONE)]);
        assert_eq!(code.sels.len(), 1);
    }

    #[test]
    fn rows_follow_wrapping_and_folding() {
        let mut code = Code::new("fn a() {\n    one two three four five\n    six\n}\nend", &Rules::RUST);
        code.wrap = true;
        code.fit(12);
        code.tops();
        assert_eq!(code.wraps, [1, 3, 1, 1, 1]);
        assert_eq!(code.row_count(), 7);
        let spans: Vec<&str> = (0..7).map(|row| code.row_span(row)).map(|(_, from, to, _)| &code.text[from..to]).collect();
        assert_eq!(spans, ["fn a() {", "    one two ", "three four ", "five", "    six", "}", "end"]);
        // Down keeps its column across rows of one line.
        code.sels = vec![Sel::at(code.starts[1] as usize + 5)];
        keys(&mut code, &[(Key::Down, NONE)]);
        assert_eq!(code.place(code.sels[0].cursor), (2, 5));
        // Folding hides the block's lines; a cursor in them moves to the standing line.
        assert!(code.fold(0));
        code.tops();
        assert_eq!(code.row_count(), 3);
        assert_eq!((code.line_at(1), code.line_at(2)), (3, 4));
        assert_eq!(code.sels[0].cursor, 8);
        keys(&mut code, &[(Key::Down, NONE)]);
        assert_eq!(code.line_of(code.sels[0].cursor), 3, "Down goes past what is folded");
        // An edit on a fold's standing line unfolds it.
        code.sels = vec![Sel::at(2)];
        code.put("x", true);
        assert!(code.folds.is_empty());
        same_as_afresh(&code);
    }

    #[test]
    fn find_and_replace_with_case_and_whole_words() {
        let mut code = Code::new("Cat cat catalog\ncat", &Rules::PLAIN);
        code.finding.words = "cat".into();
        assert_eq!(code.count(), (4, 0));
        code.finding.whole = true;
        assert_eq!(code.count(), (3, 0));
        code.finding.case = true;
        assert_eq!(code.count(), (2, 0));
        keys(&mut code, &[(Key::F(3), NONE)]);
        assert_eq!(shown(&code), "Cat [cat] catalog\ncat");
        assert_eq!(code.count(), (2, 1));
        keys(&mut code, &[(Key::F(3), NONE), (Key::F(3), NONE)]);
        assert_eq!(shown(&code), "Cat [cat] catalog\ncat", "it goes round");
        keys(&mut code, &[(Key::F(3), SHIFT)]);
        assert_eq!(shown(&code), "Cat cat catalog\n[cat]");
        code.finding.with = "dog".into();
        code.replace_one();
        assert_eq!(shown(&code), "Cat [cat] catalog\ndog");
        code.finding.case = false;
        code.replace_all();
        assert_eq!(code.text, "dog dog catalog\ndog");
        keys(&mut code, &[(Key::Char('z'), CTRL)]);
        assert_eq!(code.text, "Cat cat catalog\ndog", "replacing all is undone as one");
        // Non-ASCII letters are found case-insensitively.
        code.set_text("Ärger ärger");
        (code.finding.words, code.finding.whole) = ("ÄRGER".into(), false);
        assert_eq!(code.count().0, 2);
    }

    #[test]
    fn brackets_pair_only_in_code() {
        let mut code = Code::new("f(a, \"(\", [b]) // )\n{\n  g()\n}", &Rules::RUST);
        code.sels = vec![Sel::at(1)];
        assert_eq!(code.paired(), Some([1, 13]));
        code.sels = vec![Sel::at(14)];
        assert_eq!(code.paired(), Some([13, 1]), "the one before the cursor, where none is after it");
        code.sels = vec![Sel::at(20)];
        assert_eq!(code.paired(), Some([20, 28]), "over lines");
        code.sels = vec![Sel::at(6)];
        assert_eq!(code.paired(), None, "a bracket in a quote pairs with nothing");
    }

    #[test]
    fn the_pointer_selects_by_letter_word_and_line() {
        let mut code = Code::new("one two\nthree", &Rules::PLAIN);
        code.point(5, 1, true, NONE);
        code.point(10, 1, false, NONE);
        assert_eq!(shown(&code), "one t[wo\nth]ree");
        code.point(5, 2, true, NONE);
        assert_eq!(shown(&code), "one [two]\nthree");
        code.point(5, 3, true, NONE);
        assert_eq!(shown(&code), "[one two\n]three");
        code.point(10, 1, true, NONE);
        code.point(1, 1, true, Mods { alt: true, ..NONE });
        assert_eq!(code.sels.len(), 2, "a press with Alt is one cursor more");
        code.point(2, 1, true, Mods { alt: true, ..NONE });
        assert_eq!(code.sels.len(), 3);
    }
}