elisprs 0.1.20

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

use std::collections::HashMap;

use lsp_server::{Connection, Message, Request, RequestId, Response};
use lsp_types::notification::{
    DidChangeTextDocument, DidCloseTextDocument, DidOpenTextDocument, Notification as _,
    PublishDiagnostics,
};
use lsp_types::request::{
    Completion, DocumentSymbolRequest, HoverRequest, Request as _, SignatureHelpRequest,
};
use lsp_types::{
    CompletionItem, CompletionItemKind, CompletionOptions, CompletionResponse, Diagnostic,
    DiagnosticSeverity, DidChangeTextDocumentParams, DidOpenTextDocumentParams, Documentation,
    Hover, HoverContents, HoverParams, MarkupContent, MarkupKind, OneOf, Position,
    PublishDiagnosticsParams, Range, ServerCapabilities, SignatureHelp, SignatureHelpOptions,
    SignatureHelpParams, SignatureInformation, SymbolInformation, SymbolKind,
    TextDocumentSyncCapability, TextDocumentSyncKind, Uri, WorkDoneProgressOptions,
};

/// One entry in the language metadata table.
pub struct Entry {
    pub name: &'static str,
    pub kind: Kind,
    pub sig: &'static str,
    pub doc: &'static str,
}
#[derive(Clone, Copy)]
pub enum Kind {
    SpecialForm,
    Function,
}

/// Special forms the compiler recognizes (lowered or pending). Offered for
/// completion/hover regardless of lowering status — they are valid elisp.
pub const SPECIAL_FORMS: &[Entry] = &[
    Entry {
        name: "quote",
        kind: Kind::SpecialForm,
        sig: "(quote OBJECT)",
        doc: "Return OBJECT, unevaluated.",
    },
    Entry {
        name: "function",
        kind: Kind::SpecialForm,
        sig: "(function OBJECT)",
        doc: "Like quote, but for functions (`#'`).",
    },
    Entry {
        name: "lambda",
        kind: Kind::SpecialForm,
        sig: "(lambda ARGLIST BODY...)",
        doc: "An anonymous function.",
    },
    Entry {
        name: "progn",
        kind: Kind::SpecialForm,
        sig: "(progn BODY...)",
        doc: "Evaluate BODY in order; return the last value.",
    },
    Entry {
        name: "if",
        kind: Kind::SpecialForm,
        sig: "(if COND THEN ELSE...)",
        doc: "If COND is non-nil, eval THEN, else the ELSE forms.",
    },
    Entry {
        name: "when",
        kind: Kind::SpecialForm,
        sig: "(when COND BODY...)",
        doc: "If COND is non-nil, eval BODY.",
    },
    Entry {
        name: "unless",
        kind: Kind::SpecialForm,
        sig: "(unless COND BODY...)",
        doc: "If COND is nil, eval BODY.",
    },
    Entry {
        name: "and",
        kind: Kind::SpecialForm,
        sig: "(and CONDITIONS...)",
        doc: "Eval forms until one is nil; return the last value.",
    },
    Entry {
        name: "or",
        kind: Kind::SpecialForm,
        sig: "(or CONDITIONS...)",
        doc: "Eval forms until one is non-nil; return it.",
    },
    Entry {
        name: "setq",
        kind: Kind::SpecialForm,
        sig: "(setq SYM VAL SYM VAL...)",
        doc: "Set each SYM's value cell to VAL.",
    },
    Entry {
        name: "let",
        kind: Kind::SpecialForm,
        sig: "(let BINDINGS BODY...)",
        doc: "Bind variables in parallel, then eval BODY.",
    },
    Entry {
        name: "let*",
        kind: Kind::SpecialForm,
        sig: "(let* BINDINGS BODY...)",
        doc: "Bind variables sequentially, then eval BODY.",
    },
    Entry {
        name: "while",
        kind: Kind::SpecialForm,
        sig: "(while COND BODY...)",
        doc: "While COND is non-nil, eval BODY.",
    },
    Entry {
        name: "cond",
        kind: Kind::SpecialForm,
        sig: "(cond CLAUSES...)",
        doc: "Try each (TEST BODY...) clause; eval the first whose TEST is non-nil.",
    },
    Entry {
        name: "defun",
        kind: Kind::SpecialForm,
        sig: "(defun NAME ARGLIST BODY...)",
        doc: "Define NAME as a function.",
    },
    Entry {
        name: "defmacro",
        kind: Kind::SpecialForm,
        sig: "(defmacro NAME ARGLIST BODY...)",
        doc: "Define NAME as a macro.",
    },
    Entry {
        name: "defvar",
        kind: Kind::SpecialForm,
        sig: "(defvar NAME &optional INIT DOC)",
        doc: "Define a special (dynamic) variable.",
    },
    Entry {
        name: "defconst",
        kind: Kind::SpecialForm,
        sig: "(defconst NAME INIT &optional DOC)",
        doc: "Define a constant special variable.",
    },
];

