xemacs-21.4 / src / symbols.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
/* "intern" and friends -- moved here from lread.c and data.c
   Copyright (C) 1985-1989, 1992-1994 Free Software Foundation, Inc.
   Copyright (C) 1995 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. */

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

/* NOTE:

   The value cell of a symbol can contain a simple value or one of
   various symbol-value-magic objects.  Some of these objects can
   chain into other kinds of objects.  Here is a table of possibilities:

   1a) simple value
   1b) Qunbound
   1c) symbol-value-forward, excluding Qunbound
   2) symbol-value-buffer-local -> 1a or 1b or 1c
   3) symbol-value-lisp-magic -> 1a or 1b or 1c
   4) symbol-value-lisp-magic -> symbol-value-buffer-local -> 1a or 1b or 1c
   5) symbol-value-varalias
   6) symbol-value-lisp-magic -> symbol-value-varalias

   The "chain" of a symbol-value-buffer-local is its current_value slot.

   The "chain" of a symbol-value-lisp-magic is its shadowed slot, which
   applies for handler types without associated handlers.

   All other fields in all the structures (including the "shadowed" slot
   in a symbol-value-varalias) can *only* contain a simple value or Qunbound.

*/

/* #### Ugh, though, this file does awful things with symbol-value-magic
   objects.  This ought to be cleaned up. */

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

#include "buffer.h"		/* for Vbuffer_defaults */
#include "console.h"

Lisp_Object Qad_advice_info, Qad_activate;

Lisp_Object Qget_value, Qset_value, Qbound_predicate, Qmake_unbound;
Lisp_Object Qlocal_predicate, Qmake_local;

Lisp_Object Qboundp, Qfboundp, Qglobally_boundp, Qmakunbound;
Lisp_Object Qsymbol_value, Qset, Qdefault_boundp, Qdefault_value;
Lisp_Object Qset_default, Qmake_variable_buffer_local, Qmake_local_variable;
Lisp_Object Qkill_local_variable, Qkill_console_local_variable;
Lisp_Object Qsymbol_value_in_buffer, Qsymbol_value_in_console;
Lisp_Object Qlocal_variable_p;

Lisp_Object Qconst_integer, Qconst_boolean, Qconst_object;
Lisp_Object Qconst_specifier;
Lisp_Object Qdefault_buffer, Qcurrent_buffer, Qconst_current_buffer;
Lisp_Object Qdefault_console, Qselected_console, Qconst_selected_console;

static Lisp_Object maybe_call_magic_handler (Lisp_Object sym,
					     Lisp_Object funsym,
					     int nargs, ...);
static Lisp_Object fetch_value_maybe_past_magic (Lisp_Object sym,
						 Lisp_Object
						 follow_past_lisp_magic);
static Lisp_Object *value_slot_past_magic (Lisp_Object sym);
static Lisp_Object follow_varalias_pointers (Lisp_Object object,
					     Lisp_Object
					     follow_past_lisp_magic);


#ifdef LRECORD_SYMBOL

static Lisp_Object mark_symbol (Lisp_Object, void (*) (Lisp_Object));
extern void print_symbol (Lisp_Object, Lisp_Object, int);
DEFINE_BASIC_LRECORD_IMPLEMENTATION ("symbol", symbol,
				     mark_symbol, print_symbol, 0, 0, 0,
				     struct Lisp_Symbol);

static Lisp_Object
mark_symbol (Lisp_Object obj, void (*markobj) (Lisp_Object))
{
  struct Lisp_Symbol *sym = XSYMBOL (obj);
  Lisp_Object pname;

  ((markobj) (sym->value));
  ((markobj) (sym->function));
  XSETSTRING (pname, sym->name);
  ((markobj) (pname));
  if (!symbol_next (sym))
    return (sym->plist);
  else
  {
    ((markobj) (sym->plist));
    /* Mark the rest of the symbols in the obarray hash-chain */
    sym = symbol_next (sym);
    XSETSYMBOL (obj, sym);
    return (obj);
  }
}

#endif /* LRECORD_SYMBOL */


/**********************************************************************/
/*                              Intern				      */
/**********************************************************************/

/* #### using a vector here is way bogus.  Use a hash table instead. */

Lisp_Object Vobarray;

static Lisp_Object initial_obarray;

/* oblookup stores the bucket number here, for the sake of Funintern.  */

static int oblookup_last_bucket_number;

static Lisp_Object
check_obarray (Lisp_Object obarray)
{
  while (!VECTORP (obarray) || vector_length (XVECTOR (obarray)) == 0)
    {
      /* If Vobarray is now invalid, force it to be valid.  */
      if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;

      obarray = wrong_type_argument (Qvectorp, obarray);
    }
  return obarray;
}

Lisp_Object
intern (CONST char *str)
{
  Lisp_Object tem;
  Bytecount len = strlen (str);
  Lisp_Object obarray = Vobarray;
  if (!VECTORP (obarray) || vector_length (XVECTOR (obarray)) == 0)
    obarray = check_obarray (obarray);
  tem = oblookup (obarray, (CONST Bufbyte *) str, len);
    
  if (SYMBOLP (tem))
    return tem;
  return Fintern (((purify_flag)
		   ? make_pure_pname ((CONST Bufbyte *) str, len, 0)
		   : make_string ((CONST Bufbyte *) str, len)),
		  obarray);
}

DEFUN ("intern", Fintern, Sintern, 1, 2, 0 /*
Return the canonical symbol whose name is STRING.
If there is none, one is created by this function and returned.
A second optional argument specifies the obarray to use;
it defaults to the value of `obarray'.
*/ )
  (str, obarray)
     Lisp_Object str, obarray;
{
  Lisp_Object sym, *ptr;
  Bytecount len;

  if (NILP (obarray)) obarray = Vobarray;
  obarray = check_obarray (obarray);

  CHECK_STRING (str);

  len = string_length (XSTRING (str));
  sym = oblookup (obarray, string_data (XSTRING (str)), len);
  if (!INTP (sym))
    /* Found it */
    return sym;

  ptr = &vector_data (XVECTOR (obarray))[XINT (sym)];

  if (purify_flag && ! purified (str))
    str = make_pure_pname (string_data (XSTRING (str)), len, 0);
  sym = Fmake_symbol (str);

  if (SYMBOLP (*ptr))
    symbol_next (XSYMBOL (sym)) = XSYMBOL (*ptr);
  else
    symbol_next (XSYMBOL (sym)) = 0;
  *ptr = sym;
  return sym;
}

DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0 /*
Return the canonical symbol whose name is STRING, or nil if none exists.
A second optional argument specifies the obarray to use;
it defaults to the value of `obarray'.
*/ )
  (str, obarray)
     Lisp_Object str, obarray;
{
  Lisp_Object tem;

  if (NILP (obarray)) obarray = Vobarray;
  obarray = check_obarray (obarray);

  CHECK_STRING (str);

  tem = oblookup (obarray, string_data (XSTRING (str)),
		  string_length (XSTRING (str)));
  if (!INTP (tem))
    return tem;
  return Qnil;
}

DEFUN ("unintern", Funintern, Sunintern, 1, 2, 0 /*
Delete the symbol named NAME, if any, from OBARRAY.
The value is t if a symbol was found and deleted, nil otherwise.
NAME may be a string or a symbol.  If it is a symbol, that symbol
is deleted, if it belongs to OBARRAY--no other symbol is deleted.
OBARRAY defaults to the value of the variable `obarray'
*/ )
  (name, obarray)
     Lisp_Object name, obarray;
{
  Lisp_Object string, tem;
  int hash;

  if (NILP (obarray)) obarray = Vobarray;
  obarray = check_obarray (obarray);

  if (SYMBOLP (name))
    XSETSTRING (string, XSYMBOL (name)->name);
  else
    {
      CHECK_STRING (name);
      string = name;
    }

  tem = oblookup (obarray, string_data (XSTRING (string)),
		  string_length (XSTRING (string)));
  if (INTP (tem))
    return Qnil;
  /* If arg was a symbol, don't delete anything but that symbol itself.  */
  if (SYMBOLP (name) && !EQ (name, tem))
    return Qnil;

  hash = oblookup_last_bucket_number;

  if (EQ (XVECTOR (obarray)->contents[hash], tem))
    {
      if (XSYMBOL (tem)->next)
	XSETSYMBOL (XVECTOR (obarray)->contents[hash], XSYMBOL (tem)->next);
      else
	XVECTOR (obarray)->contents[hash] = Qzero;
    }
  else
    {
      Lisp_Object tail, following;

      for (tail = XVECTOR (obarray)->contents[hash];
	   XSYMBOL (tail)->next;
	   tail = following)
	{
	  XSETSYMBOL (following, XSYMBOL (tail)->next);
	  if (EQ (following, tem))
	    {
	      XSYMBOL (tail)->next = XSYMBOL (following)->next;
	      break;
	    }
	}
    }

  return Qt;
}

/* Return the symbol in OBARRAY whose names matches the string
   of SIZE characters at PTR.  If there is no such symbol in OBARRAY,
   return nil.

   Also store the bucket number in oblookup_last_bucket_number.  */

Lisp_Object
oblookup (Lisp_Object obarray, CONST Bufbyte *ptr, Bytecount size)
{
  int hash, obsize;
  struct Lisp_Symbol *tail;
  Lisp_Object bucket;

  if (!VECTORP (obarray) ||
      (obsize = vector_length (XVECTOR (obarray))) == 0)
    {
      obarray = check_obarray (obarray);
      obsize = vector_length (XVECTOR (obarray));
    }
#if 0 /* FSFmacs */
  /* #### Huh? */
  /* This is sometimes needed in the middle of GC.  */
  obsize &= ~ARRAY_MARK_FLAG;
#endif
  /* Combining next two lines breaks VMS C 2.3.	 */
  hash = hash_string (ptr, size);
  hash %= obsize;
  bucket = vector_data (XVECTOR (obarray))[hash];
  oblookup_last_bucket_number = hash;
  if (ZEROP (bucket))
    ;
  else if (!SYMBOLP (bucket))
    error ("Bad data in guts of obarray"); /* Like CADR error message */
  else
    for (tail = XSYMBOL (bucket); ;)
      {
	if (string_length (tail->name) == size &&
	    !memcmp (string_data (tail->name), ptr, size))
	  {
	    XSETSYMBOL (bucket, tail);
	    return (bucket);
	  }
	tail = symbol_next (tail);
	if (!tail)
	  break;
      }
  return (make_int (hash));
}

int
hash_string (CONST Bufbyte *ptr, Bytecount len)
{
  CONST Bufbyte *p = ptr;
  CONST Bufbyte *end = p + len;
  Bufbyte c;
  int hash = 0;

  while (p != end)
    {
      c = *p++;
      if (c >= 0140) c -= 40;
      hash = ((hash<<3) + (hash>>28) + c);
    }
  return hash & 07777777777;
}

void
map_obarray (Lisp_Object obarray,
	     void (*fn) (Lisp_Object sym, Lisp_Object arg),
	     Lisp_Object arg)
{
  REGISTER int i;
  REGISTER Lisp_Object tail;
  CHECK_VECTOR (obarray);
  for (i = vector_length (XVECTOR (obarray)) - 1; i >= 0; i--)
    {
      tail = vector_data (XVECTOR (obarray))[i];
      if (SYMBOLP (tail))
	while (1)
	  {
	    struct Lisp_Symbol *next;
	    (*fn) (tail, arg);
	    next = symbol_next (XSYMBOL (tail));
	    if (!next)
	      break;
	    XSETSYMBOL (tail, next);
	  }
    }
}

static void
mapatoms_1 (Lisp_Object sym, Lisp_Object function)
{
  call1 (function, sym);
}

DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0 /*
Call FUNCTION on every symbol in OBARRAY.
OBARRAY defaults to the value of `obarray'.
*/ )
  (function, obarray)
     Lisp_Object function, obarray;
{
  if (NILP (obarray))
    obarray = Vobarray;
  obarray = check_obarray (obarray);

  map_obarray (obarray, mapatoms_1, function);
  return Qnil;
}


/**********************************************************************/
/*                              Apropos				      */
/**********************************************************************/

static void
apropos_accum (Lisp_Object symbol, Lisp_Object arg)
{
  Lisp_Object tem;
  Lisp_Object string = XCAR (arg);
  Lisp_Object predicate = XCAR (XCDR (arg));
  Lisp_Object *accumulation = &(XCDR (XCDR (arg)));

  tem = Fstring_match (string, Fsymbol_name (symbol), Qnil,
		       /* #### current-buffer dependence is bogus. */
		       Fcurrent_buffer ());
  if (!NILP (tem) && !NILP (predicate))
    tem = call1 (predicate, symbol);
  if (!NILP (tem))
    *accumulation = Fcons (symbol, *accumulation);
}

DEFUN ("apropos-internal", Fapropos_internal, Sapropos_internal, 1, 2, 0 /*
Show all symbols whose names contain match for REGEXP.
If optional 2nd arg PRED is non-nil, (funcall PRED SYM) is done
for each symbol and a symbol is mentioned only if that returns non-nil.
Return list of symbols found.
*/ )
  (string, pred)
     Lisp_Object string, pred;
{
  struct gcpro gcpro1;
  Lisp_Object accumulation;

  CHECK_STRING (string);
  accumulation = Fcons (string, Fcons (pred, Qnil));
  GCPRO1 (accumulation);
  map_obarray (Vobarray, apropos_accum, accumulation);
  accumulation = Fsort (Fcdr (Fcdr (accumulation)), Qstring_lessp);
  UNGCPRO;
  return (accumulation);
}


/* Extract and set components of symbols */

static void set_up_buffer_local_cache (Lisp_Object sym, 
				       struct symbol_value_buffer_local *bfwd,
				       struct buffer *buf);

DEFUN ("boundp", Fboundp, Sboundp, 1, 1, 0 /*
T if SYMBOL's value is not void.
*/ )
  (sym)
     Lisp_Object sym;
{
  CHECK_SYMBOL (sym);
  return (UNBOUNDP (find_symbol_value (sym)) ? Qnil : Qt);
}

DEFUN ("globally-boundp", Fglobally_boundp, Sglobally_boundp, 1, 1, 0 /*
T if SYMBOL has a global (non-bound) value.
This is for the byte-compiler; you really shouldn't be using this.
*/ )
  (sym)
     Lisp_Object sym;
{
  CHECK_SYMBOL (sym);
  return (UNBOUNDP (top_level_value (sym)) ? Qnil : Qt);
}

DEFUN ("fboundp", Ffboundp, Sfboundp, 1, 1, 0 /*
T if SYMBOL's function definition is not void.
*/ )
  (sym)
     Lisp_Object sym;
{
  CHECK_SYMBOL (sym);
  return ((UNBOUNDP (XSYMBOL (sym)->function)) ? Qnil : Qt);
}

/* Return non-zero if SYM's value or function (the current contents of
   which should be passed in as VAL) is constant, i.e. unsettable. */

