neovm-core 0.0.2

Core runtime structures for NeoVM
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
//! Emacs coding system support.
//!
//! Since Rust natively uses UTF-8, this module is primarily for API
//! compatibility. The coding system infrastructure tracks registered
//! systems and their aliases but all actual encoding/decoding passes
//! through as UTF-8 identity operations.
//!
//! Contains:
//! - CodingSystemManager: registry of coding systems, aliases, priority list
//! - CodingSystemInfo: per-system metadata (name, type, mnemonic, EOL)
//! - Pure builtins: coding-system-list, coding-system-aliases, coding-system-get,
//!   coding-system-put, coding-system-base, coding-system-eol-type,
//!   coding-system-type, coding-system-change-eol-conversion,
//!   coding-system-change-text-conversion,
//!   detect-coding-string, detect-coding-region, keyboard-coding-system,
//!   terminal-coding-system, set-keyboard-coding-system,
//!   set-terminal-coding-system, coding-system-priority-list

use super::error::{EvalResult, Flow, signal};
use super::eval::Context;
use super::intern::{SymId, intern, lookup_interned, resolve_sym};
use super::value::*;
use std::collections::{HashMap, HashSet};

// ---------------------------------------------------------------------------
// Argument helpers (local to this module)
// ---------------------------------------------------------------------------

fn expect_args(name: &str, args: &[Value], n: usize) -> Result<(), Flow> {
    if args.len() != n {
        Err(signal(
            "wrong-number-of-arguments",
            vec![Value::symbol(name), Value::fixnum(args.len() as i64)],
        ))
    } else {
        Ok(())
    }
}

fn expect_min_args(name: &str, args: &[Value], min: usize) -> Result<(), Flow> {
    if args.len() < min {
        Err(signal(
            "wrong-number-of-arguments",
            vec![Value::symbol(name), Value::fixnum(args.len() as i64)],
        ))
    } else {
        Ok(())
    }
}

fn expect_max_args(name: &str, args: &[Value], max: usize) -> Result<(), Flow> {
    if args.len() > max {
        Err(signal(
            "wrong-number-of-arguments",
            vec![Value::symbol(name), Value::fixnum(args.len() as i64)],
        ))
    } else {
        Ok(())
    }
}

fn expect_integer_or_marker(val: &Value) -> Result<(), Flow> {
    if val.is_marker() {
        return Ok(());
    }
    match val.kind() {
        ValueKind::Fixnum(_) => Ok(()),
        _ => Err(signal(
            "wrong-type-argument",
            vec![Value::symbol("integer-or-marker-p"), *val],
        )),
    }
}

fn is_known_or_derived_coding_system(mgr: &CodingSystemManager, name: &str) -> bool {
    resolve_runtime_name(mgr, name).is_some()
}

fn normalize_keyboard_coding_system(name: &str) -> String {
    if let Some(eol) = EolType::from_suffix(name) {
        let base = strip_eol_suffix(name);
        return match eol {
            EolType::Unix => match base {
                "binary" | "no-conversion" => base.to_string(),
                _ => format!("{base}-unix"),
            },
            EolType::Dos | EolType::Mac => normalize_keyboard_coding_system(base),
            EolType::Undecided => unreachable!("suffix-based eol cannot be undecided"),
        };
    }
    match name {
        "binary" | "no-conversion" => name.to_string(),
        "emacs-internal" => "emacs-internal".to_string(),
        "ascii" | "us-ascii" => "us-ascii-unix".to_string(),
        "latin-1" | "iso-8859-1" | "iso-latin-1" => "iso-latin-1-unix".to_string(),
        "latin-5" | "iso-8859-9" | "iso-latin-5" => "iso-latin-5-unix".to_string(),
        "latin-0" | "latin-9" | "iso-8859-15" | "iso-latin-9" => "iso-latin-9-unix".to_string(),
        _ => format!("{name}-unix"),
    }
}

/// Extract a coding system name from a symbol or string argument.
#[cfg(test)]
fn coding_system_name(val: &Value) -> Result<String, Flow> {
    match val.kind() {
        ValueKind::Symbol(id) => Ok(resolve_sym(id).to_owned()),
        ValueKind::String => coding_runtime_string(val),
        ValueKind::Nil => Ok("nil".to_string()),
        _ => Err(signal(
            "wrong-type-argument",
            vec![Value::symbol("symbolp"), *val],
        )),
    }
}

/// Extract a coding system name from a symbol-like argument.
/// Accepts symbols, keywords, nil, and t.
fn coding_symbol_name(val: &Value) -> Result<String, Flow> {
    match val.as_symbol_name() {
        Some(name) => Ok(name.to_string()),
        None => Err(signal(
            "wrong-type-argument",
            vec![Value::symbol("symbolp"), *val],
        )),
    }
}

fn coding_runtime_string(value: &Value) -> Result<String, Flow> {
    value.as_runtime_string_owned().ok_or_else(|| {
        signal(
            "wrong-type-argument",
            vec![Value::symbol("stringp"), *value],
        )
    })
}

// ---------------------------------------------------------------------------
// EOL types
// ---------------------------------------------------------------------------

/// End-of-line conversion types matching Emacs conventions.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EolType {
    /// LF (Unix) -- value 0
    Unix,
    /// CRLF (DOS/Windows) -- value 1
    Dos,
    /// CR (Classic Mac) -- value 2
    Mac,
    /// Undecided / detect automatically
    Undecided,
}

impl EolType {
    pub fn to_int(&self) -> i64 {
        match self {
            EolType::Unix => 0,
            EolType::Dos => 1,
            EolType::Mac => 2,
            EolType::Undecided => 0,
        }
    }

    pub fn to_symbol(&self) -> Value {
        match self {
            EolType::Unix => Value::symbol("unix"),
            EolType::Dos => Value::symbol("dos"),
            EolType::Mac => Value::symbol("mac"),
            EolType::Undecided => Value::symbol("undecided"),
        }
    }

    pub fn suffix(&self) -> &'static str {
        match self {
            EolType::Unix => "-unix",
            EolType::Dos => "-dos",
            EolType::Mac => "-mac",
            EolType::Undecided => "",
        }
    }

    pub fn from_suffix(name: &str) -> Option<EolType> {
        if name.ends_with("-unix") {
            Some(EolType::Unix)
        } else if name.ends_with("-dos") {
            Some(EolType::Dos)
        } else if name.ends_with("-mac") {
            Some(EolType::Mac)
        } else {
            None
        }
    }
}

// ---------------------------------------------------------------------------
// CodingSystemInfo
// ---------------------------------------------------------------------------

/// Information about a single coding system.
#[derive(Clone, Debug)]
pub struct CodingSystemInfo {
    /// Canonical name of the coding system (e.g. "utf-8").
    pub name: SymId,
    /// Type category (e.g. "utf-8", "charset", "raw-text", "undecided").
    pub coding_type: SymId,
    /// Mnemonic character shown in the mode line.
    pub mnemonic: char,
    /// End-of-line conversion type.
    pub eol_type: EolType,
    /// Whether this coding system is ASCII compatible.
    pub ascii_compatible_p: bool,
    /// Charset list (names of supported charsets).
    pub charset_list: Vec<SymId>,
    /// Post-read conversion function name.
    pub post_read_conversion: Option<SymId>,
    /// Pre-write conversion function name.
    pub pre_write_conversion: Option<SymId>,
    /// Default character for encoding.
    pub default_char: Option<char>,
    /// Whether this is for unibyte buffers.
    pub for_unibyte: bool,
    /// Arbitrary property list for coding-system-get / coding-system-put.
    pub properties: HashMap<SymId, Value>,
    /// Integer property slots used by coding-system-get / coding-system-put.
    pub int_properties: HashMap<i64, Value>,
}

impl CodingSystemInfo {
    fn new(name: &str, coding_type: &str, mnemonic: char, eol_type: EolType) -> Self {
        Self {
            name: intern(name),
            coding_type: intern(coding_type),
            mnemonic,
            eol_type,
            ascii_compatible_p: false,
            charset_list: Vec::new(),
            post_read_conversion: None,
            pre_write_conversion: None,
            default_char: None,
            for_unibyte: false,
            properties: HashMap::new(),
            int_properties: HashMap::new(),
        }
    }

    /// Return the base name (strip -unix/-dos/-mac suffix).
    #[cfg(test)]
    fn base_name(&self) -> String {
        let name = resolve_sym(self.name);
        for suffix in &["-unix", "-dos", "-mac"] {
            if name.ends_with(suffix) {
                return name[..name.len() - suffix.len()].to_string();
            }
        }
        name.to_string()
    }
}

// ---------------------------------------------------------------------------
// CodingSystemManager
// ---------------------------------------------------------------------------

/// Central registry for all coding systems and their aliases.
pub struct CodingSystemManager {
    /// Registered coding systems, keyed by canonical name.
    pub systems: HashMap<SymId, CodingSystemInfo>,
    /// Alias -> canonical name mapping.
    pub aliases: HashMap<SymId, SymId>,
    /// Detection priority list (ordered list of system names).
    pub priority: Vec<SymId>,
    /// Current keyboard coding system.
    keyboard_coding: SymId,
    /// Current terminal coding system.
    terminal_coding: SymId,
}

