Source

xemacs-beta / src / eval.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
/* Evaluator for XEmacs Lisp interpreter.
   Copyright (C) 1985-1987, 1992-1994 Free Software Foundation, Inc.
   Copyright (C) 1995 Sun Microsystems, Inc.
   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2010 Ben Wing.

This file is part of XEmacs.

XEmacs is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

XEmacs is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with XEmacs; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

/* Synched up with: FSF 19.30 (except for Fsignal), Mule 2.0. */

/* Authorship:

   Based on code from pre-release FSF 19, c. 1991.
   Some work by Richard Mlynarik long ago (c. 1993?) --
     added call-with-condition-handler; synch. up to released FSF 19.7
     for lemacs 19.8.  some signal changes.
   Various work by Ben Wing, 1995-1996:
     added all stuff dealing with trapping errors, suspended-errors, etc.
     added most Fsignal front ends.
     added warning code.
     reworked the Fsignal code and synched the rest up to FSF 19.30.
   Some changes by Martin Buchholz c. 1999?
     e.g. PRIMITIVE_FUNCALL macros.
   New call_trapping_problems code and large comments below
     by Ben Wing, Mar-Apr 2000.
*/

/* This file has been Mule-ized. */

/* What is in this file?

   This file contains the engine for the ELisp interpreter in XEmacs.
   The engine does the actual work of implementing function calls,
   form evaluation, non-local exits (catch, throw, signal,
   condition-case, call-with-condition-handler), unwind-protects,
   dynamic bindings, let constructs, backtraces, etc.  You might say
   that this module is the very heart of XEmacs, and everything else
   in XEmacs is merely an auxiliary module implementing some specific
   functionality that may be called from the heart at an appropriate
   time.

   The only exception is the alloc.c module, which implements the
   framework upon which this module (eval.c) works.  alloc.c works
   with creating the actual Lisp objects themselves and garbage
   collecting them as necessary, presenting a nice, high-level
   interface for object creation, deletion, access, and modification.

   The only other exception that could be cited is the event-handling
   module in event-stream.c.  From its perspective, it is also the
   heart of XEmacs, and controls exactly what gets done at what time.
   From its perspective, eval.c is merely one of the auxiliary modules
   out there that can be invoked by event-stream.c.

   Although the event-stream-centric view is a convenient fiction that
   makes sense particularly from the user's perspective and from the
   perspective of time, the engine-centric view is actually closest to
   the truth, because anywhere within the event-stream module, you are
   still somewhere in a Lisp backtrace, and event-loops are begun by
   functions such as `command-loop-1', a Lisp function.

   As the Lisp engine is doing its thing, it maintains the state of
   the engine primarily in five list-like items, which are:

   -- the backtrace list
   -- the catchtag list
   -- the condition-handler list
   -- the specbind list
   -- the GCPRO list.

   These are described in detail in the next comment.

   --ben
 */

/* Note that there are five separate lists used to maintain state in
   the evaluator.  All of them conceptually are stacks (last-in,
   first-out).  All non-local exits happen ultimately through the
   catch/throw mechanism, which uses one of the five lists (the
   catchtag list) and records the current state of the others in each
   frame of the list (some other information is recorded and restored
   as well, such as the current eval depth), so that all the state of
   the evaluator is restored properly when a non-local exit occurs.
   (Note that the current state of the condition-handler list is not
   recorded in the catchtag list.  Instead, when a condition-case or
   call-with-condition-handler is set up, it installs an
   unwind-protect on the specbind list to restore the appropriate
   setting for the condition-handler list.  During the course of
   handling the non-local exit, all entries on the specbind list that
   are past the location stored in the catch frame are "unwound"
   (i.e. variable bindings are restored and unwind-protects are
   executed), so the condition-handler list gets reset properly.

   The five lists are

   1. The backtrace list, which is chained through `struct backtrace's
      declared in the stack frames of various primitives, and keeps
      track of all Lisp function call entries and exits.
   2. The catchtag list, which is chained through `struct catchtag's
      declared in the stack frames of internal_catch and condition_case_1,
      and keeps track of information needed to reset the internal state
      of the evaluator to the state that was current when the catch or
      condition-case were established, in the event of a non-local exit.
   3. The condition-handler list, which is a simple Lisp list with new
      entries consed onto the front of the list.  It records condition-cases
      and call-with-condition-handlers established either from C or from
      Lisp.  Unlike with the other lists (but similar to everything else
      of a similar nature in the rest of the C and Lisp code), it takes care
      of restoring itself appropriately in the event of a non-local exit
      through the use of the unwind-protect mechanism.
   4. The specbind list, which is a contiguous array of `struct specbinding's,
      expanded as necessary using realloc().  It holds dynamic variable
      bindings (the only kind we currently have in ELisp) and unwind-protects.
   5. The GCPRO list, which is chained through `struct gcpro's declared in
      the stack frames of any functions that need to GC-protect Lisp_Objects
      declared on the stack.  This is one of the most fragile areas of the
      entire scheme -- you must not forget to UNGCPRO at the end of your
      function, you must make sure you GCPRO in many circumstances you don't
      think you have to, etc.  See the internals manual for more information
      about this.

      --ben
*/

#include <config.h>
#include "lisp.h"

#include "commands.h"
#include "backtrace.h"
#include "bytecode.h"
#include "buffer.h"
#include "console-impl.h"
#include "device.h"
#include "frame.h"
#include "lstream.h"
#include "opaque.h"
#include "profile.h"
#include "window.h"

struct backtrace *backtrace_list;

/* Macros for calling subrs with an argument list whose length is only
   known at runtime.  See EXFUN and DEFUN for similar hackery.  */

#define AV_0(av)
#define AV_1(av) av[0]
#define AV_2(av) AV_1(av), av[1]
#define AV_3(av) AV_2(av), av[2]
#define AV_4(av) AV_3(av), av[3]
#define AV_5(av) AV_4(av), av[4]
#define AV_6(av) AV_5(av), av[5]
#define AV_7(av) AV_6(av), av[6]
#define AV_8(av) AV_7(av), av[7]

#define PRIMITIVE_FUNCALL_1(fn, av, ac) \
  (((Lisp_Object (*)(EXFUN_##ac)) (fn)) (AV_##ac (av)))

/* If subrs take more than 8 arguments, more cases need to be added
   to this switch.  (But wait - don't do it - if you really need
   a SUBR with more than 8 arguments, use max_args == MANY.
   Or better, considering using a property list as one of your args.
   See the DEFUN macro in lisp.h)  */
#define PRIMITIVE_FUNCALL(rv, fn, av, ac) do {			\
  void (*PF_fn)(void) = (void (*)(void)) fn;			\
  Lisp_Object *PF_av = (av);					\
  switch (ac)							\
    {								\
    default:rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 0); break;	\
    case 1: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 1); break;	\
    case 2: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 2); break;	\
    case 3: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 3); break;	\
    case 4: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 4); break;	\
    case 5: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 5); break;	\
    case 6: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 6); break;	\
    case 7: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 7); break;	\
    case 8: rv = PRIMITIVE_FUNCALL_1(PF_fn, PF_av, 8); break;	\
    }								\
} while (0)

#define FUNCALL_SUBR(rv, subr, av, ac) \
	PRIMITIVE_FUNCALL (rv, subr_function (subr), av, ac);


/* This is the list of current catches (and also condition-cases).
   This is a stack: the most recent catch is at the head of the list.
   The list is threaded through the stack frames of the C functions
   that set up the catches; this is similar to the way the GCPRO list
   is handled, but different from the condition-handler list (which is
   a simple Lisp list) and the specbind stack, which is a contiguous
   array of `struct specbinding's, grown (using realloc()) as
   necessary. (Note that all four of these lists behave as a stacks.)

   Catches are created by declaring a `struct catchtag' locally,
   filling the .TAG field in with the tag, and doing a setjmp() on
   .JMP.  Fthrow() will store the value passed to it in .VAL and
   longjmp() back to .JMP, back to the function that established the
   catch.  This will always be either internal_catch() (catches
   established internally or through `catch') or condition_case_1
   (condition-cases established internally or through
   `condition-case').

   The catchtag also records the current position in the
   call stack (stored in BACKTRACE_LIST), the current position
   in the specpdl stack (used for variable bindings and
   unwind-protects), the value of LISP_EVAL_DEPTH, and the
   current position in the GCPRO stack.  All of these are
   restored by Fthrow().
  */

struct catchtag *catchlist;

/* A special tag that can be used internally from C code to catch
   every attempt to throw past this level. */
Lisp_Object Vcatch_everything_tag;

Lisp_Object Qautoload, Qmacro, Qexit;
Lisp_Object Qinteractive, Qcommandp, Qdefun, Qprogn, Qvalues;
Lisp_Object Vquit_flag, Vinhibit_quit;
Lisp_Object Qand_rest, Qand_optional;
Lisp_Object Qdebug_on_error, Qstack_trace_on_error;
Lisp_Object Qdebug_on_signal, Qstack_trace_on_signal;
Lisp_Object Qdebugger;
Lisp_Object Qinhibit_quit;
Lisp_Object Qfinalize_list;
Lisp_Object Qrun_hooks;
Lisp_Object Qsetq;
Lisp_Object Qdisplay_warning;
Lisp_Object Vpending_warnings, Vpending_warnings_tail;
Lisp_Object Qif;

Lisp_Object Qthrow;
Lisp_Object Qobsolete_throw;
Lisp_Object Qmultiple_value_list_internal;

static int first_desired_multiple_value;
/* Used outside this file, somewhat uncleanly, in the IGNORE_MULTIPLE_VALUES
   macro: */
int multiple_value_current_limit;

Fixnum Vmultiple_values_limit;

/* Flags specifying which operations are currently inhibited. */
int inhibit_flags;

/* Buffers, frames, windows, devices, and consoles created since most
   recent active
   call_trapping_problems (INHIBIT_EXISTING_PERMANENT_DISPLAY_OBJECT_DELETION).
*/
Lisp_Object Vdeletable_permanent_display_objects;

/* Buffers created since most recent active
   call_trapping_problems (INHIBIT_EXISTING_BUFFER_TEXT_MODIFICATION). */
Lisp_Object Vmodifiable_buffers;

/* Minimum level at which warnings are logged.  Below this, they're ignored
   entirely -- not even generated. */
Lisp_Object Vlog_warning_minimum_level;

/* Non-nil means record all fset's and provide's, to be undone
   if the file being autoloaded is not fully loaded.
   They are recorded by being consed onto the front of Vautoload_queue:
   (FUN . ODEF) for a defun, (OFEATURES . nil) for a provide.  */
Lisp_Object Vautoload_queue;

/* Current number of specbindings allocated in specpdl.  */
int specpdl_size;

/* Pointer to beginning of specpdl.  */
struct specbinding *specpdl;

/* Pointer to first unused element in specpdl.  */
struct specbinding *specpdl_ptr;

/* specpdl_ptr - specpdl */
int specpdl_depth_counter;

/* Maximum size allowed for specpdl allocation */
Fixnum max_specpdl_size;

/* Depth in Lisp evaluations and function calls.  */
int lisp_eval_depth;

/* Maximum allowed depth in Lisp evaluations and function calls.  */
Fixnum max_lisp_eval_depth;

/* Nonzero means enter debugger before next function call */
static int debug_on_next_call;

int backtrace_with_internal_sections;

/* List of conditions (non-nil atom means all) which cause a backtrace
   if an error is handled by the command loop's error handler.  */
Lisp_Object Vstack_trace_on_error;

/* List of conditions (non-nil atom means all) which enter the debugger
   if an error is handled by the command loop's error handler.  */
Lisp_Object Vdebug_on_error;

/* List of conditions and regexps specifying error messages which
   do not enter the debugger even if Vdebug_on_error says they should.  */
Lisp_Object Vdebug_ignored_errors;

/* List of conditions (non-nil atom means all) which cause a backtrace
   if any error is signalled.  */
Lisp_Object Vstack_trace_on_signal;

/* List of conditions (non-nil atom means all) which enter the debugger
   if any error is signalled.  */
Lisp_Object Vdebug_on_signal;

/* Nonzero means enter debugger if a quit signal
   is handled by the command loop's error handler.

   From lisp, this is a boolean variable and may have the values 0 and 1.
   But, eval.c temporarily uses the second bit of this variable to indicate
   that a critical_quit is in progress.  The second bit is reset immediately
   after it is processed in signal_call_debugger().  */
int debug_on_quit;

#if 0 /* FSFmacs */
/* entering_debugger is basically equivalent */
/* The value of num_nonmacro_input_chars as of the last time we
   started to enter the debugger.  If we decide to enter the debugger
   again when this is still equal to num_nonmacro_input_chars, then we
   know that the debugger itself has an error, and we should just
   signal the error instead of entering an infinite loop of debugger
   invocations.  */
int when_entered_debugger;
#endif

/* Nonzero means we are trying to enter the debugger.
   This is to prevent recursive attempts.
   Cleared by the debugger calling Fbacktrace */
static int entering_debugger;

/* Function to call to invoke the debugger */
Lisp_Object Vdebugger;

/* List of condition handlers currently in effect.
   The elements of this lists were at one point in the past
   threaded through the stack frames of Fcondition_case and
   related functions, but now are stored separately in a normal
   stack.  When an error is signaled (by calling Fsignal, below),
   this list is searched for an element that applies.

   Each element of this list is one of the following:

   -- A list of a handler function and possibly args to pass to the
   function.  This is a handler established with the Lisp primitive
   `call-with-condition-handler' or related C function
   call_with_condition_handler():

     If the handler function is an opaque ptr object, it is a handler
     that was established in C using call_with_condition_handler(),
     and the contents of the object are a function pointer which takes
     three arguments, the signal name and signal data (same arguments
     passed to `signal') and a third Lisp_Object argument, specified
     in the call to call_with_condition_handler() and stored as the
     second element of the list containing the handler functionl.
  
     If the handler function is a regular Lisp_Object, it is a handler
     that was established using `call-with-condition-handler'.
     Currently there are no more arguments in the list containing the
     handler function, and only one argument is passed to the handler
     function: a cons of the signal name and signal data arguments
     passed to `signal'.
  
   -- A list whose car is Qunbound and whose cdr is Qt.  This is a
   special condition-case handler established by C code with
   condition_case_1().  All errors are trapped; the debugger is not
   invoked even if `debug-on-error' was set.

   -- A list whose car is Qunbound and whose cdr is Qerror.  This is a
   special condition-case handler established by C code with
   condition_case_1().  It is like Qt except that the debugger is
   invoked normally if it is called for.

   -- A list whose car is Qunbound and whose cdr is a list of lists
   (CONDITION-NAME BODY ...) exactly as in `condition-case'.  This is
   a normal `condition-case' handler.

   Note that in all cases *except* the first, there is a corresponding
   catch, whose TAG is the value of Vcondition_handlers just after the
   handler data just described is pushed onto it.  The reason is that
   `condition-case' handlers need to throw back to the place where the
   handler was installed before invoking it, while
   `call-with-condition-handler' handlers are invoked in the
   environment that `signal' was invoked in.  */


static Lisp_Object Vcondition_handlers;

/* I think we should keep this enabled all the time, not just when
   error checking is enabled, because if one of these puppies pops up,
   it will trash the stack if not caught, making it that much harder to
   debug.  It doesn't cause speed loss. */
#define DEFEND_AGAINST_THROW_RECURSION

#ifdef DEFEND_AGAINST_THROW_RECURSION
/* Used for error catching purposes by throw_or_bomb_out */
static int throw_level;
#endif

static int warning_will_be_discarded (Lisp_Object level);
static Lisp_Object maybe_get_trapping_problems_backtrace (void);



/* When parsing keyword arguments; is some element of NARGS
   :allow-other-keys, and is that element followed by a non-nil Lisp
   object? */

Boolint
non_nil_allow_other_keys_p (Elemcount offset, int nargs, Lisp_Object *args)
{
  Lisp_Object key, value;
  while (offset + 1 < nargs)
    {
      key = args[offset++];
      value = args[offset++];
      if (EQ (key, Q_allow_other_keys)) 
	{
          /* The ANSI Common Lisp standard says the first value for a given
             keyword overrides. */
          return !NILP (value);
	}
    }
  return 0;
}

/************************************************************************/
/*			The subr object type				*/
/************************************************************************/

static void
print_subr (Lisp_Object obj, Lisp_Object printcharfun, int UNUSED (escapeflag))
{
  Lisp_Subr *subr = XSUBR (obj);
  const Ascbyte *header =
    (subr->max_args == UNEVALLED) ? "#<special-operator " : "#<subr ";
  const Ascbyte *name = subr_name (subr);
  const Ascbyte *trailer = subr->prompt ? " (interactive)>" : ">";

  if (print_readably)
    printing_unreadable_object_fmt ("%s%s%s", header, name, trailer);

  write_ascstring (printcharfun, header);
  write_ascstring (printcharfun, name);
  write_ascstring (printcharfun, trailer);
}

static const struct memory_description subr_description[] = {
  { XD_DOC_STRING, offsetof (Lisp_Subr, doc), 0, { 0 }, XD_FLAG_NO_KKCC },
  { XD_END }
};

DEFINE_DUMPABLE_FROB_BLOCK_LISP_OBJECT ("subr", subr,
					0, print_subr, 0, 0, 0,
					subr_description,
					Lisp_Subr);

/************************************************************************/
/*			 Entering the debugger				*/
/************************************************************************/

static Lisp_Object
current_warning_level (void)
{
  if (inhibit_flags & ISSUE_WARNINGS_AT_DEBUG_LEVEL)
    return Qdebug;
  else
    return Qwarning;
}

/* Actually call the debugger.  ARG is a list of args that will be
   passed to the debugger function, as follows;

If due to frame exit, args are `exit' and the value being returned;
 this function's value will be returned instead of that.
If due to error, args are `error' and a list of the args to `signal'.
If due to `apply' or `funcall' entry, one arg, `lambda'.
If due to `eval' entry, one arg, t.

*/

static Lisp_Object
call_debugger_259 (Lisp_Object arg)
{
  return apply1 (Vdebugger, arg);
}

/* Call the debugger, doing some encapsulation.  We make sure we have
   some room on the eval and specpdl stacks, and bind entering_debugger
   to 1 during this call.  This is used to trap errors that may occur
   when entering the debugger (e.g. the value of `debugger' is invalid),
   so that the debugger will not be recursively entered if debug-on-error
   is set. (Otherwise, XEmacs would infinitely recurse, attempting to
   enter the debugger.) entering_debugger gets reset to 0 as soon
   as a backtrace is displayed, so that further errors can indeed be
   handled normally.

   We also establish a catch for `debugger'.  If the debugger function
   throws to this instead of returning a value, it means that the user
   pressed 'c' (pretend like the debugger was never entered).  The
   function then returns Qunbound. (If the user pressed 'r', for
   return a value, then the debugger function returns normally with
   this value.)

   The difference between 'c' and 'r' is as follows:

   debug-on-call:
     No difference.  The call proceeds as normal.
   debug-on-exit:
     With 'r', the specified value is returned as the function's
     return value.  With 'c', the value that would normally be
     returned is returned.
   signal:
     With 'r', the specified value is returned as the return
     value of `signal'. (This is the only time that `signal'
     can return, instead of making a non-local exit.) With `c',
     `signal' will continue looking for handlers as if the
     debugger was never entered, and will probably end up
     throwing to a handler or to top-level.
*/

static Lisp_Object
call_debugger (Lisp_Object arg)
{
  int threw;
  Lisp_Object val;
  int speccount;

  debug_on_next_call = 0;

  if (inhibit_flags & INHIBIT_ENTERING_DEBUGGER)
    {
      if (!(inhibit_flags & INHIBIT_WARNING_ISSUE))
	warn_when_safe
	  (Qdebugger, current_warning_level (),
	   "Unable to enter debugger within critical section");
      return Qunbound;
    }

  if (lisp_eval_depth + 20 > max_lisp_eval_depth)
    max_lisp_eval_depth = lisp_eval_depth + 20;
  if (specpdl_size + 40 > max_specpdl_size)
    max_specpdl_size = specpdl_size + 40;

  speccount = internal_bind_int (&entering_debugger, 1);
  val = internal_catch (Qdebugger, call_debugger_259, arg, &threw, 0, 0);

  return unbind_to_1 (speccount, ((threw)
				? Qunbound /* Not returning a value */
				: val));
}

/* Called when debug-on-exit behavior is called for.  Enter the debugger
   with the appropriate args for this.  VAL is the exit value that is
   about to be returned. */

static Lisp_Object
do_debug_on_exit (Lisp_Object val)
{
  /* This is falsified by call_debugger */
  Lisp_Object v = call_debugger (list2 (Qexit, val));

  return !UNBOUNDP (v) ? v : val;
}

/* Called when debug-on-call behavior is called for.  Enter the debugger
   with the appropriate args for this.  VAL is either t for a call
   through `eval' or `lambda' for a call through `funcall'.

   #### The differentiation here between EVAL and FUNCALL is bogus.
   FUNCALL can be defined as

   (defmacro func (fun &rest args)
     (cons (eval fun) args))

   and should be treated as such.
 */

static void
do_debug_on_call (Lisp_Object code)
{
  debug_on_next_call = 0;
  backtrace_list->debug_on_exit = 1;
  call_debugger (list1 (code));
}

/* LIST is the value of one of the variables `debug-on-error',
   `debug-on-signal', `stack-trace-on-error', or `stack-trace-on-signal',
   and CONDITIONS is the list of error conditions associated with
   the error being signalled.  This returns non-nil if LIST
   matches CONDITIONS. (A nil value for LIST does not match
   CONDITIONS.  A non-list value for LIST does match CONDITIONS.
   A list matches CONDITIONS when one of the symbols in LIST is the
   same as one of the symbols in CONDITIONS.) */

static int
wants_debugger (Lisp_Object list, Lisp_Object conditions)
{
  if (NILP (list))
    return 0;
  if (! CONSP (list))
    return 1;

  while (CONSP (conditions))
    {
      Lisp_Object curr, tail;
      curr = XCAR (conditions);
      for (tail = list; CONSP (tail); tail = XCDR (tail))
	if (EQ (XCAR (tail), curr))
	  return 1;
      conditions = XCDR (conditions);
    }
  return 0;
}


/* Return 1 if an error with condition-symbols CONDITIONS,
   and described by SIGNAL-DATA, should skip the debugger
   according to debug-ignored-errors.  */

static int
skip_debugger (Lisp_Object conditions, Lisp_Object data)
{
  /* This function can GC */
  Lisp_Object tail;
  int first_string = 1;
  Lisp_Object error_message = Qnil;

  for (tail = Vdebug_ignored_errors; CONSP (tail); tail = XCDR (tail))
    {
      if (STRINGP (XCAR (tail)))
	{
	  if (first_string)
	    {
	      error_message = Ferror_message_string (data);
	      first_string = 0;
	    }
	  if (fast_lisp_string_match (XCAR (tail), error_message) >= 0)
	    return 1;
	}
      else
	{
	  Lisp_Object contail;

          for (contail = conditions; CONSP (contail); contail = XCDR (contail))
            if (EQ (XCAR (tail), XCAR (contail)))
	      return 1;
	}
    }

  return 0;
}

/* Actually generate a backtrace on STREAM. */

static Lisp_Object
backtrace_259 (Lisp_Object stream)
{
  return Fbacktrace (stream, Qt);
}

#ifdef DEBUG_XEMACS

static void
trace_out_and_die (Lisp_Object err)
{
  Fdisplay_error (err, Qt);
  backtrace_259 (Qnil);
  stderr_out ("XEmacs exiting to debugger.\n");
  Fforce_debugging_signal (Qt);
  /* Unlikely to be reached */
}

#endif

/* An error was signaled.  Maybe call the debugger, if the `debug-on-error'
   etc. variables call for this.  CONDITIONS is the list of conditions
   associated with the error being signalled.  SIG is the actual error
   being signalled, and DATA is the associated data (these are exactly
   the same as the arguments to `signal').  ACTIVE_HANDLERS is the
   list of error handlers that are to be put in place while the debugger
   is called.  This is generally the remaining handlers that are
   outside of the innermost handler trapping this error.  This way,
   if the same error occurs inside of the debugger, you usually don't get
   the debugger entered recursively.

   This function returns Qunbound if it didn't call the debugger or if
   the user asked (through 'c') that XEmacs should pretend like the
   debugger was never entered.  Otherwise, it returns the value
   that the user specified with `r'. (Note that much of the time,
   the user will abort with C-], and we will never have a chance to
   return anything at all.)

   SIGNAL_VARS_ONLY means we should only look at debug-on-signal
   and stack-trace-on-signal to control whether we do anything.
   This is so that debug-on-error doesn't make handled errors
   cause the debugger to get invoked.

   STACK_TRACE_DISPLAYED and DEBUGGER_ENTERED are used so that
   those functions aren't done more than once in a single `signal'
   session. */

static Lisp_Object
signal_call_debugger (Lisp_Object conditions,
                      Lisp_Object sig, Lisp_Object data,
                      Lisp_Object active_handlers,
		      int signal_vars_only,
		      int *stack_trace_displayed,
		      int *debugger_entered)
{
#ifdef PIGS_FLY_AND_ALL_C_CODE_CAN_HANDLE_GC_OCCURRING_ALMOST_ANYWHERE
  /* This function can GC */
#else /* reality check */
  /* This function cannot GC because it inhibits GC during its operation */
#endif

  Lisp_Object val = Qunbound;
  Lisp_Object all_handlers = Vcondition_handlers;
  Lisp_Object temp_data = Qnil;
  int outer_speccount = specpdl_depth();
  int speccount;

#ifdef PIGS_FLY_AND_ALL_C_CODE_CAN_HANDLE_GC_OCCURRING_ALMOST_ANYWHERE
  struct gcpro gcpro1, gcpro2;
  GCPRO2 (all_handlers, temp_data);
#else
  begin_gc_forbidden ();
#endif

  speccount = specpdl_depth();

  Vcondition_handlers = active_handlers;

  temp_data = Fcons (sig, data); /* needed for skip_debugger */

  if (!entering_debugger && !*stack_trace_displayed && !signal_vars_only
      && wants_debugger (Vstack_trace_on_error, conditions)
      && !skip_debugger (conditions, temp_data))
    {
      specbind (Qdebug_on_error,	Qnil);
      specbind (Qstack_trace_on_error,	Qnil);
      specbind (Qdebug_on_signal,	Qnil);
      specbind (Qstack_trace_on_signal, Qnil);

      if (!noninteractive)
	internal_with_output_to_temp_buffer (build_ascstring ("*Backtrace*"),
					     backtrace_259,
					     Qnil,
					     Qnil);
      else /* in batch mode, we want this going to stderr. */
	backtrace_259 (Qnil);
      unbind_to (speccount);
      *stack_trace_displayed = 1;
    }

  if (!entering_debugger && !*debugger_entered && !signal_vars_only
      && (EQ (sig, Qquit)
	  ? debug_on_quit
	  : wants_debugger (Vdebug_on_error, conditions))
      && !skip_debugger (conditions, temp_data))
    {
      debug_on_quit &= ~2;	/* reset critical bit */

      specbind (Qdebug_on_error,	Qnil);
      specbind (Qstack_trace_on_error,	Qnil);
      specbind (Qdebug_on_signal,	Qnil);
      specbind (Qstack_trace_on_signal, Qnil);

#ifdef DEBUG_XEMACS
      if (noninteractive)
	trace_out_and_die (Fcons (sig, data));
#endif

      val = call_debugger (list2 (Qerror, (Fcons (sig, data))));
      unbind_to (speccount);
      *debugger_entered = 1;
    }

  if (!entering_debugger && !*stack_trace_displayed
      && wants_debugger (Vstack_trace_on_signal, conditions))
    {
      specbind (Qdebug_on_error,	Qnil);
      specbind (Qstack_trace_on_error,	Qnil);
      specbind (Qdebug_on_signal,	Qnil);
      specbind (Qstack_trace_on_signal, Qnil);

      if (!noninteractive)
	internal_with_output_to_temp_buffer (build_ascstring ("*Backtrace*"),
					     backtrace_259,
					     Qnil,
					     Qnil);
      else /* in batch mode, we want this going to stderr. */
	backtrace_259 (Qnil);
      unbind_to (speccount);
      *stack_trace_displayed = 1;
    }

  if (!entering_debugger && !*debugger_entered
      && (EQ (sig, Qquit)
	  ? debug_on_quit
	  : wants_debugger (Vdebug_on_signal, conditions)))
    {
      debug_on_quit &= ~2;	/* reset critical bit */

      specbind (Qdebug_on_error,	Qnil);
      specbind (Qstack_trace_on_error,	Qnil);
      specbind (Qdebug_on_signal,	Qnil);
      specbind (Qstack_trace_on_signal, Qnil);

#ifdef DEBUG_XEMACS
      if (noninteractive)
	trace_out_and_die (Fcons (sig, data));
#endif

      val = call_debugger (list2 (Qerror, (Fcons (sig, data))));
      *debugger_entered = 1;
    }

#ifdef PIGS_FLY_AND_ALL_C_CODE_CAN_HANDLE_GC_OCCURRING_ALMOST_ANYWHERE
  UNGCPRO;
#endif
  Vcondition_handlers = all_handlers;
  return unbind_to_1 (outer_speccount, val);
}


/************************************************************************/
/*		       The basic special forms				*/
/************************************************************************/

/* Except for Fprogn(), the basic special operators below are only called
   from interpreted code.  The byte compiler turns them into bytecodes. */

DEFUN ("or", For, 0, UNEVALLED, 0, /*
Eval ARGS until one of them yields non-nil, then return that value.
The remaining ARGS are not evalled at all.
If all args return nil, return nil.

Any multiple values from the last form, and only from the last form, are
passed back.  See `values' and `multiple-value-bind'. 

arguments: (&rest ARGS)
*/
       (args))
{
  /* This function can GC */
  Lisp_Object val = Qnil;

  LIST_LOOP_3 (arg, args, tail)
    {
      if (!NILP (IGNORE_MULTIPLE_VALUES (val = Feval (arg))))
	{
	  if (NILP (XCDR (tail)))
	    {
	      /* Pass back multiple values if this is the last one: */
	      return val;
	    }

	  return IGNORE_MULTIPLE_VALUES (val);
	}
    }

  return val;
}