static int
symbol_is_constant (Lisp_Object sym, Lisp_Object val)
{
  /* #### - I wonder if it would be better to just have a new magic value
     type and make nil, t, and all keywords have that same magic
     constant_symbol value.  This test is awfully specific about what is
     constant and what isn't.  --Stig */
  return (NILP (sym) || EQ (sym, Qt)
	  || (SYMBOL_VALUE_MAGIC_P (val)
	      && (XSYMBOL_VALUE_MAGIC_TYPE (val) ==
		  SYMVAL_CONST_OBJECT_FORWARD ||
		  XSYMBOL_VALUE_MAGIC_TYPE (val) ==
		  SYMVAL_CONST_SPECIFIER_FORWARD ||
		  XSYMBOL_VALUE_MAGIC_TYPE (val) ==
		  SYMVAL_CONST_FIXNUM_FORWARD ||
		  XSYMBOL_VALUE_MAGIC_TYPE (val) ==
		  SYMVAL_CONST_BOOLEAN_FORWARD ||
		  XSYMBOL_VALUE_MAGIC_TYPE (val) ==
		  SYMVAL_CONST_CURRENT_BUFFER_FORWARD ||
		  XSYMBOL_VALUE_MAGIC_TYPE (val) ==
		  SYMVAL_CONST_SELECTED_CONSOLE_FORWARD))
#if 0
	      /* #### - This is disabled until a new magic symbol_value for
		 constants is added */
	      || SYMBOL_IS_KEYWORD (sym)
#endif
	      );
}

/* We are setting SYM's value slot (or function slot, if FUNCTION_P is
   non-zero) to NEWVAL.  Make sure this is allowed.  NEWVAL is only
   used in the error message.  FOLLOW_PAST_LISP_MAGIC specifies
   whether we delve past symbol-value-lisp-magic objects.
   */

static void
reject_constant_symbols (Lisp_Object sym, Lisp_Object newval, int function_p,
			 Lisp_Object follow_past_lisp_magic)
{
  Lisp_Object val =
    (function_p ? XSYMBOL (sym)->function
     : fetch_value_maybe_past_magic (sym, follow_past_lisp_magic));

  if (SYMBOL_VALUE_MAGIC_P (val) &&
      XSYMBOL_VALUE_MAGIC_TYPE (val) ==
      SYMVAL_CONST_SPECIFIER_FORWARD)
    signal_simple_error ("Use `set-specifier' to change a specifier's value",
			 sym);

  if (symbol_is_constant (sym, val))
    {
      signal_error (Qsetting_constant,
		    ((UNBOUNDP (newval))
		     ? list1 (sym)
		     : list2 (sym, newval)));
    }
}

/* Verify that it's ok to make SYM buffer-local.  This rejects
   constants and default-buffer-local variables.  FOLLOW_PAST_LISP_MAGIC
   specifies whether we delve into symbol-value-lisp-magic objects.
   (Should be a symbol indicating what action is being taken; that way,
   we don't delve if there's a handler for that action, but do otherwise.) */

static void
verify_ok_for_buffer_local (Lisp_Object sym,
			    Lisp_Object follow_past_lisp_magic)
{
  Lisp_Object val = fetch_value_maybe_past_magic (sym, follow_past_lisp_magic);

  if (symbol_is_constant (sym, val) ||
      (SYMBOL_VALUE_MAGIC_P (val) &&
       XSYMBOL_VALUE_MAGIC_TYPE (val) ==
       SYMVAL_DEFAULT_BUFFER_FORWARD) ||
      (SYMBOL_VALUE_MAGIC_P (val) &&
       XSYMBOL_VALUE_MAGIC_TYPE (val) ==
       SYMVAL_DEFAULT_CONSOLE_FORWARD) ||
      /* #### It's theoretically possible for it to be reasonable
	 to have both console-local and buffer-local variables,
	 but I don't want to consider that right now. */
      (SYMBOL_VALUE_MAGIC_P (val) &&
       XSYMBOL_VALUE_MAGIC_TYPE (val) ==
       SYMVAL_SELECTED_CONSOLE_FORWARD)
      )
    signal_error (Qerror, 
		  list2 (build_string ("Symbol may not be buffer-local"),
			 sym));
}

DEFUN ("makunbound", Fmakunbound, Smakunbound, 1, 1, 0 /*
Make SYMBOL's value be void.
*/ )
  (sym)
     Lisp_Object sym;
{
  Fset (sym, Qunbound);
  return sym;
}

DEFUN ("fmakunbound", Ffmakunbound, Sfmakunbound, 1, 1, 0 /*
Make SYMBOL's function definition be void.
*/ )
  (sym)
     Lisp_Object sym;
{
  CHECK_SYMBOL (sym);
  reject_constant_symbols (sym, Qunbound, 1, Qt);
  XSYMBOL (sym)->function = Qunbound;
  return sym;
}

DEFUN ("symbol-function", Fsymbol_function, Ssymbol_function, 1, 1, 0 /*
Return SYMBOL's function definition.	 Error if that is void.
*/ )
  (symbol)
     Lisp_Object symbol;
{
  CHECK_SYMBOL (symbol);
  if (UNBOUNDP (XSYMBOL (symbol)->function))
    return Fsignal (Qvoid_function, list1 (symbol));
  return XSYMBOL (symbol)->function;
}

DEFUN ("symbol-plist", Fsymbol_plist, Ssymbol_plist, 1, 1, 0 /*
Return SYMBOL's property list.
*/ )
  (sym)
     Lisp_Object sym;
{
  CHECK_SYMBOL (sym);
  return XSYMBOL (sym)->plist;
}

DEFUN ("symbol-name", Fsymbol_name, Ssymbol_name, 1, 1, 0 /*
Return SYMBOL's name, a string.
*/ )
  (sym)
     Lisp_Object sym;
{
  Lisp_Object name;

  CHECK_SYMBOL (sym);
  XSETSTRING (name, XSYMBOL (sym)->name);
  return name;
}

DEFUN ("fset", Ffset, Sfset, 2, 2, 0 /*
Set SYMBOL's function definition to NEWVAL, and return NEWVAL.
*/ )
  (sym, newdef)
     Lisp_Object sym, newdef;
{
  /* This function can GC */
  CHECK_SYMBOL (sym);
  reject_constant_symbols (sym, newdef, 1, Qt);
  if (!NILP (Vautoload_queue) && !UNBOUNDP (XSYMBOL (sym)->function))
    Vautoload_queue = Fcons (Fcons (sym, XSYMBOL (sym)->function),
			     Vautoload_queue);
  XSYMBOL (sym)->function = newdef;
  /* Handle automatic advice activation */
  if (CONSP (XSYMBOL (sym)->plist) && !NILP (Fget (sym, Qad_advice_info,
  						   Qnil)))
    {
      call2 (Qad_activate, sym, Qnil);
      newdef = XSYMBOL (sym)->function;
    }
  return newdef;
}

/* FSFmacs */
DEFUN ("define-function", Fdefine_function, Sdefine_function, 2, 2, 0 /*
Set SYMBOL's function definition to NEWVAL, and return NEWVAL.
Associates the function with the current load file, if any.
*/ )
  (sym, newdef)
     Lisp_Object sym, newdef;
{
  /* This function can GC */
  CHECK_SYMBOL (sym);
  Ffset (sym, newdef);
  LOADHIST_ATTACH (sym);
  return newdef;
}


DEFUN ("setplist", Fsetplist, Ssetplist, 2, 2, 0 /*
Set SYMBOL's property list to NEWVAL, and return NEWVAL.
*/ )
  (sym, newplist)
     Lisp_Object sym, newplist;
{
  CHECK_SYMBOL (sym);
  XSYMBOL (sym)->plist = newplist;
  return newplist;
}


/**********************************************************************/
/*                           symbol-value			      */
/**********************************************************************/

/* If the contents of the value cell of a symbol is one of the following
   three types of objects, then the symbol is "magic" in that setting
   and retrieving its value doesn't just set or retrieve the raw
   contents of the value cell.  None of these objects can escape to
   the user level, so there is no loss of generality.

   If a symbol is "unbound", then the contents of its value cell is
   Qunbound.  Despite appearances, this is *not* a symbol, but is
   a symbol-value-forward object.

   Logically all of the following objects are "symbol-value-magic"
   objects, and there are some games played w.r.t. this (#### this
   should be cleaned up).  SYMBOL_VALUE_MAGIC_P is true for all of
   the object types.  XSYMBOL_VALUE_MAGIC_TYPE returns the type of
   symbol-value-magic object.  There are more than three types
   returned by this macro: in particular, symbol-value-forward
   has eight subtypes, and symbol-value-buffer-local has two.  See
   symeval.h.

   1. symbol-value-forward

   symbol-value-forward is used for variables whose actual contents
   are stored in a C variable of some sort, and for Qunbound.  The
   lcheader.next field (which is only used to chain together free
   lcrecords) holds a pointer to the actual C variable.  Included
   in this type are "buffer-local" variables that are actually
   stored in the buffer object itself; in this case, the "pointer"
   is an offset into the struct buffer structure.

   The subtypes are as follows:

   SYMVAL_OBJECT_FORWARD:
      (declare with DEFVAR_LISP)
      The value of this variable is stored in a C variable of type
      "Lisp_Object".  Setting this variable sets the C variable.
      Accessing this variable retrieves a value from the C variable.
      These variables can be buffer-local -- in this case, the
      raw symbol-value field gets converted into a
      symbol-value-buffer-local, whose "current_value" slot contains
      the symbol-value-forward. (See below.)

   SYMVAL_FIXNUM_FORWARD:
   SYMVAL_BOOLEAN_FORWARD:
      (declare with DEFVAR_INT or DEFVAR_BOOL)
      Similar to SYMVAL_OBJECT_FORWARD except that the C variable
      is of type "int" and is an integer or boolean, respectively.

   SYMVAL_CONST_OBJECT_FORWARD:
   SYMVAL_CONST_FIXNUM_FORWARD:
   SYMVAL_CONST_BOOLEAN_FORWARD:
      (declare with DEFVAR_CONST_LISP, DEFVAR_CONST_INT, or
       DEFVAR_CONST_BOOL)
      Similar to SYMVAL_OBJECT_FORWARD, SYMVAL_FIXNUM_FORWARD, or
      SYMVAL_BOOLEAN_FORWARD, respectively, except that the value cannot
      be changed.

   SYMVAL_CONST_SPECIFIER_FORWARD:
      (declare with DEFVAR_SPECIFIER)
      Exactly like SYMVAL_CONST_OBJECT_FORWARD except that error message
      you get when attempting to set the value says to use
      `set-specifier' instead.

   SYMVAL_CURRENT_BUFFER_FORWARD:
      (declare with DEFVAR_BUFFER_LOCAL)
      This is used for built-in buffer-local variables -- i.e.
      Lisp variables whose value is stored in the "struct buffer".
      Variables of this sort always forward into C "Lisp_Object"
      fields (although there's no reason in principle that other
      types for ints and booleans couldn't be added).  Note that
      some of these variables are automatically local in each
      buffer, while some are only local when they become set
      (similar to `make-variable-buffer-local').  In these latter
      cases, of course, the default value shows through in all
      buffers in which the variable doesn't have a local value.
      This is implemented by making sure the "struct buffer" field
      always contains the correct value (whether it's local or
      a default) and maintaining a mask in the "struct buffer"
      indicating which fields are local.  When `set-default' is
      called on a variable that's not always local to all buffers,
      it loops through each buffer and sets the corresponding
      field in each buffer without a local value for the field,
      according to the mask.

      Calling `make-local-variable' on a variable of this sort
      only has the effect of maybe changing the current buffer's mask.
      Calling `make-variable-buffer-local' on a variable of this
      sort has no effect at all.

   SYMVAL_CONST_CURRENT_BUFFER_FORWARD:
      (declare with DEFVAR_CONST_BUFFER_LOCAL)
      Same as SYMVAL_CURRENT_BUFFER_FORWARD except that the
      value cannot be set.

   SYMVAL_DEFAULT_BUFFER_FORWARD:
      (declare with DEFVAR_BUFFER_DEFAULTS)
      This is used for the Lisp variables that contain the
      default values of built-in buffer-local variables.  Setting
      or referencing one of these variables forwards into a slot
      in the special struct buffer Vbuffer_defaults.

   SYMVAL_UNBOUND_MARKER:
      This is used for only one object, Qunbound.

   SYMVAL_SELECTED_CONSOLE_FORWARD:
      (declare with DEFVAR_CONSOLE_LOCAL)
      This is used for built-in console-local variables -- i.e.
      Lisp variables whose value is stored in the "struct console".
      These work just like built-in buffer-local variables.
      However, calling `make-local-variable' or
      `make-variable-buffer-local' on one of these variables
      is currently disallowed because that would entail having
      both console-local and buffer-local variables, which is
      trickier to implement.
      
   SYMVAL_CONST_SELECTED_CONSOLE_FORWARD:
      (declare with DEFVAR_CONST_CONSOLE_LOCAL)
      Same as SYMVAL_SELECTED_CONSOLE_FORWARD except that the
      value cannot be set.

   SYMVAL_DEFAULT_CONSOLE_FORWARD:
      (declare with DEFVAR_CONSOLE_DEFAULTS)
      This is used for the Lisp variables that contain the
      default values of built-in console-local variables.  Setting
      or referencing one of these variables forwards into a slot
      in the special struct console Vconsole_defaults.


   2. symbol-value-buffer-local

   symbol-value-buffer-local is used for variables that have had
   `make-local-variable' or `make-variable-buffer-local' applied
   to them.  This object contains an alist mapping buffers to
   values.  In addition, the object contains a "current value",
   which is the value in some buffer.  Whenever you access the
   variable with `symbol-value' or set it with `set' or `setq',
   things are switched around so that the "current value"
   refers to the current buffer, if it wasn't already.  This
   way, repeated references to a variable in the same buffer
   are almost as efficient as if the variable weren't buffer
   local.  Note that the alist may not be up-to-date w.r.t.
   the buffer whose value is current, as the "current value"
   cache is normally only flushed into the alist when the
   buffer it refers to changes.

   Note also that it is possible for `make-local-variable'
   or `make-variable-buffer-local' to be called on a variable
   that forwards into a C variable (i.e. a variable whose
   value cell is a symbol-value-forward).  In this case,
   the value cell becomes a symbol-value-buffer-local (as
   always), and the symbol-value-forward moves into
   the "current value" cell in this object.  Also, in
   this case the "current value" *always* refers to the
   current buffer, so that the values of the C variable
   always is the correct value for the current buffer.
   set_buffer_internal() automatically updates the current-value
   cells of all buffer-local variables that forward into C
   variables. (There is a list of all buffer-local variables
   that is maintained for this and other purposes.)

   Note that only certain types of `symbol-value-forward' objects
   can find their way into the "current value" cell of a
   `symbol-value-buffer-local' object: SYMVAL_OBJECT_FORWARD,
   SYMVAL_FIXNUM_FORWARD, SYMVAL_BOOLEAN_FORWARD, and
   SYMVAL_UNBOUND_MARKER.  The SYMVAL_CONST_*_FORWARD cannot
   be buffer-local because they are unsettable;
   SYMVAL_DEFAULT_*_FORWARD cannot be buffer-local because that
   makes no sense; making SYMVAL_CURRENT_BUFFER_FORWARD buffer-local
   does not have much of an effect (it's already buffer-local); and
   SYMVAL_SELECTED_CONSOLE_FORWARD cannot be buffer-local because
   that's not currently implemented.


   3. symbol-value-varalias

   A symbol-value-varalias object is used for variables that
   are aliases for other variables.  This object contains
   the symbol that this variable is aliased to.
   symbol-value-varalias objects cannot occur anywhere within
   a symbol-value-buffer-local object, and most of the
   low-level functions below do not accept them; you need
   to call follow_varalias_pointers to get the actual
   symbol to operate on.
   */

