perga 0.1.5

A terminal Markdown browser with a persistent file tree, tabs, wiki-links and backlinks
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
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
//! The `App` struct, the blocking event loop, and top-level state transitions.
//!
//! Everything here is deliberately terminal-free: `App` is constructed, fed
//! [`Action`]s, and asserted on without a backend. The loop that reads from the
//! terminal lives in [`run`], and it does nothing but translate messages into
//! actions and hand them to [`App::update`].

use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::SystemTime;

use crossbeam_channel::Receiver;
use ratatui::layout::Rect;

use crate::action::Action;
use crate::config::keymap::Keymap;
use crate::config::schema::{
    EditorConfig, FilesConfig, GeneralConfig, SearchConfig, SessionConfig, ThemeConfig, UiConfig,
    WatchConfig, WikiLinkConfig,
};
use crate::doc::document::Document;
use crate::doc::highlight::Highlighter;
use crate::doc::links;
use crate::doc::render::{RenderedDocument, Renderer};
use crate::editor::buffer::{self, EditorState, SaveError};
use crate::search::content::{self, SearchEvent, SearchHandle};
use crate::search::in_doc::FindState;
use crate::search::SearchState;
use crate::terminal::{self, Tui};
use crate::theme::Theme;
use crate::ui;
use crate::ui::layout::{self, SidebarPlacement};
use crate::ui::overlay::prompt::{TextEdit, TextInput};
use crate::ui::sidebar::SidebarMode;
use crate::vault::index::{self, IndexEvent, WikiResolution};
use crate::vault::walker::{WalkEvent, WalkOptions};
use crate::vault::watch::WatchEvent;
use crate::vault::Vault;

/// Which pane has focus.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Focus {
    /// The sidebar.
    Sidebar,
    /// The document viewport.
    #[default]
    Viewport,
    /// An overlay, which swallows all input except `Esc` and `Ctrl+C`.
    Overlay,
}

/// Whether a tab is being read or edited.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TabMode {
    /// Reading. The mode perga opens in, always.
    #[default]
    Read,
    /// Editing.
    Edit,
}

impl TabMode {
    /// The label shown at the left of the status bar.
    pub fn label(self) -> &'static str {
        match self {
            TabMode::Read => "READ",
            TabMode::Edit => "EDIT",
        }
    }
}

/// Somewhere a tab has been, exactly enough to go back to it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Location {
    /// The document.
    pub path: PathBuf,
    /// The heading the reader was at, when they arrived at one.
    pub anchor: Option<String>,
    /// The rendered line offset, restored exactly on the way back.
    pub scroll: usize,
}

/// The most locations one tab remembers.
///
/// A cap rather than an unbounded stack: cyclic links (A → B → A → …) are
/// ordinary in a wiki, and without a cap a reader following them long enough
/// grows the stack without limit.
pub const MAX_HISTORY: usize = 100;

/// One tab's back and forward stacks.
#[derive(Debug, Clone, Default)]
pub struct History {
    back: Vec<Location>,
    forward: Vec<Location>,
}

impl History {
    /// Record where the reader is leaving from.
    ///
    /// Navigating anywhere new truncates the forward stack: the future the
    /// reader had is not the future they are choosing.
    pub fn push(&mut self, from: Location) {
        self.forward.clear();
        self.back.push(from);

        if self.back.len() > MAX_HISTORY {
            self.back.remove(0);
        }
    }

    /// Step back, given where the reader is now.
    pub fn back(&mut self, current: Location) -> Option<Location> {
        let previous = self.back.pop()?;
        self.forward.push(current);
        Some(previous)
    }

    /// Step forward, given where the reader is now.
    pub fn forward(&mut self, current: Location) -> Option<Location> {
        let next = self.forward.pop()?;
        self.back.push(current);
        Some(next)
    }

    /// How many steps back are available.
    pub fn back_len(&self) -> usize {
        self.back.len()
    }

    /// How many steps forward are available.
    pub fn forward_len(&self) -> usize {
        self.forward.len()
    }
}

/// One tab: a document, its history, and its own scroll and find state.
#[derive(Debug, Default)]
pub struct Tab {
    /// The open document. `None` shows the welcome screen.
    pub doc: Option<Document>,
    /// The document's layout at the current width.
    pub layout: RenderedDocument,
    /// Rendered line offset. Not a `u16`: a 100,000-line document overflows it.
    pub scroll: usize,
    /// Horizontal offset, for clipped code blocks and wide tables.
    pub hscroll: u16,
    /// Read or edit.
    pub mode: TabMode,
    /// Where this tab has been. Per tab, never shared.
    pub history: History,
    /// The link `Enter` would follow, as an index into the document's links.
    pub focused_link: Option<usize>,
    /// This tab's find state, kept when the bar closes so `Ctrl+F` reopens
    /// where the reader left off.
    pub find: Option<FindState>,
    /// The edit buffer, while this tab is in edit mode.
    pub editor: Option<EditorState>,
    /// Whether the buffer has edits that are not on disk.
    ///
    /// Owned by the tab rather than by the editor so that the tab bar and the
    /// quit prompt can ask about a tab that is not the active one.
    pub dirty: bool,
}

impl Tab {
    /// A tab showing this document.
    pub fn with_document(doc: Document) -> Self {
        Tab {
            doc: Some(doc),
            ..Tab::default()
        }
    }

    /// The label shown in the tab bar.
    ///
    /// A tab with no document open shows the welcome screen.
    pub fn label(&self) -> &str {
        match &self.doc {
            Some(doc) => doc.label(),
            None => "welcome",
        }
    }

    /// The label as the tab bar draws it: truncated, with a dirty marker.
    pub fn display_label(&self) -> String {
        let label = truncate_middle(self.label(), MAX_TAB_LABEL);
        if self.dirty {
            format!("{DIRTY_MARKER} {label}")
        } else {
            label
        }
    }

    /// The furthest the viewport may scroll, keeping one screen in view.
    ///
    /// `None` while the document has not been fully measured, in which case
    /// scrolling is not clamped and the scrollbar shows as indeterminate.
    fn max_scroll(&self, height: u16) -> Option<usize> {
        let doc = self.doc.as_ref()?;
        let total = self.layout.total_lines(doc)?;
        Some(total.saturating_sub(usize::from(height).max(1)))
    }
}

/// The most rows the quick switcher scores and shows.
pub const SWITCHER_ROWS: usize = 100;

/// The most documents the session's recent list remembers.
pub const MAX_RECENT: usize = 50;

/// The most tabs that may be open at once.
///
/// Past this the active tab is reused: twenty labels is already more than fits
/// on a comfortable terminal, and an unbounded tab bar is a worse answer than
/// saying so.
pub const MAX_TABS: usize = 20;

/// The widest a tab label is drawn, in columns.
pub const MAX_TAB_LABEL: usize = 20;

/// Marks a tab whose buffer has unsaved edits.
pub const DIRTY_MARKER: &str = "●";

/// The byte offset a zero-based source line begins at.
fn offset_of_source_line(source: &str, line: usize) -> usize {
    if line == 0 {
        return 0;
    }

    source
        .match_indices('\n')
        .nth(line - 1)
        .map_or(source.len(), |(at, _)| at + 1)
}

/// Give a typed path a Markdown extension when it has none.
fn with_markdown_extension(query: &str, files: &FilesConfig) -> String {
    let query = query.trim();
    if files.is_markdown(Path::new(query)) {
        return query.to_string();
    }

    let extension = files.extensions.first().map_or("md", String::as_str);
    format!("{query}.{extension}")
}

/// Shorten a label to `width` columns, eliding the middle.
///
/// The middle rather than the tail: `2024-01-conference-notes.md` and
/// `2024-01-conference-slides.md` are told apart by their ends, and a tail
/// ellipsis makes every file in a dated vault look the same.
pub fn truncate_middle(label: &str, width: usize) -> String {
    use unicode_width::UnicodeWidthChar;

    let total: usize = label.chars().map(|c| c.width().unwrap_or(0)).sum();
    if total <= width || width < 3 {
        return label.to_string();
    }

    let keep = width - 1;
    let head_width = keep.div_ceil(2);
    let tail_width = keep - head_width;

    let mut head = String::new();
    let mut used = 0;
    for c in label.chars() {
        let w = c.width().unwrap_or(0);
        if used + w > head_width {
            break;
        }
        head.push(c);
        used += w;
    }

    let mut tail = String::new();
    let mut used = 0;
    for c in label.chars().rev() {
        let w = c.width().unwrap_or(0);
        if used + w > tail_width {
            break;
        }
        tail.insert(0, c);
        used += w;
    }

    format!("{head}…{tail}")
}

/// The sidebar's own state.
#[derive(Debug, Clone)]
pub struct Sidebar {
    /// Whether the user has it switched on.
    pub visible: bool,
    /// The width the user has chosen, in columns.
    pub width: u16,
    /// Which of the four modes is showing.
    pub mode: SidebarMode,
    /// The tree filter being typed, if the filter line is open.
    ///
    /// Separate from the filter the tree is applying: closing the line keeps
    /// the filter, and cancelling it clears both.
    pub filter: Option<TextInput>,
    /// Which heading the outline mode has selected.
    ///
    /// Selection is per mode: moving down the outline must not move the tree
    /// cursor underneath it.
    pub outline_selected: usize,
}

/// How prominent a status message is.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Severity {
    /// Ordinary feedback.
    Info,
    /// Something did not work but the application carries on.
    Warning,
    /// Something failed.
    Error,
}

/// The status bar's transient content.
#[derive(Debug, Clone, Default)]
pub struct StatusLine {
    /// The current message, if any.
    pub message: Option<(String, Severity)>,
    /// A partially typed key sequence, shown as `g…`.
    pub pending: Option<String>,
}

impl StatusLine {
    /// Show a message.
    pub fn set(&mut self, text: impl Into<String>, severity: Severity) {
        self.message = Some((text.into(), severity));
    }

    /// Clear any message.
    pub fn clear(&mut self) {
        self.message = None;
    }
}

/// Which overlay is open, if any.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Overlay {
    /// The keybinding reference, generated from the resolved keymap.
    Help {
        /// How far the reference is scrolled.
        scroll: u16,
    },
    /// The find bar. The state itself lives on the tab, which is what makes
    /// find per tab; this only says the bar has focus.
    Find,
    /// A wiki-link that matched more than one page.
    Disambiguate {
        /// The page name as written.
        page: String,
        /// The candidates, relative to the vault root.
        candidates: Vec<PathBuf>,
        /// The heading fragment to apply once one is chosen.
        anchor: Option<String>,
        /// Which candidate is selected.
        selected: usize,
    },
    /// A single-line prompt. What it is for is in the [`PromptKind`].
    Prompt {
        /// What the prompt will do with what is typed.
        kind: PromptKind,
        /// The line being edited.
        input: TextInput,
    },
    /// The fuzzy quick switcher.
    Switcher {
        /// The query being typed.
        input: TextInput,
        /// The rows, best match first, or the recent list for an empty query.
        rows: Vec<SwitcherRow>,
        /// Which row is selected.
        selected: usize,
    },
    /// A yes/no/cancel question.
    Confirm {
        /// What is being asked.
        question: String,
        /// The choices, as `(key, label)`.
        choices: Vec<(char, String)>,
        /// What confirming will do.
        action: ConfirmAction,
    },
    /// Link hint mode: every visible link wears a label.
    Hints {
        /// The links wearing labels, in reading order.
        links: Vec<usize>,
        /// The label typed so far.
        typed: String,
    },
}

/// What a confirmation is about.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConfirmAction {
    /// Leaving edit mode with unsaved edits.
    LeaveEditMode,
    /// Quitting with unsaved edits in some tab.
    Quit,
    /// Saving over a file that changed on disk since it was loaded.
    OverwriteChanged,
    /// Creating the page a broken wiki-link named.
    CreatePage {
        /// Where it would be created.
        path: PathBuf,
    },
    /// Restoring unsaved text left behind by an earlier run.
    RestoreRecovery {
        /// The document the text belongs to.
        path: PathBuf,
    },
}

/// What a prompt will do with what is typed into it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PromptKind {
    /// Run a project-wide search.
    ProjectSearch,
    /// Create a file at the path typed.
    NewFile,
    /// Rename the active document.
    RenameDocument,
    /// Rename the entry the tree has selected.
    RenameTreeEntry,
}

impl PromptKind {
    /// The prefix drawn at the left of the prompt.
    pub fn prefix(self) -> &'static str {
        match self {
            PromptKind::ProjectSearch => "search: ",
            PromptKind::NewFile => "new file: ",
            PromptKind::RenameDocument | PromptKind::RenameTreeEntry => "rename to: ",
        }
    }
}

/// One row of the quick switcher.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SwitcherRow {
    /// The path, relative to the vault root.
    pub path: PathBuf,
    /// Which characters of the path matched, for highlighting.
    pub indices: Vec<u32>,
    /// Whether this row is the offer to create a file that does not exist.
    pub create: bool,
}