/// The installed primitive subrs (mirrors `builtins::install`).
pub const SUBRS: &[Entry] = &[
    Entry {
        name: "+",
        kind: Kind::Function,
        sig: "(+ &rest NUMBERS)",
        doc: "Sum of the arguments.",
    },
    Entry {
        name: "-",
        kind: Kind::Function,
        sig: "(- &rest NUMBERS)",
        doc: "Negation, or subtraction from the first.",
    },
    Entry {
        name: "*",
        kind: Kind::Function,
        sig: "(* &rest NUMBERS)",
        doc: "Product of the arguments.",
    },
    Entry {
        name: "/",
        kind: Kind::Function,
        sig: "(/ DIVIDEND &rest DIVISORS)",
        doc: "Quotient of the arguments.",
    },
    Entry {
        name: "%",
        kind: Kind::Function,
        sig: "(% X Y)",
        doc: "Integer remainder of X divided by Y.",
    },
    Entry {
        name: "1+",
        kind: Kind::Function,
        sig: "(1+ NUMBER)",
        doc: "NUMBER plus one.",
    },
    Entry {
        name: "1-",
        kind: Kind::Function,
        sig: "(1- NUMBER)",
        doc: "NUMBER minus one.",
    },
    Entry {
        name: "=",
        kind: Kind::Function,
        sig: "(= &rest NUMBERS)",
        doc: "Non-nil if all numbers are equal.",
    },
    Entry {
        name: "<",
        kind: Kind::Function,
        sig: "(< &rest NUMBERS)",
        doc: "Non-nil if numbers strictly increase.",
    },
    Entry {
        name: ">",
        kind: Kind::Function,
        sig: "(> &rest NUMBERS)",
        doc: "Non-nil if numbers strictly decrease.",
    },
    Entry {
        name: "<=",
        kind: Kind::Function,
        sig: "(<= &rest NUMBERS)",
        doc: "Non-nil if numbers are non-decreasing.",
    },
    Entry {
        name: ">=",
        kind: Kind::Function,
        sig: "(>= &rest NUMBERS)",
        doc: "Non-nil if numbers are non-increasing.",
    },
    Entry {
        name: "eq",
        kind: Kind::Function,
        sig: "(eq A B)",
        doc: "Non-nil if A and B are the same object.",
    },
    Entry {
        name: "eql",
        kind: Kind::Function,
        sig: "(eql A B)",
        doc: "Like eq, but compares numbers by value.",
    },
    Entry {
        name: "equal",
        kind: Kind::Function,
        sig: "(equal A B)",
        doc: "Non-nil if A and B are structurally equal.",
    },
    Entry {
        name: "null",
        kind: Kind::Function,
        sig: "(null OBJECT)",
        doc: "Non-nil if OBJECT is nil.",
    },
    Entry {
        name: "not",
        kind: Kind::Function,
        sig: "(not OBJECT)",
        doc: "Non-nil if OBJECT is nil.",
    },
    Entry {
        name: "consp",
        kind: Kind::Function,
        sig: "(consp OBJECT)",
        doc: "Non-nil if OBJECT is a cons cell.",
    },
    Entry {
        name: "listp",
        kind: Kind::Function,
        sig: "(listp OBJECT)",
        doc: "Non-nil if OBJECT is a list (cons or nil).",
    },
    Entry {
        name: "atom",
        kind: Kind::Function,
        sig: "(atom OBJECT)",
        doc: "Non-nil if OBJECT is not a cons cell.",
    },
    Entry {
        name: "symbolp",
        kind: Kind::Function,
        sig: "(symbolp OBJECT)",
        doc: "Non-nil if OBJECT is a symbol.",
    },
    Entry {
        name: "stringp",
        kind: Kind::Function,
        sig: "(stringp OBJECT)",
        doc: "Non-nil if OBJECT is a string.",
    },
    Entry {
        name: "numberp",
        kind: Kind::Function,
        sig: "(numberp OBJECT)",
        doc: "Non-nil if OBJECT is a number.",
    },
    Entry {
        name: "integerp",
        kind: Kind::Function,
        sig: "(integerp OBJECT)",
        doc: "Non-nil if OBJECT is an integer.",
    },
    Entry {
        name: "floatp",
        kind: Kind::Function,
        sig: "(floatp OBJECT)",
        doc: "Non-nil if OBJECT is a float.",
    },
    Entry {
        name: "vectorp",
        kind: Kind::Function,
        sig: "(vectorp OBJECT)",
        doc: "Non-nil if OBJECT is a vector.",
    },
    Entry {
        name: "zerop",
        kind: Kind::Function,
        sig: "(zerop NUMBER)",
        doc: "Non-nil if NUMBER is zero.",
    },
    Entry {
        name: "cons",
        kind: Kind::Function,
        sig: "(cons CAR CDR)",
        doc: "Create a new cons cell.",
    },
    Entry {
        name: "car",
        kind: Kind::Function,
        sig: "(car LIST)",
        doc: "Return the first element of LIST.",
    },
    Entry {
        name: "cdr",
        kind: Kind::Function,
        sig: "(cdr LIST)",
        doc: "Return the rest of LIST after the first element.",
    },
    Entry {
        name: "setcar",
        kind: Kind::Function,
        sig: "(setcar CELL VALUE)",
        doc: "Set the car of CELL to VALUE.",
    },
    Entry {
        name: "setcdr",
        kind: Kind::Function,
        sig: "(setcdr CELL VALUE)",
        doc: "Set the cdr of CELL to VALUE.",
    },
    Entry {
        name: "list",
        kind: Kind::Function,
        sig: "(list &rest OBJECTS)",
        doc: "Return a newly created list of OBJECTS.",
    },
    Entry {
        name: "append",
        kind: Kind::Function,
        sig: "(append &rest SEQUENCES)",
        doc: "Concatenate lists into one.",
    },
    Entry {
        name: "reverse",
        kind: Kind::Function,
        sig: "(reverse SEQUENCE)",
        doc: "Return a reversed copy of SEQUENCE.",
    },
    Entry {
        name: "length",
        kind: Kind::Function,
        sig: "(length SEQUENCE)",
        doc: "Return the length of SEQUENCE.",
    },
    Entry {
        name: "nth",
        kind: Kind::Function,
        sig: "(nth N LIST)",
        doc: "Return the Nth element of LIST.",
    },
    Entry {
        name: "format-time-string",
        kind: Kind::Function,
        sig: "(format-time-string FORMAT &optional TIME ZONE)",
        doc: "Format TIME (default now) per FORMAT. ZONE nil=local, t=UTC, integer=offset secs.",
    },
    Entry {
        name: "float-time",
        kind: Kind::Function,
        sig: "(float-time &optional TIME)",
        doc: "Return TIME (default now) as seconds since the epoch, a float.",
    },
    Entry {
        name: "sleep-for",
        kind: Kind::Function,
        sig: "(sleep-for SECONDS &optional MILLISECONDS)",
        doc: "Pause for SECONDS (any number) plus MILLISECONDS; a duration that is not positive returns at once.",
    },
    Entry {
        name: "current-time",
        kind: Kind::Function,
        sig: "(current-time)",
        doc: "Return the current time as (HIGH LOW USEC PSEC).",
    },
    Entry {
        name: "current-time-string",
        kind: Kind::Function,
        sig: "(current-time-string &optional TIME ZONE)",
        doc: "Return TIME (default now) as a string like \"Sun Jun 29 12:00:00 2025\".",
    },
    Entry {
        name: "decode-time",
        kind: Kind::Function,
        sig: "(decode-time &optional TIME ZONE FORM)",
        doc: "Decompose TIME into (SEC MIN HOUR DAY MON YEAR DOW DST UTCOFF). With FORM t, SEC keeps TIME's resolution as (TICKS . HZ).",
    },
    Entry {
        name: "encode-time",
        kind: Kind::Function,
        sig: "(encode-time TIME) or (encode-time SEC MIN HOUR DAY MON YEAR &optional ZONE)",
        doc: "Inverse of decode-time: turn decoded components into a time value — (HIGH LOW) for whole seconds, (TICKS . HZ) when SECOND carries sub-second resolution.",
    },
    Entry {
        name: "vector",
        kind: Kind::Function,
        sig: "(vector &rest OBJECTS)",
        doc: "Return a vector of OBJECTS.",
    },
    Entry {
        name: "make-vector",
        kind: Kind::Function,
        sig: "(make-vector LENGTH INIT)",
        doc: "Return a vector of LENGTH elements, all INIT.",
    },
    Entry {
        name: "aref",
        kind: Kind::Function,
        sig: "(aref ARRAY IDX)",
        doc: "Return the IDX'th element of ARRAY.",
    },
    Entry {
        name: "aset",
        kind: Kind::Function,
        sig: "(aset ARRAY IDX VALUE)",
        doc: "Set the IDX'th element of ARRAY to VALUE.",
    },
    Entry {
        name: "store-substring",
        kind: Kind::Function,
        sig: "(store-substring STRING IDX OBJ)",
        doc: "Write OBJ (a character or string) into STRING at IDX; return STRING.",
    },
    Entry {
        name: "clear-string",
        kind: Kind::Function,
        sig: "(clear-string STRING)",
        doc: "Overwrite every character of STRING with NUL, in place; return nil.",
    },
    Entry {
        name: "fillarray",
        kind: Kind::Function,
        sig: "(fillarray ARRAY ITEM)",
        doc: "Set every element of ARRAY (a vector or string) to ITEM; return ARRAY.",
    },
    Entry {
        name: "make-overlay",
        kind: Kind::Function,
        sig: "(make-overlay BEG END &optional BUFFER FRONT-ADVANCE REAR-ADVANCE)",
        doc: "Make an overlay over BEG..END; both ends move with edits.",
    },
    Entry {
        name: "overlay-put",
        kind: Kind::Function,
        sig: "(overlay-put OVERLAY PROP VALUE)",
        doc: "Set OVERLAY's PROP to VALUE.",
    },
    Entry {
        name: "overlay-get",
        kind: Kind::Function,
        sig: "(overlay-get OVERLAY PROP)",
        doc: "OVERLAY's PROP, or nil.",
    },
    Entry {
        name: "overlays-in",
        kind: Kind::Function,
        sig: "(overlays-in BEG END)",
        doc: "Overlays overlapping BEG..END, empty ones included.",
    },
    Entry {
        name: "overlays-at",
        kind: Kind::Function,
        sig: "(overlays-at POS)",
        doc: "Overlays covering POS (start <= POS < end).",
    },
    Entry {
        name: "delete-overlay",
        kind: Kind::Function,
        sig: "(delete-overlay OVERLAY)",
        doc: "Detach OVERLAY from its buffer; `move-overlay' re-attaches it.",
    },
    Entry {
        name: "remove-overlays",
        kind: Kind::Function,
        sig: "(remove-overlays &optional BEG END NAME VAL)",
        doc: "Remove matching overlays in BEG..END, moving or splitting partial ones.",
    },
    Entry {
        name: "string-pixel-width",
        kind: Kind::Function,
        sig: "(string-pixel-width STRING &optional BUFFER)",
        doc: "Width of STRING in pixels: display columns, with TAB advancing to the next tab stop.",
    },
    Entry {
        name: "define-hash-table-test",
        kind: Kind::Function,
        sig: "(define-hash-table-test NAME TEST HASH)",
        doc: "Define a hash table test NAME whose elisp TEST compares keys and HASH hashes them.",
    },
    Entry {
        name: "text-property-any",
        kind: Kind::Function,
        sig: "(text-property-any START END PROP VALUE &optional OBJECT)",
        doc: "First position in [START, END) where PROP is `eq' to VALUE, or nil.",
    },
    Entry {
        name: "text-property-not-all",
        kind: Kind::Function,
        sig: "(text-property-not-all START END PROP VALUE &optional OBJECT)",
        doc: "First position in [START, END) where PROP is not `eq' to VALUE, or nil.",
    },
    Entry {
        name: "listify-key-sequence",
        kind: Kind::Function,
        sig: "(listify-key-sequence KEY)",
        doc: "Convert the key sequence KEY to a list of events.",
    },
    Entry {
        name: "looking-back",
        kind: Kind::Function,
        sig: "(looking-back REGEXP &optional LIMIT GREEDY)",
        doc: "Non-nil if the text before point matches REGEXP.",
    },
    Entry {
        name: "regexp-opt",
        kind: Kind::Function,
        sig: "(regexp-opt STRINGS &optional PAREN)",
        doc: "Regexp matching any of STRINGS, with shared affixes factored out.",
    },
    Entry {
        name: "regexp-opt-charset",
        kind: Kind::Function,
        sig: "(regexp-opt-charset CHARS)",
        doc: "Regexp matching one character in CHARS, using ranges where they pay.",
    },
    Entry {
        name: "pcase-lambda",
        kind: Kind::Function,
        sig: "(pcase-lambda LAMBDA-LIST &rest BODY)",
        doc: "A `lambda' whose parameters may be pcase patterns.",
    },
    Entry {
        name: "symbol-name",
        kind: Kind::Function,
        sig: "(symbol-name SYMBOL)",
        doc: "Return SYMBOL's name as a string.",
    },
    Entry {
        name: "intern",
        kind: Kind::Function,
        sig: "(intern NAME)",
        doc: "Return the interned symbol named NAME.",
    },
    Entry {
        name: "make-symbol",
        kind: Kind::Function,
        sig: "(make-symbol NAME)",
        doc: "Return a fresh symbol named NAME.",
    },
    Entry {
        name: "set",
        kind: Kind::Function,
        sig: "(set SYMBOL VALUE)",
        doc: "Set SYMBOL's value cell to VALUE.",
    },
    Entry {
        name: "symbol-value",
        kind: Kind::Function,
        sig: "(symbol-value SYMBOL)",
        doc: "Return SYMBOL's value.",
    },
    Entry {
        name: "boundp",
        kind: Kind::Function,
        sig: "(boundp SYMBOL)",
        doc: "Non-nil if SYMBOL has a value.",
    },
    Entry {
        name: "fboundp",
        kind: Kind::Function,
        sig: "(fboundp SYMBOL)",
        doc: "Non-nil if SYMBOL has a function definition.",
    },
    Entry {
        name: "fset",
        kind: Kind::Function,
        sig: "(fset SYMBOL DEFINITION)",
        doc: "Set SYMBOL's function cell to DEFINITION.",
    },
    Entry {
        name: "eval",
        kind: Kind::Function,
        sig: "(eval FORM &optional LEXICAL)",
        doc: "Evaluate FORM and return its value.",
    },
    Entry {
        name: "funcall",
        kind: Kind::Function,
        sig: "(funcall FUNCTION &rest ARGS)",
        doc: "Call FUNCTION with ARGS.",
    },
    Entry {
        name: "apply",
        kind: Kind::Function,
        sig: "(apply FUNCTION &rest ARGS LIST)",
        doc: "Call FUNCTION with ARGS and the elements of LIST.",
    },
    Entry {
        name: "identity",
        kind: Kind::Function,
        sig: "(identity ARG)",
        doc: "Return ARG unchanged.",
    },
    Entry {
        name: "concat",
        kind: Kind::Function,
        sig: "(concat &rest STRINGS)",
        doc: "Concatenate STRINGS into one string.",
    },
    Entry {
        name: "format",
        kind: Kind::Function,
        sig: "(format STRING &rest ARGS)",
        doc: "Format ARGS per the %-directives in STRING.",
    },
    Entry {
        name: "message",
        kind: Kind::Function,
        sig: "(message FORMAT &rest ARGS)",
        doc: "Print a formatted message (to stderr).",
    },
    Entry {
        name: "princ",
        kind: Kind::Function,
        sig: "(princ OBJECT &optional PRINTCHARFUN)",
        doc: "Output OBJECT (no quoting) to PRINTCHARFUN: a buffer, a marker, a function called per character, t for stdout, or nil for `standard-output`.",
    },
    Entry {
        name: "prin1",
        kind: Kind::Function,
        sig: "(prin1 OBJECT &optional PRINTCHARFUN)",
        doc: "Output OBJECT in read syntax to PRINTCHARFUN (as for `princ`).",
    },
    Entry {
        name: "number-to-string",
        kind: Kind::Function,
        sig: "(number-to-string NUMBER)",
        doc: "Return NUMBER rendered as a string.",
    },
    Entry {
        name: "string-match",
        kind: Kind::Function,
        sig: "(string-match REGEXP STRING &optional START)",
        doc: "Search STRING for REGEXP from START; set match data, return match index or nil.",
    },
    Entry {
        name: "string-match-p",
        kind: Kind::Function,
        sig: "(string-match-p REGEXP STRING &optional START)",
        doc: "Like string-match but preserve the existing match data.",
    },
    Entry {
        name: "match-beginning",
        kind: Kind::Function,
        sig: "(match-beginning N)",
        doc: "Char position where the Nth subexpression of the last match began.",
    },
    Entry {
        name: "match-end",
        kind: Kind::Function,
        sig: "(match-end N)",
        doc: "Char position where the Nth subexpression of the last match ended.",
    },
    Entry {
        name: "match-string",
        kind: Kind::Function,
        sig: "(match-string N &optional STRING)",
        doc: "Text matched by the Nth subexpression of the last match.",
    },
    Entry {
        name: "match-data",
        kind: Kind::Function,
        sig: "(match-data &optional INTEGERS REUSE RESEAT)",
        doc: "Last match's positions as a flat list (beg0 end0 beg1 end1 …). After a buffer search they are markers into that buffer; with INTEGERS they are integers and the buffer is appended. A cons REUSE is filled in place and returned.",
    },
    Entry {
        name: "set-match-data",
        kind: Kind::Function,
        sig: "(set-match-data LIST &optional RESEAT)",
        doc: "Restore match positions from a match-data list (integers or markers, optionally ending in a buffer). RESEAT detaches the markers in LIST.",
    },
    Entry {
        name: "replace-regexp-in-string",
        kind: Kind::Function,
        sig: "(replace-regexp-in-string REGEXP REP STRING &optional FIXEDCASE LITERAL SUBEXP START)",
        doc:
            "Replace every match of REGEXP in STRING with REP — a string (\\& / \\N templates unless LITERAL) or a function of the matched text.",
    },
    Entry {
        name: "regexp-quote",
        kind: Kind::Function,
        sig: "(regexp-quote STRING)",
        doc: "Return STRING with regexp metacharacters escaped to match literally.",
    },
    Entry {
        name: "save-match-data",
        kind: Kind::Function,
        sig: "(save-match-data &rest BODY)",
        doc: "Eval BODY, preserving the caller's regexp match data.",
    },
    Entry {
        name: "pcase",
        kind: Kind::Function,
        sig: "(pcase EXPR (PATTERN BODY...)...)",
        doc:
            "Structural dispatch: _, literals, 'x, binders, (pred FN), (guard E), (and …), (or …).",
    },
    Entry {
        name: "mod",
        kind: Kind::Function,
        sig: "(mod X Y)",
        doc: "X modulo Y, the remainder taking the sign of the divisor Y.",
    },
    Entry {
        name: "floor",
        kind: Kind::Function,
        sig: "(floor ARG &optional DIVISOR)",
        doc: "Largest integer less than or equal to ARG (or ARG/DIVISOR).",
    },
    Entry {
        name: "ceiling",
        kind: Kind::Function,
        sig: "(ceiling ARG &optional DIVISOR)",
        doc: "Smallest integer greater than or equal to ARG (or ARG/DIVISOR).",
    },
    Entry {
        name: "round",
        kind: Kind::Function,
        sig: "(round ARG &optional DIVISOR)",
        doc: "ARG (or ARG/DIVISOR) rounded to the nearest integer.",
    },
    Entry {
        name: "truncate",
        kind: Kind::Function,
        sig: "(truncate ARG &optional DIVISOR)",
        doc: "ARG (or ARG/DIVISOR) truncated toward zero to an integer.",
    },
    Entry {
        name: "float",
        kind: Kind::Function,
        sig: "(float ARG)",
        doc: "Convert ARG to a floating-point number.",
    },
    Entry {
        name: "logand",
        kind: Kind::Function,
        sig: "(logand &rest INTS)",
        doc: "Bitwise AND of the integer arguments.",
    },
    Entry {
        name: "logior",
        kind: Kind::Function,
        sig: "(logior &rest INTS)",
        doc: "Bitwise inclusive OR of the integer arguments.",
    },
    Entry {
        name: "logxor",
        kind: Kind::Function,
        sig: "(logxor &rest INTS)",
        doc: "Bitwise exclusive OR of the integer arguments.",
    },
    Entry {
        name: "lognot",
        kind: Kind::Function,
        sig: "(lognot NUMBER)",
        doc: "Bitwise NOT (one's complement) of NUMBER.",
    },
    Entry {
        name: "ash",
        kind: Kind::Function,
        sig: "(ash VALUE COUNT)",
        doc: "Arithmetic shift VALUE left COUNT bits (right if COUNT is negative).",
    },
    Entry {
        name: "lsh",
        kind: Kind::Function,
        sig: "(lsh VALUE COUNT)",
        doc: "Logical shift VALUE left COUNT bits (right if COUNT is negative).",
    },
    Entry {
        name: "expt",
        kind: Kind::Function,
        sig: "(expt BASE EXPONENT)",
        doc: "Return BASE raised to the power EXPONENT.",
    },
    Entry {
        name: "sqrt",
        kind: Kind::Function,
        sig: "(sqrt ARG)",
        doc: "Return the square root of ARG.",
    },
    Entry {
        name: "exp",
        kind: Kind::Function,
        sig: "(exp ARG)",
        doc: "Return e (the base of natural logarithms) raised to ARG.",
    },
    Entry {
        name: "log",
        kind: Kind::Function,
        sig: "(log ARG &optional BASE)",
        doc: "Natural logarithm of ARG, or the logarithm base BASE.",
    },
    Entry {
        name: "sin",
        kind: Kind::Function,
        sig: "(sin ARG)",
        doc: "Return the sine of ARG (in radians).",
    },
    Entry {
        name: "cos",
        kind: Kind::Function,
        sig: "(cos ARG)",
        doc: "Return the cosine of ARG (in radians).",
    },
    Entry {
        name: "tan",
        kind: Kind::Function,
        sig: "(tan ARG)",
        doc: "Return the tangent of ARG (in radians).",
    },
    Entry {
        name: "asin",
        kind: Kind::Function,
        sig: "(asin ARG)",
        doc: "Return the inverse sine of ARG, in radians.",
    },
    Entry {
        name: "acos",
        kind: Kind::Function,
        sig: "(acos ARG)",
        doc: "Return the inverse cosine of ARG, in radians.",
    },
    Entry {
        name: "atan",
        kind: Kind::Function,
        sig: "(atan Y &optional X)",
        doc: "Arc tangent of Y, or of Y/X using both signs to choose the quadrant.",
    },
    Entry {
        name: "ldexp",
        kind: Kind::Function,
        sig: "(ldexp SGNFCAND EXPONENT)",
        doc: "Return SGNFCAND * 2**EXPONENT, as a float.",
    },
    Entry {
        name: "copysign",
        kind: Kind::Function,
        sig: "(copysign X1 X2)",
        doc: "Return X1 with the sign of X2.",
    },
    Entry {
        name: "frexp",
        kind: Kind::Function,
        sig: "(frexp X)",
        doc: "Return (SIGNIFICAND . EXPONENT) with X = SIGNIFICAND * 2**EXPONENT and 0.5 <= |SIGNIFICAND| < 1.",
    },
    Entry {
        name: "isnan",
        kind: Kind::Function,
        sig: "(isnan X)",
        doc: "Non-nil if the float X is a NaN.",
    },
    Entry {
        name: "fround",
        kind: Kind::Function,
        sig: "(fround X)",
        doc: "Round X to the nearest integer, returned as a float.",
    },
    Entry {
        name: "ffloor",
        kind: Kind::Function,
        sig: "(ffloor X)",
        doc: "Largest integer value not greater than X, returned as a float.",
    },
    Entry {
        name: "fceiling",
        kind: Kind::Function,
        sig: "(fceiling X)",
        doc: "Smallest integer value not less than X, returned as a float.",
    },
    Entry {
        name: "ftruncate",
        kind: Kind::Function,
        sig: "(ftruncate X)",
        doc: "Truncate X toward zero to an integer value, returned as a float.",
    },
    Entry {
        name: "abs",
        kind: Kind::Function,
        sig: "(abs ARG)",
        doc: "Return the absolute value of ARG.",
    },
    Entry {
        name: "logcount",
        kind: Kind::Function,
        sig: "(logcount VALUE)",
        doc: "Number of one bits in VALUE (or zero bits if VALUE is negative).",
    },
    Entry {
        name: "logb",
        kind: Kind::Function,
        sig: "(logb ARG)",
        doc: "Return the binary exponent of ARG: the floor of log base 2 of |ARG|.",
    },
    Entry {
        name: "random",
        kind: Kind::Function,
        sig: "(random &optional LIMIT)",
        doc: "Pseudo-random integer; if LIMIT is a positive integer (a bignum too), in the range [0, LIMIT). A non-positive integer LIMIT signals args-out-of-range; t reseeds from the clock and a string reseeds from its contents; otherwise any fixnum.",
    },
    Entry {
        name: "string-to-number",
        kind: Kind::Function,
        sig: "(string-to-number STRING &optional BASE)",
        doc: "Parse the number at the front of STRING (radix BASE, default 10).",
    },
    Entry {
        name: "type-of",
        kind: Kind::Function,
        sig: "(type-of OBJECT)",
        doc: "Return a symbol naming the primitive type of OBJECT.",
    },
    Entry {
        name: "recordp",
        kind: Kind::Function,
        sig: "(recordp OBJECT)",
        doc: "Non-nil if OBJECT is a record.",
    },
    Entry {
        name: "cl-struct-p",
        kind: Kind::Function,
        sig: "(cl-struct-p OBJECT)",
        doc: "Non-nil if OBJECT is a record (a cl-defstruct instance).",
    },
    Entry {
        name: "functionp",
        kind: Kind::Function,
        sig: "(functionp OBJECT)",
        doc: "Non-nil if OBJECT is callable as a function.",
    },
    Entry {
        name: "char-or-string-p",
        kind: Kind::Function,
        sig: "(char-or-string-p OBJECT)",
        doc: "Non-nil if OBJECT is a character (integer) or a string.",
    },
    Entry {
        name: "subrp",
        kind: Kind::Function,
        sig: "(subrp OBJECT)",
        doc: "Non-nil if OBJECT is a built-in (primitive) function.",
    },
    Entry {
        name: "macrop",
        kind: Kind::Function,
        sig: "(macrop OBJECT)",
        doc: "Non-nil if OBJECT is a macro.",
    },
    Entry {
        name: "special-form-p",
        kind: Kind::Function,
        sig: "(special-form-p OBJECT)",
        doc: "Non-nil if OBJECT is a special form.",
    },
    Entry {
        name: "char-uppercase-p",
        kind: Kind::Function,
        sig: "(char-uppercase-p CHAR)",
        doc: "Non-nil if CHAR is an uppercase character.",
    },
    Entry {
        name: "special-variable-p",
        kind: Kind::Function,
        sig: "(special-variable-p SYMBOL)",
        doc: "Non-nil if SYMBOL is a special (dynamically scoped) variable.",
    },
    Entry {
        name: "char-equal",
        kind: Kind::Function,
        sig: "(char-equal C1 C2)",
        doc: "Non-nil if characters C1 and C2 are equal (case-insensitive when case-fold-search).",
    },
    Entry {
        name: "makunbound",
        kind: Kind::Function,
        sig: "(makunbound SYMBOL)",
        doc: "Make SYMBOL's value cell void; return SYMBOL.",
    },
    Entry {
        name: "intern-soft",
        kind: Kind::Function,
        sig: "(intern-soft NAME)",
        doc: "Return the interned symbol named NAME, or nil if none exists.",
    },
    Entry {
        name: "symbol-function",
        kind: Kind::Function,
        sig: "(symbol-function SYMBOL)",
        doc: "Return SYMBOL's function definition, or nil if it has none.",
    },
    Entry {
        name: "indirect-function",
        kind: Kind::Function,
        sig: "(indirect-function OBJECT &optional NOERROR)",
        doc: "Chase symbol function cells from OBJECT to the underlying function.",
    },
    Entry {
        name: "defvaralias",
        kind: Kind::Function,
        sig: "(defvaralias NEW-ALIAS BASE-VARIABLE &optional DOCSTRING)",
        doc: "Make NEW-ALIAS a variable alias for BASE-VARIABLE: value operations on either affect both. Returns BASE-VARIABLE.",
    },
    Entry {
        name: "indirect-variable",
        kind: Kind::Function,
        sig: "(indirect-variable OBJECT)",
        doc: "Follow the `defvaralias' chain from OBJECT to the base variable symbol, or return OBJECT if it is not a symbol.",
    },
    Entry {
        name: "func-arity",
        kind: Kind::Function,
        sig: "(func-arity FUNCTION)",
        doc: "Return (MIN . MAX) giving FUNCTION's minimum and maximum argument counts (MAX may be `many').",
    },
    Entry {
        name: "subr-arity",
        kind: Kind::Function,
        sig: "(subr-arity SUBR)",
        doc: "Return (MIN . MAX), the minimum and maximum argument counts of built-in SUBR.",
    },
    Entry {
        name: "throw",
        kind: Kind::Function,
        sig: "(throw TAG VALUE)",
        doc: "Throw to the `catch' for TAG, returning VALUE from it.",
    },
    Entry {
        name: "error",
        kind: Kind::Function,
        sig: "(error FORMAT &rest ARGS)",
        doc: "Signal an error whose message is FORMAT formatted with ARGS.",
    },
    Entry {
        name: "user-error",
        kind: Kind::Function,
        sig: "(user-error FORMAT &rest ARGS)",
        doc: "Signal a user error (a mistake, not a bug) with a formatted message.",
    },
    Entry {
        name: "signal",
        kind: Kind::Function,
        sig: "(signal ERROR-SYMBOL &optional DATA)",
        doc: "Signal the error named ERROR-SYMBOL with associated DATA.",
    },
    Entry {
        name: "read",
        kind: Kind::Function,
        sig: "(read &optional STREAM)",
        doc: "Read and return one Lisp object from STREAM: a string (from its start), a buffer (from its point, which moves past the object) or a marker (from its position, which advances).",
    },
    Entry {
        name: "read-from-string",
        kind: Kind::Function,
        sig: "(read-from-string STRING &optional START END)",
        doc: "Read one object from STRING; return (OBJECT . NEXT-INDEX).",
    },
    Entry {
        name: "terpri",
        kind: Kind::Function,
        sig: "(terpri &optional PRINTCHARFUN ENSURE)",
        doc: "Output a newline to PRINTCHARFUN. With ENSURE, only when not already at the start of a line; return t if a newline was output.",
    },
    Entry {
        name: "write-char",
        kind: Kind::Function,
        sig: "(write-char CHARACTER &optional PRINTCHARFUN)",
        doc: "Output CHARACTER to PRINTCHARFUN (as for `princ`); return CHARACTER.",
    },
    Entry {
        name: "print",
        kind: Kind::Function,
        sig: "(print OBJECT &optional PRINTCHARFUN)",
        doc: "Output OBJECT in read syntax, surrounded by newlines; return OBJECT.",
    },
    Entry {
        name: "prin1-to-string",
        kind: Kind::Function,
        sig: "(prin1-to-string OBJECT)",
        doc: "Return a string of OBJECT printed in read syntax.",
    },
    Entry {
        name: "substring",
        kind: Kind::Function,
        sig: "(substring STRING &optional FROM TO)",
        doc: "Return the substring of STRING from index FROM to TO (negative indices count from the end).",
    },
    Entry {
        name: "split-string",
        kind: Kind::Function,
        sig: "(split-string STRING &optional SEPARATORS OMIT-NULLS TRIM)",
        doc: "Split STRING into a list of substrings around matches of SEPARATORS.",
    },
    Entry {
        name: "string-prefix-p",
        kind: Kind::Function,
        sig: "(string-prefix-p PREFIX STRING &optional IGNORE-CASE)",
        doc: "Non-nil if PREFIX is a prefix of STRING.",
    },
    Entry {
        name: "string-suffix-p",
        kind: Kind::Function,
        sig: "(string-suffix-p SUFFIX STRING &optional IGNORE-CASE)",
        doc: "Non-nil if SUFFIX is a suffix of STRING.",
    },
    Entry {
        name: "string-empty-p",
        kind: Kind::Function,
        sig: "(string-empty-p STRING)",
        doc: "Non-nil if STRING is the empty string.",
    },
    Entry {
        name: "string-join",
        kind: Kind::Function,
        sig: "(string-join STRINGS &optional SEPARATOR)",
        doc: "Concatenate the list STRINGS, inserting SEPARATOR between elements.",
    },
    Entry {
        name: "char-to-string",
        kind: Kind::Function,
        sig: "(char-to-string CHAR)",
        doc: "Return a one-character string containing CHAR.",
    },
    Entry {
        name: "string-to-char",
        kind: Kind::Function,
        sig: "(string-to-char STRING)",
        doc: "Return the first character of STRING as an integer (0 if empty).",
    },
    Entry {
        name: "make-string",
        kind: Kind::Function,
        sig: "(make-string COUNT CHARACTER &optional MULTIBYTE)",
        doc: "Return a string of COUNT copies of CHARACTER.",
    },
    Entry {
        name: "string",
        kind: Kind::Function,
        sig: "(string &rest CHARACTERS)",
        doc: "Return a string made from the character arguments.",
    },
    Entry {
        name: "string-search",
        kind: Kind::Function,
        sig: "(string-search NEEDLE HAYSTACK &optional START-POS)",
        doc: "Index of the first occurrence of NEEDLE in HAYSTACK (from START-POS), or nil.",
    },
    Entry {
        name: "downcase",
        kind: Kind::Function,
        sig: "(downcase OBJ)",
        doc: "Return OBJ (a string or character) converted to lower case.",
    },
    Entry {
        name: "upcase",
        kind: Kind::Function,
        sig: "(upcase OBJ)",
        doc: "Return OBJ (a string or character) converted to upper case.",
    },
    Entry {
        name: "compare-strings",
        kind: Kind::Function,
        sig: "(compare-strings STR1 START1 END1 STR2 START2 END2 &optional IGNORE-CASE)",
        doc: "Compare the specified substrings; t if equal, else a signed mismatch position.",
    },
    Entry {
        name: "string-distance",
        kind: Kind::Function,
        sig: "(string-distance STRING1 STRING2 &optional BYTECOMPARE)",
        doc: "Levenshtein edit distance between STRING1 and STRING2.",
    },
    Entry {
        name: "fillarray",
        kind: Kind::Function,
        sig: "(fillarray ARRAY ITEM)",
        doc: "Set every element of ARRAY to ITEM; return ARRAY.",
    },
    Entry {
        name: "vconcat",
        kind: Kind::Function,
        sig: "(vconcat &rest SEQUENCES)",
        doc: "Concatenate the SEQUENCES into a single new vector.",
    },
    Entry {
        name: "string-to-vector",
        kind: Kind::Function,
        sig: "(string-to-vector STRING)",
        doc: "Return a vector of the character codes in STRING.",
    },
    Entry {
        name: "string-to-list",
        kind: Kind::Function,
        sig: "(string-to-list STRING)",
        doc: "Return a list of the character codes in STRING.",
    },
    Entry {
        name: "make-hash-table",
        kind: Kind::Function,
        sig: "(make-hash-table &rest KEYWORD-ARGS)",
        doc: "Create and return a new empty hash table (accepts :test, :size, etc.).",
    },
    Entry {
        name: "gethash",
        kind: Kind::Function,
        sig: "(gethash KEY TABLE &optional DEFAULT)",
        doc: "Return the value for KEY in TABLE, or DEFAULT if absent.",
    },
    Entry {
        name: "puthash",
        kind: Kind::Function,
        sig: "(puthash KEY VALUE TABLE)",
        doc: "Associate KEY with VALUE in TABLE; return VALUE.",
    },
    Entry {
        name: "remhash",
        kind: Kind::Function,
        sig: "(remhash KEY TABLE)",
        doc: "Remove KEY and its value from TABLE.",
    },
    Entry {
        name: "clrhash",
        kind: Kind::Function,
        sig: "(clrhash TABLE)",
        doc: "Remove all entries from TABLE; return it.",
    },
    Entry {
        name: "hash-table-count",
        kind: Kind::Function,
        sig: "(hash-table-count TABLE)",
        doc: "Return the number of entries in TABLE.",
    },
    Entry {
        name: "hash-table-test",
        kind: Kind::Function,
        sig: "(hash-table-test TABLE)",
        doc: "Return the test function symbol used by TABLE.",
    },
    Entry {
        name: "hash-table-p",
        kind: Kind::Function,
        sig: "(hash-table-p OBJECT)",
        doc: "Non-nil if OBJECT is a hash table.",
    },
    Entry {
        name: "hash-table-keys",
        kind: Kind::Function,
        sig: "(hash-table-keys TABLE)",
        doc: "Return a list of all keys in TABLE.",
    },
    Entry {
        name: "hash-table-values",
        kind: Kind::Function,
        sig: "(hash-table-values TABLE)",
        doc: "Return a list of all values in TABLE.",
    },
    Entry {
        name: "copy-hash-table",
        kind: Kind::Function,
        sig: "(copy-hash-table TABLE)",
        doc: "Return a shallow copy of TABLE.",
    },
    Entry {
        name: "sha1",
        kind: Kind::Function,
        sig: "(sha1 OBJECT &optional START END BINARY)",
        doc: "Return the SHA-1 hash of OBJECT (a string or buffer) as a hex string.",
    },
    Entry {
        name: "md5",
        kind: Kind::Function,
        sig: "(md5 OBJECT &optional START END CODING-SYSTEM NOERROR)",
        doc: "Return the MD5 hash of OBJECT as a hex string.",
    },
    Entry {
        name: "secure-hash",
        kind: Kind::Function,
        sig: "(secure-hash ALGORITHM OBJECT &optional START END BINARY)",
        doc: "Return the ALGORITHM (md5/sha1/sha224/sha256/sha384/sha512) hash of OBJECT.",
    },
    Entry {
        name: "base64-encode-string",
        kind: Kind::Function,
        sig: "(base64-encode-string STRING &optional NO-LINE-BREAK)",
        doc: "Return the base64 encoding of STRING.",
    },
    Entry {
        name: "base64-decode-string",
        kind: Kind::Function,
        sig: "(base64-decode-string STRING &optional BASE64URL IGNORE-INVALID)",
        doc: "Return the contents of base64-encoded STRING decoded.",
    },
    Entry {
        name: "base64url-encode-string",
        kind: Kind::Function,
        sig: "(base64url-encode-string STRING &optional NO-PAD)",
        doc: "Return the base64url (URL-safe) encoding of STRING.",
    },
    Entry {
        name: "base64url-decode-string",
        kind: Kind::Function,
        sig: "(base64url-decode-string STRING &optional IGNORE-INVALID)",
        doc: "Decode a base64url (URL-safe) encoded STRING.",
    },
    Entry {
        name: "url-hexify-string",
        kind: Kind::Function,
        sig: "(url-hexify-string STRING &optional ALLOWED-CHARS)",
        doc: "Percent-encode (URL-escape) the characters of STRING.",
    },
    Entry {
        name: "url-unhex-string",
        kind: Kind::Function,
        sig: "(url-unhex-string STRING &optional ALLOW-NEWLINES)",
        doc: "Decode the %-escaped sequences in STRING.",
    },
    Entry {
        name: "sxhash",
        kind: Kind::Function,
        sig: "(sxhash OBJ)",
        doc: "Return an `equal'-based hash code for OBJ (alias of sxhash-equal).",
    },
    Entry {
        name: "sxhash-equal",
        kind: Kind::Function,
        sig: "(sxhash-equal OBJ)",
        doc: "Return a hash code for OBJ such that `equal' objects hash alike.",
    },
    Entry {
        name: "sxhash-eq",
        kind: Kind::Function,
        sig: "(sxhash-eq OBJ)",
        doc: "Return a hash code for OBJ such that `eq' objects hash alike.",
    },
    Entry {
        name: "sxhash-eql",
        kind: Kind::Function,
        sig: "(sxhash-eql OBJ)",
        doc: "Return a hash code for OBJ such that `eql' objects hash alike.",
    },
    Entry {
        name: "getenv",
        kind: Kind::Function,
        sig: "(getenv VARIABLE &optional FRAME)",
        doc: "Return the value of environment VARIABLE as a string, or nil.",
    },
    Entry {
        name: "setenv",
        kind: Kind::Function,
        sig: "(setenv VARIABLE &optional VALUE SUBSTITUTE-ENV-VARS)",
        doc: "Set environment VARIABLE to VALUE (unset it if VALUE is nil).",
    },
    Entry {
        name: "--current-directory--",
        kind: Kind::Function,
        sig: "(--current-directory--)",
        doc: "Internal: the process working directory as a string (backs `default-directory').",
    },
    Entry {
        name: "file-exists-p",
        kind: Kind::Function,
        sig: "(file-exists-p FILENAME)",
        doc: "Non-nil if FILENAME names an existing file.",
    },
    Entry {
        name: "file-directory-p",
        kind: Kind::Function,
        sig: "(file-directory-p FILENAME)",
        doc: "Non-nil if FILENAME names an existing directory.",
    },
    Entry {
        name: "file-regular-p",
        kind: Kind::Function,
        sig: "(file-regular-p FILENAME)",
        doc: "Non-nil if FILENAME names a regular file.",
    },
    Entry {
        name: "file-readable-p",
        kind: Kind::Function,
        sig: "(file-readable-p FILENAME)",
        doc: "Non-nil if FILENAME names a file you can read.",
    },
    Entry {
        name: "file-writable-p",
        kind: Kind::Function,
        sig: "(file-writable-p FILENAME)",
        doc: "Non-nil if FILENAME names a file you can write (or create).",
    },
    Entry {
        name: "file-symlink-p",
        kind: Kind::Function,
        sig: "(file-symlink-p FILENAME)",
        doc: "If FILENAME is a symbolic link, return its target; else nil.",
    },
    Entry {
        name: "file-executable-p",
        kind: Kind::Function,
        sig: "(file-executable-p FILENAME)",
        doc: "Non-nil if FILENAME names a file you can execute (search permission for a directory).",
    },
    Entry {
        name: "--invocation-file--",
        kind: Kind::Function,
        sig: "(--invocation-file--)",
        doc: "Internal: absolute path of the running `elisp' binary, backing `invocation-name', `invocation-directory', and `exec-directory'.",
    },
    Entry {
        name: "--directory-files--",
        kind: Kind::Function,
        sig: "(--directory-files-- DIRECTORY &optional MATCH-REGEXP NOSORT)",
        doc: "Internal primitive behind `directory-files': names in DIRECTORY, optionally MATCH-filtered and sorted.",
    },
    Entry {
        name: "write-region",
        kind: Kind::Function,
        sig: "(write-region START END FILENAME &optional APPEND VISIT LOCKNAME MUSTBENEW)",
        doc: "Write the buffer text between START and END to FILENAME.",
    },
    Entry {
        name: "delete-file",
        kind: Kind::Function,
        sig: "(delete-file FILENAME &optional TRASH)",
        doc: "Delete the file named FILENAME.",
    },
    Entry {
        name: "make-directory",
        kind: Kind::Function,
        sig: "(make-directory DIR &optional PARENTS)",
        doc: "Create the directory DIR (and parents when PARENTS is non-nil).",
    },
    Entry {
        name: "rename-file",
        kind: Kind::Function,
        sig: "(rename-file FILE NEWNAME &optional OK-IF-ALREADY-EXISTS)",
        doc: "Rename FILE to NEWNAME.",
    },
    Entry {
        name: "copy-file",
        kind: Kind::Function,
        sig: "(copy-file FILE NEWNAME &optional OK-IF-ALREADY-EXISTS KEEP-TIME PRESERVE-UID-GID PRESERVE-PERMISSIONS)",
        doc: "Copy FILE to NEWNAME.",
    },
    Entry {
        name: "insert-file-contents",
        kind: Kind::Function,
        sig: "(insert-file-contents FILENAME &optional VISIT BEG END REPLACE)",
        doc: "Insert the contents of FILENAME into the current buffer at point.",
    },
    Entry {
        name: "shell-command-to-string",
        kind: Kind::Function,
        sig: "(shell-command-to-string COMMAND)",
        doc: "Run shell COMMAND and return its standard output as a string.",
    },
    Entry {
        name: "call-process",
        kind: Kind::Function,
        sig: "(call-process PROGRAM &optional INFILE DESTINATION DISPLAY &rest ARGS)",
        doc: "Run PROGRAM synchronously with ARGS; return its exit status.",
    },
    Entry {
        name: "process-lines",
        kind: Kind::Function,
        sig: "(process-lines PROGRAM &rest ARGS)",
        doc: "Run PROGRAM with ARGS and return its output as a list of lines.",
    },
    Entry {
        name: "insert",
        kind: Kind::Function,
        sig: "(insert &rest ARGS)",
        doc: "Insert the ARGS (strings or characters) into the buffer at point.",
    },
    Entry {
        name: "buffer-string",
        kind: Kind::Function,
        sig: "(buffer-string)",
        doc: "Return the entire contents of the current buffer as a string.",
    },
    Entry {
        name: "buffer-size",
        kind: Kind::Function,
        sig: "(buffer-size &optional BUFFER)",
        doc: "Return the number of characters in the current buffer.",
    },
    Entry {
        name: "point",
        kind: Kind::Function,
        sig: "(point)",
        doc: "Return the value of point, as an integer (1-based).",
    },
    Entry {
        name: "point-min",
        kind: Kind::Function,
        sig: "(point-min)",
        doc: "Return the minimum accessible buffer position.",
    },
    Entry {
        name: "point-max",
        kind: Kind::Function,
        sig: "(point-max)",
        doc: "Return the maximum accessible buffer position.",
    },
    Entry {
        name: "goto-char",
        kind: Kind::Function,
        sig: "(goto-char POSITION)",
        doc: "Set point to POSITION in the current buffer.",
    },
    Entry {
        name: "erase-buffer",
        kind: Kind::Function,
        sig: "(erase-buffer)",
        doc: "Delete the entire contents of the current buffer.",
    },
    Entry {
        name: "buffer-modified-p",
        kind: Kind::Function,
        sig: "(buffer-modified-p &optional BUFFER)",
        doc: "Non-nil if BUFFER (default the current one) was modified since it was last marked unmodified: t, or `autosaved' when it was auto-saved since its last change.",
    },
    Entry {
        name: "set-buffer-modified-p",
        kind: Kind::Function,
        sig: "(set-buffer-modified-p FLAG)",
        doc: "Mark the current buffer modified (FLAG non-nil) or unmodified (nil). Returns nil.",
    },
    Entry {
        name: "restore-buffer-modified-p",
        kind: Kind::Function,
        sig: "(restore-buffer-modified-p FLAG)",
        doc: "Like `set-buffer-modified-p' without the side effects; FLAG `autosaved' also marks the buffer auto-saved. Returns FLAG.",
    },
    Entry {
        name: "buffer-modified-tick",
        kind: Kind::Function,
        sig: "(buffer-modified-tick &optional BUFFER)",
        doc: "BUFFER's modification count: it advances on every change to its text or text properties.",
    },
    Entry {
        name: "buffer-chars-modified-tick",
        kind: Kind::Function,
        sig: "(buffer-chars-modified-tick &optional BUFFER)",
        doc: "BUFFER's character-change count: like `buffer-modified-tick', but text-property changes leave it alone.",
    },
    Entry {
        name: "position-bytes",
        kind: Kind::Function,
        sig: "(position-bytes POSITION)",
        doc: "The byte position of character POSITION in the whole buffer, or nil if POSITION is outside it.",
    },
    Entry {
        name: "byte-to-position",
        kind: Kind::Function,
        sig: "(byte-to-position BYTEPOS)",
        doc: "The character position whose bytes include byte position BYTEPOS, or nil if it is outside the buffer.",
    },
    Entry {
        name: "char-after",
        kind: Kind::Function,
        sig: "(char-after &optional POS)",
        doc: "Return the character after POS (point by default), or nil.",
    },
    Entry {
        name: "buffer-substring",
        kind: Kind::Function,
        sig: "(buffer-substring START END)",
        doc: "Return the buffer text between positions START and END as a string.",
    },
    Entry {
        name: "buffer-substring-no-properties",
        kind: Kind::Function,
        sig: "(buffer-substring-no-properties START END)",
        doc: "Return the buffer text between START and END, with no text properties.",
    },
    Entry {
        name: "delete-region",
        kind: Kind::Function,
        sig: "(delete-region START END)",
        doc: "Delete the buffer text between positions START and END.",
    },
    Entry {
        name: "forward-char",
        kind: Kind::Function,
        sig: "(forward-char &optional N)",
        doc: "Move point N characters forward (default 1).",
    },
    Entry {
        name: "backward-char",
        kind: Kind::Function,
        sig: "(backward-char &optional N)",
        doc: "Move point N characters backward (default 1).",
    },
    Entry {
        name: "beginning-of-line",
        kind: Kind::Function,
        sig: "(beginning-of-line &optional N)",
        doc: "Move point to the beginning of the current line.",
    },
    Entry {
        name: "end-of-line",
        kind: Kind::Function,
        sig: "(end-of-line &optional N)",
        doc: "Move point to the end of the current line.",
    },
    Entry {
        name: "line-beginning-position",
        kind: Kind::Function,
        sig: "(line-beginning-position &optional N)",
        doc: "Return the position of the beginning of the current line.",
    },
    Entry {
        name: "line-end-position",
        kind: Kind::Function,
        sig: "(line-end-position &optional N)",
        doc: "Return the position of the end of the current line.",
    },
    Entry {
        name: "pos-bol",
        kind: Kind::Function,
        sig: "(pos-bol &optional N)",
        doc: "Return the position of the beginning of the current line (like line-beginning-position).",
    },
    Entry {
        name: "pos-eol",
        kind: Kind::Function,
        sig: "(pos-eol &optional N)",
        doc: "Return the position of the end of the current line (like line-end-position).",
    },
    Entry {
        name: "bolp",
        kind: Kind::Function,
        sig: "(bolp)",
        doc: "Non-nil if point is at the beginning of a line.",
    },
    Entry {
        name: "eolp",
        kind: Kind::Function,
        sig: "(eolp)",
        doc: "Non-nil if point is at the end of a line.",
    },
    Entry {
        name: "bobp",
        kind: Kind::Function,
        sig: "(bobp)",
        doc: "Non-nil if point is at the beginning of the buffer.",
    },
    Entry {
        name: "eobp",
        kind: Kind::Function,
        sig: "(eobp)",
        doc: "Non-nil if point is at the end of the buffer.",
    },
    Entry {
        name: "forward-line",
        kind: Kind::Function,
        sig: "(forward-line &optional N)",
        doc: "Move point N lines forward; return the count of lines left unmoved.",
    },
    Entry {
        name: "search-forward",
        kind: Kind::Function,
        sig: "(search-forward STRING &optional BOUND NOERROR COUNT)",
        doc: "Search forward for STRING, leaving point after it; return the new point.",
    },
    Entry {
        name: "re-search-forward",
        kind: Kind::Function,
        sig: "(re-search-forward REGEXP &optional BOUND NOERROR COUNT)",
        doc: "Search forward for a match of REGEXP, leaving point after it.",
    },
    Entry {
        name: "looking-at",
        kind: Kind::Function,
        sig: "(looking-at REGEXP &optional INHIBIT-MODIFY)",
        doc: "Non-nil if text after point matches REGEXP; sets the match data.",
    },
    Entry {
        name: "looking-at-p",
        kind: Kind::Function,
        sig: "(looking-at-p REGEXP)",
        doc: "Non-nil if text after point matches REGEXP, without changing the match data.",
    },
    Entry {
        name: "replace-match",
        kind: Kind::Function,
        sig: "(replace-match NEWTEXT &optional FIXEDCASE LITERAL STRING SUBEXP)",
        doc: "Replace the text matched by the last search with NEWTEXT.",
    },
    Entry {
        name: "char-before",
        kind: Kind::Function,
        sig: "(char-before &optional POS)",
        doc: "Return the character before POS (point by default), or nil.",
    },
    Entry {
        name: "delete-char",
        kind: Kind::Function,
        sig: "(delete-char N &optional KILLFLAG)",
        doc: "Delete N characters forward from point (backward if N is negative).",
    },
    Entry {
        name: "insert-char",
        kind: Kind::Function,
        sig: "(insert-char CHARACTER &optional COUNT INHERIT)",
        doc: "Insert COUNT copies of CHARACTER at point.",
    },
    Entry {
        name: "count-lines",
        kind: Kind::Function,
        sig: "(count-lines START END)",
        doc: "Return the number of lines between positions START and END.",
    },
    Entry {
        name: "line-number-at-pos",
        kind: Kind::Function,
        sig: "(line-number-at-pos &optional POS ABSOLUTE)",
        doc: "Return the line number of POS (point by default) in the current buffer.",
    },
    Entry {
        name: "current-column",
        kind: Kind::Function,
        sig: "(current-column)",
        doc: "Return the horizontal column position of point.",
    },
    Entry {
        name: "search-backward",
        kind: Kind::Function,
        sig: "(search-backward STRING &optional BOUND NOERROR COUNT)",
        doc: "Search backward for STRING, leaving point before it.",
    },
    Entry {
        name: "re-search-backward",
        kind: Kind::Function,
        sig: "(re-search-backward REGEXP &optional BOUND NOERROR COUNT)",
        doc: "Search backward for a match of REGEXP, leaving point before it.",
    },
    Entry {
        name: "skip-chars-forward",
        kind: Kind::Function,
        sig: "(skip-chars-forward STRING &optional LIM)",
        doc: "Move point forward over characters in the set STRING; return the distance moved.",
    },
    Entry {
        name: "skip-chars-backward",
        kind: Kind::Function,
        sig: "(skip-chars-backward STRING &optional LIM)",
        doc: "Move point backward over characters in the set STRING; return the distance moved.",
    },
    Entry {
        name: "forward-word",
        kind: Kind::Function,
        sig: "(forward-word &optional N)",
        doc: "Move point forward N words (default 1).",
    },
    Entry {
        name: "backward-word",
        kind: Kind::Function,
        sig: "(backward-word &optional N)",
        doc: "Move point backward N words (default 1).",
    },
    Entry {
        name: "current-indentation",
        kind: Kind::Function,
        sig: "(current-indentation)",
        doc: "Return the indentation of the current line: the column of its first character that is not a space or tab.",
    },
    Entry {
        name: "indent-to",
        kind: Kind::Function,
        sig: "(indent-to COLUMN &optional MINIMUM)",
        doc: "Indent from point with tabs (if `indent-tabs-mode') and spaces to reach COLUMN, inserting at least MINIMUM columns; return the column reached.",
    },
    Entry {
        name: "move-to-column",
        kind: Kind::Function,
        sig: "(move-to-column COLUMN &optional FORCE)",
        doc: "Move point to COLUMN on the current line, or as close as the line allows. FORCE non-nil splits a tab that spans COLUMN; FORCE t also pads a short line. Return the column reached.",
    },
    Entry {
        name: "back-to-indentation",
        kind: Kind::Function,
        sig: "(back-to-indentation)",
        doc: "Move point to the first non-whitespace character on this line.",
    },
    Entry {
        name: "backward-prefix-chars",
        kind: Kind::Function,
        sig: "(backward-prefix-chars)",
        doc: "Move point backward over characters with expression-prefix syntax or the `p' syntax flag.",
    },
    Entry {
        name: "buffer-narrowed-p",
        kind: Kind::Function,
        sig: "(buffer-narrowed-p)",
        doc: "Return non-nil if the current buffer is narrowed.",
    },
    Entry {
        name: "insert-buffer-substring",
        kind: Kind::Function,
        sig: "(insert-buffer-substring BUFFER &optional START END)",
        doc: "Insert BUFFER's text from START to END (default its accessible region), with text properties, before point.",
    },
    Entry {
        name: "insert-buffer-substring-no-properties",
        kind: Kind::Function,
        sig: "(insert-buffer-substring-no-properties BUFFER &optional START END)",
        doc: "Like `insert-buffer-substring', without text properties.",
    },
    Entry {
        name: "delete-and-extract-region",
        kind: Kind::Function,
        sig: "(delete-and-extract-region START END)",
        doc: "Delete the text between START and END and return it as a string.",
    },
    Entry {
        name: "point-min-marker",
        kind: Kind::Function,
        sig: "(point-min-marker)",
        doc: "Return a marker at the start of the accessible region.",
    },
    Entry {
        name: "point-max-marker",
        kind: Kind::Function,
        sig: "(point-max-marker)",
        doc: "Return a marker at the end of the accessible region.",
    },
    Entry {
        name: "replace-string-in-region",
        kind: Kind::Function,
        sig: "(replace-string-in-region STRING REPLACEMENT &optional START END)",
        doc: "Replace STRING with REPLACEMENT from START (default point) to END (default point-max), case-sensitively; return the count, or nil if none.",
    },
    Entry {
        name: "replace-regexp-in-region",
        kind: Kind::Function,
        sig: "(replace-regexp-in-region REGEXP REPLACEMENT &optional START END)",
        doc: "Replace REGEXP with REPLACEMENT (`\\&', `\\N', `\\\\') from START to END, case-sensitively; return the count, or nil if none.",
    },
    Entry {
        name: "delete-blank-lines",
        kind: Kind::Function,
        sig: "(delete-blank-lines)",
        doc: "On a blank line, delete the surrounding blank lines, leaving one; on an isolated blank line, delete it; on a nonblank line, delete the blank lines after it.",
    },
    Entry {
        name: "--push-output-capture--",
        kind: Kind::Function,
        sig: "(--push-output-capture--)",
        doc: "Internal: start redirecting princ/prin1/print output into a capture buffer.",
    },
    Entry {
        name: "--pop-output-capture--",
        kind: Kind::Function,
        sig: "(--pop-output-capture--)",
        doc: "Internal: stop capturing output and return the captured string.",
    },
    // AOP pattern-intercept layer (elisprs extension, ported from zshrs). Glob
    // advice across many function names at once — distinct from nadvice.
    Entry {
        name: "intercept",
        kind: Kind::Function,
        sig: "(intercept KIND PATTERN FORM)",
        doc: "Register AOP advice. KIND is before/after/around; PATTERN is a glob (\"forward-*\", \"_*\", \"all\") matched against function names; FORM is the (quoted) advice form. Returns the intercept ID. Distinct from nadvice's advice-add: one registration fires on every matching name.",
    },
    Entry {
        name: "intercept-list",
        kind: Kind::Function,
        sig: "(intercept-list)",
        doc: "Return registered intercepts as a list of (ID KIND PATTERN FORM) entries, or nil.",
    },
    Entry {
        name: "intercept-remove",
        kind: Kind::Function,
        sig: "(intercept-remove ID)",
        doc: "Remove the intercept with integer ID. Returns t if removed, nil otherwise.",
    },
    Entry {
        name: "intercept-clear",
        kind: Kind::Function,
        sig: "(intercept-clear)",
        doc: "Remove all intercepts. Returns the count removed.",
    },
    Entry {
        name: "intercept-proceed",
        kind: Kind::Function,
        sig: "(intercept-proceed)",
        doc: "From an around advice, run the original function and return its value. Bound context in advice: intercept-name, intercept-args, intercept-cmd, and (after) intercept-ms/intercept-us.",
    },
];