impl CodingSystemManager {
    /// Create a new manager pre-populated with the standard coding systems.
    pub fn new() -> Self {
        let mut mgr = Self {
            systems: HashMap::new(),
            aliases: HashMap::new(),
            priority: Vec::new(),
            keyboard_coding: intern("utf-8-unix"),
            terminal_coding: intern("utf-8-unix"),
        };

        // Register standard coding systems
        mgr.register(CodingSystemInfo::new(
            "utf-8",
            "utf-8",
            'U',
            EolType::Undecided,
        ));
        mgr.register(CodingSystemInfo::new(
            "utf-8-unix",
            "utf-8",
            'U',
            EolType::Unix,
        ));
        mgr.register(CodingSystemInfo::new(
            "utf-8-dos",
            "utf-8",
            'U',
            EolType::Dos,
        ));
        mgr.register(CodingSystemInfo::new(
            "utf-8-mac",
            "utf-8",
            'U',
            EolType::Mac,
        ));
        mgr.register(CodingSystemInfo::new(
            "iso-latin-1",
            "charset",
            'l',
            EolType::Undecided,
        ));
        mgr.register(CodingSystemInfo::new(
            "iso-latin-1-unix",
            "charset",
            'l',
            EolType::Unix,
        ));
        mgr.register(CodingSystemInfo::new(
            "iso-latin-1-dos",
            "charset",
            'l',
            EolType::Dos,
        ));
        mgr.register(CodingSystemInfo::new(
            "iso-latin-1-mac",
            "charset",
            'l',
            EolType::Mac,
        ));
        mgr.register(CodingSystemInfo::new(
            "iso-latin-5",
            "charset",
            '9',
            EolType::Undecided,
        ));
        mgr.register(CodingSystemInfo::new(
            "iso-latin-5-unix",
            "charset",
            '9',
            EolType::Unix,
        ));
        mgr.register(CodingSystemInfo::new(
            "iso-latin-5-dos",
            "charset",
            '9',
            EolType::Dos,
        ));
        mgr.register(CodingSystemInfo::new(
            "iso-latin-5-mac",
            "charset",
            '9',
            EolType::Mac,
        ));
        mgr.register(CodingSystemInfo::new(
            "iso-latin-9",
            "charset",
            '0',
            EolType::Undecided,
        ));
        mgr.register(CodingSystemInfo::new(
            "iso-latin-9-unix",
            "charset",
            '0',
            EolType::Unix,
        ));
        mgr.register(CodingSystemInfo::new(
            "iso-latin-9-dos",
            "charset",
            '0',
            EolType::Dos,
        ));
        mgr.register(CodingSystemInfo::new(
            "iso-latin-9-mac",
            "charset",
            '0',
            EolType::Mac,
        ));
        mgr.register(CodingSystemInfo::new(
            "us-ascii",
            "charset",
            'A',
            EolType::Undecided,
        ));
        mgr.register(CodingSystemInfo::new(
            "us-ascii-unix",
            "charset",
            'A',
            EolType::Unix,
        ));
        mgr.register(CodingSystemInfo::new(
            "us-ascii-dos",
            "charset",
            'A',
            EolType::Dos,
        ));
        mgr.register(CodingSystemInfo::new(
            "us-ascii-mac",
            "charset",
            'A',
            EolType::Mac,
        ));
        mgr.register(CodingSystemInfo::new(
            "binary",
            "raw-text",
            '=',
            EolType::Unix,
        ));
        mgr.register(CodingSystemInfo::new(
            "raw-text",
            "raw-text",
            '=',
            EolType::Undecided,
        ));
        mgr.register(CodingSystemInfo::new(
            "raw-text-unix",
            "raw-text",
            '=',
            EolType::Unix,
        ));
        mgr.register(CodingSystemInfo::new(
            "raw-text-dos",
            "raw-text",
            '=',
            EolType::Dos,
        ));
        mgr.register(CodingSystemInfo::new(
            "raw-text-mac",
            "raw-text",
            '=',
            EolType::Mac,
        ));
        mgr.register(CodingSystemInfo::new(
            "undecided",
            "undecided",
            '-',
            EolType::Undecided,
        ));
        mgr.register(CodingSystemInfo::new(
            "undecided-unix",
            "undecided",
            '-',
            EolType::Unix,
        ));
        mgr.register(CodingSystemInfo::new(
            "undecided-dos",
            "undecided",
            '-',
            EolType::Dos,
        ));
        mgr.register(CodingSystemInfo::new(
            "undecided-mac",
            "undecided",
            '-',
            EolType::Mac,
        ));
        mgr.register(CodingSystemInfo::new(
            "emacs-internal",
            "utf-8",
            'U',
            EolType::Unix,
        ));
        mgr.register(CodingSystemInfo::new(
            "utf-8-emacs",
            "utf-8",
            'U',
            EolType::Undecided,
        ));
        mgr.register(CodingSystemInfo::new(
            "no-conversion",
            "raw-text",
            '=',
            EolType::Unix,
        ));
        mgr.register(CodingSystemInfo::new(
            "utf-8-auto",
            "utf-8",
            'U',
            EolType::Undecided,
        ));
        mgr.register(CodingSystemInfo::new(
            "utf-16",
            "utf-16",
            'U',
            EolType::Undecided,
        ));
        mgr.register(CodingSystemInfo::new(
            "utf-16-be",
            "utf-16",
            'U',
            EolType::Undecided,
        ));
        mgr.register(CodingSystemInfo::new(
            "utf-16-le",
            "utf-16",
            'U',
            EolType::Undecided,
        ));
        mgr.register(CodingSystemInfo::new(
            "utf-16be",
            "utf-16",
            'U',
            EolType::Undecided,
        ));
        mgr.register(CodingSystemInfo::new(
            "utf-16le",
            "utf-16",
            'U',
            EolType::Undecided,
        ));
        mgr.register(CodingSystemInfo::new(
            "utf-16be-with-signature",
            "utf-16",
            'U',
            EolType::Undecided,
        ));
        mgr.register(CodingSystemInfo::new(
            "utf-16le-with-signature",
            "utf-16",
            'U',
            EolType::Undecided,
        ));
        mgr.register(CodingSystemInfo::new(
            "prefer-utf-8",
            "undecided",
            '-',
            EolType::Undecided,
        ));
        for (name, eol) in [
            ("chinese-iso-8bit", EolType::Undecided),
            ("chinese-iso-8bit-unix", EolType::Unix),
            ("chinese-iso-8bit-dos", EolType::Dos),
            ("chinese-iso-8bit-mac", EolType::Mac),
        ] {
            let mut info = CodingSystemInfo::new(name, "iso-2022", 'c', eol);
            info.ascii_compatible_p = true;
            info.charset_list = vec![intern("ascii"), intern("chinese-gb2312")];
            mgr.register(info);
        }
        for (name, eol) in [
            ("chinese-big5", EolType::Undecided),
            ("chinese-big5-unix", EolType::Unix),
            ("chinese-big5-dos", EolType::Dos),
            ("chinese-big5-mac", EolType::Mac),
        ] {
            let mut info = CodingSystemInfo::new(name, "big5", 'B', eol);
            info.ascii_compatible_p = true;
            info.charset_list = vec![intern("ascii"), intern("big5")];
            mgr.register(info);
        }
        for (name, eol) in [
            ("chinese-big5-hkscs", EolType::Undecided),
            ("chinese-big5-hkscs-unix", EolType::Unix),
            ("chinese-big5-hkscs-dos", EolType::Dos),
            ("chinese-big5-hkscs-mac", EolType::Mac),
        ] {
            let mut info = CodingSystemInfo::new(name, "charset", 'B', eol);
            info.ascii_compatible_p = true;
            info.charset_list = vec![intern("ascii"), intern("big5-hkscs")];
            mgr.register(info);
        }

        // Common aliases
        mgr.add_alias("mule-utf-8", "utf-8");
        mgr.add_alias("cp65001", "utf-8");
        mgr.add_alias("iso-8859-1", "iso-latin-1");
        mgr.add_alias("latin-1", "iso-latin-1");
        mgr.add_alias("iso-8859-9", "iso-latin-5");
        mgr.add_alias("latin-5", "iso-latin-5");
        mgr.add_alias("iso-8859-15", "iso-latin-9");
        mgr.add_alias("latin-9", "iso-latin-9");
        mgr.add_alias("latin-0", "iso-latin-9");
        mgr.add_alias("ascii", "us-ascii");
        mgr.add_alias("iso-safe", "us-ascii");
        mgr.add_alias("cn-gb-2312", "chinese-iso-8bit");
        mgr.add_alias("euc-china", "chinese-iso-8bit");
        mgr.add_alias("euc-cn", "chinese-iso-8bit");
        mgr.add_alias("cn-gb", "chinese-iso-8bit");
        mgr.add_alias("gb2312", "chinese-iso-8bit");
        mgr.add_alias("big5", "chinese-big5");
        mgr.add_alias("cn-big5", "chinese-big5");
        mgr.add_alias("cp950", "chinese-big5");
        mgr.add_alias("big5-hkscs", "chinese-big5-hkscs");
        mgr.add_alias("cn-big5-hkscs", "chinese-big5-hkscs");

        // Default priority list
        mgr.priority = vec![
            intern("utf-8"),
            intern("utf-8-unix"),
            intern("undecided"),
            intern("iso-latin-1"),
            intern("us-ascii"),
            intern("raw-text"),
            intern("binary"),
            intern("no-conversion"),
        ];

        mgr
    }

    /// Register a coding system.
    fn register(&mut self, info: CodingSystemInfo) {
        self.systems.insert(info.name, info);
    }

    /// Resolve a name through the alias table to a canonical name.
    /// Returns either the input name (if it's a direct system) or the
    /// canonical name from the alias table.
    pub fn resolve(&self, name: &str) -> Option<SymId> {
        let name = lookup_interned(name)?;
        if self.systems.contains_key(&name) {
            Some(name)
        } else {
            self.aliases
                .get(&name)
                .copied()
                .filter(|canonical| self.systems.contains_key(canonical))
        }
    }

    /// Look up a coding system by name (resolving aliases).
    pub fn get(&self, name: &str) -> Option<&CodingSystemInfo> {
        let canonical = self.resolve(name)?;
        self.systems.get(&canonical)
    }

    /// Look up a coding system mutably by name (resolving aliases).
    pub fn get_mut(&mut self, name: &str) -> Option<&mut CodingSystemInfo> {
        let canonical = self.resolve(name)?;
        self.systems.get_mut(&canonical)
    }

    /// Check if a name is a known coding system (or alias).
    pub fn is_known(&self, name: &str) -> bool {
        self.resolve(name).is_some()
    }

    /// Check if a name is a known coding system, alias, or derived EOL variant.
    pub fn is_known_or_derived(&self, name: &str) -> bool {
        is_known_or_derived_coding_system(self, name)
    }

    /// Return the canonical runtime name for a coding system or alias.
    pub(crate) fn canonical_runtime_name(&self, name: &str) -> Option<String> {
        canonical_runtime_name(self, name)
    }

    /// Return the canonical coding system with EOL detected from file bytes.
    pub(crate) fn canonical_name_for_detected_eol(
        &self,
        name: &str,
        eol_suffix: &str,
    ) -> Option<String> {
        let normalized = normalize_coding_name_for_lookup(name);
        if EolType::from_suffix(normalized).is_some() {
            return canonical_runtime_name(self, normalized);
        }

        let eol = match eol_suffix {
            "-unix" => 0,
            "-dos" => 1,
            "-mac" => 2,
            _ => return canonical_runtime_name(self, normalized),
        };
        let canonical_base = self.resolve(normalized)?;
        derive_coding_for_eol(resolve_sym(canonical_base), eol)
            .or_else(|| canonical_runtime_name(self, normalized))
    }

    /// Add an alias mapping.
    pub fn add_alias(&mut self, alias: &str, target: &str) {
        self.aliases.insert(intern(alias), intern(target));
    }

    /// Get all aliases that point to a given canonical name.
    pub fn aliases_for(&self, canonical: SymId) -> Vec<SymId> {
        self.aliases
            .iter()
            .filter(|(_, v)| **v == canonical)
            .map(|(k, _)| *k)
            .collect()
    }

    /// List all registered coding system names (canonical only).
    pub fn list_all(&self) -> Vec<SymId> {
        let mut names: Vec<SymId> = self.systems.keys().copied().collect();
        names.sort_by(|left, right| resolve_sym(*left).cmp(resolve_sym(*right)));
        names
    }

    pub(crate) fn keyboard_coding_sym(&self) -> SymId {
        self.keyboard_coding
    }
    pub(crate) fn terminal_coding_sym(&self) -> SymId {
        self.terminal_coding
    }
    // pdump accessors
    pub(crate) fn dump_keyboard_coding(&self) -> &str {
        resolve_sym(self.keyboard_coding)
    }
    pub(crate) fn dump_terminal_coding(&self) -> &str {
        resolve_sym(self.terminal_coding)
    }
    pub(crate) fn dump_keyboard_coding_sym(&self) -> SymId {
        self.keyboard_coding
    }
    pub(crate) fn dump_terminal_coding_sym(&self) -> SymId {
        self.terminal_coding
    }
    pub(crate) fn from_dump(
        systems: HashMap<SymId, CodingSystemInfo>,
        aliases: HashMap<SymId, SymId>,
        priority: Vec<SymId>,
        keyboard_coding: SymId,
        terminal_coding: SymId,
    ) -> Self {
        Self {
            systems,
            aliases,
            priority,
            keyboard_coding,
            terminal_coding,
        }
    }