/// The whole application state.
pub struct App {
    /// The open directory: its root, its tree, and its index.
    pub vault: Vault,
    /// The `[files]` table, which the tree and the walker both read.
    pub files: FilesConfig,
    /// The `[general]` table.
    pub general: GeneralConfig,
    /// The `[wikilinks]` table.
    pub wikilinks: WikiLinkConfig,
    /// The `[search]` table.
    pub search_config: SearchConfig,
    /// The `[editor]` table.
    pub editor_config: EditorConfig,
    /// Writes perga made, so the watcher does not report them back.
    pub own_writes: crate::vault::watch::OwnWrites,
    /// Set when the active document should be handed to `$EDITOR`.
    pub external_edit: Option<PathBuf>,
    /// The `[watch]` table.
    pub watch_config: WatchConfig,
    /// The `[session]` table.
    pub session_config: SessionConfig,
    /// The `[theme]` table. Its `name` follows a runtime switch, so the file
    /// watcher re-reads whichever theme is showing rather than the one that
    /// was configured.
    pub theme_config: ThemeConfig,
    /// Whether `--theme` was given, which pins the theme for this run.
    ///
    /// A session records the theme so a runtime switch survives a restart, and
    /// a flag on the command line is a deliberate choice for *this* run that
    /// the session must not override.
    pub theme_pinned: bool,
    /// The running watch. Dropping it stops watching.
    watch: Option<crate::vault::watch::WatchHandle>,
    /// The watch on the theme directory, for hot reload while authoring one.
    theme_watch: Option<crate::vault::watch::WatchHandle>,
    /// The last project-wide search.
    pub search: SearchState,
    /// Documents opened this session, most recent first.
    ///
    /// What the quick switcher shows before anything has been typed.
    pub recent: Vec<PathBuf>,
    /// The running project search. Dropping it cancels the search.
    search_handle: Option<SearchHandle>,
    /// The fuzzy matcher, reused between keystrokes for its scratch buffers.
    fuzzy: crate::search::fuzzy::Fuzzy,
    /// How a project search reports back.
    search_sink: Option<Arc<dyn Fn(SearchEvent) + Send + Sync>>,
    /// How the index reports back, once the walk has said what to index.
    ///
    /// Held rather than passed in because the build starts when the *walk*
    /// finishes, which is several frames after the application was built.
    index_sink: Option<Arc<dyn Fn(IndexEvent) + Send + Sync>>,
    /// The resolved theme. Nothing in the UI hardcodes a colour.
    pub theme: Theme,
    /// Syntax highlighting, which loads on a background thread.
    pub highlighter: Highlighter,
    /// The resolved keymap, and the source of the help overlay.
    pub keymap: Keymap,
    /// Presentation settings.
    pub ui: UiConfig,
    /// Open tabs. Never empty.
    pub tabs: Vec<Tab>,
    /// Index into [`App::tabs`].
    pub active_tab: usize,
    /// The sidebar.
    pub sidebar: Sidebar,
    /// Which pane has focus.
    pub focus: Focus,
    /// The open overlay, if any.
    pub overlay: Option<Overlay>,
    /// The status bar.
    pub status: StatusLine,
    /// Set when the application should exit after the current update.
    pub should_quit: bool,
    /// Set when the application should suspend itself after the current update.
    pub should_suspend: bool,
    /// The process exit code.
    pub exit_code: u8,
    /// The last known terminal size, so layout decisions can be made outside a
    /// draw call.
    pub area: Rect,
    /// Whether mouse capture is on.
    pub mouse_capture: bool,
}

impl App {
    /// A new application with built-in defaults.
    pub fn new(theme: Theme, keymap: Keymap, ui: UiConfig, files: FilesConfig) -> Self {
        let sidebar = Sidebar {
            visible: ui.sidebar_visible,
            width: ui.sidebar_width,
            mode: ui.sidebar_default_mode,
            filter: None,
            outline_selected: 0,
        };
        let mouse_capture = ui.mouse;

        // Anything the keymap could not make sense of is the user's to know
        // about; it is never a hard error.
        let mut status = StatusLine::default();
        if let Some(warning) = keymap.warnings().first() {
            status.set(warning.clone(), Severity::Warning);
        }

        App {
            vault: Vault::new(".", &files),
            files,
            general: GeneralConfig::default(),
            wikilinks: WikiLinkConfig::default(),
            search_config: SearchConfig::default(),
            editor_config: EditorConfig::default(),
            own_writes: crate::vault::watch::OwnWrites::default(),
            external_edit: None,
            watch_config: WatchConfig::default(),
            session_config: SessionConfig::default(),
            theme_config: ThemeConfig::default(),
            theme_pinned: false,
            watch: None,
            theme_watch: None,
            search: SearchState::default(),
            recent: Vec::new(),
            search_handle: None,
            fuzzy: crate::search::fuzzy::Fuzzy::new(),
            search_sink: None,
            index_sink: None,
            theme,
            highlighter: Highlighter::new(),
            keymap,
            ui,
            tabs: vec![Tab::default()],
            active_tab: 0,
            sidebar,
            focus: Focus::Viewport,
            overlay: None,
            status,
            should_quit: false,
            should_suspend: false,
            exit_code: 0,
            area: Rect::default(),
            mouse_capture,
        }
    }

    /// The active tab.
    pub fn tab(&self) -> &Tab {
        &self.tabs[self.active_tab]
    }

    /// The active document's path, relative to the vault root.
    pub fn title_path(&self) -> Option<String> {
        self.tab()
            .doc
            .as_ref()
            .map(|doc| doc.display_path(&self.vault.root))
    }

    /// The active document's path relative to the vault root, for matching
    /// against tree rows.
    pub fn active_path(&self) -> Option<PathBuf> {
        let doc = self.tab().doc.as_ref()?;
        self.vault.relative(&doc.path).map(Path::to_path_buf)
    }

    /// The scroll position, as `current/total`, once the total is known.
    pub fn scroll_position(&self) -> Option<(usize, usize)> {
        let tab = self.tab();
        let doc = tab.doc.as_ref()?;
        let total = tab.layout.total_lines(doc)?;
        Some((tab.scroll.saturating_add(1).min(total.max(1)), total))
    }

    /// Point the vault root at a directory, discarding any tree already built
    /// for the previous one.
    pub fn set_vault_root(&mut self, root: impl AsRef<Path>) {
        self.vault = Vault::new(root.as_ref(), &self.files);
    }

    /// Start walking the vault, reporting each batch back through `sink`.
    ///
    /// The sink is what turns a worker message into an [`Action`]; nothing on
    /// the walker thread touches application state.
    pub fn start_walk(&mut self, sink: impl Fn(WalkEvent) + Send + Sync + 'static) {
        let options = WalkOptions {
            respect_gitignore: self.files.respect_gitignore,
            follow_symlinks: self.general.follow_symlinks,
        };
        self.vault.start_walk(options, sink);
    }

    /// Register how the index reports back.
    ///
    /// The build itself starts when the walk finishes and has said which files
    /// the cache does not already cover.
    pub fn on_index(&mut self, sink: impl Fn(IndexEvent) + Send + Sync + 'static) {
        self.index_sink = Some(Arc::new(sink));
    }

    /// Register how a project search reports back.
    pub fn on_search(&mut self, sink: impl Fn(SearchEvent) + Send + Sync + 'static) {
        self.search_sink = Some(Arc::new(sink));
    }

    /// A renderer for the current theme at a given content width.
    pub fn renderer(&self, width: u16) -> Renderer {
        Renderer::new(&self.theme, self.highlighter.clone(), width)
            .with_heading_markers(self.ui.show_heading_markers)
    }

    /// The content area of the viewport, inside its border.
    pub fn viewport_inner(&self) -> Rect {
        let viewport = self.frames().viewport;
        Rect {
            x: viewport.x.saturating_add(1),
            y: viewport.y.saturating_add(1),
            width: viewport.width.saturating_sub(2),
            height: viewport.height.saturating_sub(2),
        }
    }

    /// Open a document in the active tab.
    pub fn open(&mut self, doc: Document) {
        if let Some(reason) = doc.read_only {
            self.status.set(reason.message(), Severity::Warning);
        }
        // Everything about the *view* is replaced; the tab's history and its
        // read-or-edit mode belong to the tab, not to the document in it.
        let path = doc.path.clone();
        let tab = &mut self.tabs[self.active_tab];
        tab.doc = Some(doc);
        tab.layout = RenderedDocument::new();
        tab.scroll = 0;
        tab.hscroll = 0;
        tab.focused_link = None;

        self.remember(&path);
        self.reveal_active_document();
    }

    /// The layout for the current state and terminal size.
    pub fn frames(&self) -> layout::Frames {
        layout::compute(layout::LayoutInput {
            area: self.area,
            sidebar_visible: self.sidebar.visible,
            sidebar_width: self.sidebar.width,
            narrow_threshold: self.ui.narrow_threshold,
            tab_count: self.tabs.len(),
            always_show_tabs: self.ui.always_show_tabs,
            show_status_bar: self.ui.show_status_bar,
        })
    }

    /// Apply one action. The only place application state changes.
    pub fn update(&mut self, action: Action) {
        // Any deliberate action supersedes whatever the status bar was saying.
        if !matches!(action, Action::Resize(..)) {
            self.status.clear();
        }

        match action {
            // -- Lifecycle -----------------------------------------------
            Action::Quit => self.quit(),
            Action::ForceQuit(code) => {
                // Section 13: a signal gives no chance to ask, so unsaved text
                // is parked where the next run can offer it back.
                self.write_recovery_files();
                self.should_quit = true;
                self.exit_code = code;
            }
            Action::Suspend => self.should_suspend = true,
            Action::Resize(width, height) => {
                // The chosen width is deliberately not clamped here. Layout
                // clamps what it draws, so a temporary shrink of the terminal
                // narrows the sidebar on screen without destroying the width
                // the user picked; widening the terminal restores it.
                self.area = Rect::new(0, 0, width, height);
            }

            // -- Focus and chrome ----------------------------------------
            Action::FocusNext => self.cycle_focus(true),
            Action::FocusPrev => self.cycle_focus(false),
            Action::ToggleSidebar => self.toggle_sidebar(),
            Action::SidebarWiden => self.resize_sidebar(1),
            Action::SidebarNarrow => self.resize_sidebar(-1),
            Action::SetSidebarMode(mode) => {
                self.sidebar.mode = mode;
                if !self.sidebar.visible {
                    self.sidebar.visible = true;
                }
            }
            Action::ToggleMouse => self.toggle_mouse(),
            Action::ToggleHelp => self.toggle_help(),
            Action::Escape => self.escape(),
            Action::SyntaxReady => self.status.clear(),

            // -- Viewport scrolling --------------------------------------
            Action::ScrollLineDown => self.scroll_by(1),
            Action::ScrollLineUp => self.scroll_by(-1),
            Action::ScrollHalfPageDown => self.scroll_by(i64::from(self.page() / 2).max(1)),
            Action::ScrollHalfPageUp => self.scroll_by(-i64::from(self.page() / 2).max(1)),
            Action::ScrollPageDown => self.scroll_by(i64::from(self.page()).max(1)),
            Action::ScrollPageUp => self.scroll_by(-i64::from(self.page()).max(1)),
            Action::ScrollWheelDown => {
                self.scroll_by(i64::from(self.ui.mouse_scroll_lines).max(1));
            }
            Action::ScrollWheelUp => {
                self.scroll_by(-i64::from(self.ui.mouse_scroll_lines).max(1));
            }
            Action::ScrollTop => self.scroll_to_top(),
            Action::ScrollBottom => self.scroll_to_bottom(),
            Action::ScrollLeft => self.scroll_horizontally(-1),
            Action::ScrollRight => self.scroll_horizontally(1),
            Action::PrevHeading => self.scroll_to_heading(false),
            Action::NextHeading => self.scroll_to_heading(true),

            // -- Vault walking -------------------------------------------
            Action::VaultEntries(entries) => {
                for entry in &entries {
                    if !entry.is_dir && self.files.is_markdown(&entry.path) {
                        self.vault
                            .markdown
                            .push((entry.path.clone(), entry.mtime, entry.size));
                    }
                }
                self.vault.tree.insert_all(entries);
                // A document opened before its row existed still gets its
                // path expanded, as soon as the batch carrying it lands.
                self.reveal_active_document();
            }
            Action::VaultWalkFinished(total) => {
                self.vault.tree.complete = true;
                self.vault.tree.entries = total;
                self.reveal_active_document();
                self.start_indexing();
            }
            Action::IndexBatch(entries) => {
                for (path, entry) in entries {
                    self.vault.index.insert(path, entry);
                }
            }
            Action::IndexFinished => self.finish_indexing(),
            Action::FilesChanged(paths) => self.files_changed(&paths),
            Action::FilesRemoved(paths) => self.files_removed(&paths),
            Action::WatchStopped(reason) => self.status.set(reason, Severity::Warning),
            Action::ReloadTheme => self.reload_theme(),
            Action::CycleTheme => self.cycle_theme(),
            Action::VaultWalkFailed(reason) => self.status.set(reason, Severity::Error),

            // -- Sidebar ---------------------------------------------------
            Action::SidebarDown => self.move_sidebar_selection(1),
            Action::SidebarUp => self.move_sidebar_selection(-1),
            Action::SidebarActivate => self.activate_sidebar_selection(),
            Action::SidebarBack => self.sidebar_back(),
            Action::TreeToggleHidden => {
                self.vault.tree.toggle_hidden();
                let state = if self.vault.tree.include_hidden {
                    "shown"
                } else {
                    "hidden"
                };
                self.status
                    .set(format!("Dotted entries {state}"), Severity::Info);
            }
            Action::TreeToggleAllFiles => {
                self.vault.tree.toggle_all_files();
                let state = if self.vault.tree.show_all {
                    "All files"
                } else {
                    "Markdown files only"
                };
                self.status.set(state, Severity::Info);
            }
            Action::TreeFilter if self.sidebar.mode == SidebarMode::Search => {
                self.open_prompt(PromptKind::ProjectSearch);
            }
            Action::TreeFilter => self.open_tree_filter(),
            Action::TreeFilterEdit(edit) => self.edit_tree_filter(edit),
            Action::TreeFilterAccept => self.sidebar.filter = None,
            Action::TreeFilterCancel => {
                self.sidebar.filter = None;
                self.vault.tree.set_filter(None);
            }
            Action::OpenPath(path) => self.open_path(&path),

            // -- Project search and the quick switcher ----------------------
            Action::OpenProjectSearch => self.open_prompt(PromptKind::ProjectSearch),
            Action::PromptEdit(edit) => self.edit_prompt(edit),
            Action::PromptAccept => self.accept_prompt(),
            Action::SearchHits(hits) => {
                self.search.hits.extend(hits);
            }
            Action::SearchFinished { total, truncated } => {
                self.search.running = false;
                self.search.truncated = truncated;
                self.search.elapsed = self.search.started.map(|at| at.elapsed());
                let _ = total;
                self.search_handle = None;
            }
            Action::SearchFailed(error) => {
                self.search.running = false;
                self.search.error = Some(error);
                self.search_handle = None;
            }
            Action::OpenQuickSwitcher => self.open_switcher(),
            Action::SwitcherEdit(edit) => self.edit_switcher(edit),
            Action::SwitcherMove(delta) => self.move_switcher(delta),
            Action::SwitcherAccept { new_tab } => self.accept_switcher(new_tab),

            // -- Editing ---------------------------------------------------
            Action::EnterEditMode => self.enter_edit_mode(),
            Action::EditInput(key) => self.edit_input(key),
            Action::EditPaste(text) => self.edit_paste(&text),
            Action::Save => self.save(false),
            Action::Undo => self.edit_history(false),
            Action::Redo => self.edit_history(true),
            Action::ReloadDocument => self.reload_document(),
            Action::NewFile => self.open_prompt(PromptKind::NewFile),
            Action::RenameDocument => self.open_prompt(PromptKind::RenameDocument),
            Action::TreeRename => self.open_prompt(PromptKind::RenameTreeEntry),
            Action::OpenInExternalEditor => self.hand_to_external_editor(),
            Action::CopyDocumentPath => self.copy_document_path(),

            // -- Find in document -------------------------------------------
            Action::OpenFindInDocument => self.open_find(),
            Action::FindEdit(edit) => self.edit_find(edit),
            Action::FindNext => self.step_find(true),
            Action::FindPrev => self.step_find(false),
            Action::CloseFind => self.close_find(),

            // -- Tabs ------------------------------------------------------
            Action::NewTab => self.new_tab(),
            Action::CloseTab => self.close_tab(),
            Action::NextTab => self.switch_tab(1),
            Action::PrevTab => self.switch_tab(-1),

            // -- Links and history ----------------------------------------
            Action::NextLink => self.cycle_link(true),
            Action::PrevLink => self.cycle_link(false),
            Action::FollowLink => self.follow_focused_link(),
            Action::FollowLinkInNewTab => self.follow_focused_link_in_new_tab(),
            Action::HintMode => self.open_hint_mode(),
            Action::FollowHintedLink(index) => self.follow_link(index),
            Action::ChooseCandidate(index) => self.choose_candidate(index),
            Action::Confirm(key) => self.confirm(key),
            Action::HistoryBack => self.navigate_history(false),
            Action::HistoryForward => self.navigate_history(true),
        }
    }