static Lisp_Object mark_symbol_value_buffer_local (Lisp_Object,
						   void (*) (Lisp_Object));
static Lisp_Object mark_symbol_value_lisp_magic (Lisp_Object,
						 void (*) (Lisp_Object));
static Lisp_Object mark_symbol_value_varalias (Lisp_Object,
					       void (*) (Lisp_Object));

DEFINE_LRECORD_IMPLEMENTATION ("symbol-value-forward",
			       symbol_value_forward,
			       this_one_is_unmarkable,
			       print_symbol_value_magic, 0, 0, 0,
			       struct symbol_value_forward);

DEFINE_LRECORD_IMPLEMENTATION ("symbol-value-buffer-local",
			       symbol_value_buffer_local, 
			       mark_symbol_value_buffer_local,
			       print_symbol_value_magic, 
			       0, 0, 0,
			       struct symbol_value_buffer_local);

DEFINE_LRECORD_IMPLEMENTATION ("symbol-value-lisp-magic",
			       symbol_value_lisp_magic, 
			       mark_symbol_value_lisp_magic,
			       print_symbol_value_magic, 
			       0, 0, 0,
			       struct symbol_value_lisp_magic);

DEFINE_LRECORD_IMPLEMENTATION ("symbol-value-varalias",
			       symbol_value_varalias, 
			       mark_symbol_value_varalias,
			       print_symbol_value_magic, 
			       0, 0, 0,
			       struct symbol_value_varalias);

static Lisp_Object
mark_symbol_value_buffer_local (Lisp_Object obj,
				void (*markobj) (Lisp_Object))
{
  struct symbol_value_buffer_local *bfwd;

  assert (XSYMBOL_VALUE_MAGIC_TYPE (obj) == SYMVAL_BUFFER_LOCAL ||
	  XSYMBOL_VALUE_MAGIC_TYPE (obj) == SYMVAL_SOME_BUFFER_LOCAL);

  bfwd = XSYMBOL_VALUE_BUFFER_LOCAL (obj);
  ((markobj) (bfwd->default_value));
  ((markobj) (bfwd->current_value));
  ((markobj) (bfwd->current_buffer));
  return (bfwd->current_alist_element);
}

static Lisp_Object
mark_symbol_value_lisp_magic (Lisp_Object obj,
			      void (*markobj) (Lisp_Object))
{
  struct symbol_value_lisp_magic *bfwd;
  int i;

  assert (XSYMBOL_VALUE_MAGIC_TYPE (obj) == SYMVAL_LISP_MAGIC);

  bfwd = XSYMBOL_VALUE_LISP_MAGIC (obj);
  for (i = 0; i < MAGIC_HANDLER_MAX; i++)
    {
      ((markobj) (bfwd->handler[i]));
      ((markobj) (bfwd->harg[i]));
    }
  return (bfwd->shadowed);
}

static Lisp_Object
mark_symbol_value_varalias (Lisp_Object obj,
			    void (*markobj) (Lisp_Object))
{
  struct symbol_value_varalias *bfwd;

  assert (XSYMBOL_VALUE_MAGIC_TYPE (obj) == SYMVAL_VARALIAS);

  bfwd = XSYMBOL_VALUE_VARALIAS (obj);
  ((markobj) (bfwd->shadowed));
  return (bfwd->aliasee);
}

/* Should never, ever be called. (except by an external debugger) */
void
print_symbol_value_magic (Lisp_Object obj, 
			  Lisp_Object printcharfun, int escapeflag)
{
  char buf[200];
  sprintf (buf, "#<INTERNAL EMACS BUG (symfwd %d) 0x%x>",
	   (EMACS_INT) XSYMBOL_VALUE_MAGIC_TYPE (obj),
	   (EMACS_INT) XPNTR (obj));
  write_c_string (buf, printcharfun);
}


/* Getting and setting values of symbols */

/* Given the raw contents of a symbol value cell, return the Lisp value of
   the symbol.  However, VALCONTENTS cannot be a symbol-value-buffer-local,
   symbol-value-lisp-magic, or symbol-value-varalias.

   BUFFER specifies a buffer, and is used for built-in buffer-local
   variables, which are of type SYMVAL_CURRENT_BUFFER_FORWARD.
   Note that such variables are never encapsulated in a
   symbol-value-buffer-local structure.

   CONSOLE specifies a console, and is used for built-in console-local
   variables, which are of type SYMVAL_SELECTED_CONSOLE_FORWARD.
   Note that such variables are (currently) never encapsulated in a
   symbol-value-buffer-local structure.
 */

static Lisp_Object
do_symval_forwarding (Lisp_Object valcontents, struct buffer *buffer,
		      struct console *console)
{
  CONST struct symbol_value_forward *fwd;

  if (!SYMBOL_VALUE_MAGIC_P (valcontents))
    return (valcontents);

  fwd = XSYMBOL_VALUE_FORWARD (valcontents);
  switch (fwd->magic.type)
    {
    case SYMVAL_FIXNUM_FORWARD:
    case SYMVAL_CONST_FIXNUM_FORWARD:
      return (make_int (*((int *)symbol_value_forward_forward (fwd))));

    case SYMVAL_BOOLEAN_FORWARD:
    case SYMVAL_CONST_BOOLEAN_FORWARD:
      {
	if (*((int *)symbol_value_forward_forward (fwd)))
	  return (Qt);
	else
	  return (Qnil);
      }

    case SYMVAL_OBJECT_FORWARD:
    case SYMVAL_CONST_OBJECT_FORWARD:
    case SYMVAL_CONST_SPECIFIER_FORWARD:
      return (*((Lisp_Object *)symbol_value_forward_forward (fwd)));

    case SYMVAL_DEFAULT_BUFFER_FORWARD:
      return (*((Lisp_Object *)((char *) XBUFFER (Vbuffer_defaults)
				+ ((char *)symbol_value_forward_forward (fwd)
				   - (char *)&buffer_local_flags))));


    case SYMVAL_CURRENT_BUFFER_FORWARD:
    case SYMVAL_CONST_CURRENT_BUFFER_FORWARD:
      assert (buffer);
      return (*((Lisp_Object *)((char *)buffer
				+ ((char *)symbol_value_forward_forward (fwd)
				   - (char *)&buffer_local_flags))));

    case SYMVAL_DEFAULT_CONSOLE_FORWARD:
      return (*((Lisp_Object *)((char *) XCONSOLE (Vconsole_defaults)
				+ ((char *)symbol_value_forward_forward (fwd)
				   - (char *)&console_local_flags))));

    case SYMVAL_SELECTED_CONSOLE_FORWARD:
    case SYMVAL_CONST_SELECTED_CONSOLE_FORWARD:
      assert (console);
      return (*((Lisp_Object *)((char *)console
				+ ((char *)symbol_value_forward_forward (fwd)
				   - (char *)&console_local_flags))));

    case SYMVAL_UNBOUND_MARKER:
      return (valcontents);

    default:
      abort ();
    }
  return Qnil;	/* suppress compiler warning */
}

/* Set the value of default-buffer-local variable SYM to VALUE. */

static void
set_default_buffer_slot_variable (Lisp_Object sym,
				  Lisp_Object value)
{
  /* Handle variables like case-fold-search that have special slots in
     the buffer. Make them work apparently like buffer_local variables.
     */
  /* At this point, the value cell may not contain a symbol-value-varalias
     or symbol-value-buffer-local, and if there's a handler, we should
     have already called it. */
  Lisp_Object valcontents = fetch_value_maybe_past_magic (sym, Qt);
  CONST struct symbol_value_forward *fwd 
    = XSYMBOL_VALUE_FORWARD (valcontents);
  int offset = ((char *) symbol_value_forward_forward (fwd) 
		- (char *) &buffer_local_flags);
  int mask = XINT (*((Lisp_Object *) symbol_value_forward_forward (fwd)));
  int (*magicfun) (Lisp_Object simm, Lisp_Object *val, Lisp_Object in_object,
		   int flags) = symbol_value_forward_magicfun (fwd);
  
  *((Lisp_Object *) (offset + (char *) XBUFFER (Vbuffer_defaults)))
    = value;
  
  if (mask > 0)		/* Not always per-buffer */
    {
      Lisp_Object tail;
      
      /* Set value in each buffer which hasn't shadowed the default */
      LIST_LOOP (tail, Vbuffer_alist)
	{
	  struct buffer *b = XBUFFER (XCDR (XCAR (tail)));
	  if (!(b->local_var_flags & mask))
	    {
	      if (magicfun)
		(magicfun) (sym, &value, make_buffer (b), 0);
	      *((Lisp_Object *) (offset + (char *) b)) = value; 
	    }
	}
    }
}

/* Set the value of default-console-local variable SYM to VALUE. */

static void
set_default_console_slot_variable (Lisp_Object sym,
				   Lisp_Object value)
{
  /* Handle variables like case-fold-search that have special slots in
     the console. Make them work apparently like console_local variables.
     */
  /* At this point, the value cell may not contain a symbol-value-varalias
     or symbol-value-buffer-local, and if there's a handler, we should
     have already called it. */
  Lisp_Object valcontents = fetch_value_maybe_past_magic (sym, Qt);
  CONST struct symbol_value_forward *fwd 
    = XSYMBOL_VALUE_FORWARD (valcontents);
  int offset = ((char *) symbol_value_forward_forward (fwd) 
		- (char *) &console_local_flags);
  int mask = XINT (*((Lisp_Object *) symbol_value_forward_forward (fwd)));
  int (*magicfun) (Lisp_Object simm, Lisp_Object *val, Lisp_Object in_object,
		   int flags) = symbol_value_forward_magicfun (fwd);
  
  *((Lisp_Object *) (offset + (char *) XCONSOLE (Vconsole_defaults)))
    = value;
  
  if (mask > 0)		/* Not always per-console */
    {
      Lisp_Object tail;
      
      /* Set value in each console which hasn't shadowed the default */
      LIST_LOOP (tail, Vconsole_list)
	{
	  Lisp_Object dev = XCAR (tail);
	  struct console *d = XCONSOLE (dev);
	  if (!(d->local_var_flags & mask))
	    {
	      if (magicfun)
		(magicfun) (sym, &value, dev, 0);
	      *((Lisp_Object *) (offset + (char *) d)) = value; 
	    }
	}
    }
}

/* Store NEWVAL into SYM.

   SYM's value slot may *not* be types (5) or (6) above,
   i.e. no symbol-value-varalias objects. (You should have
   forwarded past all of these.)

   SYM should not be an unsettable symbol or a symbol with
   a magic `set-value' handler (unless you want to explicitly
   ignore this handler).

   OVALUE is the current value of SYM, but forwarded past any
   symbol-value-buffer-local and symbol-value-lisp-magic objects.
   (i.e. if SYM is a symbol-value-buffer-local, OVALUE should be
   the contents of its current-value cell.) NEWVAL may only be
   a simple value or Qunbound.  If SYM is a symbol-value-buffer-local,
   this function will only modify its current-value cell, which should
   already be set up to point to the current buffer.
  */

static void
store_symval_forwarding (Lisp_Object sym, Lisp_Object ovalue,
			 Lisp_Object newval)
{
  if (!SYMBOL_VALUE_MAGIC_P (ovalue) || UNBOUNDP (ovalue))
    {
      Lisp_Object *store_pointer = value_slot_past_magic (sym);

      if (SYMBOL_VALUE_BUFFER_LOCAL_P (*store_pointer))
	store_pointer =
	  &XSYMBOL_VALUE_BUFFER_LOCAL (*store_pointer)->current_value;

      assert (UNBOUNDP (*store_pointer)
	      || !SYMBOL_VALUE_MAGIC_P (*store_pointer));
      *store_pointer = newval;
    }

  else
    {
      CONST struct symbol_value_forward *fwd
	= XSYMBOL_VALUE_FORWARD (ovalue);
      int type = XSYMBOL_VALUE_MAGIC_TYPE (ovalue);
      int (*magicfun) (Lisp_Object simm, Lisp_Object *val,
		       Lisp_Object in_object, int flags) =
			 symbol_value_forward_magicfun (fwd);

      switch (type)
	{
	case SYMVAL_FIXNUM_FORWARD:
	  {
	    CHECK_INT (newval);
	    if (magicfun)
	      (magicfun) (sym, &newval, Qnil, 0);
	    *((int *) symbol_value_forward_forward (fwd)) = XINT (newval);
	    return;
	  }

	case SYMVAL_BOOLEAN_FORWARD:
	  {
	    if (magicfun)
	      (magicfun) (sym, &newval, Qnil, 0);
	    *((int *) symbol_value_forward_forward (fwd))
	      = ((NILP (newval)) ? 0 : 1);
	    return;
	  }

	case SYMVAL_OBJECT_FORWARD:
	  {
	    if (magicfun)
	      (magicfun) (sym, &newval, Qnil, 0);
	    *((Lisp_Object *) symbol_value_forward_forward (fwd)) = newval;
	    return;
	  }
	  
	case SYMVAL_DEFAULT_BUFFER_FORWARD:
	  {
	    set_default_buffer_slot_variable (sym, newval);
	    return;
	  }

	case SYMVAL_CURRENT_BUFFER_FORWARD:
	  {
	    if (magicfun)
	      (magicfun) (sym, &newval, make_buffer (current_buffer), 0);
	    *((Lisp_Object *) ((char *) current_buffer
			       + ((char *) symbol_value_forward_forward (fwd)
				  - (char *) &buffer_local_flags))) 
	      = newval;
	    return;
	  }

	case SYMVAL_DEFAULT_CONSOLE_FORWARD:
	  {
	    set_default_console_slot_variable (sym, newval);
	    return;
	  }

	case SYMVAL_SELECTED_CONSOLE_FORWARD:
	  {
	    if (magicfun)
	      (magicfun) (sym, &newval, Vselected_console, 0);
	    *((Lisp_Object *) ((char *) XCONSOLE (Vselected_console)
			       + ((char *) symbol_value_forward_forward (fwd)
				  - (char *) &console_local_flags))) 
	      = newval;
	    return;
	  }

	default:
	  abort ();
	}
    }
}