DEFUN ("and", Fand, 0, UNEVALLED, 0, /*
Eval ARGS until one of them yields nil, then return nil.
The remaining ARGS are not evalled at all.
If no arg yields nil, return the last arg's value.

Any multiple values from the last form, and only from the last form, are
passed back.  See `values' and `multiple-value-bind'. 

arguments: (&rest ARGS)
*/
       (args))
{
  /* This function can GC */
  Lisp_Object val = Qt;

  LIST_LOOP_3 (arg, args, tail)
    {
      if (NILP (IGNORE_MULTIPLE_VALUES (val = Feval (arg))))
	{
	  if (NILP (XCDR (tail)))
	    {
	      /* Pass back any multiple values for the last form: */
	      return val;
	    }

	  return Qnil;
	}
    }

  return val;
}

DEFUN ("if", Fif, 2, UNEVALLED, 0, /*
If COND yields non-nil, do THEN, else do ELSE.
Returns the value of THEN or the value of the last of the ELSE's.
THEN must be one expression, but ELSE... can be zero or more expressions.
If COND yields nil, and there are no ELSE's, the value is nil.

arguments: (COND THEN &rest ELSE)
*/
       (args))
{
  /* This function can GC */
  Lisp_Object condition  = XCAR (args);
  Lisp_Object then_form  = XCAR (XCDR (args));
  Lisp_Object else_forms = XCDR (XCDR (args));

  if (!NILP (IGNORE_MULTIPLE_VALUES (Feval (condition))))
    return Feval (then_form);
  else
    return Fprogn (else_forms);
}

/* Macros `when' and `unless' are trivially defined in Lisp,
   but it helps for bootstrapping to have them ALWAYS defined. */

DEFUN ("when", Fwhen, 1, MANY, 0, /*
If COND yields non-nil, do BODY, else return nil.
BODY can be zero or more expressions.  If BODY is nil, return nil.

arguments: (COND &rest BODY)
*/
       (int nargs, Lisp_Object *args))
{
  Lisp_Object cond = args[0];
  Lisp_Object body;
 
  switch (nargs)
    {
    case 1:  body = Qnil; break;
    case 2:  body = args[1]; break;
    default: body = Fcons (Qprogn, Flist (nargs-1, args+1)); break;
    }

  return list3 (Qif, cond, body);
}

DEFUN ("unless", Funless, 1, MANY, 0, /*
If COND yields nil, do BODY, else return nil.
BODY can be zero or more expressions.  If BODY is nil, return nil.

arguments: (COND &rest BODY)
*/
       (int nargs, Lisp_Object *args))
{
  Lisp_Object cond = args[0];
  Lisp_Object body = Flist (nargs-1, args+1);
  return Fcons (Qif, Fcons (cond, Fcons (Qnil, body)));
}

DEFUN ("cond", Fcond, 0, UNEVALLED, 0, /*
Try each clause until one succeeds.
Each clause looks like (CONDITION BODY...).  CONDITION is evaluated
and, if the value is non-nil, this clause succeeds:
then the expressions in BODY are evaluated and the last one's
value is the value of the cond-form.
If no clause succeeds, cond returns nil.
If a clause has one element, as in (CONDITION),
CONDITION's value if non-nil is returned from the cond-form.

arguments: (&rest CLAUSES)
*/
       (args))
{
  /* This function can GC */
  REGISTER Lisp_Object val;

  LIST_LOOP_2 (clause, args)
    {
      CHECK_CONS (clause);
      if (!NILP (val = IGNORE_MULTIPLE_VALUES (Feval (XCAR (clause)))))
	{
	  if (!NILP (clause = XCDR (clause)))
	    {
	      CHECK_TRUE_LIST (clause);
	      /* Pass back any multiple values here: */
	      val = Fprogn (clause);
	    }
	  return val;
	}
    }

  return Qnil;
}

DEFUN ("progn", Fprogn, 0, UNEVALLED, 0, /*
Eval BODY forms sequentially and return value of last one.

arguments: (&rest BODY)
*/
       (args))
{
  /* This function can GC */
  /* Caller must provide a true list in ARGS */
  REGISTER Lisp_Object val = Qnil;
  struct gcpro gcpro1;

  GCPRO1 (args);

  {
    LIST_LOOP_2 (form, args)
      val = Feval (form);
  }

  UNGCPRO;
  return val;
}

/* Fprog1() is the canonical example of a function that must GCPRO a
   Lisp_Object across calls to Feval(). */

DEFUN ("prog1", Fprog1, 1, UNEVALLED, 0, /*
Similar to `progn', but the value of the first form is returned.

All the arguments are evaluated sequentially.  The value of FIRST is saved
during evaluation of the remaining args, whose values are discarded.

arguments: (FIRST &rest BODY)
*/
       (args))
{
  Lisp_Object val;
  struct gcpro gcpro1;

  val = IGNORE_MULTIPLE_VALUES (Feval (Fcar (args)));

  GCPRO1 (val);

  {
    LIST_LOOP_2 (form, XCDR (args))
      Feval (form);
  }

  UNGCPRO;
  return val;
}

DEFUN ("prog2", Fprog2, 2, UNEVALLED, 0, /*
Similar to `progn', but the value of the second form is returned.

All the arguments are evaluated sequentially.  The value of SECOND is saved
during evaluation of the remaining args, whose values are discarded.

arguments: (FIRST SECOND &rest BODY)
*/
       (args))
{
  /* This function can GC */
  Lisp_Object val;
  struct gcpro gcpro1;

  Feval (XCAR (args));
  args = XCDR (args);

  val = IGNORE_MULTIPLE_VALUES (Feval (XCAR (args)));

  args = XCDR (args);

  GCPRO1 (val);

  {
    LIST_LOOP_2 (form, args)
      Feval (form);
  }

  UNGCPRO;
  return val;
}

DEFUN ("let*", FletX, 1, UNEVALLED, 0, /*
Bind variables according to VARLIST then eval BODY.
The value of the last form in BODY is returned.
Each element of VARLIST is a symbol (which is bound to nil)
or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
Each VALUEFORM can refer to the symbols already bound by this VARLIST.

arguments: (VARLIST &rest BODY)
*/
       (args))
{
  /* This function can GC */
  Lisp_Object varlist = XCAR (args);
  Lisp_Object body    = XCDR (args);
  int speccount = specpdl_depth();

  EXTERNAL_LIST_LOOP_3 (var, varlist, tail)
    {
      Lisp_Object symbol, value, tem;
      if (SYMBOLP (var))
	symbol = var, value = Qnil;
      else
	{
	  CHECK_CONS (var);
	  symbol = XCAR (var);
	  tem    = XCDR (var);
	  if (NILP (tem))
	    value = Qnil;
	  else
	    {
	      CHECK_CONS (tem);
              value = IGNORE_MULTIPLE_VALUES (Feval (XCAR (tem)));
	      if (!NILP (XCDR (tem)))
		sferror
		  ("`let' bindings can have only one value-form", var);
	    }
	}
      specbind (symbol, value);
    }
  return unbind_to_1 (speccount, Fprogn (body));
}

DEFUN ("let", Flet, 1, UNEVALLED, 0, /*
Bind variables according to VARLIST then eval BODY.
The value of the last form in BODY is returned.
Each element of VARLIST is a symbol (which is bound to nil)
or a list (SYMBOL VALUEFORM) (which binds SYMBOL to the value of VALUEFORM).
All the VALUEFORMs are evalled before any symbols are bound.

arguments: (VARLIST &rest BODY)
*/
       (args))
{
  /* This function can GC */
  Lisp_Object varlist = XCAR (args);
  Lisp_Object body    = XCDR (args);
  int speccount = specpdl_depth();
  Lisp_Object *temps;
  int idx;
  struct gcpro gcpro1;

  /* Make space to hold the values to give the bound variables. */
  {
    int varcount;
    GET_EXTERNAL_LIST_LENGTH (varlist, varcount);
    temps = alloca_array (Lisp_Object, varcount);
  }

  /* Compute the values and store them in `temps' */
  GCPRO1 (*temps);
  gcpro1.nvars = 0;

  idx = 0;
  {
    LIST_LOOP_2 (var, varlist)
      {
	Lisp_Object *value = &temps[idx++];
	if (SYMBOLP (var))
	  *value = Qnil;
	else
	  {
	    Lisp_Object tem;
	    CHECK_CONS (var);
	    tem = XCDR (var);
	    if (NILP (tem))
	      *value = Qnil;
	    else
	      {
		CHECK_CONS (tem);
                *value = IGNORE_MULTIPLE_VALUES (Feval (XCAR (tem)));
		gcpro1.nvars = idx;

		if (!NILP (XCDR (tem)))
		  sferror
		    ("`let' bindings can have only one value-form", var);
	      }
	  }
      }
  }

  idx = 0;
  {
    LIST_LOOP_2 (var, varlist)
      {
	specbind (SYMBOLP (var) ? var : XCAR (var), temps[idx++]);
      }
  }

  UNGCPRO;

  return unbind_to_1 (speccount, Fprogn (body));
}

DEFUN ("while", Fwhile, 1, UNEVALLED, 0, /*
If TEST yields non-nil, eval BODY... and repeat.
The order of execution is thus TEST, BODY, TEST, BODY and so on
until TEST returns nil.

arguments: (TEST &rest BODY)
*/
       (args))
{
  /* This function can GC */
  Lisp_Object test = XCAR (args);
  Lisp_Object body = XCDR (args);

  while (!NILP (IGNORE_MULTIPLE_VALUES (Feval (test))))
    {
      QUIT;
      Fprogn (body);
    }

  return Qnil;
}

DEFUN ("setq", Fsetq, 0, UNEVALLED, 0, /*
\(setq SYM VAL SYM VAL ...): set each SYM to the value of its VAL.
The symbols SYM are variables; they are literal (not evaluated).
The values VAL are expressions; they are evaluated.
Thus, (setq x (1+ y)) sets `x' to the value of `(1+ y)'.
The second VAL is not computed until after the first SYM is set, and so on;
each VAL can use the new value of variables set earlier in the `setq'.
The return value of the `setq' form is the value of the last VAL.
*/
       (args))
{
  /* This function can GC */
  int nargs;
  Lisp_Object retval = Qnil;

  GET_LIST_LENGTH (args, nargs);

  if (nargs & 1)		/* Odd number of arguments? */
    Fsignal (Qwrong_number_of_arguments, list2 (Qsetq, make_int (nargs)));

  GC_PROPERTY_LIST_LOOP_3 (symbol, val, args)
    {
      val = Feval (val);
      val = IGNORE_MULTIPLE_VALUES (val);
      Fset (symbol, val);
      retval = val;
    }

  END_GC_PROPERTY_LIST_LOOP (symbol);

  return retval;
}

DEFUN ("quote", Fquote, 1, UNEVALLED, 0, /*
Return the argument, without evaluating it.  `(quote x)' yields `x'.

`quote' differs from `function' in that it is a hint that an expression is
data, not a function.  In particular, under some circumstances the byte
compiler will compile an expression quoted with `function', but it will
never do so for an expression quoted with `quote'.  These issues are most
important for lambda expressions (see `lambda').

There is an alternative, more readable, reader syntax for `quote':  a Lisp
object preceded by `''. Thus, `'x' is equivalent to `(quote x)', in all
contexts.  A print function may use either.  Internally the expression is
represented as `(quote x)').

arguments: (OBJECT)
*/
       (args))
{
  int nargs;

  GET_LIST_LENGTH (args, nargs);
  if (nargs != 1)
    {
      Fsignal (Qwrong_number_of_arguments,
               list2 (Qquote, make_int (nargs)));
    }

  return XCAR (args);
}

/* Originally, this was just a function -- but `custom' used a garden-
   variety version, so why not make it a subr?  */
DEFUN ("quote-maybe", Fquote_maybe, 1, 1, 0, /*
Quote EXPR if it is not self quoting.

In contrast with `quote', this is a function, not a special form; its
argument is evaluated before `quote-maybe' is called.  It returns either
EXPR (if it is self-quoting) or a list `(quote EXPR)' if it is not
self-quoting.  Lists starting with the symbol `lambda' are regarded as
self-quoting.
*/
       (expr))
{
  if ((XTYPE (expr)) == Lisp_Type_Record)
    {
      switch (XRECORD_LHEADER (expr)->type)
        {
        case lrecord_type_symbol:
          if (NILP (expr) || (EQ (expr, Qt)) || SYMBOL_IS_KEYWORD (expr))
            {
              return expr;
            }
          break;
        case lrecord_type_cons: 
          if (EQ (XCAR (expr), Qlambda))
            {
              return expr;
            }
          break;

        case lrecord_type_vector:
        case lrecord_type_string:
        case lrecord_type_compiled_function:
        case lrecord_type_bit_vector:
        case lrecord_type_float:
        case lrecord_type_hash_table:
        case lrecord_type_char_table:
        case lrecord_type_range_table:
        case lrecord_type_bignum:
        case lrecord_type_ratio:
        case lrecord_type_bigfloat:
          return expr;
        }
      return list2 (Qquote, expr);
    }

  /* Fixnums and characters are self-quoting: */
  return expr;
}

DEFUN ("function", Ffunction, 1, UNEVALLED, 0, /*
Return the argument, without evaluating it.  `(function x)' yields `x'.

`function' differs from `quote' in that it is a hint that an expression is
a function, not data.  In particular, under some circumstances the byte
compiler will compile an expression quoted with `function', but it will
never do so for an expression quoted with `quote'.  However, the byte
compiler will not compile an expression buried in a data structure such as
a vector or a list which is not syntactically a function.  These issues are
most important for lambda expressions (see `lambda').

There is an alternative, more readable, reader syntax for `function':  a Lisp
object preceded by `#''. Thus, #'x is equivalent to (function x), in all
contexts.  A print function may use either.  Internally the expression is
represented as `(function x)').

arguments: (SYMBOL-OR-LAMBDA)
*/
       (args))
{
  int nargs;

  GET_LIST_LENGTH (args, nargs);
  if (nargs != 1)
    {
      Fsignal (Qwrong_number_of_arguments,
               list2 (Qfunction, make_int (nargs)));
    }

  return XCAR (args);
}


/************************************************************************/
/*			Defining functions/variables			*/
/************************************************************************/
static Lisp_Object
define_function (Lisp_Object name, Lisp_Object defn)
{
  Ffset (name, defn);
  LOADHIST_ATTACH (Fcons (Qdefun, name));
  return name;
}

DEFUN ("defun", Fdefun, 2, UNEVALLED, 0, /*
Define NAME as a function.
The definition is (lambda ARGLIST [DOCSTRING] BODY...).
See also the function `interactive'.

arguments: (NAME ARGLIST &optional DOCSTRING &rest BODY)
*/
       (args))
{
  /* This function can GC */
  return define_function (XCAR (args),
			  Fcons (Qlambda, XCDR (args)));
}

DEFUN ("defmacro", Fdefmacro, 2, UNEVALLED, 0, /*
Define NAME as a macro.
The definition is (macro lambda ARGLIST [DOCSTRING] BODY...).
When the macro is called, as in (NAME ARGS...),
the function (lambda ARGLIST BODY...) is applied to
the list ARGS... as it appears in the expression,
and the result should be a form to be evaluated instead of the original.

arguments: (NAME ARGLIST &optional DOCSTRING &rest BODY)
*/
       (args))
{
  /* This function can GC */
  return define_function (XCAR (args),
			  Fcons (Qmacro, Fcons (Qlambda, XCDR (args))));
}

DEFUN ("defvar", Fdefvar, 1, UNEVALLED, 0, /*
Define SYMBOL as a variable.
You are not required to define a variable in order to use it,
 but the definition can supply documentation and an initial value
 in a way that tags can recognize.

INITVALUE is evaluated, and used to set SYMBOL, only if SYMBOL's value is
 void. (However, when you evaluate a defvar interactively, it acts like a
 defconst: SYMBOL's value is always set regardless of whether it's currently
 void.)
If SYMBOL is buffer-local, its default value is what is set;
 buffer-local values are not affected.
INITVALUE and DOCSTRING are optional.
If DOCSTRING starts with *, this variable is identified as a user option.
 This means that M-x set-variable recognizes it.
If INITVALUE is missing, SYMBOL's value is not set.

In lisp-interaction-mode defvar is treated as defconst.

arguments: (SYMBOL &optional INITVALUE DOCSTRING)
*/
       (args))
{
  /* This function can GC */
  Lisp_Object sym = XCAR (args);

  if (!NILP (args = XCDR (args)))
    {
      Lisp_Object val = XCAR (args);

      if (NILP (Fdefault_boundp (sym)))
	{
	  struct gcpro gcpro1;
	  GCPRO1 (val);
	  val = IGNORE_MULTIPLE_VALUES (Feval (val));
	  Fset_default (sym, val);
	  UNGCPRO;
	}

      if (!NILP (args = XCDR (args)))
	{
	  Lisp_Object doc = XCAR (args);
	  Fput (sym, Qvariable_documentation, doc);
	  if (!NILP (args = XCDR (args)))
	    signal_error (Qwrong_number_of_arguments, "too many arguments", Qunbound);
	}
    }

#ifdef I18N3
  if (!NILP (Vfile_domain))
    Fput (sym, Qvariable_domain, Vfile_domain);
#endif

  LOADHIST_ATTACH (sym);
  return sym;
}

DEFUN ("defconst", Fdefconst, 2, UNEVALLED, 0, /*
Define SYMBOL as a constant variable.
The intent is that programs do not change this value, but users may.
Always sets the value of SYMBOL to the result of evalling INITVALUE.
If SYMBOL is buffer-local, its default value is what is set;
 buffer-local values are not affected.
DOCSTRING is optional.
If DOCSTRING starts with *, this variable is identified as a user option.
 This means that M-x set-variable recognizes it.

Note: do not use `defconst' for user options in libraries that are not
 normally loaded, since it is useful for users to be able to specify
 their own values for such variables before loading the library.
Since `defconst' unconditionally assigns the variable,
 it would override the user's choice.

arguments: (SYMBOL &optional INITVALUE DOCSTRING)
*/
       (args))
{
  /* This function can GC */
  Lisp_Object sym = XCAR (args);
  Lisp_Object val = Feval (XCAR (args = XCDR (args)));
  struct gcpro gcpro1;

  GCPRO1 (val);

  val = IGNORE_MULTIPLE_VALUES (val);

  Fset_default (sym, val);

  UNGCPRO;

  if (!NILP (args = XCDR (args)))
    {
      Lisp_Object doc = XCAR (args);
      Fput (sym, Qvariable_documentation, doc);
      if (!NILP (args = XCDR (args)))
	signal_error (Qwrong_number_of_arguments, "too many arguments", Qunbound);
    }

#ifdef I18N3
  if (!NILP (Vfile_domain))
    Fput (sym, Qvariable_domain, Vfile_domain);
#endif

  LOADHIST_ATTACH (sym);
  return sym;
}

/* XEmacs: user-variable-p is in symbols.c, since it needs to mess around
   with the symbol variable aliases. */

DEFUN ("macroexpand-internal", Fmacroexpand_internal, 1, 2, 0, /*
Return result of expanding macros at top level of FORM.
If FORM is not a macro call, it is returned unchanged.
Otherwise, the macro is expanded and the expansion is considered
in place of FORM.  When a non-macro-call results, it is returned.

The second optional arg ENVIRONMENT specifies an environment of macro
definitions to shadow the loaded ones for use in file byte-compilation.
*/
       (form, environment))
{
  /* This function can GC */
  /* With cleanups from Hallvard Furuseth.  */
  REGISTER Lisp_Object expander, sym, def, tem;

  while (1)
    {
      /* Come back here each time we expand a macro call,
	 in case it expands into another macro call.  */
      if (!CONSP (form))
	break;
      /* Set SYM, give DEF and TEM right values in case SYM is not a symbol. */
      def = sym = XCAR (form);
      tem = Qnil;
      /* Trace symbols aliases to other symbols
	 until we get a symbol that is not an alias.  */
      while (SYMBOLP (def))
	{
	  QUIT;
	  sym = def;
	  tem = Fassq (sym, environment);
	  if (NILP (tem))
	    {
	      def = XSYMBOL (sym)->function;
	      if (!UNBOUNDP (def))
		continue;
	    }
	  break;
	}
      /* Right now TEM is the result from SYM in ENVIRONMENT,
	 and if TEM is nil then DEF is SYM's function definition.  */
      if (NILP (tem))
	{
	  /* SYM is not mentioned in ENVIRONMENT.
	     Look at its function definition.  */
	  if (UNBOUNDP (def)
	      || !CONSP (def))
	    /* Not defined or definition not suitable */
	    break;
	  if (EQ (XCAR (def), Qautoload))
	    {
	      /* Autoloading function: will it be a macro when loaded?  */
	      tem = Felt (def, make_int (4));
	      if (EQ (tem, Qt) || EQ (tem, Qmacro))
		{
		  /* Yes, load it and try again.  */
		  /* do_autoload GCPROs both arguments */
		  do_autoload (def, sym);
		  continue;
		}
	      else
		break;
	    }
	  else if (!EQ (XCAR (def), Qmacro))
	    break;
	  else expander = XCDR (def);
	}
      else
	{
	  expander = XCDR (tem);
	  if (NILP (expander))
	    break;
	}
      form = apply1 (expander, XCDR (form));
    }
  return form;
}


/************************************************************************/
/*			    Non-local exits				*/
/************************************************************************/

#ifdef ERROR_CHECK_TRAPPING_PROBLEMS

int
proper_redisplay_wrapping_in_place (void)
{
  return !in_display
    || ((get_inhibit_flags () & INTERNAL_INHIBIT_ERRORS)
	&& (get_inhibit_flags () & INTERNAL_INHIBIT_THROWS));
}

static void
check_proper_critical_section_nonlocal_exit_protection (void)
{
  assert_with_message
    (proper_redisplay_wrapping_in_place (),
     "Attempted non-local exit from within redisplay without being properly wrapped");
}

static void
check_proper_critical_section_lisp_protection (void)
{
  assert_with_message
    (proper_redisplay_wrapping_in_place (),
     "Attempt to call Lisp code from within redisplay without being properly wrapped");
}

#endif /* ERROR_CHECK_TRAPPING_PROBLEMS */

DEFUN ("catch", Fcatch, 1, UNEVALLED, 0, /*
Eval BODY allowing nonlocal exits using `throw'.
TAG is evalled to get the tag to use.  Then the BODY is executed.
Within BODY, (throw TAG VAL) with same (`eq') tag exits BODY and this `catch'.
If no throw happens, `catch' returns the value of the last BODY form.
If a throw happens, it specifies the value to return from `catch'.

arguments: (TAG &rest BODY)
*/
       (args))
{
  /* This function can GC */
  Lisp_Object tag  = Feval (XCAR (args));
  Lisp_Object body = XCDR (args);
  return internal_catch (tag, Fprogn, body, 0, 0, 0);
}

/* Set up a catch, then call C function FUNC on argument ARG.
   FUNC should return a Lisp_Object.
   This is how catches are done from within C code. */

Lisp_Object
internal_catch (Lisp_Object tag,
                Lisp_Object (*func) (Lisp_Object arg),
                Lisp_Object arg,
                int * volatile threw,
		Lisp_Object * volatile thrown_tag,
		Lisp_Object * volatile backtrace_before_throw)
{
  /* This structure is made part of the chain `catchlist'.  */
  struct catchtag c;

  /* Fill in the components of c, and put it on the list.  */
  c.next = catchlist;
  c.tag = tag;
  c.actual_tag = Qnil;
  c.backtrace = Qnil;
  c.val = Qnil;
  c.backlist = backtrace_list;
#if 0 /* FSFmacs */
  /* #### */
  c.handlerlist = handlerlist;
#endif
  c.lisp_eval_depth = lisp_eval_depth;
  c.pdlcount = specpdl_depth();
#if 0 /* FSFmacs */
  c.poll_suppress_count = async_timer_suppress_count;
#endif
  c.gcpro = gcprolist;
  catchlist = &c;

  /* Call FUNC.  */
  if (SETJMP (c.jmp))
    {
      /* Throw works by a longjmp that comes right here.  */
      if (threw) *threw = 1;
      if (thrown_tag) *thrown_tag = c.actual_tag;
      if (backtrace_before_throw) *backtrace_before_throw = c.backtrace;
      return c.val;
    }
  c.val = (*func) (arg);
  if (threw) *threw = 0;
  if (thrown_tag) *thrown_tag = Qnil;
  catchlist = c.next;
  check_catchlist_sanity ();
  return c.val;
}


/* Unwind the specbind, catch, and handler stacks back to CATCH, and
   jump to that CATCH, returning VALUE as the value of that catch.

   This is the guts of Fthrow and Fsignal; they differ only in the
   way they choose the catch tag to throw to.  A catch tag for a
   condition-case form has a TAG of Qnil.

   Before each catch is discarded, unbind all special bindings and
   execute all unwind-protect clauses made above that catch.  Unwind
   the handler stack as we go, so that the proper handlers are in
   effect for each unwind-protect clause we run.  At the end, restore
   some static info saved in CATCH, and longjmp to the location
   specified in the

   This is used for correct unwinding in Fthrow and Fsignal.  */

static DECLARE_DOESNT_RETURN (unwind_to_catch (struct catchtag *, Lisp_Object,
					       Lisp_Object));

static DOESNT_RETURN
unwind_to_catch (struct catchtag *c, Lisp_Object val, Lisp_Object tag)
{
  REGISTER int last_time;

  /* Unwind the specbind, catch, and handler stacks back to CATCH
     Before each catch is discarded, unbind all special bindings
     and execute all unwind-protect clauses made above that catch.
     At the end, restore some static info saved in CATCH,
     and longjmp to the location specified.
     */

  /* Save the value somewhere it will be GC'ed.
     (Can't overwrite tag slot because an unwind-protect may
     want to throw to this same tag, which isn't yet invalid.) */
  c->val = val;
  c->actual_tag = tag;

#if 0 /* FSFmacs */
  /* Restore the polling-suppression count.  */
  set_poll_suppress_count (catch->poll_suppress_count);
#endif

#if 1
  do
    {
      last_time = catchlist == c;

      /* Unwind the specpdl stack, and then restore the proper set of
         handlers.  */
      unbind_to (catchlist->pdlcount);
      catchlist = catchlist->next;
      check_catchlist_sanity ();
    }
  while (! last_time);
#else
  /* Former XEmacs code.  This is definitely not as correct because
     there may be a number of catches we're unwinding, and a number
     of unwind-protects in the process.  By not undoing the catches till
     the end, there may be invalid catches still current. (This would
     be a particular problem with code like this:

     (catch 'foo
       (call-some-code-which-does...
        (catch 'bar
          (unwind-protect
              (call-some-code-which-does...
               (catch 'bar
                 (call-some-code-which-does...
                  (throw 'foo nil))))
            (throw 'bar nil)))))

     This would try to throw to the inner (catch 'bar)!

     --ben
   */
  /* Unwind the specpdl stack */
  unbind_to (c->pdlcount);
  catchlist = c->next;
  check_catchlist_sanity ();
#endif /* Former code */

  UNWIND_GCPRO_TO (c->gcpro);
  if (profiling_active)
    {
      while (backtrace_list != c->backlist)
	{
          profile_record_unwind (backtrace_list);
	  backtrace_list = backtrace_list->next;
	}
    }
  else
    backtrace_list = c->backlist;
  lisp_eval_depth = c->lisp_eval_depth;

#ifdef DEFEND_AGAINST_THROW_RECURSION
  throw_level = 0;
#endif
  LONGJMP (c->jmp, 1);
}

DECLARE_DOESNT_RETURN (throw_or_bomb_out_unsafe (Lisp_Object, Lisp_Object, int,
					  Lisp_Object, Lisp_Object));

DOESNT_RETURN
throw_or_bomb_out_unsafe (Lisp_Object tag, Lisp_Object val, int bomb_out_p,
			  Lisp_Object sig, Lisp_Object data)
{
  /* If bomb_out_p is t, this is being called from Fsignal as a
     "last resort" when there is no handler for this error and
      the debugger couldn't be invoked, so we are throwing to
     `top-level'.  If this tag doesn't exist (happens during the
     initialization stages) we would get in an infinite recursive
     Fsignal/Fthrow loop, so instead we bomb out to the
     really-early-error-handler.

     Note that in fact the only time that the "last resort"
     occurs is when there's no catch for `top-level' -- the
     `top-level' catch and the catch-all error handler are
     established at the same time, in initial_command_loop/
     top_level_1.

     [[#### Fix this horrifitude!]]

     I don't think this is horrifitude, just defensive programming. --ben
     */

  while (1)
    {
      REGISTER struct catchtag *c;

#if 0 /* FSFmacs */
      if (!NILP (tag)) /* #### */
#endif
      for (c = catchlist; c; c = c->next)
	{
	  if (EQ (c->tag, Vcatch_everything_tag))
	    c->backtrace = maybe_get_trapping_problems_backtrace ();
	  if (EQ (c->tag, tag) || EQ (c->tag, Vcatch_everything_tag))
	    unwind_to_catch (c, val, tag);
	}
      if (!bomb_out_p)
        tag = Fsignal (Qno_catch, list2 (tag, val));
      else
        call1 (Qreally_early_error_handler, Fcons (sig, data));
    }
}
  
DECLARE_DOESNT_RETURN (throw_or_bomb_out (Lisp_Object, Lisp_Object, int,
					  Lisp_Object, Lisp_Object));

DOESNT_RETURN
throw_or_bomb_out (Lisp_Object tag, Lisp_Object val, int bomb_out_p,
		   Lisp_Object sig, Lisp_Object data)
{
#ifdef DEFEND_AGAINST_THROW_RECURSION
  /* die if we recurse more than is reasonable */
  assert (++throw_level <= 20);
#endif

#ifdef ERROR_CHECK_TRAPPING_PROBLEMS
  check_proper_critical_section_nonlocal_exit_protection ();
#endif
  throw_or_bomb_out_unsafe (tag, val, bomb_out_p, sig, data);
}

/* See above, where CATCHLIST is defined, for a description of how
   Fthrow() works.

   Fthrow() is also called by Fsignal(), to do a non-local jump
   back to the appropriate condition-case handler after (maybe)
   the debugger is entered.  In that case, TAG is the value
   of Vcondition_handlers that was in place just after the
   condition-case handler was set up.  The car of this will be
   some data referring to the handler: Its car will be Qunbound
   (thus, this tag can never be generated by Lisp code), and
   its CDR will be the HANDLERS argument to condition_case_1()
   (either Qerror, Qt, or a list of handlers as in `condition-case').
   This works fine because Fthrow() does not care what TAG was
   passed to it: it just looks up the catch list for something
   that is EQ() to TAG.  When it finds it, it will longjmp()
   back to the place that established the catch (in this case,
   condition_case_1).  See below for more info.
*/