pub fn lookup(name: &str) -> Option<&'static Entry> {
    SPECIAL_FORMS.iter().chain(SUBRS).find(|e| e.name == name)
}

pub fn run_stdio() -> i32 {
    match run() {
        Ok(()) => 0,
        Err(e) => {
            eprintln!("elisp --lsp: {e}");
            1
        }
    }
}

/// Guard against leaking as an orphan LSP process. Editors / GUI apps reap us via
/// kill-on-drop, which only fires on a *graceful* client exit; a hard client
/// death (SIGKILL/crash/force-quit) skips it, and a leaked pipe fd can keep our
/// stdin open so we never see EOF either. Watch for reparenting to pid 1 (our
/// client died) and exit — nothing this read-only server holds is worth leaking.
fn spawn_orphan_guard() {
    std::thread::spawn(|| {
        // Linux: also ask the kernel to SIGKILL us the instant the parent dies.
        // Best-effort; the getppid poll below is the portable guarantee (macOS
        // has no PDEATHSIG).
        #[cfg(target_os = "linux")]
        // SAFETY: prctl(PR_SET_PDEATHSIG, ...) only registers a signal disposition.
        unsafe {
            libc::prctl(
                libc::PR_SET_PDEATHSIG,
                libc::SIGKILL as libc::c_ulong,
                0,
                0,
                0,
            );
        }
        loop {
            std::thread::sleep(std::time::Duration::from_secs(2));
            // SAFETY: getppid takes no arguments and never fails.
            if unsafe { libc::getppid() } == 1 {
                std::process::exit(0);
            }
        }
    });
}

