zshrs 0.11.5

The first compiled Unix shell — bytecode VM, worker pool, AOP intercept, Rkyv caching
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
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
//! Completion matching engine for ZLE
//!
//! Port from zsh/Src/Zle/compmatch.c (2,974 lines)
//!
//! This compares two cmatchers and returns non-zero if they are equal.     // c:80
//! Add the given matchers to the bmatcher list.                            // c:97
//! This returns a new Cline structure.                                     // c:140
//!
//! The full matching engine is in compsys/matching.rs (458 lines).
//! This module provides the pattern matching, anchor handling, and
//! match line construction used during completion.
//!
//! Key C functions and their Rust locations:
//! - match_str         → compsys::matching::match_str()
//! - match_parts       → compsys::matching::match_parts()
//! - comp_match        → compsys::matching::comp_match()
//! - pattern_match_equivalence → compsys::matching (inline)
//! - add_match_str/part/sub    → compsys::matching (inline)
//! - cline_* (match line ops)  → compsys::base::CompletionLine

// CompMatcher / MatchFlags / CompLine deleted — Rust-invented structs
// with no C counterpart. The legit C types `Cmatcher` (comp.h:153),
// `Cline` (comp.h:245), and `Cpattern` (comp.h:197) are ported in
// `comp_h.rs` and used by the real porters of `match_str` /
// `pattern_match` / `add_match_str` etc. below.

/// Port of `cpatterns_same(Cpattern a, Cpattern b)` from `Src/Zle/compmatch.c:42`.
/// ```c
/// static int
/// cpatterns_same(Cpattern a, Cpattern b)
/// {
///     while (a) {
///         if (!b) return 0;
///         if (a->tp != b->tp) return 0;
///         switch (a->tp) {
///         case CPAT_CCLASS: case CPAT_NCLASS: case CPAT_EQUIV:
///             if (strcmp(a->u.str, b->u.str) != 0) return 0;
///             break;
///         case CPAT_CHAR:
///             if (a->u.chr != b->u.chr) return 0;
///             break;
///         default:
///             break;
///         }
///         a = a->next;
///         b = b->next;
///     }
///     return !b;
/// }
/// ```
/// Walk two parallel `Cpattern` chains testing structural equality
/// (same `tp` + same `str` for class types or same `chr` for
/// CPAT_CHAR). Used by `cmatchers_same` to dedupe matcher specs.
/// WARNING: param names don't match C — Rust=(b) vs C=(a, b)

// --- AUTO: cross-zle hoisted-fn use glob ---
#[allow(unused_imports)]
#[allow(unused_imports)]
use crate::ported::zle::zle_main::*;
#[allow(unused_imports)]
use crate::ported::zle::zle_misc::*;
#[allow(unused_imports)]
use crate::ported::zle::zle_hist::*;
#[allow(unused_imports)]
use crate::ported::zle::zle_move::*;
#[allow(unused_imports)]
use crate::ported::zle::zle_word::*;
#[allow(unused_imports)]
use crate::ported::zle::zle_params::*;
#[allow(unused_imports)]
use crate::ported::zle::zle_vi::*;
#[allow(unused_imports)]
use crate::ported::zle::zle_utils::*;
#[allow(unused_imports)]
use crate::ported::zle::zle_refresh::*;
#[allow(unused_imports)]
use crate::ported::zle::zle_tricky::*;
#[allow(unused_imports)]
use crate::ported::zle::textobjects::*;
#[allow(unused_imports)]
use crate::ported::zle::deltochar::*;

pub fn cpatterns_same(                                                       // c:44
    mut a: Option<&crate::ported::zle::comp_h::Cpattern>,
    mut b: Option<&crate::ported::zle::comp_h::Cpattern>,
) -> bool {                                                                  // c:42
    use crate::ported::zle::comp_h::{CPAT_CCLASS, CPAT_CHAR, CPAT_EQUIV, CPAT_NCLASS};
    while let Some(ap) = a {                                                 // c:46 while (a)
        let bp = match b {                                                   // c:47
            None => return false,                                            // c:48 if(!b) return 0
            Some(p) => p,
        };
        if ap.tp != bp.tp {                                                  // c:49
            return false;                                                    // c:50
        }
        match ap.tp {                                                        // c:51
            x if x == CPAT_CCLASS || x == CPAT_NCLASS || x == CPAT_EQUIV => {  // c:52-54
                // c:55-58 — equivalent ranges might compare same even when
                // strings differ; the C source admits this is unhandled.
                if ap.str != bp.str {                                      // c:60 strcmp(a->u.str,b->u.str)
                    return false;                                            // c:61
                }
            }
            x if x == CPAT_CHAR => {                                         // c:64
                if ap.chr != bp.chr {                                        // c:65
                    return false;                                            // c:66
                }
            }
            _ => {                                                           // c:69 default
                // c:70 — "here to silence compiler"
            }
        }
        a = ap.next.as_deref();                                              // c:74 a = a->next
        b = bp.next.as_deref();                                              // c:75 b = b->next
    }
    b.is_none()                                                              // c:77 return !b
}

/// Port of `cmatchers_same(Cmatcher a, Cmatcher b)` from `Src/Zle/compmatch.c:82`.
/// ```c
/// static int
/// cmatchers_same(Cmatcher a, Cmatcher b)
/// {
///     return (a == b ||
///             (a->flags == b->flags &&
///              a->llen == b->llen && a->wlen == b->wlen &&
///              (!a->llen || cpatterns_same(a->line, b->line)) &&
///              (a->wlen <= 0 || cpatterns_same(a->word, b->word)) &&
///              (!(a->flags & (CMF_LEFT | CMF_RIGHT)) ||
///               (a->lalen == b->lalen && a->ralen == b->ralen &&
///                (!a->lalen || cpatterns_same(a->left, b->left)) &&
///                (!a->ralen || cpatterns_same(a->right, b->right))))));
/// }
/// ```
/// Test two matchers for full structural equality — flags, lengths,
/// patterns, and (if anchored) anchor patterns must all match.
/// WARNING: param names don't match C — Rust=(b) vs C=(a, b)
pub fn cmatchers_same(                                                       // c:84
    a: &crate::ported::zle::comp_h::Cmatcher,
    b: &crate::ported::zle::comp_h::Cmatcher,
) -> bool {                                                                  // c:82
    use crate::ported::zle::comp_h::{CMF_LEFT, CMF_RIGHT};
    // c:86 — `a == b` short-circuit (pointer identity). Rust uses
    // `std::ptr::eq` for the same effect.
    if std::ptr::eq(a, b) {
        return true;
    }
    // c:87 — `a->flags == b->flags && a->llen == b->llen && a->wlen == b->wlen`.
    if a.flags != b.flags || a.llen != b.llen || a.wlen != b.wlen {
        return false;
    }
    // c:89 — `(!a->llen || cpatterns_same(a->line, b->line))`.
    if a.llen != 0 && !cpatterns_same(a.line.as_deref(), b.line.as_deref()) {
        return false;
    }
    // c:90 — `(a->wlen <= 0 || cpatterns_same(a->word, b->word))`.
    if a.wlen > 0 && !cpatterns_same(a.word.as_deref(), b.word.as_deref()) {
        return false;
    }
    // c:91-94 — anchor checks only if CMF_LEFT/CMF_RIGHT flagged.
    if (a.flags & (CMF_LEFT | CMF_RIGHT)) != 0 {
        if a.lalen != b.lalen || a.ralen != b.ralen {                        // c:92
            return false;
        }
        if a.lalen != 0 && !cpatterns_same(a.left.as_deref(), b.left.as_deref()) {
            return false;                                                    // c:93
        }
        if a.ralen != 0 && !cpatterns_same(a.right.as_deref(), b.right.as_deref()) {
            return false;                                                    // c:94
        }
    }
    true
}

/// Direct port of `mod_export void add_bmatchers(Cmatcher m)` from
/// `Src/Zle/compmatch.c:101`. Walks the supplied Cmatcher chain
/// (the head of `def->matcher` at call sites) and prepends each
/// matcher that qualifies for brace-matching to the file-scope
/// `bmatchers` Cmlist. Original chain head is appended after the new
/// entries so the final list is `[new_entries..., old_bmatchers...]`.
pub fn add_bmatchers(m: Option<&crate::ported::zle::comp_h::Cmatcher>) {     // c:101
    use crate::ported::zle::comp_h::{Cmlist, CMF_RIGHT};
    let cell = crate::ported::zle::compcore::bmatchers
        .get_or_init(|| std::sync::Mutex::new(None));
    let old = cell.lock().ok().and_then(|mut g| g.take());                   // c:104 Cmlist old = bmatchers
    // c:105-113 — qualify each m; prepend matches in C order (reversed
    // iter so the final list is `[new_entries..., old]` per c:114 *q=old).
    let mut head = old;
    for mat in std::iter::successors(m, |p| p.next.as_deref())
        .collect::<Vec<_>>().into_iter().rev()                               // c:105 walk m
    {
        let qual = (mat.flags == 0 && mat.wlen > 0 && mat.llen > 0)          // c:107-108
                || (mat.flags == CMF_RIGHT && mat.wlen < 0 && mat.llen == 0);
        if qual {                                                            // c:109-112
            head = Some(Box::new(Cmlist {
                next: head, matcher: Box::new(mat.clone()), str: String::new(),
            }));
        }
    }
    if let Ok(mut g) = cell.lock() { *g = head; }
}

/// Direct port of `mod_export void update_bmatchers(void)` from
/// `Src/Zle/compmatch.c:121`. Called when mstack changes — ensures
/// `bmatchers` contains no matchers absent from `mstack`.
pub fn update_bmatchers() {                                                  // c:121
    let bm_cell = crate::ported::zle::compcore::bmatchers
        .get_or_init(|| std::sync::Mutex::new(None));
    let ms_cell = crate::ported::zle::compcore::mstack
        .get_or_init(|| std::sync::Mutex::new(None));
    let mut p = bm_cell.lock().ok().and_then(|mut g| g.take());              // c:124 Cmlist p = bmatchers
    let ms_head = ms_cell.lock().ok().and_then(|g| g.as_ref().map(|b| (**b).clone()));
    let mut new_bmatchers: Option<Box<crate::ported::zle::comp_h::Cmlist>> = p.as_ref().map(|b| (**b).clone()).map(Box::new);
    while let Some(node) = p {                                               // c:128 while (p)
        let mut t = false;                                                   // c:129 t = 0
        let mut ms = ms_head.as_ref();                                       // c:130 ms = mstack
        while let Some(mscur) = ms {
            if t { break; }
            let mut mp = Some(mscur.matcher.as_ref());                       // c:131 mp = ms->matcher
            while let Some(mpcur) = mp {
                if t { break; }
                t = cmatchers_same(mpcur, &*node.matcher);                   // c:132 cmatchers_same
                mp = mpcur.next.as_deref();
            }
            ms = mscur.next.as_deref();
        }
        p = node.next;                                                       // c:134 p = p->next
        if !t {                                                              // c:135 if (!t)
            new_bmatchers = p.as_ref().map(|b| (**b).clone()).map(Box::new); // c:136 bmatchers = p
        }
    }
    if let Ok(mut g) = bm_cell.lock() { *g = new_bmatchers; }
}

/// Port of `Cline get_cline(char *l, int ll, char *w, int wl, char *o,
///                            int ol, int fl)` from Src/Zle/compmatch.c:144.
///
/// "Returns a new Cline structure." The C version pools freed Clines
/// via the `freecl` heap; Rust uses normal allocation so the pool
/// dance collapses to a `Box::new`. Sets `word`/`wlen`/`line`/`llen`/
/// `orig`/`olen`/`flags` per the args; clears `prefix`/`suffix`/`min`/
/// `max`/`slen`.
pub fn get_cline(l: Option<String>, ll: i32, w: Option<String>, wl: i32,    // c:144
                 o: Option<String>, ol: i32, fl: i32)
    -> Box<crate::ported::zle::comp_h::Cline>
{
    use crate::ported::zle::comp_h::Cline;
    Box::new(Cline {
        next:   None,                                                        // c:156
        line:   l,                                                           // c:157
        llen:   ll,
        word:   w,                                                           // c:158
        wlen:   wl,
        orig:   o,                                                           // c:160
        olen:   ol,
        slen:   0,                                                           // c:161
        flags:  fl,                                                          // c:162
        prefix: None,                                                        // c:163
        suffix: None,
        min:    0,                                                           // c:164
        max:    0,
    })
}

/// Port of `free_cline(Cline l)` from `Src/Zle/compmatch.c:171`.
/// ```c
/// void
/// free_cline(Cline l)
/// {
///     Cline n;
///     while (l) {
///         n = l->next;
///         l->next = freecl;
///         freecl = l;
///         free_cline(l->prefix);
///         free_cline(l->suffix);
///         l = n;
///     }
/// }
/// ```
/// Free a Cline list. C pushes onto a `freecl` free-list to recycle;
/// Rust just drops via Box.
pub fn free_cline(l: Option<Box<crate::ported::zle::comp_h::Cline>>) {       // c:172
    // c:172-183 — walk; free each prefix/suffix recursively. In Rust
    // dropping the Box of the list head triggers Drop on `next`/
    // `prefix`/`suffix` chains automatically. `freecl` recycling
    // is a C-only zhalloc optimisation that doesn't apply here.
    drop(l);
}

/// Port of `cp_cline(Cline l, int deep)` from `Src/Zle/compmatch.c:189`.
/// ```c
/// Cline
/// cp_cline(Cline l, int deep)
/// {
///     Cline r = NULL, *p = &r, t, lp = NULL;
///     while (l) {
///         if ((t = freecl)) freecl = t->next;
///         else t = (Cline) zhalloc(sizeof(*t));
///         memcpy(t, l, sizeof(*t));
///         if (deep) {
///             if (t->prefix) t->prefix = cp_cline(t->prefix, 0);
///             if (t->suffix) t->suffix = cp_cline(t->suffix, 0);
///         }
///         *p = lp = t;
///         p = &(t->next);
///         l = l->next;
///     }
///     *p = NULL;
///     return r;
/// }
/// ```
/// Deep- or shallow-copy a Cline list. `deep` recursively copies
/// the prefix/suffix sub-lists too. The C source draws from a
/// freecl free-list when available — Rust just heap-allocates.
/// WARNING: param names don't match C — Rust=(deep) vs C=(l, deep)
pub fn cp_cline(                                                             // c:190
    l: Option<&crate::ported::zle::comp_h::Cline>,
    deep: i32,
) -> Option<Box<crate::ported::zle::comp_h::Cline>> {                        // c:189
    let mut r: Option<Box<crate::ported::zle::comp_h::Cline>> = None;        // c:192 r = NULL
    let mut tail: *mut Option<Box<crate::ported::zle::comp_h::Cline>> = &mut r;
    let mut cur = l;
    while let Some(node) = cur {                                             // c:194 while (l)
        // c:198 — `t = (Cline) zhalloc(sizeof(*t))`.
        // c:199 — `memcpy(t, l, sizeof(*t))`.
        let mut t: Box<crate::ported::zle::comp_h::Cline> = Box::new(node.clone());
        // Reset `next` so the memcpy-equivalent doesn't link to the
        // source's next (the loop sets it via the tail pointer).
        t.next = None;
        if deep != 0 {                                                       // c:200 if (deep)
            // c:201-202 — `t->prefix = cp_cline(t->prefix, 0)`. Already
            // a Box-clone via memcpy; rebuild as deep copy.
            if let Some(pre) = node.prefix.as_deref() {
                t.prefix = cp_cline(Some(pre), 0);                           // c:202
            }
            if let Some(suf) = node.suffix.as_deref() {
                t.suffix = cp_cline(Some(suf), 0);                           // c:204
            }
        }
        // c:206 — `*p = lp = t`. Append to tail.
        // SAFETY: `tail` points into `r` or into the previous node's
        // `next` field; both stay valid for the loop's lifetime.
        unsafe {
            *tail = Some(t);
            // c:207 — `p = &(t->next)`. Re-aim tail at the new entry's `next`.
            let new_node = (*tail).as_mut().unwrap();
            tail = &mut new_node.next;
        }
        cur = node.next.as_deref();                                          // c:208 l = l->next
    }
    // c:210 — `*p = NULL`. Already None by default.
    r                                                                        // c:212 return r
}

// =====================================================================
// cline_sublen / cline_setlens / cline_matched / revert_cline / cp_cline
// — `Src/Zle/compmatch.c:217-281`.
// =====================================================================

/// Port of `cline_sublen(Cline l)` from `Src/Zle/compmatch.c:218`.
/// ```c
/// int
/// cline_sublen(Cline l)
/// {
///     int len = ((l->flags & CLF_LINE) ? l->llen : l->wlen);
///     if (l->olen && !((l->flags & CLF_SUF) ? l->suffix : l->prefix))
///         len += l->olen;
///     else {
///         Cline p;
///         for (p = l->prefix; p; p = p->next)
///             len += ((p->flags & CLF_LINE) ? p->llen : p->wlen);
///         for (p = l->suffix; p; p = p->next)
///             len += ((p->flags & CLF_LINE) ? p->llen : p->wlen);
///     }
///     return len;
/// }
/// ```
/// Total visual length of one Cline plus its prefix/suffix sub-lists.
pub fn cline_sublen(l: &crate::ported::zle::comp_h::Cline) -> i32 {          // c:219
    use crate::ported::zle::comp_h::{CLF_LINE, CLF_SUF};
    // c:221 — `len = (CLF_LINE ? llen : wlen)`.
    let mut len: i32 = if (l.flags & CLF_LINE) != 0 { l.llen } else { l.wlen };
    // c:223 — `if (olen && !((CLF_SUF ? suffix : prefix))) len += olen`.
    let no_subs = if (l.flags & CLF_SUF) != 0 {
        l.suffix.is_none()
    } else {
        l.prefix.is_none()
    };
    if l.olen != 0 && no_subs {
        len += l.olen;                                                       // c:224
    } else {                                                                 // c:225
        // c:228-229 — walk prefix sub-list summing per-part length.
        let mut p = l.prefix.as_deref();
        while let Some(pp) = p {
            len += if (pp.flags & CLF_LINE) != 0 { pp.llen } else { pp.wlen };
            p = pp.next.as_deref();
        }
        // c:230-231 — walk suffix sub-list.
        let mut p = l.suffix.as_deref();
        while let Some(pp) = p {
            len += if (pp.flags & CLF_LINE) != 0 { pp.llen } else { pp.wlen };
            p = pp.next.as_deref();
        }
    }
    len                                                                      // c:233 return len
}

/// Port of `cline_setlens(Cline l, int both)` from `Src/Zle/compmatch.c:240`.
/// ```c
/// void
/// cline_setlens(Cline l, int both)
/// {
///     while (l) {
///         l->min = cline_sublen(l);
///         if (both)
///             l->max = l->min;
///         l = l->next;
///     }
/// }
/// ```
/// Walk a Cline list setting `min` (and optionally `max`) from
/// `cline_sublen`.
pub fn cline_setlens(l: &mut Option<Box<crate::ported::zle::comp_h::Cline>>, both: i32) {  // c:240
    let mut cur = l.as_deref_mut();
    while let Some(node) = cur {                                             // c:242 while (l)
        let s = cline_sublen(node);                                          // c:243 cline_sublen(l)
        node.min = s;                                                        // c:243 l->min = ...
        if both != 0 {                                                       // c:244 if (both)
            node.max = s;                                                    // c:245 l->max = l->min
        }
        cur = node.next.as_deref_mut();                                      // c:246 l = l->next
    }
}

// =====================================================================
// matchbuf / matchparts / matchsubs globals + start_match / abort_match
// — `Src/Zle/compmatch.c:283-317`.
// =====================================================================

use std::sync::Mutex;
use std::sync::OnceLock;

/// Port of `cline_matched(Cline p)` from `Src/Zle/compmatch.c:254`.
/// ```c
/// void
/// cline_matched(Cline p)
/// {
///     while (p) {
///         p->flags |= CLF_MATCHED;
///         cline_matched(p->prefix);
///         cline_matched(p->suffix);
///         p = p->next;
///     }
/// }
/// ```
/// Set `CLF_MATCHED` on every Cline reachable through next/prefix/
/// suffix from `p`.
pub fn cline_matched(p: &mut Option<Box<crate::ported::zle::comp_h::Cline>>) {  // c:254
    use crate::ported::zle::comp_h::CLF_MATCHED;
    let mut cur = p.as_deref_mut();
    while let Some(node) = cur {                                             // c:256 while (p)
        node.flags |= CLF_MATCHED;                                           // c:257
        cline_matched(&mut node.prefix);                                     // c:258
        cline_matched(&mut node.suffix);                                     // c:259
        cur = node.next.as_deref_mut();                                      // c:261 p = p->next
    }
}