DEFUN_NORETURN ("throw", Fthrow, 2, UNEVALLED, 0, /*
Throw to the catch for TAG and return VALUE from it.

Both TAG and VALUE are evalled, and multiple values in VALUE will be passed
back.  Tags are the same if and only if they are `eq'.

arguments: (TAG VALUE)
*/
       (args))
{
  int nargs;
  Lisp_Object tag, value;

  GET_LIST_LENGTH (args, nargs);
  if (nargs != 2)
    {
      Fsignal (Qwrong_number_of_arguments, list2 (Qthrow, make_int (nargs)));
    }

  tag = IGNORE_MULTIPLE_VALUES (Feval (XCAR(args)));

  value = Feval (XCAR (XCDR (args)));

  throw_or_bomb_out (tag, value, 0, Qnil, Qnil); /* Doesn't return */
  RETURN_NOT_REACHED (Qnil);
}

DEFUN ("unwind-protect", Funwind_protect, 1, UNEVALLED, 0, /*
Do BODYFORM, protecting with UNWINDFORMS.
If BODYFORM completes normally, its value is returned
after executing the UNWINDFORMS.
If BODYFORM exits nonlocally, the UNWINDFORMS are executed anyway.

arguments: (BODYFORM &rest UNWINDFORMS)
*/
       (args))
{
  /* This function can GC */
  int speccount = specpdl_depth();

  record_unwind_protect (Fprogn, XCDR (args));
  return unbind_to_1 (speccount, Feval (XCAR (args)));
}


/************************************************************************/
/*                           Trapping errors                            */
/************************************************************************/

static Lisp_Object
condition_bind_unwind (Lisp_Object loser)
{
  /* There is no problem freeing stuff here like there is in
     condition_case_unwind(), because there are no outside pointers
     (like the tag below in the catchlist) pointing to the objects. */
  
  /* ((handler-fun . handler-args) ... other handlers) */
  Lisp_Object tem = XCAR (loser);
  int first = 1;

  while (CONSP (tem))
    {
      Lisp_Object victim = tem;
      if (first && OPAQUE_PTRP (XCAR (victim)))
	free_opaque_ptr (XCAR (victim));
      first = 0;
      tem = XCDR (victim);
      free_cons (victim);
    }

  if (EQ (loser, Vcondition_handlers)) /* may have been rebound to some tail */
    Vcondition_handlers = XCDR (loser);

  free_cons (loser);
  return Qnil;
}

static Lisp_Object
condition_case_unwind (Lisp_Object loser)
{
  /* ((<unbound> . clauses) ... other handlers */
  /* NO! Doing this now leaves the tag deleted in a still-active
     catch.  With the recent changes to unwind_to_catch(), the
     evil situation might not happen any more; it certainly could
     happen before because it did.  But it's very precarious to rely
     on something like this.  #### Instead we should rewrite, adopting
     the FSF's mechanism with a struct handler instead of
     Vcondition_handlers; then we have NO Lisp-object structures used
     to hold all of the values, and there's no possibility either of
     crashes from freeing objects too quickly, or objects not getting
     freed and hanging around till the next GC.

     In practice, the extra consing here should not matter because
     it only happens when we throw past the condition-case, which almost
     always is the result of an error.  Most of the time, there will be
     no error, and we will free the objects below in the main function.

     --ben

     DO NOT DO: free_cons (XCAR (loser));
     */

  if (EQ (loser, Vcondition_handlers)) /* may have been rebound to some tail */
    Vcondition_handlers = XCDR (loser);

  /* DO NOT DO: free_cons (loser); */
  return Qnil;
}

/* Split out from condition_case_3 so that primitive C callers
   don't have to cons up a lisp handler form to be evaluated. */

/* Call a function BFUN of one argument BARG, trapping errors as
   specified by HANDLERS.  If no error occurs that is indicated by
   HANDLERS as something to be caught, the return value of this
   function is the return value from BFUN.  If such an error does
   occur, HFUN is called, and its return value becomes the
   return value of condition_case_1().  The second argument passed
   to HFUN will always be HARG.  The first argument depends on
   HANDLERS:

   If HANDLERS is Qt, all errors (this includes QUIT, but not
   non-local exits with `throw') cause HFUN to be invoked, and VAL
   (the first argument to HFUN) is a cons (SIG . DATA) of the
   arguments passed to `signal'.  The debugger is not invoked even if
   `debug-on-error' was set.

   A HANDLERS value of Qerror is the same as Qt except that the
   debugger is invoked if `debug-on-error' was set.

   Otherwise, HANDLERS should be a list of lists (CONDITION-NAME BODY ...)
   exactly as in `condition-case', and errors will be trapped
   as indicated in HANDLERS.  VAL (the first argument to HFUN) will
   be a cons whose car is the cons (SIG . DATA) and whose CDR is the
   list (BODY ...) from the appropriate slot in HANDLERS.

   This function pushes HANDLERS onto the front of Vcondition_handlers
   (actually with a Qunbound marker as well -- see Fthrow() above
   for why), establishes a catch whose tag is this new value of
   Vcondition_handlers, and calls BFUN.  When Fsignal() is called,
   it calls Fthrow(), setting TAG to this same new value of
   Vcondition_handlers and setting VAL to the same thing that will
   be passed to HFUN, as above.  Fthrow() longjmp()s back to the
   jump point we just established, and we in turn just call the
   HFUN and return its value.

   For a real condition-case, HFUN will always be
   run_condition_case_handlers() and HARG is the argument VAR
   to condition-case.  That function just binds VAR to the cons
   (SIG . DATA) that is the CAR of VAL, and calls the handler
   (BODY ...) that is the CDR of VAL.  Note that before calling
   Fthrow(), Fsignal() restored Vcondition_handlers to the value
   it had *before* condition_case_1() was called.  This maintains
   consistency (so that the state of things at exit of
   condition_case_1() is the same as at entry), and implies
   that the handler can signal the same error again (possibly
   after processing of its own), without getting in an infinite
   loop. */

Lisp_Object
condition_case_1 (Lisp_Object handlers,
                  Lisp_Object (*bfun) (Lisp_Object barg),
                  Lisp_Object barg,
                  Lisp_Object (*hfun) (Lisp_Object val, Lisp_Object harg),
                  Lisp_Object harg)
{
  int speccount = specpdl_depth();
  struct catchtag c;
  struct gcpro gcpro1, gcpro2, gcpro3;

#if 0 /* FSFmacs */
  c.tag = Qnil;
#else
  /* Do consing now so out-of-memory error happens up front */
  /* (unbound . stuff) is a special condition-case kludge marker
     which is known specially by Fsignal.
     [[ This is an abomination, but to fix it would require either
     making condition_case cons (a union of the conditions of the clauses)
     or changing the byte-compiler output (no thanks).]]

     The above comment is clearly wrong.  FSF does not do it this way
     and did not change the byte-compiler output.  Instead they use a
     `struct handler' to hold the various values (in place of our
     Vcondition_handlers) and chain them together, with pointers from
     the `struct catchtag' to the `struct handler'.  We should perhaps
     consider moving to something similar, but not before I merge my
     stderr-proc workspace, which contains changes to these
     functions. --ben */
  c.tag = noseeum_cons (noseeum_cons (Qunbound, handlers),
			Vcondition_handlers);
#endif
  c.val = Qnil;
  c.actual_tag = Qnil;
  c.backtrace = Qnil;
  c.backlist = backtrace_list;
#if 0 /* FSFmacs */
  /* #### */
  c.handlerlist = handlerlist;
#endif
  c.lisp_eval_depth = lisp_eval_depth;
  c.pdlcount = specpdl_depth();
#if 0 /* FSFmacs */
  c.poll_suppress_count = async_timer_suppress_count;
#endif
  c.gcpro = gcprolist;
  /* #### FSFmacs does the following statement *after* the setjmp(). */
  c.next = catchlist;

  if (SETJMP (c.jmp))
    {
      /* throw does ungcpro, etc */
      return (*hfun) (c.val, harg);
    }

  record_unwind_protect (condition_case_unwind, c.tag);

  catchlist = &c;
#if 0 /* FSFmacs */
  h.handler = handlers;
  h.var = Qnil;
  h.next = handlerlist;
  h.tag = &c;
  handlerlist = &h;
#else
  Vcondition_handlers = c.tag;
#endif
  GCPRO1 (harg);                /* Somebody has to gc-protect */
  c.val = ((*bfun) (barg));
  UNGCPRO;

  /* Once we change `catchlist' below, the stuff in c will not be GCPRO'd. */
  GCPRO3 (harg, c.val, c.tag);

  catchlist = c.next;
  check_catchlist_sanity ();
  /* Note: The unbind also resets Vcondition_handlers.  Maybe we should
     delete this here. */
  Vcondition_handlers = XCDR (c.tag);
  unbind_to (speccount);

  UNGCPRO;
  /* free the conses *after* the unbind, because the unbind will run
     condition_case_unwind above. */
  free_cons (XCAR (c.tag));
  free_cons (c.tag);
  return c.val;
}

static Lisp_Object
run_condition_case_handlers (Lisp_Object val, Lisp_Object var)
{
  /* This function can GC */
#if 0 /* FSFmacs */
  if (!NILP (h.var))
    specbind (h.var, c.val);
  val = Fprogn (Fcdr (h.chosen_clause));

  /* Note that this just undoes the binding of h.var; whoever
     longjmp()ed to us unwound the stack to c.pdlcount before
     throwing. */
  unbind_to (c.pdlcount);
  return val;
#else
  int speccount;

  CHECK_TRUE_LIST (val);
  if (NILP (var))
    return Fprogn (Fcdr (val)); /* tail call */

  speccount = specpdl_depth();
  specbind (var, Fcar (val));
  val = Fprogn (Fcdr (val));
  return unbind_to_1 (speccount, val);
#endif
}

/* Here for bytecode to call non-consfully.  This is exactly like
   condition-case except that it takes three arguments rather
   than a single list of arguments. */
Lisp_Object
condition_case_3 (Lisp_Object bodyform, Lisp_Object var, Lisp_Object handlers)
{
  /* This function can GC */
  EXTERNAL_LIST_LOOP_2 (handler, handlers)
    {
      if (NILP (handler))
	;
      else if (CONSP (handler))
	{
	  Lisp_Object conditions = XCAR (handler);
	  /* CONDITIONS must a condition name or a list of condition names */
	  if (SYMBOLP (conditions))
	    ;
	  else
	    {
	      EXTERNAL_LIST_LOOP_2 (condition, conditions)
		if (!SYMBOLP (condition))
		  goto invalid_condition_handler;
	    }
	}
      else
	{
	invalid_condition_handler:
	  sferror ("Invalid condition handler", handler);
	}
    }

  CHECK_SYMBOL (var);

  return condition_case_1 (handlers,
			   Feval, bodyform,
			   run_condition_case_handlers,
			   var);
}

DEFUN ("condition-case", Fcondition_case, 2, UNEVALLED, 0, /*
Regain control when an error is signalled.
Usage looks like (condition-case VAR BODYFORM HANDLERS...).
Executes BODYFORM and returns its value if no error happens.
Each element of HANDLERS looks like (CONDITION-NAME BODY...)
where the BODY is made of Lisp expressions.

A typical usage of `condition-case' looks like this:

(condition-case nil
    ;; you need a progn here if you want more than one statement ...
    (progn
      (do-something)
      (do-something-else))
  (error
   (issue-warning-or)
   ;; but strangely, you don't need one here.
   (return-a-value-etc)
   ))

A handler is applicable to an error if CONDITION-NAME is one of the
error's condition names.  If an error happens, the first applicable
handler is run.  As a special case, a CONDITION-NAME of t matches
all errors, even those without the `error' condition name on them
\(e.g. `quit').

The car of a handler may be a list of condition names
instead of a single condition name.

When a handler handles an error,
control returns to the condition-case and the handler BODY... is executed
with VAR bound to (SIGNALED-CONDITIONS . SIGNAL-DATA).
VAR may be nil; then you do not get access to the signal information.

The value of the last BODY form is returned from the condition-case.
See also the function `signal' for more info.

Note that at the time the condition handler is invoked, the Lisp stack
and the current catches, condition-cases, and bindings have all been
popped back to the state they were in just before the call to
`condition-case'.  This means that resignalling the error from
within the handler will not result in an infinite loop.

If you want to establish an error handler that is called with the
Lisp stack, bindings, etc. as they were when `signal' was called,
rather than when the handler was set, use `call-with-condition-handler'.
*/
     (args))
{
  /* This function can GC */
  Lisp_Object var = XCAR (args);
  Lisp_Object bodyform = XCAR (XCDR (args));
  Lisp_Object handlers = XCDR (XCDR (args));
  return condition_case_3 (bodyform, var, handlers);
}

DEFUN ("call-with-condition-handler", Fcall_with_condition_handler, 2, MANY, 0, /*
Call FUNCTION with arguments ARGS, regaining control on error.

This function is similar to `condition-case', but HANDLER is invoked
with the same environment (Lisp stack, bindings, catches, condition-cases)
that was current when `signal' was called, rather than when the handler
was established.

HANDLER should be a function of one argument, which is a cons of the args
\(SIG . DATA) that were passed to `signal'.  It is invoked whenever
`signal' is called (this differs from `condition-case', which allows
you to specify which errors are trapped).  If the handler function
returns, `signal' continues as if the handler were never invoked.
\(It continues to look for handlers established earlier than this one,
and invokes the standard error-handler if none is found.)

arguments: (HANDLER FUNCTION &rest ARGS)
*/
       (int nargs, Lisp_Object *args)) /* Note!  Args side-effected! */
{
  /* This function can GC */
  int speccount = specpdl_depth();
  Lisp_Object tem;

  tem = Ffunction_max_args (args[0]);
  if (! (XINT (Ffunction_min_args (args[0])) <= 1
	 && (NILP (tem) || 1 <= XINT (tem))))
    invalid_argument ("Must be function of one argument", args[0]);

  /* (handler-fun . handler-args)  but currently there are no handler-args */
  tem = noseeum_cons (list1 (args[0]), Vcondition_handlers);
  record_unwind_protect (condition_bind_unwind, tem);
  Vcondition_handlers = tem;

  /* Caller should have GC-protected args */
  return unbind_to_1 (speccount, Ffuncall (nargs - 1, args + 1));
}

/* This is the C version of the above function.  It calls FUN, passing it
   ARG, first setting up HANDLER to catch signals in the environment in
   which they were signalled. (HANDLER is only invoked if there was no
   handler (either from condition-case or call-with-condition-handler) set
   later on that handled the signal; therefore, this is a real error.

   HANDLER is invoked with three arguments: the ERROR-SYMBOL and DATA as
   passed to `signal', and HANDLER_ARG.  Originally I made HANDLER_ARG and
   ARG be void * to facilitate passing structures, but I changed to
   Lisp_Objects because all the other C interfaces to catch/condition-case/etc.
   take Lisp_Objects, and it is easy enough to use make_opaque_ptr() et al.
   to convert between Lisp_Objects and structure pointers. */

Lisp_Object
call_with_condition_handler (Lisp_Object (*handler) (Lisp_Object, Lisp_Object,
						     Lisp_Object),
			     Lisp_Object handler_arg,
			     Lisp_Object (*fun) (Lisp_Object),
			     Lisp_Object arg)
{
  /* This function can GC */
  int speccount = specpdl_depth ();
  Lisp_Object tem;

  /* ((handler-fun . (handler-arg . nil)) ... ) */
  tem = noseeum_cons (noseeum_cons (make_opaque_ptr ((void *) handler),
				    noseeum_cons (handler_arg, Qnil)),
		      Vcondition_handlers);
  record_unwind_protect (condition_bind_unwind, tem);
  Vcondition_handlers = tem;
  
  return unbind_to_1 (speccount, (*fun) (arg));
}

static int
condition_type_p (Lisp_Object type, Lisp_Object conditions)
{
  if (EQ (type, Qt))
    /* (condition-case c # (t c)) catches -all- signals
     *   Use with caution! */
    return 1;

  if (SYMBOLP (type))
    return !NILP (Fmemq (type, conditions));

  for (; CONSP (type); type = XCDR (type))
    if (!NILP (Fmemq (XCAR (type), conditions)))
      return 1;

  return 0;
}

static Lisp_Object
return_from_signal (Lisp_Object value)
{
#if 1
  /* Most callers are not prepared to handle gc if this
     returns.  So, since this feature is not very useful,
     take it out.  */
  /* Have called debugger; return value to signaller  */
  return value;
#else  /* But the reality is that that stinks, because: */
  /* GACK!!! Really want some way for debug-on-quit errors
     to be continuable!! */
  signal_error (Qunimplemented,
		"Returning a value from an error is no longer supported",
		Qunbound);
#endif
}


/************************************************************************/
/*		 the workhorse error-signaling function			*/
/************************************************************************/

/* This exists only for debugging purposes, as a place to put a breakpoint
   that won't get signalled for errors occurring when
   call_with_suspended_errors() was invoked. */

/* Don't make static or it might be compiled away */
void signal_1 (void);

void
signal_1 (void)
{
}

/* #### This function has not been synched with FSF.  It diverges
   significantly. */

/* The simplest external error function: it would be called
   signal_continuable_error() in the terminology below, but it's
   Lisp-callable. */

DEFUN ("signal", Fsignal, 2, 2, 0, /*
Signal a continuable error.  Args are ERROR-SYMBOL, and associated DATA.
An error symbol is a symbol defined using `define-error'.
DATA should be a list.  Its elements are printed as part of the error message.
If the signal is handled, DATA is made available to the handler.
See also the function `signal-error', and the functions to handle errors:
`condition-case' and `call-with-condition-handler'.

Note that this function can return, if the debugger is invoked and the
user invokes the "return from signal" option.
*/
       (error_symbol, data))
{
  /* This function can GC */
  struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
  Lisp_Object conditions = Qnil;
  Lisp_Object handlers = Qnil;
  /* signal_call_debugger() could get called more than once
     (once when a call-with-condition-handler is about to
     be dealt with, and another when a condition-case handler
     is about to be invoked).  So make sure the debugger and/or
     stack trace aren't done more than once. */
  int stack_trace_displayed = 0;
  int debugger_entered = 0;

  /* Fsignal() is one of these functions that's called all the time
     with newly-created Lisp objects.  We allow this; but we must GC-
     protect the objects because all sorts of weird stuff could
     happen. */

  GCPRO4 (conditions, handlers, error_symbol, data);

  if (!(inhibit_flags & CALL_WITH_SUSPENDED_ERRORS))
    signal_1 ();

  if (!initialized)
    {
      /* who knows how much has been initialized?  Safest bet is
         just to bomb out immediately. */
      stderr_out ("Error before initialization is complete!\n");
      ABORT ();
    }

#ifndef NEW_GC
  assert (!gc_in_progress);
#endif /* not NEW_GC */

  /* We abort if in_display and we are not protected, as garbage
     collections and non-local exits will invariably be fatal, but in
     messy, difficult-to-debug ways.  See enter_redisplay_critical_section().
  */

#ifdef ERROR_CHECK_TRAPPING_PROBLEMS
  check_proper_critical_section_nonlocal_exit_protection ();
#endif

  conditions = Fget (error_symbol, Qerror_conditions, Qnil);

  for (handlers = Vcondition_handlers;
       CONSP (handlers);
       handlers = XCDR (handlers))
    {
      Lisp_Object handler_fun = XCAR (XCAR (handlers));
      Lisp_Object handler_data = XCDR (XCAR (handlers));
      Lisp_Object outer_handlers = XCDR (handlers);

      if (!UNBOUNDP (handler_fun))
        {
          /* call-with-condition-handler */
          Lisp_Object tem;
          Lisp_Object all_handlers = Vcondition_handlers;
          struct gcpro ngcpro1;
          NGCPRO1 (all_handlers);
          Vcondition_handlers = outer_handlers;

          tem = signal_call_debugger (conditions, error_symbol, data,
				      outer_handlers, 1,
				      &stack_trace_displayed,
				      &debugger_entered);
          if (!UNBOUNDP (tem))
	    RETURN_NUNGCPRO (return_from_signal (tem));

	  if (OPAQUE_PTRP (handler_fun))
	    {
	      if (NILP (handler_data))
		{
		  Lisp_Object (*hfun) (Lisp_Object, Lisp_Object) =
		    (Lisp_Object (*) (Lisp_Object, Lisp_Object))
		    (get_opaque_ptr (handler_fun));

		  tem = (*hfun) (error_symbol, data);
		}
	      else
		{
		  Lisp_Object (*hfun) (Lisp_Object, Lisp_Object, Lisp_Object) =
		    (Lisp_Object (*) (Lisp_Object, Lisp_Object, Lisp_Object))
		    (get_opaque_ptr (handler_fun));

		  assert (NILP (XCDR (handler_data)));
		  tem = (*hfun) (error_symbol, data, XCAR (handler_data));
		}
	    }
	  else
	    {
	      tem = Fcons (error_symbol, data);
	      if (NILP (handler_data))
		tem = call1 (handler_fun, tem);
	      else
		{
		  /* (This code won't be used (for now?).) */
		  struct gcpro nngcpro1;
		  Lisp_Object args[3];
		  NNGCPRO1 (args[0]);
		  nngcpro1.nvars = 3;
		  args[0] = handler_fun;
		  args[1] = tem;
		  args[2] = handler_data;
		  nngcpro1.var = args;
		  tem = Fapply (3, args);
		  NNUNGCPRO;
		}
	    }
          NUNGCPRO;
#if 0
          if (!EQ (tem, Qsignal))
            return return_from_signal (tem);
#endif
          /* If handler didn't throw, try another handler */
          Vcondition_handlers = all_handlers;
        }

      /* It's a condition-case handler */

      /* t is used by handlers for all conditions, set up by C code.
       *  debugger is not called even if debug_on_error */
      else if (EQ (handler_data, Qt))
	{
          UNGCPRO;
          throw_or_bomb_out (handlers, Fcons (error_symbol, data),
                             0, Qnil, Qnil);
	}
      /* `error' is used similarly to the way `t' is used, but in
         addition it invokes the debugger if debug_on_error.
	 This is normally used for the outer command-loop error
	 handler. */
      else if (EQ (handler_data, Qerror))
        {
          Lisp_Object tem = signal_call_debugger (conditions, error_symbol,
						  data,
                                                  outer_handlers, 0,
						  &stack_trace_displayed,
						  &debugger_entered);

          UNGCPRO;
          if (!UNBOUNDP (tem))
            return return_from_signal (tem);

          tem = Fcons (error_symbol, data);
          throw_or_bomb_out (handlers, tem, 0, Qnil, Qnil);
        }
      else
	{
          /* handler established by real (Lisp) condition-case */
          Lisp_Object h;

	  for (h = handler_data; CONSP (h); h = Fcdr (h))
	    {
	      Lisp_Object clause = Fcar (h);
	      Lisp_Object tem = Fcar (clause);

	      if (condition_type_p (tem, conditions))
		{
		  tem = signal_call_debugger (conditions, error_symbol, data,
                                              outer_handlers, 1,
					      &stack_trace_displayed,
					      &debugger_entered);
                  UNGCPRO;
		  if (!UNBOUNDP (tem))
                    return return_from_signal (tem);

                  /* Doesn't return */
                  tem = Fcons (Fcons (error_symbol, data), Fcdr (clause));
                  throw_or_bomb_out (handlers, tem, 0, Qnil, Qnil);
                }
	    }
	}
    }

  /* If no handler is present now, try to run the debugger,
     and if that fails, throw to top level.

     #### The only time that no handler is present is during
     temacs or perhaps very early in XEmacs.  In both cases,
     there is no `top-level' catch. (That's why the
     "bomb-out" hack was added.)

     [[#### Fix this horrifitude!]]

     I don't think this is horrifitude, but just defensive coding. --ben */

  signal_call_debugger (conditions, error_symbol, data, Qnil, 0,
			&stack_trace_displayed,
			&debugger_entered);
  UNGCPRO;
  throw_or_bomb_out (Qtop_level, Qt, 1, error_symbol,
		     data); /* Doesn't return */
  RETURN_NOT_REACHED (Qnil);
}

/****************** Error functions class 1 ******************/

/* Class 1: General functions that signal an error.
   These functions take an error type and a list of associated error
   data. */

/* No signal_continuable_error_1(); it's called Fsignal(). */

/* Signal a non-continuable error. */

DOESNT_RETURN
signal_error_1 (Lisp_Object sig, Lisp_Object data)
{
  for (;;)
    Fsignal (sig, data);
}

#ifdef ERROR_CHECK_CATCH

void
check_catchlist_sanity (void)
{
#if 0
  /* vou me tomar no cu!  i just masked andy's missing-unbind
     bug! */
  struct catchtag *c;
  int found_error_tag = 0;

  for (c = catchlist; c; c = c->next)
    {
      if (EQ (c->tag, Qunbound_suspended_errors_tag))
	{
	  found_error_tag = 1;
	  break;
	}
    }

  assert (found_error_tag || NILP (Vcurrent_error_state));
#endif /* vou me tomar no cul */
}

void
check_specbind_stack_sanity (void)
{
}

#endif /* ERROR_CHECK_CATCH */

/* Signal a non-continuable error or display a warning or do nothing,
   according to ERRB.  CLASS is the class of warning and should
   refer to what sort of operation is being done (e.g. Qtoolbar,
   Qresource, etc.). */

void
maybe_signal_error_1 (Lisp_Object sig, Lisp_Object data, Lisp_Object class_,
		      Error_Behavior errb)
{
  if (ERRB_EQ (errb, ERROR_ME_NOT))
    return;
  else if (ERRB_EQ (errb, ERROR_ME_DEBUG_WARN))
    warn_when_safe_lispobj (class_, Qdebug, Fcons (sig, data));
  else if (ERRB_EQ (errb, ERROR_ME_WARN))
    warn_when_safe_lispobj (class_, Qwarning, Fcons (sig, data));
  else
    signal_error_1 (sig, data);
}

/* Signal a continuable error or display a warning or do nothing,
   according to ERRB. */

Lisp_Object
maybe_signal_continuable_error_1 (Lisp_Object sig, Lisp_Object data,
				  Lisp_Object class_, Error_Behavior errb)
{
  if (ERRB_EQ (errb, ERROR_ME_NOT))
    return Qnil;
  else if (ERRB_EQ (errb, ERROR_ME_DEBUG_WARN))
    {
      warn_when_safe_lispobj (class_, Qdebug, Fcons (sig, data));
      return Qnil;
    }
  else if (ERRB_EQ (errb, ERROR_ME_WARN))
    {
      warn_when_safe_lispobj (class_, Qwarning, Fcons (sig, data));
      return Qnil;
    }
  else
    return Fsignal (sig, data);
}


/****************** Error functions class 2 ******************/

/* Class 2: Signal an error with a string and an associated object.
   Normally these functions are used to attach one associated object,
   but to attach no objects, specify Qunbound for FROB, and for more
   than one object, make a list of the objects with Qunbound as the
   first element. (If you have specifically two objects to attach,
   consider using the function in class 3 below.) These functions
   signal an error of a specified type, whose data is one or more
   objects (usually two), a string the related Lisp object(s)
   specified as FROB. */

/* Out of REASON and FROB, return a list of elements suitable for passing
   to signal_error_1(). */

Lisp_Object
build_error_data (const Ascbyte *reason, Lisp_Object frob)
{
  if (EQ (frob, Qunbound))
    frob = Qnil;
  else if (CONSP (frob) && EQ (XCAR (frob), Qunbound))
    frob = XCDR (frob);
  else
    frob = list1 (frob);
  if (!reason)
    return frob;
  else
    return Fcons (build_msg_string (reason), frob);
}

DOESNT_RETURN
signal_error (Lisp_Object type, const Ascbyte *reason, Lisp_Object frob)
{
  signal_error_1 (type, build_error_data (reason, frob));
}

/* NOTE NOTE NOTE: If you feel you need signal_ierror() or something
   similar when reason is a non-ASCII message, you're probably doing
   something wrong.  When you have an error message from an external
   source, you should put the error message as the first item in FROB and
   put a string in REASON indicating what you were doing when the error
   message occurred.  Use signal_error_2() for such a case. */

void
maybe_signal_error (Lisp_Object type, const Ascbyte *reason,
		    Lisp_Object frob, Lisp_Object class_,
		    Error_Behavior errb)
{
  /* Optimization: */
  if (ERRB_EQ (errb, ERROR_ME_NOT))
    return;
  maybe_signal_error_1 (type, build_error_data (reason, frob), class_, errb);
}

Lisp_Object
signal_continuable_error (Lisp_Object type, const Ascbyte *reason,
			  Lisp_Object frob)
{
  return Fsignal (type, build_error_data (reason, frob));
}

Lisp_Object
maybe_signal_continuable_error (Lisp_Object type, const Ascbyte *reason,
				Lisp_Object frob, Lisp_Object class_,
				Error_Behavior errb)
{
  /* Optimization: */
  if (ERRB_EQ (errb, ERROR_ME_NOT))
    return Qnil;
  return maybe_signal_continuable_error_1 (type,
					   build_error_data (reason, frob),
					   class_, errb);
}


/****************** Error functions class 3 ******************/

/* Class 3: Signal an error with a string and two associated objects.
   These functions signal an error of a specified type, whose data
   is three objects, a string and two related Lisp objects.
   (The equivalent could be accomplished using the class 2 functions,
   but these are more convenient in this particular case.) */

DOESNT_RETURN
signal_error_2 (Lisp_Object type, const Ascbyte *reason,
		Lisp_Object frob0, Lisp_Object frob1)
{
  signal_error_1 (type, list3 (build_msg_string (reason), frob0,
			       frob1));
}

void
maybe_signal_error_2 (Lisp_Object type, const Ascbyte *reason,
		      Lisp_Object frob0, Lisp_Object frob1,
		      Lisp_Object class_, Error_Behavior errb)
{
  /* Optimization: */
  if (ERRB_EQ (errb, ERROR_ME_NOT))
    return;
  maybe_signal_error_1 (type, list3 (build_msg_string (reason), frob0,
				     frob1), class_, errb);
}

Lisp_Object
signal_continuable_error_2 (Lisp_Object type, const Ascbyte *reason,
			    Lisp_Object frob0, Lisp_Object frob1)
{
  return Fsignal (type, list3 (build_msg_string (reason), frob0,
			       frob1));
}

Lisp_Object
maybe_signal_continuable_error_2 (Lisp_Object type, const Ascbyte *reason,
				  Lisp_Object frob0, Lisp_Object frob1,
				  Lisp_Object class_, Error_Behavior errb)
{
  /* Optimization: */
  if (ERRB_EQ (errb, ERROR_ME_NOT))
    return Qnil;
  return maybe_signal_continuable_error_1
    (type, list3 (build_msg_string (reason), frob0, frob1),
     class_, errb);
}


/****************** Error functions class 4 ******************/