    /// Collect GC roots from coding system properties.
    pub fn trace_roots(&self, roots: &mut Vec<Value>) {
        for info in self.systems.values() {
            for value in info.properties.values() {
                roots.push(*value);
            }
            for value in info.int_properties.values() {
                roots.push(*value);
            }
        }
    }
}

impl crate::gc_trace::GcTrace for CodingSystemManager {
    fn trace_roots(&self, roots: &mut Vec<Value>) {
        CodingSystemManager::trace_roots(self, roots);
    }
}

fn property_lookup(info: &CodingSystemInfo, prop: SymId) -> Option<Value> {
    if let Some(value) = info.properties.get(&prop) {
        return Some(*value);
    }
    let prop_name = resolve_sym(prop);
    if !prop_name.starts_with(':') {
        let colon_key = intern(&format!(":{prop_name}"));
        return info.properties.get(&colon_key).copied();
    }
    None
}

fn plist_push_key(plist: &mut Vec<Value>, key: SymId, value: Value) {
    plist.push(Value::from_sym_id(key));
    plist.push(value);
}

fn first_emacs_char_code(value: Value) -> Option<i64> {
    match value.kind() {
        ValueKind::Fixnum(c) => Some(c),
        ValueKind::String => {
            let string = value.as_lisp_string()?;
            if string.is_empty() {
                return Some(0);
            }
            let (ch, _) = if string.is_multibyte() {
                super::emacs_char::string_char(string.as_bytes())
            } else {
                (string.as_bytes()[0] as u32, 1)
            };
            Some(ch as i64)
        }
        _ => None,
    }
}

impl Default for CodingSystemManager {
    fn default() -> Self {
        Self::new()
    }
}

// ===========================================================================
// Pure builtins
// ===========================================================================

/// `(coding-system-list &optional BASE-ONLY)` -- return a list of all coding systems.
/// If BASE-ONLY is non-nil, only return base systems (no -unix/-dos/-mac variants).
pub(crate) fn builtin_coding_system_list(
    mgr: &CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_max_args("coding-system-list", &args, 1)?;
    let base_only = args.first().is_some_and(|v| v.is_truthy());
    let names = mgr.list_all();
    let filtered: Vec<Value> = names
        .into_iter()
        .filter(|id| {
            let n = resolve_sym(*id);
            if base_only {
                !n.ends_with("-unix") && !n.ends_with("-dos") && !n.ends_with("-mac")
            } else {
                true
            }
        })
        .map(Value::symbol)
        .collect();
    Ok(Value::list(filtered))
}

/// `(coding-system-aliases CODING-SYSTEM)` -- return a list of aliases for a
/// coding system (including the name itself as the first element).
pub(crate) fn builtin_coding_system_aliases(
    mgr: &CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_args("coding-system-aliases", &args, 1)?;
    if args[0].is_string() {
        return Err(signal(
            "wrong-type-argument",
            vec![Value::symbol("symbolp"), args[0]],
        ));
    }
    let raw_name = coding_symbol_name(&args[0])?;
    let resolved_name = resolve_runtime_name(mgr, &raw_name)
        .ok_or_else(|| signal("coding-system-error", vec![args[0]]))?;
    let base = strip_eol_suffix(&resolved_name);

    if matches!(base, "binary" | "no-conversion") {
        return Ok(Value::list(vec![
            Value::symbol("no-conversion"),
            Value::symbol("binary"),
        ]));
    }

    let suffix = EolType::from_suffix(&resolved_name)
        .map(|eol| eol.suffix())
        .unwrap_or("");
    let canonical = runtime_bucket_name(mgr, &resolved_name)
        .ok_or_else(|| signal("coding-system-error", vec![args[0]]))?;
    let display = display_base_name(strip_eol_suffix(&resolved_name)).to_string();
    let canonical_id = mgr
        .resolve(&canonical)
        .ok_or_else(|| signal("coding-system-error", vec![args[0]]))?;
    let mut aliases = mgr.aliases_for(canonical_id);
    aliases.sort_by(|a, b| {
        alias_sort_rank(&canonical, resolve_sym(*a))
            .cmp(&alias_sort_rank(&canonical, resolve_sym(*b)))
            .then_with(|| resolve_sym(*a).cmp(resolve_sym(*b)))
    });
    let mut names = vec![format!("{display}{suffix}")];
    for alias in aliases {
        let alias = resolve_sym(alias);
        if alias != display {
            names.push(format!("{alias}{suffix}"));
        }
    }
    if canonical != display
        && !names
            .iter()
            .any(|name| name == &format!("{canonical}{suffix}"))
    {
        names.push(format!("{canonical}{suffix}"));
    }
    Ok(Value::list(names.into_iter().map(Value::symbol).collect()))
}

/// `(coding-system-get CODING-SYSTEM PROP)` -- get a property of a coding system.
/// Recognized built-in properties: :name, :type, :mnemonic, :eol-type.
/// Other properties are looked up from the per-system property list.
pub(crate) fn builtin_coding_system_get(mgr: &CodingSystemManager, args: Vec<Value>) -> EvalResult {
    expect_args("coding-system-get", &args, 2)?;
    let coding_name = coding_symbol_name(&args[0])?;
    let resolved_name = resolve_runtime_name(mgr, &coding_name)
        .ok_or_else(|| signal("coding-system-error", vec![args[0]]))?;
    let bucket = runtime_bucket_name(mgr, &resolved_name)
        .ok_or_else(|| signal("coding-system-error", vec![args[0]]))?;
    let info = mgr
        .get(&bucket)
        .ok_or_else(|| signal("coding-system-error", vec![args[0]]))?;

    if let Some(prop_id) = args[1].as_symbol_id() {
        let prop_name = resolve_sym(prop_id);
        if let Some(value) = property_lookup(info, prop_id) {
            return Ok(value);
        }
        return match prop_name {
            ":name" | "name" => Ok(Value::symbol(display_base_name(strip_eol_suffix(
                &resolved_name,
            )))),
            ":coding-type" | "coding-type" => Ok(Value::symbol(
                coding_type_for_base(strip_eol_suffix(&resolved_name))
                    .unwrap_or(resolve_sym(info.coding_type)),
            )),
            ":type" | "type" => Ok(Value::NIL),
            ":mnemonic" | "mnemonic" => Ok(Value::fixnum(
                default_mnemonic_for_base(strip_eol_suffix(&resolved_name))
                    .unwrap_or(info.mnemonic as i64),
            )),
            ":charset-list" | "charset-list" => Ok(Value::list(
                info.charset_list
                    .iter()
                    .copied()
                    .map(Value::from_sym_id)
                    .collect(),
            )),
            ":post-read-conversion" | "post-read-conversion" => Ok(info
                .post_read_conversion
                .map(Value::from_sym_id)
                .unwrap_or(Value::NIL)),
            ":pre-write-conversion" | "pre-write-conversion" => Ok(info
                .pre_write_conversion
                .map(Value::from_sym_id)
                .unwrap_or(Value::NIL)),
            ":eol-type" | "eol-type" => Ok(Value::NIL),
            _ => Ok(Value::NIL),
        };
    }

    if let Some(int_key) = args[1].as_int() {
        if let Some(value) = info.int_properties.get(&int_key) {
            return Ok(*value);
        }
    }

    Err(signal(
        "wrong-type-argument",
        vec![Value::symbol("symbolp"), args[1]],
    ))
}

fn plist_contains_key(plist: &[Value], key: &str) -> bool {
    let needle = key.trim_start_matches(':');
    let mut idx = 0;
    while idx + 1 < plist.len() {
        if plist[idx]
            .as_symbol_name()
            .is_some_and(|name| name.trim_start_matches(':') == needle)
        {
            return true;
        }
        idx += 2;
    }
    false
}

fn coding_category_for_base(base: &str) -> &'static str {
    match base {
        "utf-8" | "utf-8-emacs" | "utf-8-auto" | "emacs-internal" => "coding-category-utf-8",
        "latin-1" | "iso-8859-1" | "iso-latin-1" | "latin-5" | "iso-8859-9" | "iso-latin-5"
        | "latin-0" | "latin-9" | "iso-8859-15" | "iso-latin-9" | "ascii" | "us-ascii"
        | "cn-gb-2312" | "euc-china" | "euc-cn" | "cn-gb" | "gb2312" | "chinese-iso-8bit"
        | "big5" | "cn-big5" | "cp950" | "chinese-big5" | "big5-hkscs" | "cn-big5-hkscs"
        | "chinese-big5-hkscs" => "coding-category-charset",
        "raw-text" | "binary" | "no-conversion" => "coding-category-raw-text",
        "undecided" | "prefer-utf-8" => "coding-category-undecided",
        _ => "coding-category-undecided",
    }
}

fn coding_docstring_for_base(base: &str) -> Option<&'static str> {
    match base {
        "utf-8" | "utf-8-emacs" | "utf-8-auto" | "emacs-internal" => {
            Some("UTF-8 (no signature (BOM))")
        }
        "latin-1" | "iso-8859-1" | "iso-latin-1" => {
            Some("ISO 2022 based 8-bit encoding for Latin-1 (MIME:ISO-8859-1).")
        }
        "latin-5" | "iso-8859-9" | "iso-latin-5" => {
            Some("ISO 2022 based 8-bit encoding for Latin-5 (MIME:ISO-8859-9).")
        }
        "latin-0" | "latin-9" | "iso-8859-15" | "iso-latin-9" => {
            Some("ISO 2022 based 8-bit encoding for Latin-9 (MIME:ISO-8859-15).")
        }
        "cn-gb-2312" | "euc-china" | "euc-cn" | "cn-gb" | "gb2312" | "chinese-iso-8bit" => {
            Some("ISO 2022 based EUC encoding for Chinese GB2312 (MIME:GB2312).")
        }
        "big5" | "cn-big5" | "cp950" | "chinese-big5" => {
            Some("BIG5 8-bit encoding for Chinese (MIME:Big5)")
        }
        "big5-hkscs" | "cn-big5-hkscs" | "chinese-big5-hkscs" => {
            Some("BIG5-HKSCS 8-bit encoding for Chinese, Hong Kong supplement (MIME:Big5-HKSCS)")
        }
        "ascii" | "us-ascii" => Some("ASCII encoding."),
        "no-conversion" | "binary" | "raw-text" => Some("Do no conversion."),
        "undecided" => Some("Automatic conversion on decode."),
        _ => None,
    }
}

fn coding_charset_list_for_base(base: &str) -> Option<Vec<Value>> {
    match base {
        "utf-8" | "utf-8-emacs" | "utf-8-auto" | "emacs-internal" => {
            Some(vec![Value::symbol("unicode")])
        }
        "latin-1" | "iso-8859-1" | "iso-latin-1" => Some(vec![Value::symbol("iso-8859-1")]),
        "latin-5" | "iso-8859-9" | "iso-latin-5" => Some(vec![Value::symbol("iso-8859-9")]),
        "latin-0" | "latin-9" | "iso-8859-15" | "iso-latin-9" => {
            Some(vec![Value::symbol("iso-8859-15")])
        }
        "cn-gb-2312" | "euc-china" | "euc-cn" | "cn-gb" | "gb2312" | "chinese-iso-8bit" => {
            Some(vec![
                Value::symbol("ascii"),
                Value::symbol("chinese-gb2312"),
            ])
        }
        "big5" | "cn-big5" | "cp950" | "chinese-big5" => {
            Some(vec![Value::symbol("ascii"), Value::symbol("big5")])
        }
        "big5-hkscs" | "cn-big5-hkscs" | "chinese-big5-hkscs" => {
            Some(vec![Value::symbol("ascii"), Value::symbol("big5-hkscs")])
        }
        "ascii" | "us-ascii" => Some(vec![Value::symbol("ascii")]),
        _ => None,
    }
}