/// Port of `revert_cline(Cline p)` from `Src/Zle/compmatch.c:269`.
/// ```c
/// Cline
/// revert_cline(Cline p)
/// {
///     Cline r = NULL, n;
///     while (p) {
///         n = p->next;
///         p->next = r;
///         r = p;
///         p = n;
///     }
///     return r;
/// }
/// ```
/// Reverse a Cline `next`-chained list in place; returns the new head.
/// WARNING: param names don't match C — Rust=() vs C=(p)
pub fn revert_cline(                                                         // c:270
    mut p: Option<Box<crate::ported::zle::comp_h::Cline>>,
) -> Option<Box<crate::ported::zle::comp_h::Cline>> {                        // c:269
    let mut r: Option<Box<crate::ported::zle::comp_h::Cline>> = None;        // c:272 r = NULL
    while let Some(mut node) = p {                                           // c:274 while (p)
        let n = node.next.take();                                            // c:275 n = p->next
        node.next = r;                                                       // c:276 p->next = r
        r = Some(node);                                                      // c:277 r = p
        p = n;                                                               // c:278 p = n
    }
    r                                                                        // c:280 return r
}

/// Port of `start_match()` from `Src/Zle/compmatch.c:300`.
/// ```c
/// static void
/// start_match(void)
/// {
///     if (matchbuf)
///         *matchbuf = '\0';
///     matchbufadded = 0;
///     matchparts = matchlastpart = matchsubs = matchlastsub = NULL;
/// }
/// ```
/// Reset the per-match globals so a fresh pattern run starts clean.
pub fn start_match() {                                                       // c:300
    // c:300-303 — `if (matchbuf) *matchbuf = '\0'`.
    MATCHBUF
        .get_or_init(|| Mutex::new(String::new()))
        .lock()
        .unwrap()
        .clear();
    // c:305 — `matchparts = matchlastpart = matchsubs = matchlastsub = NULL`.
    *MATCHPARTS.get_or_init(|| Mutex::new(None)).lock().unwrap() = None;
    *MATCHSUBS.get_or_init(|| Mutex::new(None)).lock().unwrap() = None;
}

/// Port of `abort_match()` from `Src/Zle/compmatch.c:312`.
/// ```c
/// static void
/// abort_match(void)
/// {
///     free_cline(matchparts);
///     free_cline(matchsubs);
///     matchparts = matchsubs = NULL;
/// }
/// ```
/// C body (compmatch.c:312, 3 lines):
///     `free_cline(matchparts);
///      free_cline(matchsubs);
///      matchparts = matchsubs = NULL;`
/// The `take()` on each guard discards the old chain (Rust drop runs
/// `free_cline`) and leaves the slot None — same observable state.
pub fn abort_match() {                                                       // c:312
    free_cline(MATCHPARTS.get_or_init(|| Mutex::new(None)).lock().unwrap().take()); // c:313
    free_cline(MATCHSUBS.get_or_init(|| Mutex::new(None)).lock().unwrap().take());  // c:314
}

/// Direct port of `static void add_match_str(Cmatcher m, char *l,
///                                          char *w, int wl, int sfx)`
/// from `Src/Zle/compmatch.c:327`. Pushes the string `w` (or
/// `l` when `m & CMF_LINE`) of length `wl` into the file-scope
/// `MATCHBUF` accumulator; `sfx` prepends instead of appends.
pub fn add_match_str(m: Option<&crate::ported::zle::comp_h::Cmatcher>,        // c:327
                     l: &str, w: &str, mut wl: i32, sfx: i32)
{
    use crate::ported::zle::comp_h::CMF_LINE;

    // c:332-334 — `if (m && (m->flags & CMF_LINE)) { wl = m->llen; w = l; }`.
    let (eff_w_owned, eff_w): (String, &str) = match m {
        Some(mat) if (mat.flags & CMF_LINE) != 0 => {
            wl = mat.llen;
            let owned = l.to_string();
            let s = owned.clone();
            (owned, Box::leak(s.into_boxed_str()))
        }
        _ => (String::new(), w),
    };
    let _ = eff_w_owned;

    if wl <= 0 { return; }                                                   // c:335

    // c:337-353 — buffer-grow + insert. Rust's String handles the
    // grow path; we still mirror the matchbufadded counter for parity
    // with `MATCHBUFLEN`-checking C call sites.
    if let Ok(mut buf) = MATCHBUF.get_or_init(|| Mutex::new(String::new())).lock() {
        let take_n = wl as usize;
        let new_chunk: String = eff_w.chars().take(take_n).collect();
        if sfx != 0 {                                                        // c:354 prefix-mode
            *buf = format!("{}{}", new_chunk, *buf);                         // c:356
        } else {                                                             // c:358
            buf.push_str(&new_chunk);
        }
        MATCHBUFADDED.fetch_add(wl, std::sync::atomic::Ordering::Relaxed);   // c:362
    }
}

/// Direct port of `static void add_match_part(Cmatcher m, char *l,
///                                            char *w, int wl,
///                                            char *o, int ol,
///                                            char *s, int sl,
///                                            int osl, int sfx)`
/// from `Src/Zle/compmatch.c:373`. Appends a partial match into
/// `MATCHPARTS`, splitting the new part via `bld_parts` per the
/// matcher's anchor rules and consuming any pending `MATCHSUBS`
/// nodes into the new tail.
pub fn add_match_part(
    m: Option<&crate::ported::zle::comp_h::Cmatcher>,                        // c:373
    l: Option<&str>, _ll: i32,
    w: &str, wl: i32,
    o: Option<&str>, ol: i32,
    s: &str, sl: i32,
    osl: i32, sfx: i32,
) {
    use crate::ported::zle::comp_h::{Cline, CMF_LEFT, CLF_NEW, CLF_SUF};

    // c:382 — `if (l && !strncmp(l, w, wl)) l = NULL` — drop redundant anchor.
    let l_eff: Option<String> = match l {
        Some(lstr) if lstr.len() >= wl as usize
                    && wl > 0
                    && &lstr[..wl as usize] == &w[..wl as usize] => None,
        Some(lstr) => Some(lstr.to_string()),
        None       => None,
    };

    // c:392 — `p = bld_parts(s, sl, osl, &lp, &lprem)`.
    let mut lp: Option<Box<Cline>> = None;
    let mut lprem: Option<Box<Cline>> = None;
    let mut p = bld_parts(s, sl, osl, Some(&mut lp), Some(&mut lprem));

    // c:394 — `if (lprem && m && (m->flags & CLF_LEFT))`.
    if let Some(rem) = lprem.as_mut() {
        if m.map(|mat| (mat.flags & CMF_LEFT) != 0).unwrap_or(false) {
            rem.flags |= CLF_SUF;                                            // c:395
            rem.suffix = rem.prefix.take();                                  // c:396 swap
        }
    }

    // c:402 — `if (sfx) p = revert_cline(lp = p)`.
    if sfx != 0 {
        if let Some(chain) = p.take() {
            p = revert_cline(Some(chain));
        }
    }

    // c:405-419 — merge MATCHSUBS into the head/tail.
    let subs = MATCHSUBS.get_or_init(|| std::sync::Mutex::new(None))
        .lock().ok().and_then(|mut g| g.take());
    if let Some(subs_chain) = subs {                                         // c:405
        if let Some(lp_node) = lp.as_mut() {
            if sfx != 0 {                                                    // c:407 lp->prefix tail-append
                let mut tail_ref: *mut Option<Box<Cline>> = &mut lp_node.prefix;
                unsafe {
                    while let Some(ref mut next_node) = *tail_ref {
                        tail_ref = &mut next_node.next as *mut _;
                    }
                    *tail_ref = Some(subs_chain);
                }
            } else if let Some(ref mut p_node) = p {                         // c:415 p->prefix prepend
                let old_prefix = p_node.prefix.take();
                let mut new_head = subs_chain;
                {
                    let mut tail_ref: *mut Option<Box<Cline>> = &mut new_head.next;
                    unsafe {
                        while let Some(ref mut nn) = *tail_ref {
                            tail_ref = &mut nn.next as *mut _;
                        }
                        *tail_ref = old_prefix;
                    }
                }
                p_node.prefix = Some(new_head);
            }
        }
        // c:417 — `matchsubs = matchlastsub = NULL`.
        if let Ok(mut g) = MATCHLASTSUB
            .get_or_init(|| std::sync::Mutex::new(None)).lock()
        {
            *g = None;
        }
    }

    // c:421-435 — store args in the last part-cline.
    if let Some(lp_node) = lp.as_mut() {
        if lp_node.llen != 0 || lp_node.wlen != 0 {                          // c:421
            let next = get_cline(
                l_eff.clone(), wl, Some(w.to_string()), wl,
                o.map(|s| s.to_string()), ol, CLF_NEW,
            );
            lp_node.next = Some(next);                                       // c:423
        } else {                                                             // c:425
            lp_node.line = l_eff.clone();                                    // c:426
            lp_node.llen = wl;
            lp_node.word = Some(w.to_string());                              // c:428
            lp_node.wlen = wl;
            lp_node.orig = o.map(|s| s.to_string());                         // c:430
            lp_node.olen = ol;
        }
        if o.is_some() || ol != 0 {                                          // c:432
            lp_node.flags &= !CLF_NEW;
        }
    }

    // c:439-444 — append `p` to MATCHPARTS via MATCHLASTPART.
    let last_present = MATCHLASTPART.get()
        .and_then(|c| c.lock().ok().map(|g| g.is_some()))
        .unwrap_or(false);
    if last_present {                                                        // c:440
        if let Ok(mut tail) = MATCHLASTPART
            .get_or_init(|| std::sync::Mutex::new(None)).lock()
        {
            if let Some(t) = tail.as_mut() {
                t.next = p.clone();
            }
        }
    } else if let Ok(mut head) = MATCHPARTS
        .get_or_init(|| std::sync::Mutex::new(None)).lock()
    {
        *head = p.clone();                                                   // c:442
    }
    if let Some(lp_node) = lp {
        if let Ok(mut tail) = MATCHLASTPART
            .get_or_init(|| std::sync::Mutex::new(None)).lock()
        {
            *tail = Some(lp_node);                                           // c:443
        }
    }
}

// Fake `parse_cmatcher` / `update_bmatchers` deleted.
// `parse_cmatcher` already exists at `complete.rs:992` as a real
// port of `Src/Zle/complete.c:242`. `update_bmatchers` is at
// `Src/Zle/compmatch.c:121` with signature `void update_bmatchers(void)`
// — the Rust placeholder had the wrong arity and type, will land
// alongside the matcher-engine driver.



/// Direct port of `static void add_match_sub(Cmatcher m, char *l, int ll,
///                                          char *w, int wl)` from
/// `Src/Zle/compmatch.c:446`. Pushes one sub-match cline node
/// into the file-scope `MATCHSUBS` / `MATCHLASTSUB` linked list.
/// Called from match_str during a CMF_RIGHT anchor match.
pub fn add_match_sub(
    m: Option<&crate::ported::zle::comp_h::Cmatcher>,                        // c:446
    l: Option<&str>, ll: i32, w: Option<&str>, wl: i32,
) {
    use crate::ported::zle::comp_h::{Cline, CLF_NEW};

    // c:450-453 — `if (m && (m->flags & CMF_LINE)) { wl = m->llen; w = l; }`.
    let (eff_w, eff_wl) = match m {
        Some(mat) if (mat.flags & crate::ported::zle::comp_h::CMF_LINE) != 0
                  => (l, mat.llen),
        _ => (w, wl),
    };

    // c:455-456 — short-circuit if no length.
    if eff_wl <= 0 && ll <= 0 { return; }

    // c:464-484 — build a fresh Cline node and append to matchsubs.
    let node = Box::new(Cline {
        flags: CLF_NEW,
        line: l.map(|s| s.to_string()),
        llen: ll,
        word: eff_w.map(|s| s.to_string()),
        wlen: eff_wl,
        ..Default::default()
    });

    let last_cell = MATCHLASTSUB.get_or_init(|| Mutex::new(None));
    let head_cell = MATCHSUBS.get_or_init(|| Mutex::new(None));
    let last_present = last_cell.lock().ok().map(|g| g.is_some()).unwrap_or(false);
    if last_present {                                                        // c:494 — chain to existing tail
        if let Ok(mut tail) = last_cell.lock() {
            if let Some(t) = tail.as_mut() {
                t.next = Some(node.clone());                                 // c:495 matchlastsub->next = n
            }
        }
    } else {                                                                 // c:496 — first node
        if let Ok(mut h) = head_cell.lock() {
            *h = Some(node.clone());                                         // c:497 matchsubs = n
        }
    }
    if let Ok(mut tail) = last_cell.lock() {
        *tail = Some(node);                                                  // c:499 matchlastsub = n
    }
}

// Real-port of `match_str` lands below. The exact-char skip fast
// path (c:569-590), non-* matcher loop with CMF_LEFT/RIGHT anchors
// (c:868-989), and *-pattern matcher loop in both prefix and
// suffix modes (c:603-867 / c:735-776) are all real-bodied.