fn run() -> Result<(), Box<dyn std::error::Error + Sync + Send>> {
    spawn_orphan_guard();
    let (connection, io_threads) = Connection::stdio();
    let caps = ServerCapabilities {
        text_document_sync: Some(TextDocumentSyncCapability::Kind(TextDocumentSyncKind::FULL)),
        completion_provider: Some(CompletionOptions {
            trigger_characters: Some(vec!["(".to_string()]),
            ..Default::default()
        }),
        hover_provider: Some(lsp_types::HoverProviderCapability::Simple(true)),
        document_symbol_provider: Some(OneOf::Left(true)),
        signature_help_provider: Some(SignatureHelpOptions {
            trigger_characters: Some(vec!["(".to_string(), " ".to_string()]),
            retrigger_characters: None,
            work_done_progress_options: WorkDoneProgressOptions::default(),
        }),
        ..Default::default()
    };
    let _ = connection.initialize(serde_json::to_value(caps)?)?;
    main_loop(&connection)?;
    io_threads.join()?;
    Ok(())
}

/// lsp-types `Uri` has interior mutability, so we key documents by its string
/// form (clippy's "mutable key type") and keep the `Uri` only where the
/// protocol needs it.
fn uri_key(uri: &Uri) -> String {
    uri.as_str().to_string()
}