/* Given a per-buffer variable SYMBOL and its raw value-cell contents
   BFWD, locate and return a pointer to the element in BUFFER's
   local_var_alist for SYMBOL.  The return value will be Qnil if
   BUFFER does not have its own value for SYMBOL (i.e. the default
   value is seen in that buffer).
   */

static Lisp_Object
buffer_local_alist_element (struct buffer *buffer, Lisp_Object symbol,
			    struct symbol_value_buffer_local *bfwd)
{
  if (!NILP (bfwd->current_buffer) &&
      XBUFFER (bfwd->current_buffer) == buffer)
    /* This is just an optimization of the below. */
    return (bfwd->current_alist_element);
  else
    return (assq_no_quit (symbol, buffer->local_var_alist));
}

/* [Remember that the slot that mirrors CURRENT-VALUE in the
   symbol-value-buffer-local of a per-buffer variable -- i.e. the
   slot in CURRENT-BUFFER's local_var_alist, or the DEFAULT-VALUE
   slot -- may be out of date.]

   Write out any cached value in buffer-local variable SYMBOL's
   buffer-local structure, which is passed in as BFWD.
*/

static void
write_out_buffer_local_cache (Lisp_Object symbol,
			      struct symbol_value_buffer_local *bfwd)
{
  if (!NILP (bfwd->current_buffer))
    {
      /* We pass 0 for BUFFER because only SYMVAL_CURRENT_BUFFER_FORWARD
	 uses it, and that type cannot be inside a symbol-value-buffer-local */
      Lisp_Object cval = do_symval_forwarding (bfwd->current_value, 0, 0);
      if (NILP (bfwd->current_alist_element))
	/* current_value may be updated more recently than default_value */
	bfwd->default_value = cval;
      else
	Fsetcdr (bfwd->current_alist_element, cval);
    }
}

/* SYM is a buffer-local variable, and BFWD is its buffer-local structure.
   Set up BFWD's cache for validity in buffer BUF.  This assumes that
   the cache is currently in a consistent state (this can include
   not having any value cached, if BFWD->CURRENT_BUFFER is nil).

   If the cache is already set up for BUF, this function does nothing
   at all.

   Otherwise, if SYM forwards out to a C variable, this also forwards
   SYM's value in BUF out to the variable.  Therefore, you generally
   only want to call this when BUF is, or is about to become, the
   current buffer.

   (Otherwise, you can just retrieve the value without changing the
   cache, at the expense of slower retrieval.)
*/

static void
set_up_buffer_local_cache (Lisp_Object sym, 
			   struct symbol_value_buffer_local *bfwd,
			   struct buffer *buf)
{
  Lisp_Object new_alist_el, new_val;

  if (!NILP (bfwd->current_buffer)
      && buf == XBUFFER (bfwd->current_buffer))
    /* Cache is already set up. */
    return;

  /* Flush out the old cache. */
  write_out_buffer_local_cache (sym, bfwd);

  /* Retrieve the new alist element and new value. */
  new_alist_el = buffer_local_alist_element (buf, sym, bfwd);
  if (NILP (new_alist_el))
    new_val = bfwd->default_value;
  else
    new_val = Fcdr (new_alist_el);

  bfwd->current_alist_element = new_alist_el;
  XSETBUFFER (bfwd->current_buffer, buf);

  /* Now store the value into the current-value slot.
     We don't simply write it there, because the current-value
     slot might be a forwarding pointer, in which case we need
     to instead write the value into the C variable.

     We might also want to call a magic function.

     So instead, we call this function. */
  store_symval_forwarding (sym, bfwd->current_value, new_val);
}


void
kill_buffer_local_variables (struct buffer *buf)
{
  Lisp_Object prev = Qnil;
  Lisp_Object alist;

  /* Any which are supposed to be permanent,
     make local again, with the same values they had.  */
     
  for (alist = buf->local_var_alist; !NILP (alist); alist = XCDR (alist))
    {
      Lisp_Object sym = XCAR (XCAR (alist));
      struct symbol_value_buffer_local *bfwd;
      /* Variables with a symbol-value-varalias should not be here
	 (we should have forwarded past them) and there must be a
	 symbol-value-buffer-local.  If there's a symbol-value-lisp-magic,
	 just forward past it; if the variable has a handler, it was
	 already called. */
      Lisp_Object value = fetch_value_maybe_past_magic (sym, Qt);

      assert (SYMBOL_VALUE_BUFFER_LOCAL_P (value));
      bfwd = XSYMBOL_VALUE_BUFFER_LOCAL (value);

      if (!NILP (Fget (sym, Qpermanent_local, Qnil)))
	/* prev points to the last alist element that is still
	   staying around, so *only* update it now.  This didn't
	   used to be the case; this bug has been around since
	   mly's rewrite two years ago! */
	prev = alist;
      else
	{
	  /* Really truly kill it. */
	  if (!NILP (prev))
	    XCDR (prev) = XCDR (alist);
	  else
	    buf->local_var_alist = XCDR (alist);

	  /* We just effectively changed the value for this variable
	     in BUF. So: */

	  /* (1) If the cache is caching BUF, invalidate the cache. */
	  if (!NILP (bfwd->current_buffer) &&
	      buf == XBUFFER (bfwd->current_buffer))
	    bfwd->current_buffer = Qnil;

	  /* (2) If we changed the value in current_buffer and this
	     variable forwards to a C variable, we need to change the
	     value of the C variable.  set_up_buffer_local_cache()
	     will do this.  It doesn't hurt to do it whenever
	     BUF == current_buffer, so just go ahead and do that. */
	  if (buf == current_buffer)
	    set_up_buffer_local_cache (sym, bfwd, buf);
	}
    }
}

static Lisp_Object
find_symbol_value_1 (Lisp_Object sym, struct buffer *buf,
		     struct console *con, int swap_it_in)
{
  Lisp_Object valcontents;

 retry:
  valcontents = XSYMBOL (sym)->value;

 retry_2:
  if (!SYMBOL_VALUE_MAGIC_P (valcontents))
    return (valcontents);

  switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents))
    {
    case SYMVAL_LISP_MAGIC:
      /* #### kludge */
      valcontents = XSYMBOL_VALUE_LISP_MAGIC (valcontents)->shadowed;
      /* semi-change-o */
      goto retry_2;

    case SYMVAL_VARALIAS:
      sym = follow_varalias_pointers (sym, Qt /* #### kludge */);
      /* presto change-o! */
      goto retry;

    case SYMVAL_BUFFER_LOCAL:
    case SYMVAL_SOME_BUFFER_LOCAL:
      {
	struct symbol_value_buffer_local *bfwd
	  = XSYMBOL_VALUE_BUFFER_LOCAL (valcontents);

	if (swap_it_in)
	  {
	    set_up_buffer_local_cache (sym, bfwd, buf);
	    valcontents = bfwd->current_value;
	  }
	else
	  {
	    if (!NILP (bfwd->current_buffer) &&
		buf == XBUFFER (bfwd->current_buffer))
	      valcontents = bfwd->current_value;
	    else
	      {
		valcontents = assq_no_quit (sym, buf->local_var_alist);
		if (NILP (valcontents))
		  valcontents = bfwd->default_value;
		else
		  valcontents = Fcdr (valcontents);
	      }
	  }
	break;
      }
      
    default:
      break;
    }
  return (do_symval_forwarding (valcontents, buf, con));
}


/* Find the value of a symbol in BUFFER, returning Qunbound if it's not
   bound.  Note that it must not be possible to QUIT within this
   function. */

Lisp_Object
symbol_value_in_buffer (Lisp_Object sym, Lisp_Object buffer)
{
  struct buffer *buf;

  CHECK_SYMBOL (sym);

  if (!NILP (buffer))
    {
      CHECK_BUFFER (buffer);
      buf = XBUFFER (buffer);
    }
  else
    buf = current_buffer;

  return find_symbol_value_1 (sym, buf,
			      /* If it bombs out at startup due to a
				 Lisp error, this may be nil. */
			      CONSOLEP (Vselected_console)
			      ?	XCONSOLE (Vselected_console) : 0, 0);
}

static Lisp_Object
symbol_value_in_console (Lisp_Object sym, Lisp_Object console)
{
  CHECK_SYMBOL (sym);

  if (!NILP (console))
    CHECK_CONSOLE (console);
  else
    console = Vselected_console;

  return find_symbol_value_1 (sym, current_buffer, XCONSOLE (console), 0);
}

/* Return the current value of SYM.  The difference between this function
   and calling symbol_value_in_buffer with a BUFFER of Qnil is that
   this updates the CURRENT_VALUE slot of buffer-local variables to
   point to the current buffer, while symbol_value_in_buffer doesn't. */

Lisp_Object
find_symbol_value (Lisp_Object sym)
{
  /* WARNING: This function can be called when current_buffer is 0
     and Vselected_console is Qnil, early in initialization. */
  struct console *dev;

  CHECK_SYMBOL (sym);
  if (CONSOLEP (Vselected_console))
    dev = XCONSOLE (Vselected_console);
  else
    {
      /* This can also get called while we're preparing to shutdown.
         #### What should really happen in that case?  Should we
         actually fix things so we can't get here in that case? */
      assert (!initialized || preparing_for_armageddon);
      dev = 0;
    }

  return find_symbol_value_1 (sym, current_buffer, dev, 1);
}

DEFUN ("symbol-value", Fsymbol_value, Ssymbol_value, 1, 1, 0 /*
Return SYMBOL's value.  Error if that is void.
*/ )
  (sym)
     Lisp_Object sym;
{
  Lisp_Object val = find_symbol_value (sym);

  if (UNBOUNDP (val))
    return Fsignal (Qvoid_variable, list1 (sym));
  else
    return val;
}

DEFUN ("set", Fset, Sset, 2, 2, 0 /*
Set SYMBOL's value to NEWVAL, and return NEWVAL.
*/ )
  (sym, newval)
     Lisp_Object sym, newval;
{
  REGISTER Lisp_Object valcontents;
  /* remember, we're called by Fmakunbound() as well */

  CHECK_SYMBOL (sym);

 retry:
  reject_constant_symbols (sym, newval, 0,
			   UNBOUNDP (newval) ? Qmakunbound : Qset);
  valcontents = XSYMBOL (sym)->value;
 retry_2:

  if (SYMBOL_VALUE_MAGIC_P (valcontents))
    {
      switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents))
	{
	case SYMVAL_LISP_MAGIC:
	  {
	    Lisp_Object retval;

	    if (UNBOUNDP (newval))
	      retval = maybe_call_magic_handler (sym, Qmakunbound, 0);
	    else
	      retval = maybe_call_magic_handler (sym, Qset, 1, newval);
	    if (!UNBOUNDP (retval))
	      return newval;
	    valcontents = XSYMBOL_VALUE_LISP_MAGIC (valcontents)->shadowed;
	    /* semi-change-o */
	    goto retry_2;
	  }
	  
	case SYMVAL_VARALIAS:
	  sym = follow_varalias_pointers (sym,
					  UNBOUNDP (newval)
					  ? Qmakunbound : Qset);
	  /* presto change-o! */
	  goto retry;

	case SYMVAL_FIXNUM_FORWARD:
	case SYMVAL_BOOLEAN_FORWARD:
	case SYMVAL_OBJECT_FORWARD:
	case SYMVAL_DEFAULT_BUFFER_FORWARD:
	case SYMVAL_DEFAULT_CONSOLE_FORWARD:
	  if (UNBOUNDP (newval))
	    signal_error (Qerror, 
			  list2 (build_string ("Cannot makunbound"), sym));
	  break;

	case SYMVAL_UNBOUND_MARKER:
	  break;

	case SYMVAL_CURRENT_BUFFER_FORWARD:
	  {
	    CONST struct symbol_value_forward *fwd
	      = XSYMBOL_VALUE_FORWARD (valcontents);
	    int mask = XINT (*((Lisp_Object *)
                               symbol_value_forward_forward (fwd)));
	    if (mask > 0)
	      /* Setting this variable makes it buffer-local */
	      current_buffer->local_var_flags |= mask;
	    break;
	  }

	case SYMVAL_SELECTED_CONSOLE_FORWARD:
	  {
	    CONST struct symbol_value_forward *fwd
	      = XSYMBOL_VALUE_FORWARD (valcontents);
	    int mask = XINT (*((Lisp_Object *)
                               symbol_value_forward_forward (fwd)));
	    if (mask > 0)
	      /* Setting this variable makes it console-local */
	      XCONSOLE (Vselected_console)->local_var_flags |= mask;
	    break;
	  }

	case SYMVAL_BUFFER_LOCAL:
	case SYMVAL_SOME_BUFFER_LOCAL:
	  {
	    /* If we want to examine or set the value and
	       CURRENT-BUFFER is current, we just examine or set
	       CURRENT-VALUE. If CURRENT-BUFFER is not current, we
	       store the current CURRENT-VALUE value into
	       CURRENT-ALIST- ELEMENT, then find the appropriate alist
	       element for the buffer now current and set up
	       CURRENT-ALIST-ELEMENT.  Then we set CURRENT-VALUE out
	       of that element, and store into CURRENT-BUFFER.
	       
	       If we are setting the variable and the current buffer does
	       not have an alist entry for this variable, an alist entry is
	       created.
	       
	       Note that CURRENT-VALUE can be a forwarding pointer.
	       Each time it is examined or set, forwarding must be
	       done. */
	    struct symbol_value_buffer_local *bfwd
	      = XSYMBOL_VALUE_BUFFER_LOCAL (valcontents);
	    int some_buffer_local_p =
	      (bfwd->magic.type == SYMVAL_SOME_BUFFER_LOCAL);
	    /* What value are we caching right now?  */
	    Lisp_Object aelt = bfwd->current_alist_element;

	    if (!NILP (bfwd->current_buffer) &&
		current_buffer == XBUFFER (bfwd->current_buffer)
		&& ((some_buffer_local_p)
		    ? 1		   /* doesn't automatically become local */
		    : !NILP (aelt) /* already local */
		    ))
	      {
		/* Cache is valid */
		valcontents = bfwd->current_value;
	      }
	    else
	      {
		/* If the current buffer is not the buffer whose binding is
		   currently cached, or if it's a SYMVAL_BUFFER_LOCAL and
		   we're looking at the default value, the cache is invalid; we
		   need to write it out, and find the new CURRENT-ALIST-ELEMENT
		   */
		
		/* Write out the cached value for the old buffer; copy it
		   back to its alist element.  This works if the current
		   buffer only sees the default value, too.  */
		write_out_buffer_local_cache (sym, bfwd);

		/* Find the new value for CURRENT-ALIST-ELEMENT.  */
		aelt = buffer_local_alist_element (current_buffer, sym, bfwd);
		if (NILP (aelt))
		  {
		    /* This buffer is still seeing the default value.  */
		    if (!some_buffer_local_p)
		      {
			/* If it's a SYMVAL_BUFFER_LOCAL, give this buffer a
			   new assoc for a local value and set
			   CURRENT-ALIST-ELEMENT to point to that.  */
			aelt =
			  do_symval_forwarding (bfwd->current_value,
						current_buffer,
						XCONSOLE (Vselected_console));
			aelt = Fcons (sym, aelt);
			current_buffer->local_var_alist
			  = Fcons (aelt, current_buffer->local_var_alist);
		      }
		    else
		      {
			/* If the variable is a SYMVAL_SOME_BUFFER_LOCAL,
			   we're currently seeing the default value. */
			;
		      }
		  }
		/* Cache the new buffer's assoc in CURRENT-ALIST-ELEMENT.  */
		bfwd->current_alist_element = aelt;
		/* Set BUFFER, now that CURRENT-ALIST-ELEMENT is accurate.  */
		XSETBUFFER (bfwd->current_buffer, current_buffer);
		valcontents = bfwd->current_value;
	      }
	    break;
	  }
	default:
	  abort ();
	}
    }
  store_symval_forwarding (sym, valcontents, newval);

  return (newval);
}