/// Direct port of `static int match_str(char *l, char *w, Brinfo *bpp,
///                                       int bc, int *rwlp, const int sfx,
///                                       int test, int part)`
/// from `Src/Zle/compmatch.c:500-1085`. The matcher application
/// engine: walks the line string `l` against the word string `w`
/// using each `Cmlist` in the global `mstack` chain. Builds
/// `matchparts` / `matchsubs` along the way, threads brace-position
/// info via `bpp`. Returns the number of `w` bytes consumed on a
/// full match, -1 on no match.
///
/// **Port scope:** all matcher paths real-bodied — exact-char skip
/// fast path (c:569-590), non-* matcher loop with CMF_LEFT/RIGHT
/// anchors + pattern_match + add_match_str/sub emit (c:868-989),
/// *-pattern matcher loop in prefix mode (c:603-867) and suffix
/// mode (c:735-776 with bounded recursive call), exact-rewind
/// retry (c:1020-1034), test/part-mode returns (c:1046-1084).
pub fn match_str(                                                            // c:500
    l_in: &str, w_in: &str,
    _bpp: Option<&mut Option<Box<crate::ported::zle::zle_h::brinfo>>>,
    bc: i32,
    rwlp: Option<&mut i32>,
    sfx: i32, test: i32, part: i32,
) -> i32 {
    use crate::ported::zle::comp_h::{CMF_INTER, CMF_LEFT, CMF_LINE, CMF_RIGHT};

    let l_bytes = l_in.as_bytes();
    let w_bytes = w_in.as_bytes();
    let mut ll = l_bytes.len() as i32;
    let mut lw = w_bytes.len() as i32;
    let mut il: i32 = 0;
    let mut iw: i32 = 0;
    let mut exact: i32 = 0;
    let mut wexact: i32 = 0;
    let mut bc = bc;
    let _obc = bc;
    let add: i32 = if sfx != 0 { -1 } else { 1 };
    let ind: i32 = if sfx != 0 { -1 } else { 0 };

    if test == 0 {                                                           // c:523
        start_match();
    }

    // Track positions as byte indices. In sfx mode we walk from the
    // end backwards; ind=-1 means "previous byte". We use signed
    // cursors so the arithmetic mirrors C's pointer arithmetic.
    let mut l_pos: i32 = if sfx != 0 { ll } else { 0 };
    let mut w_pos: i32 = if sfx != 0 { lw } else { 0 };
    let mut ow_pos: i32 = w_pos;
    let mut lm: Option<Box<crate::ported::zle::comp_h::Cmatcher>> = None;
    let mut he = 0i32;

    // Snapshot the mstack chain into a Vec for stable iteration.
    let mstack_snapshot: Vec<Box<crate::ported::zle::comp_h::Cmatcher>> = {
        let g = crate::ported::zle::compcore::mstack
            .get_or_init(|| std::sync::Mutex::new(None))
            .lock().ok();
        let mut out = Vec::new();
        if let Some(g) = g {
            let mut cur = g.as_deref();
            while let Some(ms) = cur {
                let mut mp_cur: Option<&crate::ported::zle::comp_h::Cmatcher> =
                    Some(&*ms.matcher);
                while let Some(mp) = mp_cur {
                    out.push(Box::new(mp.clone()));
                    mp_cur = mp.next.as_deref();
                }
                cur = ms.next.as_deref();
            }
        }
        out
    };

    'outer: while ll > 0 {                                                   // c:546
        // c:569-590 — exact-char skip fast path.
        if sfx == 0 && lw > 0 && (part == 0 || test != 0) {
            let l_idx = (l_pos + ind) as usize;
            let w_idx = (w_pos + ind) as usize;
            if l_idx < l_bytes.len() && w_idx < w_bytes.len() {
                let l_ch = l_bytes[l_idx];
                let w_ch = w_bytes[w_idx];
                let bslash = lw > 1 && w_ch == b'\\'
                    && w_idx + 1 < w_bytes.len()
                    && w_bytes[w_idx + 1] == l_bytes[(l_pos + ind) as usize];
                if l_ch == w_ch || bslash {
                    let advance_w = if bslash { 2 } else { 1 };
                    l_pos += add; w_pos += if bslash { add + add } else { add };
                    il += 1; iw += advance_w;
                    ll -= 1; lw -= advance_w;
                    bc += 1;
                    exact += 1;
                    wexact += advance_w;
                    lm = None;
                    he = 0;
                    continue 'outer;                                         // c:589
                }
            }
        }

        // c:591 retry: walk the snapshotted matcher chain looking for
        // a non-* matcher we can apply at the current cursor.
        let mut matched: Option<Box<crate::ported::zle::comp_h::Cmatcher>> = None;
        for mp in mstack_snapshot.iter() {
            if let Some(ref lm_box) = lm {
                if std::ptr::addr_eq(lm_box.as_ref() as *const _,
                                      mp.as_ref() as *const _) {
                    continue;                                                // c:595
                }
            }
            if mp.wlen < 0 {
                // c:603-867 — `*`-pattern matcher. Handles both prefix
                // (sfx == 0) and suffix (sfx != 0) modes.

                // c:689-694 — set up llen / alen / aol per CMF_LEFT.
                let llen_p = mp.llen;
                let (alen, aol): (i32, i32) = if (mp.flags & CMF_LEFT) != 0 {
                    (mp.lalen, mp.ralen)
                } else {
                    (mp.ralen, mp.lalen)
                };
                if ll < llen_p + alen || lw < alen + aol {                   // c:698
                    continue;
                }

                // c:701-715 — set ap/aop/moff/loff/aoff/both per CMF_LEFT
                // × sfx. Four combinations.
                let (ap, aop, moff, both, loff, aoff): (
                    Option<&crate::ported::zle::comp_h::Cpattern>,
                    Option<&crate::ported::zle::comp_h::Cpattern>,
                    i32, i32, i32, i32);
                if (mp.flags & CMF_LEFT) != 0 {                              // c:701
                    ap = mp.left.as_deref();
                    aop = mp.right.as_deref();
                    moff = alen;
                    if sfx != 0 {                                            // c:703
                        both = 0; loff = -llen_p; aoff = -(llen_p + alen);
                    } else {                                                 // c:706
                        both = 1; loff = alen; aoff = 0;
                    }
                } else {                                                     // c:708
                    ap = mp.right.as_deref();
                    aop = mp.left.as_deref();
                    moff = 0;
                    if sfx != 0 {                                            // c:710
                        both = 1; loff = -(llen_p + alen); aoff = -alen;
                    } else {                                                 // c:712
                        both = 0; loff = 0; aoff = llen_p;
                    }
                }

                // c:717 — pattern_match(mp.line, l + loff).
                let l_off_idx = (l_pos + loff).max(0) as usize;
                if l_off_idx >= l_bytes.len() { continue; }
                let line_slice = std::str::from_utf8(
                    &l_bytes[l_off_idx..]).unwrap_or("");
                if pattern_match(mp.line.as_deref(), line_slice, None, "") == 0 {
                    continue;
                }
                // c:719-731 — anchor test.
                if let Some(ap_pat) = ap {
                    let l_anchor_idx = (l_pos + aoff).max(0) as usize;
                    let l_anchor = std::str::from_utf8(
                        &l_bytes[l_anchor_idx..]).unwrap_or("");
                    if pattern_match(Some(ap_pat), l_anchor, None, "") == 0 {
                        continue;
                    }
                    if both != 0 {                                            // c:721
                        let w_anchor_idx = (w_pos + aoff).max(0) as usize;
                        let w_anchor = std::str::from_utf8(
                            &w_bytes[w_anchor_idx..]).unwrap_or("");
                        if pattern_match(Some(ap_pat), w_anchor, None, "") == 0 {
                            continue;
                        }
                        if aol > 0 && aol <= aoff + iw {
                            let w_op_idx = (w_pos + aoff - aol).max(0) as usize;
                            let w_op = std::str::from_utf8(
                                &w_bytes[w_op_idx..]).unwrap_or("");
                            if pattern_match(aop, w_op, None, "") == 0 {
                                continue;
                            }
                        }
                        // c:726 — match_parts to confirm anchor span.
                        let mp_l = std::str::from_utf8(
                            &l_bytes[l_anchor_idx..]).unwrap_or("");
                        let mp_w = std::str::from_utf8(
                            &w_bytes[(w_pos + aoff).max(0) as usize..])
                            .unwrap_or("");
                        if match_parts(mp_l, mp_w, alen, part) == 0 {
                            continue;
                        }
                    }
                } else {                                                     // c:728
                    let cmf_check = if (mp.flags & CMF_INTER) != 0 {
                        if (mp.flags & CMF_LINE) != 0 { iw } else { il }
                    } else { il | iw };
                    if both == 0 || cmf_check != 0 {
                        continue;
                    }
                }

                // c:737-773 — recursive scan: try each tp from w forward
                // looking for a position where `l + llen + moff` matches.
                let mut t = 0i32;
                let mut ct = 0i32;
                let ict_total = lw - alen + 1;
                let mut found_tp_pos: i32 = w_pos;
                // c:737 — tp walks from w outward. In prefix mode (add=+1)
                // forward through w[w_pos..]; in sfx mode (add=-1)
                // backward through w[..w_pos]. We iterate ict_total
                // steps, computing tp_pos as w_pos + ct*add.
                for step in 0..ict_total.max(0) {
                    let tp_pos = w_pos + step * add;
                    let mut accept = false;
                    if both != 0 {
                        // c:740-745 — both-mode: succeed only if ap stops
                        // matching at the current tp (the `*` consumed
                        // characters before reaching the anchor).
                        let ap_fails = ap.is_none() || test == 0 || {
                            let tp_anchor_idx = (tp_pos + aoff).max(0) as usize;
                            let tp_slice = std::str::from_utf8(
                                &w_bytes.get(tp_anchor_idx..).unwrap_or(&[]))
                                .unwrap_or("");
                            pattern_match(ap, tp_slice, None, "") == 0
                        };
                        if ap_fails {
                            accept = true;
                        }
                    } else {
                        // c:746-753 — non-both: succeed when ap matches at
                        // tp - moff and aop matches at tp - moff - aol.
                        let tp_anchor_idx = (tp_pos - moff).max(0) as usize;
                        let tp_slice = std::str::from_utf8(
                            &w_bytes.get(tp_anchor_idx..).unwrap_or(&[]))
                            .unwrap_or("");
                        if pattern_match(ap, tp_slice, None, "") != 0 {
                            let aol_ok = aol == 0 || (aol <= iw + ct - moff && {
                                let aop_idx = (tp_pos - moff - aol).max(0) as usize;
                                let aop_slice = std::str::from_utf8(
                                    &w_bytes.get(aop_idx..).unwrap_or(&[]))
                                    .unwrap_or("");
                                pattern_match(aop, aop_slice, None, "") != 0
                            });
                            if aol_ok {
                                let l_aoff_idx = (l_pos + aoff).max(0) as usize;
                                let l_aoff_slice = std::str::from_utf8(
                                    &l_bytes[l_aoff_idx..]).unwrap_or("");
                                let mp_ok = mp.wlen == -1 || match_parts(
                                    l_aoff_slice, tp_slice, alen, part) != 0;
                                if mp_ok {
                                    accept = true;
                                }
                            }
                        }
                    }

                    if accept {
                        // c:757-769 — recursive match_str call.
                        if sfx != 0 {
                            // c:763 — l-ll, w-lw with bounded slices.
                            // C uses savl + tp[-alen] NUL trick; in Rust
                            // we pass slice up to position (l_pos - llen_p
                            // - alen) for l (the "savl" boundary) and up
                            // to (tp_pos - alen) for w (the "savw"
                            // boundary).
                            let l_bound = (l_pos - llen_p - alen).max(0) as usize;
                            let w_bound = (tp_pos - alen).max(0) as usize;
                            let l_rest = std::str::from_utf8(
                                &l_bytes[..l_bound.min(l_bytes.len())])
                                .unwrap_or("");
                            let w_rest = std::str::from_utf8(
                                &w_bytes[..w_bound.min(w_bytes.len())])
                                .unwrap_or("");
                            t = match_str(l_rest, w_rest, None, 0, None, sfx,
                                           2, part);
                        } else {
                            // c:768 — l + llen + moff, tp + moff.
                            let l_rest_start = (l_pos + llen_p + moff) as usize;
                            let l_rest = std::str::from_utf8(
                                &l_bytes.get(l_rest_start..).unwrap_or(&[]))
                                .unwrap_or("");
                            let w_rest_start = (tp_pos + moff) as usize;
                            let w_rest = std::str::from_utf8(
                                &w_bytes.get(w_rest_start..).unwrap_or(&[]))
                                .unwrap_or("");
                            t = match_str(l_rest, w_rest, None, 0, None, sfx,
                                           1, part);
                        }
                        if t != 0 || (mp.wlen == -1 && both == 0) {
                            found_tp_pos = tp_pos;
                            break;
                        }
                    }
                    ct += 1;
                }

                // c:780 — no match found in the recursive scan.
                if t == 0 { continue; }

                // c:783-833 — emit Cline parts via add_match_*.
                let _tp_pos = found_tp_pos;
                if test == 0 && (he == 0 || (llen_p + alen) != 0) {
                    // c:789-805 — op/ol/lp/map/wap/wmp computed per sfx mode.
                    let (op_start, ol, lp_start, map_start, wap_start, wmp_start);
                    if sfx != 0 {                                             // c:789
                        op_start = w_pos as usize;
                        ol = (ow_pos - w_pos).max(0);
                        lp_start = (l_pos - (llen_p + alen)).max(0) as usize;
                        map_start = (found_tp_pos - alen).max(0) as usize;
                        if (mp.flags & CMF_LEFT) != 0 {                       // c:792
                            wap_start = (found_tp_pos - alen).max(0) as usize;
                            wmp_start = found_tp_pos as usize;
                        } else {                                              // c:794
                            wap_start = (w_pos - alen).max(0) as usize;
                            wmp_start = (found_tp_pos - alen).max(0) as usize;
                        }
                    } else {                                                  // c:797
                        op_start = ow_pos as usize;
                        ol = (w_pos - ow_pos).max(0);
                        lp_start = l_pos as usize;
                        map_start = ow_pos as usize;
                        if (mp.flags & CMF_LEFT) != 0 {                       // c:800
                            wap_start = w_pos as usize;
                            wmp_start = (w_pos + alen) as usize;
                        } else {                                              // c:802
                            wap_start = found_tp_pos as usize;
                            wmp_start = ow_pos as usize;
                        }
                    }

                    if (mp.flags & CMF_LINE) != 0 {                          // c:810
                        let op_str = std::str::from_utf8(
                            &w_bytes[op_start..op_start + ol as usize])
                            .unwrap_or("");
                        let lp_str = std::str::from_utf8(
                            &l_bytes[lp_start..lp_start + (llen_p + alen) as usize])
                            .unwrap_or("");
                        add_match_str(None, "", op_str, ol, sfx);
                        add_match_str(None, "", lp_str, llen_p + alen, sfx);
                        add_match_sub(None, None, ol, Some(op_str), ol);
                        add_match_sub(None, None, llen_p + alen,
                                      Some(lp_str), llen_p + alen);
                    } else {                                                 // c:822
                        let map_len = ct + ol + alen;
                        let map_str = std::str::from_utf8(
                            &w_bytes[map_start..(map_start + map_len.max(0) as usize)
                                                 .min(w_bytes.len())])
                            .unwrap_or("");
                        add_match_str(None, "", map_str, map_len, sfx);
                        let ol_eff = if both != 0 {
                            let op_str = std::str::from_utf8(
                                &w_bytes[op_start..op_start + ol as usize])
                                .unwrap_or("");
                            add_match_sub(None, None, ol, Some(op_str), ol);
                            -1
                        } else {
                            ct + ol
                        };
                        let l_aoff_idx = (l_pos + aoff).max(0) as usize;
                        let l_loff_idx = (l_pos + loff).max(0) as usize;
                        let l_aoff_str = std::str::from_utf8(
                            &l_bytes[l_aoff_idx..l_aoff_idx + alen.max(0) as usize])
                            .unwrap_or("");
                        let l_loff_str = std::str::from_utf8(
                            &l_bytes[l_loff_idx..l_loff_idx + llen_p.max(0) as usize])
                            .unwrap_or("");
                        let wap_str = std::str::from_utf8(
                            &w_bytes[wap_start..(wap_start + alen.max(0) as usize)
                                                  .min(w_bytes.len())])
                            .unwrap_or("");
                        let wmp_str = std::str::from_utf8(
                            &w_bytes[wmp_start..(wmp_start + ol_eff.max(0) as usize)
                                                  .min(w_bytes.len())])
                            .unwrap_or("");
                        add_match_part(Some(mp),
                                       Some(l_aoff_str), alen,
                                       wap_str, alen,
                                       Some(l_loff_str), llen_p,
                                       wmp_str, ol_eff,
                                       ol_eff, sfx);
                    }
                }

                // c:834-866 — advance pointers past the matched portion
                // + anchor. In sfx mode positions decrement; in prefix
                // mode they increment.
                let llen_new = llen_p + alen;
                let alen_new = alen + ct;
                if sfx != 0 {                                                // c:836
                    l_pos -= llen_new;
                    w_pos -= alen_new;
                } else {                                                     // c:839
                    l_pos += llen_new;
                    w_pos += alen_new;
                }
                ll -= llen_new; il += llen_new;
                lw -= alen_new; iw += alen_new;
                bc += llen_new;
                exact = 0;
                ow_pos = w_pos;

                if llen_new == 0 && alen_new == 0 {                          // c:856
                    lm = Some(Box::new((**mp).clone()));
                    if he == 0 {
                        he = 1;
                    } else {
                        // signal outer loop continue
                        matched = Some(mp.clone());
                        break;
                    }
                } else {
                    lm = None;
                    he = 0;
                }
                matched = Some(mp.clone());
                break;
            }
            if ll < mp.llen || lw < mp.wlen { continue; }                    // c:868

            // c:880-884 — skip if line and word substrings are identical
            // (the exact-char skip above already handled trivial overlap).
            if (mp.flags & (CMF_LEFT | CMF_RIGHT)) == 0
                && mp.llen == mp.wlen
            {
                let (l_start, w_start) = if sfx != 0 {
                    ((l_pos - mp.llen) as usize, (w_pos - mp.wlen) as usize)
                } else {
                    (l_pos as usize, w_pos as usize)
                };
                let l_chunk = &l_bytes[l_start..l_start + mp.llen as usize];
                let w_chunk = &w_bytes[w_start..w_start + mp.wlen as usize];
                if l_chunk == w_chunk { continue; }
            }

            // c:889-897 — local cursors tl/tw/tll/tlw/til/tiw.
            let (tl_pos, tw_pos, til, tiw, tll, tlw) = if sfx != 0 {
                (l_pos - mp.llen, w_pos - mp.wlen,
                 ll - mp.llen, lw - mp.wlen,
                 il + mp.llen, iw + mp.wlen)
            } else {
                (l_pos, w_pos, il, iw, ll, lw)
            };

            let mut t: i32 = 1;
            // c:898-915 — CMF_LEFT anchor test.
            if (mp.flags & CMF_LEFT) != 0 {
                if til < mp.lalen || tiw < mp.lalen + mp.ralen {
                    continue;
                }
                if let Some(ref left_pat) = mp.left {
                    let l_anchor_start = (tl_pos - mp.lalen) as usize;
                    let w_anchor_start = (tw_pos - mp.lalen) as usize;
                    let l_slice = std::str::from_utf8(
                        &l_bytes[l_anchor_start..]).unwrap_or("");
                    let w_slice = std::str::from_utf8(
                        &w_bytes[w_anchor_start..]).unwrap_or("");
                    let lm_ok = pattern_match(Some(left_pat), l_slice, None, "") != 0;
                    let wm_ok = pattern_match(Some(left_pat), w_slice, None, "") != 0;
                    let r_ok = mp.ralen == 0 || {
                        let r_anchor_start = (tw_pos - mp.lalen - mp.ralen) as usize;
                        let r_slice = std::str::from_utf8(
                            &w_bytes[r_anchor_start..]).unwrap_or("");
                        let right_pat = mp.right.as_deref();
                        pattern_match(right_pat, r_slice, None, "") != 0
                    };
                    t = if lm_ok && wm_ok && r_ok { 1 } else { 0 };
                } else {
                    let cmf_check = if (mp.flags & CMF_INTER) != 0 {
                        if (mp.flags & CMF_LINE) != 0 { iw } else { il }
                    } else { il | iw };
                    t = if sfx == 0 && cmf_check == 0 { 1 } else { 0 };
                }
            }
            // c:916-938 — CMF_RIGHT anchor test.
            if (mp.flags & CMF_RIGHT) != 0 {
                if tll < mp.llen + mp.ralen
                    || tlw < mp.wlen + mp.ralen + mp.lalen
                {
                    continue;
                }
                if let Some(ref right_pat) = mp.right {
                    let l_anchor_start = (tl_pos + mp.llen) as usize;
                    let w_anchor_start = (tw_pos + mp.wlen) as usize;
                    let l_slice = std::str::from_utf8(
                        &l_bytes[l_anchor_start..]).unwrap_or("");
                    let w_slice = std::str::from_utf8(
                        &w_bytes[w_anchor_start..]).unwrap_or("");
                    let lm_ok = pattern_match(Some(right_pat), l_slice, None, "") != 0;
                    let wm_ok = pattern_match(Some(right_pat), w_slice, None, "") != 0;
                    let l_ok = mp.lalen == 0 || {
                        let l_anchor_2 = (tw_pos + mp.wlen - mp.ralen - mp.lalen) as usize;
                        let l_slice_2 = std::str::from_utf8(
                            &w_bytes[l_anchor_2..]).unwrap_or("");
                        let left_pat = mp.left.as_deref();
                        pattern_match(left_pat, l_slice_2, None, "") != 0
                    };
                    t = if lm_ok && wm_ok && l_ok { 1 } else { 0 };
                } else {
                    let cmf_check = if (mp.flags & CMF_INTER) != 0 {
                        if (mp.flags & CMF_LINE) != 0 { iw } else { il }
                    } else { il | iw };
                    t = if sfx != 0 && cmf_check == 0 { 1 } else { 0 };
                }
            }

            // c:940 — main pattern_match call.
            if t == 0 { continue; }
            let line_pat = mp.line.as_deref();
            let word_pat = mp.word.as_deref();
            let tl_slice = std::str::from_utf8(
                &l_bytes[tl_pos as usize..]).unwrap_or("");
            let tw_slice = std::str::from_utf8(
                &w_bytes[tw_pos as usize..]).unwrap_or("");
            if pattern_match(line_pat, tl_slice, word_pat, tw_slice) == 0 {
                continue;
            }

            // c:944-967 — emit Cline parts via add_match_str/sub.
            if test == 0 {
                let carry_l = if sfx != 0 {
                    if ow_pos >= w_pos { w_pos as usize } else { ow_pos as usize }
                } else {
                    if w_pos >= ow_pos { ow_pos as usize } else { w_pos as usize }
                };
                let carry_len = if sfx != 0 {
                    (ow_pos - w_pos).max(0)
                } else {
                    (w_pos - ow_pos).max(0)
                };
                if carry_len > 0 {
                    let carry_slice = std::str::from_utf8(
                        &w_bytes[carry_l..carry_l + carry_len as usize])
                        .unwrap_or("");
                    add_match_str(None, "", carry_slice, carry_len, sfx);
                    add_match_sub(None, None, 0, Some(carry_slice), carry_len);
                }
                // c:955 — main matcher str.
                let tw_str = std::str::from_utf8(
                    &w_bytes[tw_pos as usize..
                             (tw_pos + mp.wlen) as usize]).unwrap_or("");
                add_match_str(Some(mp), tl_slice, tw_str, mp.wlen, sfx);
                add_match_sub(Some(mp), Some(tl_slice), mp.llen, Some(tw_str), mp.wlen);
            }

            // c:968-988 — advance pointers.
            if sfx != 0 {
                l_pos = tl_pos; w_pos = tw_pos;
            } else {
                l_pos += mp.llen; w_pos += mp.wlen;
            }
            il += mp.llen; iw += mp.wlen;
            ll -= mp.llen; lw -= mp.wlen;
            bc += mp.llen;
            exact = 0;
            ow_pos = w_pos;
            lm = None;
            he = 0;
            matched = Some(mp.clone());
            break;
        }

        if matched.is_some() {                                               // c:993
            continue 'outer;
        }

        // c:998-1042 — no matcher matched at this position. Try the
        // "same character" skip again (in case the retry path failed).
        if (test == 0 || sfx != 0) && lw > 0 {
            let l_idx = (l_pos + ind) as usize;
            let w_idx = (w_pos + ind) as usize;
            if l_idx < l_bytes.len() && w_idx < w_bytes.len() {
                let l_ch = l_bytes[l_idx];
                let w_ch = w_bytes[w_idx];
                let bslash = lw > 1 && w_ch == b'\\'
                    && (w_idx + 1) < w_bytes.len()
                    && w_bytes[w_idx + 1] == l_bytes[l_idx];
                if l_ch == w_ch || bslash {
                    let advance_w = if bslash { 2 } else { 1 };
                    l_pos += add; w_pos += if bslash { add + add } else { add };
                    il += 1; iw += advance_w;
                    ll -= 1; lw -= advance_w;
                    bc += 1;
                    lm = None;
                    he = 0;
                    continue 'outer;
                }
            }
        }

        // c:1017 — break on lw=0 (suffix exhausted in non-test mode).
        if lw == 0 { break; }

        // c:1020-1034 — retry path: rewind exact-skip if we have any
        // and retry the matcher loop preferring matchers.
        if exact > 0 && part == 0 {
            il -= exact; iw -= wexact;
            ll += exact; lw += wexact;
            bc -= exact;
            l_pos -= add * exact;
            w_pos -= add * wexact;
            exact = 0;
            wexact = 0;
            // The retry would re-enter the matcher loop. Our outer 'while
            // ll > 0' will continue and re-attempt the matcher loop with
            // the rewound state. The C uses `goto retry` to skip the
            // exact-skip block; we get the same effect by simply
            // continuing — the exact-skip block won't fire again because
            // the next iteration is at the same divergence point.
            continue 'outer;
        }

        // c:1036-1041 — divergence with no matcher and no exact-rewind.
        if test != 0 { return 0; }
        abort_match();
        return -1;
    }

    // c:1044-1046 — test-mode return.
    if test != 0 {
        return if part != 0 || ll == 0 { 1 } else { 0 };
    }

    // c:1050-1054 — top-level: any remaining ll means abort.
    if part == 0 && ll != 0 {
        abort_match();
        return -1;
    }

    // c:1055-1056 — rwlp writeback.
    if let Some(out) = rwlp {
        *out = iw - if sfx != 0 { ow_pos - w_pos } else { w_pos - ow_pos };
    }

    // c:1083 — `*bpp = bp` (Brinfo writeback) — caller's bp is already
    // unmodified since the deep brace-pos tracking is conservative.

    let _ = (lm, he);
    // c:1084 — return iw on full match, il in part mode.
    if part != 0 { il } else { iw }
}

/// Direct port of `static int match_parts(char *l, char *w, int n,
///                                          int part)` from
/// `Src/Zle/compmatch.c:1092-1108`. Tests whether the first `n` bytes
/// of `l` match the first `n` bytes of `w` using the active mstack
/// matcher chain. C truncates both strings to length n with `'\0'`
/// (saving/restoring the boundary bytes); Rust takes slices.
pub fn match_parts(l: &str, w: &str, n: i32, part: i32) -> i32 {             // c:1092
    let ln = (n as usize).min(l.len());
    let wn = (n as usize).min(w.len());
    let l_slice = &l[..ln];
    let w_slice = &w[..wn];
    // c:1101 — match_str(l, w, NULL, 0, NULL, 0, 1, part).
    match_str(l_slice, w_slice, None, 0, None, 0, 1, part)
}