    /// The viewport's height in rendered lines.
    fn page(&self) -> u16 {
        self.viewport_inner().height.max(1)
    }

    /// Scroll the active tab by a signed number of lines.
    fn scroll_by(&mut self, delta: i64) {
        let height = self.page();
        let renderer = self.renderer(self.viewport_inner().width);
        let tab = &mut self.tabs[self.active_tab];

        let Some(doc) = &tab.doc else { return };

        let wanted = (tab.scroll as i64 + delta).max(0) as usize;

        // Scrolling down past what has been measured is what triggers the next
        // chunk of measurement; scrolling up never needs any.
        if delta > 0 {
            tab.layout.window(doc, &renderer, wanted, height);
        }

        tab.scroll = match tab.max_scroll(height) {
            Some(max) => wanted.min(max),
            // Not fully measured yet: allow the move, and the clamp lands once
            // the total is known rather than blocking the scroll now.
            None => wanted.min(tab.layout.measured_lines()),
        };
    }

    /// Jump to the top of the document.
    fn scroll_to_top(&mut self) {
        self.tabs[self.active_tab].scroll = 0;
    }

    /// Jump to the bottom, measuring the rest of the document to find it.
    fn scroll_to_bottom(&mut self) {
        let height = self.page();
        let renderer = self.renderer(self.viewport_inner().width);
        let tab = &mut self.tabs[self.active_tab];

        let Some(doc) = &tab.doc else { return };

        // Measured in chunks, so a jump to the end of a very large document
        // costs several frames rather than one long freeze.
        if !tab.layout.resolve_all(doc, &renderer) {
            self.status.set("Measuring the document…", Severity::Info);
        }

        if let Some(max) = tab.max_scroll(height) {
            tab.scroll = max;
        } else {
            tab.scroll = tab.layout.measured_lines();
        }
    }

    /// Scroll a clipped code block or wide table sideways.
    fn scroll_horizontally(&mut self, delta: i16) {
        let tab = &mut self.tabs[self.active_tab];
        if tab.doc.is_none() {
            return;
        }
        tab.hscroll =
            (i32::from(tab.hscroll) + i32::from(delta)).clamp(0, i32::from(u16::MAX)) as u16;
    }

    /// Scroll to the next or previous heading.
    fn scroll_to_heading(&mut self, forward: bool) {
        let renderer = self.renderer(self.viewport_inner().width);
        let tab = &mut self.tabs[self.active_tab];

        let Some(doc) = &tab.doc else { return };

        let offsets: Vec<usize> = doc.outline.iter().map(|h| h.offset).collect();
        let current = tab.scroll;

        let mut target = None;
        for offset in offsets {
            let Some(line) = tab.layout.line_of_offset(doc, &renderer, offset) else {
                continue;
            };
            if forward && line > current {
                target = Some(line);
                break;
            }
            if !forward && line < current {
                target = Some(line);
            }
        }

        if let Some(line) = target {
            tab.scroll = line;
        }
    }

    /// Expand the selected directory, or open the selected file.
    fn tree_expand_or_open(&mut self) {
        if self.vault.tree.expand_selected() {
            return;
        }

        let Some(node) = self.vault.tree.selected() else {
            return;
        };
        let path = node.path.clone();
        self.open_path(&path);
    }

    /// Open a path from the tree, relative to the vault root.
    ///
    /// A file perga does not render is handed to the desktop opener rather
    /// than shown as mojibake.
    fn open_path(&mut self, relative: &Path) {
        let absolute = self.vault.absolute(relative);

        if !self.files.is_markdown(&absolute) {
            match crate::vault::open_external(&absolute) {
                Ok(()) => self.status.set(
                    format!("Opened {} externally", relative.display()),
                    Severity::Info,
                ),
                Err(e) => self
                    .status
                    .set(format!("Cannot open externally: {e}"), Severity::Error),
            }
            return;
        }

        match Document::load(&absolute) {
            Ok(document) => {
                self.open(document);
                // A sidebar drawn over the viewport is in the way of the
                // document it has just opened.
                if self.sidebar.visible
                    && self.frames().sidebar_placement == SidebarPlacement::Overlaid
                {
                    self.sidebar.visible = false;
                    self.focus = Focus::Viewport;
                }
            }
            Err(e) => self.status.set(
                format!("Cannot read {}: {e}", relative.display()),
                Severity::Error,
            ),
        }
    }

    /// Expand the tree down to the active document and select it.
    fn reveal_active_document(&mut self) {
        if let Some(path) = self.active_path() {
            self.vault.tree.reveal(&path);
        }
    }

    /// Open the tree filter line, pre-filled with whatever filter is applied.
    fn open_tree_filter(&mut self) {
        let existing = self.vault.tree.filter().unwrap_or_default().to_string();
        self.sidebar.filter = Some(TextInput::with_value(existing));
        self.sidebar.visible = true;
        self.focus = Focus::Sidebar;
    }

    /// Apply one edit to the filter line, filtering as the user types.
    fn edit_tree_filter(&mut self, edit: TextEdit) {
        let Some(input) = &mut self.sidebar.filter else {
            return;
        };

        input.apply(edit);
        let value = input.value().to_string();
        self.vault.tree.set_filter(Some(value));
    }

    // -- Links and history -------------------------------------------------

    /// Where the active tab is, for the history stack.
    fn location(&self) -> Option<Location> {
        let tab = self.tab();
        let doc = tab.doc.as_ref()?;
        Some(Location {
            path: doc.path.clone(),
            anchor: None,
            scroll: tab.scroll,
        })
    }

    /// Move the link focus, bringing the newly focused link into view.
    fn cycle_link(&mut self, forward: bool) {
        let Some(doc) = self.tab().doc.as_ref() else {
            return;
        };
        let count = doc.links.len();

        if count == 0 {
            self.status.set("No links in this document", Severity::Info);
            return;
        }

        let next = match self.tab().focused_link {
            Some(at) if forward => (at + 1) % count,
            Some(at) => (at + count - 1) % count,
            // Starting from nothing, `n` takes the first link and `N` the last.
            None if forward => 0,
            None => count - 1,
        };

        self.tabs[self.active_tab].focused_link = Some(next);
        self.scroll_focused_link_into_view();
    }

    /// Scroll just enough to put the focused link on screen.
    fn scroll_focused_link_into_view(&mut self) {
        let height = usize::from(self.page());
        let renderer = self.renderer(self.viewport_inner().width);
        let tab = &mut self.tabs[self.active_tab];

        let Some(doc) = &tab.doc else { return };
        let Some(index) = tab.focused_link else {
            return;
        };
        let Some(link) = doc.links.get(index) else {
            return;
        };

        let Some(line) = tab.layout.line_of_offset(doc, &renderer, link.range.start) else {
            return;
        };

        // Only move when the link is off screen: cycling through links in a
        // paragraph that is already visible should not shift the page.
        if line < tab.scroll {
            tab.scroll = line;
        } else if line >= tab.scroll + height {
            tab.scroll = line.saturating_sub(height.saturating_sub(1));
        }
    }

    /// Resolve and act on the focused link.
    pub fn follow_focused_link(&mut self) {
        let Some(index) = self.tab().focused_link else {
            self.status
                .set("No link focused; press `n` or `f`", Severity::Info);
            return;
        };
        self.follow_link(index);
    }

    /// Resolve and act on one of the active document's links.
    pub fn follow_link(&mut self, index: usize) {
        let Some(doc) = self.tab().doc.as_ref() else {
            return;
        };
        let Some(link) = doc.links.get(index) else {
            return;
        };

        let target = link.target.clone();

        if link.kind == crate::doc::links::LinkKind::Wiki {
            self.follow_wiki_link(&target);
            return;
        }

        let resolved = links::resolve(&target, doc.dir(), &self.vault.root, &self.files);

        match resolved {
            links::Resolved::Document { path, anchor } => self.navigate_to(&path, anchor),
            // An anchor in the current document scrolls; it does not reload.
            links::Resolved::Anchor { slug } => self.jump_to_anchor(&slug),
            links::Resolved::Directory { path } => self.reveal_directory(&path),
            links::Resolved::External { url } => self.open_externally(Path::new(&url), &url),
            links::Resolved::Other { path } => {
                let shown = path.display().to_string();
                self.open_externally(&path, &shown);
            }
            links::Resolved::Broken { target } => self
                .status
                .set(format!("Cannot resolve: {target}"), Severity::Error),
        }
    }

    /// Open a document, recording where the reader came from.
    fn navigate_to(&mut self, path: &Path, anchor: Option<String>) {
        let from = self.location();

        match Document::load(path) {
            Ok(document) => {
                if let Some(from) = from {
                    self.tabs[self.active_tab].history.push(from);
                }
                self.open(document);
                if let Some(slug) = anchor {
                    self.jump_to_anchor(&slug);
                }
            }
            Err(e) => self.status.set(
                format!("Cannot read {}: {e}", path.display()),
                Severity::Error,
            ),
        }
    }

    /// Scroll to a heading in the active document.
    fn jump_to_anchor(&mut self, slug: &str) {
        let renderer = self.renderer(self.viewport_inner().width);
        let tab = &mut self.tabs[self.active_tab];

        let Some(doc) = &tab.doc else { return };
        let Some(offset) = doc.heading(slug).map(|h| h.offset) else {
            self.status
                .set(format!("No heading `{slug}`"), Severity::Warning);
            return;
        };

        if let Some(line) = tab.layout.line_of_offset(doc, &renderer, offset) {
            tab.scroll = line;
        }
    }

    /// Show a directory in the tree rather than trying to open it.
    fn reveal_directory(&mut self, path: &Path) {
        let Some(relative) = self.vault.relative(path).map(Path::to_path_buf) else {
            self.status.set(
                format!("{} is outside the vault", path.display()),
                Severity::Warning,
            );
            return;
        };

        if !self.vault.tree.reveal(&relative) {
            // The walk may not have reached it yet, which is not an error.
            self.status.set(
                format!("{} is not in the tree yet", relative.display()),
                Severity::Info,
            );
            return;
        }

        self.sidebar.mode = SidebarMode::Files;
        self.sidebar.visible = true;
        self.focus = Focus::Sidebar;
    }

    /// Hand something to the desktop opener, reporting either way.
    fn open_externally(&mut self, path: &Path, shown: &str) {
        match crate::vault::open_external(path) {
            Ok(()) => self.status.set(format!("Opened {shown}"), Severity::Info),
            // Without an opener the URL is at least readable, and `y` copies it.
            Err(e) => self
                .status
                .set(format!("Cannot open {shown}: {e}"), Severity::Error),
        }
    }