/* Access or set a buffer-local symbol's default value.	 */

/* Return the default value of SYM, but don't check for voidness.
   Return Qunbound if it is void.  */

static Lisp_Object
default_value (Lisp_Object sym)
{
  Lisp_Object valcontents;

  CHECK_SYMBOL (sym);

 retry:
  valcontents = XSYMBOL (sym)->value;

 retry_2:
  if (!SYMBOL_VALUE_MAGIC_P (valcontents))
    return (valcontents);
    
  switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents))
    {
    case SYMVAL_LISP_MAGIC:
      /* #### kludge */
      valcontents = XSYMBOL_VALUE_LISP_MAGIC (valcontents)->shadowed;
      /* semi-change-o */
      goto retry_2;

    case SYMVAL_VARALIAS:
      sym = follow_varalias_pointers (sym, Qt /* #### kludge */);
      /* presto change-o! */
      goto retry;

    case SYMVAL_UNBOUND_MARKER:
      return valcontents;

    case SYMVAL_CURRENT_BUFFER_FORWARD:
      {
	CONST struct symbol_value_forward *fwd
	  = XSYMBOL_VALUE_FORWARD (valcontents);
	return (*((Lisp_Object *)((char *) XBUFFER (Vbuffer_defaults)
				  + ((char *)symbol_value_forward_forward (fwd)
				     - (char *)&buffer_local_flags))));
      }

    case SYMVAL_SELECTED_CONSOLE_FORWARD:
      {
	CONST struct symbol_value_forward *fwd
	  = XSYMBOL_VALUE_FORWARD (valcontents);
	return (*((Lisp_Object *)((char *) XCONSOLE (Vconsole_defaults)
				  + ((char *)symbol_value_forward_forward (fwd)
				     - (char *)&console_local_flags))));
      }

    case SYMVAL_BUFFER_LOCAL:
    case SYMVAL_SOME_BUFFER_LOCAL:
      {
	struct symbol_value_buffer_local *bfwd = 
	  XSYMBOL_VALUE_BUFFER_LOCAL (valcontents);

	/* Handle user-created local variables.	 */
	/* If var is set up for a buffer that lacks a local value for it,
	   the current value is nominally the default value.
	   But the current value slot may be more up to date, since
	   ordinary setq stores just that slot.	 So use that.  */
	if (NILP (bfwd->current_alist_element))
	  return (do_symval_forwarding (bfwd->current_value, current_buffer,
					XCONSOLE (Vselected_console)));
	else
	  return (bfwd->default_value);
      }
    default:
      /* For other variables, get the current value.	*/
      return (do_symval_forwarding (valcontents, current_buffer,
				    XCONSOLE (Vselected_console)));
    }
  
  RETURN_NOT_REACHED(Qnil)	/* suppress compiler warning */
}

DEFUN ("default-boundp", Fdefault_boundp, Sdefault_boundp, 1, 1, 0 /*
Return T if SYMBOL has a non-void default value.
This is the value that is seen in buffers that do not have their own values
for this variable.
*/ )
  (sym)
     Lisp_Object sym;
{
  Lisp_Object value;

  value = default_value (sym);
  return (UNBOUNDP (value) ? Qnil : Qt);
}

DEFUN ("default-value", Fdefault_value, Sdefault_value, 1, 1, 0 /*
Return SYMBOL's default value.
This is the value that is seen in buffers that do not have their own values
for this variable.  The default value is meaningful for variables with
local bindings in certain buffers.
*/ )
  (sym)
     Lisp_Object sym;
{
  Lisp_Object value;

  value = default_value (sym);
  if (UNBOUNDP (value))
    return Fsignal (Qvoid_variable, list1 (sym));
  return value;
}

DEFUN ("set-default", Fset_default, Sset_default, 2, 2, 0 /*
Set SYMBOL's default value to VAL.  SYMBOL and VAL are evaluated.
The default value is seen in buffers that do not have their own values
for this variable.
*/ )
  (sym, value)
     Lisp_Object sym, value;
{
  Lisp_Object valcontents;

  CHECK_SYMBOL (sym);

 retry:
  valcontents = XSYMBOL (sym)->value;

 retry_2:
  if (!SYMBOL_VALUE_MAGIC_P (valcontents))
    return Fset (sym, value);

  switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents))
    {
    case SYMVAL_LISP_MAGIC:
      RETURN_IF_NOT_UNBOUND (maybe_call_magic_handler (sym, Qset_default, 1,
						       value));
      valcontents = XSYMBOL_VALUE_LISP_MAGIC (valcontents)->shadowed;
      /* semi-change-o */
      goto retry_2;
	  
    case SYMVAL_VARALIAS:
      sym = follow_varalias_pointers (sym, Qset_default);
      /* presto change-o! */
      goto retry;

    case SYMVAL_CURRENT_BUFFER_FORWARD:
      set_default_buffer_slot_variable (sym, value);
      return (value);

    case SYMVAL_SELECTED_CONSOLE_FORWARD:
      set_default_console_slot_variable (sym, value);
      return (value);

    case SYMVAL_BUFFER_LOCAL:
    case SYMVAL_SOME_BUFFER_LOCAL:
      {
	/* Store new value into the DEFAULT-VALUE slot */
	struct symbol_value_buffer_local *bfwd
	  = XSYMBOL_VALUE_BUFFER_LOCAL (valcontents);

	bfwd->default_value = value;
	/* If current-buffer doesn't shadow default_value,
	 *  we must set the CURRENT-VALUE slot too */
	if (NILP (bfwd->current_alist_element))
	  store_symval_forwarding (sym, bfwd->current_value, value);
	return (value);
      }

    default:
      return Fset (sym, value);
    }
  RETURN_NOT_REACHED(Qnil)	/* suppress compiler warning */
}

DEFUN ("setq-default", Fsetq_default, Ssetq_default, 2, UNEVALLED, 0 /*
Set the default value of variable VAR to VALUE.
VAR, the variable name, is literal (not evaluated);
VALUE is an expression and it is evaluated.
The default value of a variable is seen in buffers
that do not have their own values for the variable.

More generally, you can use multiple variables and values, as in
  (setq-default SYM VALUE SYM VALUE...)
This sets each SYM's default value to the corresponding VALUE.
The VALUE for the Nth SYM can refer to the new default values
of previous SYMs.
*/ )
  (args)
     Lisp_Object args;
{
  /* This function can GC */
  Lisp_Object args_left;
  Lisp_Object val, sym;
  struct gcpro gcpro1;

  if (NILP (args))
    return Qnil;

  args_left = args;
  GCPRO1 (args);

  do
    {
      val = Feval (Fcar (Fcdr (args_left)));
      sym = Fcar (args_left);
      Fset_default (sym, val);
      args_left = Fcdr (Fcdr (args_left));
    }
  while (!NILP (args_left));

  UNGCPRO;
  return val;
}

/* Lisp functions for creating and removing buffer-local variables.  */

DEFUN ("make-variable-buffer-local", Fmake_variable_buffer_local,
       Smake_variable_buffer_local,
  1, 1, "vMake Variable Buffer Local: " /*
Make VARIABLE have a separate value for each buffer.
At any time, the value for the current buffer is in effect.
There is also a default value which is seen in any buffer which has not yet
set its own value.
Using `set' or `setq' to set the variable causes it to have a separate value
for the current buffer if it was previously using the default value.
The function `default-value' gets the default value and `set-default'
sets it.
*/ )
  (variable)
     Lisp_Object variable;
{
  Lisp_Object valcontents;

  CHECK_SYMBOL (variable);

 retry:
  verify_ok_for_buffer_local (variable, Qmake_variable_buffer_local);

  valcontents = XSYMBOL (variable)->value;

 retry_2:
  if (SYMBOL_VALUE_MAGIC_P (valcontents))
    {
      switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents))
	{
	case SYMVAL_LISP_MAGIC:
	  if (!UNBOUNDP (maybe_call_magic_handler
			 (variable, Qmake_variable_buffer_local, 0)))
	    return variable;
	  valcontents = XSYMBOL_VALUE_LISP_MAGIC (valcontents)->shadowed;
	  /* semi-change-o */
	  goto retry_2;

	case SYMVAL_VARALIAS:
	  variable = follow_varalias_pointers (variable,
					       Qmake_variable_buffer_local);
	  /* presto change-o! */
	  goto retry;

	case SYMVAL_FIXNUM_FORWARD:
	case SYMVAL_BOOLEAN_FORWARD:
	case SYMVAL_OBJECT_FORWARD:
	case SYMVAL_UNBOUND_MARKER:
	  break;

	case SYMVAL_CURRENT_BUFFER_FORWARD:
	case SYMVAL_BUFFER_LOCAL:
	  /* Already per-each-buffer */
	  return (variable);

	case SYMVAL_SOME_BUFFER_LOCAL:
	  /* Transmogrify */
	  XSYMBOL_VALUE_BUFFER_LOCAL (valcontents)->magic.type =
	    SYMVAL_BUFFER_LOCAL;
	  return (variable);

	default:
	  abort ();
	}
    }
  
  {
    struct symbol_value_buffer_local *bfwd
      = alloc_lcrecord (sizeof (struct symbol_value_buffer_local),
			lrecord_symbol_value_buffer_local);
    Lisp_Object foo = Qnil;
    bfwd->magic.type = SYMVAL_BUFFER_LOCAL;

    bfwd->default_value = find_symbol_value (variable);
    bfwd->current_value = valcontents;
    bfwd->current_alist_element = Qnil;
    bfwd->current_buffer = Fcurrent_buffer ();
    XSETSYMBOL_VALUE_MAGIC (foo, bfwd);
    *value_slot_past_magic (variable) = foo;
#if 1				/* #### Yuck!   FSFmacs bug-compatibility*/
    /* This sets the default-value of any make-variable-buffer-local to nil.
       That just sucks.	 User can just use setq-default to effect that,
       but there's no way to do makunbound-default to undo this lossage. */
    if (UNBOUNDP (valcontents))
      bfwd->default_value = Qnil;
#endif
#if 0				/* #### Yuck! */
    /* This sets the value to nil in this buffer.
       User could use (setq variable nil) to do this.
       It isn't as egregious to do this automatically
       as it is to do so to the default-value, but it's
       still really dubious. */
    if (UNBOUNDP (valcontents))
      Fset (variable, Qnil);
#endif
    return (variable);
  }
}

DEFUN ("make-local-variable", Fmake_local_variable, Smake_local_variable,
  1, 1, "vMake Local Variable: " /*
Make VARIABLE have a separate value in the current buffer.
Other buffers will continue to share a common default value.
\(The buffer-local value of VARIABLE starts out as the same value
VARIABLE previously had.  If VARIABLE was void, it remains void.)
See also `make-variable-buffer-local'.

If the variable is already arranged to become local when set,
this function causes a local value to exist for this buffer,
just as setting the variable would do.

Do not use `make-local-variable' to make a hook variable buffer-local.
Use `make-local-hook' instead.
*/ )
  (variable)
     Lisp_Object variable;
{
  Lisp_Object valcontents;
  struct symbol_value_buffer_local *bfwd;

  CHECK_SYMBOL (variable);

 retry:
  verify_ok_for_buffer_local (variable, Qmake_local_variable);

  valcontents = XSYMBOL (variable)->value;

 retry_2:
  if (SYMBOL_VALUE_MAGIC_P (valcontents))
    {
      switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents))
	{
	case SYMVAL_LISP_MAGIC:
	  if (!UNBOUNDP (maybe_call_magic_handler
			 (variable, Qmake_local_variable, 0)))
	    return variable;
	  valcontents = XSYMBOL_VALUE_LISP_MAGIC (valcontents)->shadowed;
	  /* semi-change-o */
	  goto retry_2;

	case SYMVAL_VARALIAS:
	  variable = follow_varalias_pointers (variable, Qmake_local_variable);
	  /* presto change-o! */
	  goto retry;

	case SYMVAL_FIXNUM_FORWARD:
	case SYMVAL_BOOLEAN_FORWARD:
	case SYMVAL_OBJECT_FORWARD:
	case SYMVAL_UNBOUND_MARKER:
	  break;

	case SYMVAL_BUFFER_LOCAL:
	case SYMVAL_CURRENT_BUFFER_FORWARD:
	  {
	    /* Make sure the symbol has a local value in this particular
	       buffer, by setting it to the same value it already has.	*/
	    Fset (variable, find_symbol_value (variable));
	    return (variable);
	  }

	case SYMVAL_SOME_BUFFER_LOCAL:
	  {
	    if (!NILP (buffer_local_alist_element (current_buffer,
						   variable,
						   (XSYMBOL_VALUE_BUFFER_LOCAL
						    (valcontents)))))
	      goto already_local_to_current_buffer;
	    else
	      goto already_local_to_some_other_buffer;
	  }

	default:
	  abort ();
	}
    }

  /* Make sure variable is set up to hold per-buffer values */
  bfwd = alloc_lcrecord (sizeof (struct symbol_value_buffer_local),
			 lrecord_symbol_value_buffer_local);
  bfwd->magic.type = SYMVAL_SOME_BUFFER_LOCAL;

  bfwd->current_buffer = Qnil;
  bfwd->current_alist_element = Qnil;
  bfwd->current_value = valcontents;
  /* passing 0 is OK because this should never be a
     SYMVAL_CURRENT_BUFFER_FORWARD or SYMVAL_SELECTED_CONSOLE_FORWARD
     variable. */
  bfwd->default_value = do_symval_forwarding (valcontents, 0, 0);

#if 0
  if (UNBOUNDP (bfwd->default_value))
    bfwd->default_value = Qnil; /* Yuck! */