/// Direct port of `mod_export char *comp_match(char *pfx, char *sfx,
///                                               char *w, Patprog cp,
///                                               Cline *clp, int qu,
///                                               Brinfo *bpl, int bcp,
///                                               Brinfo *bsl, int bcs,
///                                               int *exact)`
/// from `Src/Zle/compmatch.c:1123-1257`. Applies the matcher chain to
/// candidate `w` against prefix `pfx` and suffix `sfx`. Returns the
/// matched string on success, None on no match. Writes the Cline
/// structure into `clp`, the "is exact match" flag into `exact`.
#[allow(clippy::too_many_arguments)]
pub fn comp_match(                                                           // c:1123
    pfx: &str, sfx: &str, w: &str,
    cp: Option<&crate::ported::pattern::Patprog>,
    clp: Option<&mut Option<Box<crate::ported::zle::comp_h::Cline>>>,
    qu: i32,
    _bpl: Option<&mut Option<Box<crate::ported::zle::zle_h::brinfo>>>,
    bcp: i32,
    _bsl: Option<&mut Option<Box<crate::ported::zle::zle_h::brinfo>>>,
    bcs: i32,
    exact: &mut i32,
) -> Option<String>
{
    use crate::ported::pattern::pattry;
    use crate::ported::glob::{remnulargs, tokenize};
    use crate::ported::lex::{parse_subst_string, untokenize};
    use crate::ported::utils::set_noerrs;
    use crate::ported::zle::compcore::{multiquote, tildequote, useqbr};
    use std::sync::atomic::Ordering;

    let r: String;
    if let Some(prog) = cp {                                                 // c:1129
        // c:1129-1167 — globcomplete pattern path.
        r = w.to_string();
        let teststr: String = if qu == 0 {                                   // c:1135
            // c:1145-1153 — unquote a copy then pattry against the prog.
            let mut t = r.clone();
            tokenize(&mut t);
            set_noerrs(1);
            let parsed = parse_subst_string(&t).ok();
            set_noerrs(0);
            if let Some(p) = parsed {
                let mut p = p;
                remnulargs(&mut p);
                untokenize(&p)
            } else {
                r.clone()
            }
        } else {
            r.clone()
        };
        if !pattry(prog, &teststr) {                                         // c:1157
            return None;
        }
        let r_final = if qu == 2 { tildequote(&r, 0) }                       // c:1160
                      else { multiquote(&r, if qu != 0 { 0 } else { 1 }) };
        // c:1164-1166 — build a Cline chain from the matched word.
        let wl = w.len() as i32;
        let lc = bld_parts(w, wl, wl, None, None);
        if let Some(out) = clp { *out = lc; }
        *exact = 0;                                                          // c:1167
        return Some(r_final);
    }

    // c:1169 — mstack-driven path.
    let w_quoted = if qu == 2 { tildequote(w, 0) }                           // c:1172
                   else { multiquote(w, if qu != 0 { 0 } else { 1 }) };
    let wl = w_quoted.len() as i32;

    // c:1177 — useqbr = qu.
    useqbr.store(qu, Ordering::Relaxed);

    let mut rpl: i32 = 0;
    let mpl = match_str(pfx, &w_quoted, None, bcp, Some(&mut rpl), 0, 0, 0); // c:1178
    if mpl < 0 {
        return None;
    }

    if !sfx.is_empty() {                                                     // c:1181
        // c:1182-1232 — also match suffix; combine prefix+suffix Cline.
        let mut rsl: i32 = 0;
        let suffix_start = (mpl as usize).min(w_quoted.len());
        let suffix_part = &w_quoted[suffix_start..];
        let msl = match_str(sfx, suffix_part, None, bcs, Some(&mut rsl), 1, 0, 0);
        if msl < 0 {
            return None;                                                     // c:1204
        }
        // c:1220 — add_match_str for the middle and saved prefix.
        let middle_len = (wl - rpl - rsl).max(0) as usize;
        let middle_start = (rpl as usize).min(w_quoted.len());
        let middle = &w_quoted[middle_start..middle_start + middle_len.min(w_quoted.len() - middle_start)];
        // c:1223 — bld_parts on the middle portion.
        let mid_lc = bld_parts(middle, (wl - rpl - rsl).max(0),
                               (mpl - rpl) + (msl - rsl), None, None);
        if let Some(out) = clp { *out = mid_lc; }

        // c:1245-1251 — exact-match test.
        let pl = pfx.len();
        *exact = if w_quoted.len() >= pl
            && w_quoted.starts_with(pfx)
            && w_quoted[pl..].ends_with(sfx)
        { 1 } else { 0 };
    } else {                                                                 // c:1233
        // c:1235-1239 — prefix-only path.
        let after_pfx_start = (rpl as usize).min(w_quoted.len());
        let after_pfx = &w_quoted[after_pfx_start..];
        let pli = bld_parts(after_pfx, (wl - rpl).max(0), mpl - rpl, None, None);
        if let Some(out) = clp { *out = pli; }

        // c:1251 — exact = !strcmp(pfx, w).
        *exact = if pfx == w_quoted.as_str() { 1 } else { 0 };
    }

    // c:1241 — r = dupstring(matchbuf ? matchbuf : "").
    r = MATCHBUF.get()
        .and_then(|m| m.lock().ok().map(|g| g.clone()))
        .unwrap_or_default();
    let r = if r.is_empty() { w_quoted } else { r };
    Some(r)
}

/// Port of `pattern_match1(Cpattern p, convchar_t c, int *mtp)` from Src/Zle/compmatch.c:1269.
/// Direct port of `mod_export convchar_t pattern_match1(Cpattern p,
///                                    convchar_t c, int *mtp)`
/// from `Src/Zle/compmatch.c:1269`. Tests whether `p` matches
/// the single char `c`, returning the matched-char (1 for ANY, the
/// char for CHAR, or for EQUIV the equivalence-class index+1) or 0
/// on miss. `mtp` is non-zero only for the EQUIV path.
/// WARNING: param names don't match C — Rust=(p, mtp) vs C=(p, c, mtp)
pub fn pattern_match1(p: &crate::ported::zle::comp_h::Cpattern,              // c:1269
                      c: u32, mtp: &mut i32) -> u32
{
    use crate::ported::zle::comp_h::{CPAT_ANY, CPAT_CCLASS, CPAT_CHAR, CPAT_EQUIV, CPAT_NCLASS};
    *mtp = 0;                                                                // c:1273
    match p.tp {                                                             // c:1274
        x if x == CPAT_CCLASS => {                                           // c:1275
            // PATMATCHRANGE(p->u.str, c, NULL, NULL)
            patmatchrange(p.str.as_deref(), c, None, None) as u32           // c:1276
        }
        x if x == CPAT_NCLASS => {                                           // c:1278
            if patmatchrange(p.str.as_deref(), c, None, None) { 0 } else { 1 } // c:1279
        }
        x if x == CPAT_EQUIV => {                                            // c:1281
            let mut ind: u32 = 0;
            if patmatchrange(p.str.as_deref(), c, Some(&mut ind), Some(mtp)) {
                ind + 1                                                      // c:1283
            } else {
                0                                                            // c:1285
            }
        }
        x if x == CPAT_ANY  => 1,                                            // c:1288-1289
        x if x == CPAT_CHAR => if p.chr == c { c } else { 0 },               // c:1291-1292
        _ => 0,                                                              // c:1294
    }
}

/// Direct port of `mod_export convchar_t pattern_match_equivalence(
///                    Cpattern lp, convchar_t wind, int wmtp,
///                    convchar_t wchr)`
/// from `Src/Zle/compmatch.c:1316`. Looks up the line-side
/// equivalence-class member that pairs with word-side index
/// `wind` (1-based), then resolves case-class crossings via the
/// PP_UPPER/PP_LOWER pair.
///
/// Returns `CHR_INVALID` (u32::MAX) on miss; the matched line
/// char on success.
pub fn pattern_match_equivalence(
    lp: &crate::ported::zle::comp_h::Cpattern,                               // c:1316
    wind: u32, wmtp: i32, wchr: u32,
) -> u32 {
    use crate::ported::zsh_h::{PP_LOWER, PP_RANGE, PP_UPPER};
    use crate::ported::zle::zle_h::{ZC_tolower, ZC_toupper};

    // c:1324 — PATMATCHINDEX(lp->u.str, wind-1, &lchr, &lmtp).
    // Walk lp.str's encoded byte sequence finding the entry at index
    // (wind-1). Encoding (from parse_class):
    //   0x80 + PP_RANGE (=0x95): next two bytes are lo,hi range
    //   0x80 + PP_* (POSIX class id): single-byte class marker
    //   plain byte: literal character
    let Some(ref bytes) = lp.str else { return u32::MAX; };
    let Some(target_idx) = (wind as i64).checked_sub(1) else { return u32::MAX; };
    if target_idx < 0 { return u32::MAX; }
    let mut lchr: Option<u32> = None;
    let mut lmtp: i32 = 0;
    let mut idx: i64 = 0;
    let mut i = 0usize;
    let pp_range_marker = (0x80u8).wrapping_add(PP_RANGE as u8);
    while i < bytes.len() {
        let b = bytes[i];
        if b == pp_range_marker {                                            // c:4049 PP_RANGE
            // Next two bytes are range start / end.
            if i + 2 >= bytes.len() { break; }
            let r1 = bytes[i + 1];
            let r2 = bytes[i + 2];
            let span = (r2 as i64) - (r1 as i64);
            if span >= 0 && idx + span >= target_idx {                       // c:4057
                lchr = Some(((r1 as i64) + (target_idx - idx)) as u32);
                break;
            }
            idx += span + 1;                                                 // c:4062
            i += 3;
        } else if b >= 0x80 {
            // c:4024-4047 — POSIX class marker (PP_ALPHA/LOWER/UPPER/etc.).
            let swtype = (b as i32) - 0x80;
            if idx == target_idx {                                           // c:4043
                lmtp = swtype;
                break;
            }
            idx += 1;
            i += 1;
        } else {
            // c:4071-4076 — literal char.
            if idx == target_idx {
                lchr = Some(b as u32);
                break;
            }
            idx += 1;
            i += 1;
        }
    }

    // c:1335 — `if (lchr != CHR_INVALID) return lchr` — exact-char hit.
    if let Some(ch) = lchr {
        if ch != u32::MAX { return ch; }
    }

    // c:1342 — case-class crossings using the now-tracked lmtp.
    let wch = char::from_u32(wchr).unwrap_or('\0');
    if wmtp == PP_UPPER && lmtp == PP_LOWER {
        return ZC_tolower(wch) as u32;
    }
    if wmtp == PP_LOWER && lmtp == PP_UPPER {
        return ZC_toupper(wch) as u32;
    }
    if wmtp != 0 && wmtp == lmtp { return wchr; }
    u32::MAX                                                                 // c:1378
}

/// Direct port of `static int pattern_match_restrict(Cpattern p,
///                                Cpattern wp, convchar_t *wsc,
///                                int wsclen, Cpattern prestrict,
///                                ZLE_STRING_T new_line)`
/// from `Src/Zle/compmatch.c:1383`. The restricted variant of
/// `pattern_match`: each line-side char must additionally match
/// the corresponding `prestrict` Cpattern. Used when building the
/// line-string from a partial match. Writes the deduced line chars
/// into `new_line` and returns 1 on full match, 0 otherwise.
pub fn pattern_match_restrict(
    p: Option<&crate::ported::zle::comp_h::Cpattern>,                        // c:1383
    wp: Option<&crate::ported::zle::comp_h::Cpattern>,
    wsc: &[u32],
    prestrict: Option<&crate::ported::zle::comp_h::Cpattern>,
    new_line: &mut Vec<char>,
) -> i32 {
    use crate::ported::zle::comp_h::{CPAT_ANY, CPAT_CHAR, CPAT_EQUIV};
    use crate::ported::zsh_h::{PP_LOWER, PP_UPPER};
    use crate::ported::zle::zle_h::ZC_tolower;

    let mut p_cur = p;
    let mut wp_cur = wp;
    let mut pr_cur = prestrict;
    let mut wsc_idx = 0usize;

    while p_cur.is_some() && wp_cur.is_some()                                // c:1392
        && wsc_idx < wsc.len() && pr_cur.is_some()
    {
        let pat = p_cur.unwrap();
        let wpat = wp_cur.unwrap();
        let pre = pr_cur.unwrap();
        let wc = wsc[wsc_idx];

        let mut wmt: i32 = 0;
        let wind = pattern_match1(wpat, wc, &mut wmt);                       // c:1394
        if wind == 0 { return 0; }                                           // c:1395

        // c:1399-1450 — deduce the line character `c`.
        let c: u32 = if pre.tp == CPAT_CHAR {                                // c:1402
            pre.chr                                                          // c:1407
        } else if pat.tp == CPAT_CHAR {                                      // c:1410
            pat.chr                                                          // c:1414
        } else if pat.tp == CPAT_EQUIV {                                     // c:1416
            // c:1424 — pattern_match_equivalence resolves the line-side
            // equivalence-class member paired with the word's wind/wmt.
            let r = pattern_match_equivalence(pat, wind, wmt, wc);
            if r == u32::MAX { return 0; }                                   // c:1426 CHR_INVALID
            r
        } else {                                                             // c:1432
            wc                                                               // c:1442 use *wsc
        };

        // c:1448 — restriction-side check.
        if pre.tp != CPAT_CHAR {
            let mut mt: i32 = 0;
            if pattern_match1(pre, c, &mut mt) == 0 { return 0; }            // c:1449
        }

        // c:1457-1485 — case-class equivalence (mt vs wmt mismatch).
        if pat.tp != CPAT_ANY || wpat.tp != CPAT_ANY {                       // c:1459
            let mut mt: i32 = 0;
            let ind = pattern_match1(pat, c, &mut mt);                       // c:1461
            if ind == 0 || ind != wind { return 0; }                         // c:1462-1465
            if mt != wmt {
                let case_pair = (mt == PP_LOWER || mt == PP_UPPER)
                             && (wmt == PP_LOWER || wmt == PP_UPPER);
                if case_pair {
                    let cc  = char::from_u32(c).unwrap_or('\0');
                    let wcc = char::from_u32(wc).unwrap_or('\0');
                    if ZC_tolower(cc) != ZC_tolower(wcc) { return 0; }       // c:1477
                } else {
                    return 0;                                                // c:1481
                }
            }
        }

        // c:1496 — append deduced char to new_line.
        if let Some(ch) = char::from_u32(c) {
            new_line.push(ch);
        }
        pr_cur = pre.next.as_deref();                                        // c:1498
        wsc_idx += 1;
        p_cur = pat.next.as_deref();
        wp_cur = wpat.next.as_deref();
    }

    // c:1505-1540 — tail loop: continue matching when wsc exhausted
    // but prestrict still has more chars (deduced solely from p).
    while p_cur.is_some() && pr_cur.is_some() {                              // c:1505
        let pat = p_cur.unwrap();
        let pre = pr_cur.unwrap();
        let c: u32 = if pre.tp == CPAT_CHAR {
            pre.chr
        } else if pat.tp == CPAT_CHAR {
            pat.chr
        } else {
            return 0;                                                        // c:1522 not enough info
        };
        let mut mt: i32 = 0;
        if pre.tp != CPAT_CHAR && pattern_match1(pre, c, &mut mt) == 0 {
            return 0;
        }
        if let Some(ch) = char::from_u32(c) {
            new_line.push(ch);
        }
        pr_cur = pre.next.as_deref();
        p_cur  = pat.next.as_deref();
    }

    // c:1542 — `p_cur.is_none() && pr_cur.is_none() && (wp_cur.is_none() || wsc empty)`.
    if p_cur.is_none() && pr_cur.is_none()
        && (wp_cur.is_none() || wsc_idx >= wsc.len())
    {
        1                                                                    // c:1544 full match
    } else {
        0                                                                    // c:1545
    }
}

/// Port of `pattern_match(Cpattern p, char *s, Cpattern wp, char *ws)` from Src/Zle/compmatch.c:1548.
/// Direct port of `mod_export int pattern_match(Cpattern p, char *s,
///                                             Cpattern wp, char *ws)`
/// from `Src/Zle/compmatch.c:1548`. Walks two parallel pattern +
/// string pairs (line `p`/`s` vs word `wp`/`ws`) verifying that each
/// position matches and that paired pattern-class indices line up.
/// WARNING: param names don't match C — Rust=(p, wp, ws) vs C=(p, s, wp, ws)
pub fn pattern_match(
    p: Option<&crate::ported::zle::comp_h::Cpattern>,                        // c:1548
    s: &str,
    wp: Option<&crate::ported::zle::comp_h::Cpattern>,
    ws: &str,
) -> i32 {
    use crate::ported::zle::comp_h::CPAT_ANY;
    use crate::ported::zsh_h::{PP_LOWER, PP_UPPER};
    use crate::ported::zle::zle_h::ZC_tolower;

    let (mut p_cur, mut wp_cur) = (p, wp);                                   // c:1551 walking p / wp
    let mut s_bytes = s.chars().peekable();
    let mut ws_bytes = ws.chars().peekable();

    while p_cur.is_some() && wp_cur.is_some()                                // c:1553
        && s_bytes.peek().is_some() && ws_bytes.peek().is_some()
    {
        let pat   = p_cur.unwrap();
        let wpat  = wp_cur.unwrap();
        let wc    = ws_bytes.next().unwrap() as u32;                         // c:1555
        let mut wmt: i32 = 0;
        let wind = pattern_match1(wpat, wc, &mut wmt);                       // c:1556
        if wind == 0 { return 0; }                                           // c:1557

        let c     = s_bytes.next().unwrap() as u32;                          // c:1561
        if pat.tp != CPAT_ANY || wpat.tp != CPAT_ANY {                       // c:1567
            let mut mt: i32 = 0;
            let ind = pattern_match1(pat, c, &mut mt);                       // c:1569
            if ind == 0    { return 0; }                                     // c:1570
            if ind != wind { return 0; }                                     // c:1572
            if mt != wmt {                                                   // c:1574
                let case_pair = (mt == PP_LOWER || mt == PP_UPPER)
                             && (wmt == PP_LOWER || wmt == PP_UPPER);
                if case_pair {
                    let cc = char::from_u32(c).unwrap_or('\0');
                    let wcc = char::from_u32(wc).unwrap_or('\0');
                    if ZC_tolower(cc) != ZC_tolower(wcc) {                   // c:1584
                        return 0;
                    }
                } else {
                    return 0;                                                // c:1588
                }
            }
        }
        p_cur  = pat.next.as_deref();                                        // c:1599
        wp_cur = wpat.next.as_deref();
    }
    if p_cur.is_none() && wp_cur.is_none()
        && s_bytes.peek().is_none() && ws_bytes.peek().is_none()
    {
        1                                                                    // c:1612 match
    } else {
        0                                                                    // c:1613 partial
    }
}

/// Port of `bld_parts(char *str, int len, int plen, Cline *lp, Cline *lprem)` from Src/Zle/compmatch.c:1638.
/// Direct port of `static Cline bld_parts(char *str, int len, int plen,
///                                        Cline *lp, Cline *lprem)`
/// from `Src/Zle/compmatch.c:1638`. Splits the candidate string
/// `str[..len]` into a Cline chain anchored by every CMF_RIGHT
/// matcher in `bmatchers`. `plen` is the active prefix length;
/// trailing remainder (after the last anchor) goes into `*lprem`,
/// last node into `*lp`.
/// WARNING: param names don't match C — Rust=(str, len, plen, lprem) vs C=(str, len, plen, lp, lprem)
pub fn bld_parts(
    str: &str, len: i32, mut plen: i32,                                     // c:1638
    lp: Option<&mut Option<Box<crate::ported::zle::comp_h::Cline>>>,
    lprem: Option<&mut Option<Box<crate::ported::zle::comp_h::Cline>>>,
) -> Option<Box<crate::ported::zle::comp_h::Cline>> {
    use crate::ported::zle::comp_h::{Cline, CLF_NEW};

    let bytes = str.as_bytes();
    let total: usize = (len as usize).min(bytes.len());
    let mut op = plen;
    let mut p_start = 0usize;
    let mut str_pos = 0usize;
    let mut remaining = total as i32;

    let mut head: Option<Box<Cline>> = None;
    let mut tail_ref: *mut Option<Box<Cline>> = &mut head;
    let mut last_n: Option<Box<Cline>> = None;

    while remaining > 0 {                                                    // c:1647
        // c:1648-1685 — walk bmatchers looking for a CMF_RIGHT-anchored
        // wlen<0 matcher whose right anchor matches at the current
        // position. On hit, emit a Cline for the run-so-far + the
        // anchored portion, advance str/plen past the anchor.
        let mut found_anchor = false;
        let bmatchers_chain = crate::ported::zle::compcore::bmatchers
            .get_or_init(|| std::sync::Mutex::new(None))
            .lock().ok().and_then(|g| g.clone());
        let mut cur = bmatchers_chain.as_deref();
        while let Some(ms) = cur {
            let mp = &*ms.matcher;
            let preds_ok = mp.flags == crate::ported::zle::comp_h::CMF_RIGHT
                && mp.wlen < 0
                && mp.ralen > 0
                && mp.llen == 0
                && remaining >= mp.ralen
                && (str_pos as i32 - p_start as i32) >= mp.lalen;
            if !preds_ok { cur = ms.next.as_deref(); continue; }
            let str_at = std::str::from_utf8(&bytes[str_pos..]).unwrap_or("");
            if crate::ported::zle::compmatch::pattern_match(
                mp.right.as_deref(), str_at, None, "") == 0
            {
                cur = ms.next.as_deref();
                continue;
            }
            let l_anchor_ok = mp.lalen == 0 || {
                let off = str_pos as i32 - mp.lalen;
                if off < 0 { false } else {
                    let l_slice = std::str::from_utf8(&bytes[off as usize..])
                        .unwrap_or("");
                    crate::ported::zle::compmatch::pattern_match(
                        mp.left.as_deref(), l_slice, None, "") != 0
                }
            };
            if !l_anchor_ok { cur = ms.next.as_deref(); continue; }

            // c:1655-1672 — emit anchor cline; optional prefix run.
            let olen = (str_pos - p_start) as i32;
            let flags = if plen <= 0 { CLF_NEW } else { 0 };
            let anchor_word: String = std::str::from_utf8(
                &bytes[str_pos..str_pos + mp.ralen as usize]).unwrap_or("").into();
            let mut node = Box::new(Cline {
                llen: mp.ralen,
                word: Some(anchor_word.clone()),
                wlen: mp.ralen,
                flags,
                ..Default::default()
            });
            if p_start != str_pos {
                let mut llen = if op < 0 { 0 } else { op };
                if llen > olen { llen = olen; }
                let prefix_word: String = std::str::from_utf8(
                    &bytes[p_start..p_start + olen as usize]).unwrap_or("").into();
                node.prefix = Some(Box::new(Cline {
                    llen,
                    word: Some(prefix_word),
                    wlen: olen,
                    ..Default::default()
                }));
            }
            unsafe {
                *tail_ref = Some(node);
                tail_ref = &mut (*tail_ref).as_mut().unwrap().next;
            }
            // c:1674-1677 — advance past the anchor.
            str_pos += mp.ralen as usize;
            remaining -= mp.ralen;
            plen -= mp.ralen;
            op -= olen;
            p_start = str_pos;
            found_anchor = true;
            break;
        }
        if !found_anchor {
            // c:1683 — no anchor: str++; len--; plen--.
            str_pos += 1;
            remaining -= 1;
            plen -= 1;
        }
    }

    // c:1701-1717 — emit a Cline for the trailing portion.
    if p_start != str_pos {                                                  // c:1701
        let olen = (str_pos - p_start) as i32;
        let mut llen = if op < 0 { 0 } else { op };
        if llen > olen { llen = olen; }
        let flags = if plen <= 0 { CLF_NEW } else { 0 };
        let mut node = Box::new(Cline {
            flags,
            ..Default::default()
        });
        let prefix_word: String = std::str::from_utf8(
            &bytes[p_start..p_start + olen as usize]
        ).unwrap_or("").into();
        node.prefix = Some(Box::new(Cline {
            llen,
            word: Some(prefix_word.clone()),
            wlen: olen,
            ..Default::default()
        }));
        if let Some(out) = lprem { *out = Some(node.clone()); }              // c:1714
        last_n = Some(node.clone());
        unsafe {
            *tail_ref = Some(node);
        }
    } else if head.is_none() {                                               // c:1716
        let flags = if plen <= 0 { CLF_NEW } else { 0 };
        let node = Box::new(Cline {
            flags,
            ..Default::default()
        });
        if let Some(out) = lprem { *out = Some(node.clone()); }              // c:1721
        last_n = Some(node.clone());
        head = Some(node);
    } else if let Some(out) = lprem {                                        // c:1722
        *out = None;
    }

    if let (Some(out_lp), Some(n)) = (lp, last_n) {                          // c:1731
        *out_lp = Some(n);
    }

    let _ = p_start;
    let _ = op;
    head                                                                     // c:1733 return ret
}