    /// Step back or forward through the active tab's history.
    fn navigate_history(&mut self, forward: bool) {
        let Some(current) = self.location() else {
            return;
        };

        let index = self.active_tab;
        let step = if forward {
            self.tabs[index].history.forward(current)
        } else {
            self.tabs[index].history.back(current)
        };

        let Some(target) = step else {
            let direction = if forward { "forward" } else { "back" };
            self.status
                .set(format!("No further {direction}"), Severity::Info);
            return;
        };

        // Already here: an anchor jump within one document is a history entry
        // whose path has not changed, so it must not cost a reload.
        let same = self
            .tab()
            .doc
            .as_ref()
            .is_some_and(|d| d.path == target.path);

        if !same {
            match Document::load(&target.path) {
                Ok(document) => self.open(document),
                Err(e) => {
                    self.status.set(
                        format!("Cannot read {}: {e}", target.path.display()),
                        Severity::Error,
                    );
                    return;
                }
            }
        }

        self.restore_scroll(target.scroll);
    }

    /// Put the viewport back at an exact scroll offset.
    ///
    /// Going back restores where the reader was, not the top of the document,
    /// which means measuring far enough down to know the offset is reachable.
    fn restore_scroll(&mut self, scroll: usize) {
        let height = self.page();
        let renderer = self.renderer(self.viewport_inner().width);
        let tab = &mut self.tabs[self.active_tab];

        let Some(doc) = &tab.doc else { return };

        tab.layout.window(doc, &renderer, scroll, height);
        tab.scroll = match tab.max_scroll(height) {
            Some(max) => scroll.min(max),
            None => scroll.min(tab.layout.measured_lines()),
        };
    }

    /// Label every link in view and wait for the label to be typed.
    fn open_hint_mode(&mut self) {
        let Some(doc) = self.tab().doc.as_ref() else {
            return;
        };
        if doc.links.is_empty() {
            self.status.set("No links in this document", Severity::Info);
            return;
        }

        let visible = self.visible_links();
        if visible.is_empty() {
            self.status.set("No links in view", Severity::Info);
            return;
        }

        self.overlay = Some(Overlay::Hints {
            links: visible,
            typed: String::new(),
        });
        self.focus = Focus::Overlay;
    }

    /// The indices of the links inside the current viewport, in reading order.
    pub fn visible_links(&self) -> Vec<usize> {
        let Some(doc) = self.tab().doc.as_ref() else {
            return Vec::new();
        };

        let tab = self.tab();
        let height = usize::from(self.page());
        let map = tab.layout.line_map();
        let window = tab.scroll..tab.scroll + height;

        doc.links
            .iter()
            .enumerate()
            .filter(|(_, link)| {
                map.line_of_offset(link.range.start)
                    .is_some_and(|line| window.contains(&line))
            })
            .map(|(index, _)| index)
            .collect()
    }

    // -- Edit mode -----------------------------------------------------------

    /// Enter edit mode on the active document.
    ///
    /// The cursor lands on the source line the reader was looking at, so
    /// crossing the boundary does not lose their place.
    fn enter_edit_mode(&mut self) {
        let Some(doc) = self.tab().doc.as_ref() else {
            return;
        };

        if let Some(reason) = doc.read_only {
            self.status.set(reason.message(), Severity::Warning);
            return;
        }

        let line = self.source_line_at_scroll();
        let mut editor = EditorState::new(doc);

        editor
            .textarea
            .move_cursor(tui_textarea::CursorMove::Jump(line as u16, 0));

        let index = self.active_tab;
        self.tabs[index].editor = Some(editor);
        self.tabs[index].mode = TabMode::Edit;
        // Section 7.3: edit mode locks focus to the viewport.
        self.focus = Focus::Viewport;

        if let Some(text) = self.pending_recovery() {
            let path = self.tab().doc.as_ref().map(|d| d.path.clone());
            if let Some(path) = path {
                self.overlay = Some(Overlay::Confirm {
                    question: format!(
                        "Unsaved text for {} was left behind. Restore it?",
                        path.file_name().unwrap_or_default().to_string_lossy()
                    ),
                    choices: vec![('y', "restore".into()), ('n', "discard".into())],
                    action: ConfirmAction::RestoreRecovery { path },
                });
                self.focus = Focus::Overlay;
                let _ = text;
            }
        }
    }

    /// Unsaved text an earlier run left behind for the active document.
    fn pending_recovery(&self) -> Option<String> {
        let doc = self.tab().doc.as_ref()?;
        buffer::read_recovery(&self.vault.root, &doc.path)
    }

    /// The zero-based source line the top of the viewport is showing.
    fn source_line_at_scroll(&self) -> usize {
        let tab = self.tab();
        let Some(doc) = tab.doc.as_ref() else {
            return 0;
        };

        let Some(offset) = tab.layout.line_map().offset_of_line(tab.scroll) else {
            return 0;
        };

        doc.source[..offset.min(doc.source.len())]
            .matches('\n')
            .count()
    }

    /// Leave edit mode, asking about unsaved edits first.
    fn leave_edit_mode(&mut self) {
        if self.tab().mode != TabMode::Edit {
            return;
        }

        if self.tab().dirty {
            self.overlay = Some(Overlay::Confirm {
                question: "This document has unsaved edits.".to_string(),
                choices: vec![
                    ('s', "save".into()),
                    ('d', "discard".into()),
                    ('c', "cancel".into()),
                ],
                action: ConfirmAction::LeaveEditMode,
            });
            self.focus = Focus::Overlay;
            return;
        }

        self.finish_edit_mode();
    }

    /// Drop the buffer and go back to reading, at the line the cursor was on.
    fn finish_edit_mode(&mut self) {
        let index = self.active_tab;
        let cursor = self.tabs[index].editor.as_ref().map(|e| e.cursor().0);

        self.tabs[index].editor = None;
        self.tabs[index].mode = TabMode::Read;
        self.tabs[index].dirty = false;
        self.focus = Focus::Viewport;

        if let Some(line) = cursor {
            self.scroll_to_source_line(line);
        }
    }

    /// Put the rendered view where a source line is.
    fn scroll_to_source_line(&mut self, line: usize) {
        let Some(offset) = self
            .tab()
            .doc
            .as_ref()
            .map(|doc| offset_of_source_line(&doc.source, line))
        else {
            return;
        };

        let renderer = self.renderer(self.viewport_inner().width);
        let height = usize::from(self.page());
        let tab = &mut self.tabs[self.active_tab];

        let Some(doc) = &tab.doc else { return };
        let Some(rendered) = tab.layout.line_of_offset(doc, &renderer, offset) else {
            return;
        };

        // Placed a third of the way down rather than at the very top: the
        // reader was looking at the line, not at the line above it.
        tab.scroll = rendered.saturating_sub(height / 3);
    }

    /// Hand a key press to the text area.
    fn edit_input(&mut self, key: crossterm::event::KeyEvent) {
        let index = self.active_tab;
        let Some(editor) = &mut self.tabs[index].editor else {
            return;
        };

        editor.textarea.input(key);
        self.tabs[index].dirty = self.tabs[index]
            .editor
            .as_ref()
            .is_some_and(EditorState::is_dirty);
    }

    /// Insert pasted text as a single undo step.
    fn edit_paste(&mut self, text: &str) {
        let index = self.active_tab;
        let Some(editor) = &mut self.tabs[index].editor else {
            return;
        };

        // One `insert_str` rather than a character at a time: 5,000 lines
        // pasted must be one undo, not five thousand.
        editor.textarea.insert_str(text);
        self.tabs[index].dirty = self.tabs[index]
            .editor
            .as_ref()
            .is_some_and(EditorState::is_dirty);
    }

    /// Undo or redo in the active buffer.
    fn edit_history(&mut self, redo: bool) {
        let index = self.active_tab;
        let Some(editor) = &mut self.tabs[index].editor else {
            return;
        };

        if redo {
            editor.textarea.redo();
        } else {
            editor.textarea.undo();
        }

        self.tabs[index].dirty = self.tabs[index]
            .editor
            .as_ref()
            .is_some_and(EditorState::is_dirty);
    }

    /// Write the active buffer to disk.
    pub fn save(&mut self, force: bool) {
        let index = self.active_tab;

        let Some(editor) = &self.tabs[index].editor else {
            return;
        };
        let Some(doc) = &self.tabs[index].doc else {
            return;
        };

        let path = doc.path.clone();
        let contents = editor.contents();
        let expected = (!force).then_some(editor.known_mtime);

        match buffer::save(&path, &contents, expected) {
            Ok(mtime) => {
                // Recorded before anything else: the watcher will report this
                // write, and without the record it would reload the document
                // on top of the buffer that produced it.
                self.own_writes.record(path.clone(), mtime);

                if let Some(editor) = &mut self.tabs[index].editor {
                    editor.mark_saved(mtime);
                }
                self.tabs[index].dirty = false;

                // The document is re-parsed so the outline, the links, and the
                // rendered blocks all follow the text that was just written.
                self.reload_from(&path, &contents, mtime);
                buffer::clear_recovery(&self.vault.root, &path);

                if let Some(relative) = self.vault.relative(&path).map(Path::to_path_buf) {
                    self.reindex(&relative);
                }

                self.status.set("Saved", Severity::Info);
            }
            Err(SaveError::Conflict { .. }) => {
                self.overlay = Some(Overlay::Confirm {
                    question: format!(
                        "{} changed on disk since it was opened.",
                        path.file_name().unwrap_or_default().to_string_lossy()
                    ),
                    choices: vec![
                        ('o', "overwrite".into()),
                        ('r', "reload and discard".into()),
                        ('c', "cancel".into()),
                    ],
                    action: ConfirmAction::OverwriteChanged,
                });
                self.focus = Focus::Overlay;
            }
            Err(e) => self
                .status
                .set(format!("Cannot save: {e}"), Severity::Error),
        }
    }

    /// Replace the active document with freshly parsed text.
    fn reload_from(&mut self, path: &Path, source: &str, mtime: SystemTime) {
        let index = self.active_tab;
        let Some(doc) = &self.tabs[index].doc else {
            return;
        };

        let parsed = Document::from_source(
            path.to_path_buf(),
            source.to_string(),
            mtime,
            doc.had_bom,
            doc.read_only,
        );

        self.tabs[index].doc = Some(parsed);
        // The layout keys its cache by content hash, so the blocks that did
        // not change are still hits; only the edited ones re-render.
        self.tabs[index].layout = RenderedDocument::new();
    }

    /// Re-read the active document from disk.
    fn reload_document(&mut self) {
        let Some(path) = self.tab().doc.as_ref().map(|doc| doc.path.clone()) else {
            return;
        };

        if self.tab().dirty {
            self.status.set(
                "This document has unsaved edits; save or discard first",
                Severity::Warning,
            );
            return;
        }

        let scroll = self.tab().scroll;

        match Document::load(&path) {
            Ok(document) => {
                self.open(document);
                // Reloading is not navigating: the reader stays where they
                // were, as closely as the new text allows.
                self.restore_scroll(scroll);
                self.status.set("Reloaded", Severity::Info);
            }
            Err(e) => self.status.set(
                format!("Cannot reload {}: {e}", path.display()),
                Severity::Error,
            ),
        }
    }

    /// Put the active document's path on the clipboard.
    fn copy_document_path(&mut self) {
        let Some(doc) = self.tab().doc.as_ref() else {
            return;
        };

        // The path relative to the vault, which is what a reader pasting it
        // into a note or a message means by "the path".
        let shown = doc.display_path(&self.vault.root);

        match crate::terminal::copy_to_clipboard(&shown) {
            // OSC 52 has no reply, and several emulators disable it, so this
            // says what was sent rather than claiming it arrived.
            Ok(()) => self.status.set(format!("Copied {shown}"), Severity::Info),
            Err(e) => self
                .status
                .set(format!("Cannot copy: {e}"), Severity::Error),
        }
    }

    /// Ask the event loop to hand the active document to `$EDITOR`.
    ///
    /// The loop does the work, because putting the terminal back afterwards is
    /// its job and `App` is deliberately terminal-free.
    fn hand_to_external_editor(&mut self) {
        let Some(path) = self.tab().doc.as_ref().map(|doc| doc.path.clone()) else {
            return;
        };

        if self.tab().dirty {
            self.status.set(
                "Save or discard this buffer before handing it to $EDITOR",
                Severity::Warning,
            );
            return;
        }

        if self.external_command().is_none() {
            self.status.set(
                "No editor: set $EDITOR or editor.external_command",
                Severity::Error,
            );
            return;
        }

        self.external_edit = Some(path);
    }

    /// The command `o` runs, from configuration or the environment.
    pub fn external_command(&self) -> Option<String> {
        let configured = self.editor_config.external_command.trim();
        if !configured.is_empty() {
            return Some(configured.to_string());
        }

        std::env::var("EDITOR")
            .ok()
            .or_else(|| std::env::var("VISUAL").ok())
            .filter(|command| !command.trim().is_empty())
    }

    // -- Creating and renaming ----------------------------------------------

    /// Create a file at the path typed into the prompt.
    fn create_file(&mut self, typed: &str) {
        let relative_to = self
            .active_path()
            .and_then(|path| path.parent().map(Path::to_path_buf))
            .map_or_else(|| self.vault.root.clone(), |dir| self.vault.absolute(&dir));

        let resolved =
            crate::editor::resolve_new_path(typed, &relative_to, &self.vault.root, &self.files);

        let path = match resolved {
            Ok(path) => path,
            Err(e) => {
                self.status.set(e.to_string(), Severity::Error);
                return;
            }
        };

        self.create_and_open(&path);
    }