#endif

  XSETSYMBOL_VALUE_MAGIC (valcontents, bfwd);
  *value_slot_past_magic (variable) = valcontents;

 already_local_to_some_other_buffer:

  /* Make sure this buffer has its own value of variable */
  bfwd = XSYMBOL_VALUE_BUFFER_LOCAL (valcontents);

  if (UNBOUNDP (bfwd->default_value))
    {
      /* If default value is unbound, set local value to nil. */
      XSETBUFFER (bfwd->current_buffer, current_buffer);
      bfwd->current_alist_element = Fcons (variable, Qnil);
      current_buffer->local_var_alist =
	Fcons (bfwd->current_alist_element, current_buffer->local_var_alist);
      store_symval_forwarding (variable, bfwd->current_value, Qnil);
      return (variable);
    }

  current_buffer->local_var_alist
    = Fcons (Fcons (variable, bfwd->default_value),
	     current_buffer->local_var_alist);

  /* Make sure symbol does not think it is set up for this buffer;
     force it to look once again for this buffer's value */
  if (!NILP (bfwd->current_buffer) &&
      current_buffer == XBUFFER (bfwd->current_buffer))
    bfwd->current_buffer = Qnil;

 already_local_to_current_buffer:

  /* If the symbol forwards into a C variable, then swap in the
     variable for this buffer immediately.  If C code modifies the
     variable before we swap in, then that new value will clobber the
     default value the next time we swap.  */
  bfwd = XSYMBOL_VALUE_BUFFER_LOCAL (valcontents);
  if (SYMBOL_VALUE_MAGIC_P (bfwd->current_value))
    {
      switch (XSYMBOL_VALUE_MAGIC_TYPE (bfwd->current_value))
	{
	case SYMVAL_FIXNUM_FORWARD:
	case SYMVAL_BOOLEAN_FORWARD:
	case SYMVAL_OBJECT_FORWARD:
	case SYMVAL_DEFAULT_BUFFER_FORWARD:
	  set_up_buffer_local_cache (variable, bfwd, current_buffer);
	  break;

	case SYMVAL_UNBOUND_MARKER:
	case SYMVAL_CURRENT_BUFFER_FORWARD:
	  break;

	default:
	  abort ();
	}
    }

  return (variable);
}

DEFUN ("kill-local-variable", Fkill_local_variable, Skill_local_variable,
  1, 1, "vKill Local Variable: " /*
Make VARIABLE no longer have a separate value in the current buffer.
From now on the default value will apply in this buffer.
*/ )
  (variable)
     Lisp_Object variable;
{
  Lisp_Object valcontents;

  CHECK_SYMBOL (variable);

 retry:
  valcontents = XSYMBOL (variable)->value;

 retry_2:
  if (!SYMBOL_VALUE_MAGIC_P (valcontents))
    return (variable);

  switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents))
    {
    case SYMVAL_LISP_MAGIC:
      if (!UNBOUNDP (maybe_call_magic_handler
		     (variable, Qkill_local_variable, 0)))
	return variable;
      valcontents = XSYMBOL_VALUE_LISP_MAGIC (valcontents)->shadowed;
      /* semi-change-o */
      goto retry_2;

    case SYMVAL_VARALIAS:
      variable = follow_varalias_pointers (variable, Qkill_local_variable);
      /* presto change-o! */
      goto retry;

    case SYMVAL_CURRENT_BUFFER_FORWARD:
      {
	CONST struct symbol_value_forward *fwd
	  = XSYMBOL_VALUE_FORWARD (valcontents);
	int offset = ((char *) symbol_value_forward_forward (fwd) 
			       - (char *) &buffer_local_flags);
	int mask =
	  XINT (*((Lisp_Object *) symbol_value_forward_forward (fwd)));

	if (mask > 0)
	  {
	    int (*magicfun) (Lisp_Object sym, Lisp_Object *val,
			     Lisp_Object in_object, int flags) =
			       symbol_value_forward_magicfun (fwd);
	    Lisp_Object oldval = * (Lisp_Object *)
	      (offset + (char *) XBUFFER (Vbuffer_defaults));
	    if (magicfun)
	      (magicfun) (variable, &oldval, make_buffer (current_buffer), 0);
	    *(Lisp_Object *) (offset + (char *) current_buffer)
	      = oldval;
	    current_buffer->local_var_flags &= ~mask;
	  }
	return (variable);
      }

    case SYMVAL_BUFFER_LOCAL:
    case SYMVAL_SOME_BUFFER_LOCAL:
      {
	/* Get rid of this buffer's alist element, if any */
	struct symbol_value_buffer_local *bfwd
	  = XSYMBOL_VALUE_BUFFER_LOCAL (valcontents);
	Lisp_Object alist = current_buffer->local_var_alist;
	Lisp_Object alist_element
	  = buffer_local_alist_element (current_buffer, variable, bfwd);

	if (!NILP (alist_element))
	  current_buffer->local_var_alist = Fdelq (alist_element, alist);

	/* Make sure symbol does not think it is set up for this buffer;
	   force it to look once again for this buffer's value */
	if (!NILP (bfwd->current_buffer) &&
	    current_buffer == XBUFFER (bfwd->current_buffer))
	  bfwd->current_buffer = Qnil;

	/* We just changed the value in the current_buffer.  If this
	   variable forwards to a C variable, we need to change the
	   value of the C variable.  set_up_buffer_local_cache()
	   will do this.  It doesn't hurt to do it always,
	   so just go ahead and do that. */
	set_up_buffer_local_cache (variable, bfwd, current_buffer);
      }
      return (variable);

    default:
      return (variable);
    }
  RETURN_NOT_REACHED(Qnil)	/* suppress compiler warning */
}


DEFUN ("kill-console-local-variable", Fkill_console_local_variable,
       Skill_console_local_variable,
  1, 1, "vKill Console Local Variable: " /*
Make VARIABLE no longer have a separate value in the selected console.
From now on the default value will apply in this console.
*/ )
  (variable)
     Lisp_Object variable;
{
  Lisp_Object valcontents;

  CHECK_SYMBOL (variable);

 retry:
  valcontents = XSYMBOL (variable)->value;

 retry_2:
  if (!SYMBOL_VALUE_MAGIC_P (valcontents))
    return (variable);

  switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents))
    {
    case SYMVAL_LISP_MAGIC:
      if (!UNBOUNDP (maybe_call_magic_handler
		     (variable, Qkill_console_local_variable, 0)))
	return variable;
      valcontents = XSYMBOL_VALUE_LISP_MAGIC (valcontents)->shadowed;
      /* semi-change-o */
      goto retry_2;

    case SYMVAL_VARALIAS:
      variable = follow_varalias_pointers (variable,
					   Qkill_console_local_variable);
      /* presto change-o! */
      goto retry;

    case SYMVAL_SELECTED_CONSOLE_FORWARD:
      {
	CONST struct symbol_value_forward *fwd
	  = XSYMBOL_VALUE_FORWARD (valcontents);
	int offset = ((char *) symbol_value_forward_forward (fwd) 
			       - (char *) &console_local_flags);
	int mask =
	  XINT (*((Lisp_Object *) symbol_value_forward_forward (fwd)));

	if (mask > 0)
	  {
	    int (*magicfun) (Lisp_Object sym, Lisp_Object *val,
			     Lisp_Object in_object, int flags) =
			       symbol_value_forward_magicfun (fwd);
	    Lisp_Object oldval = * (Lisp_Object *)
	      (offset + (char *) XCONSOLE (Vconsole_defaults));
	    if (magicfun)
	      (magicfun) (variable, &oldval, Vselected_console, 0);
	    *(Lisp_Object *) (offset + (char *) XCONSOLE (Vselected_console))
	      = oldval;
	    XCONSOLE (Vselected_console)->local_var_flags &= ~mask;
	  }
	return (variable);
      }

    default:
      return (variable);
    }
  RETURN_NOT_REACHED(Qnil)	/* suppress compiler warning */
}

/* Used by specbind to determine what effects it might have.  Returns:
 *   0 if symbol isn't buffer-local, and wouldn't be after it is set
 *  <0 if symbol isn't presently buffer-local, but set would make it so
 *  >0 if symbol is presently buffer-local
 */
int
symbol_value_buffer_local_info (Lisp_Object symbol, struct buffer *buffer)
{
  Lisp_Object valcontents;

 retry:
  valcontents = XSYMBOL (symbol)->value;

 retry_2:
  if (SYMBOL_VALUE_MAGIC_P (valcontents))
    {
      switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents))
	{
	case SYMVAL_LISP_MAGIC:
	  /* #### kludge */
	  valcontents = XSYMBOL_VALUE_LISP_MAGIC (valcontents)->shadowed;
	  /* semi-change-o */
	  goto retry_2;

	case SYMVAL_VARALIAS:
	  symbol = follow_varalias_pointers (symbol, Qt /* #### kludge */);
	  /* presto change-o! */
	  goto retry;

	case SYMVAL_CURRENT_BUFFER_FORWARD:
	  {
	    CONST struct symbol_value_forward *fwd
	      = XSYMBOL_VALUE_FORWARD (valcontents);
	    int mask = XINT (*((Lisp_Object *)
			       symbol_value_forward_forward (fwd)));
	    if ((mask <= 0) || (buffer && (buffer->local_var_flags & mask)))
	      /* Already buffer-local */
	      return (1);
	    else
	      /* Would be buffer-local after set */
	      return (-1);
	  }
	case SYMVAL_BUFFER_LOCAL:
	case SYMVAL_SOME_BUFFER_LOCAL:
	  {
	    struct symbol_value_buffer_local *bfwd
	      = XSYMBOL_VALUE_BUFFER_LOCAL (valcontents);
	    if (buffer
		&& !NILP (buffer_local_alist_element (buffer, symbol, bfwd)))
	      return (1);
	    else
	      return ((bfwd->magic.type == SYMVAL_BUFFER_LOCAL)
		      ? -1	 /* Automatically becomes local when set */
		      : 0);
	  }
	default:
	  return (0);
	}
    }
  return (0);
}


DEFUN ("symbol-value-in-buffer", Fsymbol_value_in_buffer,
       Ssymbol_value_in_buffer, 2, 3, 0 /*
Return the value of SYMBOL in BUFFER, or UNBOUND-VALUE if it is unbound.
*/ )
  (symbol, buffer, unbound_value)
     Lisp_Object symbol, buffer, unbound_value;
{
  Lisp_Object value;
  CHECK_SYMBOL (symbol);
  CHECK_BUFFER (buffer);
  value = symbol_value_in_buffer (symbol, buffer);
  if (UNBOUNDP (value))
    return (unbound_value);
  else
    return (value);
}

DEFUN ("symbol-value-in-console", Fsymbol_value_in_console,
       Ssymbol_value_in_console, 2, 3, 0 /*
Return the value of SYMBOL in CONSOLE, or UNBOUND-VALUE if it is unbound.
*/ )
  (symbol, console, unbound_value)
     Lisp_Object symbol, console, unbound_value;
{
  Lisp_Object value;
  CHECK_SYMBOL (symbol);
  CHECK_CONSOLE (console);
  value = symbol_value_in_console (symbol, console);
  if (UNBOUNDP (value))
    return (unbound_value);
  else
    return (value);
}

DEFUN ("built-in-variable-type", Fbuilt_in_variable_type,
       Sbuilt_in_variable_type, 1, 1, 0 /*
If SYM is a built-in variable, return info about this; else return nil.
The returned info will be a symbol, one of

`object'		A simple built-in variable.
`const-object'		Same, but cannot be set.
`integer'		A built-in integer variable.
`const-integer'		Same, but cannot be set.
`boolean'		A built-in boolean variable.
`const-boolean'		Same, but cannot be set.
`const-specifier'	Always contains a specifier; e.g. `has-modeline-p'.
`current-buffer'	A built-in buffer-local variable.
`const-current-buffer'	Same, but cannot be set.
`default-buffer'	Forwards to the default value of a built-in
			buffer-local variable.
`selected-console'	A built-in console-local variable.
`const-selected-console' Same, but cannot be set.
`default-console'	Forwards to the default value of a built-in
			console-local variable.
*/ )
  (sym)
     Lisp_Object sym;
{
  REGISTER Lisp_Object valcontents;

  CHECK_SYMBOL (sym);

 retry:
  valcontents = XSYMBOL (sym)->value;
 retry_2:

  if (SYMBOL_VALUE_MAGIC_P (valcontents))
    {
      switch (XSYMBOL_VALUE_MAGIC_TYPE (valcontents))
	{
	case SYMVAL_LISP_MAGIC:
	  valcontents = XSYMBOL_VALUE_LISP_MAGIC (valcontents)->shadowed;
	  /* semi-change-o */
	  goto retry_2;
	  
	case SYMVAL_VARALIAS:
	  sym = follow_varalias_pointers (sym, Qt);
	  /* presto change-o! */
	  goto retry;

	case SYMVAL_BUFFER_LOCAL:
	case SYMVAL_SOME_BUFFER_LOCAL:
	  valcontents =
	    XSYMBOL_VALUE_BUFFER_LOCAL (valcontents)->current_value;
	  /* semi-change-o */
	  goto retry_2;

	case SYMVAL_FIXNUM_FORWARD:
	  return Qinteger;

	case SYMVAL_CONST_FIXNUM_FORWARD:
	  return Qconst_integer;

	case SYMVAL_BOOLEAN_FORWARD:
	  return Qboolean;

	case SYMVAL_CONST_BOOLEAN_FORWARD:
	  return Qconst_boolean;

	case SYMVAL_OBJECT_FORWARD:
	  return Qobject;

	case SYMVAL_CONST_OBJECT_FORWARD:
	  return Qconst_object;

	case SYMVAL_CONST_SPECIFIER_FORWARD:
	  return Qconst_specifier;

	case SYMVAL_DEFAULT_BUFFER_FORWARD:
	  return Qdefault_buffer;

	case SYMVAL_CURRENT_BUFFER_FORWARD:
	  return Qcurrent_buffer;

	case SYMVAL_CONST_CURRENT_BUFFER_FORWARD:
	  return Qconst_current_buffer;

	case SYMVAL_DEFAULT_CONSOLE_FORWARD:
	  return Qdefault_console;

	case SYMVAL_SELECTED_CONSOLE_FORWARD:
	  return Qselected_console;

	case SYMVAL_CONST_SELECTED_CONSOLE_FORWARD:
	  return Qconst_selected_console;

	case SYMVAL_UNBOUND_MARKER:
	  return Qnil;

	default:
	  abort ();
	}
    }

  return Qnil;
}