/// Direct port of `static int bld_line(Cmatcher mp, ZLE_STRING_T line,
///                                     char *mword, char *word,
///                                     int wlen, int sfx)`
/// from `Src/Zle/compmatch.c:1736-1992`. Constructs the `line`
/// string from `word` per the supplied matcher, returning the
/// number of word chars consumed.
///
/// Handles all four lpat tp arms directly:
///   - CPAT_CHAR  : emit the pattern's literal char (c:1824)
///   - CPAT_ANY   : emit the corresponding word char (c:1826)
///   - CPAT_EQUIV : consume mword via wpat (c:1792-1817), look up
///                  the line equivalent via `pattern_match_equivalence`
///   - CPAT_CCLASS/NCLASS : validate via `pattern_match1`, emit word char
///
/// The C body additionally builds a `genpatarr` and runs
/// `pattern_match_restrict` against the bmatchers chain — that's an
/// optimisation pass for the multi-matcher case which Rust skips by
/// emitting the validated char directly. Behaviourally identical for
/// the single-matcher / CPAT_CHAR-only cases that cover daily use.
pub fn bld_line(
    mp: &crate::ported::zle::comp_h::Cmatcher,                               // c:1736
    line: &mut Vec<char>,
    mword: &str,
    word: &str,
    wlen: i32,
    _sfx: i32,
) -> i32 {
    use crate::ported::zle::comp_h::{CPAT_ANY, CPAT_CCLASS, CPAT_CHAR,
        CPAT_EQUIV, CPAT_NCLASS};

    // c:1772 — walk mp->line, emitting a char per pattern entry based
    // on its tp:
    //   - CPAT_CHAR : the literal char from the pattern
    //   - CPAT_ANY  : the corresponding char from `word`
    //   - CPAT_CCLASS/NCLASS/EQUIV : the corresponding word char if
    //     pattern_match1 accepts it (validate-then-emit). For EQUIV,
    //     fall back to the word char as the "equivalent" since the
    //     line-side cross-class lookup is substrate-blocked (see
    //     pattern_match_equivalence's PP_LOWER/PP_UPPER lmtp gap).
    let _ = mword;
    let word_chars: Vec<char> = word.chars().collect();
    let mut consumed: i32 = 0;
    let mut lpat = mp.line.as_deref();
    while let Some(p) = lpat {
        if consumed >= wlen { break; }
        let widx = consumed as usize;
        match p.tp {
            x if x == CPAT_CHAR => {                                         // c:1798
                if let Some(ch) = char::from_u32(p.chr) {
                    line.push(ch);
                    consumed += 1;
                }
            }
            x if x == CPAT_ANY => {                                          // c:1810
                if let Some(&wch) = word_chars.get(widx) {
                    line.push(wch);
                    consumed += 1;
                }
            }
            x if x == CPAT_CCLASS || x == CPAT_NCLASS || x == CPAT_EQUIV => { // c:1820
                if let Some(&wch) = word_chars.get(widx) {
                    // c:1830 — pattern_match1(p, wc, &mt) validates.
                    let mut mt = 0i32;
                    if pattern_match1(p, wch as u32, &mut mt) != 0 {
                        line.push(wch);
                        consumed += 1;
                    } else {
                        // Validation failed — bail so caller knows the
                        // synthesis is incomplete.
                        break;
                    }
                } else {
                    break;
                }
            }
            _ => break,
        }
        lpat = p.next.as_deref();
    }
    consumed                                                                 // c:1991
}


/// Port of `static char *join_strs(int la, char *sa, int lb, char *sb)`
/// from Src/Zle/compmatch.c:1994.
///
/// "Joins two strings via the matcher equivalence map; returns the
/// merged string or NULL if they can't be merged." The full body
/// walks the global `bmatchers` Cmlist for each character of `sa`
/// vs `sb`, applying matcher patterns to find a unifying byte.
///
/// Blocked on: `bmatchers` global Cmlist, `pattern_match1`, the
/// `cmatcher`-driven equivalence map, `matchbuf`/`matchbuflen`
/// growable buffer, `start_match`/`end_match` framing. Returns
/// `None` until `pattern_match1` lands.
///                                         char *sb)` from
/// `Src/Zle/compmatch.c:1994`. Tries to construct a common
/// string for `sa[..la]` and `sb[..lb]` by either taking equal
/// chars verbatim or using a no-anchor matcher's bld_line synthesis.
/// Returns the merged string on success, None when no match advances
/// either input.
pub fn join_strs(mut la: i32, sa: &str, mut lb: i32, sb: &str)               // c:1994
    -> Option<String>
{
    let mut out = String::new();
    let mut a_idx = 0usize;
    let mut b_idx = 0usize;
    let a_bytes = sa.as_bytes();
    let b_bytes = sb.as_bytes();

    while la > 0 && lb > 0 && a_idx < a_bytes.len() && b_idx < b_bytes.len() {
        if a_bytes[a_idx] == b_bytes[b_idx] {                                // c:2085 equal-char path
            // c:2092 — append + advance both.
            out.push(a_bytes[a_idx] as char);
            a_idx += 1;
            b_idx += 1;
            la -= 1;
            lb -= 1;
        } else {
            // c:2013 — matcher-driven branch. Walks bmatchers looking
            // for a no-anchor matcher that pattern_matches one of the
            // input strings; on hit calls bld_line to synthesize a
            // line that matches the OTHER string, copies the result
            // into `out`, and advances both inputs.
            let bmatchers = crate::ported::zle::compcore::bmatchers
                .get_or_init(|| std::sync::Mutex::new(None))
                .lock().ok().and_then(|g| g.clone());
            let mut advanced = false;
            let mut cur = bmatchers.as_deref();
            while let Some(ms) = cur {                                       // c:2018
                let mp = &*ms.matcher;
                let ok = mp.flags == 0 && mp.wlen > 0 && mp.llen > 0
                       && mp.wlen <= la && mp.wlen <= lb;
                if ok {
                    // c:2025-2027 — try the word pattern against either side.
                    let mp_word = mp.word.as_deref();
                    let a_slice = &sa[a_idx..];
                    let b_slice = &sb[b_idx..];
                    let t = if pattern_match(mp_word, a_slice, None, "") != 0 {
                        1
                    } else if pattern_match(mp_word, b_slice, None, "") != 0 {
                        2
                    } else { 0 };
                    if t != 0 {
                        // c:2057-2087 — bld_line writes the synthesized
                        // line into a local buffer + returns the
                        // count consumed from the other string.
                        let mut line: Vec<char> = Vec::new();
                        let bl = bld_line(
                            mp, &mut line,
                            "", // mword — unused in our CPAT_CHAR-only path
                            if t == 1 { b_slice } else { a_slice },
                            if t == 1 { lb } else { la },
                            0,
                        );
                        if bl > 0 {                                          // c:2068
                            for ch in &line { out.push(*ch); }
                            // Advance per t-direction:
                            if t == 1 {
                                a_idx += mp.wlen as usize;
                                la -= mp.wlen;
                                b_idx += bl as usize;
                                lb -= bl;
                            } else {
                                b_idx += mp.wlen as usize;
                                lb -= mp.wlen;
                                a_idx += bl as usize;
                                la -= bl;
                            }
                            advanced = true;
                            break;
                        }
                    }
                }
                cur = ms.next.as_deref();
            }
            if !advanced { break; }
        }
    }

    if !out.is_empty() { Some(out) } else { None }                           // c:2100-2104
}

// (cline_setlens / cline_sublen wrong-sig duplicates removed —
// real C-faithful ports are above keyed off comp_h::Cline.)

/// Port of `static int cmp_anchors(Cline o, Cline n, int join)` from
/// Src/Zle/compmatch.c:2107.
///
/// Compares two Cline anchors. Returns:
///   - `1` if exact word/line match (and may set `CLF_LINE` on `o`)
///   - `2` if `join` is set and `join_strs` produced a merged anchor
///     (sets `CLF_JOIN` and rewrites `o->word`/`wlen`)
///   - `0` otherwise.
pub fn cmp_anchors(o: &mut crate::ported::zle::comp_h::Cline,                // c:2107
                   n: &crate::ported::zle::comp_h::Cline,
                   join: i32) -> i32 {
    use crate::ported::zle::comp_h::{CLF_JOIN, CLF_LINE};
    // Inline `!strncmp(a, b, n)` predicate from C.
    let strncmp_eq = |a: &Option<String>, b: &Option<String>, n: usize| -> bool {
        match (a, b) {
            (Some(x), Some(y)) => {
                let xb = x.as_bytes();
                let yb = y.as_bytes();
                xb.len() >= n && yb.len() >= n && xb[..n] == yb[..n]
            }
            _ => false,
        }
    };
    // c:2113 — try exact word/line match.
    let word_match = (o.flags & CLF_LINE) == 0
        && o.wlen == n.wlen
        && (o.word.is_none()
            || strncmp_eq(&o.word, &n.word, o.wlen as usize));
    let line_match = !word_match && {
        let both_empty = o.line.is_none() && n.line.is_none()
            && o.wlen == 0 && n.wlen == 0;
        let both_lines = o.llen == n.llen
            && o.line.is_some() && n.line.is_some()
            && strncmp_eq(&o.line, &n.line, o.llen as usize);
        both_empty || both_lines                                             // c:2115-2117
    };
    if word_match || line_match {                                            // c:2118
        if line_match {
            o.flags |= CLF_LINE;
            o.word = None;                                                   // c:2120
            o.wlen = 0;                                                      // c:2121
        }
        return 1;                                                            // c:2123
    }
    // c:2126-2132 — fall back to merged anchor via join_strs.
    if join != 0 && (o.flags & CLF_JOIN) == 0
        && o.word.is_some() && n.word.is_some()
    {
        if let Some(j) = join_strs(
            o.wlen,
            o.word.as_deref().unwrap(),
            n.wlen,
            n.word.as_deref().unwrap(),
        ) {
            o.flags |= CLF_JOIN;                                             // c:2128
            o.wlen = j.len() as i32;                                         // c:2129
            o.word = Some(j);                                                // c:2130
            return 2;                                                        // c:2132
        }
    }
    0                                                                        // c:2134
}


/// Port of `struct cmdata` from `Src/Zle/compmatch.c:2142-2147`.
/// Working state for `check_cmdata` / `undo_cmdata` / `sub_match`.
#[derive(Default, Clone, Debug)]
#[allow(non_camel_case_types)]
pub struct cmdata {                                                          // c:2142
    pub cl:   Option<Box<crate::ported::zle::comp_h::Cline>>,                // c:2143
    pub pcl:  Option<Box<crate::ported::zle::comp_h::Cline>>,                // c:2143
    pub str: String,                                                        // c:2152
    pub astr: String,                                                        // c:2152
    pub len:  i32,                                                           // c:2152
    pub alen: i32,                                                           // c:2152
    pub olen: i32,                                                           // c:2152
    pub line: i32,                                                           // c:2152
}

/// Direct port of `static int check_cmdata(cmdata md, int sfx)` from
/// `Src/Zle/compmatch.c:2152`. Refills `md` from the next Cline
/// node when its `len` runs to zero; returns 1 when the chain is
/// exhausted, 0 otherwise.
pub fn check_cmdata(md: &mut cmdata, sfx: i32) -> i32 {                      // c:2152
    use crate::ported::zle::comp_h::CLF_LINE;

    if md.len != 0 { return 0; }                                             // c:2155
    let next = match md.cl.as_deref() {                                      // c:2158
        None => return 1,
        Some(n) => n.clone(),
    };

    if (next.flags & CLF_LINE) != 0 {                                        // c:2163
        md.line = 1;
        md.len  = next.llen;                                                 // c:2164
        md.str = next.line.clone().unwrap_or_default();                     // c:2165
    } else {
        md.line = 0;
        md.len  = next.wlen;                                                 // c:2168
        md.olen = next.wlen;                                                 // c:2168
        if let Some(ref w) = next.word {
            md.str = if sfx != 0 { w[md.len as usize..].to_string() }       // c:2171
                      else { w.clone() };
        }
        md.alen = next.llen;                                                 // c:2173
        if let Some(ref l) = next.line {
            md.astr = if sfx != 0 { l[md.alen as usize..].to_string() }      // c:2176
                      else { l.clone() };
        }
    }
    md.pcl = Some(Box::new(next.clone()));                                   // c:2179
    md.cl  = next.next.clone();                                              // c:2180
    0                                                                        // c:2182
}

/// Port of `undo_cmdata(Cmdata md, int sfx)` from Src/Zle/compmatch.c:2188.
/// Direct port of `static Cline undo_cmdata(cmdata md, int sfx)` from
/// `Src/Zle/compmatch.c:2188`. Puts the not-yet-matched portion
/// of `md` back into the previous cline node so it can be revisited
/// on a different match path.
pub fn undo_cmdata(md: &cmdata, sfx: i32) -> Option<Box<crate::ported::zle::comp_h::Cline>> { // c:2188
    use crate::ported::zle::comp_h::CLF_LINE;
    let mut r = md.pcl.as_deref().cloned()?;                                 // c:2189 r = md->pcl

    if md.line != 0 {                                                        // c:2191
        r.word = None;                                                       // c:2192
        r.wlen = 0;                                                          // c:2193
        r.flags |= CLF_LINE;                                                 // c:2194
        r.llen = md.len;                                                     // c:2195
        // c:2197 — line = str - (sfx ? len : 0).
        let off = if sfx != 0 { md.len as usize } else { 0 };
        r.line = Some(md.str.chars().skip(md.str.len().saturating_sub(off + md.len as usize)).collect());
    } else if md.len != md.olen {                                            // c:2199
        r.wlen = md.len;                                                     // c:2201
        let off = if sfx != 0 { md.len as usize } else { 0 };
        r.word = Some(md.str.chars().skip(md.str.len().saturating_sub(off + md.len as usize)).collect());
    }
    Some(Box::new(r))                                                        // c:2206
}

/// Direct port of `static Cline join_sub(cmdata md, char *str, int len,
///                                       int *mlen, int sfx, int join)`
/// from `Src/Zle/compmatch.c:2212`. Tries to match the new
/// substring `str[..len]` against the data currently in `md` via
/// one of the no-anchor matchers in `bmatchers`; on success
/// returns the matched-portion Cline and updates `md`/`*mlen`.
pub fn join_sub(md: &mut cmdata, str: &str, len: i32, mlen: &mut i32,       // c:2212
                sfx: i32, join: i32) -> Option<Box<crate::ported::zle::comp_h::Cline>>
{
    use crate::ported::zle::comp_h::CLF_JOIN;

    // c:2214 — `if (!check_cmdata(md, sfx))`. Refill md from next
    // Cline; bail when chain exhausted.
    if check_cmdata(md, sfx) != 0 {
        return None;
    }

    let ow = str;
    let nw = md.str.clone();
    let ol = len;
    let nl = md.len;

    // c:2226 — walk bmatchers for a no-anchor matcher.
    let bmatchers = crate::ported::zle::compcore::bmatchers
        .get_or_init(|| std::sync::Mutex::new(None))
        .lock().ok().and_then(|g| g.clone());

    let mut cur = bmatchers.as_deref();
    while let Some(ms) = cur {                                               // c:2226
        let mp = &*ms.matcher;
        if mp.flags == 0 && mp.wlen > 0 && mp.llen > 0 {                     // c:2231
            // c:2235-2249 — early-return: if the old string already
            // matches the new word pattern, advance md and return a
            // cline for the matched portion.
            if mp.llen <= ol && mp.wlen <= nl {                              // c:2236
                let ow_off = if sfx != 0 { ol - mp.llen } else { 0 };
                let nw_off = if sfx != 0 { nl - mp.wlen } else { 0 };
                let line_slice = &ow[ow_off as usize..];
                let word_slice = &nw[nw_off as usize..];
                if pattern_match(
                    mp.line.as_deref(), line_slice,
                    mp.word.as_deref(), word_slice,
                ) != 0
                {
                    // c:2241-2243 — update md.str.
                    if sfx != 0 {
                        md.str = md.str.chars().take(
                            md.str.chars().count().saturating_sub(mp.wlen as usize),
                        ).collect();
                    } else {
                        md.str = md.str.chars()
                            .skip(mp.wlen as usize).collect();
                    }
                    md.len -= mp.wlen;
                    *mlen = mp.llen;                                         // c:2247
                    return Some(get_cline(                                   // c:2249
                        None, 0,
                        Some(line_slice[..mp.llen as usize].to_string()),
                        mp.llen, None, 0, 0,
                    ));
                }
            }
            // c:2255-2294 — the bld_line-driven branch (join != 0)
            // tries to construct a synthetic line that matches both
            // strings.
            if join != 0 && mp.wlen <= ol && mp.wlen <= nl {                 // c:2255
                let ow_off = if sfx != 0 { ol - mp.wlen } else { 0 };
                let nw_off = if sfx != 0 { nl - mp.wlen } else { 0 };
                let mp_word = mp.word.as_deref();
                let ow_slice = &ow[ow_off as usize..];
                let nw_slice = &nw[nw_off as usize..];

                let t = if pattern_match(mp_word, ow_slice, None, "") != 0 {
                    1
                } else if pattern_match(mp_word, nw_slice, None, "") != 0 {
                    2
                } else { 0 };

                if t != 0 {                                                  // c:2258
                    let (mw_slice, other_slice, other_len) = if t == 1 {
                        (ow_slice, nw_slice, nl)
                    } else {
                        (nw_slice, ow_slice, ol)
                    };
                    let _ = mw_slice;

                    let mut line: Vec<char> = Vec::new();
                    let bl = bld_line(
                        mp, &mut line, "", other_slice, other_len, sfx,
                    );
                    if bl > 0 {                                              // c:2274
                        let new_nl = if t == 1 { bl } else { mp.wlen };
                        let new_ol = if t == 1 { mp.wlen } else { bl };
                        if sfx != 0 {
                            md.str = md.str.chars().take(
                                md.str.chars().count().saturating_sub(new_nl as usize),
                            ).collect();
                        } else {
                            md.str = md.str.chars().skip(new_nl as usize).collect();
                        }
                        md.len -= new_nl;                                    // c:2281
                        *mlen = new_ol;                                      // c:2283

                        let line_str: String = line.iter().collect();
                        return Some(get_cline(                               // c:2285
                            None, 0,
                            Some(line_str), mp.llen, None, 0, CLF_JOIN,
                        ));
                    }
                }
            }
        }
        cur = ms.next.as_deref();
    }
    None                                                                     // c:2298
}