    /// Create a file, open it in a new tab, and start editing it.
    pub fn create_and_open(&mut self, path: &Path) {
        let contents = if self.editor_config.new_file_frontmatter {
            crate::editor::frontmatter_stub(path)
        } else {
            String::new()
        };

        if let Err(e) = crate::editor::create(path, &contents) {
            self.status
                .set(format!("Cannot create the file: {e}"), Severity::Error);
            return;
        }

        match Document::load(path) {
            Ok(document) => {
                // A new note goes in its own tab: creating one is not the
                // same as leaving whatever was being read. A tab showing the
                // welcome screen is the exception — it is already empty.
                let wants_tab = self.tab().doc.is_some();

                if wants_tab && self.open_in_background_tab(document.clone()) {
                    self.active_tab = self.tabs.len() - 1;
                } else {
                    self.open(document);
                }

                if let Some(relative) = self.vault.relative(path).map(Path::to_path_buf) {
                    self.vault.tree.insert_all([crate::vault::walker::Entry {
                        path: relative.clone(),
                        is_dir: false,
                        mtime: None,
                        size: 0,
                    }]);
                    self.vault.markdown.push((relative.clone(), None, 0));
                    self.reindex(&relative);
                    self.vault.tree.reveal(&relative);
                }

                self.update(Action::EnterEditMode);
                self.status
                    .set(format!("Created {}", path.display()), Severity::Info);
            }
            Err(e) => self.status.set(
                format!("Created but cannot open {}: {e}", path.display()),
                Severity::Error,
            ),
        }
    }

    /// Rename a file, following every tab that had it open.
    fn rename(&mut self, existing: &Path, typed: &str) {
        let resolved = crate::editor::resolve_rename(typed, existing, &self.vault.root);

        let target = match resolved {
            Ok(path) => path,
            Err(e) => {
                self.status.set(e.to_string(), Severity::Error);
                return;
            }
        };

        if target == existing {
            return;
        }

        if let Err(e) = std::fs::rename(existing, &target) {
            self.status
                .set(format!("Cannot rename: {e}"), Severity::Error);
            return;
        }

        // Every tab pointing at the old path follows it, so a rename does not
        // leave a tab reading a file that is no longer there.
        let mut followed = 0;
        for tab in &mut self.tabs {
            if let Some(doc) = &mut tab.doc {
                if doc.path == existing {
                    doc.path = target.clone();
                    followed += 1;
                }
            }
        }

        self.rebuild_after_rename(existing, &target);

        // Section 9.11: links elsewhere are deliberately *not* rewritten, so
        // the reader is told how many now point at nothing.
        let broken = self.count_inbound(existing);
        let name = target.file_name().unwrap_or_default().to_string_lossy();

        if broken > 0 {
            self.status.set(
                format!("Renamed to {name}; {broken} documents now link to the old name"),
                Severity::Warning,
            );
        } else {
            self.status.set(
                format!("Renamed to {name} ({followed} tabs)"),
                Severity::Info,
            );
        }
    }

    /// Move a renamed file in the tree and the index.
    fn rebuild_after_rename(&mut self, from: &Path, to: &Path) {
        let (Some(old), Some(new)) = (
            self.vault.relative(from).map(Path::to_path_buf),
            self.vault.relative(to).map(Path::to_path_buf),
        ) else {
            return;
        };

        self.vault.index.remove(&old);
        self.vault.markdown.retain(|(path, _, _)| path != &old);

        self.vault.tree.insert_all([crate::vault::walker::Entry {
            path: new.clone(),
            is_dir: false,
            mtime: None,
            size: 0,
        }]);
        self.vault.markdown.push((new.clone(), None, 0));
        self.vault.tree.remove(&old);
        self.reindex(&new);
        self.vault.tree.reveal(&new);
    }

    /// How many indexed documents link to a path.
    fn count_inbound(&self, path: &Path) -> usize {
        let Some(relative) = self.vault.relative(path) else {
            return 0;
        };
        self.vault.index.backlinks(relative, &self.wikilinks).len()
    }

    // -- Confirmations -------------------------------------------------------

    /// Quit, asking first when a tab has unsaved edits.
    fn quit(&mut self) {
        if !self.any_dirty() {
            self.should_quit = true;
            return;
        }

        let dirty = self.tabs.iter().filter(|tab| tab.dirty).count();
        self.overlay = Some(Overlay::Confirm {
            question: format!("{dirty} documents have unsaved edits. Quit anyway?"),
            choices: vec![('d', "discard and quit".into())],
            action: ConfirmAction::Quit,
        });
        self.focus = Focus::Overlay;
    }

    /// Answer the open confirmation.
    fn confirm(&mut self, key: char) {
        let Some(Overlay::Confirm { action, .. }) = self.overlay.clone() else {
            return;
        };

        match (&action, key) {
            (ConfirmAction::LeaveEditMode, 's') => {
                self.close_overlay();
                self.save(false);
                // A save that hit a conflict has opened its own overlay; only
                // a clean save leaves edit mode.
                if self.overlay.is_none() && !self.tab().dirty {
                    self.finish_edit_mode();
                }
            }
            (ConfirmAction::LeaveEditMode, 'd') => {
                self.close_overlay();
                self.finish_edit_mode();
            }
            (ConfirmAction::Quit, 'd') => {
                self.close_overlay();
                self.should_quit = true;
            }
            (ConfirmAction::OverwriteChanged, 'o') => {
                self.close_overlay();
                self.save(true);
            }
            (ConfirmAction::OverwriteChanged, 'r') => {
                self.close_overlay();
                self.tabs[self.active_tab].dirty = false;
                self.finish_edit_mode();
                self.reload_document();
            }
            (ConfirmAction::CreatePage { path }, 'y') => {
                let path = path.clone();
                self.close_overlay();
                self.create_and_open(&path);
            }
            (ConfirmAction::RestoreRecovery { path }, 'y') => {
                let path = path.clone();
                self.close_overlay();
                self.restore_recovery(&path);
            }
            (ConfirmAction::RestoreRecovery { path }, 'n') => {
                let path = path.clone();
                self.close_overlay();
                buffer::clear_recovery(&self.vault.root, &path);
            }
            // Anything else — `c`, `n`, `Esc` — leaves things as they were.
            _ => self.close_overlay(),
        }
    }

    /// Put recovered text into the buffer.
    fn restore_recovery(&mut self, path: &Path) {
        let Some(text) = buffer::read_recovery(&self.vault.root, path) else {
            return;
        };

        let index = self.active_tab;
        if let Some(editor) = &mut self.tabs[index].editor {
            editor.textarea.select_all();
            editor.textarea.insert_str(text);
            self.tabs[index].dirty = true;
        }

        buffer::clear_recovery(&self.vault.root, path);
        self.status.set("Restored unsaved text", Severity::Info);
    }

    /// Park every dirty buffer where it can be offered back.
    ///
    /// Called on the way out when a signal arrives: the process is about to
    /// end and there is nowhere to ask the reader what they want.
    pub fn write_recovery_files(&self) {
        for tab in &self.tabs {
            let (Some(doc), Some(editor)) = (&tab.doc, &tab.editor) else {
                continue;
            };
            if !editor.is_dirty() {
                continue;
            }

            if let Err(e) = buffer::write_recovery(&self.vault.root, &doc.path, &editor.text()) {
                tracing::warn!("cannot write a recovery file: {e}");
            }
        }
    }

    /// Whether any tab has unsaved edits.
    pub fn any_dirty(&self) -> bool {
        self.tabs.iter().any(|tab| tab.dirty)
    }

    // -- Prompts -------------------------------------------------------------

    /// Open a prompt, pre-filled where that is the useful thing to do.
    fn open_prompt(&mut self, kind: PromptKind) {
        let prefill = match kind {
            // Re-running a search with one word changed is far more common
            // than starting from nothing.
            PromptKind::ProjectSearch => self.search.query.clone(),
            // A new file starts beside where the reader is, so the common
            // case is typing a name and pressing Enter.
            PromptKind::NewFile => self.new_file_prefix(),
            PromptKind::RenameDocument => self
                .tab()
                .doc
                .as_ref()
                .and_then(|doc| doc.path.file_name())
                .map(|name| name.to_string_lossy().into_owned())
                .unwrap_or_default(),
            PromptKind::RenameTreeEntry => self
                .vault
                .tree
                .selected()
                .map(|node| node.name.clone())
                .unwrap_or_default(),
        };

        self.overlay = Some(Overlay::Prompt {
            kind,
            input: TextInput::with_value(prefill),
        });
        self.focus = Focus::Overlay;
    }

    /// Apply one edit to the open prompt.
    fn edit_prompt(&mut self, edit: TextEdit) {
        if let Some(Overlay::Prompt { input, .. }) = &mut self.overlay {
            input.apply(edit);
        }
    }

    /// Act on what was typed into the prompt.
    fn accept_prompt(&mut self) {
        let Some(Overlay::Prompt { kind, input }) = self.overlay.clone() else {
            return;
        };

        self.close_overlay();

        match kind {
            PromptKind::ProjectSearch => self.start_search(input.value()),
            PromptKind::NewFile => self.create_file(input.value()),
            PromptKind::RenameDocument => {
                let path = self.tab().doc.as_ref().map(|doc| doc.path.clone());
                if let Some(path) = path {
                    self.rename(&path, input.value());
                }
            }
            PromptKind::RenameTreeEntry => {
                let path = self
                    .vault
                    .tree
                    .selected()
                    .map(|node| self.vault.absolute(&node.path));
                if let Some(path) = path {
                    self.rename(&path, input.value());
                }
            }
        }
    }

    /// What the new-file prompt opens pre-filled with.
    ///
    /// The directory the reader is in, so a bare name lands beside what they
    /// are looking at. The sidebar's selection wins when it has focus, because
    /// that is what they were pointing at.
    fn new_file_prefix(&self) -> String {
        let from_tree = (self.focus == Focus::Sidebar)
            .then(|| self.vault.tree.selected())
            .flatten()
            .map(|node| {
                if node.is_dir {
                    node.path.clone()
                } else {
                    node.path.parent().unwrap_or(Path::new("")).to_path_buf()
                }
            });

        let directory = from_tree.or_else(|| {
            self.active_path()
                .and_then(|path| path.parent().map(Path::to_path_buf))
        });

        match directory {
            Some(dir) if !dir.as_os_str().is_empty() => format!("{}/", dir.display()),
            _ => String::new(),
        }
    }

    // -- Project search ------------------------------------------------------

    /// Run a project-wide search, cancelling whatever was already running.
    pub fn start_search(&mut self, query: &str) {
        // Dropping the handle cancels the previous search, so a reader
        // re-running a query does not leave a thread reading the vault for the
        // query they have already replaced.
        self.search_handle = None;

        if query.trim().is_empty() {
            self.search = SearchState::default();
            return;
        }

        self.search.begin(query.to_string());
        self.sidebar.mode = SidebarMode::Search;
        self.sidebar.visible = true;

        let Some(sink) = self.search_sink.clone() else {
            // No sink means no event loop; the tests drive `search_now`.
            return;
        };

        let walk = WalkOptions {
            respect_gitignore: self.files.respect_gitignore,
            follow_symlinks: self.general.follow_symlinks,
        };

        self.search_handle = Some(content::spawn(
            self.vault.root.clone(),
            content::Query::parse(query, &self.search_config),
            self.search_config.clone(),
            self.files.clone(),
            walk,
            move |event| sink(event),
        ));
    }

    /// Run a project search synchronously, for tests and for the paths with no
    /// event loop behind them.
    pub fn search_now(&mut self, query: &str) {
        use std::sync::atomic::AtomicBool;
        use std::sync::Mutex;

        self.search.begin(query.to_string());
        self.sidebar.mode = SidebarMode::Search;

        let walk = WalkOptions {
            respect_gitignore: self.files.respect_gitignore,
            follow_symlinks: self.general.follow_symlinks,
        };
        let collected = Mutex::new(Vec::new());
        let outcome = Mutex::new((false, None));

        content::run(
            &self.vault.root,
            &content::Query::parse(query, &self.search_config),
            &self.search_config,
            &self.files,
            walk,
            &AtomicBool::new(false),
            &|event| match event {
                SearchEvent::Hits(hits) => collected.lock().unwrap().extend(hits),
                SearchEvent::Finished { truncated, .. } => {
                    outcome.lock().unwrap().0 = truncated;
                }
                SearchEvent::BadPattern(e) => outcome.lock().unwrap().1 = Some(e),
            },
        );

        self.search.hits = collected.into_inner().unwrap();
        let (truncated, error) = outcome.into_inner().unwrap();
        self.search.truncated = truncated;
        self.search.error = error;
        self.search.running = false;
        self.search.elapsed = self.search.started.map(|at| at.elapsed());
    }

    /// Open the search hit the sidebar has selected, scrolled to its line.
    fn open_selected_hit(&mut self) {
        let Some(hit) = self.search.hits.get(self.search.selected).cloned() else {
            return;
        };

        let absolute = self.vault.absolute(&hit.path);
        self.navigate_to(&absolute, None);

        // The line is one-based and the offset map is in bytes, so the line is
        // converted here rather than every caller doing it.
        if let Some(offset) = self.offset_of_line(hit.line) {
            self.scroll_to_offset(offset);
        }
    }

