1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
|
/* Primitive operations on Lisp data types for GNU Emacs Lisp interpreter.
Copyright (C) 1985-1986, 1988, 1993-1995, 1997-2018 Free Software
Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs 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 3 of the License, or (at
your option) any later version.
GNU Emacs 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
#include <math.h>
#include <stdio.h>
#include <byteswap.h>
#include <count-one-bits.h>
#include <count-trailing-zeros.h>
#include <intprops.h>
#include "lisp.h"
#include "puresize.h"
#include "character.h"
#include "buffer.h"
#include "keyboard.h"
#include "process.h"
#include "frame.h"
#include "keymap.h"
static void swap_in_symval_forwarding (struct Lisp_Symbol *,
struct Lisp_Buffer_Local_Value *);
static bool
BOOLFWDP (union Lisp_Fwd *a)
{
return XFWDTYPE (a) == Lisp_Fwd_Bool;
}
static bool
INTFWDP (union Lisp_Fwd *a)
{
return XFWDTYPE (a) == Lisp_Fwd_Int;
}
static bool
KBOARD_OBJFWDP (union Lisp_Fwd *a)
{
return XFWDTYPE (a) == Lisp_Fwd_Kboard_Obj;
}
static bool
OBJFWDP (union Lisp_Fwd *a)
{
return XFWDTYPE (a) == Lisp_Fwd_Obj;
}
static struct Lisp_Boolfwd *
XBOOLFWD (union Lisp_Fwd *a)
{
eassert (BOOLFWDP (a));
return &a->u_boolfwd;
}
static struct Lisp_Kboard_Objfwd *
XKBOARD_OBJFWD (union Lisp_Fwd *a)
{
eassert (KBOARD_OBJFWDP (a));
return &a->u_kboard_objfwd;
}
static struct Lisp_Intfwd *
XINTFWD (union Lisp_Fwd *a)
{
eassert (INTFWDP (a));
return &a->u_intfwd;
}
static struct Lisp_Objfwd *
XOBJFWD (union Lisp_Fwd *a)
{
eassert (OBJFWDP (a));
return &a->u_objfwd;
}
static void
CHECK_SUBR (Lisp_Object x)
{
CHECK_TYPE (SUBRP (x), Qsubrp, x);
}
static void
set_blv_found (struct Lisp_Buffer_Local_Value *blv, int found)
{
eassert (found == !EQ (blv->defcell, blv->valcell));
blv->found = found;
}
static Lisp_Object
blv_value (struct Lisp_Buffer_Local_Value *blv)
{
return XCDR (blv->valcell);
}
static void
set_blv_value (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
{
XSETCDR (blv->valcell, val);
}
static void
set_blv_where (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
{
blv->where = val;
}
static void
set_blv_defcell (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
{
blv->defcell = val;
}
static void
set_blv_valcell (struct Lisp_Buffer_Local_Value *blv, Lisp_Object val)
{
blv->valcell = val;
}
static _Noreturn void
wrong_length_argument (Lisp_Object a1, Lisp_Object a2, Lisp_Object a3)
{
Lisp_Object size1 = make_fixnum (bool_vector_size (a1));
Lisp_Object size2 = make_fixnum (bool_vector_size (a2));
if (NILP (a3))
xsignal2 (Qwrong_length_argument, size1, size2);
else
xsignal3 (Qwrong_length_argument, size1, size2,
make_fixnum (bool_vector_size (a3)));
}
_Noreturn void
wrong_type_argument (register Lisp_Object predicate, register Lisp_Object value)
{
/* If VALUE is not even a valid Lisp object, we'd want to abort here
where we can get a backtrace showing where it came from. We used
to try and do that by checking the tagbits, but nowadays all
tagbits are potentially valid. */
/* if ((unsigned int) XTYPE (value) >= Lisp_Type_Limit)
* emacs_abort (); */
xsignal2 (Qwrong_type_argument, predicate, value);
}
void
pure_write_error (Lisp_Object obj)
{
xsignal2 (Qerror, build_string ("Attempt to modify read-only object"), obj);
}
void
args_out_of_range (Lisp_Object a1, Lisp_Object a2)
{
xsignal2 (Qargs_out_of_range, a1, a2);
}
void
args_out_of_range_3 (Lisp_Object a1, Lisp_Object a2, Lisp_Object a3)
{
xsignal3 (Qargs_out_of_range, a1, a2, a3);
}
void
circular_list (Lisp_Object list)
{
xsignal1 (Qcircular_list, list);
}
/* Data type predicates. */
DEFUN ("eq", Feq, Seq, 2, 2, 0,
doc: /* Return t if the two args are the same Lisp object. */
attributes: const)
(Lisp_Object obj1, Lisp_Object obj2)
{
if (EQ (obj1, obj2))
return Qt;
return Qnil;
}
DEFUN ("null", Fnull, Snull, 1, 1, 0,
doc: /* Return t if OBJECT is nil, and return nil otherwise. */
attributes: const)
(Lisp_Object object)
{
if (NILP (object))
return Qt;
return Qnil;
}
DEFUN ("type-of", Ftype_of, Stype_of, 1, 1, 0,
doc: /* Return a symbol representing the type of OBJECT.
The symbol returned names the object's basic type;
for example, (type-of 1) returns `integer'. */)
(Lisp_Object object)
{
switch (XTYPE (object))
{
case_Lisp_Int:
return Qinteger;
case Lisp_Symbol:
return Qsymbol;
case Lisp_String:
return Qstring;
case Lisp_Cons:
return Qcons;
case Lisp_Misc:
switch (XMISCTYPE (object))
{
case Lisp_Misc_Marker:
return Qmarker;
case Lisp_Misc_Overlay:
return Qoverlay;
case Lisp_Misc_Finalizer:
return Qfinalizer;
#ifdef HAVE_MODULES
case Lisp_Misc_User_Ptr:
return Quser_ptr;
#endif
case Lisp_Misc_Bignum:
return Qinteger;
default:
emacs_abort ();
}
case Lisp_Vectorlike:
switch (PSEUDOVECTOR_TYPE (XVECTOR (object)))
{
case PVEC_NORMAL_VECTOR: return Qvector;
case PVEC_WINDOW_CONFIGURATION: return Qwindow_configuration;
case PVEC_PROCESS: return Qprocess;
case PVEC_WINDOW: return Qwindow;
case PVEC_SUBR: return Qsubr;
case PVEC_COMPILED: return Qcompiled_function;
case PVEC_BUFFER: return Qbuffer;
case PVEC_CHAR_TABLE: return Qchar_table;
case PVEC_BOOL_VECTOR: return Qbool_vector;
case PVEC_FRAME: return Qframe;
case PVEC_HASH_TABLE: return Qhash_table;
case PVEC_FONT:
if (FONT_SPEC_P (object))
return Qfont_spec;
if (FONT_ENTITY_P (object))
return Qfont_entity;
if (FONT_OBJECT_P (object))
return Qfont_object;
else
emacs_abort (); /* return Qfont? */
case PVEC_THREAD: return Qthread;
case PVEC_MUTEX: return Qmutex;
case PVEC_CONDVAR: return Qcondition_variable;
case PVEC_TERMINAL: return Qterminal;
case PVEC_RECORD:
{
Lisp_Object t = AREF (object, 0);
if (RECORDP (t) && 1 < PVSIZE (t))
/* Return the type name field of the class! */
return AREF (t, 1);
else
return t;
}
case PVEC_MODULE_FUNCTION:
return Qmodule_function;
/* "Impossible" cases. */
case PVEC_XWIDGET:
case PVEC_OTHER:
case PVEC_XWIDGET_VIEW:
case PVEC_SUB_CHAR_TABLE:
case PVEC_FREE: ;
}
emacs_abort ();
case Lisp_Float:
return Qfloat;
default:
emacs_abort ();
}
}
DEFUN ("consp", Fconsp, Sconsp, 1, 1, 0,
doc: /* Return t if OBJECT is a cons cell. */
attributes: const)
(Lisp_Object object)
{
if (CONSP (object))
return Qt;
return Qnil;
}
DEFUN ("atom", Fatom, Satom, 1, 1, 0,
doc: /* Return t if OBJECT is not a cons cell. This includes nil. */
attributes: const)
(Lisp_Object object)
{
if (CONSP (object))
return Qnil;
return Qt;
}
DEFUN ("listp", Flistp, Slistp, 1, 1, 0,
doc: /* Return t if OBJECT is a list, that is, a cons cell or nil.
Otherwise, return nil. */
attributes: const)
(Lisp_Object object)
{
if (CONSP (object) || NILP (object))
return Qt;
return Qnil;
}
DEFUN ("nlistp", Fnlistp, Snlistp, 1, 1, 0,
doc: /* Return t if OBJECT is not a list. Lists include nil. */
attributes: const)
(Lisp_Object object)
{
if (CONSP (object) || NILP (object))
return Qnil;
return Qt;
}
DEFUN ("symbolp", Fsymbolp, Ssymbolp, 1, 1, 0,
doc: /* Return t if OBJECT is a symbol. */
attributes: const)
(Lisp_Object object)
{
if (SYMBOLP (object))
return Qt;
return Qnil;
}
/* Define this in C to avoid unnecessarily consing up the symbol
name. */
DEFUN ("keywordp", Fkeywordp, Skeywordp, 1, 1, 0,
doc: /* Return t if OBJECT is a keyword.
This means that it is a symbol with a print name beginning with `:'
interned in the initial obarray. */)
(Lisp_Object object)
{
if (SYMBOLP (object)
&& SREF (SYMBOL_NAME (object), 0) == ':'
&& SYMBOL_INTERNED_IN_INITIAL_OBARRAY_P (object))
return Qt;
return Qnil;
}
DEFUN ("vectorp", Fvectorp, Svectorp, 1, 1, 0,
doc: /* Return t if OBJECT is a vector. */)
(Lisp_Object object)
{
if (VECTORP (object))
return Qt;
return Qnil;
}
DEFUN ("recordp", Frecordp, Srecordp, 1, 1, 0,
doc: /* Return t if OBJECT is a record. */)
(Lisp_Object object)
{
if (RECORDP (object))
return Qt;
return Qnil;
}
DEFUN ("stringp", Fstringp, Sstringp, 1, 1, 0,
doc: /* Return t if OBJECT is a string. */
attributes: const)
(Lisp_Object object)
{
if (STRINGP (object))
return Qt;
return Qnil;
}
DEFUN ("multibyte-string-p", Fmultibyte_string_p, Smultibyte_string_p,
1, 1, 0,
doc: /* Return t if OBJECT is a multibyte string.
Return nil if OBJECT is either a unibyte string, or not a string. */)
(Lisp_Object object)
{
if (STRINGP (object) && STRING_MULTIBYTE (object))
return Qt;
return Qnil;
}
DEFUN ("char-table-p", Fchar_table_p, Schar_table_p, 1, 1, 0,
doc: /* Return t if OBJECT is a char-table. */)
(Lisp_Object object)
{
if (CHAR_TABLE_P (object))
return Qt;
return Qnil;
}
DEFUN ("vector-or-char-table-p", Fvector_or_char_table_p,
Svector_or_char_table_p, 1, 1, 0,
doc: /* Return t if OBJECT is a char-table or vector. */)
(Lisp_Object object)
{
if (VECTORP (object) || CHAR_TABLE_P (object))
return Qt;
return Qnil;
}
DEFUN ("bool-vector-p", Fbool_vector_p, Sbool_vector_p, 1, 1, 0,
doc: /* Return t if OBJECT is a bool-vector. */)
(Lisp_Object object)
{
if (BOOL_VECTOR_P (object))
return Qt;
return Qnil;
}
DEFUN ("arrayp", Farrayp, Sarrayp, 1, 1, 0,
doc: /* Return t if OBJECT is an array (string or vector). */)
(Lisp_Object object)
{
if (ARRAYP (object))
return Qt;
return Qnil;
}
DEFUN ("sequencep", Fsequencep, Ssequencep, 1, 1, 0,
doc: /* Return t if OBJECT is a sequence (list or array). */)
(register Lisp_Object object)
{
if (CONSP (object) || NILP (object) || ARRAYP (object))
return Qt;
return Qnil;
}
DEFUN ("bufferp", Fbufferp, Sbufferp, 1, 1, 0,
doc: /* Return t if OBJECT is an editor buffer. */)
(Lisp_Object object)
{
if (BUFFERP (object))
return Qt;
return Qnil;
}
DEFUN ("markerp", Fmarkerp, Smarkerp, 1, 1, 0,
doc: /* Return t if OBJECT is a marker (editor pointer). */)
(Lisp_Object object)
{
if (MARKERP (object))
return Qt;
return Qnil;
}
#ifdef HAVE_MODULES
DEFUN ("user-ptrp", Fuser_ptrp, Suser_ptrp, 1, 1, 0,
doc: /* Return t if OBJECT is a module user pointer. */)
(Lisp_Object object)
{
if (USER_PTRP (object))
return Qt;
return Qnil;
}
#endif
DEFUN ("subrp", Fsubrp, Ssubrp, 1, 1, 0,
doc: /* Return t if OBJECT is a built-in function. */)
(Lisp_Object object)
{
if (SUBRP (object))
return Qt;
return Qnil;
}
DEFUN ("byte-code-function-p", Fbyte_code_function_p, Sbyte_code_function_p,
1, 1, 0,
doc: /* Return t if OBJECT is a byte-compiled function object. */)
(Lisp_Object object)
{
if (COMPILEDP (object))
return Qt;
return Qnil;
}
DEFUN ("module-function-p", Fmodule_function_p, Smodule_function_p, 1, 1, NULL,
doc: /* Return t if OBJECT is a function loaded from a dynamic module. */
attributes: const)
(Lisp_Object object)
{
return MODULE_FUNCTIONP (object) ? Qt : Qnil;
}
DEFUN ("char-or-string-p", Fchar_or_string_p, Schar_or_string_p, 1, 1, 0,
doc: /* Return t if OBJECT is a character or a string. */
attributes: const)
(register Lisp_Object object)
{
if (CHARACTERP (object) || STRINGP (object))
return Qt;
return Qnil;
}
DEFUN ("integerp", Fintegerp, Sintegerp, 1, 1, 0,
doc: /* Return t if OBJECT is an integer. */
attributes: const)
(Lisp_Object object)
{
if (INTEGERP (object))
return Qt;
return Qnil;
}
DEFUN ("fixnump", Ffixnump, Sfixnump, 1, 1, 0,
doc: /* Return t if OBJECT is an fixnum. */
attributes: const)
(Lisp_Object object)
{
if (FIXNUMP (object))
return Qt;
return Qnil;
}
DEFUN ("integer-or-marker-p", Finteger_or_marker_p, Sinteger_or_marker_p, 1, 1, 0,
doc: /* Return t if OBJECT is an integer or a marker (editor pointer). */)
(register Lisp_Object object)
{
if (MARKERP (object) || INTEGERP (object))
return Qt;
return Qnil;
}
DEFUN ("natnump", Fnatnump, Snatnump, 1, 1, 0,
doc: /* Return t if OBJECT is a nonnegative integer. */
attributes: const)
(Lisp_Object object)
{
if (NATNUMP (object))
return Qt;
return Qnil;
}
DEFUN ("numberp", Fnumberp, Snumberp, 1, 1, 0,
doc: /* Return t if OBJECT is a number (floating point or integer). */
attributes: const)
(Lisp_Object object)
{
if (NUMBERP (object))
return Qt;
else
return Qnil;
}
DEFUN ("number-or-marker-p", Fnumber_or_marker_p,
Snumber_or_marker_p, 1, 1, 0,
doc: /* Return t if OBJECT is a number or a marker. */)
(Lisp_Object object)
{
if (NUMBERP (object) || MARKERP (object))
return Qt;
return Qnil;
}
DEFUN ("floatp", Ffloatp, Sfloatp, 1, 1, 0,
doc: /* Return t if OBJECT is a floating point number. */
attributes: const)
(Lisp_Object object)
{
if (FLOATP (object))
return Qt;
return Qnil;
}
DEFUN ("threadp", Fthreadp, Sthreadp, 1, 1, 0,
doc: /* Return t if OBJECT is a thread. */)
(Lisp_Object object)
{
if (THREADP (object))
return Qt;
return Qnil;
}
DEFUN ("mutexp", Fmutexp, Smutexp, 1, 1, 0,
doc: /* Return t if OBJECT is a mutex. */)
(Lisp_Object object)
{
if (MUTEXP (object))
return Qt;
return Qnil;
}
DEFUN ("condition-variable-p", Fcondition_variable_p, Scondition_variable_p,
1, 1, 0,
doc: /* Return t if OBJECT is a condition variable. */)
(Lisp_Object object)
{
if (CONDVARP (object))
return Qt;
return Qnil;
}
DEFUN ("bignump", Fbignump, Sbignump, 1, 1, 0,
doc: /* Return t if OBJECT is a bignum. */)
(Lisp_Object object)
{
if (BIGNUMP (object))
return Qt;
return Qnil;
}
/* Extract and set components of lists. */
DEFUN ("car", Fcar, Scar, 1, 1, 0,
doc: /* Return the car of LIST. If arg is nil, return nil.
Error if arg is not nil and not a cons cell. See also `car-safe'.
See Info node `(elisp)Cons Cells' for a discussion of related basic
Lisp concepts such as car, cdr, cons cell and list. */)
(register Lisp_Object list)
{
return CAR (list);
}
DEFUN ("car-safe", Fcar_safe, Scar_safe, 1, 1, 0,
doc: /* Return the car of OBJECT if it is a cons cell, or else nil. */)
(Lisp_Object object)
{
return CAR_SAFE (object);
}
DEFUN ("cdr", Fcdr, Scdr, 1, 1, 0,
doc: /* Return the cdr of LIST. If arg is nil, return nil.
Error if arg is not nil and not a cons cell. See also `cdr-safe'.
See Info node `(elisp)Cons Cells' for a discussion of related basic
Lisp concepts such as cdr, car, cons cell and list. */)
(register Lisp_Object list)
{
return CDR (list);
}
DEFUN ("cdr-safe", Fcdr_safe, Scdr_safe, 1, 1, 0,
doc: /* Return the cdr of OBJECT if it is a cons cell, or else nil. */)
(Lisp_Object object)
{
return CDR_SAFE (object);
}
DEFUN ("setcar", Fsetcar, Ssetcar, 2, 2, 0,
doc: /* Set the car of CELL to be NEWCAR. Returns NEWCAR. */)
(register Lisp_Object cell, Lisp_Object newcar)
{
CHECK_CONS (cell);
CHECK_IMPURE (cell, XCONS (cell));
XSETCAR (cell, newcar);
return newcar;
}
DEFUN ("setcdr", Fsetcdr, Ssetcdr, 2, 2, 0,
doc: /* Set the cdr of CELL to be NEWCDR. Returns NEWCDR. */)
(register Lisp_Object cell, Lisp_Object newcdr)
{
CHECK_CONS (cell);
CHECK_IMPURE (cell, XCONS (cell));
XSETCDR (cell, newcdr);
return newcdr;
}
/* Extract and set components of symbols. */
DEFUN ("boundp", Fboundp, Sboundp, 1, 1, 0,
doc: /* Return t if SYMBOL's value is not void.
Note that if `lexical-binding' is in effect, this refers to the
global value outside of any lexical scope. */)
(register Lisp_Object symbol)
{
Lisp_Object valcontents;
struct Lisp_Symbol *sym;
CHECK_SYMBOL (symbol);
sym = XSYMBOL (symbol);
start:
switch (sym->u.s.redirect)
{
case SYMBOL_PLAINVAL: valcontents = SYMBOL_VAL (sym); break;
case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
case SYMBOL_LOCALIZED:
{
struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
if (blv->fwd)
/* In set_internal, we un-forward vars when their value is
set to Qunbound. */
return Qt;
else
{
swap_in_symval_forwarding (sym, blv);
valcontents = blv_value (blv);
}
break;
}
case SYMBOL_FORWARDED:
/* In set_internal, we un-forward vars when their value is
set to Qunbound. */
return Qt;
default: emacs_abort ();
}
return (EQ (valcontents, Qunbound) ? Qnil : Qt);
}
/* It has been previously suggested to make this function an alias for
symbol-function, but upon discussion at Bug#23957, there is a risk
breaking backward compatibility, as some users of fboundp may
expect `t' in particular, rather than any true value. */
DEFUN ("fboundp", Ffboundp, Sfboundp, 1, 1, 0,
doc: /* Return t if SYMBOL's function definition is not void. */)
(Lisp_Object symbol)
{
CHECK_SYMBOL (symbol);
return NILP (XSYMBOL (symbol)->u.s.function) ? Qnil : Qt;
}
DEFUN ("makunbound", Fmakunbound, Smakunbound, 1, 1, 0,
doc: /* Make SYMBOL's value be void.
Return SYMBOL. */)
(register Lisp_Object symbol)
{
CHECK_SYMBOL (symbol);
if (SYMBOL_CONSTANT_P (symbol))
xsignal1 (Qsetting_constant, symbol);
Fset (symbol, Qunbound);
return symbol;
}
DEFUN ("fmakunbound", Ffmakunbound, Sfmakunbound, 1, 1, 0,
doc: /* Make SYMBOL's function definition be nil.
Return SYMBOL. */)
(register Lisp_Object symbol)
{
CHECK_SYMBOL (symbol);
if (NILP (symbol) || EQ (symbol, Qt))
xsignal1 (Qsetting_constant, symbol);
set_symbol_function (symbol, Qnil);
return symbol;
}
DEFUN ("symbol-function", Fsymbol_function, Ssymbol_function, 1, 1, 0,
doc: /* Return SYMBOL's function definition, or nil if that is void. */)
(Lisp_Object symbol)
{
CHECK_SYMBOL (symbol);
return XSYMBOL (symbol)->u.s.function;
}
DEFUN ("symbol-plist", Fsymbol_plist, Ssymbol_plist, 1, 1, 0,
doc: /* Return SYMBOL's property list. */)
(Lisp_Object symbol)
{
CHECK_SYMBOL (symbol);
return XSYMBOL (symbol)->u.s.plist;
}
DEFUN ("symbol-name", Fsymbol_name, Ssymbol_name, 1, 1, 0,
doc: /* Return SYMBOL's name, a string. */)
(register Lisp_Object symbol)
{
register Lisp_Object name;
CHECK_SYMBOL (symbol);
name = SYMBOL_NAME (symbol);
return name;
}
DEFUN ("fset", Ffset, Sfset, 2, 2, 0,
doc: /* Set SYMBOL's function definition to DEFINITION, and return DEFINITION. */)
(register Lisp_Object symbol, Lisp_Object definition)
{
register Lisp_Object function;
CHECK_SYMBOL (symbol);
/* Perhaps not quite the right error signal, but seems good enough. */
if (NILP (symbol))
xsignal1 (Qsetting_constant, symbol);
function = XSYMBOL (symbol)->u.s.function;
if (!NILP (Vautoload_queue) && !NILP (function))
Vautoload_queue = Fcons (Fcons (symbol, function), Vautoload_queue);
if (AUTOLOADP (function))
Fput (symbol, Qautoload, XCDR (function));
/* Convert to eassert or remove after GC bug is found. In the
meantime, check unconditionally, at a slight perf hit. */
if (! valid_lisp_object_p (definition))
emacs_abort ();
set_symbol_function (symbol, definition);
return definition;
}
DEFUN ("defalias", Fdefalias, Sdefalias, 2, 3, 0,
doc: /* Set SYMBOL's function definition to DEFINITION.
Associates the function with the current load file, if any.
The optional third argument DOCSTRING specifies the documentation string
for SYMBOL; if it is omitted or nil, SYMBOL uses the documentation string
determined by DEFINITION.
Internally, this normally uses `fset', but if SYMBOL has a
`defalias-fset-function' property, the associated value is used instead.
The return value is undefined. */)
(register Lisp_Object symbol, Lisp_Object definition, Lisp_Object docstring)
{
CHECK_SYMBOL (symbol);
if (!NILP (Vpurify_flag)
/* If `definition' is a keymap, immutable (and copying) is wrong. */
&& !KEYMAPP (definition))
definition = Fpurecopy (definition);
{
bool autoload = AUTOLOADP (definition);
if (NILP (Vpurify_flag) || !autoload)
{ /* Only add autoload entries after dumping, because the ones before are
not useful and else we get loads of them from the loaddefs.el. */
if (AUTOLOADP (XSYMBOL (symbol)->u.s.function))
/* Remember that the function was already an autoload. */
LOADHIST_ATTACH (Fcons (Qt, symbol));
LOADHIST_ATTACH (Fcons (autoload ? Qautoload : Qdefun, symbol));
}
}
{ /* Handle automatic advice activation. */
Lisp_Object hook = Fget (symbol, Qdefalias_fset_function);
if (!NILP (hook))
call2 (hook, symbol, definition);
else
Ffset (symbol, definition);
}
if (!NILP (docstring))
Fput (symbol, Qfunction_documentation, docstring);
/* We used to return `definition', but now that `defun' and `defmacro' expand
to a call to `defalias', we return `symbol' for backward compatibility
(bug#11686). */
return symbol;
}
DEFUN ("setplist", Fsetplist, Ssetplist, 2, 2, 0,
doc: /* Set SYMBOL's property list to NEWPLIST, and return NEWPLIST. */)
(register Lisp_Object symbol, Lisp_Object newplist)
{
CHECK_SYMBOL (symbol);
set_symbol_plist (symbol, newplist);
return newplist;
}
DEFUN ("subr-arity", Fsubr_arity, Ssubr_arity, 1, 1, 0,
doc: /* Return minimum and maximum number of args allowed for SUBR.
SUBR must be a built-in function.
The returned value is a pair (MIN . MAX). MIN is the minimum number
of args. MAX is the maximum number or the symbol `many', for a
function with `&rest' args, or `unevalled' for a special form. */)
(Lisp_Object subr)
{
short minargs, maxargs;
CHECK_SUBR (subr);
minargs = XSUBR (subr)->min_args;
maxargs = XSUBR (subr)->max_args;
return Fcons (make_fixnum (minargs),
maxargs == MANY ? Qmany
: maxargs == UNEVALLED ? Qunevalled
: make_fixnum (maxargs));
}
DEFUN ("subr-name", Fsubr_name, Ssubr_name, 1, 1, 0,
doc: /* Return name of subroutine SUBR.
SUBR must be a built-in function. */)
(Lisp_Object subr)
{
const char *name;
CHECK_SUBR (subr);
name = XSUBR (subr)->symbol_name;
return build_string (name);
}
DEFUN ("interactive-form", Finteractive_form, Sinteractive_form, 1, 1, 0,
doc: /* Return the interactive form of CMD or nil if none.
If CMD is not a command, the return value is nil.
Value, if non-nil, is a list (interactive SPEC). */)
(Lisp_Object cmd)
{
Lisp_Object fun = indirect_function (cmd); /* Check cycles. */
if (NILP (fun))
return Qnil;
/* Use an `interactive-form' property if present, analogous to the
function-documentation property. */
fun = cmd;
while (SYMBOLP (fun))
{
Lisp_Object tmp = Fget (fun, Qinteractive_form);
if (!NILP (tmp))
return tmp;
else
fun = Fsymbol_function (fun);
}
if (SUBRP (fun))
{
const char *spec = XSUBR (fun)->intspec;
if (spec)
return list2 (Qinteractive,
(*spec != '(') ? build_string (spec) :
Fcar (Fread_from_string (build_string (spec), Qnil, Qnil)));
}
else if (COMPILEDP (fun))
{
if (PVSIZE (fun) > COMPILED_INTERACTIVE)
return list2 (Qinteractive, AREF (fun, COMPILED_INTERACTIVE));
}
else if (AUTOLOADP (fun))
return Finteractive_form (Fautoload_do_load (fun, cmd, Qnil));
else if (CONSP (fun))
{
Lisp_Object funcar = XCAR (fun);
if (EQ (funcar, Qclosure))
return Fassq (Qinteractive, Fcdr (Fcdr (XCDR (fun))));
else if (EQ (funcar, Qlambda))
return Fassq (Qinteractive, Fcdr (XCDR (fun)));
}
return Qnil;
}
/***********************************************************************
Getting and Setting Values of Symbols
***********************************************************************/
/* Return the symbol holding SYMBOL's value. Signal
`cyclic-variable-indirection' if SYMBOL's chain of variable
indirections contains a loop. */
struct Lisp_Symbol *
indirect_variable (struct Lisp_Symbol *symbol)
{
struct Lisp_Symbol *tortoise, *hare;
hare = tortoise = symbol;
while (hare->u.s.redirect == SYMBOL_VARALIAS)
{
hare = SYMBOL_ALIAS (hare);
if (hare->u.s.redirect != SYMBOL_VARALIAS)
break;
hare = SYMBOL_ALIAS (hare);
tortoise = SYMBOL_ALIAS (tortoise);
if (hare == tortoise)
{
Lisp_Object tem;
XSETSYMBOL (tem, symbol);
xsignal1 (Qcyclic_variable_indirection, tem);
}
}
return hare;
}
DEFUN ("indirect-variable", Findirect_variable, Sindirect_variable, 1, 1, 0,
doc: /* Return the variable at the end of OBJECT's variable chain.
If OBJECT is a symbol, follow its variable indirections (if any), and
return the variable at the end of the chain of aliases. See Info node
`(elisp)Variable Aliases'.
If OBJECT is not a symbol, just return it. If there is a loop in the
chain of aliases, signal a `cyclic-variable-indirection' error. */)
(Lisp_Object object)
{
if (SYMBOLP (object))
{
struct Lisp_Symbol *sym = indirect_variable (XSYMBOL (object));
XSETSYMBOL (object, sym);
}
return object;
}
/* Given the raw contents of a symbol value cell,
return the Lisp value of the symbol.
This does not handle buffer-local variables; use
swap_in_symval_forwarding for that. */
Lisp_Object
do_symval_forwarding (register union Lisp_Fwd *valcontents)
{
register Lisp_Object val;
switch (XFWDTYPE (valcontents))
{
case Lisp_Fwd_Int:
XSETINT (val, *XINTFWD (valcontents)->intvar);
return val;
case Lisp_Fwd_Bool:
return (*XBOOLFWD (valcontents)->boolvar ? Qt : Qnil);
case Lisp_Fwd_Obj:
return *XOBJFWD (valcontents)->objvar;
case Lisp_Fwd_Buffer_Obj:
return per_buffer_value (current_buffer,
XBUFFER_OBJFWD (valcontents)->offset);
case Lisp_Fwd_Kboard_Obj:
/* We used to simply use current_kboard here, but from Lisp
code, its value is often unexpected. It seems nicer to
allow constructions like this to work as intuitively expected:
(with-selected-frame frame
(define-key local-function-map "\eOP" [f1]))
On the other hand, this affects the semantics of
last-command and real-last-command, and people may rely on
that. I took a quick look at the Lisp codebase, and I
don't think anything will break. --lorentey */
return *(Lisp_Object *)(XKBOARD_OBJFWD (valcontents)->offset
+ (char *)FRAME_KBOARD (SELECTED_FRAME ()));
default: emacs_abort ();
}
}
/* Used to signal a user-friendly error when symbol WRONG is
not a member of CHOICE, which should be a list of symbols. */
void
wrong_choice (Lisp_Object choice, Lisp_Object wrong)
{
ptrdiff_t i = 0, len = XINT (Flength (choice));
Lisp_Object obj, *args;
AUTO_STRING (one_of, "One of ");
AUTO_STRING (comma, ", ");
AUTO_STRING (or, " or ");
AUTO_STRING (should_be_specified, " should be specified");
USE_SAFE_ALLOCA;
SAFE_ALLOCA_LISP (args, len * 2 + 1);
args[i++] = one_of;
for (obj = choice; !NILP (obj); obj = XCDR (obj))
{
args[i++] = SYMBOL_NAME (XCAR (obj));
args[i++] = (NILP (XCDR (obj)) ? should_be_specified
: NILP (XCDR (XCDR (obj))) ? or : comma);
}
obj = Fconcat (i, args);
/* No need to call SAFE_FREE, since signaling does that for us. */
(void) sa_count;
xsignal2 (Qerror, obj, wrong);
}
/* Used to signal a user-friendly error if WRONG is not a number or
integer/floating-point number outsize of inclusive MIN..MAX range. */
static void
wrong_range (Lisp_Object min, Lisp_Object max, Lisp_Object wrong)
{
AUTO_STRING (value_should_be_from, "Value should be from ");
AUTO_STRING (to, " to ");
xsignal2 (Qerror,
CALLN (Fconcat, value_should_be_from, Fnumber_to_string (min),
to, Fnumber_to_string (max)),
wrong);
}
/* Store NEWVAL into SYMBOL, where VALCONTENTS is found in the value cell
of SYMBOL. If SYMBOL is buffer-local, VALCONTENTS should be the
buffer-independent contents of the value cell: forwarded just one
step past the buffer-localness.
BUF non-zero means set the value in buffer BUF instead of the
current buffer. This only plays a role for per-buffer variables. */
static void
store_symval_forwarding (union Lisp_Fwd *valcontents, register Lisp_Object newval, struct buffer *buf)
{
switch (XFWDTYPE (valcontents))
{
case Lisp_Fwd_Int:
CHECK_FIXNUM (newval);
*XINTFWD (valcontents)->intvar = XINT (newval);
break;
case Lisp_Fwd_Bool:
*XBOOLFWD (valcontents)->boolvar = !NILP (newval);
break;
case Lisp_Fwd_Obj:
*XOBJFWD (valcontents)->objvar = newval;
/* If this variable is a default for something stored
in the buffer itself, such as default-fill-column,
find the buffers that don't have local values for it
and update them. */
if (XOBJFWD (valcontents)->objvar > (Lisp_Object *) &buffer_defaults
&& XOBJFWD (valcontents)->objvar < (Lisp_Object *) (&buffer_defaults + 1))
{
int offset = ((char *) XOBJFWD (valcontents)->objvar
- (char *) &buffer_defaults);
int idx = PER_BUFFER_IDX (offset);
Lisp_Object tail, buf;
if (idx <= 0)
break;
FOR_EACH_LIVE_BUFFER (tail, buf)
{
struct buffer *b = XBUFFER (buf);
if (! PER_BUFFER_VALUE_P (b, idx))
set_per_buffer_value (b, offset, newval);
}
}
break;
case Lisp_Fwd_Buffer_Obj:
{
int offset = XBUFFER_OBJFWD (valcontents)->offset;
Lisp_Object predicate = XBUFFER_OBJFWD (valcontents)->predicate;
if (!NILP (newval))
{
if (SYMBOLP (predicate))
{
Lisp_Object prop;
if ((prop = Fget (predicate, Qchoice), !NILP (prop)))
{
if (NILP (Fmemq (newval, prop)))
wrong_choice (prop, newval);
}
else if ((prop = Fget (predicate, Qrange), !NILP (prop)))
{
Lisp_Object min = XCAR (prop), max = XCDR (prop);
if (! FIXED_OR_FLOATP (newval)
|| NILP (CALLN (Fleq, min, newval, max)))
wrong_range (min, max, newval);
}
else if (FUNCTIONP (predicate))
{
if (NILP (call1 (predicate, newval)))
wrong_type_argument (predicate, newval);
}
}
}
if (buf == NULL)
buf = current_buffer;
set_per_buffer_value (buf, offset, newval);
}
break;
case Lisp_Fwd_Kboard_Obj:
{
char *base = (char *) FRAME_KBOARD (SELECTED_FRAME ());
char *p = base + XKBOARD_OBJFWD (valcontents)->offset;
*(Lisp_Object *) p = newval;
}
break;
default:
emacs_abort (); /* goto def; */
}
}
/* Set up SYMBOL to refer to its global binding. This makes it safe
to alter the status of other bindings. BEWARE: this may be called
during the mark phase of GC, where we assume that Lisp_Object slots
of BLV are marked after this function has changed them. */
void
swap_in_global_binding (struct Lisp_Symbol *symbol)
{
struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (symbol);
/* Unload the previously loaded binding. */
if (blv->fwd)
set_blv_value (blv, do_symval_forwarding (blv->fwd));
/* Select the global binding in the symbol. */
set_blv_valcell (blv, blv->defcell);
if (blv->fwd)
store_symval_forwarding (blv->fwd, XCDR (blv->defcell), NULL);
/* Indicate that the global binding is set up now. */
set_blv_where (blv, Qnil);
set_blv_found (blv, false);
}
/* Set up the buffer-local symbol SYMBOL for validity in the current buffer.
VALCONTENTS is the contents of its value cell,
which points to a struct Lisp_Buffer_Local_Value.
Return the value forwarded one step past the buffer-local stage.
This could be another forwarding pointer. */
static void
swap_in_symval_forwarding (struct Lisp_Symbol *symbol, struct Lisp_Buffer_Local_Value *blv)
{
register Lisp_Object tem1;
eassert (blv == SYMBOL_BLV (symbol));
tem1 = blv->where;
if (NILP (tem1)
|| current_buffer != XBUFFER (tem1))
{
/* Unload the previously loaded binding. */
tem1 = blv->valcell;
if (blv->fwd)
set_blv_value (blv, do_symval_forwarding (blv->fwd));
/* Choose the new binding. */
{
Lisp_Object var;
XSETSYMBOL (var, symbol);
tem1 = assq_no_quit (var, BVAR (current_buffer, local_var_alist));
set_blv_where (blv, Fcurrent_buffer ());
}
if (!(blv->found = !NILP (tem1)))
tem1 = blv->defcell;
/* Load the new binding. */
set_blv_valcell (blv, tem1);
if (blv->fwd)
store_symval_forwarding (blv->fwd, blv_value (blv), NULL);
}
}
/* Find the value of a symbol, returning Qunbound if it's not bound.
This is helpful for code which just wants to get a variable's value
if it has one, without signaling an error.
Note that it must not be possible to quit
within this function. Great care is required for this. */
Lisp_Object
find_symbol_value (Lisp_Object symbol)
{
struct Lisp_Symbol *sym;
CHECK_SYMBOL (symbol);
sym = XSYMBOL (symbol);
start:
switch (sym->u.s.redirect)
{
case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
case SYMBOL_PLAINVAL: return SYMBOL_VAL (sym);
case SYMBOL_LOCALIZED:
{
struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
swap_in_symval_forwarding (sym, blv);
return blv->fwd ? do_symval_forwarding (blv->fwd) : blv_value (blv);
}
case SYMBOL_FORWARDED:
return do_symval_forwarding (SYMBOL_FWD (sym));
default: emacs_abort ();
}
}
DEFUN ("symbol-value", Fsymbol_value, Ssymbol_value, 1, 1, 0,
doc: /* Return SYMBOL's value. Error if that is void.
Note that if `lexical-binding' is in effect, this returns the
global value outside of any lexical scope. */)
(Lisp_Object symbol)
{
Lisp_Object val;
val = find_symbol_value (symbol);
if (!EQ (val, Qunbound))
return val;
xsignal1 (Qvoid_variable, symbol);
}
DEFUN ("set", Fset, Sset, 2, 2, 0,
doc: /* Set SYMBOL's value to NEWVAL, and return NEWVAL. */)
(register Lisp_Object symbol, Lisp_Object newval)
{
set_internal (symbol, newval, Qnil, SET_INTERNAL_SET);
return newval;
}
/* Store the value NEWVAL into SYMBOL.
If buffer-locality is an issue, WHERE specifies which context to use.
(nil stands for the current buffer/frame).
If BINDFLAG is SET_INTERNAL_SET, then if this symbol is supposed to
become local in every buffer where it is set, then we make it
local. If BINDFLAG is SET_INTERNAL_BIND or SET_INTERNAL_UNBIND, we
don't do that. */
void
set_internal (Lisp_Object symbol, Lisp_Object newval, Lisp_Object where,
enum Set_Internal_Bind bindflag)
{
bool voide = EQ (newval, Qunbound);
struct Lisp_Symbol *sym;
Lisp_Object tem1;
/* If restoring in a dead buffer, do nothing. */
/* if (BUFFERP (where) && NILP (XBUFFER (where)->name))
return; */
CHECK_SYMBOL (symbol);
sym = XSYMBOL (symbol);
switch (sym->u.s.trapped_write)
{
case SYMBOL_NOWRITE:
if (NILP (Fkeywordp (symbol))
|| !EQ (newval, Fsymbol_value (symbol)))
xsignal1 (Qsetting_constant, symbol);
else
/* Allow setting keywords to their own value. */
return;
case SYMBOL_TRAPPED_WRITE:
/* Setting due to thread-switching doesn't count. */
if (bindflag != SET_INTERNAL_THREAD_SWITCH)
notify_variable_watchers (symbol, voide? Qnil : newval,
(bindflag == SET_INTERNAL_BIND? Qlet :
bindflag == SET_INTERNAL_UNBIND? Qunlet :
voide? Qmakunbound : Qset),
where);
/* FALLTHROUGH! */
case SYMBOL_UNTRAPPED_WRITE:
break;
default: emacs_abort ();
}
start:
switch (sym->u.s.redirect)
{
case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
case SYMBOL_PLAINVAL: SET_SYMBOL_VAL (sym , newval); return;
case SYMBOL_LOCALIZED:
{
struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
if (NILP (where))
XSETBUFFER (where, current_buffer);
/* If the current buffer is not the buffer whose binding is
loaded, or if it's a Lisp_Buffer_Local_Value and
the default binding is loaded, the loaded binding may be the
wrong one. */
if (!EQ (blv->where, where)
/* Also unload a global binding (if the var is local_if_set). */
|| (EQ (blv->valcell, blv->defcell)))
{
/* The currently loaded binding is not necessarily valid.
We need to unload it, and choose a new binding. */
/* Write out `realvalue' to the old loaded binding. */
if (blv->fwd)
set_blv_value (blv, do_symval_forwarding (blv->fwd));
/* Find the new binding. */
XSETSYMBOL (symbol, sym); /* May have changed via aliasing. */
tem1 = assq_no_quit (symbol,
BVAR (XBUFFER (where), local_var_alist));
set_blv_where (blv, where);
blv->found = true;
if (NILP (tem1))
{
/* This buffer still sees the default value. */
/* If the variable is a Lisp_Some_Buffer_Local_Value,
or if this is `let' rather than `set',
make CURRENT-ALIST-ELEMENT point to itself,
indicating that we're seeing the default value.
Likewise if the variable has been let-bound
in the current buffer. */
if (bindflag || !blv->local_if_set
|| let_shadows_buffer_binding_p (sym))
{
blv->found = false;
tem1 = blv->defcell;
}
/* If it's a local_if_set, being set not bound,
and we're not within a let that was made for this buffer,
create a new buffer-local binding for the variable.
That means, give this buffer a new assoc for a local value
and load that binding. */
else
{
tem1 = Fcons (symbol, XCDR (blv->defcell));
bset_local_var_alist
(XBUFFER (where),
Fcons (tem1, BVAR (XBUFFER (where), local_var_alist)));
}
}
/* Record which binding is now loaded. */
set_blv_valcell (blv, tem1);
}
/* Store the new value in the cons cell. */
set_blv_value (blv, newval);
if (blv->fwd)
{
if (voide)
/* If storing void (making the symbol void), forward only through
buffer-local indicator, not through Lisp_Objfwd, etc. */
blv->fwd = NULL;
else
store_symval_forwarding (blv->fwd, newval,
BUFFERP (where)
? XBUFFER (where) : current_buffer);
}
break;
}
case SYMBOL_FORWARDED:
{
struct buffer *buf
= BUFFERP (where) ? XBUFFER (where) : current_buffer;
union Lisp_Fwd *innercontents = SYMBOL_FWD (sym);
if (BUFFER_OBJFWDP (innercontents))
{
int offset = XBUFFER_OBJFWD (innercontents)->offset;
int idx = PER_BUFFER_IDX (offset);
if (idx > 0
&& bindflag == SET_INTERNAL_SET
&& !let_shadows_buffer_binding_p (sym))
SET_PER_BUFFER_VALUE_P (buf, idx, 1);
}
if (voide)
{ /* If storing void (making the symbol void), forward only through
buffer-local indicator, not through Lisp_Objfwd, etc. */
sym->u.s.redirect = SYMBOL_PLAINVAL;
SET_SYMBOL_VAL (sym, newval);
}
else
store_symval_forwarding (/* sym, */ innercontents, newval, buf);
break;
}
default: emacs_abort ();
}
return;
}
static void
set_symbol_trapped_write (Lisp_Object symbol, enum symbol_trapped_write trap)
{
struct Lisp_Symbol *sym = XSYMBOL (symbol);
if (sym->u.s.trapped_write == SYMBOL_NOWRITE)
xsignal1 (Qtrapping_constant, symbol);
sym->u.s.trapped_write = trap;
}
static void
restore_symbol_trapped_write (Lisp_Object symbol)
{
set_symbol_trapped_write (symbol, SYMBOL_TRAPPED_WRITE);
}
static void
harmonize_variable_watchers (Lisp_Object alias, Lisp_Object base_variable)
{
if (!EQ (base_variable, alias)
&& EQ (base_variable, Findirect_variable (alias)))
set_symbol_trapped_write
(alias, XSYMBOL (base_variable)->u.s.trapped_write);
}
DEFUN ("add-variable-watcher", Fadd_variable_watcher, Sadd_variable_watcher,
2, 2, 0,
doc: /* Cause WATCH-FUNCTION to be called when SYMBOL is set.
It will be called with 4 arguments: (SYMBOL NEWVAL OPERATION WHERE).
SYMBOL is the variable being changed.
NEWVAL is the value it will be changed to.
OPERATION is a symbol representing the kind of change, one of: `set',
`let', `unlet', `makunbound', and `defvaralias'.
WHERE is a buffer if the buffer-local value of the variable is being
changed, nil otherwise.
All writes to aliases of SYMBOL will call WATCH-FUNCTION too. */)
(Lisp_Object symbol, Lisp_Object watch_function)
{
symbol = Findirect_variable (symbol);
set_symbol_trapped_write (symbol, SYMBOL_TRAPPED_WRITE);
map_obarray (Vobarray, harmonize_variable_watchers, symbol);
Lisp_Object watchers = Fget (symbol, Qwatchers);
Lisp_Object member = Fmember (watch_function, watchers);
if (NILP (member))
Fput (symbol, Qwatchers, Fcons (watch_function, watchers));
return Qnil;
}
DEFUN ("remove-variable-watcher", Fremove_variable_watcher, Sremove_variable_watcher,
2, 2, 0,
doc: /* Undo the effect of `add-variable-watcher'.
Remove WATCH-FUNCTION from the list of functions to be called when
SYMBOL (or its aliases) are set. */)
(Lisp_Object symbol, Lisp_Object watch_function)
{
symbol = Findirect_variable (symbol);
Lisp_Object watchers = Fget (symbol, Qwatchers);
watchers = Fdelete (watch_function, watchers);
if (NILP (watchers))
{
set_symbol_trapped_write (symbol, SYMBOL_UNTRAPPED_WRITE);
map_obarray (Vobarray, harmonize_variable_watchers, symbol);
}
Fput (symbol, Qwatchers, watchers);
return Qnil;
}
DEFUN ("get-variable-watchers", Fget_variable_watchers, Sget_variable_watchers,
1, 1, 0,
doc: /* Return a list of SYMBOL's active watchers. */)
(Lisp_Object symbol)
{
return (SYMBOL_TRAPPED_WRITE_P (symbol) == SYMBOL_TRAPPED_WRITE)
? Fget (Findirect_variable (symbol), Qwatchers)
: Qnil;
}
void
notify_variable_watchers (Lisp_Object symbol,
Lisp_Object newval,
Lisp_Object operation,
Lisp_Object where)
{
symbol = Findirect_variable (symbol);
ptrdiff_t count = SPECPDL_INDEX ();
record_unwind_protect (restore_symbol_trapped_write, symbol);
/* Avoid recursion. */
set_symbol_trapped_write (symbol, SYMBOL_UNTRAPPED_WRITE);
if (NILP (where)
&& !EQ (operation, Qset_default) && !EQ (operation, Qmakunbound)
&& !NILP (Flocal_variable_if_set_p (symbol, Fcurrent_buffer ())))
{
XSETBUFFER (where, current_buffer);
}
if (EQ (operation, Qset_default))
operation = Qset;
for (Lisp_Object watchers = Fget (symbol, Qwatchers);
CONSP (watchers);
watchers = XCDR (watchers))
{
Lisp_Object watcher = XCAR (watchers);
/* Call subr directly to avoid gc. */
if (SUBRP (watcher))
{
Lisp_Object args[] = { symbol, newval, operation, where };
funcall_subr (XSUBR (watcher), ARRAYELTS (args), args);
}
else
CALLN (Ffuncall, watcher, symbol, newval, operation, where);
}
unbind_to (count, Qnil);
}
/* Access or set a buffer-local symbol's default value. */
/* Return the default value of SYMBOL, but don't check for voidness.
Return Qunbound if it is void. */
static Lisp_Object
default_value (Lisp_Object symbol)
{
struct Lisp_Symbol *sym;
CHECK_SYMBOL (symbol);
sym = XSYMBOL (symbol);
start:
switch (sym->u.s.redirect)
{
case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
case SYMBOL_PLAINVAL: return SYMBOL_VAL (sym);
case SYMBOL_LOCALIZED:
{
/* 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 `realvalue' slot may be more up to date, since
ordinary setq stores just that slot. So use that. */
struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
if (blv->fwd && EQ (blv->valcell, blv->defcell))
return do_symval_forwarding (blv->fwd);
else
return XCDR (blv->defcell);
}
case SYMBOL_FORWARDED:
{
union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
/* For a built-in buffer-local variable, get the default value
rather than letting do_symval_forwarding get the current value. */
if (BUFFER_OBJFWDP (valcontents))
{
int offset = XBUFFER_OBJFWD (valcontents)->offset;
if (PER_BUFFER_IDX (offset) != 0)
return per_buffer_default (offset);
}
/* For other variables, get the current value. */
return do_symval_forwarding (valcontents);
}
default: emacs_abort ();
}
}
DEFUN ("default-boundp", Fdefault_boundp, Sdefault_boundp, 1, 1, 0,
doc: /* 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. */)
(Lisp_Object symbol)
{
register Lisp_Object value;
value = default_value (symbol);
return (EQ (value, Qunbound) ? Qnil : Qt);
}
DEFUN ("default-value", Fdefault_value, Sdefault_value, 1, 1, 0,
doc: /* 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. */)
(Lisp_Object symbol)
{
Lisp_Object value = default_value (symbol);
if (!EQ (value, Qunbound))
return value;
xsignal1 (Qvoid_variable, symbol);
}
void
set_default_internal (Lisp_Object symbol, Lisp_Object value,
enum Set_Internal_Bind bindflag)
{
struct Lisp_Symbol *sym;
CHECK_SYMBOL (symbol);
sym = XSYMBOL (symbol);
switch (sym->u.s.trapped_write)
{
case SYMBOL_NOWRITE:
if (NILP (Fkeywordp (symbol))
|| !EQ (value, Fsymbol_value (symbol)))
xsignal1 (Qsetting_constant, symbol);
else
/* Allow setting keywords to their own value. */
return;
case SYMBOL_TRAPPED_WRITE:
/* Don't notify here if we're going to call Fset anyway. */
if (sym->u.s.redirect != SYMBOL_PLAINVAL
/* Setting due to thread switching doesn't count. */
&& bindflag != SET_INTERNAL_THREAD_SWITCH)
notify_variable_watchers (symbol, value, Qset_default, Qnil);
/* FALLTHROUGH! */
case SYMBOL_UNTRAPPED_WRITE:
break;
default: emacs_abort ();
}
start:
switch (sym->u.s.redirect)
{
case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
case SYMBOL_PLAINVAL: set_internal (symbol, value, Qnil, bindflag); return;
case SYMBOL_LOCALIZED:
{
struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
/* Store new value into the DEFAULT-VALUE slot. */
XSETCDR (blv->defcell, value);
/* If the default binding is now loaded, set the REALVALUE slot too. */
if (blv->fwd && EQ (blv->defcell, blv->valcell))
store_symval_forwarding (blv->fwd, value, NULL);
return;
}
case SYMBOL_FORWARDED:
{
union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
/* Handle variables like case-fold-search that have special slots
in the buffer.
Make them work apparently like Lisp_Buffer_Local_Value variables. */
if (BUFFER_OBJFWDP (valcontents))
{
int offset = XBUFFER_OBJFWD (valcontents)->offset;
int idx = PER_BUFFER_IDX (offset);
set_per_buffer_default (offset, value);
/* If this variable is not always local in all buffers,
set it in the buffers that don't nominally have a local value. */
if (idx > 0)
{
Lisp_Object buf, tail;
/* Do this only in live buffers, so that if there are
a lot of buffers which are dead, that doesn't slow
down let-binding of variables that are
automatically local when set, like
case-fold-search. This is for Lisp programs that
let-bind such variables in their inner loops. */
FOR_EACH_LIVE_BUFFER (tail, buf)
{
struct buffer *b = XBUFFER (buf);
if (!PER_BUFFER_VALUE_P (b, idx))
set_per_buffer_value (b, offset, value);
}
}
}
else
set_internal (symbol, value, Qnil, bindflag);
return;
}
default: emacs_abort ();
}
}
DEFUN ("set-default", Fset_default, Sset_default, 2, 2, 0,
doc: /* Set SYMBOL's default value to VALUE. SYMBOL and VALUE are evaluated.
The default value is seen in buffers that do not have their own values
for this variable. */)
(Lisp_Object symbol, Lisp_Object value)
{
set_default_internal (symbol, value, SET_INTERNAL_SET);
return value;
}
DEFUN ("setq-default", Fsetq_default, Ssetq_default, 0, UNEVALLED, 0,
doc: /* Set the default value of variable VAR to VALUE.
VAR, the variable name, is literal (not evaluated);
VALUE is an expression: it is evaluated and its value returned.
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 VAR VALUE VAR VALUE...)
This sets each VAR's default value to the corresponding VALUE.
The VALUE for the Nth VAR can refer to the new default values
of previous VARs.
usage: (setq-default [VAR VALUE]...) */)
(Lisp_Object args)
{
Lisp_Object args_left, symbol, val;
args_left = val = args;
while (CONSP (args_left))
{
val = eval_sub (Fcar (XCDR (args_left)));
symbol = XCAR (args_left);
Fset_default (symbol, val);
args_left = Fcdr (XCDR (args_left));
}
return val;
}
/* Lisp functions for creating and removing buffer-local variables. */
union Lisp_Val_Fwd
{
Lisp_Object value;
union Lisp_Fwd *fwd;
};
static struct Lisp_Buffer_Local_Value *
make_blv (struct Lisp_Symbol *sym, bool forwarded,
union Lisp_Val_Fwd valcontents)
{
struct Lisp_Buffer_Local_Value *blv = xmalloc (sizeof *blv);
Lisp_Object symbol;
Lisp_Object tem;
XSETSYMBOL (symbol, sym);
tem = Fcons (symbol, (forwarded
? do_symval_forwarding (valcontents.fwd)
: valcontents.value));
/* Buffer_Local_Values cannot have as realval a buffer-local
or keyboard-local forwarding. */
eassert (!(forwarded && BUFFER_OBJFWDP (valcontents.fwd)));
eassert (!(forwarded && KBOARD_OBJFWDP (valcontents.fwd)));
blv->fwd = forwarded ? valcontents.fwd : NULL;
set_blv_where (blv, Qnil);
blv->local_if_set = 0;
set_blv_defcell (blv, tem);
set_blv_valcell (blv, tem);
set_blv_found (blv, false);
return blv;
}
DEFUN ("make-variable-buffer-local", Fmake_variable_buffer_local,
Smake_variable_buffer_local, 1, 1, "vMake Variable Buffer Local: ",
doc: /* Make VARIABLE become buffer-local whenever it is set.
At any time, the value for the current buffer is in effect,
unless the variable has never been set in this buffer,
in which case the default value is in effect.
Note that binding the variable with `let', or setting it while
a `let'-style binding made in this buffer is in effect,
does not make the variable buffer-local. Return VARIABLE.
This globally affects all uses of this variable, so it belongs together with
the variable declaration, rather than with its uses (if you just want to make
a variable local to the current buffer for one particular use, use
`make-local-variable'). Buffer-local bindings are normally cleared
while setting up a new major mode, unless they have a `permanent-local'
property.
The function `default-value' gets the default value and `set-default' sets it. */)
(register Lisp_Object variable)
{
struct Lisp_Symbol *sym;
struct Lisp_Buffer_Local_Value *blv = NULL;
union Lisp_Val_Fwd valcontents;
bool forwarded UNINIT;
CHECK_SYMBOL (variable);
sym = XSYMBOL (variable);
start:
switch (sym->u.s.redirect)
{
case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
case SYMBOL_PLAINVAL:
forwarded = 0; valcontents.value = SYMBOL_VAL (sym);
if (EQ (valcontents.value, Qunbound))
valcontents.value = Qnil;
break;
case SYMBOL_LOCALIZED:
blv = SYMBOL_BLV (sym);
break;
case SYMBOL_FORWARDED:
forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
if (KBOARD_OBJFWDP (valcontents.fwd))
error ("Symbol %s may not be buffer-local",
SDATA (SYMBOL_NAME (variable)));
else if (BUFFER_OBJFWDP (valcontents.fwd))
return variable;
break;
default: emacs_abort ();
}
if (SYMBOL_CONSTANT_P (variable))
xsignal1 (Qsetting_constant, variable);
if (!blv)
{
blv = make_blv (sym, forwarded, valcontents);
sym->u.s.redirect = SYMBOL_LOCALIZED;
SET_SYMBOL_BLV (sym, blv);
}
blv->local_if_set = 1;
return variable;
}
DEFUN ("make-local-variable", Fmake_local_variable, Smake_local_variable,
1, 1, "vMake Local Variable: ",
doc: /* 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.)
Return VARIABLE.
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.
This function returns VARIABLE, and therefore
(set (make-local-variable \\='VARIABLE) VALUE-EXP)
works.
See also `make-variable-buffer-local'.
Do not use `make-local-variable' to make a hook variable buffer-local.
Instead, use `add-hook' and specify t for the LOCAL argument. */)
(Lisp_Object variable)
{
Lisp_Object tem;
bool forwarded UNINIT;
union Lisp_Val_Fwd valcontents;
struct Lisp_Symbol *sym;
struct Lisp_Buffer_Local_Value *blv = NULL;
CHECK_SYMBOL (variable);
sym = XSYMBOL (variable);
start:
switch (sym->u.s.redirect)
{
case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
case SYMBOL_PLAINVAL:
forwarded = 0; valcontents.value = SYMBOL_VAL (sym); break;
case SYMBOL_LOCALIZED:
blv = SYMBOL_BLV (sym);
break;
case SYMBOL_FORWARDED:
forwarded = 1; valcontents.fwd = SYMBOL_FWD (sym);
if (KBOARD_OBJFWDP (valcontents.fwd))
error ("Symbol %s may not be buffer-local",
SDATA (SYMBOL_NAME (variable)));
break;
default: emacs_abort ();
}
if (sym->u.s.trapped_write == SYMBOL_NOWRITE)
xsignal1 (Qsetting_constant, variable);
if (blv ? blv->local_if_set
: (forwarded && BUFFER_OBJFWDP (valcontents.fwd)))
{
tem = Fboundp (variable);
/* Make sure the symbol has a local value in this particular buffer,
by setting it to the same value it already has. */
Fset (variable, (EQ (tem, Qt) ? Fsymbol_value (variable) : Qunbound));
return variable;
}
if (!blv)
{
blv = make_blv (sym, forwarded, valcontents);
sym->u.s.redirect = SYMBOL_LOCALIZED;
SET_SYMBOL_BLV (sym, blv);
}
/* Make sure this buffer has its own value of symbol. */
XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
tem = Fassq (variable, BVAR (current_buffer, local_var_alist));
if (NILP (tem))
{
if (let_shadows_buffer_binding_p (sym))
{
AUTO_STRING (format,
"Making %s buffer-local while locally let-bound!");
CALLN (Fmessage, format, SYMBOL_NAME (variable));
}
if (BUFFERP (blv->where) && current_buffer == XBUFFER (blv->where))
/* Make sure the current value is permanently recorded, if it's the
default value. */
swap_in_global_binding (sym);
bset_local_var_alist
(current_buffer,
Fcons (Fcons (variable, XCDR (blv->defcell)),
BVAR (current_buffer, local_var_alist)));
}
return variable;
}
DEFUN ("kill-local-variable", Fkill_local_variable, Skill_local_variable,
1, 1, "vKill Local Variable: ",
doc: /* Make VARIABLE no longer have a separate value in the current buffer.
From now on the default value will apply in this buffer. Return VARIABLE. */)
(register Lisp_Object variable)
{
register Lisp_Object tem;
struct Lisp_Buffer_Local_Value *blv;
struct Lisp_Symbol *sym;
CHECK_SYMBOL (variable);
sym = XSYMBOL (variable);
start:
switch (sym->u.s.redirect)
{
case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
case SYMBOL_PLAINVAL: return variable;
case SYMBOL_FORWARDED:
{
union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
if (BUFFER_OBJFWDP (valcontents))
{
int offset = XBUFFER_OBJFWD (valcontents)->offset;
int idx = PER_BUFFER_IDX (offset);
if (idx > 0)
{
SET_PER_BUFFER_VALUE_P (current_buffer, idx, 0);
set_per_buffer_value (current_buffer, offset,
per_buffer_default (offset));
}
}
return variable;
}
case SYMBOL_LOCALIZED:
blv = SYMBOL_BLV (sym);
break;
default: emacs_abort ();
}
if (sym->u.s.trapped_write == SYMBOL_TRAPPED_WRITE)
notify_variable_watchers (variable, Qnil, Qmakunbound, Fcurrent_buffer ());
/* Get rid of this buffer's alist element, if any. */
XSETSYMBOL (variable, sym); /* Propagate variable indirection. */
tem = Fassq (variable, BVAR (current_buffer, local_var_alist));
if (!NILP (tem))
bset_local_var_alist
(current_buffer,
Fdelq (tem, BVAR (current_buffer, local_var_alist)));
/* If the symbol is set up with the current buffer's binding
loaded, recompute its value. We have to do it now, or else
forwarded objects won't work right. */
{
Lisp_Object buf; XSETBUFFER (buf, current_buffer);
if (EQ (buf, blv->where))
swap_in_global_binding (sym);
}
return variable;
}
/* Lisp functions for creating and removing buffer-local variables. */
DEFUN ("local-variable-p", Flocal_variable_p, Slocal_variable_p,
1, 2, 0,
doc: /* Non-nil if VARIABLE has a local binding in buffer BUFFER.
BUFFER defaults to the current buffer. */)
(Lisp_Object variable, Lisp_Object buffer)
{
struct buffer *buf = decode_buffer (buffer);
struct Lisp_Symbol *sym;
CHECK_SYMBOL (variable);
sym = XSYMBOL (variable);
start:
switch (sym->u.s.redirect)
{
case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
case SYMBOL_PLAINVAL: return Qnil;
case SYMBOL_LOCALIZED:
{
Lisp_Object tail, elt, tmp;
struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
XSETBUFFER (tmp, buf);
XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
if (EQ (blv->where, tmp)) /* The binding is already loaded. */
return blv_found (blv) ? Qt : Qnil;
else
for (tail = BVAR (buf, local_var_alist); CONSP (tail); tail = XCDR (tail))
{
elt = XCAR (tail);
if (EQ (variable, XCAR (elt)))
return Qt;
}
return Qnil;
}
case SYMBOL_FORWARDED:
{
union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
if (BUFFER_OBJFWDP (valcontents))
{
int offset = XBUFFER_OBJFWD (valcontents)->offset;
int idx = PER_BUFFER_IDX (offset);
if (idx == -1 || PER_BUFFER_VALUE_P (buf, idx))
return Qt;
}
return Qnil;
}
default: emacs_abort ();
}
}
DEFUN ("local-variable-if-set-p", Flocal_variable_if_set_p, Slocal_variable_if_set_p,
1, 2, 0,
doc: /* Non-nil if VARIABLE is local in buffer BUFFER when set there.
BUFFER defaults to the current buffer.
More precisely, return non-nil if either VARIABLE already has a local
value in BUFFER, or if VARIABLE is automatically buffer-local (see
`make-variable-buffer-local'). */)
(register Lisp_Object variable, Lisp_Object buffer)
{
struct Lisp_Symbol *sym;
CHECK_SYMBOL (variable);
sym = XSYMBOL (variable);
start:
switch (sym->u.s.redirect)
{
case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
case SYMBOL_PLAINVAL: return Qnil;
case SYMBOL_LOCALIZED:
{
struct Lisp_Buffer_Local_Value *blv = SYMBOL_BLV (sym);
if (blv->local_if_set)
return Qt;
XSETSYMBOL (variable, sym); /* Update in case of aliasing. */
return Flocal_variable_p (variable, buffer);
}
case SYMBOL_FORWARDED:
/* All BUFFER_OBJFWD slots become local if they are set. */
return (BUFFER_OBJFWDP (SYMBOL_FWD (sym)) ? Qt : Qnil);
default: emacs_abort ();
}
}
DEFUN ("variable-binding-locus", Fvariable_binding_locus, Svariable_binding_locus,
1, 1, 0,
doc: /* Return a value indicating where VARIABLE's current binding comes from.
If the current binding is buffer-local, the value is the current buffer.
If the current binding is global (the default), the value is nil. */)
(register Lisp_Object variable)
{
struct Lisp_Symbol *sym;
CHECK_SYMBOL (variable);
sym = XSYMBOL (variable);
/* Make sure the current binding is actually swapped in. */
find_symbol_value (variable);
start:
switch (sym->u.s.redirect)
{
case SYMBOL_VARALIAS: sym = indirect_variable (sym); goto start;
case SYMBOL_PLAINVAL: return Qnil;
case SYMBOL_FORWARDED:
{
union Lisp_Fwd *valcontents = SYMBOL_FWD (sym);
if (KBOARD_OBJFWDP (valcontents))
return Fframe_terminal (selected_frame);
else if (!BUFFER_OBJFWDP (valcontents))
return Qnil;
}
FALLTHROUGH;
case SYMBOL_LOCALIZED:
/* For a local variable, record both the symbol and which
buffer's or frame's value we are saving. */
if (!NILP (Flocal_variable_p (variable, Qnil)))
return Fcurrent_buffer ();
else if (sym->u.s.redirect == SYMBOL_LOCALIZED
&& blv_found (SYMBOL_BLV (sym)))
return SYMBOL_BLV (sym)->where;
else
return Qnil;
default: emacs_abort ();
}
}
/* Find the function at the end of a chain of symbol function indirections. */
/* If OBJECT is a symbol, find the end of its function chain and
return the value found there. If OBJECT is not a symbol, just
return it. If there is a cycle in the function chain, signal a
cyclic-function-indirection error.
This is like Findirect_function, except that it doesn't signal an
error if the chain ends up unbound. */
Lisp_Object
indirect_function (register Lisp_Object object)
{
Lisp_Object tortoise, hare;
hare = tortoise = object;
for (;;)
{
if (!SYMBOLP (hare) || NILP (hare))
break;
hare = XSYMBOL (hare)->u.s.function;
if (!SYMBOLP (hare) || NILP (hare))
break;
hare = XSYMBOL (hare)->u.s.function;
tortoise = XSYMBOL (tortoise)->u.s.function;
if (EQ (hare, tortoise))
xsignal1 (Qcyclic_function_indirection, object);
}
return hare;
}
DEFUN ("indirect-function", Findirect_function, Sindirect_function, 1, 2, 0,
doc: /* Return the function at the end of OBJECT's function chain.
If OBJECT is not a symbol, just return it. Otherwise, follow all
function indirections to find the final function binding and return it.
Signal a cyclic-function-indirection error if there is a loop in the
function chain of symbols. */)
(register Lisp_Object object, Lisp_Object noerror)
{
Lisp_Object result;
/* Optimize for no indirection. */
result = object;
if (SYMBOLP (result) && !NILP (result)
&& (result = XSYMBOL (result)->u.s.function, SYMBOLP (result)))
result = indirect_function (result);
if (!NILP (result))
return result;
return Qnil;
}
/* Extract and set vector and string elements. */
DEFUN ("aref", Faref, Saref, 2, 2, 0,
doc: /* Return the element of ARRAY at index IDX.
ARRAY may be a vector, a string, a char-table, a bool-vector, a record,
or a byte-code object. IDX starts at 0. */)
(register Lisp_Object array, Lisp_Object idx)
{
register EMACS_INT idxval;
CHECK_FIXNUM (idx);
idxval = XINT (idx);
if (STRINGP (array))
{
int c;
ptrdiff_t idxval_byte;
if (idxval < 0 || idxval >= SCHARS (array))
args_out_of_range (array, idx);
if (! STRING_MULTIBYTE (array))
return make_fixnum ((unsigned char) SREF (array, idxval));
idxval_byte = string_char_to_byte (array, idxval);
c = STRING_CHAR (SDATA (array) + idxval_byte);
return make_fixnum (c);
}
else if (BOOL_VECTOR_P (array))
{
if (idxval < 0 || idxval >= bool_vector_size (array))
args_out_of_range (array, idx);
return bool_vector_ref (array, idxval);
}
else if (CHAR_TABLE_P (array))
{
CHECK_CHARACTER (idx);
return CHAR_TABLE_REF (array, idxval);
}
else
{
ptrdiff_t size = 0;
if (VECTORP (array))
size = ASIZE (array);
else if (COMPILEDP (array) || RECORDP (array))
size = PVSIZE (array);
else
wrong_type_argument (Qarrayp, array);
if (idxval < 0 || idxval >= size)
args_out_of_range (array, idx);
return AREF (array, idxval);
}
}
DEFUN ("aset", Faset, Saset, 3, 3, 0,
doc: /* Store into the element of ARRAY at index IDX the value NEWELT.
Return NEWELT. ARRAY may be a vector, a string, a char-table or a
bool-vector. IDX starts at 0. */)
(register Lisp_Object array, Lisp_Object idx, Lisp_Object newelt)
{
register EMACS_INT idxval;
CHECK_FIXNUM (idx);
idxval = XINT (idx);
if (! RECORDP (array))
CHECK_ARRAY (array, Qarrayp);
if (VECTORP (array))
{
CHECK_IMPURE (array, XVECTOR (array));
if (idxval < 0 || idxval >= ASIZE (array))
args_out_of_range (array, idx);
ASET (array, idxval, newelt);
}
else if (BOOL_VECTOR_P (array))
{
if (idxval < 0 || idxval >= bool_vector_size (array))
args_out_of_range (array, idx);
bool_vector_set (array, idxval, !NILP (newelt));
}
else if (CHAR_TABLE_P (array))
{
CHECK_CHARACTER (idx);
CHAR_TABLE_SET (array, idxval, newelt);
}
else if (RECORDP (array))
{
if (idxval < 0 || idxval >= PVSIZE (array))
args_out_of_range (array, idx);
ASET (array, idxval, newelt);
}
else /* STRINGP */
{
int c;
CHECK_IMPURE (array, XSTRING (array));
if (idxval < 0 || idxval >= SCHARS (array))
args_out_of_range (array, idx);
CHECK_CHARACTER (newelt);
c = XFASTINT (newelt);
if (STRING_MULTIBYTE (array))
{
ptrdiff_t idxval_byte, nbytes;
int prev_bytes, new_bytes;
unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *p0 = workbuf, *p1;
nbytes = SBYTES (array);
idxval_byte = string_char_to_byte (array, idxval);
p1 = SDATA (array) + idxval_byte;
prev_bytes = BYTES_BY_CHAR_HEAD (*p1);
new_bytes = CHAR_STRING (c, p0);
if (prev_bytes != new_bytes)
{
/* We must relocate the string data. */
ptrdiff_t nchars = SCHARS (array);
USE_SAFE_ALLOCA;
unsigned char *str = SAFE_ALLOCA (nbytes);
memcpy (str, SDATA (array), nbytes);
allocate_string_data (XSTRING (array), nchars,
nbytes + new_bytes - prev_bytes);
memcpy (SDATA (array), str, idxval_byte);
p1 = SDATA (array) + idxval_byte;
memcpy (p1 + new_bytes, str + idxval_byte + prev_bytes,
nbytes - (idxval_byte + prev_bytes));
SAFE_FREE ();
clear_string_char_byte_cache ();
}
while (new_bytes--)
*p1++ = *p0++;
}
else
{
if (! SINGLE_BYTE_CHAR_P (c))
{
ptrdiff_t i;
for (i = SBYTES (array) - 1; i >= 0; i--)
if (SREF (array, i) >= 0x80)
args_out_of_range (array, newelt);
/* ARRAY is an ASCII string. Convert it to a multibyte
string, and try `aset' again. */
STRING_SET_MULTIBYTE (array);
return Faset (array, idx, newelt);
}
SSET (array, idxval, c);
}
}
return newelt;
}
/* Arithmetic functions */
#ifndef isnan
# define isnan(x) ((x) != (x))
#endif
static Lisp_Object
bignumcompare (Lisp_Object num1, Lisp_Object num2,
enum Arith_Comparison comparison)
{
int cmp;
bool test;
if (BIGNUMP (num1))
{
if (FLOATP (num2))
{
/* Note that GMP doesn't define comparisons against NaN, so
we need to handle them specially. */
if (isnan (XFLOAT_DATA (num2)))
return Qnil;
cmp = mpz_cmp_d (XBIGNUM (num1)->value, XFLOAT_DATA (num2));
}
else if (FIXNUMP (num2))
{
if (sizeof (EMACS_INT) > sizeof (long) && XINT (num2) > LONG_MAX)
{
mpz_t tem;
mpz_init (tem);
mpz_set_intmax (tem, XINT (num2));
cmp = mpz_cmp (XBIGNUM (num1)->value, tem);
mpz_clear (tem);
}
else
cmp = mpz_cmp_si (XBIGNUM (num1)->value, XINT (num2));
}
else
{
eassume (BIGNUMP (num2));
cmp = mpz_cmp (XBIGNUM (num1)->value, XBIGNUM (num2)->value);
}
}
else
{
eassume (BIGNUMP (num2));
if (FLOATP (num1))
{
/* Note that GMP doesn't define comparisons against NaN, so
we need to handle them specially. */
if (isnan (XFLOAT_DATA (num1)))
return Qnil;
cmp = - mpz_cmp_d (XBIGNUM (num2)->value, XFLOAT_DATA (num1));
}
else
{
eassume (FIXNUMP (num1));
if (sizeof (EMACS_INT) > sizeof (long) && XINT (num1) > LONG_MAX)
{
mpz_t tem;
mpz_init (tem);
mpz_set_intmax (tem, XINT (num1));
cmp = - mpz_cmp (XBIGNUM (num2)->value, tem);
mpz_clear (tem);
}
else
cmp = - mpz_cmp_si (XBIGNUM (num2)->value, XINT (num1));
}
}
switch (comparison)
{
case ARITH_EQUAL:
test = cmp == 0;
break;
case ARITH_NOTEQUAL:
test = cmp != 0;
break;
case ARITH_LESS:
test = cmp < 0;
break;
case ARITH_LESS_OR_EQUAL:
test = cmp <= 0;
break;
case ARITH_GRTR:
test = cmp > 0;
break;
case ARITH_GRTR_OR_EQUAL:
test = cmp >= 0;
break;
default:
eassume (false);
}
return test ? Qt : Qnil;
}
Lisp_Object
arithcompare (Lisp_Object num1, Lisp_Object num2,
enum Arith_Comparison comparison)
{
double f1, f2;
EMACS_INT i1, i2;
bool fneq;
bool test;
CHECK_NUMBER_COERCE_MARKER (num1);
CHECK_NUMBER_COERCE_MARKER (num2);
if (BIGNUMP (num1) || BIGNUMP (num2))
return bignumcompare (num1, num2, comparison);
/* If either arg is floating point, set F1 and F2 to the 'double'
approximations of the two arguments, and set FNEQ if floating-point
comparison reports that F1 is not equal to F2, possibly because F1
or F2 is a NaN. Regardless, set I1 and I2 to integers that break
ties if the floating-point comparison is either not done or reports
equality. */
if (FLOATP (num1))
{
f1 = XFLOAT_DATA (num1);
if (FLOATP (num2))
{
i1 = i2 = 0;
f2 = XFLOAT_DATA (num2);
}
else
{
/* Compare a float NUM1 to an integer NUM2 by converting the
integer I2 (i.e., NUM2) to the double F2 (a conversion that
can round on some platforms, if I2 is large enough), and then
converting F2 back to the integer I1 (a conversion that is
always exact), so that I1 exactly equals ((double) NUM2). If
floating-point comparison reports a tie, NUM1 = F1 = F2 = I1
(exactly) so I1 - I2 = NUM1 - NUM2 (exactly), so comparing I1
to I2 will break the tie correctly. */
i1 = f2 = i2 = XINT (num2);
}
fneq = f1 != f2;
}
else
{
i1 = XINT (num1);
if (FLOATP (num2))
{
/* Compare an integer NUM1 to a float NUM2. This is the
converse of comparing float to integer (see above). */
i2 = f1 = i1;
f2 = XFLOAT_DATA (num2);
fneq = f1 != f2;
}
else
{
i2 = XINT (num2);
fneq = false;
}
}
switch (comparison)
{
case ARITH_EQUAL:
test = !fneq && i1 == i2;
break;
case ARITH_NOTEQUAL:
test = fneq || i1 != i2;
break;
case ARITH_LESS:
test = fneq ? f1 < f2 : i1 < i2;
break;
case ARITH_LESS_OR_EQUAL:
test = fneq ? f1 <= f2 : i1 <= i2;
break;
case ARITH_GRTR:
test = fneq ? f1 > f2 : i1 > i2;
break;
case ARITH_GRTR_OR_EQUAL:
test = fneq ? f1 >= f2 : i1 >= i2;
break;
default:
eassume (false);
}
return test ? Qt : Qnil;
}
static Lisp_Object
arithcompare_driver (ptrdiff_t nargs, Lisp_Object *args,
enum Arith_Comparison comparison)
{
for (ptrdiff_t i = 1; i < nargs; i++)
if (NILP (arithcompare (args[i - 1], args[i], comparison)))
return Qnil;
return Qt;
}
DEFUN ("=", Feqlsign, Seqlsign, 1, MANY, 0,
doc: /* Return t if args, all numbers or markers, are equal.
usage: (= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
(ptrdiff_t nargs, Lisp_Object *args)
{
return arithcompare_driver (nargs, args, ARITH_EQUAL);
}
DEFUN ("<", Flss, Slss, 1, MANY, 0,
doc: /* Return t if each arg (a number or marker), is less than the next arg.
usage: (< NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
(ptrdiff_t nargs, Lisp_Object *args)
{
return arithcompare_driver (nargs, args, ARITH_LESS);
}
DEFUN (">", Fgtr, Sgtr, 1, MANY, 0,
doc: /* Return t if each arg (a number or marker) is greater than the next arg.
usage: (> NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
(ptrdiff_t nargs, Lisp_Object *args)
{
return arithcompare_driver (nargs, args, ARITH_GRTR);
}
DEFUN ("<=", Fleq, Sleq, 1, MANY, 0,
doc: /* Return t if each arg (a number or marker) is less than or equal to the next.
usage: (<= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
(ptrdiff_t nargs, Lisp_Object *args)
{
return arithcompare_driver (nargs, args, ARITH_LESS_OR_EQUAL);
}
DEFUN (">=", Fgeq, Sgeq, 1, MANY, 0,
doc: /* Return t if each arg (a number or marker) is greater than or equal to the next.
usage: (>= NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
(ptrdiff_t nargs, Lisp_Object *args)
{
return arithcompare_driver (nargs, args, ARITH_GRTR_OR_EQUAL);
}
DEFUN ("/=", Fneq, Sneq, 2, 2, 0,
doc: /* Return t if first arg is not equal to second arg. Both must be numbers or markers. */)
(register Lisp_Object num1, Lisp_Object num2)
{
return arithcompare (num1, num2, ARITH_NOTEQUAL);
}
/* Convert the integer I to a cons-of-integers, where I is not in
fixnum range. */
#define INTBIG_TO_LISP(i, extremum) \
(eassert (FIXNUM_OVERFLOW_P (i)), \
(! (FIXNUM_OVERFLOW_P ((extremum) >> 16) \
&& FIXNUM_OVERFLOW_P ((i) >> 16)) \
? Fcons (make_fixnum ((i) >> 16), make_fixnum ((i) & 0xffff)) \
: ! (FIXNUM_OVERFLOW_P ((extremum) >> 16 >> 24) \
&& FIXNUM_OVERFLOW_P ((i) >> 16 >> 24)) \
? Fcons (make_fixnum ((i) >> 16 >> 24), \
Fcons (make_fixnum ((i) >> 16 & 0xffffff), \
make_fixnum ((i) & 0xffff))) \
: make_float (i)))
Lisp_Object
intbig_to_lisp (intmax_t i)
{
return INTBIG_TO_LISP (i, INTMAX_MIN);
}
Lisp_Object
uintbig_to_lisp (uintmax_t i)
{
return INTBIG_TO_LISP (i, UINTMAX_MAX);
}
/* Convert the cons-of-integers, integer, or float value C to an
unsigned value with maximum value MAX, where MAX is one less than a
power of 2. Signal an error if C does not have a valid format or
is out of range. */
uintmax_t
cons_to_unsigned (Lisp_Object c, uintmax_t max)
{
bool valid = false;
uintmax_t val UNINIT;
if (FIXNUMP (c))
{
valid = XINT (c) >= 0;
val = XINT (c);
}
else if (FLOATP (c))
{
double d = XFLOAT_DATA (c);
if (d >= 0 && d < 1.0 + max)
{
val = d;
valid = val == d;
}
}
else if (CONSP (c) && FIXNATP (XCAR (c)))
{
uintmax_t top = XFASTINT (XCAR (c));
Lisp_Object rest = XCDR (c);
if (top <= UINTMAX_MAX >> 24 >> 16
&& CONSP (rest)
&& FIXNATP (XCAR (rest)) && XFASTINT (XCAR (rest)) < 1 << 24
&& FIXNATP (XCDR (rest)) && XFASTINT (XCDR (rest)) < 1 << 16)
{
uintmax_t mid = XFASTINT (XCAR (rest));
val = top << 24 << 16 | mid << 16 | XFASTINT (XCDR (rest));
valid = true;
}
else if (top <= UINTMAX_MAX >> 16)
{
if (CONSP (rest))
rest = XCAR (rest);
if (FIXNATP (rest) && XFASTINT (rest) < 1 << 16)
{
val = top << 16 | XFASTINT (rest);
valid = true;
}
}
}
if (! (valid && val <= max))
error ("Not an in-range integer, integral float, or cons of integers");
return val;
}
/* Convert the cons-of-integers, integer, or float value C to a signed
value with extrema MIN and MAX. MAX should be one less than a
power of 2, and MIN should be zero or the negative of a power of 2.
Signal an error if C does not have a valid format or is out of
range. */
intmax_t
cons_to_signed (Lisp_Object c, intmax_t min, intmax_t max)
{
bool valid = false;
intmax_t val UNINIT;
if (FIXNUMP (c))
{
val = XINT (c);
valid = true;
}
else if (FLOATP (c))
{
double d = XFLOAT_DATA (c);
if (d >= min && d < 1.0 + max)
{
val = d;
valid = val == d;
}
}
else if (CONSP (c) && FIXNUMP (XCAR (c)))
{
intmax_t top = XINT (XCAR (c));
Lisp_Object rest = XCDR (c);
if (top >= INTMAX_MIN >> 24 >> 16 && top <= INTMAX_MAX >> 24 >> 16
&& CONSP (rest)
&& FIXNATP (XCAR (rest)) && XFASTINT (XCAR (rest)) < 1 << 24
&& FIXNATP (XCDR (rest)) && XFASTINT (XCDR (rest)) < 1 << 16)
{
intmax_t mid = XFASTINT (XCAR (rest));
val = top << 24 << 16 | mid << 16 | XFASTINT (XCDR (rest));
valid = true;
}
else if (top >= INTMAX_MIN >> 16 && top <= INTMAX_MAX >> 16)
{
if (CONSP (rest))
rest = XCAR (rest);
if (FIXNATP (rest) && XFASTINT (rest) < 1 << 16)
{
val = top << 16 | XFASTINT (rest);
valid = true;
}
}
}
if (! (valid && min <= val && val <= max))
error ("Not an in-range integer, integral float, or cons of integers");
return val;
}
DEFUN ("number-to-string", Fnumber_to_string, Snumber_to_string, 1, 1, 0,
doc: /* Return the decimal representation of NUMBER as a string.
Uses a minus sign if negative.
NUMBER may be an integer or a floating point number. */)
(Lisp_Object number)
{
char buffer[max (FLOAT_TO_STRING_BUFSIZE, INT_BUFSIZE_BOUND (EMACS_INT))];
int len;
if (BIGNUMP (number))
{
ptrdiff_t count = SPECPDL_INDEX ();
char *str = mpz_get_str (NULL, 10, XBIGNUM (number)->value);
record_unwind_protect_ptr (xfree, str);
return unbind_to (count, make_unibyte_string (str, strlen (str)));
}
CHECK_FIXNUM_OR_FLOAT (number);
if (FLOATP (number))
len = float_to_string (buffer, XFLOAT_DATA (number));
else
len = sprintf (buffer, "%"pI"d", XINT (number));
return make_unibyte_string (buffer, len);
}
DEFUN ("string-to-number", Fstring_to_number, Sstring_to_number, 1, 2, 0,
doc: /* Parse STRING as a decimal number and return the number.
Ignore leading spaces and tabs, and all trailing chars. Return 0 if
STRING cannot be parsed as an integer or floating point number.
If BASE, interpret STRING as a number in that base. If BASE isn't
present, base 10 is used. BASE must be between 2 and 16 (inclusive).
If the base used is not 10, STRING is always parsed as an integer. */)
(register Lisp_Object string, Lisp_Object base)
{
int b;
CHECK_STRING (string);
if (NILP (base))
b = 10;
else
{
CHECK_FIXNUM (base);
if (! (XINT (base) >= 2 && XINT (base) <= 16))
xsignal1 (Qargs_out_of_range, base);
b = XINT (base);
}
char *p = SSDATA (string);
while (*p == ' ' || *p == '\t')
p++;
Lisp_Object val = string_to_number (p, b, S2N_IGNORE_TRAILING);
return NILP (val) ? make_fixnum (0) : val;
}
enum arithop
{
Aadd,
Asub,
Amult,
Adiv,
Alogand,
Alogior,
Alogxor
};
static void
free_mpz_value (void *value_ptr)
{
mpz_clear (*(mpz_t *) value_ptr);
}
static Lisp_Object float_arith_driver (double, ptrdiff_t, enum arithop,
ptrdiff_t, Lisp_Object *);
static Lisp_Object
arith_driver (enum arithop code, ptrdiff_t nargs, Lisp_Object *args)
{
Lisp_Object val = Qnil;
ptrdiff_t argnum;
ptrdiff_t count = SPECPDL_INDEX ();
mpz_t accum;
mpz_init (accum);
record_unwind_protect_ptr (free_mpz_value, &accum);
switch (code)
{
case Alogior:
case Alogxor:
case Aadd:
case Asub:
/* ACCUM is already 0. */
break;
case Amult:
case Adiv:
mpz_set_si (accum, 1);
break;
case Alogand:
mpz_set_si (accum, -1);
break;
default:
break;
}
for (argnum = 0; argnum < nargs; argnum++)
{
/* Using args[argnum] as argument to CHECK_NUMBER... */
val = args[argnum];
CHECK_NUMBER_COERCE_MARKER (val);
if (FLOATP (val))
return unbind_to (count,
float_arith_driver (mpz_get_d (accum), argnum, code,
nargs, args));
switch (code)
{
case Aadd:
if (BIGNUMP (val))
mpz_add (accum, accum, XBIGNUM (val)->value);
else if (sizeof (EMACS_INT) > sizeof (long))
{
mpz_t tem;
mpz_init (tem);
mpz_set_intmax (tem, XINT (val));
mpz_add (accum, accum, tem);
mpz_clear (tem);
}
else if (XINT (val) < 0)
mpz_sub_ui (accum, accum, - XINT (val));
else
mpz_add_ui (accum, accum, XINT (val));
break;
case Asub:
if (! argnum)
{
if (BIGNUMP (val))
mpz_set (accum, XBIGNUM (val)->value);
else
mpz_set_intmax (accum, XINT (val));
if (nargs == 1)
mpz_neg (accum, accum);
}
else if (BIGNUMP (val))
mpz_sub (accum, accum, XBIGNUM (val)->value);
else if (sizeof (EMACS_INT) > sizeof (long))
{
mpz_t tem;
mpz_init (tem);
mpz_set_intmax (tem, XINT (val));
mpz_sub (accum, accum, tem);
mpz_clear (tem);
}
else if (XINT (val) < 0)
mpz_add_ui (accum, accum, - XINT (val));
else
mpz_sub_ui (accum, accum, XINT (val));
break;
case Amult:
if (BIGNUMP (val))
mpz_mul (accum, accum, XBIGNUM (val)->value);
else if (sizeof (EMACS_INT) > sizeof (long))
{
mpz_t tem;
mpz_init (tem);
mpz_set_intmax (tem, XINT (val));
mpz_mul (accum, accum, tem);
mpz_clear (tem);
}
else
mpz_mul_si (accum, accum, XINT (val));
break;
case Adiv:
if (! (argnum || nargs == 1))
{
if (BIGNUMP (val))
mpz_set (accum, XBIGNUM (val)->value);
else
mpz_set_intmax (accum, XINT (val));
}
else
{
/* Note that a bignum can never be 0, so we don't need
to check that case. */
if (FIXNUMP (val) && XINT (val) == 0)
xsignal0 (Qarith_error);
if (BIGNUMP (val))
mpz_tdiv_q (accum, accum, XBIGNUM (val)->value);
else if (sizeof (EMACS_INT) > sizeof (long))
{
mpz_t tem;
mpz_init (tem);
mpz_set_intmax (tem, XINT (val));
mpz_tdiv_q (accum, accum, tem);
mpz_clear (tem);
}
else
{
EMACS_INT value = XINT (val);
bool negate = value < 0;
if (negate)
value = -value;
mpz_tdiv_q_ui (accum, accum, value);
if (negate)
mpz_neg (accum, accum);
}
}
break;
case Alogand:
if (BIGNUMP (val))
mpz_and (accum, accum, XBIGNUM (val)->value);
else
{
mpz_t tem;
mpz_init (tem);
mpz_set_uintmax (tem, XUINT (val));
mpz_and (accum, accum, tem);
mpz_clear (tem);
}
break;
case Alogior:
if (BIGNUMP (val))
mpz_ior (accum, accum, XBIGNUM (val)->value);
else
{
mpz_t tem;
mpz_init (tem);
mpz_set_uintmax (tem, XUINT (val));
mpz_ior (accum, accum, tem);
mpz_clear (tem);
}
break;
case Alogxor:
if (BIGNUMP (val))
mpz_xor (accum, accum, XBIGNUM (val)->value);
else
{
mpz_t tem;
mpz_init (tem);
mpz_set_uintmax (tem, XUINT (val));
mpz_xor (accum, accum, tem);
mpz_clear (tem);
}
break;
}
}
return unbind_to (count, make_number (accum));
}
static Lisp_Object
float_arith_driver (double accum, ptrdiff_t argnum, enum arithop code,
ptrdiff_t nargs, Lisp_Object *args)
{
register Lisp_Object val;
double next;
for (; argnum < nargs; argnum++)
{
/* using args[argnum] as argument to CHECK_NUMBER_... */
val = args[argnum];
CHECK_NUMBER_COERCE_MARKER (val);
if (FLOATP (val))
{
next = XFLOAT_DATA (val);
}
else if (BIGNUMP (val))
next = mpz_get_d (XBIGNUM (val)->value);
else
{
args[argnum] = val; /* runs into a compiler bug. */
next = XINT (args[argnum]);
}
switch (code)
{
case Aadd:
accum += next;
break;
case Asub:
accum = argnum ? accum - next : nargs == 1 ? - next : next;
break;
case Amult:
accum *= next;
break;
case Adiv:
if (! (argnum || nargs == 1))
accum = next;
else
{
if (! IEEE_FLOATING_POINT && next == 0)
xsignal0 (Qarith_error);
accum /= next;
}
break;
case Alogand:
case Alogior:
case Alogxor:
wrong_type_argument (Qinteger_or_marker_p, val);
}
}
return make_float (accum);
}
DEFUN ("+", Fplus, Splus, 0, MANY, 0,
doc: /* Return sum of any number of arguments, which are numbers or markers.
usage: (+ &rest NUMBERS-OR-MARKERS) */)
(ptrdiff_t nargs, Lisp_Object *args)
{
return arith_driver (Aadd, nargs, args);
}
DEFUN ("-", Fminus, Sminus, 0, MANY, 0,
doc: /* Negate number or subtract numbers or markers and return the result.
With one arg, negates it. With more than one arg,
subtracts all but the first from the first.
usage: (- &optional NUMBER-OR-MARKER &rest MORE-NUMBERS-OR-MARKERS) */)
(ptrdiff_t nargs, Lisp_Object *args)
{
return arith_driver (Asub, nargs, args);
}
DEFUN ("*", Ftimes, Stimes, 0, MANY, 0,
doc: /* Return product of any number of arguments, which are numbers or markers.
usage: (* &rest NUMBERS-OR-MARKERS) */)
(ptrdiff_t nargs, Lisp_Object *args)
{
return arith_driver (Amult, nargs, args);
}
DEFUN ("/", Fquo, Squo, 1, MANY, 0,
doc: /* Divide number by divisors and return the result.
With two or more arguments, return first argument divided by the rest.
With one argument, return 1 divided by the argument.
The arguments must be numbers or markers.
usage: (/ NUMBER &rest DIVISORS) */)
(ptrdiff_t nargs, Lisp_Object *args)
{
ptrdiff_t argnum;
for (argnum = 2; argnum < nargs; argnum++)
if (FLOATP (args[argnum]))
return float_arith_driver (0, 0, Adiv, nargs, args);
return arith_driver (Adiv, nargs, args);
}
DEFUN ("%", Frem, Srem, 2, 2, 0,
doc: /* Return remainder of X divided by Y.
Both must be integers or markers. */)
(register Lisp_Object x, Lisp_Object y)
{
Lisp_Object val;
CHECK_INTEGER_COERCE_MARKER (x);
CHECK_INTEGER_COERCE_MARKER (y);
/* Note that a bignum can never be 0, so we don't need to check that
case. */
if (FIXNUMP (y) && XINT (y) == 0)
xsignal0 (Qarith_error);
if (FIXNUMP (x) && FIXNUMP (y))
XSETINT (val, XINT (x) % XINT (y));
else
{
mpz_t xm, ym, *xmp, *ymp;
mpz_t result;
if (BIGNUMP (x))
xmp = &XBIGNUM (x)->value;
else
{
mpz_init (xm);
mpz_set_intmax (xm, XINT (x));
xmp = &xm;
}
if (BIGNUMP (y))
ymp = &XBIGNUM (y)->value;
else
{
mpz_init (ym);
mpz_set_intmax (ym, XINT (y));
ymp = &ym;
}
mpz_init (result);
mpz_tdiv_r (result, *xmp, *ymp);
val = make_number (result);
mpz_clear (result);
if (xmp == &xm)
mpz_clear (xm);
if (ymp == &ym)
mpz_clear (ym);
}
return val;
}
DEFUN ("mod", Fmod, Smod, 2, 2, 0,
doc: /* Return X modulo Y.
The result falls between zero (inclusive) and Y (exclusive).
Both X and Y must be numbers or markers. */)
(register Lisp_Object x, Lisp_Object y)
{
Lisp_Object val;
EMACS_INT i1, i2;
CHECK_NUMBER_COERCE_MARKER (x);
CHECK_NUMBER_COERCE_MARKER (y);
/* Note that a bignum can never be 0, so we don't need to check that
case. */
if (FIXNUMP (y) && XINT (y) == 0)
xsignal0 (Qarith_error);
if (FLOATP (x) || FLOATP (y))
return fmod_float (x, y);
if (FIXNUMP (x) && FIXNUMP (y))
{
i1 = XINT (x);
i2 = XINT (y);
if (i2 == 0)
xsignal0 (Qarith_error);
i1 %= i2;
/* If the "remainder" comes out with the wrong sign, fix it. */
if (i2 < 0 ? i1 > 0 : i1 < 0)
i1 += i2;
XSETINT (val, i1);
}
else
{
mpz_t xm, ym, *xmp, *ymp;
mpz_t result;
int cmpr, cmpy;
if (BIGNUMP (x))
xmp = &XBIGNUM (x)->value;
else
{
mpz_init (xm);
mpz_set_intmax (xm, XINT (x));
xmp = &xm;
}
if (BIGNUMP (y))
ymp = &XBIGNUM (y)->value;
else
{
mpz_init (ym);
mpz_set_intmax (ym, XINT (y));
ymp = &ym;
}
mpz_init (result);
mpz_mod (result, *xmp, *ymp);
/* Fix the sign if needed. */
cmpr = mpz_cmp_si (result, 0);
cmpy = mpz_cmp_si (*ymp, 0);
if (cmpy < 0 ? cmpr > 0 : cmpr < 0)
mpz_add (result, result, *ymp);
val = make_number (result);
mpz_clear (result);
if (xmp == &xm)
mpz_clear (xm);
if (ymp == &ym)
mpz_clear (ym);
}
return val;
}
static Lisp_Object
minmax_driver (ptrdiff_t nargs, Lisp_Object *args,
enum Arith_Comparison comparison)
{
Lisp_Object accum = args[0];
CHECK_NUMBER_COERCE_MARKER (accum);
for (ptrdiff_t argnum = 1; argnum < nargs; argnum++)
{
Lisp_Object val = args[argnum];
CHECK_NUMBER_COERCE_MARKER (val);
if (!NILP (arithcompare (val, accum, comparison)))
accum = val;
else if (FLOATP (val) && isnan (XFLOAT_DATA (val)))
return val;
}
return accum;
}
DEFUN ("max", Fmax, Smax, 1, MANY, 0,
doc: /* Return largest of all the arguments (which must be numbers or markers).
The value is always a number; markers are converted to numbers.
usage: (max NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
(ptrdiff_t nargs, Lisp_Object *args)
{
return minmax_driver (nargs, args, ARITH_GRTR);
}
DEFUN ("min", Fmin, Smin, 1, MANY, 0,
doc: /* Return smallest of all the arguments (which must be numbers or markers).
The value is always a number; markers are converted to numbers.
usage: (min NUMBER-OR-MARKER &rest NUMBERS-OR-MARKERS) */)
(ptrdiff_t nargs, Lisp_Object *args)
{
return minmax_driver (nargs, args, ARITH_LESS);
}
DEFUN ("logand", Flogand, Slogand, 0, MANY, 0,
doc: /* Return bitwise-and of all the arguments.
Arguments may be integers, or markers converted to integers.
usage: (logand &rest INTS-OR-MARKERS) */)
(ptrdiff_t nargs, Lisp_Object *args)
{
return arith_driver (Alogand, nargs, args);
}
DEFUN ("logior", Flogior, Slogior, 0, MANY, 0,
doc: /* Return bitwise-or of all the arguments.
Arguments may be integers, or markers converted to integers.
usage: (logior &rest INTS-OR-MARKERS) */)
(ptrdiff_t nargs, Lisp_Object *args)
{
return arith_driver (Alogior, nargs, args);
}
DEFUN ("logxor", Flogxor, Slogxor, 0, MANY, 0,
doc: /* Return bitwise-exclusive-or of all the arguments.
Arguments may be integers, or markers converted to integers.
usage: (logxor &rest INTS-OR-MARKERS) */)
(ptrdiff_t nargs, Lisp_Object *args)
{
return arith_driver (Alogxor, nargs, args);
}
DEFUN ("logcount", Flogcount, Slogcount, 1, 1, 0,
doc: /* Return population count of VALUE.
This is the number of one bits in the two's complement representation
of VALUE. If VALUE is negative, return the number of zero bits in the
representation. */)
(Lisp_Object value)
{
CHECK_INTEGER (value);
if (BIGNUMP (value))
{
if (mpz_sgn (XBIGNUM (value)->value) >= 0)
return make_fixnum (mpz_popcount (XBIGNUM (value)->value));
mpz_t tem;
mpz_init (tem);
mpz_neg (tem, XBIGNUM (value)->value);
mpz_sub_ui (tem, tem, 1);
Lisp_Object result = make_fixnum (mpz_popcount (tem));
mpz_clear (tem);
return result;
}
eassume (FIXNUMP (value));
EMACS_INT v = XINT (value) < 0 ? -1 - XINT (value) : XINT (value);
return make_fixnum (EMACS_UINT_WIDTH <= UINT_WIDTH
? count_one_bits (v)
: EMACS_UINT_WIDTH <= ULONG_WIDTH
? count_one_bits_l (v)
: count_one_bits_ll (v));
}
static Lisp_Object
ash_lsh_impl (Lisp_Object value, Lisp_Object count, bool lsh)
{
/* This code assumes that signed right shifts are arithmetic. */
verify ((EMACS_INT) -1 >> 1 == -1);
Lisp_Object val;
CHECK_INTEGER (value);
CHECK_FIXNUM (count);
if (BIGNUMP (value))
{
mpz_t result;
mpz_init (result);
if (XINT (count) >= 0)
mpz_mul_2exp (result, XBIGNUM (value)->value, XINT (count));
else if (lsh)
mpz_tdiv_q_2exp (result, XBIGNUM (value)->value, - XINT (count));
else
mpz_fdiv_q_2exp (result, XBIGNUM (value)->value, - XINT (count));
val = make_number (result);
mpz_clear (result);
}
else
{
/* Just do the work as bignums to make the code simpler. */
mpz_t result;
eassume (FIXNUMP (value));
mpz_init (result);
mpz_set_intmax (result, XINT (value));
if (XINT (count) >= 0)
mpz_mul_2exp (result, result, XINT (count));
else if (lsh)
{
if (mpz_sgn (result) > 0)
mpz_fdiv_q_2exp (result, result, - XINT (count));
else
mpz_fdiv_q_2exp (result, result, - XINT (count));
}
else /* ash */
mpz_fdiv_q_2exp (result, result, - XINT (count));
val = make_number (result);
mpz_clear (result);
}
return val;
}
DEFUN ("ash", Fash, Sash, 2, 2, 0,
doc: /* Return VALUE with its bits shifted left by COUNT.
If COUNT is negative, shifting is actually to the right.
In this case, the sign bit is duplicated. */)
(register Lisp_Object value, Lisp_Object count)
{
return ash_lsh_impl (value, count, false);
}
DEFUN ("lsh", Flsh, Slsh, 2, 2, 0,
doc: /* Return VALUE with its bits shifted left by COUNT.
If COUNT is negative, shifting is actually to the right.
In this case, zeros are shifted in on the left. */)
(register Lisp_Object value, Lisp_Object count)
{
return ash_lsh_impl (value, count, true);
}
DEFUN ("1+", Fadd1, Sadd1, 1, 1, 0,
doc: /* Return NUMBER plus one. NUMBER may be a number or a marker.
Markers are converted to integers. */)
(register Lisp_Object number)
{
CHECK_NUMBER_COERCE_MARKER (number);
if (FLOATP (number))
return (make_float (1.0 + XFLOAT_DATA (number)));
if (BIGNUMP (number))
{
mpz_t num;
mpz_init (num);
mpz_add_ui (num, XBIGNUM (number)->value, 1);
number = make_number (num);
mpz_clear (num);
}
else
{
eassume (FIXNUMP (number));
if (XINT (number) < MOST_POSITIVE_FIXNUM)
XSETINT (number, XINT (number) + 1);
else
{
mpz_t num;
mpz_init (num);
mpz_set_intmax (num, XINT (number) + 1);
number = make_number (num);
mpz_clear (num);
}
}
return number;
}
DEFUN ("1-", Fsub1, Ssub1, 1, 1, 0,
doc: /* Return NUMBER minus one. NUMBER may be a number or a marker.
Markers are converted to integers. */)
(register Lisp_Object number)
{
CHECK_NUMBER_COERCE_MARKER (number);
if (FLOATP (number))
return (make_float (-1.0 + XFLOAT_DATA (number)));
if (BIGNUMP (number))
{
mpz_t num;
mpz_init (num);
mpz_sub_ui (num, XBIGNUM (number)->value, 1);
number = make_number (num);
mpz_clear (num);
}
else
{
eassume (FIXNUMP (number));
if (XINT (number) > MOST_NEGATIVE_FIXNUM)
XSETINT (number, XINT (number) - 1);
else
{
mpz_t num;
mpz_init (num);
mpz_set_intmax (num, XINT (number) - 1);
number = make_number (num);
mpz_clear (num);
}
}
return number;
}
DEFUN ("lognot", Flognot, Slognot, 1, 1, 0,
doc: /* Return the bitwise complement of NUMBER. NUMBER must be an integer. */)
(register Lisp_Object number)
{
CHECK_INTEGER (number);
if (BIGNUMP (number))
{
mpz_t value;
mpz_init (value);
mpz_com (value, XBIGNUM (number)->value);
number = make_number (value);
mpz_clear (value);
}
else
{
eassume (FIXNUMP (number));
XSETINT (number, ~XINT (number));
}
return number;
}
DEFUN ("byteorder", Fbyteorder, Sbyteorder, 0, 0, 0,
doc: /* Return the byteorder for the machine.
Returns 66 (ASCII uppercase B) for big endian machines or 108 (ASCII
lowercase l) for small endian machines. */
attributes: const)
(void)
{
unsigned i = 0x04030201;
int order = *(char *)&i == 1 ? 108 : 66;
return make_fixnum (order);
}
/* Because we round up the bool vector allocate size to word_size
units, we can safely read past the "end" of the vector in the
operations below. These extra bits are always zero. */
static bits_word
bool_vector_spare_mask (EMACS_INT nr_bits)
{
return (((bits_word) 1) << (nr_bits % BITS_PER_BITS_WORD)) - 1;
}
/* Info about unsigned long long, falling back on unsigned long
if unsigned long long is not available. */
#if HAVE_UNSIGNED_LONG_LONG_INT && defined ULLONG_WIDTH
enum { ULL_WIDTH = ULLONG_WIDTH };
# define ULL_MAX ULLONG_MAX
#else
enum { ULL_WIDTH = ULONG_WIDTH };
# define ULL_MAX ULONG_MAX
# define count_one_bits_ll count_one_bits_l
# define count_trailing_zeros_ll count_trailing_zeros_l
#endif
/* Shift VAL right by the width of an unsigned long long.
ULL_WIDTH must be less than BITS_PER_BITS_WORD. */
static bits_word
shift_right_ull (bits_word w)
{
/* Pacify bogus GCC warning about shift count exceeding type width. */
int shift = ULL_WIDTH - BITS_PER_BITS_WORD < 0 ? ULL_WIDTH : 0;
return w >> shift;
}
/* Return the number of 1 bits in W. */
static int
count_one_bits_word (bits_word w)
{
if (BITS_WORD_MAX <= UINT_MAX)
return count_one_bits (w);
else if (BITS_WORD_MAX <= ULONG_MAX)
return count_one_bits_l (w);
else
{
int i = 0, count = 0;
while (count += count_one_bits_ll (w),
(i += ULL_WIDTH) < BITS_PER_BITS_WORD)
w = shift_right_ull (w);
return count;
}
}
enum bool_vector_op { bool_vector_exclusive_or,
bool_vector_union,
bool_vector_intersection,
bool_vector_set_difference,
bool_vector_subsetp };
static Lisp_Object
bool_vector_binop_driver (Lisp_Object a,
Lisp_Object b,
Lisp_Object dest,
enum bool_vector_op op)
{
EMACS_INT nr_bits;
bits_word *adata, *bdata, *destdata;
ptrdiff_t i = 0;
ptrdiff_t nr_words;
CHECK_BOOL_VECTOR (a);
CHECK_BOOL_VECTOR (b);
nr_bits = bool_vector_size (a);
if (bool_vector_size (b) != nr_bits)
wrong_length_argument (a, b, dest);
nr_words = bool_vector_words (nr_bits);
adata = bool_vector_data (a);
bdata = bool_vector_data (b);
if (NILP (dest))
{
dest = make_uninit_bool_vector (nr_bits);
destdata = bool_vector_data (dest);
}
else
{
CHECK_BOOL_VECTOR (dest);
destdata = bool_vector_data (dest);
if (bool_vector_size (dest) != nr_bits)
wrong_length_argument (a, b, dest);
switch (op)
{
case bool_vector_exclusive_or:
for (; i < nr_words; i++)
if (destdata[i] != (adata[i] ^ bdata[i]))
goto set_dest;
break;
case bool_vector_subsetp:
for (; i < nr_words; i++)
if (adata[i] &~ bdata[i])
return Qnil;
return Qt;
case bool_vector_union:
for (; i < nr_words; i++)
if (destdata[i] != (adata[i] | bdata[i]))
goto set_dest;
break;
case bool_vector_intersection:
for (; i < nr_words; i++)
if (destdata[i] != (adata[i] & bdata[i]))
goto set_dest;
break;
case bool_vector_set_difference:
for (; i < nr_words; i++)
if (destdata[i] != (adata[i] &~ bdata[i]))
goto set_dest;
break;
}
return Qnil;
}
set_dest:
switch (op)
{
case bool_vector_exclusive_or:
for (; i < nr_words; i++)
destdata[i] = adata[i] ^ bdata[i];
break;
case bool_vector_union:
for (; i < nr_words; i++)
destdata[i] = adata[i] | bdata[i];
break;
case bool_vector_intersection:
for (; i < nr_words; i++)
destdata[i] = adata[i] & bdata[i];
break;
case bool_vector_set_difference:
for (; i < nr_words; i++)
destdata[i] = adata[i] &~ bdata[i];
break;
default:
eassume (0);
}
return dest;
}
/* PRECONDITION must be true. Return VALUE. This odd construction
works around a bogus GCC diagnostic "shift count >= width of type". */
static int
pre_value (bool precondition, int value)
{
eassume (precondition);
return precondition ? value : 0;
}
/* Compute the number of trailing zero bits in val. If val is zero,
return the number of bits in val. */
static int
count_trailing_zero_bits (bits_word val)
{
if (BITS_WORD_MAX == UINT_MAX)
return count_trailing_zeros (val);
if (BITS_WORD_MAX == ULONG_MAX)
return count_trailing_zeros_l (val);
if (BITS_WORD_MAX == ULL_MAX)
return count_trailing_zeros_ll (val);
/* The rest of this code is for the unlikely platform where bits_word differs
in width from unsigned int, unsigned long, and unsigned long long. */
val |= ~ BITS_WORD_MAX;
if (BITS_WORD_MAX <= UINT_MAX)
return count_trailing_zeros (val);
if (BITS_WORD_MAX <= ULONG_MAX)
return count_trailing_zeros_l (val);
else
{
int count;
for (count = 0;
count < BITS_PER_BITS_WORD - ULL_WIDTH;
count += ULL_WIDTH)
{
if (val & ULL_MAX)
return count + count_trailing_zeros_ll (val);
val = shift_right_ull (val);
}
if (BITS_PER_BITS_WORD % ULL_WIDTH != 0
&& BITS_WORD_MAX == (bits_word) -1)
val |= (bits_word) 1 << pre_value (ULONG_MAX < BITS_WORD_MAX,
BITS_PER_BITS_WORD % ULL_WIDTH);
return count + count_trailing_zeros_ll (val);
}
}
static bits_word
bits_word_to_host_endian (bits_word val)
{
#ifndef WORDS_BIGENDIAN
return val;
#else
if (BITS_WORD_MAX >> 31 == 1)
return bswap_32 (val);
# if HAVE_UNSIGNED_LONG_LONG
if (BITS_WORD_MAX >> 31 >> 31 >> 1 == 1)
return bswap_64 (val);
# endif
{
int i;
bits_word r = 0;
for (i = 0; i < sizeof val; i++)
{
r = ((r << 1 << (CHAR_BIT - 1))
| (val & ((1u << 1 << (CHAR_BIT - 1)) - 1)));
val = val >> 1 >> (CHAR_BIT - 1);
}
return r;
}
#endif
}
DEFUN ("bool-vector-exclusive-or", Fbool_vector_exclusive_or,
Sbool_vector_exclusive_or, 2, 3, 0,
doc: /* Return A ^ B, bitwise exclusive or.
If optional third argument C is given, store result into C.
A, B, and C must be bool vectors of the same length.
Return the destination vector if it changed or nil otherwise. */)
(Lisp_Object a, Lisp_Object b, Lisp_Object c)
{
return bool_vector_binop_driver (a, b, c, bool_vector_exclusive_or);
}
DEFUN ("bool-vector-union", Fbool_vector_union,
Sbool_vector_union, 2, 3, 0,
doc: /* Return A | B, bitwise or.
If optional third argument C is given, store result into C.
A, B, and C must be bool vectors of the same length.
Return the destination vector if it changed or nil otherwise. */)
(Lisp_Object a, Lisp_Object b, Lisp_Object c)
{
return bool_vector_binop_driver (a, b, c, bool_vector_union);
}
DEFUN ("bool-vector-intersection", Fbool_vector_intersection,
Sbool_vector_intersection, 2, 3, 0,
doc: /* Return A & B, bitwise and.
If optional third argument C is given, store result into C.
A, B, and C must be bool vectors of the same length.
Return the destination vector if it changed or nil otherwise. */)
(Lisp_Object a, Lisp_Object b, Lisp_Object c)
{
return bool_vector_binop_driver (a, b, c, bool_vector_intersection);
}
DEFUN ("bool-vector-set-difference", Fbool_vector_set_difference,
Sbool_vector_set_difference, 2, 3, 0,
doc: /* Return A &~ B, set difference.
If optional third argument C is given, store result into C.
A, B, and C must be bool vectors of the same length.
Return the destination vector if it changed or nil otherwise. */)
(Lisp_Object a, Lisp_Object b, Lisp_Object c)
{
return bool_vector_binop_driver (a, b, c, bool_vector_set_difference);
}
DEFUN ("bool-vector-subsetp", Fbool_vector_subsetp,
Sbool_vector_subsetp, 2, 2, 0,
doc: /* Return t if every t value in A is also t in B, nil otherwise.
A and B must be bool vectors of the same length. */)
(Lisp_Object a, Lisp_Object b)
{
return bool_vector_binop_driver (a, b, b, bool_vector_subsetp);
}
DEFUN ("bool-vector-not", Fbool_vector_not,
Sbool_vector_not, 1, 2, 0,
doc: /* Compute ~A, set complement.
If optional second argument B is given, store result into B.
A and B must be bool vectors of the same length.
Return the destination vector. */)
(Lisp_Object a, Lisp_Object b)
{
EMACS_INT nr_bits;
bits_word *bdata, *adata;
ptrdiff_t i;
CHECK_BOOL_VECTOR (a);
nr_bits = bool_vector_size (a);
if (NILP (b))
b = make_uninit_bool_vector (nr_bits);
else
{
CHECK_BOOL_VECTOR (b);
if (bool_vector_size (b) != nr_bits)
wrong_length_argument (a, b, Qnil);
}
bdata = bool_vector_data (b);
adata = bool_vector_data (a);
for (i = 0; i < nr_bits / BITS_PER_BITS_WORD; i++)
bdata[i] = BITS_WORD_MAX & ~adata[i];
if (nr_bits % BITS_PER_BITS_WORD)
{
bits_word mword = bits_word_to_host_endian (adata[i]);
mword = ~mword;
mword &= bool_vector_spare_mask (nr_bits);
bdata[i] = bits_word_to_host_endian (mword);
}
return b;
}
DEFUN ("bool-vector-count-population", Fbool_vector_count_population,
Sbool_vector_count_population, 1, 1, 0,
doc: /* Count how many elements in A are t.
A is a bool vector. To count A's nil elements, subtract the return
value from A's length. */)
(Lisp_Object a)
{
EMACS_INT count;
EMACS_INT nr_bits;
bits_word *adata;
ptrdiff_t i, nwords;
CHECK_BOOL_VECTOR (a);
nr_bits = bool_vector_size (a);
nwords = bool_vector_words (nr_bits);
count = 0;
adata = bool_vector_data (a);
for (i = 0; i < nwords; i++)
count += count_one_bits_word (adata[i]);
return make_fixnum (count);
}
DEFUN ("bool-vector-count-consecutive", Fbool_vector_count_consecutive,
Sbool_vector_count_consecutive, 3, 3, 0,
doc: /* Count how many consecutive elements in A equal B starting at I.
A is a bool vector, B is t or nil, and I is an index into A. */)
(Lisp_Object a, Lisp_Object b, Lisp_Object i)
{
EMACS_INT count;
EMACS_INT nr_bits;
int offset;
bits_word *adata;
bits_word twiddle;
bits_word mword; /* Machine word. */
ptrdiff_t pos, pos0;
ptrdiff_t nr_words;
CHECK_BOOL_VECTOR (a);
CHECK_FIXNAT (i);
nr_bits = bool_vector_size (a);
if (XFASTINT (i) > nr_bits) /* Allow one past the end for convenience */
args_out_of_range (a, i);
adata = bool_vector_data (a);
nr_words = bool_vector_words (nr_bits);
pos = XFASTINT (i) / BITS_PER_BITS_WORD;
offset = XFASTINT (i) % BITS_PER_BITS_WORD;
count = 0;
/* By XORing with twiddle, we transform the problem of "count
consecutive equal values" into "count the zero bits". The latter
operation usually has hardware support. */
twiddle = NILP (b) ? 0 : BITS_WORD_MAX;
/* Scan the remainder of the mword at the current offset. */
if (pos < nr_words && offset != 0)
{
mword = bits_word_to_host_endian (adata[pos]);
mword ^= twiddle;
mword >>= offset;
/* Do not count the pad bits. */
mword |= (bits_word) 1 << (BITS_PER_BITS_WORD - offset);
count = count_trailing_zero_bits (mword);
pos++;
if (count + offset < BITS_PER_BITS_WORD)
return make_fixnum (count);
}
/* Scan whole words until we either reach the end of the vector or
find an mword that doesn't completely match. twiddle is
endian-independent. */
pos0 = pos;
while (pos < nr_words && adata[pos] == twiddle)
pos++;
count += (pos - pos0) * BITS_PER_BITS_WORD;
if (pos < nr_words)
{
/* If we stopped because of a mismatch, see how many bits match
in the current mword. */
mword = bits_word_to_host_endian (adata[pos]);
mword ^= twiddle;
count += count_trailing_zero_bits (mword);
}
else if (nr_bits % BITS_PER_BITS_WORD != 0)
{
/* If we hit the end, we might have overshot our count. Reduce
the total by the number of spare bits at the end of the
vector. */
count -= BITS_PER_BITS_WORD - nr_bits % BITS_PER_BITS_WORD;
}
return make_fixnum (count);
}
void
syms_of_data (void)
{
Lisp_Object error_tail, arith_tail;
DEFSYM (Qquote, "quote");
DEFSYM (Qlambda, "lambda");
DEFSYM (Qerror_conditions, "error-conditions");
DEFSYM (Qerror_message, "error-message");
DEFSYM (Qtop_level, "top-level");
DEFSYM (Qerror, "error");
DEFSYM (Quser_error, "user-error");
DEFSYM (Qquit, "quit");
DEFSYM (Qwrong_length_argument, "wrong-length-argument");
DEFSYM (Qwrong_type_argument, "wrong-type-argument");
DEFSYM (Qargs_out_of_range, "args-out-of-range");
DEFSYM (Qvoid_function, "void-function");
DEFSYM (Qcyclic_function_indirection, "cyclic-function-indirection");
DEFSYM (Qcyclic_variable_indirection, "cyclic-variable-indirection");
DEFSYM (Qvoid_variable, "void-variable");
DEFSYM (Qsetting_constant, "setting-constant");
DEFSYM (Qtrapping_constant, "trapping-constant");
DEFSYM (Qinvalid_read_syntax, "invalid-read-syntax");
DEFSYM (Qinvalid_function, "invalid-function");
DEFSYM (Qwrong_number_of_arguments, "wrong-number-of-arguments");
DEFSYM (Qno_catch, "no-catch");
DEFSYM (Qend_of_file, "end-of-file");
DEFSYM (Qarith_error, "arith-error");
DEFSYM (Qbeginning_of_buffer, "beginning-of-buffer");
DEFSYM (Qend_of_buffer, "end-of-buffer");
DEFSYM (Qbuffer_read_only, "buffer-read-only");
DEFSYM (Qtext_read_only, "text-read-only");
DEFSYM (Qmark_inactive, "mark-inactive");
DEFSYM (Qlistp, "listp");
DEFSYM (Qconsp, "consp");
DEFSYM (Qsymbolp, "symbolp");
DEFSYM (Qfixnump, "fixnump");
DEFSYM (Qintegerp, "integerp");
DEFSYM (Qnatnump, "natnump");
DEFSYM (Qwholenump, "wholenump");
DEFSYM (Qstringp, "stringp");
DEFSYM (Qarrayp, "arrayp");
DEFSYM (Qsequencep, "sequencep");
DEFSYM (Qbufferp, "bufferp");
DEFSYM (Qvectorp, "vectorp");
DEFSYM (Qrecordp, "recordp");
DEFSYM (Qbool_vector_p, "bool-vector-p");
DEFSYM (Qchar_or_string_p, "char-or-string-p");
DEFSYM (Qmarkerp, "markerp");
#ifdef HAVE_MODULES
DEFSYM (Quser_ptrp, "user-ptrp");
#endif
DEFSYM (Qbuffer_or_string_p, "buffer-or-string-p");
DEFSYM (Qinteger_or_marker_p, "integer-or-marker-p");
DEFSYM (Qfboundp, "fboundp");
DEFSYM (Qfloatp, "floatp");
DEFSYM (Qnumberp, "numberp");
DEFSYM (Qnumber_or_marker_p, "number-or-marker-p");
DEFSYM (Qchar_table_p, "char-table-p");
DEFSYM (Qvector_or_char_table_p, "vector-or-char-table-p");
DEFSYM (Qsubrp, "subrp");
DEFSYM (Qunevalled, "unevalled");
DEFSYM (Qmany, "many");
DEFSYM (Qcdr, "cdr");
error_tail = pure_cons (Qerror, Qnil);
/* ERROR is used as a signaler for random errors for which nothing else is
right. */
Fput (Qerror, Qerror_conditions,
error_tail);
Fput (Qerror, Qerror_message,
build_pure_c_string ("error"));
#define PUT_ERROR(sym, tail, msg) \
Fput (sym, Qerror_conditions, pure_cons (sym, tail)); \
Fput (sym, Qerror_message, build_pure_c_string (msg))
PUT_ERROR (Qquit, Qnil, "Quit");
PUT_ERROR (Quser_error, error_tail, "");
PUT_ERROR (Qwrong_length_argument, error_tail, "Wrong length argument");
PUT_ERROR (Qwrong_type_argument, error_tail, "Wrong type argument");
PUT_ERROR (Qargs_out_of_range, error_tail, "Args out of range");
PUT_ERROR (Qvoid_function, error_tail,
"Symbol's function definition is void");
PUT_ERROR (Qcyclic_function_indirection, error_tail,
"Symbol's chain of function indirections contains a loop");
PUT_ERROR (Qcyclic_variable_indirection, error_tail,
"Symbol's chain of variable indirections contains a loop");
DEFSYM (Qcircular_list, "circular-list");
PUT_ERROR (Qcircular_list, error_tail, "List contains a loop");
PUT_ERROR (Qvoid_variable, error_tail, "Symbol's value as variable is void");
PUT_ERROR (Qsetting_constant, error_tail,
"Attempt to set a constant symbol");
PUT_ERROR (Qtrapping_constant, error_tail,
"Attempt to trap writes to a constant symbol");
PUT_ERROR (Qinvalid_read_syntax, error_tail, "Invalid read syntax");
PUT_ERROR (Qinvalid_function, error_tail, "Invalid function");
PUT_ERROR (Qwrong_number_of_arguments, error_tail,
"Wrong number of arguments");
PUT_ERROR (Qno_catch, error_tail, "No catch for tag");
PUT_ERROR (Qend_of_file, error_tail, "End of file during parsing");
arith_tail = pure_cons (Qarith_error, error_tail);
Fput (Qarith_error, Qerror_conditions, arith_tail);
Fput (Qarith_error, Qerror_message, build_pure_c_string ("Arithmetic error"));
PUT_ERROR (Qbeginning_of_buffer, error_tail, "Beginning of buffer");
PUT_ERROR (Qend_of_buffer, error_tail, "End of buffer");
PUT_ERROR (Qbuffer_read_only, error_tail, "Buffer is read-only");
PUT_ERROR (Qtext_read_only, pure_cons (Qbuffer_read_only, error_tail),
"Text is read-only");
DEFSYM (Qrange_error, "range-error");
DEFSYM (Qdomain_error, "domain-error");
DEFSYM (Qsingularity_error, "singularity-error");
DEFSYM (Qoverflow_error, "overflow-error");
DEFSYM (Qunderflow_error, "underflow-error");
PUT_ERROR (Qdomain_error, arith_tail, "Arithmetic domain error");
PUT_ERROR (Qrange_error, arith_tail, "Arithmetic range error");
PUT_ERROR (Qsingularity_error, Fcons (Qdomain_error, arith_tail),
"Arithmetic singularity error");
PUT_ERROR (Qoverflow_error, Fcons (Qdomain_error, arith_tail),
"Arithmetic overflow error");
PUT_ERROR (Qunderflow_error, Fcons (Qdomain_error, arith_tail),
"Arithmetic underflow error");
/* Types that type-of returns. */
DEFSYM (Qinteger, "integer");
DEFSYM (Qsymbol, "symbol");
DEFSYM (Qstring, "string");
DEFSYM (Qcons, "cons");
DEFSYM (Qmarker, "marker");
DEFSYM (Qoverlay, "overlay");
DEFSYM (Qfinalizer, "finalizer");
#ifdef HAVE_MODULES
DEFSYM (Qmodule_function, "module-function");
DEFSYM (Quser_ptr, "user-ptr");
#endif
DEFSYM (Qfloat, "float");
DEFSYM (Qwindow_configuration, "window-configuration");
DEFSYM (Qprocess, "process");
DEFSYM (Qwindow, "window");
DEFSYM (Qsubr, "subr");
DEFSYM (Qcompiled_function, "compiled-function");
DEFSYM (Qbuffer, "buffer");
DEFSYM (Qframe, "frame");
DEFSYM (Qvector, "vector");
DEFSYM (Qrecord, "record");
DEFSYM (Qchar_table, "char-table");
DEFSYM (Qbool_vector, "bool-vector");
DEFSYM (Qhash_table, "hash-table");
DEFSYM (Qthread, "thread");
DEFSYM (Qmutex, "mutex");
DEFSYM (Qcondition_variable, "condition-variable");
DEFSYM (Qfont_spec, "font-spec");
DEFSYM (Qfont_entity, "font-entity");
DEFSYM (Qfont_object, "font-object");
DEFSYM (Qterminal, "terminal");
DEFSYM (Qdefun, "defun");
DEFSYM (Qinteractive_form, "interactive-form");
DEFSYM (Qdefalias_fset_function, "defalias-fset-function");
defsubr (&Sindirect_variable);
defsubr (&Sinteractive_form);
defsubr (&Seq);
defsubr (&Snull);
defsubr (&Stype_of);
defsubr (&Slistp);
defsubr (&Snlistp);
defsubr (&Sconsp);
defsubr (&Satom);
defsubr (&Sintegerp);
defsubr (&Sfixnump);
defsubr (&Sinteger_or_marker_p);
defsubr (&Snumberp);
defsubr (&Snumber_or_marker_p);
defsubr (&Sfloatp);
defsubr (&Snatnump);
defsubr (&Ssymbolp);
defsubr (&Skeywordp);
defsubr (&Sstringp);
defsubr (&Smultibyte_string_p);
defsubr (&Svectorp);
defsubr (&Srecordp);
defsubr (&Schar_table_p);
defsubr (&Svector_or_char_table_p);
defsubr (&Sbool_vector_p);
defsubr (&Sarrayp);
defsubr (&Ssequencep);
defsubr (&Sbufferp);
defsubr (&Smarkerp);
defsubr (&Ssubrp);
defsubr (&Sbyte_code_function_p);
defsubr (&Smodule_function_p);
defsubr (&Schar_or_string_p);
defsubr (&Sthreadp);
defsubr (&Smutexp);
defsubr (&Scondition_variable_p);
defsubr (&Sbignump);
defsubr (&Scar);
defsubr (&Scdr);
defsubr (&Scar_safe);
defsubr (&Scdr_safe);
defsubr (&Ssetcar);
defsubr (&Ssetcdr);
defsubr (&Ssymbol_function);
defsubr (&Sindirect_function);
defsubr (&Ssymbol_plist);
defsubr (&Ssymbol_name);
defsubr (&Smakunbound);
defsubr (&Sfmakunbound);
defsubr (&Sboundp);
defsubr (&Sfboundp);
defsubr (&Sfset);
defsubr (&Sdefalias);
defsubr (&Ssetplist);
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 (&Slocal_variable_p);
defsubr (&Slocal_variable_if_set_p);
defsubr (&Svariable_binding_locus);
defsubr (&Saref);
defsubr (&Saset);
defsubr (&Snumber_to_string);
defsubr (&Sstring_to_number);
defsubr (&Seqlsign);
defsubr (&Slss);
defsubr (&Sgtr);
defsubr (&Sleq);
defsubr (&Sgeq);
defsubr (&Sneq);
defsubr (&Splus);
defsubr (&Sminus);
defsubr (&Stimes);
defsubr (&Squo);
defsubr (&Srem);
defsubr (&Smod);
defsubr (&Smax);
defsubr (&Smin);
defsubr (&Slogand);
defsubr (&Slogior);
defsubr (&Slogxor);
defsubr (&Slogcount);
defsubr (&Slsh);
defsubr (&Sash);
defsubr (&Sadd1);
defsubr (&Ssub1);
defsubr (&Slognot);
defsubr (&Sbyteorder);
defsubr (&Ssubr_arity);
defsubr (&Ssubr_name);
#ifdef HAVE_MODULES
defsubr (&Suser_ptrp);
#endif
defsubr (&Sbool_vector_exclusive_or);
defsubr (&Sbool_vector_union);
defsubr (&Sbool_vector_intersection);
defsubr (&Sbool_vector_set_difference);
defsubr (&Sbool_vector_not);
defsubr (&Sbool_vector_subsetp);
defsubr (&Sbool_vector_count_consecutive);
defsubr (&Sbool_vector_count_population);
set_symbol_function (Qwholenump, XSYMBOL (Qnatnump)->u.s.function);
DEFVAR_LISP ("most-positive-fixnum", Vmost_positive_fixnum,
doc: /* The largest value that is representable in a Lisp integer.
This variable cannot be set; trying to do so will signal an error. */);
Vmost_positive_fixnum = make_fixnum (MOST_POSITIVE_FIXNUM);
make_symbol_constant (intern_c_string ("most-positive-fixnum"));
DEFVAR_LISP ("most-negative-fixnum", Vmost_negative_fixnum,
doc: /* The smallest value that is representable in a Lisp integer.
This variable cannot be set; trying to do so will signal an error. */);
Vmost_negative_fixnum = make_fixnum (MOST_NEGATIVE_FIXNUM);
make_symbol_constant (intern_c_string ("most-negative-fixnum"));
DEFSYM (Qwatchers, "watchers");
DEFSYM (Qmakunbound, "makunbound");
DEFSYM (Qunlet, "unlet");
DEFSYM (Qset, "set");
DEFSYM (Qset_default, "set-default");
defsubr (&Sadd_variable_watcher);
defsubr (&Sremove_variable_watcher);
defsubr (&Sget_variable_watchers);
}
|