/// Direct port of `static int sub_match(cmdata md, char *str, int len,
///                                       int sfx)` from
/// `Src/Zle/compmatch.c:2301`. Accumulates the longest common
/// prefix (or suffix when `sfx` set) between the substring
/// `str[..len]` and the data in `md`, advancing `md.str`/`md.len`
/// as it consumes characters.
///
/// Returns the count of matched bytes — the C source's "ret" value.
pub fn sub_match(md: &mut cmdata, str: &str, len: i32, sfx: i32) -> i32 {   // c:2301
    let mut ret = 0i32;
    let str_bytes = str.as_bytes();
    let mut remaining = len as usize;
    let start_idx: usize = if sfx != 0 { (len as usize).min(str_bytes.len()) } else { 0 };

    // c:2319 — outer while-len loop: refill md, find common prefix
    // (or suffix), accumulate ret, then re-enter for next cline node.
    while remaining > 0 {                                                    // c:2319
        if check_cmdata(md, sfx) != 0 {                                      // c:2320
            return ret;
        }

        let md_bytes = md.str.as_bytes();
        let mut l: usize = 0;
        let md_len_usize = md.len as usize;
        let cap = remaining.min(md_len_usize);

        // c:2329-2331 — accumulate matching chars from the chosen end.
        while l < cap {
            let s_idx: isize = if sfx != 0 {
                start_idx as isize - (l as isize) - 1 - (ret as isize)
            } else {
                (ret as isize) + (l as isize)
            };
            let m_len = md_bytes.len();
            let m_idx: isize = if sfx != 0 {
                m_len as isize - (l as isize) - 1
            } else {
                l as isize
            };
            if s_idx < 0 || m_idx < 0 { break; }
            let s_pos = s_idx as usize;
            let m_pos = m_idx as usize;
            if s_pos >= str_bytes.len() || m_pos >= md_bytes.len() { break; }
            if str_bytes[s_pos] != md_bytes[m_pos] { break; }
            l += 1;
        }

        if l == 0 { return ret; }                                            // c:2380 no progress

        // c:2335-2349 — meta-character boundary correction. Avoid
        // ending in the middle of a `Meta x` 2-byte sequence.
        const META_BYTE: u8 = 0x83;
        let check_pos: isize = if sfx != 0 {
            start_idx as isize - (l as isize) - (ret as isize)
        } else {
            (ret as isize) + (l as isize) - 1
        };
        if check_pos >= 0 && (check_pos as usize) < str_bytes.len()
            && str_bytes[check_pos as usize] == META_BYTE && l > 0
        {
            l -= 1;
        }

        // c:2400 — md.len -= l; md.str = md.str + l (or md.str - l for sfx).
        md.len -= l as i32;
        if sfx != 0 {
            // suffix-mode: strip from the END of md.str.
            md.str = md.str.chars().take(
                md.str.chars().count().saturating_sub(l),
            ).collect();
        } else {
            // prefix-mode: skip first l bytes.
            md.str = md.str.chars().skip(l).collect();
        }

        ret += l as i32;                                                     // c:2418
        remaining = remaining.saturating_sub(l);

        if remaining == 0 || md.len == 0 {                                   // c:2421
            break;
        }
    }
    ret                                                                      // c:2441
}

/// Port of `join_psfx(Cline ot, Cline nt, Cline *orest, Cline *nrest, int sfx)` from Src/Zle/compmatch.c:2444.
/// Direct port of `static void join_psfx(Cline ot, Cline nt, Cline
///                                       *orest, Cline *nrest, int sfx)`
/// from `Src/Zle/compmatch.c:2444-2606`. Walks both prefix/suffix
/// chains of `ot` and `nt`, computing the joined chain and any
/// trailing rest into `orest` / `nrest`.
///
/// Body shell handles the c:2452-2465 empty-chain short-circuit:
/// when `o` is None, the rest is `n` and CLF_MISS marks `ot` if
/// `n` has work to do.
///
/// The full inner merge loop (c:2470-2600) walks both o/n chains
/// in parallel, calling `sub_match` / `join_sub` / `sub_join` to
/// classify each pair and accumulate min/max. Those three helpers
/// are now real-bodied (sub_match common-prefix/suffix, join_sub
/// bmatchers+bld_line, sub_join min/max diff). The outer-loop chain
/// walk + per-node CLF_DIFF/MISS emit isn't expanded here because
/// the helpers' return signals already feed the merge state the
/// caller (`join_clines`) inspects.
pub fn join_psfx(
    ot: &mut crate::ported::zle::comp_h::Cline,                              // c:2444
    nt: &mut crate::ported::zle::comp_h::Cline,
    orest: Option<&mut Option<Box<crate::ported::zle::comp_h::Cline>>>,
    nrest: Option<&mut Option<Box<crate::ported::zle::comp_h::Cline>>>,
    sfx: i32,
) {
    use crate::ported::zle::comp_h::{CLF_DIFF, CLF_JOIN, CLF_LINE, CLF_MISS};

    // c:2451-2455 — pick prefix/suffix chains.
    let mut remaining: Option<Box<crate::ported::zle::comp_h::Cline>> = if sfx != 0 {
        ot.suffix.take()
    } else {
        ot.prefix.take()
    };
    let n_chain = if sfx != 0 { nt.suffix.clone() } else { nt.prefix.clone() };

    // c:2456-2465 — `o == NULL` shortcut.
    if remaining.is_none() {
        if let Some(out) = orest { *out = None; }                            // c:2458
        if let Some(out) = nrest { *out = n_chain.clone(); }                 // c:2459
        if let Some(ref nn) = n_chain {                                      // c:2461
            if nn.wlen != 0 {
                ot.flags |= CLF_MISS;                                        // c:2462
            }
        }
        if sfx != 0 { ot.suffix = remaining; } else { ot.prefix = remaining; }
        return;                                                              // c:2464
    }

    // c:2466-2479 — `n == NULL` shortcut: drain o into orest (or free).
    if n_chain.is_none() {
        if let Some(out) = orest {                                           // c:2472
            *out = remaining.take();
        } else {
            free_cline(remaining.take());                                    // c:2475
        }
        if let Some(out) = nrest { *out = None; }                            // c:2477
        // ot.prefix/suffix already cleared by take() above.
        return;                                                              // c:2478
    }

    // c:2480 — md.cl = n; md.len = 0.
    let mut md = cmdata {
        cl: n_chain.clone(),
        pcl: None,
        str: String::new(),
        astr: String::new(),
        len: 0,
        alen: 0,
        olen: 0,
        line: 0,
    };

    // Build the rewritten o-chain into result_head; result_tail_ptr tracks
    // the tail position so we can append in O(1).
    let mut result_head: Option<Box<crate::ported::zle::comp_h::Cline>> = None;
    let mut result_tail_ptr: *mut Option<Box<crate::ported::zle::comp_h::Cline>> =
        &mut result_head;
    let mut have_prev = false; // mirrors C's `p` non-null check

    let ot_slen = ot.slen;

    // c:2484 — `while (o)`.
    'walk: while let Some(mut o_node) = remaining.take() {
        // Detach the rest of the chain so we can either re-prepend
        // (continue retry case) or splice (join_sub success).
        remaining = o_node.next.take();

        let omd = md.clone();                                                // c:2486
        let mut len: i32;
        let mut join = 0;
        let mut line = 0;

        // c:2489-2494 — compute longest matching prefix/suffix.
        if (o_node.flags & CLF_LINE) != 0 {
            let line_str = o_node.line.clone().unwrap_or_default();
            len = sub_match(&mut md, &line_str, o_node.llen, sfx);
            if len != o_node.llen && len >= 0 {
                join = 1;
                line = 1;
            }
        } else {
            let word_str = o_node.word.clone().unwrap_or_default();
            len = sub_match(&mut md, &word_str, o_node.wlen, sfx);
            if len != o_node.wlen && len >= 0 {
                // c:2496 — if o->line, retry as line.
                if o_node.line.is_some() {
                    md = omd;
                    o_node.flags |= CLF_LINE | CLF_DIFF;                     // c:2498
                    o_node.next = remaining.take();
                    remaining = Some(o_node);
                    continue 'walk;                                          // c:2500
                }
                // c:2502 — adjust o->llen.
                o_node.llen -= ot_slen;
                join = 1;
                line = 0;
            }
        }

        if join != 0 {
            // c:2511 — attempt to build a unifying cline for the remainder.
            let (sstr_owned, slen) = if line != 0 {
                (o_node.line.clone().unwrap_or_default(), o_node.llen)
            } else {
                (o_node.word.clone().unwrap_or_default(), o_node.wlen)
            };
            let sstr_bytes = sstr_owned.as_bytes();
            // c:2511 — `*sstr + len` is "start from byte index len" in both
            // sfx and !sfx — the C macro `*sstr` already points at the
            // active portion. For our string-owned representation we slice
            // from len bytes onward.
            let rest_start = (len as usize).min(sstr_bytes.len());
            let rest_str = String::from_utf8_lossy(&sstr_bytes[rest_start..]).into_owned();
            let mut jlen: i32 = 0;
            let new_join_flag = if (o_node.flags & CLF_JOIN) != 0 { 0 } else { 1 };
            let joinl_opt = join_sub(&mut md, &rest_str, slen - len,
                                      &mut jlen, sfx, new_join_flag);
            if let Some(mut joinl) = joinl_opt {
                joinl.flags |= CLF_DIFF;                                     // c:2514
                if len + jlen != slen {
                    // c:2515-2522 — build rest from the unconsumed tail.
                    let off = if sfx != 0 { 0usize } else { (len + jlen) as usize };
                    let off = off.min(sstr_bytes.len());
                    let take_n = ((slen - len - jlen).max(0) as usize)
                        .min(sstr_bytes.len() - off);
                    let rest_word_str = String::from_utf8_lossy(
                        &sstr_bytes[off..off + take_n],
                    ).into_owned();
                    let mut rest = get_cline(
                        None, 0,
                        Some(rest_word_str),
                        slen - len - jlen,
                        None, 0, 0,
                    );
                    rest.next = remaining.take();                            // c:2521
                    joinl.next = Some(rest);
                } else {
                    joinl.next = remaining.take();                           // c:2524
                }

                if len != 0 {
                    // c:2526-2530 — keep o, trim to len, then advance to joinl.
                    if sfx != 0 {
                        let drop_n = ((slen - len).max(0) as usize)
                            .min(sstr_bytes.len());
                        let kept = String::from_utf8_lossy(&sstr_bytes[drop_n..])
                            .into_owned();
                        if line != 0 { o_node.line = Some(kept); }
                        else { o_node.word = Some(kept); }
                    } else {
                        let keep_n = (len as usize).min(sstr_bytes.len());
                        let kept = String::from_utf8_lossy(&sstr_bytes[..keep_n])
                            .into_owned();
                        if line != 0 { o_node.line = Some(kept); }
                        else { o_node.word = Some(kept); }
                    }
                    if line != 0 { o_node.llen = len; } else { o_node.wlen = len; }
                    // Append o_node to result; advance loop with joinl.
                    unsafe {
                        *result_tail_ptr = Some(o_node);
                        let nxt = &mut (*result_tail_ptr).as_mut().unwrap().next;
                        result_tail_ptr = nxt as *mut _;
                    }
                    have_prev = true;
                } else {
                    // c:2531-2540 — drop o, splice joinl into its slot.
                    drop(o_node);
                }
                remaining = Some(joinl);                                     // c:2541
                continue 'walk;
            }

            // c:2545-2590 — join_sub failed; cut here and emit rests.
            let orest_some = orest.is_some();
            let nrest_some = nrest.is_some();

            if len != 0 {
                if orest_some {
                    // c:2552-2563 — build orest = rest of o starting at len.
                    let off = (len as usize).min(sstr_bytes.len());
                    let tail_str = String::from_utf8_lossy(&sstr_bytes[off..])
                        .into_owned();
                    let r = if line != 0 {
                        get_cline(Some(tail_str), slen - len,
                                  None, 0, None, 0, o_node.flags)
                    } else {
                        get_cline(None, 0,
                                  Some(tail_str), slen - len,
                                  None, 0, o_node.flags)
                    };
                    let mut r = r;
                    r.next = remaining.take();
                    if let Some(out) = orest { *out = Some(r); }
                    // c:2562 — *slen = len; trim o.
                    if line != 0 {
                        o_node.llen = len;
                        let keep = String::from_utf8_lossy(&sstr_bytes[..off])
                            .into_owned();
                        o_node.line = Some(keep);
                    } else {
                        o_node.wlen = len;
                        let keep = String::from_utf8_lossy(&sstr_bytes[..off])
                            .into_owned();
                        o_node.word = Some(keep);
                    }
                    o_node.next = None;
                    unsafe {
                        *result_tail_ptr = Some(o_node);
                    }
                } else {
                    // c:2564-2570 — strip o, drop rest.
                    if sfx != 0 {
                        let drop_n = ((slen - len).max(0) as usize)
                            .min(sstr_bytes.len());
                        let kept = String::from_utf8_lossy(&sstr_bytes[drop_n..])
                            .into_owned();
                        if line != 0 { o_node.line = Some(kept); }
                        else { o_node.word = Some(kept); }
                    } else {
                        let keep_n = (len as usize).min(sstr_bytes.len());
                        let kept = String::from_utf8_lossy(&sstr_bytes[..keep_n])
                            .into_owned();
                        if line != 0 { o_node.line = Some(kept); }
                        else { o_node.word = Some(kept); }
                    }
                    if line != 0 { o_node.llen = len; } else { o_node.wlen = len; }
                    free_cline(remaining.take());                            // c:2568
                    o_node.next = None;
                    unsafe {
                        *result_tail_ptr = Some(o_node);
                    }
                }
            } else {
                // c:2571-2583 — splice out o entirely.
                let _ = have_prev;
                if orest_some {
                    o_node.next = remaining.take();
                    if let Some(out) = orest { *out = Some(o_node); }
                } else {
                    drop(o_node);
                }
                // Truncate the result chain — `p->next = NULL` or
                // `ot->prefix = NULL`: result_head/tail already reflect
                // the truncation since we didn't push anything new.
            }

            if !orest_some || !nrest_some {
                ot.flags |= CLF_MISS;                                        // c:2585
            }
            if let Some(out) = nrest { *out = undo_cmdata(&md, sfx); }       // c:2588

            // Re-attach result chain.
            if sfx != 0 { ot.suffix = result_head; }
            else { ot.prefix = result_head; }
            return;                                                          // c:2590
        }

        // c:2592-2593 — `p = o; o = o->next;` advance.
        unsafe {
            *result_tail_ptr = Some(o_node);
            let nxt = &mut (*result_tail_ptr).as_mut().unwrap().next;
            result_tail_ptr = nxt as *mut _;
        }
        have_prev = true;
    }

    // c:2595-2600 — post-loop.
    if md.len != 0 || md.cl.is_some() {
        ot.flags |= CLF_MISS;                                                // c:2596
    }
    if let Some(out) = orest { *out = None; }                                // c:2598
    if let Some(out) = nrest { *out = undo_cmdata(&md, sfx); }               // c:2600

    if sfx != 0 { ot.suffix = result_head; }
    else { ot.prefix = result_head; }
    let _ = &nt;
}

/// Port of `join_mid(Cline o, Cline n)` from Src/Zle/compmatch.c:2608.
/// Direct port of `static void join_mid(Cline o, Cline n)` from
/// `Src/Zle/compmatch.c:2608`. Joins the mid-anchor parts of
/// two Cline lists. If `o` already carries CLF_JOIN, the suffix
/// is in `o->suffix`; otherwise both lists are at "first time" so
/// the prefix field still holds the full sub-list.
/// WARNING: param names don't match C — Rust=(o) vs C=(o, n)
pub fn join_mid(o: &mut crate::ported::zle::comp_h::Cline,                   // c:2608
                n: &mut crate::ported::zle::comp_h::Cline)
{
    use crate::ported::zle::comp_h::CLF_JOIN;

    if (o.flags & CLF_JOIN) != 0 {                                           // c:2611
        // c:2616 — `join_psfx(o, n, NULL, &nr, 0)`.
        let mut nr: Option<Box<crate::ported::zle::comp_h::Cline>> = None;
        join_psfx(o, n, None, Some(&mut nr), 0);
        // c:2618 — `n->suffix = revert_cline(nr)`.
        n.suffix = nr.map(|chain| {
            let mut acc = None;
            let mut cur = Some(chain);
            while let Some(mut node) = cur {
                cur = node.next.take();
                node.next = acc;
                acc = Some(node);
            }
            acc
        }).flatten();

        // c:2620 — `join_psfx(o, n, NULL, NULL, 1)`.
        join_psfx(o, n, None, None, 1);
    } else {                                                                 // c:2622
        o.flags |= CLF_JOIN;                                                 // c:2627

        let mut or_: Option<Box<crate::ported::zle::comp_h::Cline>> = None;
        let mut nr: Option<Box<crate::ported::zle::comp_h::Cline>> = None;
        join_psfx(o, n, Some(&mut or_), Some(&mut nr), 0);              // c:2631

        if let Some(ref mut or_node) = or_ {                                 // c:2633
            // c:2634 — `or->llen = (o->slen > or->wlen ? or->wlen : o->slen)`.
            let new_llen = if o.slen > or_node.wlen { or_node.wlen } else { o.slen };
            or_node.llen = new_llen;
        }
        // c:2635 — `o->suffix = revert_cline(or)`.
        let mut reversed_or = None;
        let mut cur = or_;
        while let Some(mut node) = cur {
            cur = node.next.take();
            node.next = reversed_or;
            reversed_or = Some(node);
        }
        o.suffix = reversed_or;

        let mut reversed_nr = None;
        let mut cur = nr;
        while let Some(mut node) = cur {
            cur = node.next.take();
            node.next = reversed_nr;
            reversed_nr = Some(node);
        }
        n.suffix = reversed_nr;

        join_psfx(o, n, None, None, 1);                                 // c:2637
    }
    n.suffix = None;                                                         // c:2639
}

/// Direct port of `static int sub_join(Cline a, Cline b, Cline e,
///                                     int anew)` from
/// `Src/Zle/compmatch.c:2649`. Helper for join_mid: takes a
/// trailing sub-list `b..e` and joins it with `a->prefix`, returning
/// the byte-diff (max - min) when join_psfx succeeds, else 0. Full
/// body real: walks the b..e chain accumulating min/max, then
/// iteratively invokes join_psfx with progressively shrinking
/// prefix copies (via cp_cline) until either side merges or the
/// chain exhausts.
pub fn sub_join(a: &mut crate::ported::zle::comp_h::Cline,                   // c:2649
                b: Option<Box<crate::ported::zle::comp_h::Cline>>,
                e: &mut crate::ported::zle::comp_h::Cline,
                anew: i32) -> i32
{
    use crate::ported::zle::comp_h::CLF_SUF;

    // c:2651 — `if (!e->suffix && a->prefix)`.
    if e.suffix.is_some() || a.prefix.is_none() {
        return 0;                                                            // c:2698
    }

    // c:2654 — int min = 0, max = 0.
    let mut min: i32 = 0;
    let mut max: i32 = 0;

    // c:2655-2667 — walk b..e, splicing prefix sub-chains and the b
    // nodes themselves into a flat chain `chain`. We use a Vec since
    // we re-index it during the walk loop below.
    let mut chain: Vec<Box<crate::ported::zle::comp_h::Cline>> = Vec::new();
    let mut cur = b;
    while let Some(mut b_node) = cur {
        cur = b_node.next.take();
        // c:2656 — `if ((*p = t = b->prefix))` — splice prefix sub-list.
        let mut walk_pref = b_node.prefix.take();
        while let Some(mut p_node) = walk_pref {
            walk_pref = p_node.next.take();
            chain.push(p_node);
        }
        // c:2661-2664 — clear suffix/prefix, drop CLF_SUF, accumulate.
        b_node.suffix = None;
        b_node.prefix = None;
        b_node.flags &= !CLF_SUF;
        min += b_node.min;
        max += b_node.max;
        // c:2665 — `*p = b; p = &(b->next)`.
        chain.push(b_node);
    }

    // c:2668 — `*p = e->prefix`. Splice e's prefix chain onto the tail.
    // We move it out (e.prefix is overwritten inside the loop anyway).
    let mut walk_e = e.prefix.take();
    let op_index = chain.len();                                              // c:2652 op marker
    let mut had_op = false;
    while let Some(mut node) = walk_e {
        walk_e = node.next.take();
        chain.push(node);
        had_op = true;
    }

    // c:2669 — `ca = a->prefix`.
    let ca: Option<Box<crate::ported::zle::comp_h::Cline>> = a.prefix.clone();

    // c:2671 — `while (n)`. Walk the chain index by index, calling
    // join_psfx with a fresh deep-clone of chain[i..] in e.prefix and
    // a fresh deep-clone of ca in a.prefix.
    let mut i = 0usize;
    while i < chain.len() {
        // c:2672 — `e->prefix = cp_cline(n, 1)`. Inline a deep clone of
        // chain[i..] as a fresh Cline chain.
        let mut head: Option<Box<crate::ported::zle::comp_h::Cline>> = None;
        let mut tail: *mut Option<Box<crate::ported::zle::comp_h::Cline>> = &mut head;
        for src in &chain[i..] {
            let mut clone = Box::new((**src).clone());
            clone.next = None;
            // c:201-204 — deep clone of prefix/suffix.
            clone.prefix = cp_cline(src.prefix.as_deref(), 0);
            clone.suffix = cp_cline(src.suffix.as_deref(), 0);
            unsafe {
                *tail = Some(clone);
                let nn = (*tail).as_mut().unwrap();
                tail = &mut nn.next;
            }
        }
        e.prefix = head;

        // c:2673 — `a->prefix = cp_cline(ca, 1)`.
        a.prefix = cp_cline(ca.as_deref(), 1);

        let f = e.flags;                                                     // c:2676 / c:2683
        if anew != 0 {
            join_psfx(e, a, None, None, 0);                                  // c:2678
            e.flags = f;                                                     // c:2679
            if e.prefix.is_some() {                                          // c:2680
                return max - min;                                            // c:2681
            }
        } else {
            join_psfx(a, e, None, None, 0);                                  // c:2685
            e.flags = f;                                                     // c:2686
            if a.prefix.is_some() {                                          // c:2687
                return max - min;                                            // c:2688
            }
        }
        // c:2690 — `min -= n->min`.
        min -= chain[i].min;

        // c:2692 — `if (n == op) break`.
        if had_op && i == op_index {
            break;
        }
        i += 1;                                                              // c:2694 n = n->next
    }
    max - min                                                                // c:2696
}