    /// The byte offset a one-based line begins at in the active document.
    fn offset_of_line(&self, line: u64) -> Option<usize> {
        let source = &self.tab().doc.as_ref()?.source;
        let wanted = line.saturating_sub(1) as usize;

        if wanted == 0 {
            return Some(0);
        }

        source
            .match_indices('\n')
            .nth(wanted - 1)
            .map(|(at, _)| at + 1)
    }

    // -- The quick switcher --------------------------------------------------

    /// Open the fuzzy switcher, showing the recent list until something is
    /// typed.
    fn open_switcher(&mut self) {
        let rows = self.switcher_rows("");

        self.overlay = Some(Overlay::Switcher {
            input: TextInput::new(),
            rows,
            selected: 0,
        });
        self.focus = Focus::Overlay;
    }

    /// Apply one edit to the switcher query and rescore.
    fn edit_switcher(&mut self, edit: TextEdit) {
        let Some(Overlay::Switcher { input, .. }) = &mut self.overlay else {
            return;
        };

        input.apply(edit);
        let query = input.value().to_string();
        let rows = self.switcher_rows(&query);

        if let Some(Overlay::Switcher {
            rows: slot,
            selected,
            ..
        }) = &mut self.overlay
        {
            *slot = rows;
            // The list under the cursor changed, so the cursor goes back to
            // the best match rather than to whatever is now in its old row.
            *selected = 0;
        }
    }

    /// The rows for a switcher query.
    fn switcher_rows(&mut self, query: &str) -> Vec<SwitcherRow> {
        if query.trim().is_empty() {
            // Section 8.3: an empty query shows the recent list, most recent
            // first, not an arbitrary slice of the vault.
            return self
                .recent
                .iter()
                .map(|path| SwitcherRow {
                    path: path.clone(),
                    indices: Vec::new(),
                    create: false,
                })
                .collect();
        }

        let paths: Vec<PathBuf> = self
            .vault
            .markdown
            .iter()
            .map(|(path, _, _)| path.clone())
            .collect();

        let mut rows: Vec<SwitcherRow> = self
            .fuzzy
            .search(&paths, query, SWITCHER_ROWS)
            .into_iter()
            .map(|candidate| SwitcherRow {
                path: candidate.path,
                indices: candidate.indices,
                create: false,
            })
            .collect();

        // A path that matches nothing is a note the reader has not written
        // yet, and the last row offers to write it.
        if rows.is_empty() {
            rows.push(SwitcherRow {
                path: PathBuf::from(with_markdown_extension(query, &self.files)),
                indices: Vec::new(),
                create: true,
            });
        }

        rows
    }

    /// Move the switcher selection, stopping at both ends.
    fn move_switcher(&mut self, delta: isize) {
        let Some(Overlay::Switcher { rows, selected, .. }) = &mut self.overlay else {
            return;
        };

        if rows.is_empty() {
            *selected = 0;
            return;
        }

        let last = rows.len() as isize - 1;
        *selected = (*selected as isize + delta).clamp(0, last) as usize;
    }

    /// Open whatever the switcher has selected.
    fn accept_switcher(&mut self, new_tab: bool) {
        let Some(Overlay::Switcher { rows, selected, .. }) = self.overlay.clone() else {
            return;
        };

        let Some(row) = rows.get(selected).cloned() else {
            return;
        };
        self.close_overlay();

        if row.create {
            // Creating the file is Section 9.11; until then the switcher says
            // what it would create rather than pretending it did.
            self.status.set(
                format!("`{}` does not exist yet", row.path.display()),
                Severity::Warning,
            );
            return;
        }

        let absolute = self.vault.absolute(&row.path);

        if new_tab {
            match Document::load(&absolute) {
                Ok(document) => {
                    self.open_in_background_tab(document);
                }
                Err(e) => self.status.set(
                    format!("Cannot read {}: {e}", row.path.display()),
                    Severity::Error,
                ),
            }
            return;
        }

        self.navigate_to(&absolute, None);
    }

    /// Record a document in the session's recent list.
    fn remember(&mut self, path: &Path) {
        let Some(relative) = self.vault.relative(path).map(Path::to_path_buf) else {
            return;
        };

        self.recent.retain(|seen| seen != &relative);
        self.recent.insert(0, relative);
        self.recent.truncate(MAX_RECENT);
    }

    // -- Session -------------------------------------------------------------

    /// Reopen the tabs, the sidebar, and the recent list from the last run.
    ///
    /// A tab whose file has gone since is dropped rather than opened empty: a
    /// session is a convenience, and reconstructing one badly is worse than
    /// opening on the welcome screen.
    pub fn restore_session(&mut self) {
        if !self.session_config.restore {
            return;
        }

        let Some(session) = crate::config::session::Session::load(&self.vault.root) else {
            return;
        };

        self.sidebar.visible = session.sidebar_visible;
        self.sidebar.width = session.sidebar_width;
        self.sidebar.mode = session.sidebar_mode;
        self.recent = session.recent;
        self.recent.truncate(self.session_config.max_recent);

        // Section 9.10: the last theme, when it was chosen at runtime. A
        // `--theme` on the command line is this run's choice and wins.
        if let Some(name) = session.theme.filter(|_| !self.theme_pinned) {
            if name != self.theme.name {
                let dir = self.theme_dir();
                let mut warnings = Vec::new();
                let theme = Theme::resolve(&name, dir.as_deref(), &mut warnings);

                if warnings.is_empty() {
                    self.theme_config.name = name;
                    self.apply_theme(theme);
                }
            }
        }

        let mut restored = Vec::new();
        for saved in &session.tabs {
            let absolute = self.vault.absolute(&saved.path);
            let Ok(document) = Document::load(&absolute) else {
                continue;
            };

            let mut tab = Tab::with_document(document);
            tab.scroll = saved.scroll;
            restored.push(tab);
        }

        if restored.is_empty() {
            return;
        }

        self.active_tab = session.active_tab.min(restored.len() - 1);
        self.tabs = restored;
    }

    /// Write the session for this vault.
    pub fn save_session(&self) {
        if !self.session_config.restore {
            return;
        }

        let session = crate::config::session::Session {
            version: crate::config::session::VERSION,
            tabs: self
                .tabs
                .iter()
                .filter_map(|tab| {
                    let doc = tab.doc.as_ref()?;
                    Some(crate::config::session::SavedTab {
                        path: self.vault.relative(&doc.path)?.to_path_buf(),
                        scroll: tab.scroll,
                    })
                })
                .collect(),
            active_tab: self.active_tab,
            sidebar_visible: self.sidebar.visible,
            sidebar_width: self.sidebar.width,
            sidebar_mode: self.sidebar.mode,
            theme: Some(self.theme.name.clone()),
            recent: self.recent.clone(),
        };

        // A session that cannot be written costs the next run its tabs and
        // nothing else, so it is logged rather than shown.
        if let Err(e) = session.save(&self.vault.root) {
            tracing::warn!("cannot write the session: {e}");
        }
    }

    // -- Live reload ---------------------------------------------------------

    /// Start watching the vault for changes.
    pub fn start_watch(&mut self, sink: impl Fn(WatchEvent) + Send + Sync + 'static) {
        if !self.watch_config.enabled {
            return;
        }

        self.watch = Some(crate::vault::watch::spawn(
            self.vault.root.clone(),
            std::time::Duration::from_millis(self.watch_config.debounce_ms),
            sink,
        ));
    }

    /// The directory user themes are read from.
    pub fn theme_dir(&self) -> Option<PathBuf> {
        if self.theme_config.dir.as_os_str().is_empty() {
            crate::config::user_theme_dir()
        } else {
            Some(self.theme_config.dir.clone())
        }
    }

    /// Watch the theme directory, so authoring a theme shows its effect.
    pub fn start_theme_watch(&mut self, sink: impl Fn(WatchEvent) + Send + Sync + 'static) {
        let Some(dir) = self.theme_dir().filter(|dir| dir.is_dir()) else {
            return;
        };

        self.theme_watch = Some(crate::vault::watch::spawn(
            dir,
            std::time::Duration::from_millis(self.watch_config.debounce_ms),
            sink,
        ));
    }

    /// Every theme that can be switched to, in the order `m t` visits them.
    ///
    /// The three built-ins first, then whatever is in the theme directory, so
    /// the order is stable and a reader who has written none still has three
    /// to cycle. `auto` is not a stop: it is a way of *choosing* a theme at
    /// startup, and offering it as a destination would be a stop whose meaning
    /// depends on the terminal.
    pub fn available_themes(&self) -> Vec<String> {
        let mut names: Vec<String> = crate::theme::builtin::ALL
            .iter()
            .map(|(name, _)| (*name).to_string())
            .collect();

        let Some(dir) = self.theme_dir() else {
            return names;
        };

        let Ok(entries) = std::fs::read_dir(dir) else {
            return names;
        };

        let mut mine: Vec<String> = entries
            .filter_map(Result::ok)
            .map(|entry| entry.path())
            .filter(|path| path.extension().is_some_and(|e| e == "toml"))
            .filter_map(|path| path.file_stem()?.to_str().map(str::to_string))
            .filter(|stem| !names.contains(stem))
            .collect();

        // Read order from a directory is not stable; the cycle must be.
        mine.sort();
        names.extend(mine);
        names
    }

    /// Switch to the next theme.
    fn cycle_theme(&mut self) {
        let names = self.available_themes();
        if names.is_empty() {
            return;
        }

        // Wherever the current theme sits in the list, the next one follows;
        // a theme that is not in the list at all starts the cycle over.
        let next = names
            .iter()
            .position(|name| *name == self.theme.name)
            .map_or(0, |at| (at + 1) % names.len());

        let wanted = names[next].clone();
        let dir = self.theme_dir();
        let mut warnings = Vec::new();
        let mut theme = Theme::resolve(&wanted, dir.as_deref(), &mut warnings);

        if let Some(warning) = warnings.first() {
            self.status.set(warning.clone(), Severity::Warning);
            return;
        }

        if theme.code_theme.is_none() {
            theme.code_theme = Some(self.theme_config.code_theme.clone());
        }

        // The watcher re-reads `theme_config.name`, so it has to follow the
        // switch or editing the showing theme would reload the previous one.
        self.theme_config.name = wanted;
        self.apply_theme(theme);

        self.status
            .set(format!("Theme: {}", self.theme.name), Severity::Info);
    }

    /// Install a theme and discard everything drawn with the old one.
    fn apply_theme(&mut self, theme: Theme) {
        self.theme = theme;

        // Every cached line carries the styles it was rendered with, so a new
        // theme means every block has to be drawn again.
        for tab in &mut self.tabs {
            tab.layout = RenderedDocument::new();
        }
    }

    /// Re-read the configured theme from disk.
    pub fn reload_theme(&mut self) {
        let dir = self.theme_dir();
        let mut warnings = Vec::new();

        let mut theme = Theme::resolve(&self.theme_config.name, dir.as_deref(), &mut warnings);
        if theme.code_theme.is_none() {
            theme.code_theme = Some(self.theme_config.code_theme.clone());
        }

        if let Some(warning) = warnings.first() {
            self.status.set(warning.clone(), Severity::Warning);
            return;
        }

        self.apply_theme(theme);

        self.status.set(
            format!("Theme reloaded: {}", self.theme.name),
            Severity::Info,
        );
    }

    /// Handle a debounced batch of changes.
    pub fn files_changed(&mut self, paths: &[PathBuf]) {
        for relative in paths {
            let absolute = self.vault.absolute(relative);
            let mtime = std::fs::metadata(&absolute).and_then(|m| m.modified()).ok();

            // A write perga made is not news; reloading on it would race the
            // dirty flag that same save has just cleared.
            if self.own_writes.claim(&absolute, mtime) {
                continue;
            }

            if !self.files.is_markdown(&absolute) {
                continue;
            }

            let size = std::fs::metadata(&absolute).map(|m| m.len()).unwrap_or(0);
            self.vault.tree.insert_all([crate::vault::walker::Entry {
                path: relative.clone(),
                is_dir: false,
                mtime,
                size,
            }]);
            self.reindex(relative);

            if self.active_path().as_deref() == Some(relative.as_path()) {
                self.active_document_changed();
            }
        }
    }

    /// Handle paths that are gone.
    pub fn files_removed(&mut self, paths: &[PathBuf]) {
        for relative in paths {
            self.vault.tree.remove(relative);
            self.vault.index.remove(relative);
            self.vault.markdown.retain(|(path, _, _)| path != relative);
        }
    }

    /// The document being read changed underneath the reader.
    fn active_document_changed(&mut self) {
        if self.tab().dirty {
            // Never clobber a buffer. The reader is told, and `r` is theirs to
            // press when they are ready.
            self.status
                .set("File changed on disk (r to reload)", Severity::Warning);
            return;
        }

        let scroll = self.tab().scroll;
        let focused = self.tab().focused_link;

        let Some(path) = self.tab().doc.as_ref().map(|doc| doc.path.clone()) else {
            return;
        };

        match Document::load(&path) {
            Ok(document) => {
                self.open(document);
                self.restore_scroll(scroll);
                // The link index may have moved under the reader, so it is
                // kept only where it still names a link.
                let count = self.tab().doc.as_ref().map_or(0, |doc| doc.links.len());
                self.tabs[self.active_tab].focused_link = focused.filter(|at| *at < count);
            }
            Err(e) => self.status.set(
                format!("Cannot reload {}: {e}", path.display()),
                Severity::Warning,
            ),
        }
    }

    // -- Sidebar dispatch ---------------------------------------------------