fn main_loop(c: &Connection) -> Result<(), Box<dyn std::error::Error + Sync + Send>> {
    let mut docs: HashMap<String, String> = HashMap::new();
    for msg in &c.receiver {
        match msg {
            Message::Request(req) => {
                if c.handle_shutdown(&req)? {
                    return Ok(());
                }
                let resp = handle_request(&req, &docs);
                c.sender.send(Message::Response(resp))?;
            }
            Message::Notification(not) => match not.method.as_str() {
                DidOpenTextDocument::METHOD => {
                    let p: DidOpenTextDocumentParams = serde_json::from_value(not.params)?;
                    let uri = p.text_document.uri.clone();
                    docs.insert(uri_key(&uri), p.text_document.text.clone());
                    publish(c, &uri, &p.text_document.text)?;
                }
                DidChangeTextDocument::METHOD => {
                    let p: DidChangeTextDocumentParams = serde_json::from_value(not.params)?;
                    if let Some(change) = p.content_changes.into_iter().last() {
                        let uri = p.text_document.uri.clone();
                        docs.insert(uri_key(&uri), change.text.clone());
                        publish(c, &uri, &change.text)?;
                    }
                }
                DidCloseTextDocument::METHOD => {
                    let p: lsp_types::DidCloseTextDocumentParams =
                        serde_json::from_value(not.params)?;
                    docs.remove(&uri_key(&p.text_document.uri));
                }
                _ => {}
            },
            Message::Response(_) => {}
        }
    }
    Ok(())
}