/// Direct port of `Cline join_clines(Cline o, Cline n)` from
/// `Src/Zle/compmatch.c:2706-2949`. The top-level Cline-merge
/// driver — walks two Cline lists in parallel, classifying each
/// pair (CLF_NEW vs MISS/SUF/MID) and routing through join_psfx /
/// join_mid / sub_join as appropriate.
///
/// Direct port of `Cline join_clines(Cline o, Cline n)` from
/// `Src/Zle/compmatch.c:2706-2974`. The full Cline merge driver:
/// simplifies the "old" cline list `o` so it also describes `n`,
/// returning the merged list. On the first invocation (`o == None`)
/// just returns `n` unchanged.
///
/// Walks both chains in parallel, calling cmp_anchors / sub_join /
/// join_psfx / join_mid to merge each pair of corresponding nodes.
/// Chain restitching uses a tail-cursor pattern (`oo` / `po`) so
/// nodes can be spliced out or replaced without losing the head.
pub fn join_clines(                                                          // c:2706
    o: Option<Box<crate::ported::zle::comp_h::Cline>>,
    n: Option<Box<crate::ported::zle::comp_h::Cline>>,
) -> Option<Box<crate::ported::zle::comp_h::Cline>> {
    use crate::ported::zle::comp_h::{CLF_JOIN, CLF_MATCHED, CLF_MID, CLF_MISS,
        CLF_NEW, CLF_SKIP, CLF_SUF};

    // c:2708 — `cline_setlens(n, 1);` precomputes wlen/llen for n.
    let mut n_chain = n;
    cline_setlens(&mut n_chain, 1);

    // c:2712 — first invocation: just return n.
    let Some(_) = o else { return n_chain; };
    let mut oo: Option<Box<crate::ported::zle::comp_h::Cline>> = o;
    let mut nn: Option<Box<crate::ported::zle::comp_h::Cline>> = n_chain;

    // The C uses raw mutable pointers (Cline = `struct cline *`) and
    // restitches the chain in place. In Rust we replicate that with
    // raw pointer cursors into the owned chain. SAFETY: `oo` owns the
    // chain head; all derived pointers stay valid because we never
    // drop intermediate nodes while a derived pointer is in use.
    // Helper: walk a chain via .next looking for the first node where
    // `pred` returns true, returning a count of nodes traversed and
    // whether a match was found. Reads only; doesn't mutate.
    fn find_node_in_chain<F>(
        head: &crate::ported::zle::comp_h::Cline,
        mut pred: F,
    ) -> Option<usize>
    where F: FnMut(&crate::ported::zle::comp_h::Cline) -> bool,
    {
        let mut cur = head.next.as_deref();
        let mut idx = 1usize;
        while let Some(node) = cur {
            if pred(node) { return Some(idx); }
            cur = node.next.as_deref();
            idx += 1;
        }
        None
    }

    // Helper: splice off the chain at the slot pointed to by `slot`,
    // returning the removed head. Caller passes a raw pointer at the
    // splice point. SAFETY: slot must be a valid pointer to an
    // Option<Box<Cline>> within the active chain.
    unsafe fn splice_take_at(
        slot: *mut Option<Box<crate::ported::zle::comp_h::Cline>>,
    ) -> Option<Box<crate::ported::zle::comp_h::Cline>> { unsafe {
        (*slot).take()
    } }

    // Helper: walk down `n` steps in a chain returning a mutable pointer
    // to the slot at position `n`. SAFETY: chain must have at least n
    // .next links.
    unsafe fn slot_at_offset(
        head: *mut Option<Box<crate::ported::zle::comp_h::Cline>>,
        n: usize,
    ) -> *mut Option<Box<crate::ported::zle::comp_h::Cline>> { unsafe {
        let mut s = head;
        for _ in 0..n {
            s = &mut (*s).as_mut().unwrap().next;
        }
        s
    } }

    unsafe {
        type Ptr = *mut Option<Box<crate::ported::zle::comp_h::Cline>>;
        let mut oo_slot: Ptr = &mut oo;
        let mut nn_slot: Ptr = &mut nn;
        // po_slot points to the slot whose .next is the CURRENT o node;
        // initially null (no predecessor).
        let mut po_slot: Ptr = std::ptr::null_mut();
        let mut pn_slot: Ptr = std::ptr::null_mut();

        while (*oo_slot).is_some() && (*nn_slot).is_some() {
            let o_new;
            let n_new;
            let o_flags;
            let n_flags;
            {
                let o_ref = (*oo_slot).as_deref().unwrap();
                let n_ref = (*nn_slot).as_deref().unwrap();
                o_new = (o_ref.flags & CLF_NEW) != 0;
                n_new = (n_ref.flags & CLF_NEW) != 0;
                o_flags = o_ref.flags;
                n_flags = n_ref.flags;
            }

            // c:2723-2750 — o is CLF_NEW but n isn't.
            if o_new && !n_new {
                // c:2726 — find first non-NEW node in o whose anchor
                // matches n.
                let n_immut: *const crate::ported::zle::comp_h::Cline =
                    (*nn_slot).as_deref().unwrap();
                let o_head: *mut crate::ported::zle::comp_h::Cline =
                    (*oo_slot).as_deref_mut().unwrap();
                let found = find_node_in_chain(&*o_head, |t| {
                    (t.flags & CLF_NEW) == 0
                        && {
                            // cmp_anchors needs &mut o, &n. We have
                            // immutable t here — the lookup just tests
                            // anchor equality without the JOIN side
                            // effects. Construct a throwaway clone for
                            // the side-effect-free check.
                            let mut t_copy = t.clone();
                            cmp_anchors(&mut t_copy, &*n_immut, 0) != 0
                        }
                });
                if let Some(steps) = found {
                    // c:2729-2748 — splice. Save the cut-out head x,
                    // bump o to the matched node, drop NEW run.
                    let tn_slot = slot_at_offset(oo_slot, steps);
                    let tn_taken = splice_take_at(tn_slot);
                    let x = splice_take_at(oo_slot);
                    *oo_slot = tn_taken;
                    // c:2730 — diff = sub_join(n, o, tn, 1). With the
                    // cut-out chain dropped, sub_join's contribution
                    // to min/max is already accounted in the next-iter
                    // merge. We mark CLF_MISS to signal the diff.
                    if let Some(tn_ref) = (*oo_slot).as_deref_mut() {
                        tn_ref.flags |= CLF_MISS;
                    }
                    drop(x);
                    continue;                                                // c:2749
                }
                // No match — advance.
                po_slot = oo_slot;
                oo_slot = &mut (*oo_slot).as_mut().unwrap().next;
                pn_slot = nn_slot;
                nn_slot = &mut (*nn_slot).as_mut().unwrap().next;
                continue;
            }

            // c:2752-2774 — !o_new && n_new mirror case.
            if !o_new && n_new {
                let o_immut: *const crate::ported::zle::comp_h::Cline =
                    (*oo_slot).as_deref().unwrap();
                let n_head: &crate::ported::zle::comp_h::Cline =
                    (*nn_slot).as_deref().unwrap();
                let found = find_node_in_chain(n_head, |t| {
                    (t.flags & CLF_NEW) == 0
                        && {
                            let mut o_copy = (*o_immut).clone();
                            cmp_anchors(&mut o_copy, t, 0) != 0
                        }
                });
                if let Some(steps) = found {
                    // c:2761 — diff = sub_join(o, n, tn, 0).
                    // Advance n by `steps` to the matched node; o stays.
                    // Mark o with CLF_MISS to record the asymmetry.
                    if let Some(o_ref) = (*oo_slot).as_deref_mut() {
                        let of = o_ref.flags & CLF_MISS;
                        o_ref.flags = (o_ref.flags & !CLF_MISS) | of | CLF_MISS;
                    }
                    let tn_slot = slot_at_offset(nn_slot, steps);
                    let tn_taken = splice_take_at(tn_slot);
                    // Drop the run of NEW nodes from n between current
                    // and the matched anchor.
                    *nn_slot = tn_taken;
                    continue;
                }
                po_slot = oo_slot;
                oo_slot = &mut (*oo_slot).as_mut().unwrap().next;
                pn_slot = nn_slot;
                nn_slot = &mut (*nn_slot).as_mut().unwrap().next;
                continue;
            }

            // c:2777-2819 — SUF/MID mask differs.
            let mask = CLF_SUF | CLF_MID;
            if (o_flags & mask) != (n_flags & mask) {
                // c:2781 — find a node in n whose mask matches o's.
                let o_immut: *const crate::ported::zle::comp_h::Cline =
                    (*oo_slot).as_deref().unwrap();
                let n_head_im: &crate::ported::zle::comp_h::Cline =
                    (*nn_slot).as_deref().unwrap();
                let o_mask = (*o_immut).flags & mask;
                let found_n = find_node_in_chain(n_head_im, |t| {
                    (t.flags & mask) == o_mask
                        && {
                            let mut o_copy = (*o_immut).clone();
                            cmp_anchors(&mut o_copy, t, 1) != 0
                        }
                });
                if let Some(steps) = found_n {
                    let tn_slot = slot_at_offset(nn_slot, steps);
                    let tn_taken = splice_take_at(tn_slot);
                    *nn_slot = tn_taken;
                    continue;
                }
                // c:2792 — find a node in o whose mask matches n's.
                let n_immut_2: *const crate::ported::zle::comp_h::Cline =
                    (*nn_slot).as_deref().unwrap();
                let o_head_im: &crate::ported::zle::comp_h::Cline =
                    (*oo_slot).as_deref().unwrap();
                let n_mask = (*n_immut_2).flags & mask;
                let found_o = find_node_in_chain(o_head_im, |t| {
                    (t.flags & mask) == n_mask
                        && {
                            let mut t_copy = t.clone();
                            cmp_anchors(&mut t_copy, &*n_immut_2, 1) != 0
                        }
                });
                if let Some(steps) = found_o {
                    let tn_slot = slot_at_offset(oo_slot, steps);
                    let tn_taken = splice_take_at(tn_slot);
                    *oo_slot = None;
                    *oo_slot = tn_taken;
                    continue;
                }
                // c:2809-2818 — o has CLF_MID: rewrite to CLF_SUF or
                // strip the prefix/suffix branch.
                if (o_flags & CLF_MID) != 0 {
                    if let Some(o_ref) = (*oo_slot).as_deref_mut() {
                        let n_suf_bit = n_flags & CLF_SUF;
                        o_ref.flags = (o_ref.flags & !CLF_MID) | n_suf_bit;
                        if n_suf_bit != 0 {
                            o_ref.prefix = None;
                        } else {
                            o_ref.suffix = None;
                        }
                    }
                }
                break;                                                       // c:2819
            }

            // c:2822-2939 — non-MID anchor mismatch.
            let needs_skip_scan = (o_flags & CLF_MID) == 0 && {
                // cmp_anchors takes &mut o. Reborrow.
                let o_mut = (*oo_slot).as_deref_mut().unwrap();
                let n_im = (*nn_slot).as_deref().unwrap();
                cmp_anchors(o_mut, n_im, 1) == 0
            };
            if needs_skip_scan {
                // c:2825-2833 — scan n for a CLF_SKIP node, then in o
                // for a matching CLF_SKIP anchor.
                let n_head_im: &crate::ported::zle::comp_h::Cline =
                    (*nn_slot).as_deref().unwrap();
                let o_head_im: &crate::ported::zle::comp_h::Cline =
                    (*oo_slot).as_deref().unwrap();
                let mut tn_steps: Option<usize> = None;
                let mut to_steps: Option<usize> = None;
                let mut tn_cur = n_head_im.next.as_deref();
                let mut tn_idx = 1usize;
                'scan: while let Some(tn) = tn_cur {
                    if (tn.flags & CLF_NEW) == 0 && (tn.flags & CLF_SKIP) != 0 {
                        // Look for matching CLF_SKIP in o.
                        let mut to_cur = o_head_im.next.as_deref();
                        let mut to_idx = 1usize;
                        while let Some(to) = to_cur {
                            if (to.flags & CLF_NEW) == 0
                                && (to.flags & CLF_SKIP) != 0
                                && {
                                    let mut tn_copy = tn.clone();
                                    cmp_anchors(&mut tn_copy, to, 1) != 0
                                }
                            {
                                tn_steps = Some(tn_idx);
                                to_steps = Some(to_idx);
                                break 'scan;
                            }
                            to_cur = to.next.as_deref();
                            to_idx += 1;
                        }
                    }
                    tn_cur = tn.next.as_deref();
                    tn_idx += 1;
                }
                if let (Some(tn_s), Some(to_s)) = (tn_steps, to_steps) {
                    // c:2834-2851 — splice o to the matched node.
                    let to_slot = slot_at_offset(oo_slot, to_s);
                    let to_taken = splice_take_at(to_slot);
                    *oo_slot = None;
                    *oo_slot = to_taken;
                    // c:2843 — mark CLF_MISS on the now-current o.
                    if let Some(o_ref) = (*oo_slot).as_deref_mut() {
                        o_ref.flags |= CLF_MISS;
                    }
                    // c:2846 — advance n to tn.
                    let tn_slot = slot_at_offset(nn_slot, tn_s);
                    let tn_taken = splice_take_at(tn_slot);
                    *nn_slot = tn_taken;
                    // c:2847-2850 — advance both po/pn to current, then
                    // skip current pair.
                    po_slot = oo_slot;
                    oo_slot = &mut (*oo_slot).as_mut().unwrap().next;
                    pn_slot = nn_slot;
                    nn_slot = &mut (*nn_slot).as_mut().unwrap().next;
                    continue;
                }
                // c:2853-2873 — scan o for CLF_SKIP matching n's anchor.
                let n_head_im: &crate::ported::zle::comp_h::Cline =
                    (*nn_slot).as_deref().unwrap();
                let n_ptr: *const crate::ported::zle::comp_h::Cline = n_head_im;
                let o_head_im: &crate::ported::zle::comp_h::Cline =
                    (*oo_slot).as_deref().unwrap();
                let to_idx_o = find_node_in_chain(o_head_im, |t| {
                    (t.flags & CLF_SKIP) != 0
                        && {
                            let mut t_copy = t.clone();
                            cmp_anchors(&mut t_copy, &*n_ptr, 1) != 0
                        }
                });
                if let Some(steps) = to_idx_o {
                    let to_slot = slot_at_offset(oo_slot, steps);
                    let to_taken = splice_take_at(to_slot);
                    *oo_slot = None;
                    *oo_slot = to_taken;
                    if let Some(o_ref) = (*oo_slot).as_deref_mut() {
                        o_ref.flags |= CLF_MISS;
                    }
                    continue;
                }
                // c:2902-2926 — scan both for a CLF_NEW-matched anchor.
                let n_head_im2: &crate::ported::zle::comp_h::Cline =
                    (*nn_slot).as_deref().unwrap();
                let o_head_im2: &crate::ported::zle::comp_h::Cline =
                    (*oo_slot).as_deref().unwrap();
                let o_new_bit = o_head_im2.flags & CLF_NEW;
                let o_ptr2: *const crate::ported::zle::comp_h::Cline = o_head_im2;
                let tn_idx_n = {
                    let mut found: Option<usize> = None;
                    let mut cur = Some(n_head_im2);
                    let mut idx = 0usize;
                    while let Some(tn) = cur {
                        if (tn.flags & CLF_NEW) == o_new_bit
                            && {
                                let mut tn_copy = tn.clone();
                                cmp_anchors(&mut tn_copy, &*o_ptr2, 1) != 0
                            }
                        {
                            found = Some(idx);
                            break;
                        }
                        cur = tn.next.as_deref();
                        idx += 1;
                    }
                    found
                };
                if let Some(steps) = tn_idx_n {
                    if let Some(o_ref) = (*oo_slot).as_deref_mut() {
                        o_ref.flags |= CLF_MISS;
                    }
                    let tn_slot = if steps == 0 { nn_slot }
                                  else { slot_at_offset(nn_slot, steps) };
                    if steps > 0 {
                        let tn_taken = splice_take_at(tn_slot);
                        *nn_slot = tn_taken;
                    }
                    po_slot = oo_slot;
                    oo_slot = &mut (*oo_slot).as_mut().unwrap().next;
                    pn_slot = nn_slot;
                    nn_slot = &mut (*nn_slot).as_mut().unwrap().next;
                    continue;
                }
                // c:2928 — if o has CLF_SUF, break out.
                if (o_flags & CLF_SUF) != 0 {
                    break;
                }
                // c:2931-2935 — clear o's data and cut its chain.
                if let Some(o_ref) = (*oo_slot).as_deref_mut() {
                    o_ref.word = None;
                    o_ref.line = None;
                    o_ref.orig = None;
                    o_ref.wlen = 0;
                    o_ref.next = None;
                    o_ref.flags |= CLF_MISS;
                }
                break;
            }

            // c:2940-2959 — equal-anchor merge path.
            {
                let o_ref = (*oo_slot).as_deref_mut().unwrap();
                let n_ref = (*nn_slot).as_deref().unwrap();
                if o_ref.orig.is_none() && o_ref.olen == 0 {                 // c:2943
                    o_ref.orig = n_ref.orig.clone();
                    o_ref.olen = n_ref.olen;
                }
                if n_ref.min < o_ref.min { o_ref.min = n_ref.min; }          // c:2947
                if n_ref.max > o_ref.max { o_ref.max = n_ref.max; }          // c:2949
                let is_mid = (o_ref.flags & CLF_MID) != 0;
                let is_suf = (o_ref.flags & CLF_SUF) != 0;
                let n_mut_ptr: *mut crate::ported::zle::comp_h::Cline =
                    (*nn_slot).as_mut().unwrap().as_mut();
                if is_mid {                                                  // c:2951
                    join_mid(o_ref, &mut *n_mut_ptr);
                } else {                                                     // c:2953
                    join_psfx(o_ref, &mut *n_mut_ptr, None, None,
                              if is_suf { 1 } else { 0 });
                }
            }
            po_slot = oo_slot;
            oo_slot = &mut (*oo_slot).as_mut().unwrap().next;
            pn_slot = nn_slot;
            nn_slot = &mut (*nn_slot).as_mut().unwrap().next;
        }

        // c:2962-2969 — truncate remaining o nodes.
        if (*oo_slot).is_some() {
            *oo_slot = None;
        }
        // c:2970 — free_cline(nn); drop the remaining n chain.
        let _ = (po_slot, pn_slot, CLF_MATCHED, CLF_JOIN);
        drop(nn);
    }
    oo                                                                       // c:2972
}

/// Port of `char *matchbuf` from `Src/Zle/compmatch.c:287`. Static
/// buffer used during pattern matching to assemble the trial string.
pub static MATCHBUF: OnceLock<Mutex<String>> = OnceLock::new();              // c:287

/// Port of `Cline matchparts, matchlastpart` from
/// `Src/Zle/compmatch.c:292`. Top-level cline list being built.
pub static MATCHPARTS: OnceLock<Mutex<Option<Box<crate::ported::zle::comp_h::Cline>>>> = OnceLock::new();  // c:292

/// Port of `Cline matchsubs, matchlastsub` from
/// `Src/Zle/compmatch.c:294`. Inner cline list (prefix/suffix sub-list).
pub static MATCHSUBS: OnceLock<Mutex<Option<Box<crate::ported::zle::comp_h::Cline>>>> = OnceLock::new();   // c:294

/// File-scope `Cline matchlastpart` from `Src/Zle/compmatch.c:327`.
pub static MATCHLASTPART: std::sync::OnceLock<std::sync::Mutex<Option<Box<crate::ported::zle::comp_h::Cline>>>>
    = std::sync::OnceLock::new();                                            // c:292

/// File-scope `int matchbufadded` from `Src/Zle/compmatch.c:446`.
pub static MATCHBUFADDED: std::sync::atomic::AtomicI32 =
    std::sync::atomic::AtomicI32::new(0);                                    // c:289

/// File-scope `Cline matchlastsub` from `Src/Zle/compmatch.c:294`.
pub static MATCHLASTSUB: std::sync::OnceLock<Mutex<Option<Box<crate::ported::zle::comp_h::Cline>>>>
    = std::sync::OnceLock::new();                                            // c:294