/* Class 4: Printf-like functions that signal an error.
   These functions signal an error of a specified type, whose data
   is a single string, created using the arguments. */

DOESNT_RETURN
signal_ferror (Lisp_Object type, const Ascbyte *fmt, ...)
{
  Lisp_Object obj;
  va_list args;

  va_start (args, fmt);
  obj = emacs_vsprintf_string (GETTEXT (fmt), args);
  va_end (args);

  /* Fsignal GC-protects its args */
  signal_error (type, 0, obj);
}

void
maybe_signal_ferror (Lisp_Object type, Lisp_Object class_, Error_Behavior errb,
		     const Ascbyte *fmt, ...)
{
  Lisp_Object obj;
  va_list args;

  /* Optimization: */
  if (ERRB_EQ (errb, ERROR_ME_NOT))
    return;

  va_start (args, fmt);
  obj = emacs_vsprintf_string (GETTEXT (fmt), args);
  va_end (args);

  /* Fsignal GC-protects its args */
  maybe_signal_error (type, 0, obj, class_, errb);
}

Lisp_Object
signal_continuable_ferror (Lisp_Object type, const Ascbyte *fmt, ...)
{
  Lisp_Object obj;
  va_list args;

  va_start (args, fmt);
  obj = emacs_vsprintf_string (GETTEXT (fmt), args);
  va_end (args);

  /* Fsignal GC-protects its args */
  return Fsignal (type, list1 (obj));
}

Lisp_Object
maybe_signal_continuable_ferror (Lisp_Object type, Lisp_Object class_,
				 Error_Behavior errb, const Ascbyte *fmt, ...)
{
  Lisp_Object obj;
  va_list args;

  /* Optimization: */
  if (ERRB_EQ (errb, ERROR_ME_NOT))
    return Qnil;

  va_start (args, fmt);
  obj = emacs_vsprintf_string (GETTEXT (fmt), args);
  va_end (args);

  /* Fsignal GC-protects its args */
  return maybe_signal_continuable_error (type, 0, obj, class_, errb);
}


/****************** Error functions class 5 ******************/

/* Class 5: Printf-like functions that signal an error.
   These functions signal an error of a specified type, whose data
   is a one or more objects, a string (created using the arguments)
   and additional Lisp objects specified in FROB. (The syntax of FROB
   is the same as for class 2.)

   There is no need for a class 6 because you can always attach 2
   objects using class 5 (for FROB, specify a list with three
   elements, the first of which is Qunbound), and these functions are
   not commonly used.   
*/

DOESNT_RETURN
signal_ferror_with_frob (Lisp_Object type, Lisp_Object frob, const Ascbyte *fmt,
			 ...)
{
  Lisp_Object obj;
  va_list args;

  va_start (args, fmt);
  obj = emacs_vsprintf_string (GETTEXT (fmt), args);
  va_end (args);

  /* Fsignal GC-protects its args */
  signal_error_1 (type, Fcons (obj, build_error_data (0, frob)));
}

void
maybe_signal_ferror_with_frob (Lisp_Object type, Lisp_Object frob,
			       Lisp_Object class_, Error_Behavior errb,
			       const Ascbyte *fmt, ...)
{
  Lisp_Object obj;
  va_list args;

  /* Optimization: */
  if (ERRB_EQ (errb, ERROR_ME_NOT))
    return;

  va_start (args, fmt);
  obj = emacs_vsprintf_string (GETTEXT (fmt), args);
  va_end (args);

  /* Fsignal GC-protects its args */
  maybe_signal_error_1 (type, Fcons (obj, build_error_data (0, frob)), class_,
			errb);
}

Lisp_Object
signal_continuable_ferror_with_frob (Lisp_Object type, Lisp_Object frob,
				     const Ascbyte *fmt, ...)
{
  Lisp_Object obj;
  va_list args;

  va_start (args, fmt);
  obj = emacs_vsprintf_string (GETTEXT (fmt), args);
  va_end (args);

  /* Fsignal GC-protects its args */
  return Fsignal (type, Fcons (obj, build_error_data (0, frob)));
}

Lisp_Object
maybe_signal_continuable_ferror_with_frob (Lisp_Object type, Lisp_Object frob,
					   Lisp_Object class_,
					   Error_Behavior errb,
					   const Ascbyte *fmt, ...)
{
  Lisp_Object obj;
  va_list args;

  /* Optimization: */
  if (ERRB_EQ (errb, ERROR_ME_NOT))
    return Qnil;

  va_start (args, fmt);
  obj = emacs_vsprintf_string (GETTEXT (fmt), args);
  va_end (args);

  /* Fsignal GC-protects its args */
  return maybe_signal_continuable_error_1 (type,
					   Fcons (obj,
						  build_error_data (0, frob)),
					   class_, errb);
}


/* This is what the QUIT macro calls to signal a quit */
void
signal_quit (void)
{
  /* This function cannot GC.  GC is prohibited because most callers do
     not expect GC occurring in QUIT.  Remove this if/when that gets fixed.
     --ben */

  int count;

  if (EQ (Vquit_flag, Qcritical))
    debug_on_quit |= 2;		/* set critical bit. */
  Vquit_flag = Qnil;
  count = begin_gc_forbidden ();
  /* note that this is continuable. */
  Fsignal (Qquit, Qnil);
  unbind_to (count);
}


/************************ convenience error functions ***********************/

Lisp_Object
signal_void_function_error (Lisp_Object function)
{
  return Fsignal (Qvoid_function, list1 (function));
}

Lisp_Object
signal_invalid_function_error (Lisp_Object function)
{
  return Fsignal (Qinvalid_function, list1 (function));
}

Lisp_Object
signal_wrong_number_of_arguments_error (Lisp_Object function, int nargs)
{
  return Fsignal (Qwrong_number_of_arguments,
		  list2 (function, make_int (nargs)));
}

/* Used in list traversal macros for efficiency. */
DOESNT_RETURN
signal_malformed_list_error (Lisp_Object list)
{
  signal_error (Qmalformed_list, 0, list);
}

DOESNT_RETURN
signal_malformed_property_list_error (Lisp_Object list)
{
  signal_error (Qmalformed_property_list, 0, list);
}

DOESNT_RETURN
signal_circular_list_error (Lisp_Object list)
{
  signal_error (Qcircular_list, 0, list);
}

DOESNT_RETURN
signal_circular_property_list_error (Lisp_Object list)
{
  signal_error (Qcircular_property_list, 0, list);
}

/* Called from within emacs_doprnt_1, so REASON is not formatted. */
DOESNT_RETURN
syntax_error (const Ascbyte *reason, Lisp_Object frob)
{
  signal_error (Qsyntax_error, reason, frob);
}

DOESNT_RETURN
syntax_error_2 (const Ascbyte *reason, Lisp_Object frob1, Lisp_Object frob2)
{
  signal_error_2 (Qsyntax_error, reason, frob1, frob2);
}

void
maybe_syntax_error (const Ascbyte *reason, Lisp_Object frob,
		    Lisp_Object class_, Error_Behavior errb)
{
  maybe_signal_error (Qsyntax_error, reason, frob, class_, errb);
}

DOESNT_RETURN
sferror (const Ascbyte *reason, Lisp_Object frob)
{
  signal_error (Qstructure_formation_error, reason, frob);
}

DOESNT_RETURN
sferror_2 (const Ascbyte *reason, Lisp_Object frob1, Lisp_Object frob2)
{
  signal_error_2 (Qstructure_formation_error, reason, frob1, frob2);
}

void
maybe_sferror (const Ascbyte *reason, Lisp_Object frob,
	       Lisp_Object class_, Error_Behavior errb)
{
  maybe_signal_error (Qstructure_formation_error, reason, frob, class_, errb);
}

DOESNT_RETURN
invalid_argument (const Ascbyte *reason, Lisp_Object frob)
{
  signal_error (Qinvalid_argument, reason, frob);
}

DOESNT_RETURN
invalid_argument_2 (const Ascbyte *reason, Lisp_Object frob1,
		    Lisp_Object frob2)
{
  signal_error_2 (Qinvalid_argument, reason, frob1, frob2);
}

void
maybe_invalid_argument (const Ascbyte *reason, Lisp_Object frob,
			Lisp_Object class_, Error_Behavior errb)
{
  maybe_signal_error (Qinvalid_argument, reason, frob, class_, errb);
}

DOESNT_RETURN
invalid_keyword_argument (Lisp_Object function, Lisp_Object keyword)
{
  signal_error_1 (Qinvalid_keyword_argument, list2 (function, keyword));
}

DOESNT_RETURN
invalid_constant (const Ascbyte *reason, Lisp_Object frob)
{
  signal_error (Qinvalid_constant, reason, frob);
}

DOESNT_RETURN
invalid_constant_2 (const Ascbyte *reason, Lisp_Object frob1,
		    Lisp_Object frob2)
{
  signal_error_2 (Qinvalid_constant, reason, frob1, frob2);
}

void
maybe_invalid_constant (const Ascbyte *reason, Lisp_Object frob,
			Lisp_Object class_, Error_Behavior errb)
{
  maybe_signal_error (Qinvalid_constant, reason, frob, class_, errb);
}

DOESNT_RETURN
invalid_operation (const Ascbyte *reason, Lisp_Object frob)
{
  signal_error (Qinvalid_operation, reason, frob);
}

DOESNT_RETURN
invalid_operation_2 (const Ascbyte *reason, Lisp_Object frob1,
		     Lisp_Object frob2)
{
  signal_error_2 (Qinvalid_operation, reason, frob1, frob2);
}

void
maybe_invalid_operation (const Ascbyte *reason, Lisp_Object frob,
			 Lisp_Object class_, Error_Behavior errb)
{
  maybe_signal_error (Qinvalid_operation, reason, frob, class_, errb);
}

DOESNT_RETURN
invalid_change (const Ascbyte *reason, Lisp_Object frob)
{
  signal_error (Qinvalid_change, reason, frob);
}

DOESNT_RETURN
invalid_change_2 (const Ascbyte *reason, Lisp_Object frob1, Lisp_Object frob2)
{
  signal_error_2 (Qinvalid_change, reason, frob1, frob2);
}

void
maybe_invalid_change (const Ascbyte *reason, Lisp_Object frob,
		      Lisp_Object class_, Error_Behavior errb)
{
  maybe_signal_error (Qinvalid_change, reason, frob, class_, errb);
}

DOESNT_RETURN
invalid_state (const Ascbyte *reason, Lisp_Object frob)
{
  signal_error (Qinvalid_state, reason, frob);
}

DOESNT_RETURN
invalid_state_2 (const Ascbyte *reason, Lisp_Object frob1, Lisp_Object frob2)
{
  signal_error_2 (Qinvalid_state, reason, frob1, frob2);
}

void
maybe_invalid_state (const Ascbyte *reason, Lisp_Object frob,
		     Lisp_Object class_, Error_Behavior errb)
{
  maybe_signal_error (Qinvalid_state, reason, frob, class_, errb);
}

DOESNT_RETURN
wtaerror (const Ascbyte *reason, Lisp_Object frob)
{
  signal_error (Qwrong_type_argument, reason, frob);
}

DOESNT_RETURN
stack_overflow (const Ascbyte *reason, Lisp_Object frob)
{
  signal_error (Qstack_overflow, reason, frob);
}

DOESNT_RETURN
out_of_memory (const Ascbyte *reason, Lisp_Object frob)
{
  signal_error (Qout_of_memory, reason, frob);
}


/************************************************************************/
/*			      User commands				*/
/************************************************************************/

DEFUN ("commandp", Fcommandp, 1, 1, 0, /*
Return t if FUNCTION makes provisions for interactive calling.
This means it contains a description for how to read arguments to give it.
The value is nil for an invalid function or a symbol with no function
definition.

Interactively callable functions include

-- strings and vectors (treated as keyboard macros)
-- lambda-expressions that contain a top-level call to `interactive'
-- autoload definitions made by `autoload' with non-nil fourth argument
   (i.e. the interactive flag)
-- compiled-function objects with a non-nil `compiled-function-interactive'
   value
-- subrs (built-in functions) that are interactively callable

Also, a symbol satisfies `commandp' if its function definition does so.
*/
       (function))
{
  Lisp_Object fun = indirect_function (function, 0);

  if (COMPILED_FUNCTIONP (fun))
    return XCOMPILED_FUNCTION (fun)->flags.interactivep ? Qt : Qnil;

  /* Lists may represent commands.  */
  if (CONSP (fun))
    {
      Lisp_Object funcar = XCAR (fun);
      if (EQ (funcar, Qlambda))
	return Fassq (Qinteractive, Fcdr (Fcdr (fun)));
      if (EQ (funcar, Qautoload))
	return Fcar (Fcdr (Fcdr (Fcdr (fun))));
      else
	return Qnil;
    }

  /* Emacs primitives are interactive if their DEFUN specifies an
     interactive spec.  */
  if (SUBRP (fun))
    return XSUBR (fun)->prompt ? Qt : Qnil;

  /* Strings and vectors are keyboard macros.  */
  if (VECTORP (fun) || STRINGP (fun))
    return Qt;

  /* Everything else (including Qunbound) is not a command.  */
  return Qnil;
}

DEFUN ("command-execute", Fcommand_execute, 1, 3, 0, /*
Execute CMD as an editor command.
CMD must be an object that satisfies the `commandp' predicate.
Optional second arg RECORD-FLAG is as in `call-interactively'.
The argument KEYS specifies the value to use instead of (this-command-keys)
when reading the arguments.
*/
       (cmd, record_flag, keys))
{
  /* This function can GC */
  Lisp_Object prefixarg;
  Lisp_Object final = cmd;
  PROFILE_DECLARE();
  struct console *con = XCONSOLE (Vselected_console);

  prefixarg = con->prefix_arg;
  con->prefix_arg = Qnil;
  Vcurrent_prefix_arg = prefixarg;
  debug_on_next_call = 0; /* #### from FSFmacs; correct? */

  if (SYMBOLP (cmd) && !NILP (Fget (cmd, Qdisabled, Qnil)))
    return run_hook (Qdisabled_command_hook);

  for (;;)
    {
      final = indirect_function (cmd, 1);
      if (CONSP (final) && EQ (Fcar (final), Qautoload))
	{
	  /* do_autoload GCPROs both arguments */
	  do_autoload (final, cmd);
	}
      else
	break;
    }

  if (CONSP (final) || SUBRP (final) || COMPILED_FUNCTIONP (final))
    {
      backtrace.function = &Qcall_interactively;
      backtrace.args = &cmd;
      backtrace.nargs = 1;
      backtrace.evalargs = 0;
      backtrace.pdlcount = specpdl_depth ();
      backtrace.debug_on_exit = 0;
      backtrace.function_being_called = 0;
      PUSH_BACKTRACE (backtrace);

      PROFILE_ENTER_FUNCTION ();
      final = Fcall_interactively (cmd, record_flag, keys);
      PROFILE_EXIT_FUNCTION ();

      POP_BACKTRACE (backtrace);
      return final;
    }
  else if (STRINGP (final) || VECTORP (final))
    {
      return Fexecute_kbd_macro (final, prefixarg);
    }
  else
    {
      Fsignal (Qwrong_type_argument,
	       Fcons (Qcommandp,
		      (EQ (cmd, final)
                       ? list1 (cmd)
                       : list2 (cmd, final))));
      return Qnil;
    }
}

DEFUN ("interactive-p", Finteractive_p, 0, 0, 0, /*
Return t if function in which this appears was called interactively.
This means that the function was called with call-interactively (which
includes being called as the binding of a key)
and input is currently coming from the keyboard (not in keyboard macro).
*/
       ())
{
  REGISTER struct backtrace *btp;
  REGISTER Lisp_Object fun;

  if (!INTERACTIVE)
    return Qnil;

  /*  Unless the object was compiled, skip the frame of interactive-p itself
      (if interpreted) or the frame of byte-code (if called from a compiled
      function).  Note that *btp->function may be a symbol pointing at a
      compiled function. */
  btp = backtrace_list;

#if 0 /* FSFmacs */

  /* #### FSFmacs does the following instead.  I can't figure
     out which one is more correct. */
  /* If this isn't a byte-compiled function, there may be a frame at
     the top for Finteractive_p itself.  If so, skip it.  */
  fun = Findirect_function (*btp->function);
  if (SUBRP (fun) && XSUBR (fun) == &Sinteractive_p)
    btp = btp->next;

  /* If we're running an Emacs 18-style byte-compiled function, there
     may be a frame for Fbyte_code.  Now, given the strictest
     definition, this function isn't really being called
     interactively, but because that's the way Emacs 18 always builds
     byte-compiled functions, we'll accept it for now.  */
  if (EQ (*btp->function, Qbyte_code))
    btp = btp->next;

  /* If this isn't a byte-compiled function, then we may now be
     looking at several frames for special operators.  Skip past them.  */
  while (btp &&
	 btp->nargs == UNEVALLED)
    btp = btp->next;

#else

  if (! (COMPILED_FUNCTIONP (Findirect_function (*btp->function))))
    btp = btp->next;
  for (;
       btp && (btp->nargs == UNEVALLED
	       || EQ (*btp->function, Qbyte_code));
       btp = btp->next)
    {}
  /* btp now points at the frame of the innermost function
     that DOES eval its args.
     If it is a built-in function (such as load or eval-region)
     return nil.  */
  /* Beats me why this is necessary, but it is */
  if (btp && EQ (*btp->function, Qcall_interactively))
    return Qt;

#endif

  fun = Findirect_function (*btp->function);
  if (SUBRP (fun))
    return Qnil;
  /* btp points to the frame of a Lisp function that called interactive-p.
     Return t if that function was called interactively.  */
  if (btp && btp->next && EQ (*btp->next->function, Qcall_interactively))
    return Qt;
  return Qnil;
}


/************************************************************************/
/*			      Autoloading				*/
/************************************************************************/

DEFUN ("autoload", Fautoload, 2, 5, 0, /*
Define FUNCTION to autoload from FILENAME.
FUNCTION is a symbol; FILENAME is a file name string to pass to `load'.
The remaining optional arguments provide additional info about the
real definition.
DOCSTRING is documentation for FUNCTION.
INTERACTIVE, if non-nil, says FUNCTION can be called interactively.
TYPE indicates the type of the object:
   nil or omitted says FUNCTION is a function,
   `keymap' says FUNCTION is really a keymap, and
   `macro' or t says FUNCTION is really a macro.
If FUNCTION already has a non-void function definition that is not an
autoload object, this function does nothing and returns nil.
*/
       (function, filename, docstring, interactive, type))
{
  /* This function can GC */
  CHECK_SYMBOL (function);
  CHECK_STRING (filename);

  /* If function is defined and not as an autoload, don't override */
  {
    Lisp_Object f = XSYMBOL (function)->function;
    if (!UNBOUNDP (f) && !(CONSP (f) && EQ (XCAR (f), Qautoload)))
      return Qnil;
  }

  if (purify_flag)
    {
      /* Attempt to avoid consing identical (string=) pure strings. */
      filename = Fsymbol_name (Fintern (filename, Qnil));
    }

  return Ffset (function, Fcons (Qautoload, list4 (filename,
						   docstring,
						   interactive,
						   type)));
}

Lisp_Object
un_autoload (Lisp_Object oldqueue)
{
  /* This function can GC */
  REGISTER Lisp_Object queue, first, second;

  /* Queue to unwind is current value of Vautoload_queue.
     oldqueue is the shadowed value to leave in Vautoload_queue.  */
  queue = Vautoload_queue;
  Vautoload_queue = oldqueue;
  while (CONSP (queue))
    {
      first = XCAR (queue);
      second = Fcdr (first);
      first = Fcar (first);
      if (NILP (second))
	Vfeatures = first;
      else
	Ffset (first, second);
      queue = Fcdr (queue);
    }
  return Qnil;
}

/* do_autoload GCPROs both arguments */
void
do_autoload (Lisp_Object fundef,
             Lisp_Object funname)
{
  /* This function can GC */
  int speccount = specpdl_depth();
  Lisp_Object fun = funname;
  struct gcpro gcpro1, gcpro2, gcpro3;

  CHECK_SYMBOL (funname);
  GCPRO3 (fundef, funname, fun);

  /* Value saved here is to be restored into Vautoload_queue */
  record_unwind_protect (un_autoload, Vautoload_queue);
  Vautoload_queue = Qt;
  call4 (Qload, Fcar (Fcdr (fundef)), Qnil, noninteractive ? Qt : Qnil, Qnil);

  {
    Lisp_Object queue;

    /* Save the old autoloads, in case we ever do an unload. */
    for (queue = Vautoload_queue; CONSP (queue); queue = XCDR (queue))
      {
	Lisp_Object first  = XCAR (queue);
	Lisp_Object second = Fcdr (first);

	first = Fcar (first);

	/* Note: This test is subtle.  The cdr of an autoload-queue entry
	   may be an atom if the autoload entry was generated by a defalias
	   or fset. */
	if (CONSP (second))
	  Fput (first, Qautoload, (XCDR (second)));
      }
  }

  /* Once loading finishes, don't undo it.  */
  Vautoload_queue = Qt;
  unbind_to (speccount);

  fun = indirect_function (fun, 0);

#if 0 /* FSFmacs */
  if (!NILP (Fequal (fun, fundef)))
#else
  if (UNBOUNDP (fun)
      || (CONSP (fun)
          && EQ (XCAR (fun), Qautoload)))
#endif
    invalid_state ("Autoloading failed to define function", funname);
  UNGCPRO;
}


/************************************************************************/
/*			   eval, funcall, apply				*/
/************************************************************************/

/* NOTE: If you are hearing the endless complaint that function calls in
   elisp are extremely slow, it just isn't true any more!  The stuff below
   -- in particular, the calling of subrs and compiled functions, the most
   common cases -- has been highly optimized.  There isn't a whole lot left
   to do to squeeze more speed out except by switching to lexical
   variables, which would eliminate the specbind loop. (But the real gain
   from lexical variables would come from better optimization -- with
   dynamic binding, you have the constant problem that any function call
   that you haven't explicitly proven to be side-effect-free might
   potentially side effect your local variables, which makes optimization
   extremely difficult when there are function calls anywhere in a chunk of
   code to be optimized.  Even worse, you don't know that *your* local
   variables aren't side-effecting an outer function's local variables, so
   it's impossible to optimize away almost *any* variable assignment.) */

static Lisp_Object funcall_lambda (Lisp_Object fun,
				   int nargs, Lisp_Object args[]);
static int in_warnings;


void handle_compiled_function_with_and_rest (Lisp_Compiled_Function *f,
					     int nargs,
					     Lisp_Object args[]);

/* The theory behind making this a separate function is to shrink
   funcall_compiled_function() so as to increase the likelihood of a cache
   hit in the L1 cache -- &rest processing is not going to be fast anyway.
   The idea is the same as with execute_rare_opcode() in bytecode.c.  We
   make this non-static to ensure the compiler doesn't inline it. */

void
handle_compiled_function_with_and_rest (Lisp_Compiled_Function *f, int nargs,
					Lisp_Object args[])
{
  REGISTER int i = 0;
  int max_non_rest_args = f->args_in_array - 1;
  int bindargs = min (nargs, max_non_rest_args);

  for (i = 0; i < bindargs; i++)
#ifdef NEW_GC
    SPECBIND_FAST_UNSAFE (XCOMPILED_FUNCTION_ARGS_DATA (f->arguments)[i],
			  args[i]);
#else /* not NEW_GC */
    SPECBIND_FAST_UNSAFE (f->args[i], args[i]);
#endif /* not NEW_GC */
  for (i = bindargs; i < max_non_rest_args; i++)
#ifdef NEW_GC
    SPECBIND_FAST_UNSAFE (XCOMPILED_FUNCTION_ARGS_DATA (f->arguments)[i],
			  Qnil);
#else /* not NEW_GC */
    SPECBIND_FAST_UNSAFE (f->args[i], Qnil);
#endif /* not NEW_GC */
#ifdef NEW_GC
  SPECBIND_FAST_UNSAFE
    (XCOMPILED_FUNCTION_ARGS_DATA (f->arguments)[max_non_rest_args],
     nargs > max_non_rest_args ?
     Flist (nargs - max_non_rest_args, &args[max_non_rest_args]) :
     Qnil);
#else /* not NEW_GC */
  SPECBIND_FAST_UNSAFE
    (f->args[max_non_rest_args],
     nargs > max_non_rest_args ?
     Flist (nargs - max_non_rest_args, &args[max_non_rest_args]) :
     Qnil);
#endif /* not NEW_GC */
}

/* Apply compiled-function object FUN to the NARGS evaluated arguments
   in ARGS, and return the result of evaluation. */
inline static Lisp_Object
funcall_compiled_function (Lisp_Object fun, int nargs, Lisp_Object args[])
{
  /* This function can GC */
  int speccount = specpdl_depth();
  REGISTER int i = 0;
  Lisp_Compiled_Function *f = XCOMPILED_FUNCTION (fun);

  if (!OPAQUEP (f->instructions))
    /* Lazily munge the instructions into a more efficient form */
    optimize_compiled_function (fun);

  /* optimize_compiled_function() guaranteed that f->specpdl_depth is
     the required space on the specbinding stack for binding the args
     and local variables of fun.   So just reserve it once. */
  SPECPDL_RESERVE (f->specpdl_depth);

  if (nargs == f->max_args) /* Optimize for the common case -- no unspecified
			       optional arguments. */
    {
#if 1
      for (i = 0; i < nargs; i++)
#ifdef NEW_GC
	SPECBIND_FAST_UNSAFE (XCOMPILED_FUNCTION_ARGS_DATA (f->arguments)[i],
			      args[i]);
#else /* not NEW_GC */
	SPECBIND_FAST_UNSAFE (f->args[i], args[i]);
#endif /* not NEW_GC */
#else
      /* Here's an alternate way to write the loop that tries to further
         optimize funcalls for functions with few arguments by partially
         unrolling the loop.  It's not clear whether this is a win since it
         increases the size of the function and the possibility of L1 cache
         misses. (Microsoft VC++ 6 with /O2 /G5 generates 0x90 == 144 bytes
         per SPECBIND_FAST_UNSAFE().) Tests under VC++ 6, running the byte
         compiler repeatedly and looking at the total time, show very
         little difference between the simple loop above, the unrolled code
         below, and a "partly unrolled" solution with only cases 0-2 below
         instead of 0-4.  Therefore, I'm keeping it at the simple loop
         because it's smaller. */
      switch (nargs)
	{
	default:
	  for (i = nargs - 1; i >= 4; i--)
	    SPECBIND_FAST_UNSAFE (f->args[i], args[i]);
	case 4: SPECBIND_FAST_UNSAFE (f->args[3], args[3]);
	case 3: SPECBIND_FAST_UNSAFE (f->args[2], args[2]);
	case 2: SPECBIND_FAST_UNSAFE (f->args[1], args[1]);
	case 1: SPECBIND_FAST_UNSAFE (f->args[0], args[0]);
	case 0: break;
	}
#endif
    }
  else if (nargs < f->min_args)
    goto wrong_number_of_arguments;
  else if (nargs < f->max_args)
    {
      for (i = 0; i < nargs; i++)
#ifdef NEW_GC
	SPECBIND_FAST_UNSAFE (XCOMPILED_FUNCTION_ARGS_DATA (f->arguments)[i],
			      args[i]);
#else /* not NEW_GC */
	SPECBIND_FAST_UNSAFE (f->args[i], args[i]);
#endif /* not NEW_GC */
      for (i = nargs; i < f->max_args; i++)
#ifdef NEW_GC
	SPECBIND_FAST_UNSAFE (XCOMPILED_FUNCTION_ARGS_DATA (f->arguments)[i],
			      Qnil);
#else /* not NEW_GC */
	SPECBIND_FAST_UNSAFE (f->args[i], Qnil);
#endif /* not NEW_GC */
    }
  else if (f->max_args == MANY)
    handle_compiled_function_with_and_rest (f, nargs, args);
  else
    {
    wrong_number_of_arguments:
      /* The actual printed compiled_function object is incomprehensible.
	 Check the backtrace to see if we can get a more meaningful symbol. */
      if (EQ (fun, indirect_function (*backtrace_list->function, 0)))
	fun = *backtrace_list->function;
      return Fsignal (Qwrong_number_of_arguments,
		      list2 (fun, make_int (nargs)));
    }

  {
    Lisp_Object value =
      execute_optimized_program ((Opbyte *) XOPAQUE_DATA (f->instructions),
#ifdef ERROR_CHECK_BYTE_CODE
				 XOPAQUE_SIZE (f->instructions) /
				 sizeof (Opbyte),
#endif
				 f->stack_depth,
				 XVECTOR_DATA (f->constants));

    /* The attempt to optimize this by only unbinding variables failed
       because using buffer-local variables as function parameters
       leads to specpdl_ptr->func != 0 */
    /* UNBIND_TO_GCPRO_VARIABLES_ONLY (speccount, value); */
    UNBIND_TO_GCPRO (speccount, value);
    return value;
  }
}