fn coding_mime_charset_for_base(base: &str) -> Option<&'static str> {
    match base {
        "utf-8" | "utf-8-emacs" | "utf-8-auto" | "emacs-internal" => Some("utf-8"),
        "latin-1" | "iso-8859-1" | "iso-latin-1" => Some("iso-8859-1"),
        "latin-5" | "iso-8859-9" | "iso-latin-5" => Some("iso-8859-9"),
        "latin-0" | "latin-9" | "iso-8859-15" | "iso-latin-9" => Some("iso-8859-15"),
        "cn-gb-2312" | "euc-china" | "euc-cn" | "cn-gb" | "gb2312" | "chinese-iso-8bit" => {
            Some("gb2312")
        }
        "big5" | "cn-big5" | "cp950" | "chinese-big5" => Some("big5"),
        "big5-hkscs" | "cn-big5-hkscs" | "chinese-big5-hkscs" => Some("big5-hkscs"),
        "ascii" | "us-ascii" => Some("us-ascii"),
        _ => None,
    }
}

/// `(coding-system-plist CODING-SYSTEM)` -- return a plist describing
/// CODING-SYSTEM metadata.
pub(crate) fn builtin_coding_system_plist(
    mgr: &CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_args("coding-system-plist", &args, 1)?;
    if args[0].is_string() {
        return Err(signal(
            "wrong-type-argument",
            vec![Value::symbol("symbolp"), args[0]],
        ));
    }

    let coding_name = coding_symbol_name(&args[0])?;
    let resolved_name = resolve_runtime_name(mgr, &coding_name)
        .ok_or_else(|| signal("coding-system-error", vec![args[0]]))?;
    let bucket = runtime_bucket_name(mgr, &resolved_name)
        .ok_or_else(|| signal("coding-system-error", vec![args[0]]))?;
    let info = mgr
        .get(&bucket)
        .ok_or_else(|| signal("coding-system-error", vec![args[0]]))?;

    let base = strip_eol_suffix(&resolved_name);
    let display_name = display_base_name(base);
    let coding_type = coding_type_for_base(base).unwrap_or(resolve_sym(info.coding_type));
    let mnemonic = default_mnemonic_for_base(base).unwrap_or(info.mnemonic as i64);

    let mut plist = Vec::new();
    plist_push_key(&mut plist, intern(":ascii-compatible-p"), Value::T);
    plist_push_key(
        &mut plist,
        intern(":category"),
        Value::symbol(coding_category_for_base(base)),
    );
    plist_push_key(&mut plist, intern(":name"), Value::symbol(display_name));
    if let Some(doc) = coding_docstring_for_base(base) {
        plist_push_key(&mut plist, intern(":docstring"), Value::string(doc));
    }
    plist_push_key(
        &mut plist,
        intern(":coding-type"),
        Value::symbol(coding_type),
    );
    plist_push_key(&mut plist, intern(":mnemonic"), Value::fixnum(mnemonic));
    if let Some(charset_list) = coding_charset_list_for_base(base).or_else(|| {
        (!info.charset_list.is_empty()).then(|| {
            info.charset_list
                .iter()
                .copied()
                .map(Value::from_sym_id)
                .collect()
        })
    }) {
        plist_push_key(
            &mut plist,
            intern(":charset-list"),
            Value::list(charset_list),
        );
    }
    if let Some(mime_charset) = coding_mime_charset_for_base(base) {
        plist_push_key(
            &mut plist,
            intern(":mime-charset"),
            Value::symbol(mime_charset),
        );
    }
    if let Some(post_read_conversion) = info.post_read_conversion {
        plist_push_key(
            &mut plist,
            intern(":post-read-conversion"),
            Value::from_sym_id(post_read_conversion),
        );
    }
    if let Some(pre_write_conversion) = info.pre_write_conversion {
        plist_push_key(
            &mut plist,
            intern(":pre-write-conversion"),
            Value::from_sym_id(pre_write_conversion),
        );
    }
    if matches!(base, "no-conversion" | "binary" | "raw-text") {
        plist_push_key(&mut plist, intern(":default-char"), Value::fixnum(0));
        plist_push_key(&mut plist, intern(":for-unibyte"), Value::T);
    }

    // Preserve caller-provided custom properties from coding-system-put.
    let mut custom_keys: Vec<SymId> = info.properties.keys().copied().collect();
    custom_keys.sort_by(|left, right| resolve_sym(*left).cmp(resolve_sym(*right)));
    for key in custom_keys {
        let key_name = resolve_sym(key);
        if !plist_contains_key(&plist, key_name) {
            if let Some(value) = info.properties.get(&key) {
                plist_push_key(&mut plist, key, *value);
            }
        }
    }

    Ok(Value::list(plist))
}

/// `(coding-system-put CODING-SYSTEM PROP VAL)` -- set a property of a coding system.
pub(crate) fn builtin_coding_system_put(
    mgr: &mut CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_args("coding-system-put", &args, 3)?;
    let val = args[2];

    if args[0].is_nil() {
        return Err(signal(
            "wrong-type-argument",
            vec![Value::symbol("coding-system-p"), Value::NIL],
        ));
    }

    let coding_name = coding_symbol_name(&args[0])?;
    let resolved_name = resolve_runtime_name(mgr, &coding_name)
        .ok_or_else(|| signal("coding-system-error", vec![args[0]]))?;
    let bucket = runtime_bucket_name(mgr, &resolved_name)
        .ok_or_else(|| signal("coding-system-error", vec![args[0]]))?;
    let info = mgr
        .get_mut(&bucket)
        .ok_or_else(|| signal("coding-system-error", vec![args[0]]))?;

    if let Some(prop_id) = args[1].as_symbol_id() {
        let prop_name = resolve_sym(prop_id);
        if matches!(prop_name, ":mnemonic" | "mnemonic") {
            let Some(code) = first_emacs_char_code(val) else {
                return Err(signal(
                    "wrong-type-argument",
                    vec![Value::symbol("characterp"), val],
                ));
            };
            let coerced = Value::fixnum(code);
            info.properties.insert(prop_id, coerced);
            return Ok(coerced);
        }
        info.properties.insert(prop_id, val);
        return Ok(val);
    }

    if let Some(int_key) = args[1].as_int() {
        info.int_properties.insert(int_key, val);
        return Ok(val);
    }

    Ok(val)
}

/// `(coding-system-base CODING-SYSTEM)` -- return the base coding system
/// (stripping -unix, -dos, -mac suffixes).
pub(crate) fn builtin_coding_system_base(
    mgr: &CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_args("coding-system-base", &args, 1)?;
    let name = coding_symbol_name(&args[0])?;
    let resolved_name = resolve_runtime_name(mgr, &name)
        .ok_or_else(|| signal("coding-system-error", vec![args[0]]))?;
    Ok(Value::symbol(display_base_name(strip_eol_suffix(
        &resolved_name,
    ))))
}

/// `(coding-system-eol-type CODING-SYSTEM)` -- return the EOL type.
/// Returns 0 (unix), 1 (dos), 2 (mac), or a vector of three sub-coding-systems
/// if the EOL type is undecided.
pub(crate) fn builtin_coding_system_eol_type(
    mgr: &CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_args("coding-system-eol-type", &args, 1)?;
    let Some(name) = args[0].as_symbol_name() else {
        return Ok(Value::NIL);
    };
    let resolved_name = match resolve_runtime_name(mgr, name) {
        Some(resolved) => resolved,
        None => return Ok(Value::NIL),
    };
    if let Some(eol) = EolType::from_suffix(&resolved_name) {
        return Ok(Value::fixnum(eol.to_int()));
    }
    let bucket = match runtime_bucket_name(mgr, &resolved_name) {
        Some(bucket) => bucket,
        None => return Ok(Value::NIL),
    };
    let Some(info) = mgr.get(&bucket) else {
        return Ok(Value::NIL);
    };

    match info.eol_type {
        EolType::Unix => Ok(Value::fixnum(0)),
        EolType::Dos => Ok(Value::fixnum(1)),
        EolType::Mac => Ok(Value::fixnum(2)),
        EolType::Undecided => {
            // Return [base-unix base-dos base-mac] using Emacs display base names.
            let base = eol_vector_base(strip_eol_suffix(&resolved_name));
            let vec = vec![
                Value::symbol(format!("{base}-unix")),
                Value::symbol(format!("{base}-dos")),
                Value::symbol(format!("{base}-mac")),
            ];
            Ok(Value::vector(vec))
        }
    }
}

/// `(coding-system-type CODING-SYSTEM)` -- return the type symbol of the
/// coding system (e.g. utf-8, charset, raw-text, undecided).
pub(crate) fn builtin_coding_system_type(
    mgr: &CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_args("coding-system-type", &args, 1)?;
    let name = coding_symbol_name(&args[0])?;
    let resolved_name = resolve_runtime_name(mgr, &name)
        .ok_or_else(|| signal("coding-system-error", vec![args[0]]))?;
    let base = strip_eol_suffix(&resolved_name);
    if let Some(kind) = coding_type_for_base(base) {
        return Ok(Value::symbol(kind));
    }
    let bucket = runtime_bucket_name(mgr, &resolved_name)
        .ok_or_else(|| signal("coding-system-error", vec![args[0]]))?;
    let info = mgr
        .get(&bucket)
        .ok_or_else(|| signal("coding-system-error", vec![args[0]]))?;
    Ok(Value::from_sym_id(info.coding_type))
}