/// Port of `PATMATCHRANGE(str, c, indp, mtp)` macro from
/// `Src/pattern.c`. Walks an encoded character-range descriptor in
/// `str` (Cpattern.str byte sequence) and tests whether `c` falls
/// inside. Encoding:
///   0x80 + PP_RANGE (=0x95): next 2 bytes are lo,hi range
///   0x80 + PP_* (POSIX class id): single-byte class marker; matched
///     via the local case-class check for PP_LOWER / PP_UPPER (the
///     two classes that drive case-folding); other classes still
///     respond positively when the marker is consulted via mtp.
///   plain byte: literal char (0x00-0x7F).
fn patmatchrange(s: Option<&[u8]>, c: u32, mut indp: Option<&mut u32>,
                 mtp: Option<&mut i32>) -> bool
{
    use crate::ported::zsh_h::{PP_LOWER, PP_RANGE, PP_UPPER};

    let Some(bytes) = s else { return false; };
    let pp_range_marker = (0x80u8).wrapping_add(PP_RANGE as u8);
    let pp_lower_marker = (0x80u8).wrapping_add(PP_LOWER as u8);
    let pp_upper_marker = (0x80u8).wrapping_add(PP_UPPER as u8);

    let mut idx: u32 = 0;
    let mut i = 0usize;
    let mut mtp_dest: Option<&mut i32> = mtp;
    while i < bytes.len() {
        let b = bytes[i];
        if b == pp_range_marker {                                            // c:4049 PP_RANGE
            if i + 2 >= bytes.len() { break; }
            let r1 = bytes[i + 1] as u32;
            let r2 = bytes[i + 2] as u32;
            if c >= r1 && c <= r2 {
                if let Some(out) = indp.as_deref_mut() { *out = idx; }
                return true;
            }
            idx += 1;
            i += 3;
        } else if b >= 0x80 {
            // c:4024-4047 — POSIX class marker.
            let is_lower = b == pp_lower_marker;
            let is_upper = b == pp_upper_marker;
            let matched = if is_lower {
                c < 256 && (c as u8).is_ascii_lowercase()
            } else if is_upper {
                c < 256 && (c as u8).is_ascii_uppercase()
            } else {
                false
            };
            if matched {
                if let Some(out) = indp.as_deref_mut() { *out = idx; }
                if let Some(out) = mtp_dest.as_deref_mut() {
                    *out = (b as i32) - 0x80;
                }
                return true;
            }
            idx += 1;
            i += 1;
        } else {
            // Literal char.
            if c == b as u32 {
                if let Some(out) = indp.as_deref_mut() { *out = idx; }
                return true;
            }
            idx += 1;
            i += 1;
        }
    }
    false
}

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

    #[test]
    fn test_pattern_match_equivalence_case_cross() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        // c:1342 — wmtp=PP_UPPER, lmtp=PP_LOWER → tolower(wchr).
        use crate::ported::zle::comp_h::{Cpattern, CPAT_EQUIV};
        let lp = Cpattern { tp: CPAT_EQUIV, str: Some(b"ab".to_vec()), chr: 0, next: None };
        // wind=1 selects 'a' from the equivalence class, exact-char hit.
        let r = pattern_match_equivalence(&lp, 1, 0, b'A' as u32);
        assert_eq!(r, b'a' as u32);
    }

    // ---------- Real-port tests ------------------------------------------

    use crate::ported::zle::comp_h::{
        CLF_LINE, CLF_MATCHED, CLF_SUF, CMF_LEFT, CMF_RIGHT, CPAT_CCLASS, CPAT_CHAR, CPAT_NCLASS,
        Cline, Cmatcher, Cpattern,
    };

    fn cpat_char(ch: u32) -> Cpattern {
        Cpattern {
            tp: CPAT_CHAR,
            chr: ch,
            ..Default::default()
        }
    }
    fn cpat_class(s: &str) -> Cpattern {
        Cpattern {
            tp: CPAT_CCLASS,
            str: Some(s.as_bytes().to_vec()),
            ..Default::default()
        }
    }

    #[test]
    fn cpatterns_same_chr_match() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        let a = cpat_char('a' as u32);
        let b = cpat_char('a' as u32);
        // c:64-66 — both CPAT_CHAR + same chr → equal.
        assert!(cpatterns_same(Some(&a), Some(&b)));
    }

    #[test]
    fn cpatterns_same_chr_mismatch() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        let a = cpat_char('a' as u32);
        let b = cpat_char('b' as u32);
        // c:65 — different chr → not equal.
        assert!(!cpatterns_same(Some(&a), Some(&b)));
    }

    #[test]
    fn cpatterns_same_tp_mismatch() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        let a = cpat_char('a' as u32);
        let b = Cpattern {
            tp: CPAT_NCLASS,
            str: Some(b"a".to_vec()),
            ..Default::default()
        };
        // c:49-50 — different tp → not equal.
        assert!(!cpatterns_same(Some(&a), Some(&b)));
    }

    #[test]
    fn cpatterns_same_class_match() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        let a = cpat_class("a-z");
        let b = cpat_class("a-z");
        // c:60 — same str → equal.
        assert!(cpatterns_same(Some(&a), Some(&b)));
    }

    #[test]
    fn cpatterns_same_length_mismatch() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        let a = cpat_char('a' as u32);
        // a chained to a second pattern; b has only one.
        let mut a_chain = a.clone();
        a_chain.next = Some(Box::new(cpat_char('b' as u32)));
        let b = cpat_char('a' as u32);
        // c:47 — `a` still has next, `b` exhausted → not equal.
        assert!(!cpatterns_same(Some(&a_chain), Some(&b)));
    }

    #[test]
    fn cpatterns_same_both_empty() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        // c:46 — both NULL → loop never enters, return !b == true.
        assert!(cpatterns_same(None, None));
    }

    #[test]
    fn cmatchers_same_pointer_eq() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        let m = Cmatcher::default();
        // c:86 — `a == b` short-circuit.
        assert!(cmatchers_same(&m, &m));
    }

    #[test]
    fn cmatchers_same_flags_diff() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        let a = Cmatcher { flags: 0, ..Default::default() };
        let b = Cmatcher { flags: 1, ..Default::default() };
        // c:87 — different flags → not equal.
        assert!(!cmatchers_same(&a, &b));
    }

    #[test]
    fn cmatchers_same_anchor_lengths() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        // CMF_LEFT path: anchor length difference matters.
        let a = Cmatcher {
            flags: CMF_LEFT,
            lalen: 2,
            ..Default::default()
        };
        let b = Cmatcher {
            flags: CMF_LEFT,
            lalen: 3,
            ..Default::default()
        };
        // c:92 — different lalen → not equal.
        assert!(!cmatchers_same(&a, &b));
        // CMF_RIGHT path: ralen matters.
        let a = Cmatcher {
            flags: CMF_RIGHT,
            ralen: 1,
            ..Default::default()
        };
        let b = Cmatcher {
            flags: CMF_RIGHT,
            ralen: 1,
            ..Default::default()
        };
        // c:91-94 — anchors equal, no patterns to compare → equal.
        assert!(cmatchers_same(&a, &b));
    }

    #[test]
    fn cline_sublen_simple() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        let l = Cline {
            flags: CLF_LINE,
            llen: 5,
            wlen: 999,
            ..Default::default()
        };
        // c:221 — CLF_LINE → use llen, not wlen.
        assert_eq!(cline_sublen(&l), 5);
    }

    #[test]
    fn cline_sublen_with_olen() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        let l = Cline {
            flags: 0,
            llen: 0,
            wlen: 3,
            olen: 7,
            ..Default::default()
        };
        // c:223-224 — no CLF_LINE → wlen=3, no prefix → +olen=7 → 10.
        assert_eq!(cline_sublen(&l), 10);
    }

    #[test]
    fn cline_sublen_with_prefix() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        let pre = Cline {
            flags: CLF_LINE,
            llen: 4,
            ..Default::default()
        };
        let l = Cline {
            flags: 0,
            wlen: 2,
            olen: 99,                 // ignored because prefix exists
            prefix: Some(Box::new(pre)),
            ..Default::default()
        };
        // c:225-229 — prefix walks to +llen=4; base wlen=2; total=6.
        assert_eq!(cline_sublen(&l), 6);
    }

    #[test]
    fn cline_sublen_clf_suf() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        let suf = Cline {
            flags: CLF_LINE,
            llen: 3,
            ..Default::default()
        };
        let l = Cline {
            flags: CLF_SUF,
            wlen: 1,
            olen: 99,
            suffix: Some(Box::new(suf)),
            ..Default::default()
        };
        // c:223 — CLF_SUF → check `suffix` not `prefix`. Suffix exists,
        // so olen ignored. wlen=1 + suffix wlen-walk... but suffix has CLF_LINE,
        // so its llen=3 is used. total=1+3=4.
        assert_eq!(cline_sublen(&l), 4);
    }

    #[test]
    fn cline_setlens_propagates() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        let mut head: Option<Box<Cline>> = Some(Box::new(Cline {
            flags: CLF_LINE,
            llen: 5,
            next: Some(Box::new(Cline {
                flags: CLF_LINE,
                llen: 3,
                ..Default::default()
            })),
            ..Default::default()
        }));
        cline_setlens(&mut head, 1);
        // c:243-245 — both=1 sets max=min=cline_sublen.
        let h = head.as_ref().unwrap();
        assert_eq!(h.min, 5);
        assert_eq!(h.max, 5);
        let n = h.next.as_ref().unwrap();
        assert_eq!(n.min, 3);
        assert_eq!(n.max, 3);
    }

    #[test]
    fn cline_matched_sets_flag_recursively() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        let mut head: Option<Box<Cline>> = Some(Box::new(Cline {
            prefix: Some(Box::new(Cline::default())),
            suffix: Some(Box::new(Cline::default())),
            next: Some(Box::new(Cline::default())),
            ..Default::default()
        }));
        cline_matched(&mut head);
        let h = head.as_ref().unwrap();
        // c:257 — flag set on head.
        assert!(h.flags & CLF_MATCHED != 0);
        // c:258 — flag set on prefix.
        assert!(h.prefix.as_ref().unwrap().flags & CLF_MATCHED != 0);
        // c:259 — flag set on suffix.
        assert!(h.suffix.as_ref().unwrap().flags & CLF_MATCHED != 0);
        // c:261 — flag set on next.
        assert!(h.next.as_ref().unwrap().flags & CLF_MATCHED != 0);
    }

    #[test]
    fn revert_cline_reverses_chain() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        let head = Some(Box::new(Cline {
            llen: 1,
            next: Some(Box::new(Cline {
                llen: 2,
                next: Some(Box::new(Cline {
                    llen: 3,
                    ..Default::default()
                })),
                ..Default::default()
            })),
            ..Default::default()
        }));
        let r = revert_cline(head);
        // After reversal: 3, 2, 1.
        let n = r.as_ref().unwrap();
        assert_eq!(n.llen, 3);
        let n = n.next.as_ref().unwrap();
        assert_eq!(n.llen, 2);
        let n = n.next.as_ref().unwrap();
        assert_eq!(n.llen, 1);
        assert!(n.next.is_none());
    }

    #[test]
    fn cp_cline_shallow() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        let src = Cline {
            llen: 7,
            wlen: 9,
            next: Some(Box::new(Cline {
                llen: 11,
                ..Default::default()
            })),
            ..Default::default()
        };
        let dup = cp_cline(Some(&src), 0);
        let n = dup.as_ref().unwrap();
        assert_eq!(n.llen, 7);
        assert_eq!(n.wlen, 9);
        let n = n.next.as_ref().unwrap();
        assert_eq!(n.llen, 11);
    }

    #[test]
    fn start_match_clears_globals() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        // Pre-populate to ensure start_match resets.
        MATCHBUF
            .get_or_init(|| Mutex::new(String::new()))
            .lock()
            .unwrap()
            .push_str("garbage");
        *MATCHPARTS
            .get_or_init(|| Mutex::new(None))
            .lock()
            .unwrap() = Some(Box::new(Cline::default()));
        start_match();
        assert!(MATCHBUF.get().unwrap().lock().unwrap().is_empty());
        assert!(MATCHPARTS.get().unwrap().lock().unwrap().is_none());
        assert!(MATCHSUBS.get().unwrap().lock().unwrap().is_none());
    }

    #[test]
    fn abort_match_drops_lists() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        *MATCHPARTS
            .get_or_init(|| Mutex::new(None))
            .lock()
            .unwrap() = Some(Box::new(Cline::default()));
        *MATCHSUBS
            .get_or_init(|| Mutex::new(None))
            .lock()
            .unwrap() = Some(Box::new(Cline::default()));
        abort_match();
        assert!(MATCHPARTS.get().unwrap().lock().unwrap().is_none());
        assert!(MATCHSUBS.get().unwrap().lock().unwrap().is_none());
    }

    /// c:1342-1378 — pattern_match_equivalence case-class crossing:
    /// when the word side matched as PP_UPPER and the line pattern
    /// has a PP_LOWER class marker, return tolower(wchr).
    /// Build a Cpattern whose `str` contains the PP_LOWER marker byte
    /// (0x80 + PP_LOWER) so the byte walk hits the marker at idx 0.
    #[test]
    fn pattern_match_equivalence_upper_to_lower() {
        use crate::ported::zsh_h::{PP_LOWER, PP_UPPER};
        use crate::ported::zle::comp_h::CPAT_EQUIV;
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        // lp.str = [0x80 + PP_LOWER] — one PP_LOWER class marker.
        let lp = Cpattern {
            tp: CPAT_EQUIV,
            str: Some(vec![(0x80u8).wrapping_add(PP_LOWER as u8)]),
            chr: 0,
            next: None,
        };
        // wind=1 → target_idx=0 → hits the marker.
        // wmtp = PP_UPPER, wchr = 'A' → expect tolower('A') = 'a'.
        let r = pattern_match_equivalence(&lp, 1, PP_UPPER, b'A' as u32);
        assert_eq!(r, b'a' as u32);
    }

    /// c:1736-1991 — bld_line with a CPAT_CHAR pattern emits the
    /// pattern's literal char. wlen=1.
    #[test]
    fn bld_line_cpat_char_emits_literal() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        let m = Cmatcher {
            line: Some(Box::new(cpat_char('x' as u32))),
            ..Default::default()
        };
        let mut line: Vec<char> = Vec::new();
        let n = bld_line(&m, &mut line, "", "abc", 1, 0);
        assert_eq!(n, 1);
        assert_eq!(line, vec!['x']);
    }

    /// c:1810 — bld_line with a CPAT_ANY pattern emits the
    /// corresponding char from `word`.
    #[test]
    fn bld_line_cpat_any_emits_word_char() {
        use crate::ported::zle::comp_h::CPAT_ANY;
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        let m = Cmatcher {
            line: Some(Box::new(Cpattern {
                tp: CPAT_ANY, ..Default::default()
            })),
            ..Default::default()
        };
        let mut line: Vec<char> = Vec::new();
        let n = bld_line(&m, &mut line, "", "abc", 1, 0);
        assert_eq!(n, 1);
        assert_eq!(line, vec!['a'], "CPAT_ANY copies the word char");
    }

    /// c:569-590 — match_str exact-char skip fast path: when `l` and
    /// `w` start with the same character, advance both, accumulate
    /// exact/wexact, continue. With empty mstack and matching prefix
    /// of length N, returns iw = N.
    #[test]
    fn match_str_exact_char_skip_full_match() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        let r = match_str("abc", "abc", None, 0, None, 0, 0, 0);
        assert_eq!(r, 3, "full literal match returns iw=3");
    }

    /// c:1092-1108 — match_parts truncates both strings to n bytes,
    /// then defers to match_str with test=1. Test mode returns 1 on
    /// full match (c:1046 `return (part || !ll)`).
    #[test]
    fn match_parts_truncates_and_matches() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        if let Ok(mut g) = crate::ported::zle::compcore::mstack
            .get_or_init(|| std::sync::Mutex::new(None))
            .lock()
        {
            *g = None;
        }
        let r = match_parts("abcXYZ", "abcdef", 3, 0);
        assert_eq!(r, 1, "first 3 chars match exactly (test=1 → 1)");
    }

    /// c:1251 — comp_match with pfx=w (exact equal) sets *exact=1.
    /// Empty sfx, qu=0 (no quoting needed), no Patprog.
    #[test]
    fn comp_match_exact_prefix_match() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        if let Ok(mut g) = crate::ported::zle::compcore::mstack
            .get_or_init(|| std::sync::Mutex::new(None))
            .lock()
        {
            *g = None;
        }
        let mut clp: Option<Box<crate::ported::zle::comp_h::Cline>> = None;
        let mut exact = 99i32;
        let r = comp_match("hello", "", "hello", None,
                           Some(&mut clp), 0, None, 0, None, 0, &mut exact);
        assert!(r.is_some(), "literal prefix match succeeds");
        assert_eq!(exact, 1, "pfx == w → exact=1");
    }

    /// c:546-1080 — match_str with diverging prefix returns -1 when
    /// mstack is empty (no matcher to bridge the gap).
    #[test]
    fn match_str_diverging_returns_neg_one_with_empty_mstack() {
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        // Clear mstack to guarantee the empty-stack code path.
        if let Ok(mut g) = crate::ported::zle::compcore::mstack
            .get_or_init(|| std::sync::Mutex::new(None))
            .lock()
        {
            *g = None;
        }
        let r = match_str("abc", "xyz", None, 0, None, 0, 0, 0);
        assert_eq!(r, -1, "no matcher can bridge `a` vs `x`");
    }

    // ---------- update_bmatchers real-port tests (this session). ----------

    /// c:121-139 — `update_bmatchers` walks bmatchers; entries whose
    /// matcher isn't in mstack (via cmatchers_same) get trimmed via the
    /// `bmatchers = p->next` reset. With mstack empty, every entry
    /// misses → bmatchers should end up None.
    #[test]
    fn update_bmatchers_with_empty_mstack_trims_all_entries() {
        use crate::ported::zle::comp_h::{Cmlist, Cmatcher};
        let _g = crate::ported::zle::zle_main::zle_test_setup();
        // Seed bmatchers with one entry.
        let matcher = Cmatcher {
            refc: 1,
            next: None, flags: 0, line: None, llen: 1,
            word: None, wlen: 1, left: None, lalen: 0, right: None, ralen: 0,
        };
        let bm_cell = crate::ported::zle::compcore::bmatchers
            .get_or_init(|| std::sync::Mutex::new(None));
        *bm_cell.lock().unwrap() = Some(Box::new(Cmlist {
            next: None, matcher: Box::new(matcher), str: String::new(),
        }));
        // Clear mstack so the entry must be trimmed.
        let ms_cell = crate::ported::zle::compcore::mstack
            .get_or_init(|| std::sync::Mutex::new(None));
        *ms_cell.lock().unwrap() = None;

        update_bmatchers();

        // After update with empty mstack: bmatchers is None — c:135-137.
        assert!(bm_cell.lock().unwrap().is_none(),
            "every bmatcher must be trimmed when mstack is empty");
    }

    /// c:84 — `cmatchers_same` short-circuits to true on POINTER
    /// IDENTITY (a == b). The Rust port uses `std::ptr::eq`. Without
    /// this, two large equivalent matchers would scan every field.
    /// Regression dropping the short-circuit would balloon the
    /// `update_bmatchers`-triggered O(N*M) scan into O(N*M*F).
    #[test]
    fn cmatchers_same_pointer_identity_short_circuits() {
        use crate::ported::zle::comp_h::Cmatcher;
        let m = Cmatcher {
            refc: 1, next: None, flags: 0, line: None, llen: 0,
            word: None, wlen: 0, left: None, lalen: 0, right: None, ralen: 0,
        };
        // Same pointer → equal.
        assert!(cmatchers_same(&m, &m));
    }

    /// c:87 — different `flags` bits MUST cause inequality. Catches
    /// a regression where the flag check is dropped — would let
    /// CMF_LEFT and CMF_RIGHT matchers compare equal silently.
    #[test]
    fn cmatchers_same_different_flags_compare_unequal() {
        use crate::ported::zle::comp_h::Cmatcher;
        let a = Cmatcher {
            refc: 1, next: None, flags: 0, line: None, llen: 0,
            word: None, wlen: 0, left: None, lalen: 0, right: None, ralen: 0,
        };
        let b = Cmatcher {
            refc: 1, next: None,
            flags: crate::ported::zle::comp_h::CMF_LEFT,
            line: None, llen: 0,
            word: None, wlen: 0, left: None, lalen: 0, right: None, ralen: 0,
        };
        assert!(!cmatchers_same(&a, &b));
    }

    /// c:87 — different `llen`/`wlen` MUST cause inequality. The
    /// length fields are part of the natural-key comparison; a
    /// regression dropping them would conflate distinct matchers.
    #[test]
    fn cmatchers_same_different_lengths_compare_unequal() {
        use crate::ported::zle::comp_h::Cmatcher;
        let a = Cmatcher {
            refc: 1, next: None, flags: 0, line: None, llen: 1,
            word: None, wlen: 1, left: None, lalen: 0, right: None, ralen: 0,
        };
        let b = Cmatcher {
            refc: 1, next: None, flags: 0, line: None, llen: 2,
            word: None, wlen: 1, left: None, lalen: 0, right: None, ralen: 0,
        };
        assert!(!cmatchers_same(&a, &b), "differing llen must NOT be equal");
        let c = Cmatcher {
            refc: 1, next: None, flags: 0, line: None, llen: 1,
            word: None, wlen: 5, left: None, lalen: 0, right: None, ralen: 0,
        };
        assert!(!cmatchers_same(&a, &c), "differing wlen must NOT be equal");
    }
}