    /// Move the selection in whichever sidebar mode is showing.
    fn move_sidebar_selection(&mut self, delta: isize) {
        match self.sidebar.mode {
            SidebarMode::Files => self.vault.tree.move_selection(delta),
            SidebarMode::Outline => {
                let count = self.tab().doc.as_ref().map_or(0, |doc| doc.outline.len());
                if count == 0 {
                    return;
                }
                let at = self.sidebar.outline_selected.min(count - 1) as isize;
                self.sidebar.outline_selected = (at + delta).clamp(0, count as isize - 1) as usize;
            }
            SidebarMode::Search => self.search.move_selection(delta),
            // The links mode is a list to read, not one to walk.
            SidebarMode::Links => {}
        }
    }

    /// Activate the selection in whichever sidebar mode is showing.
    fn activate_sidebar_selection(&mut self) {
        match self.sidebar.mode {
            SidebarMode::Files => self.tree_expand_or_open(),
            SidebarMode::Outline => self.jump_to_selected_heading(),
            SidebarMode::Search => self.open_selected_hit(),
            SidebarMode::Links => {}
        }
    }

    /// Step back in whichever sidebar mode is showing.
    fn sidebar_back(&mut self) {
        if self.sidebar.mode == SidebarMode::Files {
            self.vault.tree.collapse_selected();
        }
    }

    // -- Outline ------------------------------------------------------------

    /// Which heading the reader is currently inside.
    ///
    /// The last heading whose rendered line is at or above the top of the
    /// viewport, which is what "the section I am reading" means when a section
    /// is taller than the screen.
    pub fn current_heading(&self) -> usize {
        let tab = self.tab();
        let Some(doc) = tab.doc.as_ref() else {
            return 0;
        };

        let map = tab.layout.line_map();
        let mut current = 0;

        for (index, heading) in doc.outline.iter().enumerate() {
            match map.line_of_offset(heading.offset) {
                Some(line) if line <= tab.scroll => current = index,
                _ => break,
            }
        }

        current
    }

    /// Scroll the viewport to the heading the outline has selected.
    fn jump_to_selected_heading(&mut self) {
        let at = self.sidebar.outline_selected;
        let slug = self
            .tab()
            .doc
            .as_ref()
            .and_then(|doc| doc.outline.get(at))
            .map(|heading| heading.slug.clone());

        // The same slug lookup anchors use: one implementation, so an anchor
        // and the outline can never disagree about where a heading is.
        if let Some(slug) = slug {
            self.jump_to_anchor(&slug);
        }
    }

    // -- Find in document ---------------------------------------------------

    /// Open the find bar, keeping whatever was searched for last.
    fn open_find(&mut self) {
        if self.tab().doc.is_none() {
            return;
        }

        let index = self.active_tab;
        let find = self.tabs[index].find.get_or_insert_with(FindState::new);
        // Reopening selects the whole query, in the sense that typing replaces
        // it: the common case is a new search, not an edit of the old one.
        find.input.apply(TextEdit::End);

        self.overlay = Some(Overlay::Find);
        self.focus = Focus::Overlay;
    }

    /// Apply one edit to the query and re-run the search.
    fn edit_find(&mut self, edit: TextEdit) {
        let index = self.active_tab;
        let Some(source) = self.tabs[index].doc.as_ref().map(|d| d.source.clone()) else {
            return;
        };
        let Some(find) = &mut self.tabs[index].find else {
            return;
        };

        find.input.apply(edit);
        find.refresh(&source);

        if let Some(offset) = find.current_offset() {
            self.scroll_to_offset(offset);
        }
    }

    /// Step to the next or previous match.
    fn step_find(&mut self, forward: bool) {
        let index = self.active_tab;
        let Some(find) = &mut self.tabs[index].find else {
            return;
        };

        let Some(offset) = find.step(forward) else {
            self.status.set("No matches", Severity::Info);
            return;
        };

        self.scroll_to_offset(offset);
    }

    /// Close the find bar and clear its highlighting.
    fn close_find(&mut self) {
        self.tabs[self.active_tab].find = None;
        if self.overlay == Some(Overlay::Find) {
            self.close_overlay();
        }
    }

    /// Bring a source offset into view, moving as little as possible.
    fn scroll_to_offset(&mut self, offset: usize) {
        let height = usize::from(self.page());
        let renderer = self.renderer(self.viewport_inner().width);
        let tab = &mut self.tabs[self.active_tab];

        let Some(doc) = &tab.doc else { return };
        let Some(line) = tab.layout.line_of_offset(doc, &renderer, offset) else {
            return;
        };

        if line < tab.scroll {
            tab.scroll = line;
        } else if line >= tab.scroll + height {
            tab.scroll = line.saturating_sub(height / 2);
        }
    }

    // -- Tabs --------------------------------------------------------------

    /// Open a new tab on the welcome screen and switch to it.
    fn new_tab(&mut self) {
        self.leave_active_tab();

        if self.tabs.len() >= MAX_TABS {
            self.status.set(
                format!("{MAX_TABS} tabs is the maximum; reusing this one"),
                Severity::Warning,
            );
            self.tabs[self.active_tab] = Tab::default();
            return;
        }

        self.tabs.push(Tab::default());
        self.active_tab = self.tabs.len() - 1;
        self.focus = Focus::Viewport;
    }

    /// Close the active tab, or quit when it is the last one.
    fn close_tab(&mut self) {
        if self.tabs.len() == 1 {
            self.update(Action::Quit);
            return;
        }

        self.leave_active_tab();

        self.tabs.remove(self.active_tab);
        // Closing a tab lands on the one to its left, which is where the eye
        // already is; closing the first lands on the new first.
        self.active_tab = self.active_tab.saturating_sub(1);
        self.reveal_active_document();
    }

    /// Move to the next or previous tab, wrapping at both ends.
    fn switch_tab(&mut self, delta: isize) {
        let count = self.tabs.len() as isize;
        if count <= 1 {
            return;
        }

        self.leave_active_tab();

        self.active_tab = (self.active_tab as isize + delta).rem_euclid(count) as usize;
        self.reveal_active_document();
    }

    /// Tidy up whatever belongs to the tab the reader is leaving.
    ///
    /// The find bar shows the *active* tab's state, so it does not follow the
    /// reader to a tab that was not searching for anything.
    fn leave_active_tab(&mut self) {
        if self.overlay == Some(Overlay::Find) {
            self.close_overlay();
        }
    }

    /// Open a document in a new tab without leaving the current one.
    ///
    /// Returns whether the tab was opened; at the cap it is not, and the
    /// caller decides what to do instead.
    fn open_in_background_tab(&mut self, document: Document) -> bool {
        if self.tabs.len() >= MAX_TABS {
            self.status
                .set(format!("{MAX_TABS} tabs is the maximum"), Severity::Warning);
            return false;
        }

        let label = document.label().to_string();
        let mut tab = Tab::with_document(document);
        // A background tab has never been drawn, so it has no measured layout
        // and starts at the top — which is where a freshly opened document is
        // anyway.
        tab.scroll = 0;

        self.tabs.push(tab);
        self.status
            .set(format!("Opened {label} in a new tab"), Severity::Info);
        true
    }

    /// Follow the focused link into a background tab.
    ///
    /// Only a document target makes sense here: an anchor has nowhere else to
    /// go, and a directory or an external URL is not a tab.
    fn follow_focused_link_in_new_tab(&mut self) {
        let Some(index) = self.tab().focused_link else {
            self.status
                .set("No link focused; press `n` or `f`", Severity::Info);
            return;
        };

        let Some(doc) = self.tab().doc.as_ref() else {
            return;
        };
        let Some(link) = doc.links.get(index) else {
            return;
        };

        let target = link.target.clone();

        if link.kind == crate::doc::links::LinkKind::Wiki {
            self.follow_wiki_link(&target);
            return;
        }

        let resolved = links::resolve(&target, doc.dir(), &self.vault.root, &self.files);

        match resolved {
            links::Resolved::Document { path, .. } => match Document::load(&path) {
                Ok(document) => {
                    self.open_in_background_tab(document);
                }
                Err(e) => self.status.set(
                    format!("Cannot read {}: {e}", path.display()),
                    Severity::Error,
                ),
            },
            // Everything else does what it would have done in this tab.
            _ => self.follow_link(index),
        }
    }

    /// Follow a `[[wiki-link]]`, through the index rather than the filesystem.
    fn follow_wiki_link(&mut self, target: &str) {
        if !self.wikilinks.enabled {
            self.status
                .set("Wiki-links are disabled", Severity::Warning);
            return;
        }

        if !self.vault.index.ready {
            // Resolving against a half-built index would send the reader
            // somewhere arbitrary and look like a bug in their notes.
            self.status.set("Still indexing…", Severity::Info);
            return;
        }

        let source = self.active_path().unwrap_or_default();

        match self
            .vault
            .index
            .resolve_wiki(target, &source, &self.wikilinks)
        {
            WikiResolution::Found { path, anchor } => {
                let absolute = self.vault.absolute(&path);
                self.navigate_to(&absolute, anchor);
            }
            WikiResolution::Ambiguous { candidates, anchor } => {
                self.overlay = Some(Overlay::Disambiguate {
                    page: target.to_string(),
                    candidates,
                    anchor,
                    selected: 0,
                });
                self.focus = Focus::Overlay;
            }
            // Section 9.11: a broken wiki-link is the one place perga offers
            // to create a file. A broken *inline* link never does.
            WikiResolution::Missing { page, .. } => self.offer_to_create(&page),
        }
    }

    /// Offer to create the page a wiki-link named but nothing answers to.
    fn offer_to_create(&mut self, page: &str) {
        let configured = &self.wikilinks.new_file_dir;
        let directory = if configured.as_os_str().is_empty() {
            // Empty means the active document's own directory, which is where
            // a note written about what is in front of you belongs.
            self.active_path()
                .and_then(|path| path.parent().map(Path::to_path_buf))
                .unwrap_or_default()
        } else {
            configured.clone()
        };

        let resolved = crate::editor::resolve_new_path(
            page,
            &self.vault.absolute(&directory),
            &self.vault.root,
            &self.files,
        );

        let path = match resolved {
            Ok(path) => path,
            Err(e) => {
                self.status.set(e.to_string(), Severity::Warning);
                return;
            }
        };

        let shown = self
            .vault
            .relative(&path)
            .unwrap_or(&path)
            .display()
            .to_string();

        self.overlay = Some(Overlay::Confirm {
            question: format!("Create \"{shown}\"?"),
            choices: vec![('y', "create".into())],
            action: ConfirmAction::CreatePage { path },
        });
        self.focus = Focus::Overlay;
    }

    /// Open the candidate the disambiguation overlay has selected.
    pub fn choose_candidate(&mut self, index: usize) {
        let Some(Overlay::Disambiguate {
            candidates, anchor, ..
        }) = self.overlay.clone()
        else {
            return;
        };

        let Some(path) = candidates.get(index) else {
            return;
        };
        let absolute = self.vault.absolute(path);

        self.close_overlay();
        self.navigate_to(&absolute, anchor);
    }

    // -- The backlink index --------------------------------------------------

    /// Load the cache and start indexing whatever it does not cover.
    ///
    /// Called when the walk finishes, which is when the set of files in the
    /// vault is known and the cache can be validated against it file by file.
    fn start_indexing(&mut self) {
        if !self.wikilinks.enabled || !self.wikilinks.index_on_start {
            self.vault.index.ready = true;
            return;
        }

        if self.wikilinks.cache {
            if let Some(cache) = index::load_cache(&self.vault.root) {
                self.vault.index = crate::vault::index::Index::from_cache(cache);
            }
        }

        // Anything the walk no longer reports has been deleted since the cache
        // was written; its inbound links become broken, which is what they are.
        let live: std::collections::BTreeSet<PathBuf> = self
            .vault
            .markdown
            .iter()
            .map(|(path, _, _)| path.clone())
            .collect();
        self.vault.index.retain_paths(&live);

        let stale: Vec<PathBuf> = self
            .vault
            .markdown
            .iter()
            .filter(|(path, mtime, size)| !self.vault.index.is_current(path, *mtime, *size))
            .map(|(path, _, _)| path.clone())
            .collect();

        self.vault.index.total = Some(self.vault.markdown.len());
        self.vault.index.indexed = self.vault.markdown.len() - stale.len();

        if stale.is_empty() {
            self.vault.index.ready = true;
            return;
        }

        let Some(sink) = self.index_sink.clone() else {
            // No sink means no event loop — a test, or print mode. Indexing
            // synchronously there would be a surprise; leaving it unbuilt is
            // what the Links mode already knows how to show.
            return;
        };

        self.vault.start_index(stale, move |event| sink(event));
    }

    /// Mark the index complete and write the cache.
    fn finish_indexing(&mut self) {
        self.vault.index.ready = true;
        self.vault.cancel_index();

        if !self.wikilinks.cache {
            return;
        }

        // A cache that cannot be written costs a slower start next time and
        // nothing else, so it is logged rather than shown.
        if let Err(e) = index::save_cache(&self.vault.root, &self.vault.index.to_cache()) {
            tracing::warn!("cannot write the index cache: {e}");
        }
    }

    /// Build the index synchronously, for tests and for the paths with no
    /// event loop behind them.
    pub fn index_now(&mut self) {
        let files: Vec<PathBuf> = self
            .vault
            .markdown
            .iter()
            .map(|(path, _, _)| path.clone())
            .collect();

        for path in files {
            self.reindex(&path);
        }

        self.vault.index.total = Some(self.vault.markdown.len());
        self.vault.index.ready = true;
    }