/// `(coding-system-change-eol-conversion CODING-SYSTEM EOL-TYPE)` -- return
/// a coding system derived from CODING-SYSTEM but with a different EOL type.
/// EOL-TYPE is 0 (unix), 1 (dos), or 2 (mac), or a symbol.
pub(crate) fn builtin_coding_system_change_eol_conversion(
    mgr: &CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_args("coding-system-change-eol-conversion", &args, 2)?;
    if args[0].is_string() {
        return Err(signal(
            "wrong-type-argument",
            vec![Value::symbol("symbolp"), args[0]],
        ));
    }
    let raw_name = coding_symbol_name(&args[0])?;
    let is_nil_coding = raw_name == "nil";
    if !is_nil_coding && resolve_runtime_name(mgr, &raw_name).is_none() {
        return Err(signal("coding-system-error", vec![args[0]]));
    }
    let canonical_name = if is_nil_coding {
        "no-conversion".to_string()
    } else {
        canonical_runtime_name(mgr, &raw_name)
            .ok_or_else(|| signal("coding-system-error", vec![args[0]]))?
    };
    let canonical_base = strip_eol_suffix(&canonical_name);
    let resolved_base = canonical_base;
    let no_conversion_family = is_nil_coding || matches!(resolved_base, "no-conversion" | "binary");

    if no_conversion_family {
        let out = match args[1].kind() {
            ValueKind::Nil => Value::symbol("no-conversion"),
            ValueKind::Fixnum(n) => {
                if n == 0 {
                    if is_nil_coding {
                        Value::NIL
                    } else if resolved_base == "binary" {
                        Value::symbol("binary")
                    } else {
                        Value::symbol("no-conversion")
                    }
                } else {
                    Value::NIL
                }
            }
            ValueKind::Float => {
                if args[1].xfloat() == 0.0 {
                    if is_nil_coding {
                        Value::NIL
                    } else if resolved_base == "binary" {
                        Value::symbol("binary")
                    } else {
                        Value::symbol("no-conversion")
                    }
                } else {
                    Value::NIL
                }
            }
            ValueKind::Symbol(id) if resolve_sym(id) == "unix" => {
                if is_nil_coding {
                    Value::NIL
                } else if resolved_base == "binary" {
                    Value::symbol("binary")
                } else {
                    Value::symbol("no-conversion")
                }
            }
            ValueKind::Symbol(id)
                if {
                    let n = resolve_sym(id);
                    n == "dos" || n == "mac"
                } =>
            {
                Value::NIL
            }
            _ => {
                return Err(signal(
                    "wrong-type-argument",
                    vec![Value::symbol("number-or-marker-p"), args[1]],
                ));
            }
        };
        return Ok(out);
    }

    let target_eol = match args[1].kind() {
        ValueKind::Nil => None,
        ValueKind::Fixnum(n) => Some(n),
        ValueKind::Symbol(id) if resolve_sym(id) == "unix" => Some(0),
        ValueKind::Symbol(id) if resolve_sym(id) == "dos" => Some(1),
        ValueKind::Symbol(id) if resolve_sym(id) == "mac" => Some(2),
        _ => {
            return Err(signal(
                "wrong-type-argument",
                vec![Value::symbol("fixnump"), args[1]],
            ));
        }
    };

    let raw_base = strip_eol_suffix(&raw_name);
    if target_eol.is_none() {
        if EolType::from_suffix(&raw_name).is_some() {
            return Ok(Value::symbol(display_base_name(canonical_base)));
        }
        if canonical_base == "emacs-internal" {
            return Ok(Value::symbol("utf-8-emacs"));
        }
        return Ok(Value::symbol(raw_name));
    }

    let eol = target_eol.expect("checked above");
    if !(0..=2).contains(&eol) {
        let vec_base = eol_vector_base(resolved_base);
        let variants = vec![
            Value::symbol(format!("{vec_base}-unix")),
            Value::symbol(format!("{vec_base}-dos")),
            Value::symbol(format!("{vec_base}-mac")),
        ];
        return Err(signal(
            "args-out-of-range",
            vec![Value::vector(variants), Value::fixnum(eol)],
        ));
    }

    if let Some(derived) = derive_coding_for_eol(canonical_base, eol) {
        Ok(Value::symbol(derived))
    } else {
        Ok(Value::NIL)
    }
}

/// `(coding-system-change-text-conversion CODING-SYSTEM TEXT-CODING)` -- return
/// a coding system derived from TEXT-CODING but preserving the EOL type of
/// CODING-SYSTEM.
pub(crate) fn builtin_coding_system_change_text_conversion(
    mgr: &CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_args("coding-system-change-text-conversion", &args, 2)?;
    let first_eol = match args[0].kind() {
        ValueKind::Nil => Some(0),
        ValueKind::String => None,
        _ => match args[0].as_symbol_name() {
            Some(name) if name == "nil" => Some(0),
            Some(name) => {
                if let Some(resolved) = resolve_runtime_name(mgr, name) {
                    if let Some(eol) = EolType::from_suffix(&resolved) {
                        Some(eol.to_int())
                    } else if let Some(info) = mgr.get(&resolved) {
                        match info.eol_type {
                            EolType::Unix => Some(0),
                            EolType::Dos => Some(1),
                            EolType::Mac => Some(2),
                            EolType::Undecided => None,
                        }
                    } else {
                        None
                    }
                } else {
                    None
                }
            }
            None => None,
        },
    };

    let text_raw = coding_symbol_name(&args[1])?;
    let text_name = if text_raw == "nil" {
        "undecided".to_string()
    } else {
        text_raw.clone()
    };
    let resolved_text = resolve_runtime_name(mgr, &text_name)
        .ok_or_else(|| signal("coding-system-error", vec![args[1]]))?;
    let resolved_text_base = strip_eol_suffix(&resolved_text);

    if let Some(eol) = first_eol {
        if let Some(derived) = derive_coding_for_eol(resolved_text_base, eol) {
            return Ok(Value::symbol(derived));
        }
        return Ok(Value::NIL);
    }

    if EolType::from_suffix(&text_name).is_some() {
        return Ok(Value::symbol(display_base_name(strip_eol_suffix(
            &text_name,
        ))));
    }

    match text_name.as_str() {
        "binary" => Ok(Value::symbol("no-conversion")),
        "emacs-internal" => Ok(Value::symbol("utf-8-emacs")),
        _ => Ok(Value::symbol(text_name)),
    }
}

/// `(coding-system-p OBJECT)` -- return t when OBJECT names a known coding
/// system or alias, nil otherwise.
pub(crate) fn builtin_coding_system_p(mgr: &CodingSystemManager, args: Vec<Value>) -> EvalResult {
    expect_args("coding-system-p", &args, 1)?;
    let known = match args[0].as_symbol_name() {
        Some(name) if name == "nil" => true,
        Some(name) => is_known_or_derived_coding_system(mgr, name),
        None => false,
    };
    Ok(Value::bool_val(known))
}

/// `(check-coding-system CODING-SYSTEM)` -- validate CODING-SYSTEM.
/// Returns CODING-SYSTEM when valid, nil for nil, and signals on invalid input.
pub(crate) fn builtin_check_coding_system(
    mgr: &CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_args("check-coding-system", &args, 1)?;
    match args[0].kind() {
        ValueKind::Nil => Ok(Value::NIL),
        ValueKind::Symbol(id) => {
            if is_known_or_derived_coding_system(mgr, resolve_sym(id)) {
                Ok(args[0])
            } else {
                Err(signal("coding-system-error", vec![args[0]]))
            }
        }
        _ => Err(signal(
            "wrong-type-argument",
            vec![Value::symbol("symbolp"), args[0]],
        )),
    }
}

/// `(check-coding-systems-region START END CODING-SYSTEMS)` -- compatibility
/// helper that currently performs argument shape checks and returns nil.
pub(crate) fn builtin_check_coding_systems_region(
    _mgr: &CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_args("check-coding-systems-region", &args, 3)?;
    expect_integer_or_marker(&args[1])?;
    Ok(Value::NIL)
}

/// `(find-coding-system CODING-SYSTEM)` -- resolve CODING-SYSTEM to a known
/// canonical symbol, or return nil when unknown.
#[cfg(test)]
pub(crate) fn builtin_find_coding_system(
    mgr: &CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_args("find-coding-system", &args, 1)?;
    let name = coding_system_name(&args[0])?;
    if name == "nil" {
        return Ok(Value::NIL);
    }
    match mgr.resolve(&name) {
        Some(canonical) => Ok(Value::symbol(canonical)),
        None => Ok(Value::NIL),
    }
}

/// `(define-coding-system-internal NAME MNEMONIC CODING-TYPE CHARSET-LIST
///    ASCII-COMPAT DECODE-TL ENCODE-TL POST-READ PRE-WRITE DEFAULT-CHAR
///    FOR-UNIBYTE PLIST EOL-TYPE &rest TYPE-SPECIFIC-ATTRS)`
///
/// Internal entry point for registering a coding system.
/// Called by the `define-coding-system` macro in mule.el with ≥13 positional args.
pub(crate) fn builtin_define_coding_system_internal(
    mgr: &mut CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_min_args("define-coding-system-internal", &args, 13)?;

    // arg[0]: name (symbol)
    let name = coding_symbol_name_required(&args[0])?;

    // arg[1]: mnemonic (char)
    let mnemonic = match args[1].kind() {
        ValueKind::Fixnum(c) => super::builtins::character_code_to_rust_char(c).unwrap_or('?'),
        _ => '?',
    };

    // arg[2]: coding-type (symbol)
    let coding_type = match args[2].kind() {
        ValueKind::Symbol(id) => id,
        _ => intern("undecided"),
    };

    // arg[3]: charset-list (list of symbols, or special symbol like 'iso-2022)
    let charset_list = match args[3].kind() {
        ValueKind::Symbol(id) => vec![id],
        _ => {
            if let Some(items) = super::value::list_to_vec(&args[3]) {
                items.iter().filter_map(|v| v.as_symbol_id()).collect()
            } else {
                Vec::new()
            }
        }
    };

    // arg[4]: ascii-compatible-p
    let ascii_compatible_p = args[4].is_truthy();

    // arg[5]: decode-translation-table (ignored for now)
    // arg[6]: encode-translation-table (ignored for now)

    // arg[7]: post-read-conversion
    let post_read_conversion = match args[7].kind() {
        ValueKind::Symbol(id) if resolve_sym(id) != "nil" => Some(id),
        _ => None,
    };

    // arg[8]: pre-write-conversion
    let pre_write_conversion = match args[8].kind() {
        ValueKind::Symbol(id) if resolve_sym(id) != "nil" => Some(id),
        _ => None,
    };

    // arg[9]: default-char
    let default_char = match args[9].kind() {
        ValueKind::Fixnum(c) => char::from_u32(c as u32),
        _ => None,
    };

    // arg[10]: for-unibyte
    let for_unibyte = args[10].is_truthy();

    // arg[11]: plist
    let mut properties = HashMap::new();
    if let Some(items) = super::value::list_to_vec(&args[11]) {
        let mut i = 0;
        while i + 1 < items.len() {
            if let Some(key) = items[i].as_symbol_id() {
                properties.insert(key, items[i + 1]);
            }
            i += 2;
        }
    }

    // arg[12]: eol-type (symbol: unix/dos/mac, or vector for auto-detect)
    let eol_type = match args[12].kind() {
        ValueKind::Symbol(id) => {
            let s = resolve_sym(id);
            match s {
                "unix" => EolType::Unix,
                "dos" => EolType::Dos,
                "mac" => EolType::Mac,
                _ => EolType::Undecided,
            }
        }
        _ => EolType::Undecided,
    };

    // Build the base coding system info.
    let mut info =
        CodingSystemInfo::new(&name, resolve_sym(coding_type), mnemonic, eol_type.clone());
    info.ascii_compatible_p = ascii_compatible_p;
    info.charset_list = charset_list;
    info.post_read_conversion = post_read_conversion;
    info.pre_write_conversion = pre_write_conversion;
    info.default_char = default_char;
    info.for_unibyte = for_unibyte;
    info.properties = properties;

    // Register the base coding system.
    mgr.register(info);

    // Auto-create EOL variants (-unix, -dos, -mac) unless the eol_type
    // is already specific or the name already has an EOL suffix.
    if matches!(eol_type, EolType::Undecided) && EolType::from_suffix(&name).is_none() {
        for (suffix, et) in [
            ("-unix", EolType::Unix),
            ("-dos", EolType::Dos),
            ("-mac", EolType::Mac),
        ] {
            let variant_name = format!("{name}{suffix}");
            if !mgr.is_known(&variant_name) {
                let variant =
                    CodingSystemInfo::new(&variant_name, resolve_sym(coding_type), mnemonic, et);
                mgr.register(variant);
            }
        }
    }

    Ok(Value::NIL)
}