DEFUN ("local-variable-p", Flocal_variable_p, Slocal_variable_p, 2, 3, 0 /*
Return t if SYMBOL's value is local to BUFFER.
If optional third arg AFTER-SET is true, return t if SYMBOL would be
buffer-local after it is set, regardless of whether it is so presently.
A nil value for BUFFER is *not* the same as (current-buffer), but means
"no buffer".  Specifically:

-- If BUFFER is nil and AFTER-SET is nil, a return value of t indicates that
   the variable is one of the special built-in variables that is always
   buffer-local. (This includes `buffer-file-name', `buffer-read-only',
   `buffer-undo-list', and others.)

-- If BUFFER is nil and AFTER-SET is t, a return value of t indicates that
   the variable has had `make-variable-buffer-local' applied to it.
*/ )
  (symbol, buffer, after_set)
     Lisp_Object symbol, buffer, after_set;
{
  int local_info;
  
  CHECK_SYMBOL (symbol);
  if (!NILP (buffer))
    {
      buffer = get_buffer (buffer, 1);
      local_info = symbol_value_buffer_local_info (symbol, XBUFFER (buffer));
    }
  else
    {
      local_info = symbol_value_buffer_local_info (symbol, 0);
    }
  
  if (NILP (after_set))
    return ((local_info > 0) ? Qt : Qnil);
  else
    return ((local_info != 0) ? Qt : Qnil);
}


/* 
I've gone ahead and partially implemented this because it's
super-useful for dealing with the compatibility problems in supporting
the old pointer-shape variables, and preventing people from `setq'ing
the new variables.  Any other way of handling this problem is way
ugly, likely to be slow, and generally not something I want to waste
my time worrying about.

The interface and/or function name is sure to change before this
gets into its final form.  I currently like the way everything is
set up and it has all the features I want it to have, except for
one: I really want to be able to have multiple nested handlers,
to implement an `advice'-like capabiility.  This would allow,
for example, a clean way of implementing `debug-if-set' or
`debug-if-referenced' and such.

NOTE NOTE NOTE NOTE NOTE NOTE NOTE:
************************************************************
**Only** the `set-value', `make-unbound', and `make-local'
handler types are currently implemented.  Implementing the
get-value and bound-predicate handlers is somewhat tricky
because there are lots of subfunctions (e.g. find_symbol_value()).
find_symbol_value(), in fact, is called from outside of
this module.  You'd have to have it do this:

-- check for a `bound-predicate' handler, call that if so;
   if it returns nil, return Qunbound
-- check for a `get-value' handler and call it and return
   that value

It gets even trickier when you have to deal with
sub-subfunctions like find_symbol_value_1(), and esp.
when you have to properly handle variable aliases, which
can lead to lots of tricky situations.  So I've just
punted on this, since the interface isn't officially
exported and we can get by with just a `set-value'
handler.

Actions in unimplemented handler types will correctly
ignore any handlers, and will not fuck anything up or
go awry.

WARNING WARNING: If you do go and implement another
type of handler, make *sure* to change
would_be_magic_handled() so it knows about this,
or dire things could result.
************************************************************
NOTE NOTE NOTE NOTE NOTE NOTE NOTE

Real documentation is as follows.  

Set a magic handler for VARIABLE.
This allows you to specify arbitrary behavior that results from
accessing or setting a variable.  For example, retrieving the
variable's value might actually retrieve the first element off of
a list stored in another variable, and setting the variable's value
might add an element to the front of that list. (This is how the
obsolete variable `unread-command-event' is implemented.)

In general it is NOT good programming practice to use magic variables
in a new package that you are designing.  If you feel the need to
do this, it's almost certainly a sign that you should be using a
function instead of a variable.  This facility is provided to allow
a package to support obsolete variables and provide compatibility
with similar packages with different variable names and semantics.
By using magic handlers, you can cleanly provide obsoleteness and
compatibility support and separate this support from the core
routines in a package.

VARIABLE should be a symbol naming the variable for which the
magic behavior is provided.  HANDLER-TYPE is a symbol specifying
which behavior is being controlled, and HANDLER is the function
that will be called to control this behavior.  HARG is a
value that will be passed to HANDLER but is otherwise
uninterpreted.  KEEP-EXISTING specifies what to do with existing
handlers of the same type; nil means \"erase them all\", t means
\"keep them but insert at the beginning\", the list (t) means
\"keep them but insert at the end\", a function means \"keep
them but insert before the specified function\", a list containing
a function means \"keep them but insert after the specified
function\".

You can specify magic behavior for any type of variable at all,
and for any handler types that are unspecified, the standard
behavior applies.  This allows you, for example, to use
`defvaralias' in conjunction with this function. (For that
matter, `defvaralias' could be implemented using this function.)

The behaviors that can be specified in HANDLER-TYPE are

get-value		(SYM ARGS FUN HARG HANDLERS)
    This means that one of the functions `symbol-value',
    `default-value', `symbol-value-in-buffer', or
    `symbol-value-in-console' was called on SYM.

set-value		(SYM ARGS FUN HARG HANDLERS)
    This means that one of the functions `set' or `set-default'
    was called on SYM.

bound-predicate		(SYM ARGS FUN HARG HANDLERS)
    This means that one of the functions `boundp', `globally-boundp',
    or `default-boundp' was called on SYM.

make-unbound		(SYM ARGS FUN HARG HANDLERS)
    This means that the function `makunbound' was called on SYM.

local-predicate		(SYM ARGS FUN HARG HANDLERS)
    This means that the function `local-variable-p' was called
    on SYM.

make-local		(SYM ARGS FUN HARG HANDLERS)
    This means that one of the functions `make-local-variable',
    `make-variable-buffer-local', `kill-local-variable',
    or `kill-console-local-variable' was called on SYM.

The meanings of the arguments are as follows:

   SYM is the symbol on which the function was called, and is always
   the first argument to the function.
   
   ARGS are the remaining arguments in the original call (i.e. all
   but the first).  In the case of `set-value' in particular,
   the first element of ARGS is the value to which the variable
   is being set.  In some cases, ARGS is sanitized from what was
   actually given.  For example, whenever `nil' is passed to an
   argument and it means `current-buffer', the current buffer is
   substituted instead.
   
   FUN is a symbol indicating which function is being called.
   For many of the functions, you can determine the corresponding
   function of a different class using
   `symbol-function-corresponding-function'.
   
   HARG is the argument that was given in the call
   to `set-symbol-value-handler' for SYM and HANDLER-TYPE.
   
   HANDLERS is a structure containing the remaining handlers
   for the variable; to call one of them, use
   `chain-to-symbol-value-handler'.

NOTE: You may *not* modify the list in ARGS, and if you want to
keep it around after the handler function exits, you must make
a copy using `copy-sequence'. (Same caveats for HANDLERS also.)
*/

static enum lisp_magic_handler
decode_magic_handler_type (Lisp_Object symbol)
{
  if (EQ (symbol, Qget_value))
    return MAGIC_HANDLER_GET_VALUE;
  if (EQ (symbol, Qset_value))
    return MAGIC_HANDLER_SET_VALUE;
  if (EQ (symbol, Qbound_predicate))
    return MAGIC_HANDLER_BOUND_PREDICATE;
  if (EQ (symbol, Qmake_unbound))
    return MAGIC_HANDLER_MAKE_UNBOUND;
  if (EQ (symbol, Qlocal_predicate))
    return MAGIC_HANDLER_LOCAL_PREDICATE;
  if (EQ (symbol, Qmake_local))
    return MAGIC_HANDLER_MAKE_LOCAL;
  signal_simple_error ("Unrecognized symbol value handler type", symbol);
  abort ();
  return MAGIC_HANDLER_MAX;
}

static enum lisp_magic_handler
handler_type_from_function_symbol (Lisp_Object funsym, int abort_if_not_found)
{
  if (EQ (funsym, Qsymbol_value)
      || EQ (funsym, Qdefault_value)
      || EQ (funsym, Qsymbol_value_in_buffer)
      || EQ (funsym, Qsymbol_value_in_console))
    return MAGIC_HANDLER_GET_VALUE;

  if (EQ (funsym, Qset)
      || EQ (funsym, Qset_default))
    return MAGIC_HANDLER_SET_VALUE;

  if (EQ (funsym, Qboundp)
      || EQ (funsym, Qglobally_boundp)
      || EQ (funsym, Qdefault_boundp))
    return MAGIC_HANDLER_BOUND_PREDICATE;

  if (EQ (funsym, Qmakunbound))
    return MAGIC_HANDLER_MAKE_UNBOUND;

  if (EQ (funsym, Qlocal_variable_p))
    return MAGIC_HANDLER_LOCAL_PREDICATE;

  if (EQ (funsym, Qmake_variable_buffer_local)
      || EQ (funsym, Qmake_local_variable))
    return MAGIC_HANDLER_MAKE_LOCAL;

  if (abort_if_not_found)
    abort ();
  signal_simple_error ("Unrecognized symbol-value function", funsym);
  return MAGIC_HANDLER_MAX;
}

static int
would_be_magic_handled (Lisp_Object sym, Lisp_Object funsym)
{
  /* does not take into account variable aliasing. */
  Lisp_Object valcontents = XSYMBOL (sym)->value;
  enum lisp_magic_handler slot;

  if (!SYMBOL_VALUE_LISP_MAGIC_P (valcontents))
    return 0;
  slot = handler_type_from_function_symbol (funsym, 1);
  if (slot != MAGIC_HANDLER_SET_VALUE && slot != MAGIC_HANDLER_MAKE_UNBOUND
      && slot != MAGIC_HANDLER_MAKE_LOCAL)
    /* #### temporary kludge because we haven't implemented
       lisp-magic variables completely */
    return 0;
  return !NILP (XSYMBOL_VALUE_LISP_MAGIC (valcontents)->handler[slot]);
}

static Lisp_Object
fetch_value_maybe_past_magic (Lisp_Object sym,
			      Lisp_Object follow_past_lisp_magic)
{
  Lisp_Object value = XSYMBOL (sym)->value;
  if (SYMBOL_VALUE_LISP_MAGIC_P (value)
      && (EQ (follow_past_lisp_magic, Qt)
	  || (!NILP (follow_past_lisp_magic)
	      && !would_be_magic_handled (sym, follow_past_lisp_magic))))
    value = XSYMBOL_VALUE_LISP_MAGIC (value)->shadowed;
  return value;
}

static Lisp_Object *
value_slot_past_magic (Lisp_Object sym)
{
  Lisp_Object *store_pointer = &XSYMBOL (sym)->value;

  if (SYMBOL_VALUE_LISP_MAGIC_P (*store_pointer))
    store_pointer = &XSYMBOL_VALUE_LISP_MAGIC (sym)->shadowed;
  return store_pointer;
}

static Lisp_Object
maybe_call_magic_handler (Lisp_Object sym, Lisp_Object funsym, int nargs, ...)
{
  va_list vargs;
  Lisp_Object args[20]; /* should be enough ... */
  int i;
  enum lisp_magic_handler htype;
  Lisp_Object legerdemain;
  struct symbol_value_lisp_magic *bfwd;

  assert (nargs >= 0 && nargs < 20);
  legerdemain = XSYMBOL (sym)->value;
  assert (SYMBOL_VALUE_LISP_MAGIC_P (legerdemain));
  bfwd = XSYMBOL_VALUE_LISP_MAGIC (legerdemain);

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

  htype = handler_type_from_function_symbol (funsym, 1);
  if (NILP (bfwd->handler[htype]))
    return Qunbound;
  /* #### should be reusing the arglist, not always consing anew.
     Repeated handler invocations should not cause repeated consing.
     Doesn't matter for now, because this is just a quick implementation
     for obsolescence support. */
  return call5 (bfwd->handler[htype], sym, Flist (nargs, args), funsym,
		bfwd->harg[htype], Qnil);
}

DEFUN ("dontusethis-set-symbol-value-handler",
       Fdontusethis_set_symbol_value_handler,
       Sdontusethis_set_symbol_value_handler, 3, 5, 0 /*
Don't you dare use this.
If you do, suffer the wrath of Ben, who is likely to rename
this function (or change the semantics of its arguments) without
pity, thereby invalidating your code.
*/ )
  (variable, handler_type, handler, harg, keep_existing)
    Lisp_Object variable, handler_type, handler, harg, keep_existing;
{
  Lisp_Object valcontents;
  struct symbol_value_lisp_magic *bfwd;
  enum lisp_magic_handler htype;
  int i;

  /* #### WARNING, only some handler types are implemented.  See above.
     Actions of other types will ignore a handler if it's there.

     #### Also, `chain-to-symbol-value-handler' and
     `symbol-function-corresponding-function' are not implemented. */
  CHECK_SYMBOL (variable);
  CHECK_SYMBOL (handler_type);
  htype = decode_magic_handler_type (handler_type);
  valcontents = XSYMBOL (variable)->value;
  if (!SYMBOL_VALUE_LISP_MAGIC_P (valcontents))
    {
      bfwd = alloc_lcrecord (sizeof (struct symbol_value_lisp_magic),
			     lrecord_symbol_value_lisp_magic);
      bfwd->magic.type = SYMVAL_LISP_MAGIC;
      for (i = 0; i < MAGIC_HANDLER_MAX; i++)
	{
	  bfwd->handler[i] = Qnil;
	  bfwd->harg[i] = Qnil;
	}
      bfwd->shadowed = valcontents;
      XSETSYMBOL_VALUE_MAGIC (XSYMBOL (variable)->value, bfwd);
    }
  else
    bfwd = XSYMBOL_VALUE_LISP_MAGIC (valcontents);
  bfwd->handler[htype] = handler;
  bfwd->harg[htype] = harg;

  for (i = 0; i < MAGIC_HANDLER_MAX; i++)
    if (!NILP (bfwd->handler[i]))
      break;

  if (i == MAGIC_HANDLER_MAX)
    /* there are no remaining handlers, so remove the structure. */
    XSYMBOL (variable)->value = bfwd->shadowed;

  return Qnil;
}


/* functions for working with variable aliases.  */

/* Follow the chain of variable aliases for OBJECT.  Return the
   resulting symbol, whose value cell is guaranteed not to be a
   symbol-value-varalias.

   Also maybe follow past symbol-value-lisp-magic -> symbol-value-varalias.
   If FUNSYM is t, always follow in such a case.  If FUNSYM is nil,
   never follow; stop right there.  Otherwise FUNSYM should be a
   recognized symbol-value function symbol; this means, follow
   unless there is a special handler for the named function.

   OK, there is at least one reason why it's necessary for
   FOLLOW-PAST-LISP-MAGIC to be specified correctly: So that we
   can always be sure to catch cyclic variable aliasing.  If we never
   follow past Lisp magic, then if the following is done:

   (defvaralias 'a 'b)
   add some magic behavior to a, but not a "get-value" handler
   (defvaralias 'b 'a)

   then an attempt to retrieve a's or b's value would cause infinite
   looping in `symbol-value'.

   We (of course) can't always follow past Lisp magic, because then
   we make any variable that is lisp-magic -> varalias behave as if
   the lisp-magic is not present at all.
 */