    /// Re-read one file into the index.
    pub fn reindex(&mut self, relative: &Path) {
        let absolute = self.vault.absolute(relative);

        let Ok(bytes) = std::fs::read(&absolute) else {
            self.vault.index.remove(relative);
            return;
        };
        let metadata = std::fs::metadata(&absolute).ok();

        let entry = index::entry_for(
            &String::from_utf8_lossy(&bytes),
            metadata.as_ref().and_then(|m| m.modified().ok()),
            metadata.as_ref().map_or(0, |m| m.len()),
        );
        self.vault.index.insert(relative.to_path_buf(), entry);
    }

    /// Whether a link in the active document resolves to nothing.
    ///
    /// Used by the links sidebar to mark broken targets. A wiki-link is only
    /// judged once the index is ready; until then nothing is called broken,
    /// because nothing is yet known.
    pub fn is_broken(&self, link: &crate::doc::links::Link) -> bool {
        let Some(doc) = self.tab().doc.as_ref() else {
            return false;
        };

        if link.kind == crate::doc::links::LinkKind::Wiki {
            if !self.wikilinks.enabled || !self.vault.index.ready {
                return false;
            }
            let source = self.active_path().unwrap_or_default();
            return matches!(
                self.vault
                    .index
                    .resolve_wiki(&link.target, &source, &self.wikilinks),
                WikiResolution::Missing { .. }
            );
        }

        matches!(
            links::resolve(&link.target, doc.dir(), &self.vault.root, &self.files),
            links::Resolved::Broken { .. }
        )
    }

    /// Every document that links to the active one.
    pub fn backlinks(&self) -> Vec<crate::vault::index::Backlink> {
        let Some(path) = self.active_path() else {
            return Vec::new();
        };
        self.vault.index.backlinks(&path, &self.wikilinks)
    }

    /// Move focus between the sidebar and the viewport.
    ///
    /// Edit mode locks focus to the viewport, and an open overlay owns focus
    /// until it closes.
    fn cycle_focus(&mut self, _forward: bool) {
        if self.overlay.is_some() || self.tab().mode == TabMode::Edit {
            return;
        }

        // With only two focusable panes, forwards and backwards agree.
        self.focus = match self.focus {
            Focus::Viewport if self.sidebar.visible => Focus::Sidebar,
            Focus::Sidebar => Focus::Viewport,
            other => other,
        };
    }

    fn toggle_sidebar(&mut self) {
        self.sidebar.visible = !self.sidebar.visible;

        if self.sidebar.visible {
            // An overlaid sidebar is useless without focus: it covers the
            // viewport, so the keys the user is about to press should reach it.
            if self.frames().sidebar_placement == SidebarPlacement::Overlaid {
                self.focus = Focus::Sidebar;
            }
        } else if self.focus == Focus::Sidebar {
            self.focus = Focus::Viewport;
        }
    }

    /// Widen or narrow the sidebar, within what the terminal can hold.
    fn resize_sidebar(&mut self, delta: i32) {
        let requested =
            (i32::from(self.sidebar.width) + delta).clamp(0, i32::from(u16::MAX)) as u16;

        self.sidebar.width = if self.area.width > 0 {
            layout::clamp_sidebar_width(requested, self.area.width)
        } else {
            requested
        };
    }

    fn toggle_mouse(&mut self) {
        self.mouse_capture = !self.mouse_capture;

        match terminal::set_mouse_capture(self.mouse_capture) {
            Ok(()) => {
                let state = if self.mouse_capture { "on" } else { "off" };
                self.status
                    .set(format!("Mouse capture {state}"), Severity::Info);
            }
            Err(e) => {
                self.mouse_capture = !self.mouse_capture;
                self.status
                    .set(format!("Cannot change mouse capture: {e}"), Severity::Error);
            }
        }
    }

    fn toggle_help(&mut self) {
        match self.overlay {
            Some(Overlay::Help { .. }) => self.close_overlay(),
            // Help replaces whatever else was open rather than stacking on it.
            _ => {
                self.overlay = Some(Overlay::Help { scroll: 0 });
                self.focus = Focus::Overlay;
            }
        }
    }

    /// `Esc`: close the overlay, abandon a pending key sequence, or leave edit
    /// mode, in that order.
    fn escape(&mut self) {
        if self.overlay.is_some() {
            self.close_overlay();
            return;
        }

        if self.sidebar.filter.is_some() {
            self.update(Action::TreeFilterCancel);
            return;
        }

        if self.tab().mode == TabMode::Edit {
            self.leave_edit_mode();
            return;
        }

        if !self.keymap.pending().is_empty() {
            self.keymap.clear_pending();
            self.status.pending = None;
        }
    }

    fn close_overlay(&mut self) {
        // Closing the find bar clears its highlighting, which is what `Esc`
        // means there; the query survives so `Ctrl+F` reopens it.
        if self.overlay == Some(Overlay::Find) {
            if let Some(find) = &mut self.tabs[self.active_tab].find {
                find.current = None;
            }
        }

        self.overlay = None;
        self.focus = if self.sidebar.visible
            && self.frames().sidebar_placement == SidebarPlacement::Overlaid
        {
            Focus::Sidebar
        } else {
            Focus::Viewport
        };
    }
}

/// A message from any source, waiting to become an [`Action`].
///
/// Terminal input, signals, and every background worker all send these down one
/// channel, so the main loop is a single blocking `recv` and nothing else.
#[derive(Debug)]
pub enum Message {
    /// A terminal input event.
    Input(crossterm::event::Event),
    /// A signal was delivered. The payload is the `libc` signal number.
    Signal(i32),
    /// Syntax highlighting finished loading.
    SyntaxReady,
    /// The vault walker reported something.
    Walk(WalkEvent),
    /// The backlink indexer reported something.
    Index(IndexEvent),
    /// The project searcher reported something.
    Search(SearchEvent),
    /// The filesystem watcher reported something.
    Watch(WatchEvent),
    /// A theme file changed.
    ThemeChanged,
}

/// Run the application until it quits.
///
/// The loop blocks on `recv` and does no polling, which is what makes the 0%
/// idle CPU target hold by construction rather than by tuning a timeout.
pub fn run(terminal: &mut Tui, app: &mut App, messages: &Receiver<Message>) -> anyhow::Result<()> {
    // Seed the size before the first frame so layout-dependent state is right
    // from the start rather than after the first resize.
    let size = terminal.size()?;
    app.update(Action::Resize(size.width, size.height));

    loop {
        terminal.draw(|frame| ui::draw(app, frame))?;

        let Ok(message) = messages.recv() else {
            // Every sender is gone, which can only happen during teardown.
            break;
        };

        for action in crate::event::translate(app, message) {
            app.update(action);
        }

        if app.should_suspend {
            app.should_suspend = false;
            suspend(terminal)?;
        }

        if let Some(path) = app.external_edit.take() {
            match hand_over(terminal, app, &path) {
                Ok(()) => app.update(Action::ReloadDocument),
                Err(e) => app
                    .status
                    .set(format!("$EDITOR failed: {e}"), Severity::Error),
            }
        }

        if app.should_quit {
            break;
        }
    }

    Ok(())
}

/// Run `$EDITOR` on a file with the terminal handed back to it.
///
/// The editor is very likely a full-screen application itself, so perga leaves
/// the alternate screen and raw mode entirely rather than trying to share
/// them, waits for the child, and sets the terminal back up afterwards.
fn hand_over(terminal: &mut Tui, app: &App, path: &Path) -> anyhow::Result<()> {
    use std::process::Command;

    let command = app
        .external_command()
        .ok_or_else(|| anyhow::anyhow!("no editor configured"))?;

    // Split on whitespace so `editor.external_command = "code --wait"` works;
    // the *path* is still a separate argument and never goes through a shell.
    let mut parts = command.split_whitespace();
    let program = parts
        .next()
        .ok_or_else(|| anyhow::anyhow!("no editor configured"))?;

    let mouse = terminal::mouse_capture_active();
    terminal::restore()?;

    let status = Command::new(program).args(parts).arg(path).status();

    *terminal = terminal::setup(mouse)?;
    // The editor owned the screen; nothing perga drew before is still there.
    terminal.clear()?;

    let status = status?;
    if !status.success() {
        anyhow::bail!("the editor exited with {status}");
    }

    Ok(())
}

/// Suspend the process with `SIGTSTP` and pick the terminal back up on resume.
///
/// This is what a terminal user expects `Ctrl+Z` to do in a full-screen
/// program: `fg` must bring the screen back intact.
fn suspend(terminal: &mut Tui) -> anyhow::Result<()> {
    let mouse = terminal::mouse_capture_active();

    terminal::restore()?;

    #[cfg(unix)]
    signal_hook::low_level::raise(signal_hook::consts::SIGTSTP)?;

    *terminal = terminal::setup(mouse)?;
    // The screen was handed back to the shell while suspended, so the whole
    // frame has to be repainted rather than diffed against what was there.
    terminal.clear()?;

    Ok(())
}

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

    /// An app sized like a comfortable terminal.
    fn app() -> App {
        let mut app = App::new(
            Theme::dark(),
            Keymap::defaults(),
            UiConfig::default(),
            FilesConfig::default(),
        );
        app.update(Action::Resize(120, 40));
        app
    }

    #[test]
    fn opens_in_read_mode_with_the_viewport_focused() {
        let app = app();
        assert_eq!(app.tab().mode, TabMode::Read);
        assert_eq!(app.focus, Focus::Viewport);
        assert!(app.overlay.is_none());
    }

    #[test]
    fn focus_cycles_between_the_sidebar_and_the_viewport() {
        let mut app = app();
        app.update(Action::FocusNext);
        assert_eq!(app.focus, Focus::Sidebar);
        app.update(Action::FocusNext);
        assert_eq!(app.focus, Focus::Viewport);
        app.update(Action::FocusPrev);
        assert_eq!(app.focus, Focus::Sidebar);
    }

    #[test]
    fn hiding_the_sidebar_moves_focus_out_of_it() {
        let mut app = app();
        app.update(Action::FocusNext);
        assert_eq!(app.focus, Focus::Sidebar);

        app.update(Action::ToggleSidebar);
        assert!(!app.sidebar.visible);
        assert_eq!(app.focus, Focus::Viewport);
    }

    #[test]
    fn focus_does_not_move_into_a_hidden_sidebar() {
        let mut app = app();
        app.update(Action::ToggleSidebar);
        app.update(Action::FocusNext);
        assert_eq!(app.focus, Focus::Viewport);
    }

    #[test]
    fn an_overlaid_sidebar_takes_focus_when_it_opens() {
        let mut app = app();
        app.update(Action::Resize(70, 40));
        app.update(Action::ToggleSidebar); // hide
        app.update(Action::ToggleSidebar); // show, overlaid
        assert_eq!(app.frames().sidebar_placement, SidebarPlacement::Overlaid);
        assert_eq!(app.focus, Focus::Sidebar);
    }

    #[test]
    fn a_split_sidebar_does_not_steal_focus_when_it_opens() {
        let mut app = app();
        app.update(Action::ToggleSidebar);
        app.update(Action::ToggleSidebar);
        assert_eq!(app.frames().sidebar_placement, SidebarPlacement::Split);
        assert_eq!(app.focus, Focus::Viewport);
    }

    #[test]
    fn resizing_the_sidebar_stays_within_bounds() {
        let mut app = app();
        for _ in 0..200 {
            app.update(Action::SidebarWiden);
        }
        assert!(app.sidebar.width <= 60, "{}", app.sidebar.width);

        for _ in 0..200 {
            app.update(Action::SidebarNarrow);
        }
        assert_eq!(app.sidebar.width, layout::MIN_SIDEBAR_WIDTH);
    }

    #[test]
    fn a_shrunken_terminal_narrows_the_drawn_sidebar_but_keeps_the_choice() {
        let mut app = app();
        for _ in 0..40 {
            app.update(Action::SidebarWiden);
        }
        let chosen = app.sidebar.width;
        assert_eq!(chosen, 60);

        app.update(Action::Resize(60, 20));
        assert_eq!(app.frames().sidebar.unwrap().width, 30);
        assert_eq!(app.sidebar.width, chosen, "the chosen width was destroyed");

        // ...and it comes back when there is room for it again.
        app.update(Action::Resize(120, 40));
        assert_eq!(app.frames().sidebar.unwrap().width, chosen);
    }

    #[test]
    fn help_opens_takes_focus_and_closes_again() {
        let mut app = app();
        app.update(Action::ToggleHelp);
        assert_eq!(app.overlay, Some(Overlay::Help { scroll: 0 }));
        assert_eq!(app.focus, Focus::Overlay);

        app.update(Action::ToggleHelp);
        assert!(app.overlay.is_none());
        assert_eq!(app.focus, Focus::Viewport);
    }

    #[test]
    fn escape_closes_an_overlay() {
        let mut app = app();
        app.update(Action::ToggleHelp);
        app.update(Action::Escape);
        assert!(app.overlay.is_none());
        assert_eq!(app.focus, Focus::Viewport);
    }

    #[test]
    fn focus_does_not_move_while_an_overlay_is_open() {
        let mut app = app();
        app.update(Action::ToggleHelp);
        app.update(Action::FocusNext);
        assert_eq!(app.focus, Focus::Overlay);
    }

    #[test]
    fn switching_sidebar_mode_reveals_a_hidden_sidebar() {
        let mut app = app();
        app.update(Action::ToggleSidebar);
        assert!(!app.sidebar.visible);

        app.update(Action::SetSidebarMode(SidebarMode::Outline));
        assert!(app.sidebar.visible);
        assert_eq!(app.sidebar.mode, SidebarMode::Outline);
    }

    #[test]
    fn quit_sets_the_flag() {
        let mut app = app();
        app.update(Action::Quit);
        assert!(app.should_quit);
        assert_eq!(app.exit_code, 0);
    }
}