DEFUN ("eval", Feval, 1, 1, 0, /*
Evaluate FORM and return its value.
*/
       (form))
{
  /* This function can GC */
  Lisp_Object fun, val, original_fun, original_args;
  int nargs;
  PROFILE_DECLARE();

#ifdef ERROR_CHECK_TRAPPING_PROBLEMS
  check_proper_critical_section_lisp_protection ();
#endif

  if (!CONSP (form))
    {
      if (SYMBOLP (form))
        {
          return Fsymbol_value (form);
        }

      return form;
    }

  /* I think this is a pretty safe place to call Lisp code, don't you? */
  while (!in_warnings && !NILP (Vpending_warnings)
	 /* well, perhaps not so safe after all! */
	 && !(inhibit_flags & INHIBIT_ANY_CHANGE_AFFECTING_REDISPLAY))
    {
      struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
      Lisp_Object this_warning_cons, this_warning, class_, level, messij;
      int speccount = internal_bind_int (&in_warnings, 1);

      this_warning_cons = Vpending_warnings;
      this_warning = XCAR (this_warning_cons);
      /* in case an error occurs in the warn function, at least
	 it won't happen infinitely */
      Vpending_warnings = XCDR (Vpending_warnings);
      free_cons (this_warning_cons);
      class_ = XCAR (this_warning);
      level = XCAR (XCDR (this_warning));
      messij = XCAR (XCDR (XCDR (this_warning)));
      free_list (this_warning);

      if (NILP (Vpending_warnings))
	Vpending_warnings_tail = Qnil; /* perhaps not strictly necessary,
					  but safer */

      GCPRO4 (form, class_, level, messij);
      if (!STRINGP (messij))
	messij = Fprin1_to_string (messij, Qnil);
      call3 (Qdisplay_warning, class_, messij, level);
      UNGCPRO;
      unbind_to (speccount);
    }

  QUIT;
  if (need_to_garbage_collect)
    {
      struct gcpro gcpro1;
      GCPRO1 (form);
#ifdef NEW_GC
      gc_incremental ();
#else /* not NEW_GC */
      garbage_collect_1 ();
#endif /* not NEW_GC */
      UNGCPRO;
    }

  if (++lisp_eval_depth > max_lisp_eval_depth)
    {
      if (max_lisp_eval_depth < 100)
	max_lisp_eval_depth = 100;
      if (lisp_eval_depth > max_lisp_eval_depth)
	stack_overflow ("Lisp nesting exceeds `max-lisp-eval-depth'",
			Qunbound);
    }

  /* We guaranteed CONSP (form) above */
  original_fun  = XCAR (form);
  original_args = XCDR (form);

  GET_EXTERNAL_LIST_LENGTH (original_args, nargs);

  backtrace.pdlcount = specpdl_depth();
  backtrace.function = &original_fun; /* This also protects them from gc */
  backtrace.args = &original_args;
  backtrace.nargs = UNEVALLED;
  backtrace.evalargs = 1;
  backtrace.debug_on_exit = 0;
  backtrace.function_being_called = 0;
  PUSH_BACKTRACE (backtrace);

  if (debug_on_next_call)
    do_debug_on_call (Qt);

  /* At this point, only original_fun and original_args
     have values that will be used below. */
 retry:
  /* Optimise for no indirection.  */
  fun = original_fun;
  if (SYMBOLP (fun) && !EQ (fun, Qunbound)
      && (fun = XSYMBOL (fun)->function, SYMBOLP (fun)))
    {
      fun = indirect_function(original_fun, 1);
    }

  if (SUBRP (fun))
    {
      Lisp_Subr *subr = XSUBR (fun);
      int max_args = subr->max_args;

      if (nargs < subr->min_args)
	goto wrong_number_of_arguments;

      if (max_args == UNEVALLED) /* Optimize for the common case */
	{
	  backtrace.evalargs = 0;
	  PROFILE_ENTER_FUNCTION ();
	  val = (((Lisp_Object (*) (Lisp_Object)) subr_function (subr))
		 (original_args));
	  PROFILE_EXIT_FUNCTION ();
	}
      else if (nargs <= max_args)
        {
          struct gcpro gcpro1;
	  Lisp_Object args[SUBR_MAX_ARGS];
	  REGISTER Lisp_Object *p = args;

	  GCPRO1 (args[0]);
	  gcpro1.nvars = 0;

	  {
	    LIST_LOOP_2 (arg, original_args)
	      {
                *p++ = IGNORE_MULTIPLE_VALUES (Feval (arg));
		gcpro1.nvars++;
	      }
	  }

	  /* &optional args default to nil. */
	  while (p - args < max_args)
	    *p++ = Qnil;

          backtrace.args  = args;
          backtrace.nargs = nargs;

	  PROFILE_ENTER_FUNCTION ();
	  FUNCALL_SUBR (val, subr, args, max_args);
	  PROFILE_EXIT_FUNCTION ();

	  UNGCPRO;
        }
      else if (max_args == MANY)
	{
	  /* Pass a vector of evaluated arguments */
          struct gcpro gcpro1;
	  Lisp_Object *args = alloca_array (Lisp_Object, nargs);
	  REGISTER Lisp_Object *p = args;

	  GCPRO1 (args[0]);
	  gcpro1.nvars = 0;

	  {
	    LIST_LOOP_2 (arg, original_args)
	      {
                *p++ = IGNORE_MULTIPLE_VALUES (Feval (arg));
		gcpro1.nvars++;
	      }
	  }

	  backtrace.args  = args;
	  backtrace.nargs = nargs;

	  PROFILE_ENTER_FUNCTION ();
	  val = (((Lisp_Object (*) (int, Lisp_Object *)) subr_function (subr))
		 (nargs, args));
	  PROFILE_EXIT_FUNCTION ();

	  UNGCPRO;
	}
      else
	{
	wrong_number_of_arguments:
	  val = signal_wrong_number_of_arguments_error (original_fun, nargs);
	}
    }
  else if (COMPILED_FUNCTIONP (fun))
    {
      struct gcpro gcpro1;
      Lisp_Object *args = alloca_array (Lisp_Object, nargs);
      REGISTER Lisp_Object *p = args;

      GCPRO1 (args[0]);
      gcpro1.nvars = 0;

      {
	LIST_LOOP_2 (arg, original_args)
	  {
            *p++ = IGNORE_MULTIPLE_VALUES (Feval (arg));
	    gcpro1.nvars++;
	  }
      }

      backtrace.args     = args;
      backtrace.nargs    = nargs;
      backtrace.evalargs = 0;

      PROFILE_ENTER_FUNCTION ();
      val = funcall_compiled_function (fun, nargs, args);
      PROFILE_EXIT_FUNCTION ();

      /* Do the debug-on-exit now, while args is still GCPROed.  */
      if (backtrace.debug_on_exit)
	val = do_debug_on_exit (val);
      /* Don't do it again when we return to eval.  */
      backtrace.debug_on_exit = 0;

      UNGCPRO;
    }
  else if (CONSP (fun))
    {
      Lisp_Object funcar = XCAR (fun);

      if (EQ (funcar, Qautoload))
	{
	  /* do_autoload GCPROs both arguments */
	  do_autoload (fun, original_fun);
	  goto retry;
	}
      else if (EQ (funcar, Qmacro))
	{
	  PROFILE_ENTER_FUNCTION ();
	  val = Feval (apply1 (XCDR (fun), original_args));
	  PROFILE_EXIT_FUNCTION ();
	}
      else if (EQ (funcar, Qlambda))
	{
	  struct gcpro gcpro1;
	  Lisp_Object *args = alloca_array (Lisp_Object, nargs);
	  REGISTER Lisp_Object *p = args;

	  GCPRO1 (args[0]);
	  gcpro1.nvars = 0;

	  {
	    LIST_LOOP_2 (arg, original_args)
	      {
                *p++ = IGNORE_MULTIPLE_VALUES (Feval (arg));
		gcpro1.nvars++;
	      }
	  }

	  UNGCPRO;

	  backtrace.args     = args; /* this also GCPROs `args' */
	  backtrace.nargs    = nargs;
	  backtrace.evalargs = 0;

	  PROFILE_ENTER_FUNCTION ();
	  val = funcall_lambda (fun, nargs, args);
	  PROFILE_EXIT_FUNCTION ();

	  /* Do the debug-on-exit now, while args is still GCPROed.  */
	  if (backtrace.debug_on_exit)
	    val = do_debug_on_exit (val);
	  /* Don't do it again when we return to eval.  */
	  backtrace.debug_on_exit = 0;
	}
      else
	{
	  goto invalid_function;
	}
    }
  else if (UNBOUNDP (fun))
    {
      val = signal_void_function_error (original_fun);
    }
  else /* ! (SUBRP (fun) || COMPILED_FUNCTIONP (fun) || CONSP (fun)
             UNBOUNDP (fun)) */
    {
    invalid_function:
      val = signal_invalid_function_error (fun);
    }

  lisp_eval_depth--;
  if (backtrace.debug_on_exit)
    val = do_debug_on_exit (val);
  POP_BACKTRACE (backtrace);
  return val;
}



static void
run_post_gc_hook (void)
{
  Lisp_Object args[2];

  args[0] = Qpost_gc_hook;
  args[1] = Fcons (Fcons (Qfinalize_list, zap_finalize_list ()), Qnil);
  
  run_hook_with_args_trapping_problems
    (Qgarbage_collecting, 2, args, RUN_HOOKS_TO_COMPLETION,
     INHIBIT_QUIT | NO_INHIBIT_ERRORS);
}

DEFUN ("funcall", Ffuncall, 1, MANY, 0, /*
Call FUNCTION as a function, passing the remaining arguments to it.
Thus, (funcall 'cons 'x 'y) returns (x . y).

arguments: (FUNCTION &rest ARGS)
*/
       (int nargs, Lisp_Object *args))
{
  /* This function can GC */
  Lisp_Object fun;
  Lisp_Object val;
  PROFILE_DECLARE();
  int fun_nargs = nargs - 1;
  Lisp_Object *fun_args = args + 1;

  /* QUIT will check for proper redisplay wrapping */

  QUIT;

  if (funcall_allocation_flag)
    {
      if (need_to_garbage_collect)
	/* Callers should gcpro lexpr args */
#ifdef NEW_GC
	gc_incremental ();
#else /* not NEW_GC */
	garbage_collect_1 ();
#endif /* not NEW_GC */
      if (need_to_check_c_alloca)
	{
	  if (++funcall_alloca_count >= MAX_FUNCALLS_BETWEEN_ALLOCA_CLEANUP)
	    {
	      xemacs_c_alloca (0);
	      funcall_alloca_count = 0;
	    }
	}
      if (need_to_signal_post_gc)
	{
	  need_to_signal_post_gc = 0;
	  recompute_funcall_allocation_flag ();
#ifdef NEW_GC
	  run_finalizers ();
#endif /* NEW_GC */
	  run_post_gc_hook ();
	}
    }

  if (++lisp_eval_depth > max_lisp_eval_depth)
    {
      if (max_lisp_eval_depth < 100)
	max_lisp_eval_depth = 100;
      if (lisp_eval_depth > max_lisp_eval_depth)
	stack_overflow ("Lisp nesting exceeds `max-lisp-eval-depth'",
			Qunbound);
    }

  backtrace.pdlcount = specpdl_depth ();
  backtrace.function = &args[0];
  backtrace.args  = fun_args;
  backtrace.nargs = fun_nargs;
  backtrace.evalargs = 0;
  backtrace.debug_on_exit = 0;
  backtrace.function_being_called = 0;
  PUSH_BACKTRACE (backtrace);

  if (debug_on_next_call)
    do_debug_on_call (Qlambda);

 retry:

  fun = args[0];

  /* We could call indirect_function directly, but profiling shows
     this is worth optimizing by partially unrolling the loop.  */
  if (SYMBOLP (fun))
    {
      fun = XSYMBOL (fun)->function;
      if (SYMBOLP (fun))
	{
	  fun = XSYMBOL (fun)->function;
	  if (SYMBOLP (fun))
	    fun = indirect_function (fun, 1);
	}
    }

  if (SUBRP (fun))
    {
      Lisp_Subr *subr = XSUBR (fun);
      int max_args = subr->max_args;
      Lisp_Object spacious_args[SUBR_MAX_ARGS];

      if (fun_nargs == max_args) /* Optimize for the common case */
	{
	funcall_subr:
	  PROFILE_ENTER_FUNCTION ();
	  FUNCALL_SUBR (val, subr, fun_args, max_args);
	  PROFILE_EXIT_FUNCTION ();
	}
      else if (fun_nargs < subr->min_args)
	{
	  goto wrong_number_of_arguments;
	}
      else if (fun_nargs < max_args)
	{
	  Lisp_Object *p = spacious_args;

          /* Default optionals to nil */
	  while (fun_nargs--)
	    *p++ = *fun_args++;
	  while (p - spacious_args < max_args)
	    *p++ = Qnil;

	  fun_args = spacious_args;
	  goto funcall_subr;
	}
      else if (max_args == MANY)
	{
	  PROFILE_ENTER_FUNCTION ();
	  val = SUBR_FUNCTION (subr, MANY) (fun_nargs, fun_args);
	  PROFILE_EXIT_FUNCTION ();
	}
      else if (max_args == UNEVALLED) /* Can't funcall a special operator */
	{

#ifdef NEED_TO_HANDLE_21_4_CODE
          /* Ugh, ugh, ugh. */
          if (EQ (fun, XSYMBOL_FUNCTION (Qthrow)))
            {
              args[0] = Qobsolete_throw;
              goto retry;
            }
#endif /* NEED_TO_HANDLE_21_4_CODE */

	  goto invalid_function;
	}
      else
	{
	wrong_number_of_arguments:
	  val = signal_wrong_number_of_arguments_error (fun, fun_nargs);
	}
    }
  else if (COMPILED_FUNCTIONP (fun))
    {
      PROFILE_ENTER_FUNCTION ();
      val = funcall_compiled_function (fun, fun_nargs, fun_args);
      PROFILE_EXIT_FUNCTION ();
    }
  else if (CONSP (fun))
    {
      Lisp_Object funcar = XCAR (fun);

      if (EQ (funcar, Qlambda))
	{
	  PROFILE_ENTER_FUNCTION ();
	  val = funcall_lambda (fun, fun_nargs, fun_args);
	  PROFILE_EXIT_FUNCTION ();
	}
      else if (EQ (funcar, Qautoload))
	{
	  /* do_autoload GCPROs both arguments */
	  do_autoload (fun, args[0]);
	  goto retry;
	}
      else /* Can't funcall a macro */
	{
	  goto invalid_function;
	}
    }
  else if (UNBOUNDP (fun))
    {
      val = signal_void_function_error (args[0]);
    }
  else
    {
    invalid_function:
      val = signal_invalid_function_error (fun);
    }

  lisp_eval_depth--;
  if (backtrace.debug_on_exit)
    val = do_debug_on_exit (val);
  POP_BACKTRACE (backtrace);
  return val;
}

DEFUN ("functionp", Ffunctionp, 1, 1, 0, /*
Return t if OBJECT can be called as a function, else nil.
A function is an object that can be applied to arguments,
using for example `funcall' or `apply'.
*/
       (object))
{
  if (SYMBOLP (object))
    object = indirect_function (object, 0);

  if (COMPILED_FUNCTIONP (object)
      || (SUBRP (object)
	  && (XSUBR (object)->max_args != UNEVALLED)))
    return Qt;
  if (CONSP (object))
    {
      Lisp_Object car = XCAR (object);
      if (EQ (car, Qlambda))
	return Qt;
      if (EQ (car, Qautoload)
	  && NILP (Fcar_safe (Fcdr_safe(Fcdr_safe
					(Fcdr_safe (XCDR (object)))))))
	return Qt;
    }
  return Qnil;
}

static Lisp_Object
function_argcount (Lisp_Object function, int function_min_args_p)
{
  Lisp_Object orig_function = function;
  Lisp_Object arglist;

 retry:

  if (SYMBOLP (function))
    function = indirect_function (function, 1);

  if (SUBRP (function))
    {
      /* Using return with the ?: operator tickles a DEC CC compiler bug. */
      if (function_min_args_p)
	return Fsubr_min_args (function);
      else
	return Fsubr_max_args (function);
   }
  else if (COMPILED_FUNCTIONP (function))
    {
      Lisp_Compiled_Function *f = XCOMPILED_FUNCTION (function);
      
      if (!OPAQUEP (f->instructions))
	      /* Lazily munge the instructions into a more efficient form */
	      /* Needed to set max_args */
	      optimize_compiled_function (function);

      if (function_min_args_p)
	return make_int (f->min_args);
      else if (f->max_args == MANY)
	return Qnil;
      else
	return make_int (f->max_args);
    }
  else if (CONSP (function))
    {
      Lisp_Object funcar = XCAR (function);

      if (EQ (funcar, Qmacro))
	{
	  function = XCDR (function);
	  goto retry;
	}
      else if (EQ (funcar, Qautoload))
	{
	  /* do_autoload GCPROs both arguments */
	  do_autoload (function, orig_function);
	  function = orig_function;
	  goto retry;
	}
      else if (EQ (funcar, Qlambda))
	{
	  arglist = Fcar (XCDR (function));
	}
      else
	{
	  goto invalid_function;
	}
    }
  else
    {
    invalid_function:
      return signal_invalid_function_error (orig_function);
    }

  {
    int argcount = 0;

    EXTERNAL_LIST_LOOP_2 (arg, arglist)
      {
	if (EQ (arg, Qand_optional))
	  {
	    if (function_min_args_p)
	      break;
	  }
	else if (EQ (arg, Qand_rest))
	  {
	    if (function_min_args_p)
	      break;
	    else
	      return Qnil;
	  }
	else
	  {
	    argcount++;
	  }
      }

    return make_int (argcount);
  }
}

DEFUN ("function-min-args", Ffunction_min_args, 1, 1, 0, /*
Return the minimum number of arguments a function may be called with.
The function may be any form that can be passed to `funcall',
any special operator, or any macro.

To check if a function can be called with a specified number of
arguments, use `function-allows-args'.
*/
       (function))
{
  return function_argcount (function, 1);
}

DEFUN ("function-max-args", Ffunction_max_args, 1, 1, 0, /*
Return the maximum number of arguments a function may be called with.
The function may be any form that can be passed to `funcall',
any special operator, or any macro.
If the function takes an arbitrary number of arguments or is
a built-in special operator, nil is returned.

To check if a function can be called with a specified number of
arguments, use `function-allows-args'.
*/
       (function))
{
  return function_argcount (function, 0);
}


DEFUN ("apply", Fapply, 2, MANY, 0, /*
Call FUNCTION with the remaining args, using the last arg as a list of args.
Thus, (apply '+ 1 2 '(3 4)) returns 10.

arguments: (FUNCTION &rest ARGS)
*/
       (int nargs, Lisp_Object *args))
{
  /* This function can GC */
  Lisp_Object fun = args[0];
  Lisp_Object spread_arg = args [nargs - 1];
  int numargs;
  int funcall_nargs;

  GET_EXTERNAL_LIST_LENGTH (spread_arg, numargs);

  if (numargs == 0)
    /* (apply foo 0 1 '()) */
    return Ffuncall (nargs - 1, args);
  else if (numargs == 1)
    {
      /* (apply foo 0 1 '(2)) */
      args [nargs - 1] = XCAR (spread_arg);
      return Ffuncall (nargs, args);
    }

  /* -1 for function, -1 for spread arg */
  numargs = nargs - 2 + numargs;
  /* +1 for function */
  funcall_nargs = 1 + numargs;

  if (SYMBOLP (fun))
    fun = indirect_function (fun, 0);

  if (SUBRP (fun))
    {
      Lisp_Subr *subr = XSUBR (fun);
      int max_args = subr->max_args;

      if (numargs < subr->min_args
	  || (max_args >= 0 && max_args < numargs))
        {
          /* Let funcall get the error */
        }
      else if (max_args > numargs)
	{
	  /* Avoid having funcall cons up yet another new vector of arguments
	     by explicitly supplying nil's for optional values */
          funcall_nargs += (max_args - numargs);
        }
    }
  else if (UNBOUNDP (fun))
    {
      /* Let funcall get the error */
      fun = args[0];
    }

  {
    REGISTER int i;
    Lisp_Object *funcall_args = alloca_array (Lisp_Object, funcall_nargs);
    struct gcpro gcpro1;

    GCPRO1 (*funcall_args);
    gcpro1.nvars = funcall_nargs;

    /* Copy in the unspread args */
    memcpy (funcall_args, args, (nargs - 1) * sizeof (Lisp_Object));
    /* Spread the last arg we got.  Its first element goes in
       the slot that it used to occupy, hence this value of I.  */
    for (i = nargs - 1;
         !NILP (spread_arg);    /* i < 1 + numargs */
         i++, spread_arg = XCDR (spread_arg))
      {
	funcall_args [i] = XCAR (spread_arg);
      }
    /* Supply nil for optional args (to subrs) */
    for (; i < funcall_nargs; i++)
      funcall_args[i] = Qnil;


    RETURN_UNGCPRO (Ffuncall (funcall_nargs, funcall_args));
  }
}

/* Apply lambda list FUN to the NARGS evaluated arguments in ARGS and
   return the result of evaluation. */

static Lisp_Object
funcall_lambda (Lisp_Object fun, int nargs, Lisp_Object args[])
{
  /* This function can GC */
  Lisp_Object arglist, body, tail;
  int speccount = specpdl_depth();
  REGISTER int i = 0;

  tail = XCDR (fun);

  if (!CONSP (tail))
    goto invalid_function;

  arglist = XCAR (tail);
  body    = XCDR (tail);

  {
    int optional = 0, rest = 0;

    EXTERNAL_LIST_LOOP_2 (symbol, arglist)
      {
	if (!SYMBOLP (symbol))
	  goto invalid_function;
	if (EQ (symbol, Qand_rest))
	  rest = 1;
	else if (EQ (symbol, Qand_optional))
	  optional = 1;
	else if (rest)
	  {
	    specbind (symbol, Flist (nargs - i, &args[i]));
	    i = nargs;
	  }
	else if (i < nargs)
	  specbind (symbol, args[i++]);
	else if (!optional)
	  goto wrong_number_of_arguments;
	else
	  specbind (symbol, Qnil);
      }
  }

  if (i < nargs)
    goto wrong_number_of_arguments;

  return unbind_to_1 (speccount, Fprogn (body));

 wrong_number_of_arguments:
  return signal_wrong_number_of_arguments_error (fun, nargs);

 invalid_function:
  return signal_invalid_function_error (fun);
}


/* Multiple values. 

   A multiple value object is returned by #'values if:

   -- The number of arguments to #'values is not one, and: 
   -- Some special operator in the call stack is prepared to handle more than
   one multiple value.
   
   The return value of #'values-list is analogous to that of #'values.

   Henry Baker, in https://eprints.kfupm.edu.sa/31898/1/31898.pdf ("CONS
   Should not CONS its Arguments, or, a Lazy Alloc is a Smart Alloc", ACM
   Sigplan Notices 27,3 (March 1992),24-34.) says it should be possible to
   allocate Common Lisp multiple-value objects on the stack, but this
   assumes that variable-length records can be allocated on the stack,
   something not true for us. As far as I can tell, it also ignores the
   contexts where multiple-values need to be thrown, or maybe it thinks such
   objects should be converted to heap allocation at that point.

   The specific multiple values saved and returned depend on how many
   multiple-values special operators in the stack are interested in; for
   example, if #'multiple-value-call is somewhere in the call stack, all
   values passed to #'values will be saved and returned.  If an expansion of
   #'multiple-value-setq with 10 SYMS is the only part of the call stack
   interested in multiple values, then a maximum of ten multiple values will
   be saved and returned.

   (#'throw passes back multiple values in its VALUE argument; this is why
   we can't just take the details of the most immediate
   #'multiple-value-{whatever} call to work out which values to save, we
   need to look at the whole stack, or, equivalently, the dynamic variables
   we set to reflect the whole stack.)

   The first value passed to #'values will always be saved, since that is
   needed to convert a multiple value object into a single value object,
   something that is normally necessary independent of how many functions in
   the call stack are interested in multiple values.

   However many values (for values of "however many" that are not one) are
   saved and restored, the multiple value object knows how many arguments it
   would contain were none to have been discarded, and will indicate this
   on being printed from within GDB.

   In lisp-interaction-mode, no multiple values should be discarded (unless
   they need to be for the sake of the correctness of the program);
   #'eval-interactive-with-multiple-value-list in lisp-mode.el wraps its
   #'eval calls with #'multiple-value-list calls to avoid this. This means
   that there is a small performance and memory penalty for code evaluated
   in *scratch*; use M-: EXPRESSION RET if you really need to avoid
   this. Lisp code execution that is not ultimately from hitting C-j in
   *scratch*--that is, the vast vast majority of Lisp code execution--does
   not have this penalty.

   Probably the most important aspect of multiple values is stated with
   admirable clarity by CLTL2:

     "No matter how many values a form produces, if the form is an argument
     form in a function call, then exactly one value (the first one) is
     used."
   
   This means that most contexts, most of the time, will never see multiple
   values.  There are important exceptions; search the web for that text in
   quotation marks and read the related chapter. This code handles all of
   them, to my knowledge. Aidan Kehoe, Mon Mar 16 00:17:39 GMT 2009. */

static Lisp_Object
make_multiple_value (Lisp_Object first_value, Elemcount count,
                     Elemcount first_desired, Elemcount upper_limit)
{
  Bytecount sizem;
  struct multiple_value *mv;
  Elemcount i, allocated_count;
  Lisp_Object mvobj;

  assert (count != 1);

  if (1 != upper_limit && (0 == first_desired))
    {
      /* We always allocate element zero, and that's taken into account when
         working out allocated_count: */
      first_desired = 1;
    }

  if (first_desired >= count)
    {
      /* We can't pass anything back that our caller is interested in. Only
         allocate for the first argument. */
      allocated_count = 1;
    }
  else
    {
      allocated_count = 1 + ((upper_limit > count ? count : upper_limit)
                             - first_desired);
    }

  sizem = FLEXIBLE_ARRAY_STRUCT_SIZEOF (multiple_value,
                                        Lisp_Object,
                                        contents, allocated_count);
  mvobj = ALLOC_SIZED_LISP_OBJECT (sizem, multiple_value);
  mv = XMULTIPLE_VALUE (mvobj);

  mv->count = count;
  mv->first_desired = first_desired;
  mv->allocated_count = allocated_count;
  mv->contents[0] = first_value;

  for (i = first_desired; i < upper_limit && i < count; ++i)
    {
      mv->contents[1 + (i - first_desired)] = Qunbound;
    }

  return mvobj;
}

void
multiple_value_aset (Lisp_Object obj, Elemcount index, Lisp_Object value)
{
  struct multiple_value *mv = XMULTIPLE_VALUE (obj);
  Elemcount first_desired = mv->first_desired; 
  Elemcount allocated_count = mv->allocated_count; 

  if (index != 0 &&
      (index < first_desired || index >= (first_desired + allocated_count)))
    {
      args_out_of_range (make_int (first_desired),
                         make_int (first_desired + allocated_count));
    }

  mv->contents[index == 0 ? 0 : 1 + (index - first_desired)] = value;
}

Lisp_Object
multiple_value_aref (Lisp_Object obj, Elemcount index)
{
  struct multiple_value *mv = XMULTIPLE_VALUE (obj);
  Elemcount first_desired = mv->first_desired; 
  Elemcount allocated_count = mv->allocated_count; 

  if (index != 0 &&
      (index < first_desired || index >= (first_desired + allocated_count)))
    {
      args_out_of_range (make_int (first_desired),
                         make_int (first_desired + allocated_count));
    }

  return mv->contents[index == 0 ? 0 : 1 + (index - first_desired)];
}