static Lisp_Object
follow_varalias_pointers (Lisp_Object object,
			  Lisp_Object follow_past_lisp_magic)
{
  Lisp_Object tortoise = object; 
  Lisp_Object hare = object;

  /* quick out just in case */
  if (!SYMBOL_VALUE_MAGIC_P (XSYMBOL (object)->value))
    return object;

  /* based off of indirect_function() */
  for (;;)
    {
      Lisp_Object value;

      value = fetch_value_maybe_past_magic (hare, follow_past_lisp_magic);
      if (!SYMBOL_VALUE_VARALIAS_P (value))
	break;
      hare = symbol_value_varalias_aliasee (XSYMBOL_VALUE_VARALIAS (value));
      value = fetch_value_maybe_past_magic (hare, follow_past_lisp_magic);
      if (!SYMBOL_VALUE_VARALIAS_P (value))
	break;
      hare = symbol_value_varalias_aliasee (XSYMBOL_VALUE_VARALIAS (value));

      value = fetch_value_maybe_past_magic (tortoise, follow_past_lisp_magic);
      tortoise = symbol_value_varalias_aliasee
	(XSYMBOL_VALUE_VARALIAS (value));

      if (EQ (hare, tortoise))
	return (Fsignal (Qcyclic_variable_indirection, list1 (object)));
    }

  return hare;
}

DEFUN ("defvaralias", Fdefvaralias, Sdefvaralias, 2, 2, 0 /*
Define a variable as an alias for another variable.
Thenceforth, any operations performed on VARIABLE will actually be
performed on ALIAS.  Both VARIABLE and ALIAS should be symbols.
If ALIAS is nil, remove any aliases for VARIABLE.
ALIAS can itself be aliased, and the chain of variable aliases
will be followed appropriately.
If VARIABLE already has a value, this value will be shadowed
until the alias is removed, at which point it will be restored.
Currently VARIABLE cannot be a built-in variable, a variable that
has a buffer-local value in any buffer, or the symbols nil or t.
(ALIAS, however, can be any type of variable.)
*/ )
  (variable, alias)
    Lisp_Object variable, alias;
{
  struct symbol_value_varalias *bfwd;
  Lisp_Object valcontents;

  CHECK_SYMBOL (variable);
  reject_constant_symbols (variable, Qunbound, 0, Qt);

  valcontents = XSYMBOL (variable)->value;

  if (NILP (alias))
    {
      if (SYMBOL_VALUE_VARALIAS_P (valcontents))
	{
	  XSYMBOL (variable)->value =
	    symbol_value_varalias_shadowed
	      (XSYMBOL_VALUE_VARALIAS (valcontents));
	}
      return Qnil;
    }
      
  CHECK_SYMBOL (alias);
  if (SYMBOL_VALUE_VARALIAS_P (valcontents))
    {
      /* transmogrify */
      XSYMBOL_VALUE_VARALIAS (valcontents)->aliasee = alias;
      return Qnil;
    }

  if (SYMBOL_VALUE_MAGIC_P (valcontents)
      && !UNBOUNDP (valcontents))
    signal_simple_error ("Variable is magic and cannot be aliased", variable);
  reject_constant_symbols (variable, Qunbound, 0, Qt);

  bfwd = alloc_lcrecord (sizeof (struct symbol_value_varalias),
			 lrecord_symbol_value_varalias);
  bfwd->magic.type = SYMVAL_VARALIAS;
  bfwd->aliasee = alias;
  bfwd->shadowed = valcontents;
  
  XSETSYMBOL_VALUE_MAGIC (valcontents, bfwd);
  XSYMBOL (variable)->value = valcontents;
  return Qnil;
}

DEFUN ("variable-alias", Fvariable_alias, Svariable_alias, 1, 2, 0 /*
If VARIABLE is aliased to another variable, return that variable.
VARIABLE should be a symbol.  If VARIABLE is not aliased, return nil.
Variable aliases are created with `defvaralias'.  See also
`indirect-variable'.
*/ )
  (variable, follow_past_lisp_magic)
    Lisp_Object variable, follow_past_lisp_magic;
{
  Lisp_Object valcontents;

  CHECK_SYMBOL (variable);
  if (!NILP (follow_past_lisp_magic) && !EQ (follow_past_lisp_magic, Qt))
    {
      CHECK_SYMBOL (follow_past_lisp_magic);
      (void) handler_type_from_function_symbol (follow_past_lisp_magic, 0);
    }

  valcontents = fetch_value_maybe_past_magic (variable,
					      follow_past_lisp_magic);

  if (SYMBOL_VALUE_VARALIAS_P (valcontents))
    return symbol_value_varalias_aliasee
      (XSYMBOL_VALUE_VARALIAS (valcontents));
  else
    return Qnil;
}

DEFUN ("indirect-variable", Findirect_variable, Sindirect_variable, 1, 2, 0 /*
Return the variable at the end of OBJECT's variable-alias chain.
If OBJECT is a symbol, follow all variable aliases and return
the final (non-aliased) symbol.  Variable aliases are created with
the function `defvaralias'.
If OBJECT is not a symbol, just return it.
Signal a cyclic-variable-indirection error if there is a loop in the
variable chain of symbols.
*/ )
  (object, follow_past_lisp_magic)
    Lisp_Object object, follow_past_lisp_magic;
{
  if (!SYMBOLP (object))
    return object;
  if (!NILP (follow_past_lisp_magic) && !EQ (follow_past_lisp_magic, Qt))
    {
      CHECK_SYMBOL (follow_past_lisp_magic);
      (void) handler_type_from_function_symbol (follow_past_lisp_magic, 0);
    }
  return follow_varalias_pointers (object, follow_past_lisp_magic);
}


/************************************************************************/
/*                            initialization                            */
/************************************************************************/

/* A dumped XEmacs image has a lot more than 1511 symbols.  Last
   estimate was that there were actually around 6300.  So let's try
   making this bigger and see if we get better hashing behavior. */
#define OBARRAY_SIZE 16411

#ifndef Qzero
Lisp_Object Qzero;
#endif

/* some losing systems can't have static vars at function scope... */
static struct symbol_value_magic guts_of_unbound_marker =
  { { { lrecord_symbol_value_forward }, 0, 69}, SYMVAL_UNBOUND_MARKER };

void
init_symbols_once_early (void)
{
  Qnil = Fmake_symbol (make_pure_pname ((CONST Bufbyte *) "nil", 3, 1));
  /* Bootstrapping problem: Qnil isn't set when make_pure_pname is
     called the first time. */
  XSYMBOL (Qnil)->name->plist = Qnil;
  XSYMBOL (Qnil)->value = Qnil; /* Nihil ex nihil */
  XSYMBOL (Qnil)->plist = Qnil;
  
#ifndef Qzero
  Qzero = make_int (0);	/* Only used if Lisp_Object is a union type */
#endif
  
  Vobarray = make_vector (OBARRAY_SIZE, Qzero);
  initial_obarray = Vobarray;
  staticpro (&initial_obarray);
  /* Intern nil in the obarray */
  {
    /* These locals are to kludge around a pyramid compiler bug. */
    int hash;
    Lisp_Object *tem;
    
    hash = hash_string (string_data (XSYMBOL (Qnil)->name), 3);
    /* Separate statement here to avoid VAXC bug. */
    hash %= OBARRAY_SIZE;
    tem = &vector_data (XVECTOR (Vobarray))[hash];
    *tem = Qnil;
  }
  
  {
    /* Required to get around a GCC syntax error on certain
       architectures */
    struct symbol_value_magic *tem = &guts_of_unbound_marker;
    
    XSETSYMBOL_VALUE_MAGIC (Qunbound, tem);
  }
  if ((CONST void *) XPNTR (Qunbound) !=
      (CONST void *)&guts_of_unbound_marker)
    {
      /* This might happen on DATA_SEG_BITS machines. */
      /* abort (); */
      /* Can't represent a pointer to constant C data using a Lisp_Object.
	 So heap-allocate it. */
      struct symbol_value_magic *urk = xmalloc (sizeof (*urk));
      memcpy (urk, &guts_of_unbound_marker, sizeof (*urk));
      XSETSYMBOL_VALUE_MAGIC (Qunbound, urk);
    }
  
  XSYMBOL (Qnil)->function = Qunbound;
  
  defsymbol (&Qt, "t");
  XSYMBOL (Qt)->value = Qt;	/* Veritas aetera */
  Vquit_flag = Qnil;
}

void
defsymbol (Lisp_Object *location, CONST char *name)
{
  *location = Fintern (make_pure_pname ((CONST Bufbyte *) name,
                                        strlen (name), 1),
		       Qnil);
  staticpro (location);
}

void
defkeyword (Lisp_Object *location, CONST char *name)
{
  defsymbol (location, name);
  Fset (*location, *location);
}

void
defsubr (struct Lisp_Subr *subr)
{
  Lisp_Object sym = intern (subr_name (subr));

  /* Check that nobody spazzed */
  if (subr->max_args != MANY && subr->max_args != UNEVALLED)
    {
      if (subr->max_args > SUBR_MAX_ARGS /* Need to fix eval.c if so */
	  || subr->max_args < subr->min_args)
	abort ();
    }
  if (subr->min_args < 0 || subr->min_args > SUBR_MAX_ARGS)
    abort ();

  if (!UNBOUNDP (XSYMBOL (sym)->function)) abort ();

  XSETSUBR (XSYMBOL (sym)->function, subr);
}

void
deferror (Lisp_Object *symbol, CONST char *name, CONST char *messuhhj,
	  Lisp_Object inherits_from)
{
  Lisp_Object conds;
  defsymbol (symbol, name);

  assert (SYMBOLP (inherits_from));
  conds = Fget (inherits_from, Qerror_conditions, Qnil);
  pure_put (*symbol, Qerror_conditions, Fcons (*symbol, conds));
  /* NOT build_translated_string ().  This function is called at load time
     and the string needs to get translated at run time.  (This happens
     in the function (display-error) in cmdloop.el.) */
  pure_put (*symbol, Qerror_message, build_string (messuhhj));
}

void
syms_of_symbols (void)
{
  defsymbol (&Qvariable_documentation, "variable-documentation");
  defsymbol (&Qvariable_domain, "variable-domain");	/* I18N3 */
  defsymbol (&Qad_advice_info, "ad-advice-info");
  defsymbol (&Qad_activate, "ad-activate");

  defsymbol (&Qget_value, "get-value");
  defsymbol (&Qset_value, "set-value");
  defsymbol (&Qbound_predicate, "bound-predicate");
  defsymbol (&Qmake_unbound, "make-unbound");
  defsymbol (&Qlocal_predicate, "local-predicate");
  defsymbol (&Qmake_local, "make-local");

  defsymbol (&Qboundp, "boundp");
  defsymbol (&Qfboundp, "fboundp");
  defsymbol (&Qglobally_boundp, "globally-boundp");
  defsymbol (&Qmakunbound, "makunbound");
  defsymbol (&Qsymbol_value, "symbol-value");
  defsymbol (&Qset, "set");
  defsymbol (&Qdefault_boundp, "default-boundp");
  defsymbol (&Qdefault_value, "default-value");
  defsymbol (&Qset_default, "set-default");
  defsymbol (&Qmake_variable_buffer_local, "make-variable-buffer-local");
  defsymbol (&Qmake_local_variable, "make-local-variable");
  defsymbol (&Qkill_local_variable, "kill-local-variable");
  defsymbol (&Qkill_console_local_variable, "kill-console-local-variable");
  defsymbol (&Qsymbol_value_in_buffer, "symbol-value-in-buffer");
  defsymbol (&Qsymbol_value_in_console, "symbol-value-in-console");
  defsymbol (&Qlocal_variable_p, "local-variable-p");

  defsymbol (&Qconst_integer, "const-integer");
  defsymbol (&Qconst_boolean, "const-boolean");
  defsymbol (&Qconst_object, "const-object");
  defsymbol (&Qconst_specifier, "const-specifier");
  defsymbol (&Qdefault_buffer, "default-buffer");
  defsymbol (&Qcurrent_buffer, "current-buffer");
  defsymbol (&Qconst_current_buffer, "const-current-buffer");
  defsymbol (&Qdefault_console, "default-console");
  defsymbol (&Qselected_console, "selected-console");
  defsymbol (&Qconst_selected_console, "const-selected-console");

  defsubr (&Sintern);
  defsubr (&Sintern_soft);
  defsubr (&Sunintern);
  defsubr (&Smapatoms);
  defsubr (&Sapropos_internal);

  defsubr (&Ssymbol_function);
  defsubr (&Ssymbol_plist);
  defsubr (&Ssymbol_name);
  defsubr (&Smakunbound);
  defsubr (&Sfmakunbound);
  defsubr (&Sboundp);
  defsubr (&Sglobally_boundp);
  defsubr (&Sfboundp);
  defsubr (&Sfset);
  defsubr (&Sdefine_function);
  defsubr (&Ssetplist);
  defsubr (&Ssymbol_value_in_buffer);
  defsubr (&Ssymbol_value_in_console);
  defsubr (&Sbuilt_in_variable_type);
  defsubr (&Ssymbol_value);
  defsubr (&Sset);
  defsubr (&Sdefault_boundp);
  defsubr (&Sdefault_value);
  defsubr (&Sset_default);
  defsubr (&Ssetq_default);
  defsubr (&Smake_variable_buffer_local);
  defsubr (&Smake_local_variable);
  defsubr (&Skill_local_variable);
  defsubr (&Skill_console_local_variable);
  defsubr (&Slocal_variable_p);
  defsubr (&Sdefvaralias);
  defsubr (&Svariable_alias);
  defsubr (&Sindirect_variable);
  defsubr (&Sdontusethis_set_symbol_value_handler);
}

/* Create and initialize a variable whose value is forwarded to C data */
void
defvar_mumble (CONST char *namestring, 
	       CONST void *magic, int sizeof_magic)
{
  Lisp_Object kludge;
  Lisp_Object sym = Fintern (make_pure_pname ((CONST Bufbyte *) namestring,
					      strlen (namestring),
					      1),
			     Qnil);

  /* Check that magic points somewhere we can represent as a Lisp pointer */
  XSETOBJ (kludge, Lisp_Record, magic);
  if (magic != (CONST void *) XPNTR (kludge))
    {
      /* This might happen on DATA_SEG_BITS machines. */
      /* abort (); */
      /* Copy it to somewhere which is representable. */
      void *f = xmalloc (sizeof_magic);
      memcpy (f, magic, sizeof_magic);
      XSETOBJ (XSYMBOL (sym)->value, Lisp_Record, f);
    }
  else
    XSETOBJ (XSYMBOL (sym)->value, Lisp_Record, magic);
}

void
vars_of_symbols (void)
{
  DEFVAR_LISP ("obarray", &Vobarray /*
Symbol table for use by `intern' and `read'.
It is a vector whose length ought to be prime for best results.
The vector's contents don't make sense if examined from Lisp programs;
to find all the symbols in an obarray, use `mapatoms'.
*/ );
  /* obarray has been initialized long before */
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.