fn ok_response(id: RequestId, value: serde_json::Value) -> Response {
    Response {
        id,
        result: Some(value),
        error: None,
    }
}

fn handle_request(req: &Request, docs: &HashMap<String, String>) -> Response {
    let id = req.id.clone();
    let result = match req.method.as_str() {
        Completion::METHOD => serde_json::to_value(completion()).ok(),
        HoverRequest::METHOD => {
            let p: Result<HoverParams, _> = serde_json::from_value(req.params.clone());
            p.ok().and_then(|p| {
                let uri = &p.text_document_position_params.text_document.uri;
                let pos = p.text_document_position_params.position;
                docs.get(&uri_key(uri))
                    .and_then(|t| hover(t, pos))
                    .and_then(|h| serde_json::to_value(h).ok())
            })
        }
        DocumentSymbolRequest::METHOD => {
            let p: Result<lsp_types::DocumentSymbolParams, _> =
                serde_json::from_value(req.params.clone());
            p.ok().and_then(|p| {
                docs.get(&uri_key(&p.text_document.uri)).map(|t| {
                    serde_json::to_value(document_symbols(&p.text_document.uri, t)).unwrap()
                })
            })
        }
        SignatureHelpRequest::METHOD => {
            let p: Result<SignatureHelpParams, _> = serde_json::from_value(req.params.clone());
            p.ok().and_then(|p| {
                let uri = &p.text_document_position_params.text_document.uri;
                let pos = p.text_document_position_params.position;
                docs.get(&uri_key(uri))
                    .and_then(|t| signature_help(t, pos))
                    .and_then(|h| serde_json::to_value(h).ok())
            })
        }
        _ => None,
    };
    ok_response(id, result.unwrap_or(serde_json::Value::Null))
}