/// Extract a coding system name from a symbol argument, signaling on non-symbol.
fn coding_symbol_name_required(val: &Value) -> Result<String, Flow> {
    match val.kind() {
        ValueKind::Symbol(id) => Ok(resolve_sym(id).to_owned()),
        _ => Err(signal(
            "wrong-type-argument",
            vec![Value::symbol("symbolp"), *val],
        )),
    }
}

/// `(define-coding-system-alias ALIAS CODING-SYSTEM)` -- register ALIAS for
/// CODING-SYSTEM and return nil.
pub(crate) fn builtin_define_coding_system_alias(
    mgr: &mut CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_args("define-coding-system-alias", &args, 2)?;

    let alias = match args[0].kind() {
        ValueKind::Symbol(id) => resolve_sym(id).to_owned(),
        ValueKind::Nil => "nil".to_string(),
        _ => {
            return Err(signal(
                "wrong-type-argument",
                vec![Value::symbol("symbolp"), args[0]],
            ));
        }
    };

    let target = match args[1].kind() {
        ValueKind::Symbol(id) => resolve_sym(id).to_owned(),
        ValueKind::Nil => {
            return Err(signal(
                "wrong-type-argument",
                vec![Value::symbol("coding-system-p"), Value::NIL],
            ));
        }
        _ => {
            return Err(signal(
                "wrong-type-argument",
                vec![Value::symbol("symbolp"), args[1]],
            ));
        }
    };

    let canonical = mgr
        .resolve(&target)
        .ok_or_else(|| signal("coding-system-error", vec![Value::symbol(&target)]))?;
    mgr.add_alias(&alias, resolve_sym(canonical));
    Ok(Value::NIL)
}

/// `(set-coding-system-priority &rest CODING-SYSTEMS)` -- move CODING-SYSTEMS
/// to the front of the detection priority list in order, keeping relative order
/// of the remaining systems.
pub(crate) fn builtin_set_coding_system_priority(
    mgr: &mut CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    if args.is_empty() {
        return Ok(Value::NIL);
    }

    let mut requested: Vec<(String, String)> = Vec::with_capacity(args.len());
    for arg in &args {
        if arg.is_nil() {
            return Err(signal(
                "wrong-type-argument",
                vec![Value::symbol("coding-system-p"), Value::NIL],
            ));
        }
        let Some(name) = arg.as_symbol_name().map(|s| s.to_string()) else {
            return Err(signal(
                "wrong-type-argument",
                vec![Value::symbol("symbolp"), *arg],
            ));
        };
        let resolved = resolve_runtime_name(mgr, &name)
            .ok_or_else(|| signal("coding-system-error", vec![*arg]))?;
        let canonical = mgr
            .resolve(&resolved)
            .map(|id| resolve_sym(id).to_string())
            .unwrap_or(resolved.clone());
        requested.push((name, canonical));
    }

    let mut seen_canonicals: HashSet<SymId> =
        HashSet::with_capacity(mgr.priority.len() + requested.len());
    let mut reordered: Vec<SymId> = Vec::with_capacity(mgr.priority.len() + requested.len());

    for (display, canonical) in requested {
        if let Some(display_id) = lookup_interned(&display)
            && let Some(canonical_id) = lookup_interned(&canonical)
            && seen_canonicals.insert(canonical_id)
        {
            reordered.push(display_id);
        }
    }

    for &name in &mgr.priority {
        let canonical = mgr.resolve(resolve_sym(name)).unwrap_or(name);
        if seen_canonicals.insert(canonical) {
            reordered.push(name);
        }
    }

    mgr.priority = reordered;
    Ok(Value::NIL)
}

/// `(detect-coding-string STRING &optional HIGHEST)` -- detect the encoding of
/// a string. Since all strings in this runtime are UTF-8, always returns utf-8.
/// If HIGHEST is non-nil, return a single coding system; otherwise return a list.
pub(crate) fn builtin_detect_coding_string(
    _mgr: &CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_min_args("detect-coding-string", &args, 1)?;
    expect_max_args("detect-coding-string", &args, 2)?;
    match args[0].kind() {
        ValueKind::String => {}
        _ => {
            return Err(signal(
                "wrong-type-argument",
                vec![Value::symbol("stringp"), args[0]],
            ));
        }
    }
    let highest = args.get(1).is_some_and(|v| v.is_truthy());
    if highest {
        Ok(Value::symbol("undecided"))
    } else {
        Ok(Value::list(vec![Value::symbol("undecided")]))
    }
}

/// `(detect-coding-region START END &optional HIGHEST)` -- detect the encoding
/// of a buffer region. Stub: always returns utf-8.
pub(crate) fn builtin_detect_coding_region(
    _mgr: &CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_min_args("detect-coding-region", &args, 2)?;
    expect_max_args("detect-coding-region", &args, 3)?;
    expect_integer_or_marker(&args[0])?;
    expect_integer_or_marker(&args[1])?;
    let highest = args.get(2).is_some_and(|v| v.is_truthy());
    if highest {
        Ok(Value::symbol("undecided"))
    } else {
        Ok(Value::list(vec![Value::symbol("undecided")]))
    }
}

/// `(keyboard-coding-system &optional TERMINAL)` -- return the current
/// keyboard coding system. The TERMINAL argument is ignored.
pub(crate) fn builtin_keyboard_coding_system(
    mgr: &CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_max_args("keyboard-coding-system", &args, 1)?;
    Ok(Value::symbol(mgr.keyboard_coding))
}

/// `(terminal-coding-system &optional TERMINAL)` -- return the current
/// terminal coding system. The TERMINAL argument is ignored.
pub(crate) fn builtin_terminal_coding_system(
    mgr: &CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_max_args("terminal-coding-system", &args, 1)?;
    Ok(Value::symbol(mgr.terminal_coding))
}

/// `(set-keyboard-coding-system CODING-SYSTEM &optional TERMINAL)` -- set the
/// keyboard coding system. Stub: records the value but has no functional effect.
pub(crate) fn builtin_set_keyboard_coding_system(
    mgr: &mut CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_min_args("set-keyboard-coding-system", &args, 1)?;
    expect_max_args("set-keyboard-coding-system", &args, 2)?;
    if args[0].is_nil() {
        mgr.keyboard_coding = intern("no-conversion");
        return Ok(Value::NIL);
    }
    let Some(name) = args[0].as_symbol_name().map(|s| s.to_string()) else {
        return Err(signal(
            "wrong-type-argument",
            vec![Value::symbol("symbolp"), args[0]],
        ));
    };
    if !is_known_or_derived_coding_system(mgr, &name) {
        return Err(signal("coding-system-error", vec![args[0]]));
    }
    let normalization_input = if matches!(EolType::from_suffix(&name), Some(EolType::Unix)) {
        name.clone()
    } else {
        canonical_runtime_name(mgr, &name)
            .ok_or_else(|| signal("coding-system-error", vec![args[0]]))?
    };
    let base = strip_eol_suffix(&normalization_input);
    if matches!(base, "utf-8-auto" | "prefer-utf-8") {
        return Err(signal(
            "error",
            vec![Value::string(format!(
                "Unsuitable coding system for keyboard: {name}"
            ))],
        ));
    }
    if base == "undecided" {
        return Err(signal(
            "error",
            vec![Value::string(format!(
                "Unsupported coding system for keyboard: {normalization_input}"
            ))],
        ));
    }
    let normalized = normalize_keyboard_coding_system(&normalization_input);
    mgr.keyboard_coding = intern(&normalized);
    Ok(Value::symbol(mgr.keyboard_coding))
}

/// `(set-terminal-coding-system CODING-SYSTEM &optional TERMINAL)` -- set the
/// terminal coding system. Stub: records the value but has no functional effect.
pub(crate) fn builtin_set_terminal_coding_system(
    mgr: &mut CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_min_args("set-terminal-coding-system", &args, 1)?;
    expect_max_args("set-terminal-coding-system", &args, 3)?;
    if args[0].is_nil() {
        mgr.terminal_coding = intern("nil");
        return Ok(Value::NIL);
    }
    let Some(name) = args[0].as_symbol_name().map(|s| s.to_string()) else {
        return Err(signal(
            "wrong-type-argument",
            vec![Value::symbol("symbolp"), args[0]],
        ));
    };
    if !is_known_or_derived_coding_system(mgr, &name) {
        return Err(signal("coding-system-error", vec![args[0]]));
    }
    mgr.terminal_coding = intern(&name);
    Ok(Value::NIL)
}

/// `(set-keyboard-coding-system-internal CODING-SYSTEM &optional TERMINAL)` --
/// internal keyboard coding setter. Mirrors the surface validation but always
/// returns nil.
pub(crate) fn builtin_set_keyboard_coding_system_internal(
    mgr: &mut CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_min_args("set-keyboard-coding-system-internal", &args, 1)?;
    expect_max_args("set-keyboard-coding-system-internal", &args, 2)?;
    let _ = builtin_set_keyboard_coding_system(mgr, args)?;
    Ok(Value::NIL)
}

/// `(set-terminal-coding-system-internal CODING-SYSTEM &optional TERMINAL)` --
/// internal terminal coding setter.
pub(crate) fn builtin_set_terminal_coding_system_internal(
    mgr: &mut CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_min_args("set-terminal-coding-system-internal", &args, 1)?;
    expect_max_args("set-terminal-coding-system-internal", &args, 2)?;
    let _ = builtin_set_terminal_coding_system(mgr, args)?;
    Ok(Value::NIL)
}

/// `(set-safe-terminal-coding-system-internal CODING-SYSTEM)` -- internal safe
/// terminal coding setter.
pub(crate) fn builtin_set_safe_terminal_coding_system_internal(
    mgr: &mut CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_args("set-safe-terminal-coding-system-internal", &args, 1)?;
    let _ = builtin_set_terminal_coding_system(mgr, args)?;
    Ok(Value::NIL)
}

/// `(text-quoting-style)` -- return the current effective text quoting style.
///
/// Mirrors GNU `Ftext_quoting_style` (`src/doc.c:652-678`):
///   - If `text-quoting-style' is `grave', `straight', or `curve', return it.
///   - If nil (the default), return `grave' when curved quotes cannot be
///     displayed, otherwise `curve'.
///   - Any other value is treated as `curve'.
///   - Never returns nil.
///
/// The display-capability fallback (GNU's `default_to_grave_quoting_style')
/// is currently a stub that always picks `curve' — neomacs does not yet
/// query the active display table for curved-quote support. This matches
/// GNU's behavior on a graphical/UTF-8 terminal.
pub(crate) fn builtin_text_quoting_style(
    eval: &super::eval::Context,
    args: Vec<Value>,
) -> EvalResult {
    expect_args("text-quoting-style", &args, 0)?;
    let var = eval
        .obarray
        .symbol_value("text-quoting-style")
        .copied()
        .unwrap_or(Value::NIL);
    if var.is_nil() {
        // GNU `default_to_grave_quoting_style' inspects the standard
        // display table to decide whether curved quotes are renderable.
        // Stub: always pick `curve'. Bringing in real display-capability
        // detection is a separate task.
        return Ok(Value::symbol("curve"));
    }
    if let Some(name) = var.as_symbol_name() {
        match name {
            "grave" | "straight" | "curve" => return Ok(Value::symbol(name)),
            _ => {}
        }
    }
    Ok(Value::symbol("curve"))
}