static void
print_multiple_value (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
{
  struct multiple_value *mv = XMULTIPLE_VALUE (obj);
  Elemcount first_desired = mv->first_desired; 
  Elemcount allocated_count = mv->allocated_count; 
  Elemcount count = mv->count, index;

  if (print_readably)
    {
      printing_unreadable_object_fmt ("#<multiple values 0x%x>",
				      LISP_OBJECT_UID (obj));
    }

  write_fmt_string (printcharfun,
                    "#<INTERNAL OBJECT (XEmacs bug?) %d multiple values,"
                    " data (", count);

  for (index = 0; index < count;)
    {
      if (index != 0 &&
          (index < first_desired ||
           index >= (first_desired + (allocated_count - 1))))
        {
          write_fmt_string (printcharfun, "#<discarded-multiple-value %d>",
                            index);
        }
      else
        {
          print_internal (multiple_value_aref (obj, index),
                          printcharfun, escapeflag);
        }

      ++index;

      if (count > 1 && index < count)
        {
          write_ascstring (printcharfun, " ");
        }
    }

  write_fmt_string (printcharfun, ") 0x%x>", LISP_OBJECT_UID (obj));
}

static Lisp_Object
mark_multiple_value (Lisp_Object obj)
{
  struct multiple_value *mv = XMULTIPLE_VALUE (obj);
  Elemcount index, allocated_count = mv->allocated_count;

  for (index = 0; index < allocated_count; ++index)
    {
      mark_object (mv->contents[index]);
    }

  return Qnil;
}

static Bytecount
size_multiple_value (Lisp_Object obj)
{
  return FLEXIBLE_ARRAY_STRUCT_SIZEOF (struct multiple_value,
                                       Lisp_Object, contents,
                                       XMULTIPLE_VALUE (obj)->allocated_count);
}

static const struct memory_description multiple_value_description[] = {
  { XD_LONG, offsetof (struct multiple_value, count) },
  { XD_ELEMCOUNT, offsetof (struct multiple_value, allocated_count) },
  { XD_LONG, offsetof (struct multiple_value, first_desired) },
  { XD_LISP_OBJECT_ARRAY, offsetof (struct multiple_value, contents),
    XD_INDIRECT (1, 0) },
  { XD_END }
};

DEFINE_DUMPABLE_SIZABLE_LISP_OBJECT ("multiple-value", multiple_value,
				     mark_multiple_value,
				     print_multiple_value, 0,
				     0, /* No equal method. */
				     0, /* No hash method. */
				     multiple_value_description,
				     size_multiple_value,
				     struct multiple_value);

/* Given that FIRST and UPPER are the inclusive lower and exclusive upper
   bounds for the multiple values we're interested in, modify (or don't) the
   special variables used to indicate this to #'values and #'values-list.
   Returns the specpdl_depth() value before any modification. */
int
bind_multiple_value_limits (int first, int upper)
{
  int result = specpdl_depth();

  if (!(upper > first))
    {
      invalid_argument ("MULTIPLE-VALUE-UPPER-LIMIT must be greater than "
                        " FIRST-DESIRED-MULTIPLE-VALUE", Qunbound);
    }

  if (upper > Vmultiple_values_limit)
    {
      args_out_of_range (make_int (upper), make_int (Vmultiple_values_limit));
    }

  /* In the event that something back up the stack wants more multiple
     values than we do, we need to keep its figures for
     first_desired_multiple_value or multiple_value_current_limit both. It
     may be that the form will throw past us.

     If first_desired_multiple_value is zero, this means it hasn't ever been
     bound, and any value we have for first is appropriate to use.

     Zeroth element is always saved, no need to note that: */
  if (0 == first)
    {
      first = 1;
    }

  if (0 == first_desired_multiple_value
      || first < first_desired_multiple_value)
    {
      internal_bind_int (&first_desired_multiple_value, first);      
    }

  if (upper > multiple_value_current_limit)
    {
      internal_bind_int (&multiple_value_current_limit, upper);
    }

  return result;
}

Lisp_Object
multiple_value_call (int nargs, Lisp_Object *args)
{
  /* The argument order here is horrible: */
  int i, speccount = XINT (args[3]);
  Lisp_Object result = Qnil, head = Fcons (args[0], Qnil), list_offset; 
  struct gcpro gcpro1, gcpro2;
  Lisp_Object apply_args[2];
  
  GCPRO2 (head, result);
  list_offset = head;

  assert (!(MULTIPLE_VALUEP (args[0])));
  CHECK_FUNCTION (args[0]);

  /* Start at 4, to ignore the function, the speccount, and the arguments to
     multiple-values-limit (which we don't discard because
     #'multiple-value-list-internal needs them): */
  for (i = 4; i < nargs; ++i)
    {
      result = args[i];
      if (MULTIPLE_VALUEP (result))
        {
          Lisp_Object val;
          Elemcount j, count = XMULTIPLE_VALUE_COUNT (result);

          for (j = 0; j < count; j++)
            {
              val = multiple_value_aref (result, j);
              assert (!UNBOUNDP (val));

              XSETCDR (list_offset, Fcons (val, Qnil));
              list_offset = XCDR (list_offset);
            }
        }
      else
        {
          XSETCDR (list_offset, Fcons (result, Qnil));
          list_offset = XCDR (list_offset);
        }
    }

  apply_args [0] = XCAR (head);
  apply_args [1] = XCDR (head);

  unbind_to (speccount);

  RETURN_UNGCPRO (Fapply (countof(apply_args), apply_args));
}

DEFUN ("multiple-value-call", Fmultiple_value_call, 1, UNEVALLED, 0, /*
Call FUNCTION with arguments FORMS, using multiple values when returned.

All of the (possibly multiple) values returned by each form in FORMS are
gathered together, and given as arguments to FUNCTION; conceptually, this
function is a version of `apply' that by-passes the multiple values
infrastructure, treating multiple values as intercalated lists.

arguments: (FUNCTION &rest FORMS)
*/
       (args))
{
  int listcount, i = 0, speccount;
  Lisp_Object *constructed_args;
  struct gcpro gcpro1;

  GET_EXTERNAL_LIST_LENGTH (args, listcount);

  constructed_args = alloca_array (Lisp_Object, listcount + 3);

  /* Fcar so we error on non-cons: */
  constructed_args[i] = IGNORE_MULTIPLE_VALUES (Feval (Fcar (args)));

  GCPRO1 (*constructed_args);
  gcpro1.nvars = ++i; 

  /* The argument order is horrible here. */
  constructed_args[i] = make_int (0);
  gcpro1.nvars = ++i;
  constructed_args[i] = make_int (Vmultiple_values_limit);
  gcpro1.nvars = ++i;

  speccount = bind_multiple_value_limits (0, Vmultiple_values_limit);
  constructed_args[i] = make_int (speccount);
  gcpro1.nvars = ++i;

  {
    LIST_LOOP_2 (elt, XCDR (args))
      {
        constructed_args[i] = Feval (elt);
        gcpro1.nvars = ++i;
      }
  }

  RETURN_UNGCPRO (multiple_value_call (listcount + 3, constructed_args));
}

Lisp_Object
multiple_value_list_internal (int nargs, Lisp_Object *args)
{
  int first = XINT (args[0]), upper = XINT (args[1]),
    speccount = XINT(args[2]);
  Lisp_Object result = Qnil;

  assert (nargs == 4);

  result = args[3];

  unbind_to (speccount); 

  if (MULTIPLE_VALUEP (result))
    {
      Lisp_Object head = Fcons (Qnil, Qnil);
      Lisp_Object list_offset = head, val; 
      Elemcount count = XMULTIPLE_VALUE_COUNT(result);
      
      for (; first < upper && first < count; ++first)
        {
          val = multiple_value_aref (result, first);
          assert (!UNBOUNDP (val));

          XSETCDR (list_offset, Fcons (val, Qnil));
          list_offset = XCDR (list_offset);
        }

      return XCDR (head);
    }
  else
    {
      if (first == 0)
	{
          return Fcons (result, Qnil);
        }
      else
        {
          return Qnil;
        }
    }
}

DEFUN ("multiple-value-list-internal", Fmultiple_value_list_internal, 3,
       UNEVALLED, 0, /*
Evaluate FORM. Return a list of multiple vals reflecting the other two args.

Don't use this.  Use `multiple-value-list', the macro specified by Common
Lisp, instead.

FIRST-DESIRED-MULTIPLE-VALUE is the first element in list of multiple values
to pass back.  MULTIPLE-VALUE-UPPER-LIMIT is the exclusive upper limit on
the indexes within the values that may be passed back; this function will
never return a list longer than MULTIPLE-VALUE-UPPER-LIMIT -
FIRST-DESIRED-MULTIPLE-VALUE.  It may return a list shorter than that, if
`values' or `values-list' do not supply enough elements.

arguments: (FIRST-DESIRED-MULTIPLE-VALUE MULTIPLE-VALUE-UPPER-LIMIT FORM)
*/
       (args))
{
  Lisp_Object argv[4];
  int first, upper, nargs;
  struct gcpro gcpro1;

  GET_LIST_LENGTH (args, nargs);
  if (nargs != 3)
    {
      Fsignal (Qwrong_number_of_arguments,
               list2 (Qmultiple_value_list_internal, make_int (nargs)));
    }

  argv[0] = IGNORE_MULTIPLE_VALUES (Feval (XCAR (args)));

  GCPRO1 (argv[0]);
  gcpro1.nvars = 1;

  args = XCDR (args);
  argv[1] = IGNORE_MULTIPLE_VALUES (Feval (XCAR (args)));

  check_integer_range (argv[1], Qzero, make_int (EMACS_INT_MAX));
  check_integer_range (argv[0], Qzero, argv[1]);

  upper = XINT (argv[1]);
  first = XINT (argv[0]);

  gcpro1.nvars = 2;

  /* The unintuitive order of things here is for the sake of the bytecode;
     the alternative would be to encode the number of arguments in the
     bytecode stream, which complicates things if we have more than 255
     arguments. */
  argv[2] = make_int (bind_multiple_value_limits (first, upper));
  gcpro1.nvars = 3;
  args = XCDR (args);

  /* GCPROing in this function is not strictly necessary, this Feval is the
     only point that may cons up data that is not immediately discarded, and
     within it is the only point (in Fmultiple_value_list_internal and
     multiple_value_list) that we can garbage collect. But I'm conservative,
     and this function is called so rarely (only from interpreted code) that
     it doesn't matter for performance. */
  argv[3] = Feval (XCAR (args));
  gcpro1.nvars = 4;

  RETURN_UNGCPRO (multiple_value_list_internal (countof (argv), argv));
}

DEFUN ("multiple-value-prog1", Fmultiple_value_prog1, 1, UNEVALLED, 0, /*
Similar to `prog1', but return any multiple values from the first form. 
`prog1' itself will never return multiple values. 

arguments: (FIRST &rest BODY)
*/
       (args))
{
  /* This function can GC */
  Lisp_Object val;
  struct gcpro gcpro1;

  val = Feval (XCAR (args));

  GCPRO1 (val);

  {
    LIST_LOOP_2 (form, XCDR (args))
      Feval (form);
  }

  RETURN_UNGCPRO (val); 
}  

DEFUN ("values", Fvalues, 0, MANY, 0, /*
Return all ARGS as multiple values.

arguments: (&rest ARGS)
*/
       (int nargs, Lisp_Object *args))
{
  Lisp_Object result = Qnil;
  int counting = 1;

  /* Pathological cases, no need to cons up an object: */
  if (1 == nargs || 1 == multiple_value_current_limit)
    {
      return nargs ? args[0] : Qnil;
    }

  /* If nargs is zero, this code is correct and desirable.  With
     #'multiple-value-call, we want zero-length multiple values in the
     argument list to be discarded entirely, and we can't do this if we
     transform them to nil. */
  result = make_multiple_value (nargs ? args[0] : Qnil, nargs, 
                                first_desired_multiple_value,
                                multiple_value_current_limit);

  for (; counting < nargs; ++counting)
    {
      if (counting >= first_desired_multiple_value &&
          counting < multiple_value_current_limit)
        {
          multiple_value_aset (result, counting, args[counting]);
        }
    }

  return result;
}

DEFUN ("values-list", Fvalues_list, 1, 1, 0, /*
Return all the elements of LIST as multiple values.
*/
       (list))
{
  Lisp_Object result = Qnil;
  int counting = 1, listcount; 

  GET_EXTERNAL_LIST_LENGTH (list, listcount);

  /* Pathological cases, no need to cons up an object: */
  if (1 == listcount || 1 == multiple_value_current_limit)
    {
      return Fcar_safe (list);
    }

  result = make_multiple_value (Fcar_safe (list), listcount,
                                first_desired_multiple_value,
                                multiple_value_current_limit);

  list = Fcdr_safe (list);

  {
    EXTERNAL_LIST_LOOP_2 (elt, list)
      {
        if (counting >= first_desired_multiple_value &&
            counting < multiple_value_current_limit)
          {
            multiple_value_aset (result, counting, elt);
          }
        ++counting;
      }
    }

  return result;
}

Lisp_Object
values2 (Lisp_Object first, Lisp_Object second)
{
  Lisp_Object argv[2];

  argv[0] = first;
  argv[1] = second;

  return Fvalues (countof (argv), argv);
}


/************************************************************************/
/*		     Run hook variables in various ways.		*/
/************************************************************************/

DEFUN ("run-hooks", Frun_hooks, 1, MANY, 0, /*
Run each hook in HOOKS.  Major mode functions use this.
Each argument should be a symbol, a hook variable.
These symbols are processed in the order specified.
If a hook symbol has a non-nil value, that value may be a function
or a list of functions to be called to run the hook.
If the value is a function, it is called with no arguments.
If it is a list, the elements are called, in order, with no arguments.

To make a hook variable buffer-local, use `make-local-hook',
not `make-local-variable'.

arguments: (FIRST &rest REST)
*/
       (int nargs, Lisp_Object *args))
{
  REGISTER int i;

  for (i = 0; i < nargs; i++)
    run_hook_with_args (1, args + i, RUN_HOOKS_TO_COMPLETION);

  return Qnil;
}

DEFUN ("run-hook-with-args", Frun_hook_with_args, 1, MANY, 0, /*
Run HOOK with the specified arguments ARGS.
HOOK should be a symbol, a hook variable.  If HOOK has a non-nil
value, that value may be a function or a list of functions to be
called to run the hook.  If the value is a function, it is called with
the given arguments and its return value is returned.  If it is a list
of functions, those functions are called, in order,
with the given arguments ARGS.
It is best not to depend on the value returned by `run-hook-with-args',
as that may change.

To make a hook variable buffer-local, use `make-local-hook',
not `make-local-variable'.

arguments: (HOOK &rest ARGS)
*/
       (int nargs, Lisp_Object *args))
{
  return run_hook_with_args (nargs, args, RUN_HOOKS_TO_COMPLETION);
}

DEFUN ("run-hook-with-args-until-success", Frun_hook_with_args_until_success, 1, MANY, 0, /*
Run HOOK with the specified arguments ARGS.
HOOK should be a symbol, a hook variable.  Its value should
be a list of functions.  We call those functions, one by one,
passing arguments ARGS to each of them, until one of them
returns a non-nil value.  Then we return that value.
If all the functions return nil, we return nil.

To make a hook variable buffer-local, use `make-local-hook',
not `make-local-variable'.

arguments: (HOOK &rest ARGS)
*/
       (int nargs, Lisp_Object *args))
{
  return run_hook_with_args (nargs, args, RUN_HOOKS_UNTIL_SUCCESS);
}

DEFUN ("run-hook-with-args-until-failure", Frun_hook_with_args_until_failure, 1, MANY, 0, /*
Run HOOK with the specified arguments ARGS.
HOOK should be a symbol, a hook variable.  Its value should
be a list of functions.  We call those functions, one by one,
passing arguments ARGS to each of them, until one of them
returns nil.  Then we return nil.
If all the functions return non-nil, we return non-nil.

To make a hook variable buffer-local, use `make-local-hook',
not `make-local-variable'.

arguments: (HOOK &rest ARGS)
*/
       (int nargs, Lisp_Object *args))
{
  return run_hook_with_args (nargs, args, RUN_HOOKS_UNTIL_FAILURE);
}

/* ARGS[0] should be a hook symbol.
   Call each of the functions in the hook value, passing each of them
   as arguments all the rest of ARGS (all NARGS - 1 elements).
   COND specifies a condition to test after each call
   to decide whether to stop.
   The caller (or its caller, etc) must gcpro all of ARGS,
   except that it isn't necessary to gcpro ARGS[0].  */

Lisp_Object
run_hook_with_args_in_buffer (struct buffer *buf, int nargs, Lisp_Object *args,
			      enum run_hooks_condition cond)
{
  Lisp_Object sym, val, ret;

  if (!initialized || preparing_for_armageddon)
    /* We need to bail out of here pronto. */
    return Qnil;

#ifndef NEW_GC
  /* Whenever gc_in_progress is true, preparing_for_armageddon
     will also be true unless something is really hosed. */
  assert (!gc_in_progress);
#endif /* not NEW_GC */

  sym = args[0];
  val = symbol_value_in_buffer (sym, wrap_buffer (buf));
  ret = (cond == RUN_HOOKS_UNTIL_FAILURE ? Qt : Qnil);

  if (UNBOUNDP (val) || NILP (val))
    return ret;
  else if (!CONSP (val) || EQ (XCAR (val), Qlambda))
    {
      args[0] = val;
      return Ffuncall (nargs, args);
    }
  else
    {
      struct gcpro gcpro1, gcpro2, gcpro3;
      Lisp_Object globals = Qnil;
      GCPRO3 (sym, val, globals);

      for (;
	   CONSP (val) && ((cond == RUN_HOOKS_TO_COMPLETION)
			   || (cond == RUN_HOOKS_UNTIL_SUCCESS ? NILP (ret)
			       : !NILP (ret)));
	   val = XCDR (val))
	{
	  if (EQ (XCAR (val), Qt))
	    {
	      /* t indicates this hook has a local binding;
		 it means to run the global binding too.  */
	      globals = Fdefault_value (sym);

	      if ((! CONSP (globals) || EQ (XCAR (globals), Qlambda)) &&
		  ! NILP (globals))
		{
		  args[0] = globals;
		  ret = Ffuncall (nargs, args);
		}
	      else
		{
		  for (;
		       CONSP (globals) && ((cond == RUN_HOOKS_TO_COMPLETION)
					   || (cond == RUN_HOOKS_UNTIL_SUCCESS
					       ? NILP (ret)
					       : !NILP (ret)));
		       globals = XCDR (globals))
		    {
		      args[0] = XCAR (globals);
		      /* In a global value, t should not occur.  If it does, we
			 must ignore it to avoid an endless loop.  */
		      if (!EQ (args[0], Qt))
			ret = Ffuncall (nargs, args);
		    }
		}
	    }
	  else
	    {
	      args[0] = XCAR (val);
	      ret = Ffuncall (nargs, args);
	    }
	}

      UNGCPRO;
      return ret;
    }
}

Lisp_Object
run_hook_with_args (int nargs, Lisp_Object *args,
		    enum run_hooks_condition cond)
{
  return run_hook_with_args_in_buffer (current_buffer, nargs, args, cond);
}

#if 0

/* From FSF 19.30, not currently used; seems like a big kludge. */

/* Run a hook symbol ARGS[0], but use FUNLIST instead of the actual
   present value of that symbol.
   Call each element of FUNLIST,
   passing each of them the rest of ARGS.
   The caller (or its caller, etc) must gcpro all of ARGS,
   except that it isn't necessary to gcpro ARGS[0].  */

Lisp_Object
run_hook_list_with_args (Lisp_Object funlist, int nargs, Lisp_Object *args)
{
  omitted;
}

#endif /* 0 */

void
va_run_hook_with_args (Lisp_Object hook_var, int nargs, ...)
{
  /* This function can GC */
  struct gcpro gcpro1;
  int i;
  va_list vargs;
  Lisp_Object *funcall_args = alloca_array (Lisp_Object, 1 + nargs);

  va_start (vargs, nargs);
  funcall_args[0] = hook_var;
  for (i = 0; i < nargs; i++)
    funcall_args[i + 1] = va_arg (vargs, Lisp_Object);
  va_end (vargs);

  GCPRO1 (*funcall_args);
  gcpro1.nvars = nargs + 1;
  run_hook_with_args (nargs + 1, funcall_args, RUN_HOOKS_TO_COMPLETION);
  UNGCPRO;
}

void
va_run_hook_with_args_in_buffer (struct buffer *buf, Lisp_Object hook_var,
				 int nargs, ...)
{
  /* This function can GC */
  struct gcpro gcpro1;
  int i;
  va_list vargs;
  Lisp_Object *funcall_args = alloca_array (Lisp_Object, 1 + nargs);

  va_start (vargs, nargs);
  funcall_args[0] = hook_var;
  for (i = 0; i < nargs; i++)
    funcall_args[i + 1] = va_arg (vargs, Lisp_Object);
  va_end (vargs);

  GCPRO1 (*funcall_args);
  gcpro1.nvars = nargs + 1;
  run_hook_with_args_in_buffer (buf, nargs + 1, funcall_args,
				RUN_HOOKS_TO_COMPLETION);
  UNGCPRO;
}

Lisp_Object
run_hook (Lisp_Object hook)
{
  return run_hook_with_args (1, &hook, RUN_HOOKS_TO_COMPLETION);
}


/************************************************************************/
/*		    Front-ends to eval, funcall, apply			*/
/************************************************************************/

/* Apply fn to arg */
Lisp_Object
apply1 (Lisp_Object fn, Lisp_Object arg)
{
  /* This function can GC */
  struct gcpro gcpro1;
  Lisp_Object args[2];

  if (NILP (arg))
    return Ffuncall (1, &fn);
  GCPRO1 (args[0]);
  gcpro1.nvars = 2;
  args[0] = fn;
  args[1] = arg;
  RETURN_UNGCPRO (Fapply (2, args));
}

/* Call function fn on no arguments */
Lisp_Object
call0 (Lisp_Object fn)
{
  /* This function can GC */
  struct gcpro gcpro1;

  GCPRO1 (fn);
  RETURN_UNGCPRO (Ffuncall (1, &fn));
}

/* Call function fn with argument arg0 */
Lisp_Object
call1 (Lisp_Object fn,
       Lisp_Object arg0)
{
  /* This function can GC */
  struct gcpro gcpro1;
  Lisp_Object args[2];
  args[0] = fn;
  args[1] = arg0;
  GCPRO1 (args[0]);
  gcpro1.nvars = 2;
  RETURN_UNGCPRO (Ffuncall (2, args));
}

/* Call function fn with arguments arg0, arg1 */
Lisp_Object
call2 (Lisp_Object fn,
       Lisp_Object arg0, Lisp_Object arg1)
{
  /* This function can GC */
  struct gcpro gcpro1;
  Lisp_Object args[3];
  args[0] = fn;
  args[1] = arg0;
  args[2] = arg1;
  GCPRO1 (args[0]);
  gcpro1.nvars = 3;
  RETURN_UNGCPRO (Ffuncall (3, args));
}

/* Call function fn with arguments arg0, arg1, arg2 */
Lisp_Object
call3 (Lisp_Object fn,
       Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2)
{
  /* This function can GC */
  struct gcpro gcpro1;
  Lisp_Object args[4];
  args[0] = fn;
  args[1] = arg0;
  args[2] = arg1;
  args[3] = arg2;
  GCPRO1 (args[0]);
  gcpro1.nvars = 4;
  RETURN_UNGCPRO (Ffuncall (4, args));
}

/* Call function fn with arguments arg0, arg1, arg2, arg3 */
Lisp_Object
call4 (Lisp_Object fn,
       Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
       Lisp_Object arg3)
{
  /* This function can GC */
  struct gcpro gcpro1;
  Lisp_Object args[5];
  args[0] = fn;
  args[1] = arg0;
  args[2] = arg1;
  args[3] = arg2;
  args[4] = arg3;
  GCPRO1 (args[0]);
  gcpro1.nvars = 5;
  RETURN_UNGCPRO (Ffuncall (5, args));
}

/* Call function fn with arguments arg0, arg1, arg2, arg3, arg4 */
Lisp_Object
call5 (Lisp_Object fn,
       Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
       Lisp_Object arg3, Lisp_Object arg4)
{
  /* This function can GC */
  struct gcpro gcpro1;
  Lisp_Object args[6];
  args[0] = fn;
  args[1] = arg0;
  args[2] = arg1;
  args[3] = arg2;
  args[4] = arg3;
  args[5] = arg4;
  GCPRO1 (args[0]);
  gcpro1.nvars = 6;
  RETURN_UNGCPRO (Ffuncall (6, args));
}

Lisp_Object
call6 (Lisp_Object fn,
       Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
       Lisp_Object arg3, Lisp_Object arg4, Lisp_Object arg5)
{
  /* This function can GC */
  struct gcpro gcpro1;
  Lisp_Object args[7];
  args[0] = fn;
  args[1] = arg0;
  args[2] = arg1;
  args[3] = arg2;
  args[4] = arg3;
  args[5] = arg4;
  args[6] = arg5;
  GCPRO1 (args[0]);
  gcpro1.nvars = 7;
  RETURN_UNGCPRO (Ffuncall (7, args));
}

Lisp_Object
call7 (Lisp_Object fn,
       Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
       Lisp_Object arg3, Lisp_Object arg4, Lisp_Object arg5,
       Lisp_Object arg6)
{
  /* This function can GC */
  struct gcpro gcpro1;
  Lisp_Object args[8];
  args[0] = fn;
  args[1] = arg0;
  args[2] = arg1;
  args[3] = arg2;
  args[4] = arg3;
  args[5] = arg4;
  args[6] = arg5;
  args[7] = arg6;
  GCPRO1 (args[0]);
  gcpro1.nvars = 8;
  RETURN_UNGCPRO (Ffuncall (8, args));
}

Lisp_Object
call8 (Lisp_Object fn,
       Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
       Lisp_Object arg3, Lisp_Object arg4, Lisp_Object arg5,
       Lisp_Object arg6, Lisp_Object arg7)
{
  /* This function can GC */
  struct gcpro gcpro1;
  Lisp_Object args[9];
  args[0] = fn;
  args[1] = arg0;
  args[2] = arg1;
  args[3] = arg2;
  args[4] = arg3;
  args[5] = arg4;
  args[6] = arg5;
  args[7] = arg6;
  args[8] = arg7;
  GCPRO1 (args[0]);
  gcpro1.nvars = 9;
  RETURN_UNGCPRO (Ffuncall (9, args));
}

Lisp_Object
call0_in_buffer (struct buffer *buf, Lisp_Object fn)
{
  if (current_buffer == buf)
    return call0 (fn);
  else
    {
      Lisp_Object val;
      int speccount = specpdl_depth();
      record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
      set_buffer_internal (buf);
      val = call0 (fn);
      unbind_to (speccount);
      return val;
    }
}

Lisp_Object
call1_in_buffer (struct buffer *buf, Lisp_Object fn,
		 Lisp_Object arg0)
{
  if (current_buffer == buf)
    return call1 (fn, arg0);
  else
    {
      Lisp_Object val;
      int speccount = specpdl_depth();
      record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
      set_buffer_internal (buf);
      val = call1 (fn, arg0);
      unbind_to (speccount);
      return val;
    }
}

Lisp_Object
call2_in_buffer (struct buffer *buf, Lisp_Object fn,
		 Lisp_Object arg0, Lisp_Object arg1)
{
  if (current_buffer == buf)
    return call2 (fn, arg0, arg1);
  else
    {
      Lisp_Object val;
      int speccount = specpdl_depth();
      record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
      set_buffer_internal (buf);
      val = call2 (fn, arg0, arg1);
      unbind_to (speccount);
      return val;
    }
}

Lisp_Object
call3_in_buffer (struct buffer *buf, Lisp_Object fn,
		 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2)
{
  if (current_buffer == buf)
    return call3 (fn, arg0, arg1, arg2);
  else
    {
      Lisp_Object val;
      int speccount = specpdl_depth();
      record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
      set_buffer_internal (buf);
      val = call3 (fn, arg0, arg1, arg2);
      unbind_to (speccount);
      return val;
    }
}

Lisp_Object
call4_in_buffer (struct buffer *buf, Lisp_Object fn,
		 Lisp_Object arg0, Lisp_Object arg1, Lisp_Object arg2,
		 Lisp_Object arg3)
{
  if (current_buffer == buf)
    return call4 (fn, arg0, arg1, arg2, arg3);
  else
    {
      Lisp_Object val;
      int speccount = specpdl_depth();
      record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
      set_buffer_internal (buf);
      val = call4 (fn, arg0, arg1, arg2, arg3);
      unbind_to (speccount);
      return val;
    }
}

Lisp_Object
eval_in_buffer (struct buffer *buf, Lisp_Object form)
{
  if (current_buffer == buf)
    return Feval (form);
  else
    {
      Lisp_Object val;
      int speccount = specpdl_depth();
      record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
      set_buffer_internal (buf);
      val = Feval (form);
      unbind_to (speccount);
      return val;
    }
}


/************************************************************************/
/*	   Error-catching front-ends to eval, funcall, apply		*/
/************************************************************************/

int
get_inhibit_flags (void)
{
  return inhibit_flags;
}

void
check_allowed_operation (int what, Lisp_Object obj, Lisp_Object UNUSED (prop))
{
  if (inhibit_flags & INHIBIT_EXISTING_BUFFER_TEXT_MODIFICATION)
    {
      if (what == OPERATION_MODIFY_BUFFER_TEXT && BUFFERP (obj)
	  && NILP (memq_no_quit (obj, Vmodifiable_buffers)))
	invalid_change
	  ("Modification of this buffer not currently permitted", obj);
    }
  if (inhibit_flags & INHIBIT_EXISTING_PERMANENT_DISPLAY_OBJECT_DELETION)
    {
      if (what == OPERATION_DELETE_OBJECT
	  && (BUFFERP (obj) || WINDOWP (obj) || FRAMEP (obj) || DEVICEP (obj)
	      || CONSOLEP (obj))
	  && NILP (memq_no_quit (obj, Vdeletable_permanent_display_objects)))
	invalid_change
	  ("Deletion of this object not currently permitted", obj);
    }
}

void
note_object_created (Lisp_Object obj)
{
  if (inhibit_flags & INHIBIT_EXISTING_BUFFER_TEXT_MODIFICATION)
    {
      if (BUFFERP (obj))
	Vmodifiable_buffers = Fcons (obj, Vmodifiable_buffers);
    }
  if (inhibit_flags & INHIBIT_EXISTING_PERMANENT_DISPLAY_OBJECT_DELETION)
    {
      if (BUFFERP (obj) || WINDOWP (obj) || FRAMEP (obj) || DEVICEP (obj)
	  || CONSOLEP (obj))
	Vdeletable_permanent_display_objects =
	  Fcons (obj, Vdeletable_permanent_display_objects);
    }
}

void
note_object_deleted (Lisp_Object obj)
{
  if (inhibit_flags & INHIBIT_EXISTING_BUFFER_TEXT_MODIFICATION)
    {
      if (BUFFERP (obj))
	Vmodifiable_buffers = delq_no_quit (obj, Vmodifiable_buffers);
    }
  if (inhibit_flags & INHIBIT_EXISTING_PERMANENT_DISPLAY_OBJECT_DELETION)
    {
      if (BUFFERP (obj) || WINDOWP (obj) || FRAMEP (obj) || DEVICEP (obj)
	  || CONSOLEP (obj))
	Vdeletable_permanent_display_objects =
	  delq_no_quit (obj, Vdeletable_permanent_display_objects);
    }
}

struct call_trapping_problems
{
  Lisp_Object catchtag;
  Lisp_Object error_conditions;
  Lisp_Object data;
  Lisp_Object backtrace;
  Lisp_Object warning_class;

  const CIbyte *warning_string;
  Lisp_Object (*fun) (void *);
  void *arg;
};

static Lisp_Object
maybe_get_trapping_problems_backtrace (void)
{
  Lisp_Object backtrace;

  if (!(inhibit_flags & INHIBIT_WARNING_ISSUE)
      && !warning_will_be_discarded (current_warning_level ()))
    {
      struct gcpro gcpro1;
      Lisp_Object lstream = Qnil;
      int speccount = specpdl_depth ();

      /* We're no longer protected against errors or quit here, so at
	 least let's temporarily inhibit quit.  We definitely do not
	 want to inhibit quit during the calling of the function
	 itself!!!!!!!!!!! */
     
      specbind (Qinhibit_quit, Qt);

      GCPRO1 (lstream);
      lstream = make_resizing_buffer_output_stream ();
      Fbacktrace (lstream, Qt);
      Lstream_flush (XLSTREAM (lstream));
      backtrace = resizing_buffer_to_lisp_string (XLSTREAM (lstream));
      Lstream_delete (XLSTREAM (lstream));
      UNGCPRO;

      unbind_to (speccount);
    }
  else
    backtrace = Qnil;

  return backtrace;
}

static DECLARE_DOESNT_RETURN_TYPE
  (Lisp_Object, flagged_a_squirmer (Lisp_Object, Lisp_Object, Lisp_Object));

static DOESNT_RETURN_TYPE (Lisp_Object)
flagged_a_squirmer (Lisp_Object error_conditions, Lisp_Object data,
		    Lisp_Object opaque)
{
  struct call_trapping_problems *p =
    (struct call_trapping_problems *) get_opaque_ptr (opaque);

  if (!EQ (error_conditions, Qquit))
    p->backtrace = maybe_get_trapping_problems_backtrace ();
  else
    p->backtrace = Qnil;
  p->error_conditions = error_conditions;
  p->data = data;

  throw_or_bomb_out (p->catchtag, Qnil, 0, Qnil, Qnil);
  RETURN_NOT_REACHED (Qnil);
}

static Lisp_Object
call_trapping_problems_2 (Lisp_Object opaque)
{
  struct call_trapping_problems *p =
    (struct call_trapping_problems *) get_opaque_ptr (opaque);

  return (p->fun) (p->arg);
}

static Lisp_Object
call_trapping_problems_1 (Lisp_Object opaque)
{
  return call_with_condition_handler (flagged_a_squirmer, opaque,
				      call_trapping_problems_2, opaque);
}

static void
issue_call_trapping_problems_warning (Lisp_Object warning_class,
				      const CIbyte *warning_string,
				      struct call_trapping_problems_result *p)
{
  if (!warning_will_be_discarded (current_warning_level ()))
    {
      int depth = specpdl_depth ();

      /* We're no longer protected against errors or quit here, so at
	 least let's temporarily inhibit quit. */
      specbind (Qinhibit_quit, Qt);

      if (p->caught_throw)
	{
	  Lisp_Object errstr =
	    emacs_sprintf_string_lisp
	    ("%s: Attempt to throw outside of function:"
	     "To catch `%s' with value `%s'\n\nBacktrace follows:\n\n%s",
	     Qnil, 4,
	     build_msg_cistring (warning_string ? warning_string : "error"),
	     p->thrown_tag, p->thrown_value, p->backtrace);
	  warn_when_safe_lispobj (Qerror, current_warning_level (), errstr);
	}
      else if (p->caught_error && !EQ (p->error_conditions, Qquit))
	{
	  Lisp_Object errstr;
	  /* #### This should call
	     (with-output-to-string (display-error (cons error_conditions
	                                                 data))
	     but that stuff is all in Lisp currently. */
	  errstr =
	    emacs_sprintf_string_lisp
	    ("%s: (%s %s)\n\nBacktrace follows:\n\n%s",
	     Qnil, 4,
	     build_msg_cistring (warning_string ? warning_string : "error"),
	     p->error_conditions, p->data, p->backtrace);

	  warn_when_safe_lispobj (warning_class, current_warning_level (),
				  errstr);
	}

      unbind_to (depth);
    }
}

/* Turn on the trapping flags in FLAGS -- see call_trapping_problems().
   This cannot handle INTERNAL_INHIBIT_THROWS() or INTERNAL_INHIBIT_ERRORS
   (because they ultimately boil down to a setjmp()!) -- you must directly
   use call_trapping_problems() for that.  Turn the flags off with
   unbind_to().  Returns the "canonicalized" flags (particularly in the
   case of INHIBIT_ANY_CHANGE_AFFECTING_REDISPLAY, which is shorthand for
   various other flags). */

int
set_trapping_problems_flags (int flags)
{
  int new_inhibit_flags;

  if (flags & INHIBIT_ANY_CHANGE_AFFECTING_REDISPLAY)
    flags |= INHIBIT_EXISTING_PERMANENT_DISPLAY_OBJECT_DELETION
      | INHIBIT_EXISTING_BUFFER_TEXT_MODIFICATION
      | INHIBIT_ENTERING_DEBUGGER
      | INHIBIT_WARNING_ISSUE
      | INHIBIT_GC;

  new_inhibit_flags = inhibit_flags | flags;
  if (new_inhibit_flags != inhibit_flags)
    internal_bind_int (&inhibit_flags, new_inhibit_flags);

  if (flags & INHIBIT_QUIT)
    specbind (Qinhibit_quit, Qt);

  if (flags & UNINHIBIT_QUIT)
    begin_do_check_for_quit ();

  if (flags & INHIBIT_GC)
    begin_gc_forbidden ();

  /* #### If we have nested calls to call_trapping_problems(), and the
     inner one creates some buffers/etc., should the outer one be able
     to delete them?  I think so, but it means we need to combine rather
     than just reset the value. */
  if (flags & INHIBIT_EXISTING_PERMANENT_DISPLAY_OBJECT_DELETION)
    internal_bind_lisp_object (&Vdeletable_permanent_display_objects, Qnil);

  if (flags & INHIBIT_EXISTING_BUFFER_TEXT_MODIFICATION)
    internal_bind_lisp_object (&Vmodifiable_buffers, Qnil);

  return flags;
}

/* This is equivalent to (*fun) (arg), except that various conditions
   can be trapped or inhibited, according to FLAGS.

   If FLAGS does not contain NO_INHIBIT_ERRORS, when an error occurs,
   the error is caught and a warning is issued, specifying the
   specific error that occurred and a backtrace.  In that case,
   WARNING_STRING should be given, and will be printed at the
   beginning of the error to indicate where the error occurred.

   If FLAGS does not contain NO_INHIBIT_THROWS, all attempts to
   `throw' out of the function being called are trapped, and a warning
   issued. (Again, WARNING_STRING should be given.)

   If FLAGS contains INHIBIT_WARNING_ISSUE, no warnings are issued;
   this applies to recursive invocations of call_trapping_problems, too.

   If FLAGS contains POSTPONE_WARNING_ISSUE, no warnings are issued;
   but values useful for generating a warning are still computed (in
   particular, the backtrace), so that the calling function can issue
   a warning.

   If FLAGS contains ISSUE_WARNINGS_AT_DEBUG_LEVEL, warnings will be
   issued, but at level `debug', which normally is below the minimum
   specified by `log-warning-minimum-level', meaning such warnings will
   be ignored entirely.  The user can change this variable, however,
   to see the warnings.)

   Note: If neither of NO_INHIBIT_THROWS or NO_INHIBIT_ERRORS is
   given, you are *guaranteed* that there will be no non-local exits
   out of this function.

   If FLAGS contains INHIBIT_QUIT, QUIT using C-g is inhibited.  (This
   is *rarely* a good idea.  Unless you use NO_INHIBIT_ERRORS, QUIT is
   automatically caught as well, and treated as an error; you can
   check for this using EQ (problems->error_conditions, Qquit).

   If FLAGS contains UNINHIBIT_QUIT, QUIT checking will be explicitly
   turned on. (It will abort the code being called, but will still be
   trapped and reported as an error, unless NO_INHIBIT_ERRORS is
   given.) This is useful when QUIT checking has been turned off by a
   higher-level caller.

   If FLAGS contains INHIBIT_GC, garbage collection is inhibited.
   This is useful for Lisp called within redisplay, for example.

   If FLAGS contains INHIBIT_EXISTING_PERMANENT_DISPLAY_OBJECT_DELETION,
   Lisp code is not allowed to delete any window, buffers, frames, devices,
   or consoles that were already in existence at the time this function
   was called. (However, it's perfectly legal for code to create a new
   buffer and then delete it.)

   #### It might be useful to have a flag that inhibits deletion of a
   specific permanent display object and everything it's attached to
   (e.g. a window, and the buffer, frame, device, and console it's
   attached to.

   If FLAGS contains INHIBIT_EXISTING_BUFFER_TEXT_MODIFICATION, Lisp
   code is not allowed to modify the text of any buffers that were
   already in existence at the time this function was called.
   (However, it's perfectly legal for code to create a new buffer and
   then modify its text.)

       [These last two flags are implemented using global variables
       Vdeletable_permanent_display_objects and Vmodifiable_buffers,
       which keep track of a list of all buffers or permanent display
       objects created since the last time one of these flags was set.
       The code that deletes buffers, etc. and modifies buffers checks
    
       (1) if the corresponding flag is set (through the global variable
       inhibit_flags or its accessor function get_inhibit_flags()), and
    
       (2) if the object to be modified or deleted is not in the
       appropriate list.
    
       If so, it signals an error.
    
       Recursive calls to call_trapping_problems() are allowed.  In
       the case of the two flags mentioned above, the current values
       of the global variables are stored in an unwind-protect, and
       they're reset to nil.]
    
   If FLAGS contains INHIBIT_ENTERING_DEBUGGER, the debugger will not
   be entered if an error occurs inside the Lisp code being called,
   even when the user has requested an error.  In such case, a warning
   is issued stating that access to the debugger is denied, unless
   INHIBIT_WARNING_ISSUE has also been supplied.  This is useful when
   calling Lisp code inside redisplay, in menu callbacks, etc. because
   in such cases either the display is in an inconsistent state or
   doing window operations is explicitly forbidden by the OS, and the
   debugger would causes visual changes on the screen and might create
   another frame.

   If FLAGS contains INHIBIT_ANY_CHANGE_AFFECTING_REDISPLAY, no
   changes of any sort to extents, faces, glyphs, buffer text,
   specifiers relating to display, other variables relating to
   display, splitting, deleting, or resizing windows or frames,
   deleting buffers, windows, frames, devices, or consoles, etc. is
   allowed.  This is for things called absolutely in the middle of
   redisplay, which expects things to be *exactly* the same after the
   call as before.  This isn't completely implemented and needs to be
   thought out some more to determine exactly what its semantics are.
   For the moment, turning on this flag also turns on

        INHIBIT_EXISTING_PERMANENT_DISPLAY_OBJECT_DELETION
        INHIBIT_EXISTING_BUFFER_TEXT_MODIFICATION
        INHIBIT_ENTERING_DEBUGGER
        INHIBIT_WARNING_ISSUE
        INHIBIT_GC

   #### The following five flags are defined, but unimplemented:

   #define INHIBIT_EXISTING_CODING_SYSTEM_DELETION (1<<6)
   #define INHIBIT_EXISTING_CHARSET_DELETION (1<<7)
   #define INHIBIT_PERMANENT_DISPLAY_OBJECT_CREATION (1<<8)
   #define INHIBIT_CODING_SYSTEM_CREATION (1<<9)
   #define INHIBIT_CHARSET_CREATION (1<<10)

   FLAGS containing CALL_WITH_SUSPENDED_ERRORS is a sign that
   call_with_suspended_errors() was invoked.  This exists only for
   debugging purposes -- often we want to break when a signal happens,
   but ignore signals from call_with_suspended_errors(), because they
   occur often and for legitimate reasons.

   If PROBLEM is non-zero, it should be a pointer to a structure into
   which exact information about any occurring problems (either an
   error or an attempted throw past this boundary).

   If a problem occurred and aborted operation (error, quit, or
   invalid throw), Qunbound is returned.  Otherwise the return value
   from the call to (*fun) (arg) is returned.  */

Lisp_Object
call_trapping_problems (Lisp_Object warning_class,
			const CIbyte *warning_string,
			int flags,
			struct call_trapping_problems_result *problem,
			Lisp_Object (*fun) (void *),
			void *arg)
{
  int speccount = specpdl_depth ();
  struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
  struct call_trapping_problems package;
  struct call_trapping_problems_result real_problem;
  Lisp_Object opaque, thrown_tag, tem, thrown_backtrace;
  int thrown = 0;

  assert (SYMBOLP (warning_class)); /* sanity-check */
  assert (!NILP (warning_class));

  flags ^= INTERNAL_INHIBIT_ERRORS | INTERNAL_INHIBIT_THROWS;

  package.warning_class = warning_class;
  package.warning_string = warning_string;
  package.fun = fun;
  package.arg = arg;
  package.catchtag =
    flags & INTERNAL_INHIBIT_THROWS ? Vcatch_everything_tag :
    flags & INTERNAL_INHIBIT_ERRORS ? make_opaque_ptr (0) :
    Qnil;
  package.error_conditions = Qnil;
  package.data = Qnil;
  package.backtrace = Qnil;

  flags = set_trapping_problems_flags (flags);

  if (flags & (INTERNAL_INHIBIT_THROWS | INTERNAL_INHIBIT_ERRORS))
    opaque = make_opaque_ptr (&package);
  else
    opaque = Qnil;

  GCPRO5 (package.catchtag, package.error_conditions, package.data,
	  package.backtrace, opaque);

  if (flags & INTERNAL_INHIBIT_ERRORS)
    /* We need a catch so that our condition-handler can throw back here
       after printing the warning. (We print the warning in the stack
       context of the error, so we can get a backtrace.) */
    tem = internal_catch (package.catchtag, call_trapping_problems_1, opaque,
			  &thrown, &thrown_tag, &thrown_backtrace);
  else if (flags & INTERNAL_INHIBIT_THROWS)
    /* We skip over the first wrapper, which traps errors. */
    tem = internal_catch (package.catchtag, call_trapping_problems_2, opaque,
			  &thrown, &thrown_tag, &thrown_backtrace);
  else
    /* Nothing special. */
    tem = (fun) (arg);

  if (!problem)
    problem = &real_problem;

  if (!thrown)
    {
      problem->caught_error = 0;
      problem->caught_throw = 0;
      problem->error_conditions = Qnil;
      problem->data = Qnil;
      problem->backtrace = Qnil;
      problem->thrown_tag = Qnil;
      problem->thrown_value = Qnil;
    }
  else if (EQ (thrown_tag, package.catchtag))
    {
      problem->caught_error = 1;
      problem->caught_throw = 0;
      problem->error_conditions = package.error_conditions;
      problem->data = package.data;
      problem->backtrace = package.backtrace;
      problem->thrown_tag = Qnil;
      problem->thrown_value = Qnil;
    }
  else
    {
      problem->caught_error = 0;
      problem->caught_throw = 1;
      problem->error_conditions = Qnil;
      problem->data = Qnil;
      problem->backtrace = thrown_backtrace;
      problem->thrown_tag = thrown_tag;
      problem->thrown_value = tem;
    }

  if (!(flags & INHIBIT_WARNING_ISSUE) && !(flags & POSTPONE_WARNING_ISSUE))
    issue_call_trapping_problems_warning (warning_class, warning_string,
					  problem);

  if (!NILP (package.catchtag) &&
      !EQ (package.catchtag, Vcatch_everything_tag))
    free_opaque_ptr (package.catchtag);

  if (!NILP (opaque))
    free_opaque_ptr (opaque);

  unbind_to (speccount);
  RETURN_UNGCPRO (thrown ? Qunbound : tem);
}

struct va_call_trapping_problems
{
  lisp_fn_t fun;
  int nargs;
  Lisp_Object *args;
};

static Lisp_Object
va_call_trapping_problems_1 (void *ai_mi_madre)
{
  struct va_call_trapping_problems *ai_no_corrida =
    (struct va_call_trapping_problems *) ai_mi_madre;
  Lisp_Object pegar_no_bumbum;

  PRIMITIVE_FUNCALL (pegar_no_bumbum, ai_no_corrida->fun,
		     ai_no_corrida->args, ai_no_corrida->nargs);
  return pegar_no_bumbum;
}

/* #### document me. */

Lisp_Object
va_call_trapping_problems (Lisp_Object warning_class,
			   const CIbyte *warning_string,
			   int flags,
			   struct call_trapping_problems_result *problem,
			   lisp_fn_t fun, int nargs, ...)
{
  va_list vargs;
  Lisp_Object args[20];
  int i;
  struct va_call_trapping_problems fazer_invocacao_atrapalhando_problemas;
  struct gcpro gcpro1;

  assert (nargs >= 0 && nargs < 20);

  va_start (vargs, nargs);
  for (i = 0; i < nargs; i++)
    args[i] = va_arg (vargs, Lisp_Object);
  va_end (vargs);

  fazer_invocacao_atrapalhando_problemas.fun = fun;
  fazer_invocacao_atrapalhando_problemas.nargs = nargs;
  fazer_invocacao_atrapalhando_problemas.args = args;

  GCPRO1_ARRAY (args, nargs);
  RETURN_UNGCPRO
    (call_trapping_problems
     (warning_class, warning_string, flags, problem,
      va_call_trapping_problems_1, &fazer_invocacao_atrapalhando_problemas));
}

/* this is an older interface, barely different from
   va_call_trapping_problems.

   #### eliminate this or at least merge the ERROR_BEHAVIOR stuff into
   va_call_trapping_problems(). */

Lisp_Object
call_with_suspended_errors (lisp_fn_t fun, Lisp_Object retval,
			    Lisp_Object class_, Error_Behavior errb,
			    int nargs, ...)
{
  va_list vargs;
  Lisp_Object args[20];
  int i;
  struct va_call_trapping_problems fazer_invocacao_atrapalhando_problemas;
  int flags;
  struct gcpro gcpro1;

  assert (SYMBOLP (class_)); /* sanity-check */
  assert (!NILP (class_));
  assert (nargs >= 0 && nargs < 20);

  va_start (vargs, nargs);
  for (i = 0; i < nargs; i++)
    args[i] = va_arg (vargs, Lisp_Object);
  va_end (vargs);

  /* If error-checking is not disabled, just call the function. */

  if (ERRB_EQ (errb, ERROR_ME))
    {
      Lisp_Object val;
      PRIMITIVE_FUNCALL (val, fun, args, nargs);
      return val;
    }

  if (ERRB_EQ (errb, ERROR_ME_NOT)) /* person wants no warnings */
    flags = INHIBIT_WARNING_ISSUE | INHIBIT_ENTERING_DEBUGGER;
  else if (ERRB_EQ (errb, ERROR_ME_DEBUG_WARN))
    flags = ISSUE_WARNINGS_AT_DEBUG_LEVEL | INHIBIT_ENTERING_DEBUGGER;
  else
    {
      assert (ERRB_EQ (errb, ERROR_ME_WARN));
      flags = INHIBIT_ENTERING_DEBUGGER;
    }

  flags |= CALL_WITH_SUSPENDED_ERRORS;

  fazer_invocacao_atrapalhando_problemas.fun = fun;
  fazer_invocacao_atrapalhando_problemas.nargs = nargs;
  fazer_invocacao_atrapalhando_problemas.args = args;

  GCPRO1_ARRAY (args, nargs);
  {
    Lisp_Object its_way_too_goddamn_late =
      call_trapping_problems
       (class_, 0, flags, 0, va_call_trapping_problems_1,
	&fazer_invocacao_atrapalhando_problemas);
    UNGCPRO;
    if (UNBOUNDP (its_way_too_goddamn_late))
      return retval;
    else
      return its_way_too_goddamn_late;
  }
}

struct calln_trapping_problems
{
  int nargs;
  Lisp_Object *args;
};

static Lisp_Object
calln_trapping_problems_1 (void *puta)
{
  struct calln_trapping_problems *p = (struct calln_trapping_problems *) puta;

  return Ffuncall (p->nargs, p->args);
}

static Lisp_Object
calln_trapping_problems (Lisp_Object warning_class,
			 const CIbyte *warning_string, int flags,
			 struct call_trapping_problems_result *problem,
			 int nargs, Lisp_Object *args)
{
  struct calln_trapping_problems foo;
  struct gcpro gcpro1;

  if (SYMBOLP (args[0]))
    {
      Lisp_Object tem = XSYMBOL (args[0])->function;
      if (NILP (tem) || UNBOUNDP (tem))
	{
	  if (problem)
	    {
	      problem->caught_error = 0;
	      problem->caught_throw = 0;
	      problem->error_conditions = Qnil;
	      problem->data = Qnil;
	      problem->backtrace = Qnil;
	      problem->thrown_tag = Qnil;
	      problem->thrown_value = Qnil;
	    }
	  return Qnil;
	}
    }

  foo.nargs = nargs;
  foo.args = args;

  GCPRO1_ARRAY (args, nargs);
  RETURN_UNGCPRO (call_trapping_problems (warning_class, warning_string,
					  flags, problem,
					  calln_trapping_problems_1,
					  &foo));
}

/* #### fix these functions to follow the calling convention of
   call_trapping_problems! */

Lisp_Object
call0_trapping_problems (const CIbyte *warning_string, Lisp_Object function,
			 int flags)
{
  return calln_trapping_problems (Qerror, warning_string, flags, 0, 1,
				  &function);
}

Lisp_Object
call1_trapping_problems (const CIbyte *warning_string, Lisp_Object function,
			 Lisp_Object object, int flags)
{
  Lisp_Object args[2];

  args[0] = function;
  args[1] = object;

  return calln_trapping_problems (Qerror, warning_string, flags, 0, 2,
				  args);
}

Lisp_Object
call2_trapping_problems (const CIbyte *warning_string, Lisp_Object function,
			 Lisp_Object object1, Lisp_Object object2,
			 int flags)
{
  Lisp_Object args[3];

  args[0] = function;
  args[1] = object1;
  args[2] = object2;

  return calln_trapping_problems (Qerror, warning_string, flags, 0, 3,
				  args);
}

Lisp_Object
call3_trapping_problems (const CIbyte *warning_string, Lisp_Object function,
			 Lisp_Object object1, Lisp_Object object2,
			 Lisp_Object object3, int flags)
{
  Lisp_Object args[4];

  args[0] = function;
  args[1] = object1;
  args[2] = object2;
  args[3] = object3;

  return calln_trapping_problems (Qerror, warning_string, flags, 0, 4,
				  args);
}

Lisp_Object
call4_trapping_problems (const CIbyte *warning_string, Lisp_Object function,
			 Lisp_Object object1, Lisp_Object object2,
			 Lisp_Object object3, Lisp_Object object4,
			 int flags)
{
  Lisp_Object args[5];

  args[0] = function;
  args[1] = object1;
  args[2] = object2;
  args[3] = object3;
  args[4] = object4;

  return calln_trapping_problems (Qerror, warning_string, flags, 0, 5,
				  args);
}

Lisp_Object
call5_trapping_problems (const CIbyte *warning_string, Lisp_Object function,
			 Lisp_Object object1, Lisp_Object object2,
			 Lisp_Object object3, Lisp_Object object4,
			 Lisp_Object object5, int flags)
{
  Lisp_Object args[6];

  args[0] = function;
  args[1] = object1;
  args[2] = object2;
  args[3] = object3;
  args[4] = object4;
  args[5] = object5;

  return calln_trapping_problems (Qerror, warning_string, flags, 0, 6,
				  args);
}

struct eval_in_buffer_trapping_problems
{
  struct buffer *buf;
  Lisp_Object form;
};

static Lisp_Object
eval_in_buffer_trapping_problems_1 (void *arg)
{
  struct eval_in_buffer_trapping_problems *p =
    (struct eval_in_buffer_trapping_problems *) arg;

  return eval_in_buffer (p->buf, p->form);
}

/* #### fix these functions to follow the calling convention of
   call_trapping_problems! */

Lisp_Object
eval_in_buffer_trapping_problems (const CIbyte *warning_string,
				  struct buffer *buf, Lisp_Object form,
				  int flags)
{
  struct eval_in_buffer_trapping_problems p;
  Lisp_Object buffer = wrap_buffer (buf);
  struct gcpro gcpro1, gcpro2;

  GCPRO2 (buffer, form);
  p.buf = buf;
  p.form = form;
  RETURN_UNGCPRO (call_trapping_problems (Qerror, warning_string, flags, 0,
					  eval_in_buffer_trapping_problems_1,
					  &p));
}

Lisp_Object
run_hook_trapping_problems (Lisp_Object warning_class,
			    Lisp_Object hook_symbol,
			    int flags)
{
  return run_hook_with_args_trapping_problems (warning_class, 1, &hook_symbol,
					       RUN_HOOKS_TO_COMPLETION,
					       flags);
}

static Lisp_Object
safe_run_hook_trapping_problems_1 (void *puta)
{
  Lisp_Object hook = GET_LISP_FROM_VOID (puta);

  run_hook (hook);
  return Qnil;
}

/* Same as run_hook_trapping_problems() but also set the hook to nil
   if an error occurs (but not a quit). */

Lisp_Object
safe_run_hook_trapping_problems (Lisp_Object warning_class,
				 Lisp_Object hook_symbol, int flags)
{
  Lisp_Object tem;
  struct gcpro gcpro1, gcpro2;
  struct call_trapping_problems_result prob;

  if (!initialized || preparing_for_armageddon)
    return Qnil;
  tem = find_symbol_value (hook_symbol);
  if (NILP (tem) || UNBOUNDP (tem))
    return Qnil;

  GCPRO2 (hook_symbol, tem);
  tem = call_trapping_problems (Qerror, NULL,
				flags | POSTPONE_WARNING_ISSUE,
				&prob,
				safe_run_hook_trapping_problems_1,
				STORE_LISP_IN_VOID (hook_symbol));
  {
    Lisp_Object hook_name = XSYMBOL_NAME (hook_symbol);
    Ibyte *hook_str = XSTRING_DATA (hook_name);
    Ibyte *err = alloca_ibytes (XSTRING_LENGTH (hook_name) + 100);

    if (prob.caught_throw || (prob.caught_error && !EQ (prob.error_conditions,
							Qquit)))
      {
	Fset (hook_symbol, Qnil);
	qxesprintf (err, "Error in `%s' (resetting to nil)", hook_str);
      }
    else
      qxesprintf (err, "Quit in `%s'", hook_str);

  
    issue_call_trapping_problems_warning (warning_class, (CIbyte *) err,
					  &prob);
  }

  UNGCPRO;
  return tem;
}

struct run_hook_with_args_in_buffer_trapping_problems
{
  struct buffer *buf;
  int nargs;
  Lisp_Object *args;
  enum run_hooks_condition cond;
};

static Lisp_Object
run_hook_with_args_in_buffer_trapping_problems_1 (void *puta)
{
  struct run_hook_with_args_in_buffer_trapping_problems *porra =
    (struct run_hook_with_args_in_buffer_trapping_problems *) puta;

  return run_hook_with_args_in_buffer (porra->buf, porra->nargs, porra->args,
				       porra->cond);
}

/* #### fix these functions to follow the calling convention of
   call_trapping_problems! */

Lisp_Object
run_hook_with_args_in_buffer_trapping_problems (Lisp_Object warning_class,
						struct buffer *buf, int nargs,
						Lisp_Object *args,
						enum run_hooks_condition cond,
						int flags)
{
  Lisp_Object sym, val, ret;
  struct run_hook_with_args_in_buffer_trapping_problems diversity_and_distrust;
  struct gcpro gcpro1;
  Lisp_Object hook_name;
  Ibyte *hook_str;
  Ibyte *err;

  if (!initialized || preparing_for_armageddon)
    /* We need to bail out of here pronto. */
    return Qnil;

  GCPRO1_ARRAY (args, nargs);

  sym = args[0];
  val = symbol_value_in_buffer (sym, wrap_buffer (buf));
  ret = (cond == RUN_HOOKS_UNTIL_FAILURE ? Qt : Qnil);

  if (UNBOUNDP (val) || NILP (val))
    RETURN_UNGCPRO (ret);

  diversity_and_distrust.buf = buf;
  diversity_and_distrust.nargs = nargs;
  diversity_and_distrust.args = args;
  diversity_and_distrust.cond = cond;

  hook_name = XSYMBOL_NAME (args[0]);
  hook_str = XSTRING_DATA (hook_name);
  err = alloca_ibytes (XSTRING_LENGTH (hook_name) + 100);
  qxesprintf (err, "Error in `%s'", hook_str);
  RETURN_UNGCPRO
    (call_trapping_problems
     (warning_class, (CIbyte *) err, flags, 0,
      run_hook_with_args_in_buffer_trapping_problems_1,
      &diversity_and_distrust));
}

Lisp_Object
run_hook_with_args_trapping_problems (Lisp_Object warning_class,
				      int nargs,
				      Lisp_Object *args,
				      enum run_hooks_condition cond,
				      int flags)
{
  return run_hook_with_args_in_buffer_trapping_problems
    (warning_class, current_buffer, nargs, args, cond, flags);
}

Lisp_Object
va_run_hook_with_args_trapping_problems (Lisp_Object warning_class,
					 Lisp_Object hook_var,
					 int nargs, ...)
{
  /* This function can GC */
  struct gcpro gcpro1;
  int i;
  va_list vargs;
  Lisp_Object *funcall_args = alloca_array (Lisp_Object, 1 + nargs);
  int flags;

  va_start (vargs, nargs);
  funcall_args[0] = hook_var;
  for (i = 0; i < nargs; i++)
    funcall_args[i + 1] = va_arg (vargs, Lisp_Object);
  flags = va_arg (vargs, int);
  va_end (vargs);

  GCPRO1_ARRAY (funcall_args, nargs + 1);
  RETURN_UNGCPRO (run_hook_with_args_in_buffer_trapping_problems
		  (warning_class, current_buffer, nargs + 1, funcall_args,
		   RUN_HOOKS_TO_COMPLETION, flags));
}

Lisp_Object
va_run_hook_with_args_in_buffer_trapping_problems (Lisp_Object warning_class,
						   struct buffer *buf,
						   Lisp_Object hook_var,
						   int nargs, ...)
{
  /* This function can GC */
  struct gcpro gcpro1;
  int i;
  va_list vargs;
  Lisp_Object *funcall_args = alloca_array (Lisp_Object, 1 + nargs);
  int flags;

  va_start (vargs, nargs);
  funcall_args[0] = hook_var;
  for (i = 0; i < nargs; i++)
    funcall_args[i + 1] = va_arg (vargs, Lisp_Object);
  flags = va_arg (vargs, int);
  va_end (vargs);

  GCPRO1_ARRAY (funcall_args, nargs + 1);
  RETURN_UNGCPRO (run_hook_with_args_in_buffer_trapping_problems
		  (warning_class, buf, nargs + 1, funcall_args,
		   RUN_HOOKS_TO_COMPLETION, flags));
}


/************************************************************************/
/*		       The special binding stack			*/
/* Most C code should simply use specbind() and unbind_to_1().		*/
/* When performance is critical, use the macros in backtrace.h.		*/
/************************************************************************/

#define min_max_specpdl_size 400

void
grow_specpdl (EMACS_INT reserved)
{
  EMACS_INT size_needed = specpdl_depth() + reserved;
  if (size_needed >= max_specpdl_size)
    {
      if (max_specpdl_size < min_max_specpdl_size)
	max_specpdl_size = min_max_specpdl_size;
      if (size_needed >= max_specpdl_size)
	{
	  /* Leave room for some specpdl in the debugger. */
	  max_specpdl_size = size_needed + 100;
	  if (max_specpdl_size > specpdl_size)
	    {
	      specpdl_size = max_specpdl_size;
	      XREALLOC_ARRAY (specpdl, struct specbinding, specpdl_size);
	      specpdl_ptr = specpdl + specpdl_depth();
	    }
	  signal_continuable_error
	    (Qstack_overflow,
	     "Variable binding depth exceeds max-specpdl-size", Qunbound);
	}
    }
  while (specpdl_size < size_needed)
    {
      specpdl_size *= 2;
      if (specpdl_size > max_specpdl_size)
	specpdl_size = max_specpdl_size;
    }
  XREALLOC_ARRAY (specpdl, struct specbinding, specpdl_size);
  specpdl_ptr = specpdl + specpdl_depth();
  check_specbind_stack_sanity ();
}


/* Handle unbinding buffer-local variables */
static Lisp_Object
specbind_unwind_local (Lisp_Object ovalue)
{
  Lisp_Object current = Fcurrent_buffer ();
  Lisp_Object symbol = specpdl_ptr->symbol;
  Lisp_Object victim = ovalue;
  Lisp_Object buf = get_buffer (XCAR (victim), 0);
  ovalue = XCDR (victim);

  free_cons (victim);

  if (NILP (buf))
    {
      /* Deleted buffer -- do nothing */
    }
  else if (symbol_value_buffer_local_info (symbol, XBUFFER (buf)) == 0)
    {
      /* Was buffer-local when binding was made, now no longer is.
       *  (kill-local-variable can do this.)
       * Do nothing in this case.
       */
    }
  else if (EQ (buf, current))
    Fset (symbol, ovalue);
  else
  {
    /* Urk! Somebody switched buffers */
    struct gcpro gcpro1;
    GCPRO1 (current);
    Fset_buffer (buf);
    Fset (symbol, ovalue);
    Fset_buffer (current);
    UNGCPRO;
  }
  return symbol;
}

static Lisp_Object
specbind_unwind_wasnt_local (Lisp_Object buffer)
{
  Lisp_Object current = Fcurrent_buffer ();
  Lisp_Object symbol = specpdl_ptr->symbol;

  buffer = get_buffer (buffer, 0);
  if (NILP (buffer))
    {
      /* Deleted buffer -- do nothing */
    }
  else if (symbol_value_buffer_local_info (symbol, XBUFFER (buffer)) == 0)
    {
      /* Was buffer-local when binding was made, now no longer is.
       *  (kill-local-variable can do this.)
       * Do nothing in this case.
       */
    }
  else if (EQ (buffer, current))
    Fkill_local_variable (symbol);
  else
    {
      /* Urk! Somebody switched buffers */
      struct gcpro gcpro1;
      GCPRO1 (current);
      Fset_buffer (buffer);
      Fkill_local_variable (symbol);
      Fset_buffer (current);
      UNGCPRO;
    }
  return symbol;
}


void
specbind (Lisp_Object symbol, Lisp_Object value)
{
  SPECBIND (symbol, value);

  check_specbind_stack_sanity ();
}

void
specbind_magic (Lisp_Object symbol, Lisp_Object value)
{
  int buffer_local =
    symbol_value_buffer_local_info (symbol, current_buffer);

  if (buffer_local == 0)
    {
      specpdl_ptr->old_value = find_symbol_value (symbol);
      specpdl_ptr->func = 0;      /* Handled specially by unbind_to_1 */
    }
  else if (buffer_local > 0)
    {
      /* Already buffer-local */
      specpdl_ptr->old_value = noseeum_cons (Fcurrent_buffer (),
					     find_symbol_value (symbol));
      specpdl_ptr->func = specbind_unwind_local;
    }
  else
    {
      /* About to become buffer-local */
      specpdl_ptr->old_value = Fcurrent_buffer ();
      specpdl_ptr->func = specbind_unwind_wasnt_local;
    }

  specpdl_ptr->symbol = symbol;
  specpdl_ptr++;
  specpdl_depth_counter++;

  Fset (symbol, value);

  check_specbind_stack_sanity ();
}

/* Record an unwind-protect -- FUNCTION will be called with ARG no matter
   whether a normal or non-local exit occurs. (You need to call unbind_to_1()
   before your function returns normally, passing in the integer returned
   by this function.) Note: As long as the unwind-protect exists, ARG is
   automatically GCPRO'd.  The return value from FUNCTION is completely
   ignored. #### We should eliminate it entirely. */

int
record_unwind_protect (Lisp_Object (*function) (Lisp_Object arg),
                       Lisp_Object arg)
{
  SPECPDL_RESERVE (1);
  specpdl_ptr->func = function;
  specpdl_ptr->symbol = Qnil;
  specpdl_ptr->old_value = arg;
  specpdl_ptr++;
  specpdl_depth_counter++;
  check_specbind_stack_sanity ();
  return specpdl_depth_counter - 1;
}

static Lisp_Object
restore_lisp_object (Lisp_Object cons)
{
  Lisp_Object laddr = XCAR (cons);
  Lisp_Object *addr = (Lisp_Object *) GET_VOID_FROM_LISP (laddr);
  *addr = XCDR (cons);
  free_cons (cons);
  return Qnil;
}

/* Establish an unwind-protect which will restore the Lisp_Object pointed to
   by ADDR with the value VAL. */
static int
record_unwind_protect_restoring_lisp_object (Lisp_Object *addr,
					     Lisp_Object val)
{
  /* We use a cons rather than a malloc()ed structure because we want the
     Lisp object to have garbage-collection protection */
  Lisp_Object laddr = STORE_VOID_IN_LISP (addr);
  return record_unwind_protect (restore_lisp_object,
				noseeum_cons (laddr, val));
}

/* Similar to specbind() but for any C variable whose value is a
   Lisp_Object.  Sets up an unwind-protect to restore the variable
   pointed to by ADDR to its existing value, and then changes its
   value to NEWVAL.  Returns the previous value of specpdl_depth();
   pass this to unbind_to() after you are done. */
int
internal_bind_lisp_object (Lisp_Object *addr, Lisp_Object newval)
{
  int count = specpdl_depth ();
  record_unwind_protect_restoring_lisp_object (addr, *addr);
  *addr = newval;
  return count;
}

struct restore_int
{
  int *addr;
  int val;
};

static Lisp_Object
restore_int (Lisp_Object obj)
{
  struct restore_int *ri = (struct restore_int *) GET_VOID_FROM_LISP (obj);
  *(ri->addr) = ri->val;
  xfree (ri);
  return Qnil;
}

/* Establish an unwind-protect which will restore the int pointed to
   by ADDR with the value VAL.  This function works correctly with
   all ints, even those that don't fit into a Lisp integer. */
int
record_unwind_protect_restoring_int (int *addr, int val)
{
  struct restore_int *ri = xnew (struct restore_int);
  ri->addr = addr;
  ri->val = val;
  return record_unwind_protect (restore_int, STORE_VOID_IN_LISP (ri));
}

/* Similar to specbind() but for any C variable whose value is an int.
   Sets up an unwind-protect to restore the variable pointed to by
   ADDR to its existing value, and then changes its value to NEWVAL.
   Returns the previous value of specpdl_depth(); pass this to
   unbind_to() after you are done.  This function works correctly with
   all ints, even those that don't fit into a Lisp integer. */
int
internal_bind_int (int *addr, int newval)
{
  int count = specpdl_depth ();
  record_unwind_protect_restoring_int (addr, *addr);
  *addr = newval;
  return count;
}

static Lisp_Object
free_pointer (Lisp_Object opaque)
{
  void *ptr = GET_VOID_FROM_LISP (opaque);
  xfree (ptr);
  return Qnil;
}

/* Establish an unwind-protect which will free the specified block.
 */
int
record_unwind_protect_freeing (void *ptr)
{
  return record_unwind_protect (free_pointer, STORE_VOID_IN_LISP (ptr));
}

static Lisp_Object
free_dynarr (Lisp_Object opaque)
{
  Dynarr_free (GET_VOID_FROM_LISP (opaque));
  return Qnil;
}

int
record_unwind_protect_freeing_dynarr (void *ptr)
{
  return record_unwind_protect (free_dynarr, STORE_VOID_IN_LISP (ptr));
}

/* Unwind the stack till specpdl_depth() == COUNT.
   VALUE is not used, except that, purely as a convenience to the
   caller, it is protected from garbage-protection and returned. */
Lisp_Object
unbind_to_1 (int count, Lisp_Object value)
{
  UNBIND_TO_GCPRO (count, value);
  check_specbind_stack_sanity ();
  return value;
}

/* Don't call this directly.
   Only for use by UNBIND_TO* macros in backtrace.h */
void
unbind_to_hairy (int count)
{
  ++specpdl_ptr;
  ++specpdl_depth_counter;

  while (specpdl_depth_counter != count)
    {
      Lisp_Object oquit = Qunbound;

      /* Do this check BEFORE decrementing the values below, because once
	 they're decremented, GC protection is lost on
	 specpdl_ptr->old_value. */
      if (specpdl_ptr[-1].func == Fprogn)
	{
	  /* Allow QUIT within unwind-protect routines, but defer any
	     existing QUIT until afterwards.  Only do this, however, for
	     unwind-protects established by Lisp code, not by C code
	     (e.g. free_opaque_ptr() or something), because the act of
	     checking for QUIT can cause all sorts of weird things to
	     happen, since it churns the event loop -- redisplay, running
	     Lisp, etc.  Code should not have to worry about this just
	     because of establishing an unwind-protect. */
	  check_quit (); /* make Vquit_flag accurate */
	  oquit = Vquit_flag;
	  Vquit_flag = Qnil;
	}

      --specpdl_ptr;
      --specpdl_depth_counter;

      /* #### At this point, there is no GC protection on old_value.  This
	 could be a real problem, depending on what unwind-protect function
	 is called.  It looks like it just so happens that the ones
	 actually called don't have a problem with this, e.g. Fprogn.  But
	 we should look into fixing this. (Many unwind-protect functions
	 free values.  Is it a problem if freed values are
	 GC-protected?) */
      if (specpdl_ptr->func != 0)
	{
	  /* An unwind-protect */
	  (*specpdl_ptr->func) (specpdl_ptr->old_value);
	}
	  
      else
	{
	  /* We checked symbol for validity when we specbound it,
	     so only need to call Fset if symbol has magic value.  */
	  Lisp_Symbol *sym = XSYMBOL (specpdl_ptr->symbol);
	  if (!SYMBOL_VALUE_MAGIC_P (sym->value))
	    sym->value = specpdl_ptr->old_value;
	  else
	    Fset (specpdl_ptr->symbol, specpdl_ptr->old_value);
	}

#if 0 /* martin */
#ifndef EXCEEDINGLY_QUESTIONABLE_CODE
      /* There should never be anything here for us to remove.
	 If so, it indicates a logic error in Emacs.  Catches
	 should get removed when a throw or signal occurs, or
	 when a catch or condition-case exits normally.  But
	 it's too dangerous to just remove this code. --ben */

      /* Furthermore, this code is not in FSFmacs!!!
	 Braino on mly's part? */
      /* If we're unwound past the pdlcount of a catch frame,
         that catch can't possibly still be valid. */
      while (catchlist && catchlist->pdlcount > specpdl_depth_counter)
        {
          catchlist = catchlist->next;
          /* Don't mess with gcprolist, backtrace_list here */
        }
#endif
#endif

      if (!UNBOUNDP (oquit))
	Vquit_flag = oquit;
    }
  check_specbind_stack_sanity ();
}



/* Get the value of symbol's global binding, even if that binding is
   not now dynamically visible.  May return Qunbound or magic values. */

Lisp_Object
top_level_value (Lisp_Object symbol)
{
  REGISTER struct specbinding *ptr = specpdl;

  CHECK_SYMBOL (symbol);
  for (; ptr != specpdl_ptr; ptr++)
    {
      if (EQ (ptr->symbol, symbol))
	return ptr->old_value;
    }
  return XSYMBOL (symbol)->value;
}

#if 0

Lisp_Object
top_level_set (Lisp_Object symbol, Lisp_Object newval)
{
  REGISTER struct specbinding *ptr = specpdl;

  CHECK_SYMBOL (symbol);
  for (; ptr != specpdl_ptr; ptr++)
    {
      if (EQ (ptr->symbol, symbol))
	{
	  ptr->old_value = newval;
	  return newval;
	}
    }
  return Fset (symbol, newval);
}

#endif /* 0 */


/************************************************************************/
/*			      Backtraces				*/
/************************************************************************/

DEFUN ("backtrace-debug", Fbacktrace_debug, 2, 2, 0, /*
Set the debug-on-exit flag of eval frame LEVEL levels down to FLAG.
The debugger is entered when that frame exits, if the flag is non-nil.
*/
       (level, flag))
{
  REGISTER struct backtrace *backlist = backtrace_list;
  REGISTER int i;

  CHECK_INT (level);

  for (i = 0; backlist && i < XINT (level); i++)
    {
      backlist = backlist->next;
    }

  if (backlist)
    backlist->debug_on_exit = !NILP (flag);

  return flag;
}

static void
backtrace_specials (int speccount, int speclimit, Lisp_Object stream)
{
  int printing_bindings = 0;

  for (; speccount > speclimit; speccount--)
    {
      if (specpdl[speccount - 1].func == 0
          || specpdl[speccount - 1].func == specbind_unwind_local
          || specpdl[speccount - 1].func == specbind_unwind_wasnt_local)
	{
	  write_ascstring (stream, !printing_bindings ? "  # bind (" : " ");
	  Fprin1 (specpdl[speccount - 1].symbol, stream);
	  printing_bindings = 1;
	}
      else
	{
	  if (printing_bindings) write_ascstring (stream, ")\n");
	  write_ascstring (stream, "  # (unwind-protect ...)\n");
	  printing_bindings = 0;
	}
    }
  if (printing_bindings) write_ascstring (stream, ")\n");
}

static Lisp_Object
backtrace_unevalled_args (Lisp_Object *args)
{
  if (args)
    return *args;
  else
    return list1 (build_ascstring ("[internal]"));
}

DEFUN ("backtrace", Fbacktrace, 0, 2, "", /*
Print a trace of Lisp function calls currently active.
Optional arg STREAM specifies the output stream to send the backtrace to,
and defaults to the value of `standard-output'.
Optional second arg DETAILED non-nil means show places where currently
active variable bindings, catches, condition-cases, and
unwind-protects, as well as function calls, were made.
*/
       (stream, detailed))
{
  /* This function can GC */
  struct backtrace *backlist = backtrace_list;
  struct catchtag *catches = catchlist;
  int speccount = specpdl_depth();

  int old_nl = print_escape_newlines;
  int old_pr = print_readably;
  Lisp_Object old_level = Vprint_level;
  Lisp_Object oiq = Vinhibit_quit;
  struct gcpro gcpro1, gcpro2;

  /* We can't allow quits in here because that could cause the values
     of print_readably and print_escape_newlines to get screwed up.
     Normally we would use a record_unwind_protect but that would
     screw up the functioning of this function. */
  Vinhibit_quit = Qt;

  entering_debugger = 0;

  if (!NILP (detailed))
    Vprint_level = make_int (50);
  else
    Vprint_level = make_int (3);
  print_readably = 0;
  print_escape_newlines = 1;

  GCPRO2 (stream, old_level);

  stream = canonicalize_printcharfun (stream);

  for (;;)
    {
      if (!NILP (detailed) && catches && catches->backlist == backlist)
	{
          int catchpdl = catches->pdlcount;
          if (speccount > catchpdl
	      && specpdl[catchpdl].func == condition_case_unwind)
            /* This is a condition-case catchpoint */
            catchpdl = catchpdl + 1;

          backtrace_specials (speccount, catchpdl, stream);

          speccount = catches->pdlcount;
          if (catchpdl == speccount)
	    {
	      write_ascstring (stream, "  # (catch ");
	      Fprin1 (catches->tag, stream);
	      write_ascstring (stream, " ...)\n");
	    }
          else
            {
              write_ascstring (stream, "  # (condition-case ... . ");
              Fprin1 (Fcdr (Fcar (catches->tag)), stream);
              write_ascstring (stream, ")\n");
            }
          catches = catches->next;
	}
      else if (!backlist)
	break;
      else
	{
	  if (!NILP (detailed) && backlist->pdlcount < speccount)
	    {
	      backtrace_specials (speccount, backlist->pdlcount, stream);
	      speccount = backlist->pdlcount;
	    }
	  write_ascstring (stream, backlist->debug_on_exit ? "* " : "  ");
	  if (backlist->nargs == UNEVALLED)
	    {
	      Fprin1 (Fcons (*backlist->function,
			     backtrace_unevalled_args (backlist->args)),
		      stream);
	      write_ascstring (stream, "\n"); /* from FSFmacs 19.30 */
	    }
	  else
	    {
	      Lisp_Object tem = *backlist->function;
	      Fprin1 (tem, stream); /* This can QUIT */
	      write_ascstring (stream, "(");
	      if (backlist->nargs == MANY)
		{
		  int i;
		  Lisp_Object tail = Qnil;
		  struct gcpro ngcpro1;

		  NGCPRO1 (tail);
		  for (tail = *backlist->args, i = 0;
		       !NILP (tail);
		       tail = Fcdr (tail), i++)
		    {
		      if (i != 0) write_ascstring (stream, " ");
		      Fprin1 (Fcar (tail), stream);
		    }
		  NUNGCPRO;
		}
	      else
		{
		  int i;
		  for (i = 0; i < backlist->nargs; i++)
		    {
		      if (!i && EQ (tem, Qbyte_code))
			{
			  write_ascstring (stream, "\"...\"");
			  continue;
			}
		      if (i != 0) write_ascstring (stream, " ");
		      Fprin1 (backlist->args[i], stream);
		    }
		}
	      write_ascstring (stream, ")\n");
	    }
	  backlist = backlist->next;
	}
    }
  Vprint_level = old_level;
  print_readably = old_pr;
  print_escape_newlines = old_nl;
  UNGCPRO;
  Vinhibit_quit = oiq;
  return Qnil;
}


DEFUN ("backtrace-frame", Fbacktrace_frame, 1, 1, 0, /*
Return the function and arguments NFRAMES up from current execution point.
If that frame has not evaluated the arguments yet (or involves a special
operator), the value is (nil FUNCTION ARG-FORMS...).
If that frame has evaluated its arguments and called its function already,
the value is (t FUNCTION ARG-VALUES...).
A &rest arg is represented as the tail of the list ARG-VALUES.
FUNCTION is whatever was supplied as car of evaluated list,
or a lambda expression for macro calls.
If NFRAMES is more than the number of frames, the value is nil.
*/
       (nframes))
{
  REGISTER struct backtrace *backlist = backtrace_list;
  REGISTER int i;
  Lisp_Object tem;

  check_integer_range (nframes, Qzero, make_integer (EMACS_INT_MAX));

  /* Find the frame requested.  */
  for (i = XINT (nframes); backlist && (i-- > 0);)
    backlist = backlist->next;

  if (!backlist)
    return Qnil;
  if (backlist->nargs == UNEVALLED)
    return Fcons (Qnil, Fcons (*backlist->function,
			       backtrace_unevalled_args (backlist->args)));
  else
    {
      if (backlist->nargs == MANY)
	tem = *backlist->args;
      else
	tem = Flist (backlist->nargs, backlist->args);

      return Fcons (Qt, Fcons (*backlist->function, tem));
    }
}


/************************************************************************/
/*			      Warnings					*/
/************************************************************************/

static int
warning_will_be_discarded (Lisp_Object level)
{
  /* Don't even generate debug warnings if they're going to be discarded,
     to avoid excessive consing. */
  return (EQ (level, Qdebug) && !NILP (Vlog_warning_minimum_level) &&
	  !EQ (Vlog_warning_minimum_level, Qdebug));
}

void
warn_when_safe_lispobj (Lisp_Object class_, Lisp_Object level,
			Lisp_Object obj)
{
  if (warning_will_be_discarded (level))
    return;

  obj = list1 (list3 (class_, level, obj));
  if (NILP (Vpending_warnings))
    Vpending_warnings = Vpending_warnings_tail = obj;
  else
    {
      Fsetcdr (Vpending_warnings_tail, obj);
      Vpending_warnings_tail = obj;
    }
}

/* #### This should probably accept Lisp objects; but then we have
   to make sure that Feval() isn't called, since it might not be safe.

   An alternative approach is to just pass some non-string type of
   Lisp_Object to warn_when_safe_lispobj(); `prin1-to-string' will
   automatically be called when it is safe to do so. */

void
warn_when_safe (Lisp_Object class_, Lisp_Object level, const Ascbyte *fmt, ...)
{
  Lisp_Object obj;
  va_list args;

  if (warning_will_be_discarded (level))
    return;

  va_start (args, fmt);
  obj = emacs_vsprintf_string (GETTEXT (fmt), args);
  va_end (args);

  warn_when_safe_lispobj (class_, level, obj);
}




/************************************************************************/
/*			    Initialization				*/
/************************************************************************/

void
syms_of_eval (void)
{
  INIT_LISP_OBJECT (subr);
  INIT_LISP_OBJECT (multiple_value);

  DEFSYMBOL (Qinhibit_quit);
  DEFSYMBOL (Qautoload);
  DEFSYMBOL (Qdebug_on_error);
  DEFSYMBOL (Qstack_trace_on_error);
  DEFSYMBOL (Qdebug_on_signal);
  DEFSYMBOL (Qstack_trace_on_signal);
  DEFSYMBOL (Qdebugger);
  DEFSYMBOL (Qmacro);
  defsymbol (&Qand_rest, "&rest");
  defsymbol (&Qand_optional, "&optional");
  /* Note that the process code also uses Qexit */
  DEFSYMBOL (Qexit);
  DEFSYMBOL (Qsetq);
  DEFSYMBOL (Qinteractive);
  DEFSYMBOL (Qcommandp);
  DEFSYMBOL (Qdefun);
  DEFSYMBOL (Qprogn);
  DEFSYMBOL (Qvalues);
  DEFSYMBOL (Qdisplay_warning);
  DEFSYMBOL (Qrun_hooks);
  DEFSYMBOL (Qfinalize_list);
  DEFSYMBOL (Qif);
  DEFSYMBOL (Qthrow);
  DEFSYMBOL (Qobsolete_throw);  
  DEFSYMBOL (Qmultiple_value_list_internal);

  DEFSUBR (For);
  DEFSUBR (Fand);
  DEFSUBR (Fif);
  DEFSUBR_MACRO (Fwhen);
  DEFSUBR_MACRO (Funless);
  DEFSUBR (Fcond);
  DEFSUBR (Fprogn);
  DEFSUBR (Fprog1);
  DEFSUBR (Fprog2);
  DEFSUBR (Fsetq);
  DEFSUBR (Fquote);
  DEFSUBR (Fquote_maybe);
  DEFSUBR (Ffunction);
  DEFSUBR (Fdefun);
  DEFSUBR (Fdefmacro);
  DEFSUBR (Fdefvar);
  DEFSUBR (Fdefconst);
  DEFSUBR (Flet);
  DEFSUBR (FletX);
  DEFSUBR (Fwhile);
  DEFSUBR (Fmacroexpand_internal);
  DEFSUBR (Fcatch);
  DEFSUBR (Fthrow);
  DEFSUBR (Funwind_protect);
  DEFSUBR (Fcondition_case);
  DEFSUBR (Fcall_with_condition_handler);
  DEFSUBR (Fsignal);
  DEFSUBR (Finteractive_p);
  DEFSUBR (Fcommandp);
  DEFSUBR (Fcommand_execute);
  DEFSUBR (Fautoload);
  DEFSUBR (Feval);
  DEFSUBR (Fapply);
  DEFSUBR (Fmultiple_value_call);
  DEFSUBR (Fmultiple_value_list_internal);
  DEFSUBR (Fmultiple_value_prog1);
  DEFSUBR (Fvalues);
  DEFSUBR (Fvalues_list);
  DEFSUBR (Ffuncall);
  DEFSUBR (Ffunctionp);
  DEFSUBR (Ffunction_min_args);
  DEFSUBR (Ffunction_max_args);
  DEFSUBR (Frun_hooks);
  DEFSUBR (Frun_hook_with_args);
  DEFSUBR (Frun_hook_with_args_until_success);
  DEFSUBR (Frun_hook_with_args_until_failure);
  DEFSUBR (Fbacktrace_debug);
  DEFSUBR (Fbacktrace);
  DEFSUBR (Fbacktrace_frame);
}

void
init_eval_semi_early (void)
{
  specpdl_ptr = specpdl;
  specpdl_depth_counter = 0;
  catchlist = 0;
  Vcondition_handlers = Qnil;
  backtrace_list = 0;
  Vquit_flag = Qnil;
  debug_on_next_call = 0;
  lisp_eval_depth = 0;
  entering_debugger = 0;

  first_desired_multiple_value = 0;
  multiple_value_current_limit = 1;
}

void
reinit_vars_of_eval (void)
{
  preparing_for_armageddon = 0;
  in_warnings = 0;
  specpdl_size = 50;
  specpdl = xnew_array (struct specbinding, specpdl_size);
  /* XEmacs change: increase these values. */
  max_specpdl_size = 3000;
  max_lisp_eval_depth = 1000;
#ifdef DEFEND_AGAINST_THROW_RECURSION
  throw_level = 0;
#endif
  init_eval_semi_early ();
}

void
vars_of_eval (void)
{
  DEFVAR_INT ("max-specpdl-size", &max_specpdl_size /*
Limit on number of Lisp variable bindings & unwind-protects before error.
*/ );

  DEFVAR_INT ("max-lisp-eval-depth", &max_lisp_eval_depth /*
Limit on depth in `eval', `apply' and `funcall' before error.
This limit is to catch infinite recursions for you before they cause
actual stack overflow in C, which would be fatal for Emacs.
You can safely make it considerably larger than its default value,
if that proves inconveniently small.
*/ );

  DEFVAR_LISP ("quit-flag", &Vquit_flag /*
t causes running Lisp code to abort, unless `inhibit-quit' is non-nil.
`critical' causes running Lisp code to abort regardless of `inhibit-quit'.
Normally, you do not need to set this value yourself.  It is set to
t each time a Control-G is detected, and to `critical' each time a
Shift-Control-G is detected.  The XEmacs core C code is littered with
calls to the QUIT; macro, which check the values of `quit-flag' and
`inhibit-quit' and ABORT (or more accurately, call (signal 'quit)) if
it's correct to do so.
*/ );
  Vquit_flag = Qnil;

  DEFVAR_LISP ("inhibit-quit", &Vinhibit_quit /*
Non-nil inhibits C-g quitting from happening immediately.
Note that `quit-flag' will still be set by typing C-g,
so a quit will be signalled as soon as `inhibit-quit' is nil.
To prevent this happening, set `quit-flag' to nil
before making `inhibit-quit' nil.

The value of `inhibit-quit' is ignored if a critical quit is
requested by typing control-shift-G in a window-system frame;
this is explained in more detail in `quit-flag'.
*/ );
  Vinhibit_quit = Qnil;

  DEFVAR_LISP ("stack-trace-on-error", &Vstack_trace_on_error /*
*Non-nil means automatically display a backtrace buffer
after any error that is not handled by a `condition-case'.
If the value is a list, an error only means to display a backtrace
if one of its condition symbols appears in the list.
See also variable `stack-trace-on-signal'.
*/ );
  Vstack_trace_on_error = Qnil;

  DEFVAR_LISP ("stack-trace-on-signal", &Vstack_trace_on_signal /*
*Non-nil means automatically display a backtrace buffer
after any error that is signalled, whether or not it is handled by
a `condition-case'.
If the value is a list, an error only means to display a backtrace
if one of its condition symbols appears in the list.
See also variable `stack-trace-on-error'.
*/ );
  Vstack_trace_on_signal = Qnil;

  DEFVAR_LISP ("debug-ignored-errors", &Vdebug_ignored_errors /*
*List of errors for which the debugger should not be called.
Each element may be a condition-name or a regexp that matches error messages.
If any element applies to a given error, that error skips the debugger
and just returns to top level.
This overrides the variable `debug-on-error'.
It does not apply to errors handled by `condition-case'.
*/ );
  Vdebug_ignored_errors = Qnil;

  DEFVAR_LISP ("debug-on-error", &Vdebug_on_error /*
*Non-nil means enter debugger if an unhandled error is signalled.
The debugger will not be entered if the error is handled by
a `condition-case'.
If the value is a list, an error only means to enter the debugger
if one of its condition symbols appears in the list.
This variable is overridden by `debug-ignored-errors'.
See also variables `debug-on-quit' and `debug-on-signal'.

Process filters are considered to be outside of condition-case forms
(unless contained in the process filter itself).  To prevent the
debugger from being called from a process filter, use a list value, or
put the expected error\(s) in `debug-ignored-errors'.

If this variable is set while XEmacs is running noninteractively (using
`-batch'), and XEmacs was configured with `--debug' (#define XEMACS_DEBUG
in the C code), instead of trying to invoke the Lisp debugger (which
obviously won't work), XEmacs will break out to a C debugger using
\(force-debugging-signal t).  This is useful because debugging
noninteractive runs of XEmacs is often very difficult, since they typically
happen as part of sometimes large and complex make suites (e.g. rebuilding
the XEmacs packages).  NOTE: This runs ABORT()!!! (As well as and after
executing INT 3 under MS Windows, which should invoke a debugger if it's
active.) This is guaranteed to kill XEmacs! (But in this situation, XEmacs
is about to die anyway, and if no debugger is present, this will usefully
dump core.) The most useful way to set this flag when debugging
noninteractive runs, especially in makefiles, is using the environment
variable XEMACSDEBUG, like this:

\(using csh)      setenv XEMACSDEBUG '(setq debug-on-error t)'
\(using bash)     export XEMACSDEBUG='(setq debug-on-error t)'
*/ );
  Vdebug_on_error = Qnil;

  DEFVAR_LISP ("debug-on-signal", &Vdebug_on_signal /*
*Non-nil means enter debugger if an error is signalled.
The debugger will be entered whether or not the error is handled by
a `condition-case'.
If the value is a list, an error only means to enter the debugger
if one of its condition symbols appears in the list.
See also variable `debug-on-quit'.

This will attempt to enter a C debugger when XEmacs is run noninteractively
and under the same conditions as described in `debug-on-error'.
*/ );
  Vdebug_on_signal = Qnil;

  DEFVAR_BOOL ("debug-on-quit", &debug_on_quit /*
*Non-nil means enter debugger if quit is signalled (C-G, for example).
Does not apply if quit is handled by a `condition-case'.  Entering the
debugger can also be achieved at any time (for X11 console) by typing
control-shift-G to signal a critical quit.
*/ );
  debug_on_quit = 0;

  DEFVAR_BOOL ("debug-on-next-call", &debug_on_next_call /*
Non-nil means enter debugger before next `eval', `apply' or `funcall'.
*/ );

  DEFVAR_BOOL ("backtrace-with-interal-sections",
	       &backtrace_with_internal_sections /*
Non-nil means backtraces will contain additional information indicating
when particular sections of the C code have been entered, e.g. redisplay(),
byte-char conversion, internal-external conversion, etc.  This can be
particularly useful when XEmacs crashes, in helping to pinpoint the problem.
*/ );
#ifdef ERROR_CHECK_STRUCTURES
  backtrace_with_internal_sections = 1;
#else
  backtrace_with_internal_sections = 0;
#endif

  DEFVAR_LISP ("debugger", &Vdebugger /*
Function to call to invoke debugger.
If due to frame exit, args are `exit' and the value being returned;
 this function's value will be returned instead of that.
If due to error, args are `error' and a list of the args to `signal'.
If due to `apply' or `funcall' entry, one arg, `lambda'.
If due to `eval' entry, one arg, t.
*/ );
  Vdebugger = Qnil;

  DEFVAR_CONST_INT ("multiple-values-limit", &Vmultiple_values_limit /*
The exclusive upper bound on the number of multiple values. 

This applies to `values', `values-list', `multiple-value-bind' and related
macros and special operators.
*/);
  Vmultiple_values_limit = EMACS_INT_MAX > INT_MAX ? INT_MAX : EMACS_INT_MAX;

  staticpro (&Vcatch_everything_tag);
  Vcatch_everything_tag = make_opaque (OPAQUE_CLEAR, 0);

  staticpro (&Vpending_warnings);
  Vpending_warnings = Qnil;
  dump_add_root_lisp_object (&Vpending_warnings_tail);
  Vpending_warnings_tail = Qnil;

  DEFVAR_LISP ("log-warning-minimum-level", &Vlog_warning_minimum_level);
  Vlog_warning_minimum_level = Qinfo;

  staticpro (&Vautoload_queue);
  Vautoload_queue = Qnil;

  staticpro (&Vcondition_handlers);

  staticpro (&Vdeletable_permanent_display_objects);
  Vdeletable_permanent_display_objects = Qnil;

  staticpro (&Vmodifiable_buffers);
  Vmodifiable_buffers = Qnil;

  inhibit_flags = 0;
}