fn publish(
    c: &Connection,
    uri: &Uri,
    text: &str,
) -> Result<(), Box<dyn std::error::Error + Sync + Send>> {
    let diags = diagnostics(text);
    let params = PublishDiagnosticsParams {
        uri: uri.clone(),
        diagnostics: diags,
        version: None,
    };
    c.sender
        .send(Message::Notification(lsp_server::Notification {
            method: PublishDiagnostics::METHOD.to_string(),
            params: serde_json::to_value(params)?,
        }))?;
    Ok(())
}

// ── line index ──────────────────────────────────────────────────────────────

/// Maps byte/char offsets to LSP `Position` (line + char). Operates on chars.
struct LineIndex {
    line_starts: Vec<usize>, // char offset of each line start
}
impl LineIndex {
    fn new(text: &str) -> Self {
        let mut line_starts = vec![0];
        for (i, c) in text.chars().enumerate() {
            if c == '\n' {
                line_starts.push(i + 1);
            }
        }
        LineIndex { line_starts }
    }
    fn position(&self, offset: usize) -> Position {
        let line = match self.line_starts.binary_search(&offset) {
            Ok(l) => l,
            Err(l) => l - 1,
        };
        Position {
            line: line as u32,
            character: (offset - self.line_starts[line]) as u32,
        }
    }
    fn range(&self, start: usize, end: usize) -> Range {
        Range {
            start: self.position(start),
            end: self.position(end),
        }
    }
}