/// `(set-text-conversion-style STYLE &optional WHERE)` -- set conversion style.
/// NeoVM currently accepts all values and returns nil.
pub(crate) fn builtin_set_text_conversion_style(args: Vec<Value>) -> EvalResult {
    expect_min_args("set-text-conversion-style", &args, 1)?;
    expect_max_args("set-text-conversion-style", &args, 2)?;
    Ok(Value::NIL)
}

/// `(coding-system-priority-list &optional HIGHESTP)` -- return the current
/// priority list of coding systems for detection. If HIGHESTP is non-nil,
/// return only the highest-priority system.
pub(crate) fn builtin_coding_system_priority_list(
    mgr: &CodingSystemManager,
    args: Vec<Value>,
) -> EvalResult {
    expect_max_args("coding-system-priority-list", &args, 1)?;
    let highest_only = args.first().is_some_and(|v| v.is_truthy());
    if highest_only {
        if let Some(first) = mgr.priority.first() {
            Ok(Value::list(vec![Value::symbol(*first)]))
        } else {
            Ok(Value::NIL)
        }
    } else {
        let items: Vec<Value> = mgr.priority.iter().map(|id| Value::symbol(*id)).collect();
        Ok(Value::list(items))
    }
}

// ---------------------------------------------------------------------------
// Helper functions
// ---------------------------------------------------------------------------

/// Strip -unix, -dos, or -mac suffix from a coding system name.
fn strip_eol_suffix(name: &str) -> &str {
    for suffix in &["-unix", "-dos", "-mac"] {
        if name.ends_with(suffix) {
            return &name[..name.len() - suffix.len()];
        }
    }
    name
}

fn allows_derived_eol_variant(base: &str) -> bool {
    matches!(
        base,
        "utf-8"
            | "mule-utf-8"
            | "latin-1"
            | "iso-8859-1"
            | "iso-latin-1"
            | "latin-5"
            | "iso-8859-9"
            | "iso-latin-5"
            | "latin-0"
            | "latin-9"
            | "iso-8859-15"
            | "iso-latin-9"
            | "ascii"
            | "us-ascii"
            | "cn-gb-2312"
            | "euc-china"
            | "euc-cn"
            | "cn-gb"
            | "gb2312"
            | "chinese-iso-8bit"
            | "big5"
            | "cn-big5"
            | "cp950"
            | "chinese-big5"
            | "big5-hkscs"
            | "cn-big5-hkscs"
            | "chinese-big5-hkscs"
            | "raw-text"
            | "undecided"
            | "utf-8-auto"
            | "prefer-utf-8"
            | "utf-8-emacs"
    )
}

fn normalize_coding_name_for_lookup(name: &str) -> &str {
    if name == "nil" { "no-conversion" } else { name }
}

fn display_base_name(base: &str) -> &str {
    match base {
        "latin-1" | "iso-8859-1" | "iso-latin-1" => "iso-latin-1",
        "latin-5" | "iso-8859-9" | "iso-latin-5" => "iso-latin-5",
        "latin-0" | "latin-9" | "iso-8859-15" | "iso-latin-9" => "iso-latin-9",
        "cn-gb-2312" | "euc-china" | "euc-cn" | "cn-gb" | "gb2312" | "chinese-iso-8bit" => {
            "chinese-iso-8bit"
        }
        "big5" | "cn-big5" | "cp950" | "chinese-big5" => "chinese-big5",
        "big5-hkscs" | "cn-big5-hkscs" | "chinese-big5-hkscs" => "chinese-big5-hkscs",
        "ascii" | "us-ascii" => "us-ascii",
        "binary" | "no-conversion" | "nil" => "no-conversion",
        "emacs-internal" | "utf-8-emacs" => "utf-8-emacs",
        "mule-utf-8" => "utf-8",
        other => other,
    }
}

fn coding_type_for_base(base: &str) -> Option<&'static str> {
    match base {
        "utf-8" | "mule-utf-8" | "utf-8-auto" | "emacs-internal" | "utf-8-emacs" => Some("utf-8"),
        "latin-1" | "iso-8859-1" | "iso-latin-1" | "latin-5" | "iso-8859-9" | "iso-latin-5"
        | "latin-0" | "latin-9" | "iso-8859-15" | "iso-latin-9" | "ascii" | "us-ascii"
        | "big5-hkscs" | "cn-big5-hkscs" | "chinese-big5-hkscs" => Some("charset"),
        "cn-gb-2312" | "euc-china" | "euc-cn" | "cn-gb" | "gb2312" | "chinese-iso-8bit" => {
            Some("iso-2022")
        }
        "big5" | "cn-big5" | "cp950" | "chinese-big5" => Some("big5"),
        "raw-text" | "binary" | "no-conversion" => Some("raw-text"),
        "undecided" | "prefer-utf-8" => Some("undecided"),
        _ => None,
    }
}

fn default_mnemonic_for_base(base: &str) -> Option<i64> {
    match base {
        "utf-8" | "mule-utf-8" | "utf-8-auto" | "emacs-internal" | "utf-8-emacs" => {
            Some('U' as i64)
        }
        "latin-1" | "iso-8859-1" | "iso-latin-1" => Some('1' as i64),
        "latin-5" | "iso-8859-9" | "iso-latin-5" => Some('9' as i64),
        "latin-0" | "latin-9" | "iso-8859-15" | "iso-latin-9" => Some('0' as i64),
        "cn-gb-2312" | "euc-china" | "euc-cn" | "cn-gb" | "gb2312" | "chinese-iso-8bit" => {
            Some('c' as i64)
        }
        "big5" | "cn-big5" | "cp950" | "chinese-big5" | "big5-hkscs" | "cn-big5-hkscs"
        | "chinese-big5-hkscs" => Some('B' as i64),
        "ascii" | "us-ascii" | "undecided" | "prefer-utf-8" => Some('-' as i64),
        "raw-text" => Some('t' as i64),
        "binary" | "no-conversion" => Some('=' as i64),
        _ => None,
    }
}

fn properties_bucket_base(base: &str) -> &str {
    match base {
        "latin-1" | "iso-8859-1" | "iso-latin-1" => "iso-latin-1",
        "latin-5" | "iso-8859-9" | "iso-latin-5" => "iso-latin-5",
        "latin-0" | "latin-9" | "iso-8859-15" | "iso-latin-9" => "iso-latin-9",
        "cn-gb-2312" | "euc-china" | "euc-cn" | "cn-gb" | "gb2312" | "chinese-iso-8bit" => {
            "chinese-iso-8bit"
        }
        "big5" | "cn-big5" | "cp950" | "chinese-big5" => "chinese-big5",
        "big5-hkscs" | "cn-big5-hkscs" | "chinese-big5-hkscs" => "chinese-big5-hkscs",
        "ascii" | "us-ascii" => "us-ascii",
        "binary" | "no-conversion" | "nil" => "no-conversion",
        "emacs-internal" | "utf-8-emacs" => "utf-8-emacs",
        "mule-utf-8" => "utf-8",
        other => other,
    }
}

fn eol_vector_base(base: &str) -> &str {
    match base {
        "latin-1" | "iso-8859-1" | "iso-latin-1" => "iso-latin-1",
        "latin-5" | "iso-8859-9" | "iso-latin-5" => "iso-latin-5",
        "latin-0" | "latin-9" | "iso-8859-15" | "iso-latin-9" => "iso-latin-9",
        "cn-gb-2312" | "euc-china" | "euc-cn" | "cn-gb" | "gb2312" | "chinese-iso-8bit" => {
            "chinese-iso-8bit"
        }
        "big5" | "cn-big5" | "cp950" | "chinese-big5" => "chinese-big5",
        "big5-hkscs" | "cn-big5-hkscs" | "chinese-big5-hkscs" => "chinese-big5-hkscs",
        "ascii" | "us-ascii" => "us-ascii",
        "mule-utf-8" => "utf-8",
        "emacs-internal" | "utf-8-emacs" => "utf-8-emacs",
        other => other,
    }
}

fn derive_coding_for_eol(base: &str, eol: i64) -> Option<String> {
    let suffix = match eol {
        0 => "-unix",
        1 => "-dos",
        2 => "-mac",
        _ => return None,
    };
    let derived = match base {
        "latin-1" | "iso-8859-1" | "iso-latin-1" => format!("iso-latin-1{suffix}"),
        "latin-5" | "iso-8859-9" | "iso-latin-5" => format!("iso-latin-5{suffix}"),
        "latin-0" | "latin-9" | "iso-8859-15" | "iso-latin-9" => {
            format!("iso-latin-9{suffix}")
        }
        "ascii" | "us-ascii" => format!("us-ascii{suffix}"),
        "cn-gb-2312" | "euc-china" | "euc-cn" | "cn-gb" | "gb2312" | "chinese-iso-8bit" => {
            format!("chinese-iso-8bit{suffix}")
        }
        "big5" | "cn-big5" | "cp950" | "chinese-big5" => format!("chinese-big5{suffix}"),
        "big5-hkscs" | "cn-big5-hkscs" | "chinese-big5-hkscs" => {
            format!("chinese-big5-hkscs{suffix}")
        }
        "mule-utf-8" | "utf-8" => format!("utf-8{suffix}"),
        "utf-8-auto" => format!("utf-8-auto{suffix}"),
        "prefer-utf-8" => format!("prefer-utf-8{suffix}"),
        "undecided" => format!("undecided{suffix}"),
        "raw-text" => format!("raw-text{suffix}"),
        "utf-8-emacs" => format!("utf-8-emacs{suffix}"),
        "emacs-internal" => match eol {
            0 => "emacs-internal".to_string(),
            1 => "utf-8-emacs-dos".to_string(),
            2 => "utf-8-emacs-mac".to_string(),
            _ => unreachable!("validated above"),
        },
        "no-conversion" => {
            if eol == 0 {
                "no-conversion".to_string()
            } else {
                return None;
            }
        }
        "binary" => {
            if eol == 0 {
                "binary".to_string()
            } else {
                return None;
            }
        }
        other => format!("{other}{suffix}"),
    };
    Some(derived)
}

fn resolve_runtime_name(mgr: &CodingSystemManager, name: &str) -> Option<String> {
    let normalized = normalize_coding_name_for_lookup(name);
    if mgr.resolve(normalized).is_some() {
        return Some(normalized.to_string());
    }

    let eol = EolType::from_suffix(normalized)?;
    let base = strip_eol_suffix(normalized);
    if !allows_derived_eol_variant(base) {
        return None;
    }
    let canonical_base = mgr.resolve(base)?;
    derive_coding_for_eol(resolve_sym(canonical_base), eol.to_int()).map(|_| normalized.to_string())
}

fn canonical_runtime_name(mgr: &CodingSystemManager, name: &str) -> Option<String> {
    let normalized = normalize_coding_name_for_lookup(name);
    if let Some(eol) = EolType::from_suffix(normalized) {
        let base = strip_eol_suffix(normalized);
        let canonical_base = mgr.resolve(base)?;
        return derive_coding_for_eol(resolve_sym(canonical_base), eol.to_int());
    }

    mgr.resolve(normalized)
        .map(|id| resolve_sym(id).to_string())
}

fn runtime_bucket_name(mgr: &CodingSystemManager, resolved_name: &str) -> Option<String> {
    let base = strip_eol_suffix(resolved_name);
    let bucket_base = properties_bucket_base(base);
    let bucket_name = mgr
        .resolve(bucket_base)
        .map(|id| resolve_sym(id).to_string())
        .unwrap_or_else(|| bucket_base.to_string());
    if mgr.is_known(bucket_name.as_str()) {
        Some(bucket_name)
    } else {
        None
    }
}

fn alias_sort_rank(canonical: &str, alias: &str) -> usize {
    match canonical {
        "utf-8" => match alias {
            "mule-utf-8" => 0,
            "cp65001" => 1,
            _ => 2,
        },
        "iso-latin-1" => match alias {
            "iso-8859-1" => 0,
            "latin-1" => 1,
            _ => 2,
        },
        "iso-latin-5" => match alias {
            "iso-8859-9" => 0,
            "latin-5" => 1,
            _ => 2,
        },
        "iso-latin-9" => match alias {
            "iso-8859-15" => 0,
            "latin-9" => 1,
            "latin-0" => 2,
            _ => 3,
        },
        "us-ascii" => match alias {
            "iso-safe" => 0,
            "ascii" => 1,
            _ => 2,
        },
        _ => 0,
    }
}

fn raw_coding_candidates(mgr: &CodingSystemManager, exclude: Option<&[Value]>) -> Vec<String> {
    let excluded: HashSet<String> = exclude
        .unwrap_or(&[])
        .iter()
        .filter_map(|value| value.as_symbol_name().map(|name| name.to_string()))
        .collect();

    let mut names: Vec<String> = mgr
        .systems
        .values()
        .filter(|info| info.eol_type == EolType::Undecided)
        .map(|info| display_base_name(strip_eol_suffix(resolve_sym(info.name))).to_string())
        .filter(|name| {
            !matches!(
                name.as_str(),
                "raw-text" | "no-conversion" | "binary" | "undecided"
            )
        })
        .filter(|name| !excluded.contains(name))
        .collect();
    names.sort();
    names.dedup();
    names
}

fn coding_can_encode_char(coding: &str, ch: char) -> bool {
    match properties_bucket_base(coding) {
        "utf-8" | "utf-8-emacs" | "utf-8-auto" | "prefer-utf-8" => true,
        "iso-latin-1" | "iso-latin-5" | "iso-latin-9" => (ch as u32) <= 0xFF,
        "us-ascii" => ch.is_ascii(),
        _ => false,
    }
}

fn safe_coding_systems_for_text(
    mgr: &CodingSystemManager,
    text: &str,
    multibyte: bool,
    exclude: Option<&[Value]>,
) -> Value {
    if !multibyte || text.is_ascii() {
        return Value::T;
    }

    if !text.is_ascii() {
        let mut safe_codings = Vec::new();
        for coding in raw_coding_candidates(mgr, exclude) {
            if text
                .chars()
                .filter(|ch| !ch.is_ascii())
                .all(|ch| coding_can_encode_char(&coding, ch))
            {
                safe_codings.push(Value::symbol(coding));
            }
        }
        safe_codings.push(Value::symbol("raw-text"));
        safe_codings.push(Value::symbol("no-conversion"));
        return Value::list(safe_codings);
    }

    Value::T
}

fn marker_or_integer_position(value: &Value) -> Result<i64, Flow> {
    if value.is_marker() {
        return super::marker::marker_position_as_int(value);
    }
    match value.kind() {
        ValueKind::Fixnum(n) => Ok(n),
        _ => Err(signal(
            "wrong-type-argument",
            vec![Value::symbol("integer-or-marker-p"), *value],
        )),
    }
}

pub(crate) fn builtin_find_coding_systems_region_internal(
    eval: &mut Context,
    args: Vec<Value>,
) -> EvalResult {
    expect_min_args("find-coding-systems-region-internal", &args, 2)?;
    expect_max_args("find-coding-systems-region-internal", &args, 3)?;

    if args[0].is_string() {
        let exclude = args.get(2).and_then(super::value::list_to_vec);
        let text = coding_runtime_string(&args[0])?;
        let multibyte = args[0].string_is_multibyte();
        return Ok(safe_coding_systems_for_text(
            &eval.coding_systems,
            &text,
            multibyte,
            exclude.as_deref(),
        ));
    }

    let start = marker_or_integer_position(&args[0])?;
    let end = marker_or_integer_position(&args[1])?;
    let exclude = args.get(2).and_then(super::value::list_to_vec);

    let buffer = eval
        .buffers
        .current_buffer()
        .ok_or_else(|| signal("error", vec![Value::string("No current buffer")]))?;
    if !buffer.get_multibyte() {
        return Ok(Value::T);
    }

    let char_count = buffer.text.char_count() as i64;
    if !(1..=char_count + 1).contains(&start) || !(1..=char_count + 1).contains(&end) || start > end
    {
        return Err(signal("args-out-of-range", vec![args[0], args[1]]));
    }

    let start_byte = buffer.lisp_pos_to_full_buffer_byte(start);
    let end_byte = buffer.lisp_pos_to_full_buffer_byte(end);
    let text = {
        let string = buffer.buffer_substring_lisp_string(start_byte, end_byte);
        super::builtins::runtime_string_from_lisp_string(&string)
    };
    Ok(safe_coding_systems_for_text(
        &eval.coding_systems,
        &text,
        buffer.get_multibyte(),
        exclude.as_deref(),
    ))
}

// ===========================================================================
// Bootstrap variables
// ===========================================================================

/// Initialize coding-system-related variables that official Emacs sets
/// in C code (coding.c syms_of_coding).
pub fn register_bootstrap_vars(obarray: &mut crate::emacs_core::symbol::Obarray) {
    fn defvar_lisp(obarray: &mut crate::emacs_core::symbol::Obarray, name: &str, value: Value) {
        obarray.set_symbol_value(name, value);
        obarray.make_special(name);
    }

    // latin-extra-code-table: 256-element nil vector (coding.c:12065).
    defvar_lisp(
        obarray,
        "latin-extra-code-table",
        Value::vector(vec![Value::NIL; 256]),
    );

    // coding.c:11927 — DEFVAR_LISP (Vcoding_system_list)
    defvar_lisp(obarray, "coding-system-list", Value::NIL);
    // coding.c:11930 — DEFVAR_LISP (Vcoding_system_alist)
    defvar_lisp(obarray, "coding-system-alist", Value::NIL);
    // coding.c:11935 — DEFVAR_LISP (Vcoding_category_list)
    defvar_lisp(obarray, "coding-category-list", Value::NIL);
    // coding.c:11941 — DEFVAR_LISP (Vcoding_system_for_read)
    defvar_lisp(obarray, "coding-system-for-read", Value::NIL);
    // coding.c:11949 — DEFVAR_LISP (Vcoding_system_for_write)
    defvar_lisp(obarray, "coding-system-for-write", Value::NIL);
    // coding.c:11956 — DEFVAR_LISP (Vlast_coding_system_used).
    obarray.make_special("last-coding-system-used");
    // coding.c:11959 — DEFVAR_LISP (Vlast_code_conversion_error)
    defvar_lisp(obarray, "last-code-conversion-error", Value::NIL);
    // coding.c:11999 — DEFVAR_LISP (Vlocale_coding_system)
    defvar_lisp(obarray, "locale-coding-system", Value::NIL);
    // coding.c:12014 — DEFVAR_LISP (Veol_mnemonic_unix)
    defvar_lisp(obarray, "eol-mnemonic-unix", Value::string(":"));
    // coding.c:12019 — DEFVAR_LISP (Veol_mnemonic_dos)
    defvar_lisp(obarray, "eol-mnemonic-dos", Value::string("\\"));
    // coding.c:12024 — DEFVAR_LISP (Veol_mnemonic_mac)
    defvar_lisp(obarray, "eol-mnemonic-mac", Value::string("/"));
    // coding.c:12029 — DEFVAR_LISP (Veol_mnemonic_undecided)
    defvar_lisp(obarray, "eol-mnemonic-undecided", Value::string(":"));
    // coding.c:12036 — DEFVAR_LISP (Venable_character_translation)
    defvar_lisp(obarray, "enable-character-translation", Value::T);
    // coding.c:12046 — DEFVAR_LISP (Vstandard_translation_table_for_decode)
    defvar_lisp(obarray, "standard-translation-table-for-decode", Value::NIL);
    // coding.c:12050 — DEFVAR_LISP (Vstandard_translation_table_for_encode)
    defvar_lisp(obarray, "standard-translation-table-for-encode", Value::NIL);
    // coding.c:12054 — DEFVAR_LISP (Vcharset_revision_table)
    defvar_lisp(obarray, "charset-revision-table", Value::NIL);
    // coding.c:12072 — DEFVAR_LISP (Vselect_safe_coding_system_function)
    defvar_lisp(obarray, "select-safe-coding-system-function", Value::NIL);
    // coding.c:12085 — DEFVAR_LISP (Vtranslation_table_for_input)
    defvar_lisp(obarray, "translation-table-for-input", Value::NIL);
    // coding.c:11993 — DEFVAR_LISP (Vnetwork_coding_system_alist)
    defvar_lisp(obarray, "network-coding-system-alist", Value::NIL);
    // coding.c:11996 — DEFVAR_LISP (Vprocess_coding_system_alist)
    defvar_lisp(obarray, "process-coding-system-alist", Value::NIL);
    // coding.c:12008 — DEFVAR_LISP (Vfile_coding_system_alist)
    defvar_lisp(obarray, "file-coding-system-alist", Value::NIL);
}

/// `(set-buffer-file-coding-system CODING-SYSTEM &optional FORCE NOMODIFY)` --
/// set the buffer-local `buffer-file-coding-system` variable.
///
/// CODING-SYSTEM is validated via the coding-system registry.  FORCE and
/// NOMODIFY are accepted for arity compatibility but currently ignored
/// (GNU uses them to control modification flag and EOL override behaviour).
pub(crate) fn builtin_set_buffer_file_coding_system(
    eval: &mut Context,
    args: Vec<Value>,
) -> EvalResult {
    expect_min_args("set-buffer-file-coding-system", &args, 1)?;
    expect_max_args("set-buffer-file-coding-system", &args, 3)?;

    // Validate coding-system argument.
    let cs_val = args[0];
    if !cs_val.is_nil() {
        match cs_val.as_symbol_name() {
            Some(name) if is_known_or_derived_coding_system(&eval.coding_systems, name) => {}
            Some(_) => {
                return Err(signal("coding-system-error", vec![cs_val]));
            }
            None => {
                return Err(signal(
                    "wrong-type-argument",
                    vec![Value::symbol("symbolp"), cs_val],
                ));
            }
        }
    }

    // Set buffer-local buffer-file-coding-system on the current buffer.
    let current_id = eval
        .buffers
        .current_buffer_id()
        .ok_or_else(|| signal("error", vec![Value::string("No current buffer")]))?;
    eval.buffers
        .set_buffer_local_property(current_id, "buffer-file-coding-system", cs_val);

    Ok(Value::NIL)
}

// ===========================================================================
// Tests
// ===========================================================================
#[cfg(test)]
#[path = "coding_test.rs"]
mod tests;