// ── diagnostics ──────────────────────────────────────────────────────────────

/// Position-aware scan mirroring the reader's error rules: unmatched parens,
/// and unterminated strings.
fn diagnostics(text: &str) -> Vec<Diagnostic> {
    let chars: Vec<char> = text.chars().collect();
    let idx = LineIndex::new(text);
    let mut out = Vec::new();
    let mut stack: Vec<usize> = Vec::new(); // offsets of open parens
    let mut i = 0;
    while i < chars.len() {
        let c = chars[i];
        match c {
            ';' => {
                while i < chars.len() && chars[i] != '\n' {
                    i += 1;
                }
                continue;
            }
            '"' => {
                let start = i;
                i += 1;
                let mut closed = false;
                while i < chars.len() {
                    match chars[i] {
                        '\\' => i += 2,
                        '"' => {
                            closed = true;
                            i += 1;
                            break;
                        }
                        _ => i += 1,
                    }
                }
                if !closed {
                    out.push(diag(idx.range(start, chars.len()), "unterminated string"));
                }
                continue;
            }
            '?' => {
                // char literal: skip the (possibly escaped) next char
                i += if chars.get(i + 1) == Some(&'\\') {
                    3
                } else {
                    2
                };
                continue;
            }
            '(' => stack.push(i),
            ')' if stack.pop().is_none() => {
                out.push(diag(idx.range(i, i + 1), "unexpected `)`"));
            }
            _ => {}
        }
        i += 1;
    }
    for open in stack {
        out.push(diag(
            idx.range(open, open + 1),
            "unclosed `(` — missing `)`",
        ));
    }
    out
}

fn diag(range: Range, msg: &str) -> Diagnostic {
    Diagnostic {
        range,
        severity: Some(DiagnosticSeverity::ERROR),
        source: Some("elisprs".to_string()),
        message: msg.to_string(),
        ..Default::default()
    }
}

// ── token helpers ────────────────────────────────────────────────────────────

fn is_sym_char(c: char) -> bool {
    !(c.is_whitespace() || matches!(c, '(' | ')' | '"' | '\'' | '`' | ',' | ';'))
}

/// The symbol token covering char-offset `off` (or just before it).
fn token_at(chars: &[char], off: usize) -> Option<String> {
    if chars.is_empty() {
        return None;
    }
    let mut start = off.min(chars.len());
    while start > 0 && is_sym_char(chars[start - 1]) {
        start -= 1;
    }
    let mut end = off.min(chars.len());
    while end < chars.len() && is_sym_char(chars[end]) {
        end += 1;
    }
    if start == end {
        return None;
    }
    let tok: String = chars[start..end].iter().collect();
    Some(tok)
}

fn offset_of(text: &str, pos: Position) -> usize {
    let mut line = 0u32;
    for (i, c) in text.chars().enumerate() {
        if line == pos.line {
            return i + pos.character as usize;
        }
        if c == '\n' {
            line += 1;
        }
    }
    text.chars().count()
}

// ── feature handlers ─────────────────────────────────────────────────────────

/// Every metadata entry the language knows: the special forms the compiler
/// recognizes followed by the installed subrs. Single source of truth for both
/// the LSP completion list (which keeps the full sig/doc detail) and the REPL
/// wordlist (`completion_words`, bare names only).
pub fn all_entries() -> impl Iterator<Item = &'static Entry> {
    SPECIAL_FORMS.iter().chain(SUBRS)
}

/// The merged, sorted, de-duplicated bare names of every special form + subr —
/// the wordlist the reedline REPL feeds its Tab completer. Shares `all_entries`
/// with the LSP `completion()` path so the two never drift.
pub fn completion_words() -> Vec<String> {
    let mut v: Vec<String> = all_entries().map(|e| e.name.to_string()).collect();
    v.sort();
    v.dedup();
    v
}

fn completion() -> CompletionResponse {
    let items = all_entries()
        .map(|e| CompletionItem {
            label: e.name.to_string(),
            kind: Some(match e.kind {
                Kind::SpecialForm => CompletionItemKind::KEYWORD,
                Kind::Function => CompletionItemKind::FUNCTION,
            }),
            detail: Some(e.sig.to_string()),
            documentation: Some(Documentation::String(e.doc.to_string())),
            ..Default::default()
        })
        .collect();
    CompletionResponse::Array(items)
}

fn hover(text: &str, pos: Position) -> Option<Hover> {
    let chars: Vec<char> = text.chars().collect();
    let off = offset_of(text, pos);
    let tok = token_at(&chars, off)?;
    let e = lookup(&tok)?;
    let md = format!("```elisp\n{}\n```\n\n{}", e.sig, e.doc);
    Some(Hover {
        contents: HoverContents::Markup(MarkupContent {
            kind: MarkupKind::Markdown,
            value: md,
        }),
        range: None,
    })
}

fn signature_help(text: &str, pos: Position) -> Option<SignatureHelp> {
    let chars: Vec<char> = text.chars().collect();
    let off = offset_of(text, pos).min(chars.len());
    // Walk back to the innermost unmatched '(' and read the head symbol.
    let mut depth = 0i32;
    let mut i = off;
    while i > 0 {
        i -= 1;
        match chars[i] {
            ')' => depth += 1,
            '(' => {
                if depth == 0 {
                    let head = token_at(&chars, i + 1)?;
                    let e = lookup(&head)?;
                    return Some(SignatureHelp {
                        signatures: vec![SignatureInformation {
                            label: e.sig.to_string(),
                            documentation: Some(Documentation::String(e.doc.to_string())),
                            parameters: None,
                            active_parameter: None,
                        }],
                        active_signature: Some(0),
                        active_parameter: None,
                    });
                }
                depth -= 1;
            }
            _ => {}
        }
    }
    None
}

/// Document symbols: `(defun|defmacro|defvar|defconst NAME ...)` definitions.
#[allow(deprecated)]
fn document_symbols(uri: &Uri, text: &str) -> Vec<SymbolInformation> {
    let chars: Vec<char> = text.chars().collect();
    let idx = LineIndex::new(text);
    let mut out = Vec::new();
    let mut i = 0;
    while i + 1 < chars.len() {
        if chars[i] == '(' {
            let head_start = i + 1;
            if let Some(head) = token_at(&chars, head_start) {
                let (kind, is_def) = match head.as_str() {
                    "defun" | "defmacro" => (SymbolKind::FUNCTION, true),
                    "defvar" | "defconst" => (SymbolKind::VARIABLE, true),
                    _ => (SymbolKind::NULL, false),
                };
                if is_def {
                    // skip past head + whitespace to the NAME token
                    let mut j = head_start + head.chars().count();
                    while j < chars.len() && chars[j].is_whitespace() {
                        j += 1;
                    }
                    if let Some(name) = token_at(&chars, j) {
                        let end = j + name.chars().count();
                        #[allow(deprecated)]
                        out.push(SymbolInformation {
                            name,
                            kind,
                            tags: None,
                            deprecated: None,
                            location: lsp_types::Location {
                                uri: uri.clone(),
                                range: idx.range(j, end),
                            },
                            container_name: None,
                        });
                    }
                }
            }
        }
        i += 1;
    }
    out
}