File: cl.texi

package info (click to toggle)
xemacs21 21.4.24-8
  • links: PTS
  • area: main
  • in suites: buster
  • size: 33,604 kB
  • sloc: ansic: 243,821; lisp: 94,071; cpp: 5,726; sh: 4,406; perl: 1,096; cs: 775; makefile: 761; python: 279; asm: 248; lex: 119; yacc: 95; sed: 22; csh: 9
file content (5736 lines) | stat: -rw-r--r-- 230,077 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
\input texinfo    @c -*-texinfo-*-
@setfilename ../info/cl.info
@settitle Common Lisp Extensions

@iftex
@finalout
@end iftex

@ifinfo
@dircategory XEmacs Editor
@direntry
* Common Lisp: (cl).		GNU Emacs Common Lisp emulation package.
@end direntry

This file documents the GNU Emacs Common Lisp emulation package.

Copyright (C) 1993 Free Software Foundation, Inc.

Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.

@ignore
Permission is granted to process this file through TeX and print the
results, provided the printed document carries copying permission notice
identical to this one except for the removal of this paragraph (this
paragraph not being relevant to the printed manual).

@end ignore
Permission is granted to copy and distribute modified versions of this
manual under the conditions for verbatim copying, provided also that the
section entitled ``GNU General Public License'' is included exactly as
in the original, and provided that the entire resulting derived work is
distributed under the terms of a permission notice identical to this one.

Permission is granted to copy and distribute translations of this manual
into another language, under the above conditions for modified versions,
except that the section entitled ``GNU General Public License'' may be
included in a translation approved by the author instead of in the
original English.
@end ifinfo

@titlepage
@sp 6
@center @titlefont{Common Lisp Extensions}
@sp 4
@center For GNU Emacs Lisp
@sp 1
@center Version 2.02
@sp 5
@center Dave Gillespie
@center daveg@@synaptics.com
@page

@vskip 0pt plus 1filll
Copyright @copyright{} 1993 Free Software Foundation, Inc.

Permission is granted to make and distribute verbatim copies of
this manual provided the copyright notice and this permission notice
are preserved on all copies.

@ignore
Permission is granted to process this file through TeX and print the
results, provided the printed document carries copying permission notice
identical to this one except for the removal of this paragraph (this
paragraph not being relevant to the printed manual).

@end ignore
Permission is granted to copy and distribute modified versions of this
manual under the conditions for verbatim copying, provided also that the
section entitled ``GNU General Public License'' is included exactly as
in the original, and provided that the entire resulting derived work is
distributed under the terms of a permission notice identical to this one.

Permission is granted to copy and distribute translations of this manual
into another language, under the above conditions for modified versions,
except that the section entitled ``GNU General Public License'' may be
included in a translation approved by the author instead of in the
original English.
@end titlepage

@node Top, Overview,, (dir)
@chapter Common Lisp Extensions

@noindent
This document describes a set of Emacs Lisp facilities borrowed from
Common Lisp.  All the facilities are described here in detail; for
more discussion and examples, Guy L. Steele's @cite{Common Lisp, the
Language}, second edition, is the definitive book on Common Lisp.
@iftex
Chapter numbers and most section numbers of this document parallel
those of Steele's book.
@end iftex
While this document does not assume any prior knowledge of Common
Lisp, it does assume a basic familiarity with Emacs Lisp.

@menu
* Overview::             Installation, usage, etc.
* Program Structure::    Arglists, `eval-when', `defalias'
* Predicates::           `typep', `eql', and `equalp'
* Control Structure::    `setf', `when', `do', `loop', etc.
* Macros::               Destructuring, `define-compiler-macro'
* Declarations::         `proclaim', `declare', etc.
* Symbols::              Property lists, `gensym'
* Numbers::              Predicates, functions, random numbers
* Sequences::            Mapping, functions, searching, sorting
* Lists::                `cadr', `sublis', `member*', `assoc*', etc.
* Hash Tables::          `make-hash-table', `gethash', etc.
* Structures::           `defstruct'
* Assertions::           `check-type', `assert', `ignore-errors'.

* Efficiency Concerns::         Hints and techniques
* Common Lisp Compatibility::   All known differences with Steele
* Old CL Compatibility::        All known differences with old cl.el
* Porting Common Lisp::         Hints for porting Common Lisp code

* Function Index::
* Variable Index::
@end menu

@node Overview, Program Structure, Top, Top
@ifinfo
@chapter Overview
@end ifinfo
@iftex
@section Overview
@end iftex

@noindent
Common Lisp is a huge language, and Common Lisp systems tend to be
massive and extremely complex.  Emacs Lisp, by contrast, is rather
minimalist in the choice of Lisp features it offers the programmer.
As Emacs Lisp programmers have grown in number, and the applications
they write have grown more ambitious, it has become clear that Emacs
Lisp could benefit from many of the conveniences of Common Lisp.

The @dfn{CL} package adds a number of Common Lisp functions and
control structures to Emacs Lisp.  While not a 100% complete
implementation of Common Lisp, @dfn{CL} adds enough functionality
to make Emacs Lisp programming significantly more convenient.

Some Common Lisp features have been omitted from this package
for various reasons:

@itemize @bullet
@item
Some features are too complex or bulky relative to their benefit
to Emacs Lisp programmers.  CLOS and Common Lisp streams are fine
examples of this group.

@item
Other features cannot be implemented without modification to the
Emacs Lisp interpreter itself, such as multiple return values,
lexical scoping, case-insensitive symbols, and complex numbers.
The @dfn{CL} package generally makes no attempt to emulate these
features.

@item
Some features conflict with existing things in Emacs Lisp.  For
example, Emacs' @code{assoc} function is incompatible with the
Common Lisp @code{assoc}.  In such cases, this package usually
adds the suffix @samp{*} to the function name of the Common
Lisp version of the function (e.g., @code{assoc*}).
@end itemize

The package described here was written by Dave Gillespie,
@file{daveg@@synaptics.com}.  It is a total rewrite of the original
1986 @file{cl.el} package by Cesar Quiroz.  Most features of
the Quiroz package have been retained; any incompatibilities are
noted in the descriptions below.  Care has been taken in this
version to ensure that each function is defined efficiently,
concisely, and with minimal impact on the rest of the Emacs
environment.

@menu
* Usage::                How to use the CL package
* Organization::         The package's five component files
* Installation::         Compiling and installing CL
* Naming Conventions::   Notes on CL function names
@end menu

@node Usage, Organization, Overview, Overview
@section Usage

@noindent
Lisp code that uses features from the @dfn{CL} package should
include at the beginning:

@example
(require 'cl)
@end example

@noindent
If you want to ensure that the new (Gillespie) version of @dfn{CL}
is the one that is present, add an additional @code{(require 'cl-19)}
call:

@example
(require 'cl)
(require 'cl-19)
@end example

@noindent
The second call will fail (with ``@file{cl-19.el} not found'') if
the old @file{cl.el} package was in use.

It is safe to arrange to load @dfn{CL} at all times, e.g.,
in your @file{.emacs} file.  But it's a good idea, for portability,
to @code{(require 'cl)} in your code even if you do this.

@node Organization, Installation, Usage, Overview
@section Organization

@noindent
The Common Lisp package is organized into four files:

@table @file
@item cl.el
This is the ``main'' file, which contains basic functions
and information about the package.  This file is relatively
compact---about 700 lines.

@item cl-extra.el
This file contains the larger, more complex or unusual functions.
It is kept separate so that packages which only want to use Common
Lisp fundamentals like the @code{cadr} function won't need to pay
the overhead of loading the more advanced functions.

@item cl-seq.el
This file contains most of the advanced functions for operating
on sequences or lists, such as @code{delete-if} and @code{assoc*}.

@item cl-macs.el
This file contains the features of the packages which are macros
instead of functions.  Macros expand when the caller is compiled,
not when it is run, so the macros generally only need to be
present when the byte-compiler is running (or when the macros are
used in uncompiled code such as a @file{.emacs} file).  Most of
the macros of this package are isolated in @file{cl-macs.el} so
that they won't take up memory unless you are compiling.
@end table

The file @file{cl.el} includes all necessary @code{autoload}
commands for the functions and macros in the other three files.
All you have to do is @code{(require 'cl)}, and @file{cl.el}
will take care of pulling in the other files when they are
needed.

There is another file, @file{cl-compat.el}, which defines some
routines from the older @file{cl.el} package that are no longer
present in the new package.  This includes internal routines
like @code{setelt} and @code{zip-lists}, deprecated features
like @code{defkeyword}, and an emulation of the old-style
multiple-values feature.  @xref{Old CL Compatibility}.

@node Installation, Naming Conventions, Organization, Overview
@section Installation

@noindent
Installation of the @dfn{CL} package is simple:  Just put the
byte-compiled files @file{cl.elc}, @file{cl-extra.elc},
@file{cl-seq.elc}, @file{cl-macs.elc}, and @file{cl-compat.elc}
into a directory on your @code{load-path}.

There are no special requirements to compile this package:
The files do not have to be loaded before they are compiled,
nor do they need to be compiled in any particular order.

You may choose to put the files into your main @file{lisp/}
directory, replacing the original @file{cl.el} file there.  Or,
you could put them into a directory that comes before @file{lisp/}
on your @code{load-path} so that the old @file{cl.el} is
effectively hidden.

Also, format the @file{cl.texinfo} file and put the resulting
Info files in the @file{info/} directory or another suitable place.

You may instead wish to leave this package's components all in
their own directory, and then add this directory to your
@code{load-path} and (Emacs 19 only) @code{Info-directory-list}.
Add the directory to the front of the list so the old @dfn{CL}
package and its documentation are hidden.

@node Naming Conventions, , Installation, Overview
@section Naming Conventions

@noindent
Except where noted, all functions defined by this package have the
same names and calling conventions as their Common Lisp counterparts.

Following is a complete list of functions whose names were changed
from Common Lisp, usually to avoid conflicts with Emacs.  In each
case, a @samp{*} has been appended to the Common Lisp name to obtain
the Emacs name:

@example
defun*        defsubst*     defmacro*     function*
member*       assoc*        rassoc*       remove*
delete*       mapcar*       sort*         floor*
ceiling*      truncate*     round*        mod*
rem*          random*
@end example

Internal function and variable names in the package are prefixed
by @code{cl-}.  Here is a complete list of functions @emph{not}
prefixed by @code{cl-} which were not taken from Common Lisp:

@example
member        delete        remove        remq
rassoc        floatp-safe   lexical-let   lexical-let*
callf         callf2        letf          letf*
defsubst*     defalias      add-hook      eval-when-compile
@end example

@noindent
(Most of these are Emacs 19 features provided to Emacs 18 users,
or introduced, like @code{remq}, for reasons of symmetry
with similar features.)

The following simple functions and macros are defined in @file{cl.el};
they do not cause other components like @file{cl-extra} to be loaded.

@example
eql           floatp-safe   abs           endp
evenp         oddp          plusp         minusp
last          butlast       nbutlast      caar .. cddddr
list*         ldiff         rest          first .. tenth
member [1]    copy-list     subst         mapcar* [2]
adjoin [3]    acons         pairlis       when
unless        pop [4]       push [4]      pushnew [3,4]
incf [4]      decf [4]      proclaim      declaim
add-hook
@end example

@noindent
[1] This is the Emacs 19-compatible function, not @code{member*}.

@noindent
[2] Only for one sequence argument or two list arguments.

@noindent
[3] Only if @code{:test} is @code{eq}, @code{equal}, or unspecified,
and @code{:key} is not used.

@noindent
[4] Only when @var{place} is a plain variable name.

@iftex
@chapno=4
@end iftex

@node Program Structure, Predicates, Overview, Top
@chapter Program Structure

@noindent
This section describes features of the @dfn{CL} package which have to
do with programs as a whole: advanced argument lists for functions,
and the @code{eval-when} construct.

@menu
* Argument Lists::       `&key', `&aux', `defun*', `defmacro*'.
* Time of Evaluation::   The `eval-when' construct.
* Function Aliases::     The `defalias' function.
@end menu

@iftex
@secno=1
@end iftex

@node Argument Lists, Time of Evaluation, Program Structure, Program Structure
@section Argument Lists

@noindent
Emacs Lisp's notation for argument lists of functions is a subset of
the Common Lisp notation.  As well as the familiar @code{&optional}
and @code{&rest} markers, Common Lisp allows you to specify default
values for optional arguments, and it provides the additional markers
@code{&key} and @code{&aux}.

Since argument parsing is built-in to Emacs, there is no way for
this package to implement Common Lisp argument lists seamlessly.
Instead, this package defines alternates for several Lisp forms
which you must use if you need Common Lisp argument lists.

@defspec defun* name arglist body...
This form is identical to the regular @code{defun} form, except
that @var{arglist} is allowed to be a full Common Lisp argument
list.  Also, the function body is enclosed in an implicit block
called @var{name}; @pxref{Blocks and Exits}.
@end defspec

@defspec defsubst* name arglist body...
This is just like @code{defun*}, except that the function that
is defined is automatically proclaimed @code{inline}, i.e.,
calls to it may be expanded into in-line code by the byte compiler.
This is analogous to the @code{defsubst} form in Emacs 19;
@code{defsubst*} uses a different method (compiler macros) which
works in all version of Emacs, and also generates somewhat more
efficient inline expansions.  In particular, @code{defsubst*}
arranges for the processing of keyword arguments, default values,
etc., to be done at compile-time whenever possible.
@end defspec

@defspec defmacro* name arglist body...
This is identical to the regular @code{defmacro} form,
except that @var{arglist} is allowed to be a full Common Lisp
argument list.  The @code{&environment} keyword is supported as
described in Steele.  The @code{&whole} keyword is supported only
within destructured lists (see below); top-level @code{&whole}
cannot be implemented with the current Emacs Lisp interpreter.
The macro expander body is enclosed in an implicit block called
@var{name}.
@end defspec

@defspec function* symbol-or-lambda
This is identical to the regular @code{function} form,
except that if the argument is a @code{lambda} form then that
form may use a full Common Lisp argument list.
@end defspec

Also, all forms (such as @code{defsetf} and @code{flet}) defined
in this package that include @var{arglist}s in their syntax allow
full Common Lisp argument lists.

Note that it is @emph{not} necessary to use @code{defun*} in
order to have access to most @dfn{CL} features in your function.
These features are always present; @code{defun*}'s only
difference from @code{defun} is its more flexible argument
lists and its implicit block.

The full form of a Common Lisp argument list is

@example
(@var{var}...
 &optional (@var{var} @var{initform} @var{svar})...
 &rest @var{var}
 &key ((@var{keyword} @var{var}) @var{initform} @var{svar})...
 &aux (@var{var} @var{initform})...)
@end example

Each of the five argument list sections is optional.  The @var{svar},
@var{initform}, and @var{keyword} parts are optional; if they are
omitted, then @samp{(@var{var})} may be written simply @samp{@var{var}}.

The first section consists of zero or more @dfn{required} arguments.
These arguments must always be specified in a call to the function;
there is no difference between Emacs Lisp and Common Lisp as far as
required arguments are concerned.

The second section consists of @dfn{optional} arguments.  These
arguments may be specified in the function call; if they are not,
@var{initform} specifies the default value used for the argument.
(No @var{initform} means to use @code{nil} as the default.)  The
@var{initform} is evaluated with the bindings for the preceding
arguments already established; @code{(a &optional (b (1+ a)))}
matches one or two arguments, with the second argument defaulting
to one plus the first argument.  If the @var{svar} is specified,
it is an auxiliary variable which is bound to @code{t} if the optional
argument was specified, or to @code{nil} if the argument was omitted.
If you don't use an @var{svar}, then there will be no way for your
function to tell whether it was called with no argument, or with
the default value passed explicitly as an argument.

The third section consists of a single @dfn{rest} argument.  If
more arguments were passed to the function than are accounted for
by the required and optional arguments, those extra arguments are
collected into a list and bound to the ``rest'' argument variable.
Common Lisp's @code{&rest} is equivalent to that of Emacs Lisp.
Common Lisp accepts @code{&body} as a synonym for @code{&rest} in
macro contexts; this package accepts it all the time.

The fourth section consists of @dfn{keyword} arguments.  These
are optional arguments which are specified by name rather than
positionally in the argument list.  For example,

@example
(defun* foo (a &optional b &key c d (e 17)))
@end example

@noindent
defines a function which may be called with one, two, or more
arguments.  The first two arguments are bound to @code{a} and
@code{b} in the usual way.  The remaining arguments must be
pairs of the form @code{:c}, @code{:d}, or @code{:e} followed
by the value to be bound to the corresponding argument variable.
(Symbols whose names begin with a colon are called @dfn{keywords},
and they are self-quoting in the same way as @code{nil} and
@code{t}.)

For example, the call @code{(foo 1 2 :d 3 :c 4)} sets the five
arguments to 1, 2, 4, 3, and 17, respectively.  If the same keyword
appears more than once in the function call, the first occurrence
takes precedence over the later ones.  Note that it is not possible
to specify keyword arguments without specifying the optional
argument @code{b} as well, since @code{(foo 1 :c 2)} would bind
@code{b} to the keyword @code{:c}, then signal an error because
@code{2} is not a valid keyword.

If a @var{keyword} symbol is explicitly specified in the argument
list as shown in the above diagram, then that keyword will be
used instead of just the variable name prefixed with a colon.
You can specify a @var{keyword} symbol which does not begin with
a colon at all, but such symbols will not be self-quoting; you
will have to quote them explicitly with an apostrophe in the
function call.

Ordinarily it is an error to pass an unrecognized keyword to
a function, e.g., @code{(foo 1 2 :c 3 :goober 4)}.  You can ask
Lisp to ignore unrecognized keywords, either by adding the
marker @code{&allow-other-keys} after the keyword section
of the argument list, or by specifying an @code{:allow-other-keys}
argument in the call whose value is non-@code{nil}.  If the
function uses both @code{&rest} and @code{&key} at the same time,
the ``rest'' argument is bound to the keyword list as it appears
in the call.  For example:

@smallexample
(defun* find-thing (thing &rest rest &key need &allow-other-keys)
  (or (apply 'member* thing thing-list :allow-other-keys t rest)
      (if need (error "Thing not found"))))
@end smallexample

@noindent
This function takes a @code{:need} keyword argument, but also
accepts other keyword arguments which are passed on to the
@code{member*} function.  @code{allow-other-keys} is used to
keep both @code{find-thing} and @code{member*} from complaining
about each others' keywords in the arguments.

As a (significant) performance optimization, this package
implements the scan for keyword arguments by calling @code{memq}
to search for keywords in a ``rest'' argument.  Technically
speaking, this is incorrect, since @code{memq} looks at the
odd-numbered values as well as the even-numbered keywords.
The net effect is that if you happen to pass a keyword symbol
as the @emph{value} of another keyword argument, where that
keyword symbol happens to equal the name of a valid keyword
argument of the same function, then the keyword parser will
become confused.  This minor bug can only affect you if you
use keyword symbols as general-purpose data in your program;
this practice is strongly discouraged in Emacs Lisp.

The fifth section of the argument list consists of @dfn{auxiliary
variables}.  These are not really arguments at all, but simply
variables which are bound to @code{nil} or to the specified
@var{initforms} during execution of the function.  There is no
difference between the following two functions, except for a
matter of stylistic taste:

@example
(defun* foo (a b &aux (c (+ a b)) d)
  @var{body})

(defun* foo (a b)
  (let ((c (+ a b)) d)
    @var{body}))
@end example

Argument lists support @dfn{destructuring}.  In Common Lisp,
destructuring is only allowed with @code{defmacro}; this package
allows it with @code{defun*} and other argument lists as well.
In destructuring, any argument variable (@var{var} in the above
diagram) can be replaced by a list of variables, or more generally,
a recursive argument list.  The corresponding argument value must
be a list whose elements match this recursive argument list.
For example:

@example
(defmacro* dolist ((var listform &optional resultform)
                   &rest body)
  ...)
@end example

This says that the first argument of @code{dolist} must be a list
of two or three items; if there are other arguments as well as this
list, they are stored in @code{body}.  All features allowed in
regular argument lists are allowed in these recursive argument lists.
In addition, the clause @samp{&whole @var{var}} is allowed at the
front of a recursive argument list.  It binds @var{var} to the
whole list being matched; thus @code{(&whole all a b)} matches
a list of two things, with @code{a} bound to the first thing,
@code{b} bound to the second thing, and @code{all} bound to the
list itself.  (Common Lisp allows @code{&whole} in top-level
@code{defmacro} argument lists as well, but Emacs Lisp does not
support this usage.)

One last feature of destructuring is that the argument list may be
dotted, so that the argument list @code{(a b . c)} is functionally
equivalent to @code{(a b &rest c)}.

If the optimization quality @code{safety} is set to 0
(@pxref{Declarations}), error checking for wrong number of
arguments and invalid keyword arguments is disabled.  By default,
argument lists are rigorously checked.

@node Time of Evaluation, Function Aliases, Argument Lists, Program Structure
@section Time of Evaluation

@noindent
Normally, the byte-compiler does not actually execute the forms in
a file it compiles.  For example, if a file contains @code{(setq foo t)},
the act of compiling it will not actually set @code{foo} to @code{t}.
This is true even if the @code{setq} was a top-level form (i.e., not
enclosed in a @code{defun} or other form).  Sometimes, though, you
would like to have certain top-level forms evaluated at compile-time.
For example, the compiler effectively evaluates @code{defmacro} forms
at compile-time so that later parts of the file can refer to the
macros that are defined.

@defspec eval-when (situations...) forms...
This form controls when the body @var{forms} are evaluated.
The @var{situations} list may contain any set of the symbols
@code{compile}, @code{load}, and @code{eval} (or their long-winded
ANSI equivalents, @code{:compile-toplevel}, @code{:load-toplevel},
and @code{:execute}).

The @code{eval-when} form is handled differently depending on
whether or not it is being compiled as a top-level form.
Specifically, it gets special treatment if it is being compiled
by a command such as @code{byte-compile-file} which compiles files
or buffers of code, and it appears either literally at the
top level of the file or inside a top-level @code{progn}.

For compiled top-level @code{eval-when}s, the body @var{forms} are
executed at compile-time if @code{compile} is in the @var{situations}
list, and the @var{forms} are written out to the file (to be executed
at load-time) if @code{load} is in the @var{situations} list.

For non-compiled-top-level forms, only the @code{eval} situation is
relevant.  (This includes forms executed by the interpreter, forms
compiled with @code{byte-compile} rather than @code{byte-compile-file},
and non-top-level forms.)  The @code{eval-when} acts like a
@code{progn} if @code{eval} is specified, and like @code{nil}
(ignoring the body @var{forms}) if not.

The rules become more subtle when @code{eval-when}s are nested;
consult Steele (second edition) for the gruesome details (and
some gruesome examples).

Some simple examples:

@example
;; Top-level forms in foo.el:
(eval-when (compile)           (setq foo1 'bar))
(eval-when (load)              (setq foo2 'bar))
(eval-when (compile load)      (setq foo3 'bar))
(eval-when (eval)              (setq foo4 'bar))
(eval-when (eval compile)      (setq foo5 'bar))
(eval-when (eval load)         (setq foo6 'bar))
(eval-when (eval compile load) (setq foo7 'bar))
@end example

When @file{foo.el} is compiled, these variables will be set during
the compilation itself:

@example
foo1  foo3  foo5  foo7      ; `compile'
@end example

When @file{foo.elc} is loaded, these variables will be set:

@example
foo2  foo3  foo6  foo7      ; `load'
@end example

And if @file{foo.el} is loaded uncompiled, these variables will
be set:

@example
foo4  foo5  foo6  foo7      ; `eval'
@end example

If these seven @code{eval-when}s had been, say, inside a @code{defun},
then the first three would have been equivalent to @code{nil} and the
last four would have been equivalent to the corresponding @code{setq}s.

Note that @code{(eval-when (load eval) @dots{})} is equivalent
to @code{(progn @dots{})} in all contexts.  The compiler treats
certain top-level forms, like @code{defmacro} (sort-of) and
@code{require}, as if they were wrapped in @code{(eval-when
(compile load eval) @dots{})}.
@end defspec

Emacs 19 includes two special forms related to @code{eval-when}.
One of these, @code{eval-when-compile}, is not quite equivalent to
any @code{eval-when} construct and is described below.  This package
defines a version of @code{eval-when-compile} for the benefit of
Emacs 18 users.

The other form, @code{(eval-and-compile @dots{})}, is exactly
equivalent to @samp{(eval-when (compile load eval) @dots{})} and
so is not itself defined by this package.

@defspec eval-when-compile forms...
The @var{forms} are evaluated at compile-time; at execution time,
this form acts like a quoted constant of the resulting value.  Used
at top-level, @code{eval-when-compile} is just like @samp{eval-when
(compile eval)}.  In other contexts, @code{eval-when-compile}
allows code to be evaluated once at compile-time for efficiency
or other reasons.

This form is similar to the @samp{#.} syntax of true Common Lisp.
@end defspec

@defspec load-time-value form
The @var{form} is evaluated at load-time; at execution time,
this form acts like a quoted constant of the resulting value.

Early Common Lisp had a @samp{#,} syntax that was similar to
this, but ANSI Common Lisp replaced it with @code{load-time-value}
and gave it more well-defined semantics.

In a compiled file, @code{load-time-value} arranges for @var{form}
to be evaluated when the @file{.elc} file is loaded and then used
as if it were a quoted constant.  In code compiled by
@code{byte-compile} rather than @code{byte-compile-file}, the
effect is identical to @code{eval-when-compile}.  In uncompiled
code, both @code{eval-when-compile} and @code{load-time-value}
act exactly like @code{progn}.

@example
(defun report ()
  (insert "This function was executed on: "
          (current-time-string)
          ", compiled on: "
          (eval-when-compile (current-time-string))
          ;; or '#.(current-time-string) in real Common Lisp
          ", and loaded on: "
          (load-time-value (current-time-string))))
@end example

@noindent
Byte-compiled, the above defun will result in the following code
(or its compiled equivalent, of course) in the @file{.elc} file:

@example
(setq --temp-- (current-time-string))
(defun report ()
  (insert "This function was executed on: "
          (current-time-string)
          ", compiled on: "
          '"Wed Jun 23 18:33:43 1993"
          ", and loaded on: "
          --temp--))
@end example
@end defspec

@node Function Aliases, , Time of Evaluation, Program Structure
@section Function Aliases

@noindent
This section describes a feature from GNU Emacs 19 which this
package makes available in other versions of Emacs.

@defun defalias symbol function
This function sets @var{symbol}'s function cell to @var{function}.
It is equivalent to @code{fset}, except that in GNU Emacs 19 it also
records the setting in @code{load-history} so that it can be undone
by a later @code{unload-feature}.

In other versions of Emacs, @code{defalias} is a synonym for
@code{fset}.
@end defun

@node Predicates, Control Structure, Program Structure, Top
@chapter Predicates

@noindent
This section describes functions for testing whether various
facts are true or false.

@menu
* Type Predicates::      `typep', `deftype', and `coerce'
* Equality Predicates::  `eql' and `equalp'
@end menu

@node Type Predicates, Equality Predicates, Predicates, Predicates
@section Type Predicates

@noindent
The @dfn{CL} package defines a version of the Common Lisp @code{typep}
predicate.

@defun typep object type
Check if @var{object} is of type @var{type}, where @var{type} is a
(quoted) type name of the sort used by Common Lisp.  For example,
@code{(typep foo 'integer)} is equivalent to @code{(integerp foo)}.
@end defun

The @var{type} argument to the above function is either a symbol
or a list beginning with a symbol.

@itemize @bullet
@item
If the type name is a symbol, Emacs appends @samp{-p} to the
symbol name to form the name of a predicate function for testing
the type.  (Built-in predicates whose names end in @samp{p} rather
than @samp{-p} are used when appropriate.)

@item
The type symbol @code{t} stands for the union of all types.
@code{(typep @var{object} t)} is always true.  Likewise, the
type symbol @code{nil} stands for nothing at all, and
@code{(typep @var{object} nil)} is always false.

@item
The type symbol @code{null} represents the symbol @code{nil}.
Thus @code{(typep @var{object} 'null)} is equivalent to
@code{(null @var{object})}.

@item
The type symbol @code{real} is a synonym for @code{number}, and
@code{fixnum} is a synonym for @code{integer}.

@item
The type symbols @code{character} and @code{string-char} match
characters.  In Emacs-19 and XEmacs-19, characters are the same thing as
integers in the range 0-255.  In XEmacs-20, where characters are a
first-class data type, this checks for actual characters, and
@code{(typep @var{8bit-integer} 'character)} will return @code{nil}.

@item
The type symbol @code{float} uses the @code{floatp-safe} predicate
defined by this package rather than @code{floatp}, so it will work
correctly even in Emacs versions without floating-point support.

@item
The type list @code{(integer @var{low} @var{high})} represents all
integers between @var{low} and @var{high}, inclusive.  Either bound
may be a list of a single integer to specify an exclusive limit,
or a @code{*} to specify no limit.  The type @code{(integer * *)}
is thus equivalent to @code{integer}.

@item
Likewise, lists beginning with @code{float}, @code{real}, or
@code{number} represent numbers of that type falling in a particular
range.

@item
Lists beginning with @code{and}, @code{or}, and @code{not} form
combinations of types.  For example, @code{(or integer (float 0 *))}
represents all objects that are integers or non-negative floats.

@item
Lists beginning with @code{member} or @code{member*} represent
objects @code{eql} to any of the following values.  For example,
@code{(member 1 2 3 4)} is equivalent to @code{(integer 1 4)},
and @code{(member nil)} is equivalent to @code{null}.

@item
Lists of the form @code{(satisfies @var{predicate})} represent
all objects for which @var{predicate} returns true when called
with that object as an argument.
@end itemize

The following function and macro (not technically predicates) are
related to @code{typep}.

@defun coerce object type
This function attempts to convert @var{object} to the specified
@var{type}.  If @var{object} is already of that type as determined by
@code{typep}, it is simply returned.  Otherwise, certain types of
conversions will be made:  If @var{type} is any sequence type
(@code{string}, @code{list}, etc.) then @var{object} will be
converted to that type if possible.  If @var{type} is
@code{character}, then strings of length one and symbols with
one-character names can be coerced.  If @var{type} is @code{float},
then integers can be coerced in versions of Emacs that support
floats.  In all other circumstances, @code{coerce} signals an
error.
@end defun

@defspec deftype name arglist forms...
This macro defines a new type called @var{name}.  It is similar
to @code{defmacro} in many ways; when @var{name} is encountered
as a type name, the body @var{forms} are evaluated and should
return a type specifier that is equivalent to the type.  The
@var{arglist} is a Common Lisp argument list of the sort accepted
by @code{defmacro*}.  The type specifier @samp{(@var{name} @var{args}...)}
is expanded by calling the expander with those arguments; the type
symbol @samp{@var{name}} is expanded by calling the expander with
no arguments.  The @var{arglist} is processed the same as for
@code{defmacro*} except that optional arguments without explicit
defaults use @code{*} instead of @code{nil} as the ``default''
default.  Some examples:

@example
(deftype null () '(satisfies null))    ; predefined
(deftype list () '(or null cons))      ; predefined
(deftype unsigned-byte (&optional bits)
  (list 'integer 0 (if (eq bits '*) bits (1- (lsh 1 bits)))))
(unsigned-byte 8)  @equiv{}  (integer 0 255)
(unsigned-byte)  @equiv{}  (integer 0 *)
unsigned-byte  @equiv{}  (integer 0 *)
@end example

@noindent
The last example shows how the Common Lisp @code{unsigned-byte}
type specifier could be implemented if desired; this package does
not implement @code{unsigned-byte} by default.
@end defspec

The @code{typecase} and @code{check-type} macros also use type
names.  @xref{Conditionals}.  @xref{Assertions}.  The @code{map},
@code{concatenate}, and @code{merge} functions take type-name
arguments to specify the type of sequence to return.  @xref{Sequences}.

@node Equality Predicates, , Type Predicates, Predicates
@section Equality Predicates

@noindent
This package defines two Common Lisp predicates, @code{eql} and
@code{equalp}.

@defun eql a b
This function is almost the same as @code{eq}, except that if @var{a}
and @var{b} are numbers of the same type, it compares them for numeric
equality (as if by @code{equal} instead of @code{eq}).  This makes a
difference only for versions of Emacs that are compiled with
floating-point support, such as Emacs 19.  Emacs floats are allocated
objects just like cons cells, which means that @code{(eq 3.0 3.0)}
will not necessarily be true---if the two @code{3.0}s were allocated
separately, the pointers will be different even though the numbers are
the same.  But @code{(eql 3.0 3.0)} will always be true.

The types of the arguments must match, so @code{(eql 3 3.0)} is
still false.

Note that Emacs integers are ``direct'' rather than allocated, which
basically means @code{(eq 3 3)} will always be true.  Thus @code{eq}
and @code{eql} behave differently only if floating-point numbers are
involved, and are indistinguishable on Emacs versions that don't
support floats.

There is a slight inconsistency with Common Lisp in the treatment of
positive and negative zeros.  Some machines, notably those with IEEE
standard arithmetic, represent @code{+0} and @code{-0} as distinct
values.  Normally this doesn't matter because the standard specifies
that @code{(= 0.0 -0.0)} should always be true, and this is indeed
what Emacs Lisp and Common Lisp do.  But the Common Lisp standard
states that @code{(eql 0.0 -0.0)} and @code{(equal 0.0 -0.0)} should
be false on IEEE-like machines; Emacs Lisp does not do this, and in
fact the only known way to distinguish between the two zeros in Emacs
Lisp is to @code{format} them and check for a minus sign.
@end defun

@defun equalp a b
This function is a more flexible version of @code{equal}.  In
particular, it compares strings and characters case-insensitively, and
it compares numbers without regard to type (so that @code{(equalp 3
3.0)} is true).  Vectors and conses are compared recursively.  All other
objects are compared as if by @code{equal}.

This function differs from Common Lisp @code{equalp} in several
respects.  In keeping with the idea that strings are less
vector-like in Emacs Lisp, this package's @code{equalp} also will not
compare strings against vectors of integers.
@end defun

Also note that the Common Lisp functions @code{member} and @code{assoc}
use @code{eql} to compare elements, whereas Emacs Lisp follows the
MacLisp tradition and uses @code{equal} for these two functions.
In Emacs, use @code{member*} and @code{assoc*} to get functions
which use @code{eql} for comparisons.

@node Control Structure, Macros, Predicates, Top
@chapter Control Structure

@noindent
The features described in the following sections implement
various advanced control structures, including the powerful
@code{setf} facility and a number of looping and conditional
constructs.

@menu
* Assignment::             The `psetq' form
* Generalized Variables::  `setf', `incf', `push', etc.
* Variable Bindings::      `progv', `lexical-let', `flet', `macrolet'
* Conditionals::           `when', `unless', `case', `typecase'
* Blocks and Exits::       `block', `return', `return-from'
* Iteration::              `do', `dotimes', `dolist', `do-symbols'
* Loop Facility::          The Common Lisp `loop' macro
* Multiple Values::        `values', `multiple-value-bind', etc.
@end menu

@node Assignment, Generalized Variables, Control Structure, Control Structure
@section Assignment

@noindent
The @code{psetq} form is just like @code{setq}, except that multiple
assignments are done in parallel rather than sequentially.

@defspec psetq [symbol form]@dots{}
This special form (actually a macro) is used to assign to several
variables simultaneously.  Given only one @var{symbol} and @var{form},
it has the same effect as @code{setq}.  Given several @var{symbol}
and @var{form} pairs, it evaluates all the @var{form}s in advance
and then stores the corresponding variables afterwards.

@example
(setq x 2 y 3)
(setq x (+ x y)  y (* x y))
x
     @result{} 5
y                     ; @r{@code{y} was computed after @code{x} was set.}
     @result{} 15
(setq x 2 y 3)
(psetq x (+ x y)  y (* x y))
x
     @result{} 5
y                     ; @r{@code{y} was computed before @code{x} was set.}
     @result{} 6
@end example

The simplest use of @code{psetq} is @code{(psetq x y y x)}, which
exchanges the values of two variables.  (The @code{rotatef} form
provides an even more convenient way to swap two variables;
@pxref{Modify Macros}.)

@code{psetq} always returns @code{nil}.
@end defspec

@node Generalized Variables, Variable Bindings, Assignment, Control Structure
@section Generalized Variables

@noindent
A ``generalized variable'' or ``place form'' is one of the many places
in Lisp memory where values can be stored.  The simplest place form is
a regular Lisp variable.  But the cars and cdrs of lists, elements
of arrays, properties of symbols, and many other locations are also
places where Lisp values are stored.

The @code{setf} form is like @code{setq}, except that it accepts
arbitrary place forms on the left side rather than just
symbols.  For example, @code{(setf (car a) b)} sets the car of
@code{a} to @code{b}, doing the same operation as @code{(setcar a b)}
but without having to remember two separate functions for setting
and accessing every type of place.

Generalized variables are analogous to ``lvalues'' in the C
language, where @samp{x = a[i]} gets an element from an array
and @samp{a[i] = x} stores an element using the same notation.
Just as certain forms like @code{a[i]} can be lvalues in C, there
is a set of forms that can be generalized variables in Lisp.

@menu
* Basic Setf::         `setf' and place forms
* Modify Macros::      `incf', `push', `rotatef', `letf', `callf', etc.
* Customizing Setf::   `define-modify-macro', `defsetf', `define-setf-method'
@end menu

@node Basic Setf, Modify Macros, Generalized Variables, Generalized Variables
@subsection Basic Setf

@noindent
The @code{setf} macro is the most basic way to operate on generalized
variables.

@defspec setf [place form]@dots{}
This macro evaluates @var{form} and stores it in @var{place}, which
must be a valid generalized variable form.  If there are several
@var{place} and @var{form} pairs, the assignments are done sequentially
just as with @code{setq}.  @code{setf} returns the value of the last
@var{form}.

The following Lisp forms will work as generalized variables, and
so may legally appear in the @var{place} argument of @code{setf}:

@itemize @bullet
@item
A symbol naming a variable.  In other words, @code{(setf x y)} is
exactly equivalent to @code{(setq x y)}, and @code{setq} itself is
strictly speaking redundant now that @code{setf} exists.  Many
programmers continue to prefer @code{setq} for setting simple
variables, though, purely for stylistic or historical reasons.
The form @code{(setf x y)} actually expands to @code{(setq x y)},
so there is no performance penalty for using it in compiled code.

@item
A call to any of the following Lisp functions:

@smallexample
car                 cdr                 caar .. cddddr
nth                 rest                first .. tenth
aref                elt                 nthcdr
symbol-function     symbol-value        symbol-plist
get                 getf                gethash
subseq
@end smallexample

@noindent
Note that for @code{nthcdr} and @code{getf}, the list argument
of the function must itself be a valid @var{place} form.  For
example, @code{(setf (nthcdr 0 foo) 7)} will set @code{foo} itself
to 7.  Note that @code{push} and @code{pop} on an @code{nthcdr}
place can be used to insert or delete at any position in a list.
The use of @code{nthcdr} as a @var{place} form is an extension
to standard Common Lisp.

@item
The following Emacs-specific functions are also @code{setf}-able.
(Some of these are defined only in Emacs 19 or only in XEmacs.)

@smallexample
buffer-file-name                  marker-position
buffer-modified-p                 match-data
buffer-name                       mouse-position
buffer-string                     overlay-end
buffer-substring                  overlay-get
current-buffer                    overlay-start
current-case-table                point
current-column                    point-marker
current-global-map                point-max
current-input-mode                point-min
current-local-map                 process-buffer
current-window-configuration      process-filter
default-file-modes                process-sentinel
default-value                     read-mouse-position
documentation-property            screen-height
extent-data                       screen-menubar
extent-end-position               screen-width
extent-start-position             selected-window
face-background                   selected-screen
face-background-pixmap            selected-frame
face-font                         standard-case-table
face-foreground                   syntax-table
face-underline-p                  window-buffer
file-modes                        window-dedicated-p
frame-height                      window-display-table
frame-parameters                  window-height
frame-visible-p                   window-hscroll
frame-width                       window-point
get-register                      window-start
getenv                            window-width
global-key-binding                x-get-cut-buffer
keymap-parent                     x-get-cutbuffer
local-key-binding                 x-get-secondary-selection
mark                              x-get-selection
mark-marker
@end smallexample

Most of these have directly corresponding ``set'' functions, like
@code{use-local-map} for @code{current-local-map}, or @code{goto-char}
for @code{point}.  A few, like @code{point-min}, expand to longer
sequences of code when they are @code{setf}'d (@code{(narrow-to-region
x (point-max))} in this case).

@item
A call of the form @code{(substring @var{subplace} @var{n} [@var{m}])},
where @var{subplace} is itself a legal generalized variable whose
current value is a string, and where the value stored is also a
string.  The new string is spliced into the specified part of the
destination string.  For example:

@example
(setq a (list "hello" "world"))
     @result{} ("hello" "world")
(cadr a)
     @result{} "world"
(substring (cadr a) 2 4)
     @result{} "rl"
(setf (substring (cadr a) 2 4) "o")
     @result{} "o"
(cadr a)
     @result{} "wood"
a
     @result{} ("hello" "wood")
@end example

The generalized variable @code{buffer-substring}, listed above,
also works in this way by replacing a portion of the current buffer.

@item
A call of the form @code{(apply '@var{func} @dots{})} or
@code{(apply (function @var{func}) @dots{})}, where @var{func}
is a @code{setf}-able function whose store function is ``suitable''
in the sense described in Steele's book; since none of the standard
Emacs place functions are suitable in this sense, this feature is
only interesting when used with places you define yourself with
@code{define-setf-method} or the long form of @code{defsetf}.

@item
A macro call, in which case the macro is expanded and @code{setf}
is applied to the resulting form.

@item
Any form for which a @code{defsetf} or @code{define-setf-method}
has been made.
@end itemize

Using any forms other than these in the @var{place} argument to
@code{setf} will signal an error.

The @code{setf} macro takes care to evaluate all subforms in
the proper left-to-right order; for example,

@example
(setf (aref vec (incf i)) i)
@end example

@noindent
looks like it will evaluate @code{(incf i)} exactly once, before the
following access to @code{i}; the @code{setf} expander will insert
temporary variables as necessary to ensure that it does in fact work
this way no matter what setf-method is defined for @code{aref}.
(In this case, @code{aset} would be used and no such steps would
be necessary since @code{aset} takes its arguments in a convenient
order.)

However, if the @var{place} form is a macro which explicitly
evaluates its arguments in an unusual order, this unusual order
will be preserved.  Adapting an example from Steele, given

@example
(defmacro wrong-order (x y) (list 'aref y x))
@end example

@noindent
the form @code{(setf (wrong-order @var{a} @var{b}) 17)} will
evaluate @var{b} first, then @var{a}, just as in an actual call
to @code{wrong-order}.
@end defspec

@node Modify Macros, Customizing Setf, Basic Setf, Generalized Variables
@subsection Modify Macros

@noindent
This package defines a number of other macros besides @code{setf}
that operate on generalized variables.  Many are interesting and
useful even when the @var{place} is just a variable name.

@defspec psetf [place form]@dots{}
This macro is to @code{setf} what @code{psetq} is to @code{setq}:
When several @var{place}s and @var{form}s are involved, the
assignments take place in parallel rather than sequentially.
Specifically, all subforms are evaluated from left to right, then
all the assignments are done (in an undefined order).
@end defspec

@defspec incf place &optional x
This macro increments the number stored in @var{place} by one, or
by @var{x} if specified.  The incremented value is returned.  For
example, @code{(incf i)} is equivalent to @code{(setq i (1+ i))}, and
@code{(incf (car x) 2)} is equivalent to @code{(setcar x (+ (car x) 2))}.

Once again, care is taken to preserve the ``apparent'' order of
evaluation.  For example,

@example
(incf (aref vec (incf i)))
@end example

@noindent
appears to increment @code{i} once, then increment the element of
@code{vec} addressed by @code{i}; this is indeed exactly what it
does, which means the above form is @emph{not} equivalent to the
``obvious'' expansion,

@example
(setf (aref vec (incf i)) (1+ (aref vec (incf i))))   ; Wrong!
@end example

@noindent
but rather to something more like

@example
(let ((temp (incf i)))
  (setf (aref vec temp) (1+ (aref vec temp))))
@end example

@noindent
Again, all of this is taken care of automatically by @code{incf} and
the other generalized-variable macros.

As a more Emacs-specific example of @code{incf}, the expression
@code{(incf (point) @var{n})} is essentially equivalent to
@code{(forward-char @var{n})}.
@end defspec

@defspec decf place &optional x
This macro decrements the number stored in @var{place} by one, or
by @var{x} if specified.
@end defspec

@defspec pop place
This macro removes and returns the first element of the list stored
in @var{place}.  It is analogous to @code{(prog1 (car @var{place})
(setf @var{place} (cdr @var{place})))}, except that it takes care
to evaluate all subforms only once.
@end defspec

@defspec push x place
This macro inserts @var{x} at the front of the list stored in
@var{place}.  It is analogous to @code{(setf @var{place} (cons
@var{x} @var{place}))}, except for evaluation of the subforms.
@end defspec

@defspec pushnew x place @t{&key :test :test-not :key}
This macro inserts @var{x} at the front of the list stored in
@var{place}, but only if @var{x} was not @code{eql} to any
existing element of the list.  The optional keyword arguments
are interpreted in the same way as for @code{adjoin}.
@xref{Lists as Sets}.
@end defspec

@defspec shiftf place@dots{} newvalue
This macro shifts the @var{place}s left by one, shifting in the
value of @var{newvalue} (which may be any Lisp expression, not just
a generalized variable), and returning the value shifted out of
the first @var{place}.  Thus, @code{(shiftf @var{a} @var{b} @var{c}
@var{d})} is equivalent to

@example
(prog1
    @var{a}
  (psetf @var{a} @var{b}
         @var{b} @var{c}
         @var{c} @var{d}))
@end example

@noindent
except that the subforms of @var{a}, @var{b}, and @var{c} are actually
evaluated only once each and in the apparent order.
@end defspec

@defspec rotatef place@dots{}
This macro rotates the @var{place}s left by one in circular fashion.
Thus, @code{(rotatef @var{a} @var{b} @var{c} @var{d})} is equivalent to

@example
(psetf @var{a} @var{b}
       @var{b} @var{c}
       @var{c} @var{d}
       @var{d} @var{a})
@end example

@noindent
except for the evaluation of subforms.  @code{rotatef} always
returns @code{nil}.  Note that @code{(rotatef @var{a} @var{b})}
conveniently exchanges @var{a} and @var{b}.
@end defspec

The following macros were invented for this package; they have no
analogues in Common Lisp.

@defspec letf (bindings@dots{}) forms@dots{}
This macro is analogous to @code{let}, but for generalized variables
rather than just symbols.  Each @var{binding} should be of the form
@code{(@var{place} @var{value})}; the original contents of the
@var{place}s are saved, the @var{value}s are stored in them, and
then the body @var{form}s are executed.  Afterwards, the @var{places}
are set back to their original saved contents.  This cleanup happens
even if the @var{form}s exit irregularly due to a @code{throw} or an
error.

For example,

@example
(letf (((point) (point-min))
       (a 17))
  ...)
@end example

@noindent
moves ``point'' in the current buffer to the beginning of the buffer,
and also binds @code{a} to 17 (as if by a normal @code{let}, since
@code{a} is just a regular variable).  After the body exits, @code{a}
is set back to its original value and point is moved back to its
original position.

Note that @code{letf} on @code{(point)} is not quite like a
@code{save-excursion}, as the latter effectively saves a marker
which tracks insertions and deletions in the buffer.  Actually,
a @code{letf} of @code{(point-marker)} is much closer to this
behavior.  (@code{point} and @code{point-marker} are equivalent
as @code{setf} places; each will accept either an integer or a
marker as the stored value.)

Since generalized variables look like lists, @code{let}'s shorthand
of using @samp{foo} for @samp{(foo nil)} as a @var{binding} would
be ambiguous in @code{letf} and is not allowed.

However, a @var{binding} specifier may be a one-element list
@samp{(@var{place})}, which is similar to @samp{(@var{place}
@var{place})}.  In other words, the @var{place} is not disturbed
on entry to the body, and the only effect of the @code{letf} is
to restore the original value of @var{place} afterwards.  (The
redundant access-and-store suggested by the @code{(@var{place}
@var{place})} example does not actually occur.)

In most cases, the @var{place} must have a well-defined value on
entry to the @code{letf} form.  The only exceptions are plain
variables and calls to @code{symbol-value} and @code{symbol-function}.
If the symbol is not bound on entry, it is simply made unbound by
@code{makunbound} or @code{fmakunbound} on exit.
@end defspec

@defspec letf* (bindings@dots{}) forms@dots{}
This macro is to @code{letf} what @code{let*} is to @code{let}:
It does the bindings in sequential rather than parallel order.
@end defspec

@defspec callf @var{function} @var{place} @var{args}@dots{}
This is the ``generic'' modify macro.  It calls @var{function},
which should be an unquoted function name, macro name, or lambda.
It passes @var{place} and @var{args} as arguments, and assigns the
result back to @var{place}.  For example, @code{(incf @var{place}
@var{n})} is the same as @code{(callf + @var{place} @var{n})}.
Some more examples:

@example
(callf abs my-number)
(callf concat (buffer-name) "<" (int-to-string n) ">")
(callf union happy-people (list joe bob) :test 'same-person)
@end example

@xref{Customizing Setf}, for @code{define-modify-macro}, a way
to create even more concise notations for modify macros.  Note
again that @code{callf} is an extension to standard Common Lisp.
@end defspec

@defspec callf2 @var{function} @var{arg1} @var{place} @var{args}@dots{}
This macro is like @code{callf}, except that @var{place} is
the @emph{second} argument of @var{function} rather than the
first.  For example, @code{(push @var{x} @var{place})} is
equivalent to @code{(callf2 cons @var{x} @var{place})}.
@end defspec

The @code{callf} and @code{callf2} macros serve as building
blocks for other macros like @code{incf}, @code{pushnew}, and
@code{define-modify-macro}.  The @code{letf} and @code{letf*}
macros are used in the processing of symbol macros;
@pxref{Macro Bindings}.

@node Customizing Setf, , Modify Macros, Generalized Variables
@subsection Customizing Setf

@noindent
Common Lisp defines three macros, @code{define-modify-macro},
@code{defsetf}, and @code{define-setf-method}, that allow the
user to extend generalized variables in various ways.

@defspec define-modify-macro name arglist function [doc-string]
This macro defines a ``read-modify-write'' macro similar to
@code{incf} and @code{decf}.  The macro @var{name} is defined
to take a @var{place} argument followed by additional arguments
described by @var{arglist}.  The call

@example
(@var{name} @var{place} @var{args}...)
@end example

@noindent
will be expanded to

@example
(callf @var{func} @var{place} @var{args}...)
@end example

@noindent
which in turn is roughly equivalent to

@example
(setf @var{place} (@var{func} @var{place} @var{args}...))
@end example

For example:

@example
(define-modify-macro incf (&optional (n 1)) +)
(define-modify-macro concatf (&rest args) concat)
@end example

Note that @code{&key} is not allowed in @var{arglist}, but
@code{&rest} is sufficient to pass keywords on to the function.

Most of the modify macros defined by Common Lisp do not exactly
follow the pattern of @code{define-modify-macro}.  For example,
@code{push} takes its arguments in the wrong order, and @code{pop}
is completely irregular.  You can define these macros ``by hand''
using @code{get-setf-method}, or consult the source file
@file{cl-macs.el} to see how to use the internal @code{setf}
building blocks.
@end defspec

@defspec defsetf access-fn update-fn
This is the simpler of two @code{defsetf} forms.  Where
@var{access-fn} is the name of a function which accesses a place,
this declares @var{update-fn} to be the corresponding store
function.  From now on,

@example
(setf (@var{access-fn} @var{arg1} @var{arg2} @var{arg3}) @var{value})
@end example

@noindent
will be expanded to

@example
(@var{update-fn} @var{arg1} @var{arg2} @var{arg3} @var{value})
@end example

@noindent
The @var{update-fn} is required to be either a true function, or
a macro which evaluates its arguments in a function-like way.  Also,
the @var{update-fn} is expected to return @var{value} as its result.
Otherwise, the above expansion would not obey the rules for the way
@code{setf} is supposed to behave.

As a special (non-Common-Lisp) extension, a third argument of @code{t}
to @code{defsetf} says that the @code{update-fn}'s return value is
not suitable, so that the above @code{setf} should be expanded to
something more like

@example
(let ((temp @var{value}))
  (@var{update-fn} @var{arg1} @var{arg2} @var{arg3} temp)
  temp)
@end example

Some examples of the use of @code{defsetf}, drawn from the standard
suite of setf methods, are:

@example
(defsetf car setcar)
(defsetf symbol-value set)
(defsetf buffer-name rename-buffer t)
@end example
@end defspec

@defspec defsetf access-fn arglist (store-var) forms@dots{}
This is the second, more complex, form of @code{defsetf}.  It is
rather like @code{defmacro} except for the additional @var{store-var}
argument.  The @var{forms} should return a Lisp form which stores
the value of @var{store-var} into the generalized variable formed
by a call to @var{access-fn} with arguments described by @var{arglist}.
The @var{forms} may begin with a string which documents the @code{setf}
method (analogous to the doc string that appears at the front of a
function).

For example, the simple form of @code{defsetf} is shorthand for

@example
(defsetf @var{access-fn} (&rest args) (store)
  (append '(@var{update-fn}) args (list store)))
@end example

The Lisp form that is returned can access the arguments from
@var{arglist} and @var{store-var} in an unrestricted fashion;
macros like @code{setf} and @code{incf} which invoke this
setf-method will insert temporary variables as needed to make
sure the apparent order of evaluation is preserved.

Another example drawn from the standard package:

@example
(defsetf nth (n x) (store)
  (list 'setcar (list 'nthcdr n x) store))
@end example
@end defspec

@defspec define-setf-method access-fn arglist forms@dots{}
This is the most general way to create new place forms.  When
a @code{setf} to @var{access-fn} with arguments described by
@var{arglist} is expanded, the @var{forms} are evaluated and
must return a list of five items:

@enumerate
@item
A list of @dfn{temporary variables}.

@item
A list of @dfn{value forms} corresponding to the temporary variables
above.  The temporary variables will be bound to these value forms
as the first step of any operation on the generalized variable.

@item
A list of exactly one @dfn{store variable} (generally obtained
from a call to @code{gensym}).

@item
A Lisp form which stores the contents of the store variable into
the generalized variable, assuming the temporaries have been
bound as described above.

@item
A Lisp form which accesses the contents of the generalized variable,
assuming the temporaries have been bound.
@end enumerate

This is exactly like the Common Lisp macro of the same name,
except that the method returns a list of five values rather
than the five values themselves, since Emacs Lisp does not
support Common Lisp's notion of multiple return values.

Once again, the @var{forms} may begin with a documentation string.

A setf-method should be maximally conservative with regard to
temporary variables.  In the setf-methods generated by
@code{defsetf}, the second return value is simply the list of
arguments in the place form, and the first return value is a
list of a corresponding number of temporary variables generated
by @code{gensym}.  Macros like @code{setf} and @code{incf} which
use this setf-method will optimize away most temporaries that
turn out to be unnecessary, so there is little reason for the
setf-method itself to optimize.
@end defspec

@defun get-setf-method place &optional env
This function returns the setf-method for @var{place}, by
invoking the definition previously recorded by @code{defsetf}
or @code{define-setf-method}.  The result is a list of five
values as described above.  You can use this function to build
your own @code{incf}-like modify macros.  (Actually, it is
better to use the internal functions @code{cl-setf-do-modify}
and @code{cl-setf-do-store}, which are a bit easier to use and
which also do a number of optimizations; consult the source
code for the @code{incf} function for a simple example.)

The argument @var{env} specifies the ``environment'' to be
passed on to @code{macroexpand} if @code{get-setf-method} should
need to expand a macro in @var{place}.  It should come from
an @code{&environment} argument to the macro or setf-method
that called @code{get-setf-method}.

See also the source code for the setf-methods for @code{apply}
and @code{substring}, each of which works by calling
@code{get-setf-method} on a simpler case, then massaging
the result in various ways.
@end defun

Modern Common Lisp defines a second, independent way to specify
the @code{setf} behavior of a function, namely ``@code{setf}
functions'' whose names are lists @code{(setf @var{name})}
rather than symbols.  For example, @code{(defun (setf foo) @dots{})}
defines the function that is used when @code{setf} is applied to
@code{foo}.  This package does not currently support @code{setf}
functions.  In particular, it is a compile-time error to use
@code{setf} on a form which has not already been @code{defsetf}'d
or otherwise declared; in newer Common Lisps, this would not be
an error since the function @code{(setf @var{func})} might be
defined later.

@iftex
@secno=4
@end iftex

@node Variable Bindings, Conditionals, Generalized Variables, Control Structure
@section Variable Bindings

@noindent
These Lisp forms make bindings to variables and function names,
analogous to Lisp's built-in @code{let} form.

@xref{Modify Macros}, for the @code{letf} and @code{letf*} forms which
are also related to variable bindings.

@menu
* Dynamic Bindings::     The `progv' form
* Lexical Bindings::     `lexical-let' and lexical closures
* Function Bindings::    `flet' and `labels'
* Macro Bindings::       `macrolet' and `symbol-macrolet'
@end menu

@node Dynamic Bindings, Lexical Bindings, Variable Bindings, Variable Bindings
@subsection Dynamic Bindings

@noindent
The standard @code{let} form binds variables whose names are known
at compile-time.  The @code{progv} form provides an easy way to
bind variables whose names are computed at run-time.

@defspec progv symbols values forms@dots{}
This form establishes @code{let}-style variable bindings on a
set of variables computed at run-time.  The expressions
@var{symbols} and @var{values} are evaluated, and must return lists
of symbols and values, respectively.  The symbols are bound to the
corresponding values for the duration of the body @var{form}s.
If @var{values} is shorter than @var{symbols}, the last few symbols
are made unbound (as if by @code{makunbound}) inside the body.
If @var{symbols} is shorter than @var{values}, the excess values
are ignored.
@end defspec

@node Lexical Bindings, Function Bindings, Dynamic Bindings, Variable Bindings
@subsection Lexical Bindings

@noindent
The @dfn{CL} package defines the following macro which
more closely follows the Common Lisp @code{let} form:

@defspec lexical-let (bindings@dots{}) forms@dots{}
This form is exactly like @code{let} except that the bindings it
establishes are purely lexical.  Lexical bindings are similar to
local variables in a language like C:  Only the code physically
within the body of the @code{lexical-let} (after macro expansion)
may refer to the bound variables.

@example
(setq a 5)
(defun foo (b) (+ a b))
(let ((a 2)) (foo a))
     @result{} 4
(lexical-let ((a 2)) (foo a))
     @result{} 7
@end example

@noindent
In this example, a regular @code{let} binding of @code{a} actually
makes a temporary change to the global variable @code{a}, so @code{foo}
is able to see the binding of @code{a} to 2.  But @code{lexical-let}
actually creates a distinct local variable @code{a} for use within its
body, without any effect on the global variable of the same name.

The most important use of lexical bindings is to create @dfn{closures}.
A closure is a function object that refers to an outside lexical
variable.  For example:

@example
(defun make-adder (n)
  (lexical-let ((n n))
    (function (lambda (m) (+ n m)))))
(setq add17 (make-adder 17))
(funcall add17 4)
     @result{} 21
@end example

@noindent
The call @code{(make-adder 17)} returns a function object which adds
17 to its argument.  If @code{let} had been used instead of
@code{lexical-let}, the function object would have referred to the
global @code{n}, which would have been bound to 17 only during the
call to @code{make-adder} itself.

@example
(defun make-counter ()
  (lexical-let ((n 0))
    (function* (lambda (&optional (m 1)) (incf n m)))))
(setq count-1 (make-counter))
(funcall count-1 3)
     @result{} 3
(funcall count-1 14)
     @result{} 17
(setq count-2 (make-counter))
(funcall count-2 5)
     @result{} 5
(funcall count-1 2)
     @result{} 19
(funcall count-2)
     @result{} 6
@end example

@noindent
Here we see that each call to @code{make-counter} creates a distinct
local variable @code{n}, which serves as a private counter for the
function object that is returned.

Closed-over lexical variables persist until the last reference to
them goes away, just like all other Lisp objects.  For example,
@code{count-2} refers to a function object which refers to an
instance of the variable @code{n}; this is the only reference
to that variable, so after @code{(setq count-2 nil)} the garbage
collector would be able to delete this instance of @code{n}.
Of course, if a @code{lexical-let} does not actually create any
closures, then the lexical variables are free as soon as the
@code{lexical-let} returns.

Many closures are used only during the extent of the bindings they
refer to; these are known as ``downward funargs'' in Lisp parlance.
When a closure is used in this way, regular Emacs Lisp dynamic
bindings suffice and will be more efficient than @code{lexical-let}
closures:

@example
(defun add-to-list (x list)
  (mapcar (function (lambda (y) (+ x y))) list))
(add-to-list 7 '(1 2 5))
     @result{} (8 9 12)
@end example

@noindent
Since this lambda is only used while @code{x} is still bound,
it is not necessary to make a true closure out of it.

You can use @code{defun} or @code{flet} inside a @code{lexical-let}
to create a named closure.  If several closures are created in the
body of a single @code{lexical-let}, they all close over the same
instance of the lexical variable.

The @code{lexical-let} form is an extension to Common Lisp.  In
true Common Lisp, all bindings are lexical unless declared otherwise.
@end defspec

@defspec lexical-let* (bindings@dots{}) forms@dots{}
This form is just like @code{lexical-let}, except that the bindings
are made sequentially in the manner of @code{let*}.
@end defspec

@node Function Bindings, Macro Bindings, Lexical Bindings, Variable Bindings
@subsection Function Bindings

@noindent
These forms make @code{let}-like bindings to functions instead
of variables.

@defspec flet (bindings@dots{}) forms@dots{}
This form establishes @code{let}-style bindings on the function
cells of symbols rather than on the value cells.  Each @var{binding}
must be a list of the form @samp{(@var{name} @var{arglist}
@var{forms}@dots{})}, which defines a function exactly as if
it were a @code{defun*} form.  The function @var{name} is defined
accordingly for the duration of the body of the @code{flet}; then
the old function definition, or lack thereof, is restored.

While @code{flet} in Common Lisp establishes a lexical binding of
@var{name}, Emacs Lisp @code{flet} makes a dynamic binding.  The
result is that @code{flet} affects indirect calls to a function as
well as calls directly inside the @code{flet} form itself.

You can use @code{flet} to disable or modify the behavior of a
function in a temporary fashion.  This will even work on Emacs
primitives, although note that some calls to primitive functions
internal to Emacs are made without going through the symbol's
function cell, and so will not be affected by @code{flet}.  For
example,

@example
(flet ((message (&rest args) (push args saved-msgs)))
  (do-something))
@end example

This code attempts to replace the built-in function @code{message}
with a function that simply saves the messages in a list rather
than displaying them.  The original definition of @code{message}
will be restored after @code{do-something} exits.  This code will
work fine on messages generated by other Lisp code, but messages
generated directly inside Emacs will not be caught since they make
direct C-language calls to the message routines rather than going
through the Lisp @code{message} function.

Functions defined by @code{flet} may use the full Common Lisp
argument notation supported by @code{defun*}; also, the function
body is enclosed in an implicit block as if by @code{defun*}.
@xref{Program Structure}.
@end defspec

@defspec labels (bindings@dots{}) forms@dots{}
The @code{labels} form is a synonym for @code{flet}.  (In Common
Lisp, @code{labels} and @code{flet} differ in ways that depend on
their lexical scoping; these distinctions vanish in dynamically
scoped Emacs Lisp.)
@end defspec

@node Macro Bindings, , Function Bindings, Variable Bindings
@subsection Macro Bindings

@noindent
These forms create local macros and ``symbol macros.''

@defspec macrolet (bindings@dots{}) forms@dots{}
This form is analogous to @code{flet}, but for macros instead of
functions.  Each @var{binding} is a list of the same form as the
arguments to @code{defmacro*} (i.e., a macro name, argument list,
and macro-expander forms).  The macro is defined accordingly for
use within the body of the @code{macrolet}.

Because of the nature of macros, @code{macrolet} is lexically
scoped even in Emacs Lisp:  The @code{macrolet} binding will
affect only calls that appear physically within the body
@var{forms}, possibly after expansion of other macros in the
body.
@end defspec

@defspec symbol-macrolet (bindings@dots{}) forms@dots{}
This form creates @dfn{symbol macros}, which are macros that look
like variable references rather than function calls.  Each
@var{binding} is a list @samp{(@var{var} @var{expansion})};
any reference to @var{var} within the body @var{forms} is
replaced by @var{expansion}.

@example
(setq bar '(5 . 9))
(symbol-macrolet ((foo (car bar)))
  (incf foo))
bar
     @result{} (6 . 9)
@end example

A @code{setq} of a symbol macro is treated the same as a @code{setf}.
I.e., @code{(setq foo 4)} in the above would be equivalent to
@code{(setf foo 4)}, which in turn expands to @code{(setf (car bar) 4)}.

Likewise, a @code{let} or @code{let*} binding a symbol macro is
treated like a @code{letf} or @code{letf*}.  This differs from true
Common Lisp, where the rules of lexical scoping cause a @code{let}
binding to shadow a @code{symbol-macrolet} binding.  In this package,
only @code{lexical-let} and @code{lexical-let*} will shadow a symbol
macro.

There is no analogue of @code{defmacro} for symbol macros; all symbol
macros are local.  A typical use of @code{symbol-macrolet} is in the
expansion of another macro:

@example
(defmacro* my-dolist ((x list) &rest body)
  (let ((var (gensym)))
    (list 'loop 'for var 'on list 'do
          (list* 'symbol-macrolet (list (list x (list 'car var)))
                 body))))

(setq mylist '(1 2 3 4))
(my-dolist (x mylist) (incf x))
mylist
     @result{} (2 3 4 5)
@end example

@noindent
In this example, the @code{my-dolist} macro is similar to @code{dolist}
(@pxref{Iteration}) except that the variable @code{x} becomes a true
reference onto the elements of the list.  The @code{my-dolist} call
shown here expands to

@example
(loop for G1234 on mylist do
      (symbol-macrolet ((x (car G1234)))
        (incf x)))
@end example

@noindent
which in turn expands to

@example
(loop for G1234 on mylist do (incf (car G1234)))
@end example

@xref{Loop Facility}, for a description of the @code{loop} macro.
This package defines a nonstandard @code{in-ref} loop clause that
works much like @code{my-dolist}.
@end defspec

@node Conditionals, Blocks and Exits, Variable Bindings, Control Structure
@section Conditionals

@noindent
These conditional forms augment Emacs Lisp's simple @code{if},
@code{and}, @code{or}, and @code{cond} forms.

@defspec when test forms@dots{}
This is a variant of @code{if} where there are no ``else'' forms,
and possibly several ``then'' forms.  In particular,

@example
(when @var{test} @var{a} @var{b} @var{c})
@end example

@noindent
is entirely equivalent to

@example
(if @var{test} (progn @var{a} @var{b} @var{c}) nil)
@end example
@end defspec

@defspec unless test forms@dots{}
This is a variant of @code{if} where there are no ``then'' forms,
and possibly several ``else'' forms:

@example
(unless @var{test} @var{a} @var{b} @var{c})
@end example

@noindent
is entirely equivalent to

@example
(when (not @var{test}) @var{a} @var{b} @var{c})
@end example
@end defspec

@defspec case keyform clause@dots{}
This macro evaluates @var{keyform}, then compares it with the key
values listed in the various @var{clause}s.  Whichever clause matches
the key is executed; comparison is done by @code{eql}.  If no clause
matches, the @code{case} form returns @code{nil}.  The clauses are
of the form

@example
(@var{keylist} @var{body-forms}@dots{})
@end example

@noindent
where @var{keylist} is a list of key values.  If there is exactly
one value, and it is not a cons cell or the symbol @code{nil} or
@code{t}, then it can be used by itself as a @var{keylist} without
being enclosed in a list.  All key values in the @code{case} form
must be distinct.  The final clauses may use @code{t} in place of
a @var{keylist} to indicate a default clause that should be taken
if none of the other clauses match.  (The symbol @code{otherwise}
is also recognized in place of @code{t}.  To make a clause that
matches the actual symbol @code{t}, @code{nil}, or @code{otherwise},
enclose the symbol in a list.)

For example, this expression reads a keystroke, then does one of
four things depending on whether it is an @samp{a}, a @samp{b},
a @key{RET} or @key{LFD}, or anything else.

@example
(case (read-char)
  (?a (do-a-thing))
  (?b (do-b-thing))
  ((?\r ?\n) (do-ret-thing))
  (t (do-other-thing)))
@end example
@end defspec

@defspec ecase keyform clause@dots{}
This macro is just like @code{case}, except that if the key does
not match any of the clauses, an error is signalled rather than
simply returning @code{nil}.
@end defspec

@defspec typecase keyform clause@dots{}
This macro is a version of @code{case} that checks for types
rather than values.  Each @var{clause} is of the form
@samp{(@var{type} @var{body}...)}.  @xref{Type Predicates},
for a description of type specifiers.  For example,

@example
(typecase x
  (integer (munch-integer x))
  (float (munch-float x))
  (string (munch-integer (string-to-int x)))
  (t (munch-anything x)))
@end example

The type specifier @code{t} matches any type of object; the word
@code{otherwise} is also allowed.  To make one clause match any of
several types, use an @code{(or ...)} type specifier.
@end defspec

@defspec etypecase keyform clause@dots{}
This macro is just like @code{typecase}, except that if the key does
not match any of the clauses, an error is signalled rather than
simply returning @code{nil}.
@end defspec

@node Blocks and Exits, Iteration, Conditionals, Control Structure
@section Blocks and Exits

@noindent
Common Lisp @dfn{blocks} provide a non-local exit mechanism very
similar to @code{catch} and @code{throw}, but lexically rather than
dynamically scoped.  This package actually implements @code{block}
in terms of @code{catch}; however, the lexical scoping allows the
optimizing byte-compiler to omit the costly @code{catch} step if the
body of the block does not actually @code{return-from} the block.

@defspec block name forms@dots{}
The @var{forms} are evaluated as if by a @code{progn}.  However,
if any of the @var{forms} execute @code{(return-from @var{name})},
they will jump out and return directly from the @code{block} form.
The @code{block} returns the result of the last @var{form} unless
a @code{return-from} occurs.

The @code{block}/@code{return-from} mechanism is quite similar to
the @code{catch}/@code{throw} mechanism.  The main differences are
that block @var{name}s are unevaluated symbols, rather than forms
(such as quoted symbols) which evaluate to a tag at run-time; and
also that blocks are lexically scoped whereas @code{catch}/@code{throw}
are dynamically scoped.  This means that functions called from the
body of a @code{catch} can also @code{throw} to the @code{catch},
but the @code{return-from} referring to a block name must appear
physically within the @var{forms} that make up the body of the block.
They may not appear within other called functions, although they may
appear within macro expansions or @code{lambda}s in the body.  Block
names and @code{catch} names form independent name-spaces.

In true Common Lisp, @code{defun} and @code{defmacro} surround
the function or expander bodies with implicit blocks with the
same name as the function or macro.  This does not occur in Emacs
Lisp, but this package provides @code{defun*} and @code{defmacro*}
forms which do create the implicit block.

The Common Lisp looping constructs defined by this package,
such as @code{loop} and @code{dolist}, also create implicit blocks
just as in Common Lisp.

Because they are implemented in terms of Emacs Lisp @code{catch}
and @code{throw}, blocks have the same overhead as actual
@code{catch} constructs (roughly two function calls).  However,
Zawinski and Furuseth's optimizing byte compiler (standard in
Emacs 19) will optimize away the @code{catch} if the block does
not in fact contain any @code{return} or @code{return-from} calls
that jump to it.  This means that @code{do} loops and @code{defun*}
functions which don't use @code{return} don't pay the overhead to
support it.
@end defspec

@defspec return-from name [result]
This macro returns from the block named @var{name}, which must be
an (unevaluated) symbol.  If a @var{result} form is specified, it
is evaluated to produce the result returned from the @code{block}.
Otherwise, @code{nil} is returned.
@end defspec

@defspec return [result]
This macro is exactly like @code{(return-from nil @var{result})}.
Common Lisp loops like @code{do} and @code{dolist} implicitly enclose
themselves in @code{nil} blocks.
@end defspec

@node Iteration, Loop Facility, Blocks and Exits, Control Structure
@section Iteration

@noindent
The macros described here provide more sophisticated, high-level
looping constructs to complement Emacs Lisp's basic @code{while}
loop.

@defspec loop forms@dots{}
The @dfn{CL} package supports both the simple, old-style meaning of
@code{loop} and the extremely powerful and flexible feature known as
the @dfn{Loop Facility} or @dfn{Loop Macro}.  This more advanced
facility is discussed in the following section; @pxref{Loop Facility}.
The simple form of @code{loop} is described here.

If @code{loop} is followed by zero or more Lisp expressions,
then @code{(loop @var{exprs}@dots{})} simply creates an infinite
loop executing the expressions over and over.  The loop is
enclosed in an implicit @code{nil} block.  Thus,

@example
(loop (foo)  (if (no-more) (return 72))  (bar))
@end example

@noindent
is exactly equivalent to

@example
(block nil (while t (foo)  (if (no-more) (return 72))  (bar)))
@end example

If any of the expressions are plain symbols, the loop is instead
interpreted as a Loop Macro specification as described later.
(This is not a restriction in practice, since a plain symbol
in the above notation would simply access and throw away the
value of a variable.)
@end defspec

@defspec do (spec@dots{}) (end-test [result@dots{}]) forms@dots{}
This macro creates a general iterative loop.  Each @var{spec} is
of the form

@example
(@var{var} [@var{init} [@var{step}]])
@end example

The loop works as follows:  First, each @var{var} is bound to the
associated @var{init} value as if by a @code{let} form.  Then, in
each iteration of the loop, the @var{end-test} is evaluated; if
true, the loop is finished.  Otherwise, the body @var{forms} are
evaluated, then each @var{var} is set to the associated @var{step}
expression (as if by a @code{psetq} form) and the next iteration
begins.  Once the @var{end-test} becomes true, the @var{result}
forms are evaluated (with the @var{var}s still bound to their
values) to produce the result returned by @code{do}.

The entire @code{do} loop is enclosed in an implicit @code{nil}
block, so that you can use @code{(return)} to break out of the
loop at any time.

If there are no @var{result} forms, the loop returns @code{nil}.
If a given @var{var} has no @var{step} form, it is bound to its
@var{init} value but not otherwise modified during the @code{do}
loop (unless the code explicitly modifies it); this case is just
a shorthand for putting a @code{(let ((@var{var} @var{init})) @dots{})}
around the loop.  If @var{init} is also omitted it defaults to
@code{nil}, and in this case a plain @samp{@var{var}} can be used
in place of @samp{(@var{var})}, again following the analogy with
@code{let}.

This example (from Steele) illustrates a loop which applies the
function @code{f} to successive pairs of values from the lists
@code{foo} and @code{bar}; it is equivalent to the call
@code{(mapcar* 'f foo bar)}.  Note that this loop has no body
@var{forms} at all, performing all its work as side effects of
the rest of the loop.

@example
(do ((x foo (cdr x))
     (y bar (cdr y))
     (z nil (cons (f (car x) (car y)) z)))
  ((or (null x) (null y))
   (nreverse z)))
@end example
@end defspec

@defspec do* (spec@dots{}) (end-test [result@dots{}]) forms@dots{}
This is to @code{do} what @code{let*} is to @code{let}.  In
particular, the initial values are bound as if by @code{let*}
rather than @code{let}, and the steps are assigned as if by
@code{setq} rather than @code{psetq}.

Here is another way to write the above loop:

@example
(do* ((xp foo (cdr xp))
      (yp bar (cdr yp))
      (x (car xp) (car xp))
      (y (car yp) (car yp))
      z)
  ((or (null xp) (null yp))
   (nreverse z))
  (push (f x y) z))
@end example
@end defspec

@defspec dolist (var list [result]) forms@dots{}
This is a more specialized loop which iterates across the elements
of a list.  @var{list} should evaluate to a list; the body @var{forms}
are executed with @var{var} bound to each element of the list in
turn.  Finally, the @var{result} form (or @code{nil}) is evaluated
with @var{var} bound to @code{nil} to produce the result returned by
the loop.  The loop is surrounded by an implicit @code{nil} block.
@end defspec

@defspec dotimes (var count [result]) forms@dots{}
This is a more specialized loop which iterates a specified number
of times.  The body is executed with @var{var} bound to the integers
from zero (inclusive) to @var{count} (exclusive), in turn.  Then
the @code{result} form is evaluated with @var{var} bound to the total
number of iterations that were done (i.e., @code{(max 0 @var{count})})
to get the return value for the loop form.  The loop is surrounded
by an implicit @code{nil} block.
@end defspec

@defspec do-symbols (var [obarray [result]]) forms@dots{}
This loop iterates over all interned symbols.  If @var{obarray}
is specified and is not @code{nil}, it loops over all symbols in
that obarray.  For each symbol, the body @var{forms} are evaluated
with @var{var} bound to that symbol.  The symbols are visited in
an unspecified order.  Afterward the @var{result} form, if any,
is evaluated (with @var{var} bound to @code{nil}) to get the return
value.  The loop is surrounded by an implicit @code{nil} block.
@end defspec

@defspec do-all-symbols (var [result]) forms@dots{}
This is identical to @code{do-symbols} except that the @var{obarray}
argument is omitted; it always iterates over the default obarray.
@end defspec

@xref{Mapping over Sequences}, for some more functions for
iterating over vectors or lists.

@node Loop Facility, Multiple Values, Iteration, Control Structure
@section Loop Facility

@noindent
A common complaint with Lisp's traditional looping constructs is
that they are either too simple and limited, such as Common Lisp's
@code{dotimes} or Emacs Lisp's @code{while}, or too unreadable and
obscure, like Common Lisp's @code{do} loop.

To remedy this, recent versions of Common Lisp have added a new
construct called the ``Loop Facility'' or ``@code{loop} macro,''
with an easy-to-use but very powerful and expressive syntax.

@menu
* Loop Basics::           `loop' macro, basic clause structure
* Loop Examples::         Working examples of `loop' macro
* For Clauses::           Clauses introduced by `for' or `as'
* Iteration Clauses::     `repeat', `while', `thereis', etc.
* Accumulation Clauses::  `collect', `sum', `maximize', etc.
* Other Clauses::         `with', `if', `initially', `finally'
@end menu

@node Loop Basics, Loop Examples, Loop Facility, Loop Facility
@subsection Loop Basics

@noindent
The @code{loop} macro essentially creates a mini-language within
Lisp that is specially tailored for describing loops.  While this
language is a little strange-looking by the standards of regular Lisp,
it turns out to be very easy to learn and well-suited to its purpose.

Since @code{loop} is a macro, all parsing of the loop language
takes place at byte-compile time; compiled @code{loop}s are just
as efficient as the equivalent @code{while} loops written longhand.

@defspec loop clauses@dots{}
A loop construct consists of a series of @var{clause}s, each
introduced by a symbol like @code{for} or @code{do}.  Clauses
are simply strung together in the argument list of @code{loop},
with minimal extra parentheses.  The various types of clauses
specify initializations, such as the binding of temporary
variables, actions to be taken in the loop, stepping actions,
and final cleanup.

Common Lisp specifies a certain general order of clauses in a
loop:

@example
(loop @var{name-clause}
      @var{var-clauses}@dots{}
      @var{action-clauses}@dots{})
@end example

The @var{name-clause} optionally gives a name to the implicit
block that surrounds the loop.  By default, the implicit block
is named @code{nil}.  The @var{var-clauses} specify what
variables should be bound during the loop, and how they should
be modified or iterated throughout the course of the loop.  The
@var{action-clauses} are things to be done during the loop, such
as computing, collecting, and returning values.

The Emacs version of the @code{loop} macro is less restrictive about
the order of clauses, but things will behave most predictably if
you put the variable-binding clauses @code{with}, @code{for}, and
@code{repeat} before the action clauses.  As in Common Lisp,
@code{initially} and @code{finally} clauses can go anywhere.

Loops generally return @code{nil} by default, but you can cause
them to return a value by using an accumulation clause like
@code{collect}, an end-test clause like @code{always}, or an
explicit @code{return} clause to jump out of the implicit block.
(Because the loop body is enclosed in an implicit block, you can
also use regular Lisp @code{return} or @code{return-from} to
break out of the loop.)
@end defspec

The following sections give some examples of the Loop Macro in
action, and describe the particular loop clauses in great detail.
Consult the second edition of Steele's @dfn{Common Lisp, the Language},
for additional discussion and examples of the @code{loop} macro.

@node Loop Examples, For Clauses, Loop Basics, Loop Facility
@subsection Loop Examples

@noindent
Before listing the full set of clauses that are allowed, let's
look at a few example loops just to get a feel for the @code{loop}
language.

@example
(loop for buf in (buffer-list)
      collect (buffer-file-name buf))
@end example

@noindent
This loop iterates over all Emacs buffers, using the list
returned by @code{buffer-list}.  For each buffer @code{buf},
it calls @code{buffer-file-name} and collects the results into
a list, which is then returned from the @code{loop} construct.
The result is a list of the file names of all the buffers in
Emacs' memory.  The words @code{for}, @code{in}, and @code{collect}
are reserved words in the @code{loop} language.

@example
(loop repeat 20 do (insert "Yowsa\n"))
@end example

@noindent
This loop inserts the phrase ``Yowsa'' twenty times in the
current buffer.

@example
(loop until (eobp) do (munch-line) (forward-line 1))
@end example

@noindent
This loop calls @code{munch-line} on every line until the end
of the buffer.  If point is already at the end of the buffer,
the loop exits immediately.

@example
(loop do (munch-line) until (eobp) do (forward-line 1))
@end example

@noindent
This loop is similar to the above one, except that @code{munch-line}
is always called at least once.

@example
(loop for x from 1 to 100
      for y = (* x x)
      until (>= y 729)
      finally return (list x (= y 729)))
@end example

@noindent
This more complicated loop searches for a number @code{x} whose
square is 729.  For safety's sake it only examines @code{x}
values up to 100; dropping the phrase @samp{to 100} would
cause the loop to count upwards with no limit.  The second
@code{for} clause defines @code{y} to be the square of @code{x}
within the loop; the expression after the @code{=} sign is
reevaluated each time through the loop.  The @code{until}
clause gives a condition for terminating the loop, and the
@code{finally} clause says what to do when the loop finishes.
(This particular example was written less concisely than it
could have been, just for the sake of illustration.)

Note that even though this loop contains three clauses (two
@code{for}s and an @code{until}) that would have been enough to
define loops all by themselves, it still creates a single loop
rather than some sort of triple-nested loop.  You must explicitly
nest your @code{loop} constructs if you want nested loops.

@node For Clauses, Iteration Clauses, Loop Examples, Loop Facility
@subsection For Clauses

@noindent
Most loops are governed by one or more @code{for} clauses.
A @code{for} clause simultaneously describes variables to be
bound, how those variables are to be stepped during the loop,
and usually an end condition based on those variables.

The word @code{as} is a synonym for the word @code{for}.  This
word is followed by a variable name, then a word like @code{from}
or @code{across} that describes the kind of iteration desired.
In Common Lisp, the phrase @code{being the} sometimes precedes
the type of iteration; in this package both @code{being} and
@code{the} are optional.  The word @code{each} is a synonym
for @code{the}, and the word that follows it may be singular
or plural:  @samp{for x being the elements of y} or
@samp{for x being each element of y}.  Which form you use
is purely a matter of style.

The variable is bound around the loop as if by @code{let}:

@example
(setq i 'happy)
(loop for i from 1 to 10 do (do-something-with i))
i
     @result{} happy
@end example

@table @code
@item for @var{var} from @var{expr1} to @var{expr2} by @var{expr3}
This type of @code{for} clause creates a counting loop.  Each of
the three sub-terms is optional, though there must be at least one
term so that the clause is marked as a counting clause.

The three expressions are the starting value, the ending value, and
the step value, respectively, of the variable.  The loop counts
upwards by default (@var{expr3} must be positive), from @var{expr1}
to @var{expr2} inclusively.  If you omit the @code{from} term, the
loop counts from zero; if you omit the @code{to} term, the loop
counts forever without stopping (unless stopped by some other
loop clause, of course); if you omit the @code{by} term, the loop
counts in steps of one.

You can replace the word @code{from} with @code{upfrom} or
@code{downfrom} to indicate the direction of the loop.  Likewise,
you can replace @code{to} with @code{upto} or @code{downto}.
For example, @samp{for x from 5 downto 1} executes five times
with @code{x} taking on the integers from 5 down to 1 in turn.
Also, you can replace @code{to} with @code{below} or @code{above},
which are like @code{upto} and @code{downto} respectively except
that they are exclusive rather than inclusive limits:

@example
(loop for x to 10 collect x)
     @result{} (0 1 2 3 4 5 6 7 8 9 10)
(loop for x below 10 collect x)
     @result{} (0 1 2 3 4 5 6 7 8 9)
@end example

The @code{by} value is always positive, even for downward-counting
loops.  Some sort of @code{from} value is required for downward
loops; @samp{for x downto 5} is not a legal loop clause all by
itself.

@item for @var{var} in @var{list} by @var{function}
This clause iterates @var{var} over all the elements of @var{list},
in turn.  If you specify the @code{by} term, then @var{function}
is used to traverse the list instead of @code{cdr}; it must be a
function taking one argument.  For example:

@example
(loop for x in '(1 2 3 4 5 6) collect (* x x))
     @result{} (1 4 9 16 25 36)
(loop for x in '(1 2 3 4 5 6) by 'cddr collect (* x x))
     @result{} (1 9 25)
@end example

@item for @var{var} on @var{list} by @var{function}
This clause iterates @var{var} over all the cons cells of @var{list}.

@example
(loop for x on '(1 2 3 4) collect x)
     @result{} ((1 2 3 4) (2 3 4) (3 4) (4))
@end example

With @code{by}, there is no real reason that the @code{on} expression
must be a list.  For example:

@example
(loop for x on first-animal by 'next-animal collect x)
@end example

@noindent
where @code{(next-animal x)} takes an ``animal'' @var{x} and returns
the next in the (assumed) sequence of animals, or @code{nil} if
@var{x} was the last animal in the sequence.

@item for @var{var} in-ref @var{list} by @var{function}
This is like a regular @code{in} clause, but @var{var} becomes
a @code{setf}-able ``reference'' onto the elements of the list
rather than just a temporary variable.  For example,

@example
(loop for x in-ref my-list do (incf x))
@end example

@noindent
increments every element of @code{my-list} in place.  This clause
is an extension to standard Common Lisp.

@item for @var{var} across @var{array}
This clause iterates @var{var} over all the elements of @var{array},
which may be a vector or a string.

@example
(loop for x across "aeiou"
      do (use-vowel (char-to-string x)))
@end example

@item for @var{var} across-ref @var{array}
This clause iterates over an array, with @var{var} a @code{setf}-able
reference onto the elements; see @code{in-ref} above.

@item for @var{var} being the elements of @var{sequence}
This clause iterates over the elements of @var{sequence}, which may
be a list, vector, or string.  Since the type must be determined
at run-time, this is somewhat less efficient than @code{in} or
@code{across}.  The clause may be followed by the additional term
@samp{using (index @var{var2})} to cause @var{var2} to be bound to
the successive indices (starting at 0) of the elements.

This clause type is taken from older versions of the @code{loop} macro,
and is not present in modern Common Lisp.  The @samp{using (sequence ...)}
term of the older macros is not supported.

@item for @var{var} being the elements of-ref @var{sequence}
This clause iterates over a sequence, with @var{var} a @code{setf}-able
reference onto the elements; see @code{in-ref} above.

@item for @var{var} being the symbols [of @var{obarray}]
This clause iterates over symbols, either over all interned symbols
or over all symbols in @var{obarray}.  The loop is executed with
@var{var} bound to each symbol in turn.  The symbols are visited in
an unspecified order.

As an example,

@example
(loop for sym being the symbols
      when (fboundp sym)
      when (string-match "^map" (symbol-name sym))
      collect sym)
@end example

@noindent
returns a list of all the functions whose names begin with @samp{map}.

The Common Lisp words @code{external-symbols} and @code{present-symbols}
are also recognized but are equivalent to @code{symbols} in Emacs Lisp.

Due to a minor implementation restriction, it will not work to have
more than one @code{for} clause iterating over symbols, hash tables,
keymaps, overlays, or intervals in a given @code{loop}.  Fortunately,
it would rarely if ever be useful to do so.  It @emph{is} legal to mix
one of these types of clauses with other clauses like @code{for ... to}
or @code{while}.

@item for @var{var} being the hash-keys of @var{hash-table}
This clause iterates over the entries in @var{hash-table}.  For each
hash table entry, @var{var} is bound to the entry's key.  If you write
@samp{the hash-values} instead, @var{var} is bound to the values
of the entries.  The clause may be followed by the additional
term @samp{using (hash-values @var{var2})} (where @code{hash-values}
is the opposite word of the word following @code{the}) to cause
@var{var} and @var{var2} to be bound to the two parts of each
hash table entry.

@item for @var{var} being the key-codes of @var{keymap}
This clause iterates over the entries in @var{keymap}.  In GNU Emacs 18
and 19, keymaps are either alists or vectors, and key-codes are integers
or symbols.  In XEmacs, keymaps are a special new data type, and
key-codes are symbols or lists of symbols.  The iteration does not enter
nested keymaps or inherited (parent) keymaps.  You can use @samp{the
key-bindings} to access the commands bound to the keys rather than the
key codes, and you can add a @code{using} clause to access both the
codes and the bindings together.

@item for @var{var} being the key-seqs of @var{keymap}
This clause iterates over all key sequences defined by @var{keymap}
and its nested keymaps, where @var{var} takes on values which are
strings in Emacs 18 or vectors in Emacs 19.  The strings or vectors
are reused for each iteration, so you must copy them if you wish to keep
them permanently.  You can add a @samp{using (key-bindings ...)}
clause to get the command bindings as well.

@item for @var{var} being the overlays [of @var{buffer}] @dots{}
This clause iterates over the Emacs 19 ``overlays'' or XEmacs
``extents'' of a buffer (the clause @code{extents} is synonymous with
@code{overlays}).  Under Emacs 18, this clause iterates zero times.  If
the @code{of} term is omitted, the current buffer is used.  This clause
also accepts optional @samp{from @var{pos}} and @samp{to @var{pos}}
terms, limiting the clause to overlays which overlap the specified
region.

@item for @var{var} being the intervals [of @var{buffer}] @dots{}
This clause iterates over all intervals of a buffer with constant
text properties.  The variable @var{var} will be bound to conses
of start and end positions, where one start position is always equal
to the previous end position.  The clause allows @code{of},
@code{from}, @code{to}, and @code{property} terms, where the latter
term restricts the search to just the specified property.  The
@code{of} term may specify either a buffer or a string.  This
clause is useful only in GNU Emacs 19; in other versions, all
buffers and strings consist of a single interval.

@item for @var{var} being the frames
This clause iterates over all frames, i.e., X window system windows
open on Emacs files.  This clause works only under Emacs 19.  The
clause @code{screens} is a synonym for @code{frames}.  The frames
are visited in @code{next-frame} order starting from
@code{selected-frame}.

@item for @var{var} being the windows [of @var{frame}]
This clause iterates over the windows (in the Emacs sense) of
the current frame, or of the specified @var{frame}.  (In Emacs 18
there is only ever one frame, and the @code{of} term is not
allowed there.)

@item for @var{var} being the buffers
This clause iterates over all buffers in Emacs.  It is equivalent
to @samp{for @var{var} in (buffer-list)}.

@item for @var{var} = @var{expr1} then @var{expr2}
This clause does a general iteration.  The first time through
the loop, @var{var} will be bound to @var{expr1}.  On the second
and successive iterations it will be set by evaluating @var{expr2}
(which may refer to the old value of @var{var}).  For example,
these two loops are effectively the same:

@example
(loop for x on my-list by 'cddr do ...)
(loop for x = my-list then (cddr x) while x do ...)
@end example

Note that this type of @code{for} clause does not imply any sort
of terminating condition; the above example combines it with a
@code{while} clause to tell when to end the loop.

If you omit the @code{then} term, @var{expr1} is used both for
the initial setting and for successive settings:

@example
(loop for x = (random) when (> x 0) return x)
@end example

@noindent
This loop keeps taking random numbers from the @code{(random)}
function until it gets a positive one, which it then returns.
@end table

If you include several @code{for} clauses in a row, they are
treated sequentially (as if by @code{let*} and @code{setq}).
You can instead use the word @code{and} to link the clauses,
in which case they are processed in parallel (as if by @code{let}
and @code{psetq}).

@example
(loop for x below 5 for y = nil then x collect (list x y))
     @result{} ((0 nil) (1 1) (2 2) (3 3) (4 4))
(loop for x below 5 and y = nil then x collect (list x y))
     @result{} ((0 nil) (1 0) (2 1) (3 2) (4 3))
@end example

@noindent
In the first loop, @code{y} is set based on the value of @code{x}
that was just set by the previous clause; in the second loop,
@code{x} and @code{y} are set simultaneously so @code{y} is set
based on the value of @code{x} left over from the previous time
through the loop.

Another feature of the @code{loop} macro is @dfn{destructuring},
similar in concept to the destructuring provided by @code{defmacro}.
The @var{var} part of any @code{for} clause can be given as a list
of variables instead of a single variable.  The values produced
during loop execution must be lists; the values in the lists are
stored in the corresponding variables.

@example
(loop for (x y) in '((2 3) (4 5) (6 7)) collect (+ x y))
     @result{} (5 9 13)
@end example

In loop destructuring, if there are more values than variables
the trailing values are ignored, and if there are more variables
than values the trailing variables get the value @code{nil}.
If @code{nil} is used as a variable name, the corresponding
values are ignored.  Destructuring may be nested, and dotted
lists of variables like @code{(x . y)} are allowed.

@node Iteration Clauses, Accumulation Clauses, For Clauses, Loop Facility
@subsection Iteration Clauses

@noindent
Aside from @code{for} clauses, there are several other loop clauses
that control the way the loop operates.  They might be used by
themselves, or in conjunction with one or more @code{for} clauses.

@table @code
@item repeat @var{integer}
This clause simply counts up to the specified number using an
internal temporary variable.  The loops

@example
(loop repeat n do ...)
(loop for temp to n do ...)
@end example

@noindent
are identical except that the second one forces you to choose
a name for a variable you aren't actually going to use.

@item while @var{condition}
This clause stops the loop when the specified condition (any Lisp
expression) becomes @code{nil}.  For example, the following two
loops are equivalent, except for the implicit @code{nil} block
that surrounds the second one:

@example
(while @var{cond} @var{forms}@dots{})
(loop while @var{cond} do @var{forms}@dots{})
@end example

@item until @var{condition}
This clause stops the loop when the specified condition is true,
i.e., non-@code{nil}.

@item always @var{condition}
This clause stops the loop when the specified condition is @code{nil}.
Unlike @code{while}, it stops the loop using @code{return nil} so that
the @code{finally} clauses are not executed.  If all the conditions
were non-@code{nil}, the loop returns @code{t}:

@example
(if (loop for size in size-list always (> size 10))
    (some-big-sizes)
  (no-big-sizes))
@end example

@item never @var{condition}
This clause is like @code{always}, except that the loop returns
@code{t} if any conditions were false, or @code{nil} otherwise.

@item thereis @var{condition}
This clause stops the loop when the specified form is non-@code{nil};
in this case, it returns that non-@code{nil} value.  If all the
values were @code{nil}, the loop returns @code{nil}.
@end table

@node Accumulation Clauses, Other Clauses, Iteration Clauses, Loop Facility
@subsection Accumulation Clauses

@noindent
These clauses cause the loop to accumulate information about the
specified Lisp @var{form}.  The accumulated result is returned
from the loop unless overridden, say, by a @code{return} clause.

@table @code
@item collect @var{form}
This clause collects the values of @var{form} into a list.  Several
examples of @code{collect} appear elsewhere in this manual.

The word @code{collecting} is a synonym for @code{collect}, and
likewise for the other accumulation clauses.

@item append @var{form}
This clause collects lists of values into a result list using
@code{append}.

@item nconc @var{form}
This clause collects lists of values into a result list by
destructively modifying the lists rather than copying them.

@item concat @var{form}
This clause concatenates the values of the specified @var{form}
into a string.  (It and the following clause are extensions to
standard Common Lisp.)

@item vconcat @var{form}
This clause concatenates the values of the specified @var{form}
into a vector.

@item count @var{form}
This clause counts the number of times the specified @var{form}
evaluates to a non-@code{nil} value.

@item sum @var{form}
This clause accumulates the sum of the values of the specified
@var{form}, which must evaluate to a number.

@item maximize @var{form}
This clause accumulates the maximum value of the specified @var{form},
which must evaluate to a number.  The return value is undefined if
@code{maximize} is executed zero times.

@item minimize @var{form}
This clause accumulates the minimum value of the specified @var{form}.
@end table

Accumulation clauses can be followed by @samp{into @var{var}} to
cause the data to be collected into variable @var{var} (which is
automatically @code{let}-bound during the loop) rather than an
unnamed temporary variable.  Also, @code{into} accumulations do
not automatically imply a return value.  The loop must use some
explicit mechanism, such as @code{finally return}, to return
the accumulated result.

It is legal for several accumulation clauses of the same type to
accumulate into the same place.  From Steele:

@example
(loop for name in '(fred sue alice joe june)
      for kids in '((bob ken) () () (kris sunshine) ())
      collect name
      append kids)
     @result{} (fred bob ken sue alice joe kris sunshine june)
@end example

@node Other Clauses, , Accumulation Clauses, Loop Facility
@subsection Other Clauses

@noindent
This section describes the remaining loop clauses.

@table @code
@item with @var{var} = @var{value}
This clause binds a variable to a value around the loop, but
otherwise leaves the variable alone during the loop.  The following
loops are basically equivalent:

@example
(loop with x = 17 do ...)
(let ((x 17)) (loop do ...))
(loop for x = 17 then x do ...)
@end example

Naturally, the variable @var{var} might be used for some purpose
in the rest of the loop.  For example:

@example
(loop for x in my-list  with res = nil  do (push x res)
      finally return res)
@end example

This loop inserts the elements of @code{my-list} at the front of
a new list being accumulated in @code{res}, then returns the
list @code{res} at the end of the loop.  The effect is similar
to that of a @code{collect} clause, but the list gets reversed
by virtue of the fact that elements are being pushed onto the
front of @code{res} rather than the end.

If you omit the @code{=} term, the variable is initialized to
@code{nil}.  (Thus the @samp{= nil} in the above example is
unnecessary.)

Bindings made by @code{with} are sequential by default, as if
by @code{let*}.  Just like @code{for} clauses, @code{with} clauses
can be linked with @code{and} to cause the bindings to be made by
@code{let} instead.

@item if @var{condition} @var{clause}
This clause executes the following loop clause only if the specified
condition is true.  The following @var{clause} should be an accumulation,
@code{do}, @code{return}, @code{if}, or @code{unless} clause.
Several clauses may be linked by separating them with @code{and}.
These clauses may be followed by @code{else} and a clause or clauses
to execute if the condition was false.  The whole construct may
optionally be followed by the word @code{end} (which may be used to
disambiguate an @code{else} or @code{and} in a nested @code{if}).

The actual non-@code{nil} value of the condition form is available
by the name @code{it} in the ``then'' part.  For example:

@example
(setq funny-numbers '(6 13 -1))
     @result{} (6 13 -1)
(loop for x below 10
      if (oddp x)
        collect x into odds
        and if (memq x funny-numbers) return (cdr it) end
      else
        collect x into evens
      finally return (vector odds evens))
     @result{} [(1 3 5 7 9) (0 2 4 6 8)]
(setq funny-numbers '(6 7 13 -1))
     @result{} (6 7 13 -1)
(loop <@r{same thing again}>)
     @result{} (13 -1)
@end example

Note the use of @code{and} to put two clauses into the ``then''
part, one of which is itself an @code{if} clause.  Note also that
@code{end}, while normally optional, was necessary here to make
it clear that the @code{else} refers to the outermost @code{if}
clause.  In the first case, the loop returns a vector of lists
of the odd and even values of @var{x}.  In the second case, the
odd number 7 is one of the @code{funny-numbers} so the loop
returns early; the actual returned value is based on the result
of the @code{memq} call.

@item when @var{condition} @var{clause}
This clause is just a synonym for @code{if}.

@item unless @var{condition} @var{clause}
The @code{unless} clause is just like @code{if} except that the
sense of the condition is reversed.

@item named @var{name}
This clause gives a name other than @code{nil} to the implicit
block surrounding the loop.  The @var{name} is the symbol to be
used as the block name.

@item initially [do] @var{forms}...
This keyword introduces one or more Lisp forms which will be
executed before the loop itself begins (but after any variables
requested by @code{for} or @code{with} have been bound to their
initial values).  @code{initially} clauses can appear anywhere;
if there are several, they are executed in the order they appear
in the loop.  The keyword @code{do} is optional.

@item finally [do] @var{forms}...
This introduces Lisp forms which will be executed after the loop
finishes (say, on request of a @code{for} or @code{while}).
@code{initially} and @code{finally} clauses may appear anywhere
in the loop construct, but they are executed (in the specified
order) at the beginning or end, respectively, of the loop.

@item finally return @var{form}
This says that @var{form} should be executed after the loop
is done to obtain a return value.  (Without this, or some other
clause like @code{collect} or @code{return}, the loop will simply
return @code{nil}.)  Variables bound by @code{for}, @code{with},
or @code{into} will still contain their final values when @var{form}
is executed.

@item do @var{forms}...
The word @code{do} may be followed by any number of Lisp expressions
which are executed as an implicit @code{progn} in the body of the
loop.  Many of the examples in this section illustrate the use of
@code{do}.

@item return @var{form}
This clause causes the loop to return immediately.  The following
Lisp form is evaluated to give the return value of the @code{loop}
form.  The @code{finally} clauses, if any, are not executed.
Of course, @code{return} is generally used inside an @code{if} or
@code{unless}, as its use in a top-level loop clause would mean
the loop would never get to ``loop'' more than once.

The clause @samp{return @var{form}} is equivalent to
@samp{do (return @var{form})} (or @code{return-from} if the loop
was named).  The @code{return} clause is implemented a bit more
efficiently, though.
@end table

While there is no high-level way to add user extensions to @code{loop}
(comparable to @code{defsetf} for @code{setf}, say), this package
does offer two properties called @code{cl-loop-handler} and
@code{cl-loop-for-handler} which are functions to be called when
a given symbol is encountered as a top-level loop clause or
@code{for} clause, respectively.  Consult the source code in
file @file{cl-macs.el} for details.

This package's @code{loop} macro is compatible with that of Common
Lisp, except that a few features are not implemented:  @code{loop-finish}
and data-type specifiers.  Naturally, the @code{for} clauses which
iterate over keymaps, overlays, intervals, frames, windows, and
buffers are Emacs-specific extensions.

@node Multiple Values, , Loop Facility, Control Structure
@section Multiple Values

@noindent
Common Lisp functions can return zero or more results.  Emacs Lisp
functions, by contrast, always return exactly one result.  This
package makes no attempt to emulate Common Lisp multiple return
values; Emacs versions of Common Lisp functions that return more
than one value either return just the first value (as in
@code{compiler-macroexpand}) or return a list of values (as in
@code{get-setf-method}).  This package @emph{does} define placeholders
for the Common Lisp functions that work with multiple values, but
in Emacs Lisp these functions simply operate on lists instead.
The @code{values} form, for example, is a synonym for @code{list}
in Emacs.

@defspec multiple-value-bind (var@dots{}) values-form forms@dots{}
This form evaluates @var{values-form}, which must return a list of
values.  It then binds the @var{var}s to these respective values,
as if by @code{let}, and then executes the body @var{forms}.
If there are more @var{var}s than values, the extra @var{var}s
are bound to @code{nil}.  If there are fewer @var{var}s than
values, the excess values are ignored.
@end defspec

@defspec multiple-value-setq (var@dots{}) form
This form evaluates @var{form}, which must return a list of values.
It then sets the @var{var}s to these respective values, as if by
@code{setq}.  Extra @var{var}s or values are treated the same as
in @code{multiple-value-bind}.
@end defspec

The older Quiroz package attempted a more faithful (but still
imperfect) emulation of Common Lisp multiple values.  The old
method ``usually'' simulated true multiple values quite well,
but under certain circumstances would leave spurious return
values in memory where a later, unrelated @code{multiple-value-bind}
form would see them.

Since a perfect emulation is not feasible in Emacs Lisp, this
package opts to keep it as simple and predictable as possible.

@node Macros, Declarations, Control Structure, Top
@chapter Macros

@noindent
This package implements the various Common Lisp features of
@code{defmacro}, such as destructuring, @code{&environment},
and @code{&body}.  Top-level @code{&whole} is not implemented
for @code{defmacro} due to technical difficulties.
@xref{Argument Lists}.

Destructuring is made available to the user by way of the
following macro:

@defspec destructuring-bind arglist expr forms@dots{}
This macro expands to code which executes @var{forms}, with
the variables in @var{arglist} bound to the list of values
returned by @var{expr}.  The @var{arglist} can include all
the features allowed for @code{defmacro} argument lists,
including destructuring.  (The @code{&environment} keyword
is not allowed.)  The macro expansion will signal an error
if @var{expr} returns a list of the wrong number of arguments
or with incorrect keyword arguments.
@end defspec

This package also includes the Common Lisp @code{define-compiler-macro}
facility, which allows you to define compile-time expansions and
optimizations for your functions.

@defspec define-compiler-macro name arglist forms@dots{}
This form is similar to @code{defmacro}, except that it only expands
calls to @var{name} at compile-time; calls processed by the Lisp
interpreter are not expanded, nor are they expanded by the
@code{macroexpand} function.

The argument list may begin with a @code{&whole} keyword and a
variable.  This variable is bound to the macro-call form itself,
i.e., to a list of the form @samp{(@var{name} @var{args}@dots{})}.
If the macro expander returns this form unchanged, then the
compiler treats it as a normal function call.  This allows
compiler macros to work as optimizers for special cases of a
function, leaving complicated cases alone.

For example, here is a simplified version of a definition that
appears as a standard part of this package:

@example
(define-compiler-macro member* (&whole form a list &rest keys)
  (if (and (null keys)
           (eq (car-safe a) 'quote)
           (not (floatp-safe (cadr a))))
      (list 'memq a list)
    form))
@end example

@noindent
This definition causes @code{(member* @var{a} @var{list})} to change
to a call to the faster @code{memq} in the common case where @var{a}
is a non-floating-point constant; if @var{a} is anything else, or
if there are any keyword arguments in the call, then the original
@code{member*} call is left intact.  (The actual compiler macro
for @code{member*} optimizes a number of other cases, including
common @code{:test} predicates.)
@end defspec

@defun compiler-macroexpand form
This function is analogous to @code{macroexpand}, except that it
expands compiler macros rather than regular macros.  It returns
@var{form} unchanged if it is not a call to a function for which
a compiler macro has been defined, or if that compiler macro
decided to punt by returning its @code{&whole} argument.  Like
@code{macroexpand}, it expands repeatedly until it reaches a form
for which no further expansion is possible.
@end defun

@xref{Macro Bindings}, for descriptions of the @code{macrolet}
and @code{symbol-macrolet} forms for making ``local'' macro
definitions.

@node Declarations, Symbols, Macros, Top
@chapter Declarations

@noindent
Common Lisp includes a complex and powerful ``declaration''
mechanism that allows you to give the compiler special hints
about the types of data that will be stored in particular variables,
and about the ways those variables and functions will be used.  This
package defines versions of all the Common Lisp declaration forms:
@code{declare}, @code{locally}, @code{proclaim}, @code{declaim},
and @code{the}.

Most of the Common Lisp declarations are not currently useful in
Emacs Lisp, as the byte-code system provides little opportunity
to benefit from type information, and @code{special} declarations
are redundant in a fully dynamically-scoped Lisp.  A few
declarations are meaningful when the optimizing Emacs 19 byte
compiler is being used, however.  Under the earlier non-optimizing
compiler, these declarations will effectively be ignored.

@defun proclaim decl-spec
This function records a ``global'' declaration specified by
@var{decl-spec}.  Since @code{proclaim} is a function, @var{decl-spec}
is evaluated and thus should normally be quoted.
@end defun

@defspec declaim decl-specs@dots{}
This macro is like @code{proclaim}, except that it takes any number
of @var{decl-spec} arguments, and the arguments are unevaluated and
unquoted.  The @code{declaim} macro also puts an @code{(eval-when
(compile load eval) ...)} around the declarations so that they will
be registered at compile-time as well as at run-time.  (This is vital,
since normally the declarations are meant to influence the way the
compiler treats the rest of the file that contains the @code{declaim}
form.)
@end defspec

@defspec declare decl-specs@dots{}
This macro is used to make declarations within functions and other
code.  Common Lisp allows declarations in various locations, generally
at the beginning of any of the many ``implicit @code{progn}s''
throughout Lisp syntax, such as function bodies, @code{let} bodies,
etc.  Currently the only declaration understood by @code{declare}
is @code{special}.
@end defspec

@defspec locally declarations@dots{} forms@dots{}
In this package, @code{locally} is no different from @code{progn}.
@end defspec

@defspec the type form
Type information provided by @code{the} is ignored in this package;
in other words, @code{(the @var{type} @var{form})} is equivalent
to @var{form}.  Future versions of the optimizing byte-compiler may
make use of this information.

For example, @code{mapcar} can map over both lists and arrays.  It is
hard for the compiler to expand @code{mapcar} into an in-line loop
unless it knows whether the sequence will be a list or an array ahead
of time.  With @code{(mapcar 'car (the vector foo))}, a future
compiler would have enough information to expand the loop in-line.
For now, Emacs Lisp will treat the above code as exactly equivalent
to @code{(mapcar 'car foo)}.
@end defspec

Each @var{decl-spec} in a @code{proclaim}, @code{declaim}, or
@code{declare} should be a list beginning with a symbol that says
what kind of declaration it is.  This package currently understands
@code{special}, @code{inline}, @code{notinline}, @code{optimize},
and @code{warn} declarations.  (The @code{warn} declaration is an
extension of standard Common Lisp.)  Other Common Lisp declarations,
such as @code{type} and @code{ftype}, are silently ignored.

@table @code
@item special
Since all variables in Emacs Lisp are ``special'' (in the Common
Lisp sense), @code{special} declarations are only advisory.  They
simply tell the optimizing byte compiler that the specified
variables are intentionally being referred to without being
bound in the body of the function.  The compiler normally emits
warnings for such references, since they could be typographical
errors for references to local variables.

The declaration @code{(declare (special @var{var1} @var{var2}))} is
equivalent to @code{(defvar @var{var1}) (defvar @var{var2})} in the
optimizing compiler, or to nothing at all in older compilers (which
do not warn for non-local references).

In top-level contexts, it is generally better to write
@code{(defvar @var{var})} than @code{(declaim (special @var{var}))},
since @code{defvar} makes your intentions clearer.  But the older
byte compilers can not handle @code{defvar}s appearing inside of
functions, while @code{(declare (special @var{var}))} takes care
to work correctly with all compilers.

@item inline
The @code{inline} @var{decl-spec} lists one or more functions
whose bodies should be expanded ``in-line'' into calling functions
whenever the compiler is able to arrange for it.  For example,
the Common Lisp function @code{cadr} is declared @code{inline}
by this package so that the form @code{(cadr @var{x})} will
expand directly into @code{(car (cdr @var{x}))} when it is called
in user functions, for a savings of one (relatively expensive)
function call.

The following declarations are all equivalent.  Note that the
@code{defsubst} form is a convenient way to define a function
and declare it inline all at once, but it is available only in
Emacs 19.

@example
(declaim (inline foo bar))
(eval-when (compile load eval) (proclaim '(inline foo bar)))
(proclaim-inline foo bar)   ; XEmacs only
(defsubst foo (...) ...)    ; instead of defun; Emacs 19 only
@end example

@strong{Please note:}  This declaration remains in effect after the
containing source file is done.  It is correct to use it to
request that a function you have defined should be inlined,
but it is impolite to use it to request inlining of an external
function.

In Common Lisp, it is possible to use @code{(declare (inline @dots{}))}
before a particular call to a function to cause just that call to
be inlined; the current byte compilers provide no way to implement
this, so @code{(declare (inline @dots{}))} is currently ignored by
this package.

@item notinline
The @code{notinline} declaration lists functions which should
not be inlined after all; it cancels a previous @code{inline}
declaration.

@item optimize
This declaration controls how much optimization is performed by
the compiler.  Naturally, it is ignored by the earlier non-optimizing
compilers.

The word @code{optimize} is followed by any number of lists like
@code{(speed 3)} or @code{(safety 2)}.  Common Lisp defines several
optimization ``qualities''; this package ignores all but @code{speed}
and @code{safety}.  The value of a quality should be an integer from
0 to 3, with 0 meaning ``unimportant'' and 3 meaning ``very important.''
The default level for both qualities is 1.

In this package, with the Emacs 19 optimizing compiler, the
@code{speed} quality is tied to the @code{byte-compile-optimize}
flag, which is set to @code{nil} for @code{(speed 0)} and to
@code{t} for higher settings; and the @code{safety} quality is
tied to the @code{byte-compile-delete-errors} flag, which is
set to @code{t} for @code{(safety 3)} and to @code{nil} for all
lower settings.  (The latter flag controls whether the compiler
is allowed to optimize out code whose only side-effect could
be to signal an error, e.g., rewriting @code{(progn foo bar)} to
@code{bar} when it is not known whether @code{foo} will be bound
at run-time.)

Note that even compiling with @code{(safety 0)}, the Emacs
byte-code system provides sufficient checking to prevent real
harm from being done.  For example, barring serious bugs in
Emacs itself, Emacs will not crash with a segmentation fault
just because of an error in a fully-optimized Lisp program.

The @code{optimize} declaration is normally used in a top-level
@code{proclaim} or @code{declaim} in a file; Common Lisp allows
it to be used with @code{declare} to set the level of optimization
locally for a given form, but this will not work correctly with the
current version of the optimizing compiler.  (The @code{declare}
will set the new optimization level, but that level will not
automatically be unset after the enclosing form is done.)

@item warn
This declaration controls what sorts of warnings are generated
by the byte compiler.  Again, only the optimizing compiler
generates warnings.  The word @code{warn} is followed by any
number of ``warning qualities,'' similar in form to optimization
qualities.  The currently supported warning types are
@code{redefine}, @code{callargs}, @code{unresolved}, and
@code{free-vars}; in the current system, a value of 0 will
disable these warnings and any higher value will enable them.
See the documentation for the optimizing byte compiler for details.
@end table

@node Symbols, Numbers, Declarations, Top
@chapter Symbols

@noindent
This package defines several symbol-related features that were
missing from Emacs Lisp.

@menu
* Property Lists::       `getf', `remf'
* Creating Symbols::     `gensym', `gentemp'
@end menu

@node Property Lists, Creating Symbols, Symbols, Symbols
@section Property Lists

@noindent
These functions augment the standard Emacs Lisp functions @code{get}
and @code{put} for operating on properties attached to objects.
There are also functions for working with property lists as
first-class data structures not attached to particular objects.

@defun getf place property &optional default
This function scans the list @var{place} as if it were a property
list, i.e., a list of alternating property names and values.  If
an even-numbered element of @var{place} is found which is @code{eq}
to @var{property}, the following odd-numbered element is returned.
Otherwise, @var{default} is returned (or @code{nil} if no default
is given).

In particular,

@example
(get sym prop)  @equiv{}  (getf (symbol-plist sym) prop)
@end example

It is legal to use @code{getf} as a @code{setf} place, in which case
its @var{place} argument must itself be a legal @code{setf} place.
The @var{default} argument, if any, is ignored in this context.
The effect is to change (via @code{setcar}) the value cell in the
list that corresponds to @var{property}, or to cons a new property-value
pair onto the list if the property is not yet present.

@example
(put sym prop val)  @equiv{}  (setf (getf (symbol-plist sym) prop) val)
@end example

The @code{get} function is also @code{setf}-able.  The fact that
@code{default} is ignored can sometimes be useful:

@example
(incf (get 'foo 'usage-count 0))
@end example

Here, symbol @code{foo}'s @code{usage-count} property is incremented
if it exists, or set to 1 (an incremented 0) otherwise.

When not used as a @code{setf} form, @code{getf} is just a regular
function and its @var{place} argument can actually be any Lisp
expression.
@end defun

@defspec remf place property
This macro removes the property-value pair for @var{property} from
the property list stored at @var{place}, which is any @code{setf}-able
place expression.  It returns true if the property was found.  Note
that if @var{property} happens to be first on the list, this will
effectively do a @code{(setf @var{place} (cddr @var{place}))},
whereas if it occurs later, this simply uses @code{setcdr} to splice
out the property and value cells.
@end defspec

@iftex
@secno=2
@end iftex

@node Creating Symbols, , Property Lists, Symbols
@section Creating Symbols

@noindent
These functions create unique symbols, typically for use as
temporary variables.

@defun gensym &optional x
This function creates a new, uninterned symbol (using @code{make-symbol})
with a unique name.  (The name of an uninterned symbol is relevant
only if the symbol is printed.)  By default, the name is generated
from an increasing sequence of numbers, @samp{G1000}, @samp{G1001},
@samp{G1002}, etc.  If the optional argument @var{x} is a string, that
string is used as a prefix instead of @samp{G}.  Uninterned symbols
are used in macro expansions for temporary variables, to ensure that
their names will not conflict with ``real'' variables in the user's
code.
@end defun

@defvar *gensym-counter*
This variable holds the counter used to generate @code{gensym} names.
It is incremented after each use by @code{gensym}.  In Common Lisp
this is initialized with 0, but this package initializes it with a
random (time-dependent) value to avoid trouble when two files that
each used @code{gensym} in their compilation are loaded together.

@strong{XEmacs note:} As of XEmacs 21.0, an uninterned symbol remains
uninterned even after being dumped to bytecode.  Older versions of Emacs
didn't distinguish the printed representation of interned and uninterned
symbols, so their names had to be treated more carefully.
@end defvar

@defun gentemp &optional x
This function is like @code{gensym}, except that it produces a new
@emph{interned} symbol.  If the symbol that is generated already
exists, the function keeps incrementing the counter and trying
again until a new symbol is generated.
@end defun

The Quiroz @file{cl.el} package also defined a @code{defkeyword}
form for creating self-quoting keyword symbols.  This package
automatically creates all keywords that are called for by
@code{&key} argument specifiers, and discourages the use of
keywords as data unrelated to keyword arguments, so the
@code{defkeyword} form has been discontinued.

@iftex
@chapno=11
@end iftex

@node Numbers, Sequences, Symbols, Top
@chapter Numbers

@noindent
This section defines a few simple Common Lisp operations on numbers
which were left out of Emacs Lisp.

@menu
* Predicates on Numbers::       `plusp', `oddp', `floatp-safe', etc.
* Numerical Functions::         `abs', `expt', `floor*', etc.
* Random Numbers::              `random*', `make-random-state'
* Implementation Parameters::   `most-positive-fixnum', `most-positive-float'
@end menu

@iftex
@secno=1
@end iftex

@node Predicates on Numbers, Numerical Functions, Numbers, Numbers
@section Predicates on Numbers

@noindent
These functions return @code{t} if the specified condition is
true of the numerical argument, or @code{nil} otherwise.

@defun plusp number
This predicate tests whether @var{number} is positive.  It is an
error if the argument is not a number.
@end defun

@defun minusp number
This predicate tests whether @var{number} is negative.  It is an
error if the argument is not a number.
@end defun

@defun oddp integer
This predicate tests whether @var{integer} is odd.  It is an
error if the argument is not an integer.
@end defun

@defun evenp integer
This predicate tests whether @var{integer} is even.  It is an
error if the argument is not an integer.
@end defun

@defun floatp-safe object
This predicate tests whether @var{object} is a floating-point
number.  On systems that support floating-point, this is equivalent
to @code{floatp}.  On other systems, this always returns @code{nil}.
@end defun

@iftex
@secno=3
@end iftex

@node Numerical Functions, Random Numbers, Predicates on Numbers, Numbers
@section Numerical Functions

@noindent
These functions perform various arithmetic operations on numbers.

@defun abs number
This function returns the absolute value of @var{number}.  (Newer
versions of Emacs provide this as a built-in function; this package
defines @code{abs} only for Emacs 18 versions which don't provide
it as a primitive.)
@end defun

@defun expt base power
This function returns @var{base} raised to the power of @var{number}.
(Newer versions of Emacs provide this as a built-in function; this
package defines @code{expt} only for Emacs 18 versions which don't
provide it as a primitive.)
@end defun

@defun gcd &rest integers
This function returns the Greatest Common Divisor of the arguments.
For one argument, it returns the absolute value of that argument.
For zero arguments, it returns zero.
@end defun

@defun lcm &rest integers
This function returns the Least Common Multiple of the arguments.
For one argument, it returns the absolute value of that argument.
For zero arguments, it returns one.
@end defun

@defun isqrt integer
This function computes the ``integer square root'' of its integer
argument, i.e., the greatest integer less than or equal to the true
square root of the argument.
@end defun

@defun floor* number &optional divisor
This function implements the Common Lisp @code{floor} function.
It is called @code{floor*} to avoid name conflicts with the
simpler @code{floor} function built-in to Emacs 19.

With one argument, @code{floor*} returns a list of two numbers:
The argument rounded down (toward minus infinity) to an integer,
and the ``remainder'' which would have to be added back to the
first return value to yield the argument again.  If the argument
is an integer @var{x}, the result is always the list @code{(@var{x} 0)}.
If the argument is an Emacs 19 floating-point number, the first
result is a Lisp integer and the second is a Lisp float between
0 (inclusive) and 1 (exclusive).

With two arguments, @code{floor*} divides @var{number} by
@var{divisor}, and returns the floor of the quotient and the
corresponding remainder as a list of two numbers.  If
@code{(floor* @var{x} @var{y})} returns @code{(@var{q} @var{r})},
then @code{@var{q}*@var{y} + @var{r} = @var{x}}, with @var{r}
between 0 (inclusive) and @var{r} (exclusive).  Also, note
that @code{(floor* @var{x})} is exactly equivalent to
@code{(floor* @var{x} 1)}.

This function is entirely compatible with Common Lisp's @code{floor}
function, except that it returns the two results in a list since
Emacs Lisp does not support multiple-valued functions.
@end defun

@defun ceiling* number &optional divisor
This function implements the Common Lisp @code{ceiling} function,
which is analogous to @code{floor} except that it rounds the
argument or quotient of the arguments up toward plus infinity.
The remainder will be between 0 and minus @var{r}.
@end defun

@defun truncate* number &optional divisor
This function implements the Common Lisp @code{truncate} function,
which is analogous to @code{floor} except that it rounds the
argument or quotient of the arguments toward zero.  Thus it is
equivalent to @code{floor*} if the argument or quotient is
positive, or to @code{ceiling*} otherwise.  The remainder has
the same sign as @var{number}.
@end defun

@defun round* number &optional divisor
This function implements the Common Lisp @code{round} function,
which is analogous to @code{floor} except that it rounds the
argument or quotient of the arguments to the nearest integer.
In the case of a tie (the argument or quotient is exactly
halfway between two integers), it rounds to the even integer.
@end defun

@defun mod* number divisor
This function returns the same value as the second return value
of @code{floor}.
@end defun

@defun rem* number divisor
This function returns the same value as the second return value
of @code{truncate}.
@end defun

These definitions are compatible with those in the Quiroz
@file{cl.el} package, except that this package appends @samp{*}
to certain function names to avoid conflicts with existing
Emacs 19 functions, and that the mechanism for returning
multiple values is different.

@iftex
@secno=8
@end iftex

@node Random Numbers, Implementation Parameters, Numerical Functions, Numbers
@section Random Numbers

@noindent
This package also provides an implementation of the Common Lisp
random number generator.  It uses its own additive-congruential
algorithm, which is much more likely to give statistically clean
random numbers than the simple generators supplied by many
operating systems.

@defun random* number &optional state
This function returns a random nonnegative number less than
@var{number}, and of the same type (either integer or floating-point).
The @var{state} argument should be a @code{random-state} object
which holds the state of the random number generator.  The
function modifies this state object as a side effect.  If
@var{state} is omitted, it defaults to the variable
@code{*random-state*}, which contains a pre-initialized
@code{random-state} object.
@end defun

@defvar *random-state*
This variable contains the system ``default'' @code{random-state}
object, used for calls to @code{random*} that do not specify an
alternative state object.  Since any number of programs in the
Emacs process may be accessing @code{*random-state*} in interleaved
fashion, the sequence generated from this variable will be
irreproducible for all intents and purposes.
@end defvar

@defun make-random-state &optional state
This function creates or copies a @code{random-state} object.
If @var{state} is omitted or @code{nil}, it returns a new copy of
@code{*random-state*}.  This is a copy in the sense that future
sequences of calls to @code{(random* @var{n})} and
@code{(random* @var{n} @var{s})} (where @var{s} is the new
random-state object) will return identical sequences of random
numbers.

If @var{state} is a @code{random-state} object, this function
returns a copy of that object.  If @var{state} is @code{t}, this
function returns a new @code{random-state} object seeded from the
date and time.  As an extension to Common Lisp, @var{state} may also
be an integer in which case the new object is seeded from that
integer; each different integer seed will result in a completely
different sequence of random numbers.

It is legal to print a @code{random-state} object to a buffer or
file and later read it back with @code{read}.  If a program wishes
to use a sequence of pseudo-random numbers which can be reproduced
later for debugging, it can call @code{(make-random-state t)} to
get a new sequence, then print this sequence to a file.  When the
program is later rerun, it can read the original run's random-state
from the file.
@end defun

@defun random-state-p object
This predicate returns @code{t} if @var{object} is a
@code{random-state} object, or @code{nil} otherwise.
@end defun

@node Implementation Parameters, , Random Numbers, Numbers
@section Implementation Parameters

@noindent
This package defines several useful constants having to with numbers.

@defvar most-positive-fixnum
This constant equals the largest value a Lisp integer can hold.
It is typically @code{2^23-1} or @code{2^25-1}.
@end defvar

@defvar most-negative-fixnum
This constant equals the smallest (most negative) value a Lisp
integer can hold.
@end defvar

The following parameters have to do with floating-point numbers.
This package determines their values by exercising the computer's
floating-point arithmetic in various ways.  Because this operation
might be slow, the code for initializing them is kept in a separate
function that must be called before the parameters can be used.

@defun cl-float-limits
This function makes sure that the Common Lisp floating-point
parameters like @code{most-positive-float} have been initialized.
Until it is called, these parameters will be @code{nil}.  If this
version of Emacs does not support floats (e.g., most versions of
Emacs 18), the parameters will remain @code{nil}.  If the parameters
have already been initialized, the function returns immediately.

The algorithm makes assumptions that will be valid for most modern
machines, but will fail if the machine's arithmetic is extremely
unusual, e.g., decimal.
@end defun

Since true Common Lisp supports up to four different floating-point
precisions, it has families of constants like
@code{most-positive-single-float}, @code{most-positive-double-float},
@code{most-positive-long-float}, and so on.  Emacs has only one
floating-point precision, so this package omits the precision word
from the constants' names.

@defvar most-positive-float
This constant equals the largest value a Lisp float can hold.
For those systems whose arithmetic supports infinities, this is
the largest @emph{finite} value.  For IEEE machines, the value
is approximately @code{1.79e+308}.
@end defvar

@defvar most-negative-float
This constant equals the most-negative value a Lisp float can hold.
(It is assumed to be equal to @code{(- most-positive-float)}.)
@end defvar

@defvar least-positive-float
This constant equals the smallest Lisp float value greater than zero.
For IEEE machines, it is about @code{4.94e-324} if denormals are
supported or @code{2.22e-308} if not.
@end defvar

@defvar least-positive-normalized-float
This constant equals the smallest @emph{normalized} Lisp float greater
than zero, i.e., the smallest value for which IEEE denormalization
will not result in a loss of precision.  For IEEE machines, this
value is about @code{2.22e-308}.  For machines that do not support
the concept of denormalization and gradual underflow, this constant
will always equal @code{least-positive-float}.
@end defvar

@defvar least-negative-float
This constant is the negative counterpart of @code{least-positive-float}.
@end defvar

@defvar least-negative-normalized-float
This constant is the negative counterpart of
@code{least-positive-normalized-float}.
@end defvar

@defvar float-epsilon
This constant is the smallest positive Lisp float that can be added
to 1.0 to produce a distinct value.  Adding a smaller number to 1.0
will yield 1.0 again due to roundoff.  For IEEE machines, epsilon
is about @code{2.22e-16}.
@end defvar

@defvar float-negative-epsilon
This is the smallest positive value that can be subtracted from
1.0 to produce a distinct value.  For IEEE machines, it is about
@code{1.11e-16}.
@end defvar

@iftex
@chapno=13
@end iftex

@node Sequences, Lists, Numbers, Top
@chapter Sequences

@noindent
Common Lisp defines a number of functions that operate on
@dfn{sequences}, which are either lists, strings, or vectors.
Emacs Lisp includes a few of these, notably @code{elt} and
@code{length}; this package defines most of the rest.

@menu
* Sequence Basics::          Arguments shared by all sequence functions
* Mapping over Sequences::   `mapcar*', `mapcan', `map', `every', etc.
* Sequence Functions::       `subseq', `remove*', `substitute', etc.
* Searching Sequences::      `find', `position', `count', `search', etc.
* Sorting Sequences::        `sort*', `stable-sort', `merge'
@end menu

@node Sequence Basics, Mapping over Sequences, Sequences, Sequences
@section Sequence Basics

@noindent
Many of the sequence functions take keyword arguments; @pxref{Argument
Lists}.  All keyword arguments are optional and, if specified,
may appear in any order.

The @code{:key} argument should be passed either @code{nil}, or a
function of one argument.  This key function is used as a filter
through which the elements of the sequence are seen; for example,
@code{(find x y :key 'car)} is similar to @code{(assoc* x y)}:
It searches for an element of the list whose @code{car} equals
@code{x}, rather than for an element which equals @code{x} itself.
If @code{:key} is omitted or @code{nil}, the filter is effectively
the identity function.

The @code{:test} and @code{:test-not} arguments should be either
@code{nil}, or functions of two arguments.  The test function is
used to compare two sequence elements, or to compare a search value
with sequence elements.  (The two values are passed to the test
function in the same order as the original sequence function
arguments from which they are derived, or, if they both come from
the same sequence, in the same order as they appear in that sequence.)
The @code{:test} argument specifies a function which must return
true (non-@code{nil}) to indicate a match; instead, you may use
@code{:test-not} to give a function which returns @emph{false} to
indicate a match.  The default test function is @code{:test 'eql}.

Many functions which take @var{item} and @code{:test} or @code{:test-not}
arguments also come in @code{-if} and @code{-if-not} varieties,
where a @var{predicate} function is passed instead of @var{item},
and sequence elements match if the predicate returns true on them
(or false in the case of @code{-if-not}).  For example:

@example
(remove* 0 seq :test '=)  @equiv{}  (remove-if 'zerop seq)
@end example

@noindent
to remove all zeros from sequence @code{seq}.

Some operations can work on a subsequence of the argument sequence;
these function take @code{:start} and @code{:end} arguments which
default to zero and the length of the sequence, respectively.
Only elements between @var{start} (inclusive) and @var{end}
(exclusive) are affected by the operation.  The @var{end} argument
may be passed @code{nil} to signify the length of the sequence;
otherwise, both @var{start} and @var{end} must be integers, with
@code{0 <= @var{start} <= @var{end} <= (length @var{seq})}.
If the function takes two sequence arguments, the limits are
defined by keywords @code{:start1} and @code{:end1} for the first,
and @code{:start2} and @code{:end2} for the second.

A few functions accept a @code{:from-end} argument, which, if
non-@code{nil}, causes the operation to go from right-to-left
through the sequence instead of left-to-right, and a @code{:count}
argument, which specifies an integer maximum number of elements
to be removed or otherwise processed.

The sequence functions make no guarantees about the order in
which the @code{:test}, @code{:test-not}, and @code{:key} functions
are called on various elements.  Therefore, it is a bad idea to depend
on side effects of these functions.  For example, @code{:from-end}
may cause the sequence to be scanned actually in reverse, or it may
be scanned forwards but computing a result ``as if'' it were scanned
backwards.  (Some functions, like @code{mapcar*} and @code{every},
@emph{do} specify exactly the order in which the function is called
so side effects are perfectly acceptable in those cases.)

Strings in GNU Emacs 19 may contain ``text properties'' as well
as character data.  Except as noted, it is undefined whether or
not text properties are preserved by sequence functions.  For
example, @code{(remove* ?A @var{str})} may or may not preserve
the properties of the characters copied from @var{str} into the
result.

@node Mapping over Sequences, Sequence Functions, Sequence Basics, Sequences
@section Mapping over Sequences

@noindent
These functions ``map'' the function you specify over the elements
of lists or arrays.  They are all variations on the theme of the
built-in function @code{mapcar}.

@defun mapcar* function seq &rest more-seqs
This function calls @var{function} on successive parallel sets of
elements from its argument sequences.  Given a single @var{seq}
argument it is equivalent to @code{mapcar}; given @var{n} sequences,
it calls the function with the first elements of each of the sequences
as the @var{n} arguments to yield the first element of the result
list, then with the second elements, and so on.  The mapping stops as
soon as the shortest sequence runs out.  The argument sequences may
be any mixture of lists, strings, and vectors; the return sequence
is always a list.

Common Lisp's @code{mapcar} accepts multiple arguments but works
only on lists; Emacs Lisp's @code{mapcar} accepts a single sequence
argument.  This package's @code{mapcar*} works as a compatible
superset of both.
@end defun

@defun map result-type function seq &rest more-seqs
This function maps @var{function} over the argument sequences,
just like @code{mapcar*}, but it returns a sequence of type
@var{result-type} rather than a list.  @var{result-type} must
be one of the following symbols: @code{vector}, @code{string},
@code{list} (in which case the effect is the same as for
@code{mapcar*}), or @code{nil} (in which case the results are
thrown away and @code{map} returns @code{nil}).
@end defun

@defun maplist function list &rest more-lists
This function calls @var{function} on each of its argument lists,
then on the @code{cdr}s of those lists, and so on, until the
shortest list runs out.  The results are returned in the form
of a list.  Thus, @code{maplist} is like @code{mapcar*} except
that it passes in the list pointers themselves rather than the
@code{car}s of the advancing pointers.
@end defun

@defun mapc function seq &rest more-seqs
This function is like @code{mapcar*}, except that the values
returned by @var{function} are ignored and thrown away rather
than being collected into a list.  The return value of @code{mapc}
is @var{seq}, the first sequence.
@end defun

@defun mapl function list &rest more-lists
This function is like @code{maplist}, except that it throws away
the values returned by @var{function}.
@end defun

@defun mapcan function seq &rest more-seqs
This function is like @code{mapcar*}, except that it concatenates
the return values (which must be lists) using @code{nconc},
rather than simply collecting them into a list.
@end defun

@defun mapcon function list &rest more-lists
This function is like @code{maplist}, except that it concatenates
the return values using @code{nconc}.
@end defun

@defun some predicate seq &rest more-seqs
This function calls @var{predicate} on each element of @var{seq}
in turn; if @var{predicate} returns a non-@code{nil} value,
@code{some} returns that value, otherwise it returns @code{nil}.
Given several sequence arguments, it steps through the sequences
in parallel until the shortest one runs out, just as in
@code{mapcar*}.  You can rely on the left-to-right order in which
the elements are visited, and on the fact that mapping stops
immediately as soon as @var{predicate} returns non-@code{nil}.
@end defun

@defun every predicate seq &rest more-seqs
This function calls @var{predicate} on each element of the sequence(s)
in turn; it returns @code{nil} as soon as @var{predicate} returns
@code{nil} for any element, or @code{t} if the predicate was true
for all elements.
@end defun

@defun notany predicate seq &rest more-seqs
This function calls @var{predicate} on each element of the sequence(s)
in turn; it returns @code{nil} as soon as @var{predicate} returns
a non-@code{nil} value for any element, or @code{t} if the predicate
was @code{nil} for all elements.
@end defun

@defun notevery predicate seq &rest more-seqs
This function calls @var{predicate} on each element of the sequence(s)
in turn; it returns a non-@code{nil} value as soon as @var{predicate}
returns @code{nil} for any element, or @code{t} if the predicate was
true for all elements.
@end defun

@defun reduce function seq @t{&key :from-end :start :end :initial-value :key}
This function combines the elements of @var{seq} using an associative
binary operation.  Suppose @var{function} is @code{*} and @var{seq} is
the list @code{(2 3 4 5)}.  The first two elements of the list are
combined with @code{(* 2 3) = 6}; this is combined with the next
element, @code{(* 6 4) = 24}, and that is combined with the final
element: @code{(* 24 5) = 120}.  Note that the @code{*} function happens
to be self-reducing, so that @code{(* 2 3 4 5)} has the same effect as
an explicit call to @code{reduce}.

If @code{:from-end} is true, the reduction is right-associative instead
of left-associative:

@example
(reduce '- '(1 2 3 4))
     @equiv{} (- (- (- 1 2) 3) 4) @result{} -8
(reduce '- '(1 2 3 4) :from-end t)
     @equiv{} (- 1 (- 2 (- 3 4))) @result{} -2
@end example

If @code{:key} is specified, it is a function of one argument which
is called on each of the sequence elements in turn.

If @code{:initial-value} is specified, it is effectively added to the
front (or rear in the case of @code{:from-end}) of the sequence.
The @code{:key} function is @emph{not} applied to the initial value.

If the sequence, including the initial value, has exactly one element
then that element is returned without ever calling @var{function}.
If the sequence is empty (and there is no initial value), then
@var{function} is called with no arguments to obtain the return value.
@end defun

All of these mapping operations can be expressed conveniently in
terms of the @code{loop} macro.  In compiled code, @code{loop} will
be faster since it generates the loop as in-line code with no
function calls.

@node Sequence Functions, Searching Sequences, Mapping over Sequences, Sequences
@section Sequence Functions

@noindent
This section describes a number of Common Lisp functions for
operating on sequences.

@defun subseq sequence start &optional end
This function returns a given subsequence of the argument
@var{sequence}, which may be a list, string, or vector.
The indices @var{start} and @var{end} must be in range, and
@var{start} must be no greater than @var{end}.  If @var{end}
is omitted, it defaults to the length of the sequence.  The
return value is always a copy; it does not share structure
with @var{sequence}.

As an extension to Common Lisp, @var{start} and/or @var{end}
may be negative, in which case they represent a distance back
from the end of the sequence.  This is for compatibility with
Emacs' @code{substring} function.  Note that @code{subseq} is
the @emph{only} sequence function that allows negative
@var{start} and @var{end}.

You can use @code{setf} on a @code{subseq} form to replace a
specified range of elements with elements from another sequence.
The replacement is done as if by @code{replace}, described below.
@end defun

@defun concatenate result-type &rest seqs
This function concatenates the argument sequences together to
form a result sequence of type @var{result-type}, one of the
symbols @code{vector}, @code{string}, or @code{list}.  The
arguments are always copied, even in cases such as
@code{(concatenate 'list '(1 2 3))} where the result is
identical to an argument.
@end defun

@defun fill seq item @t{&key :start :end}
This function fills the elements of the sequence (or the specified
part of the sequence) with the value @var{item}.
@end defun

@defun replace seq1 seq2 @t{&key :start1 :end1 :start2 :end2}
This function copies part of @var{seq2} into part of @var{seq1}.
The sequence @var{seq1} is not stretched or resized; the amount
of data copied is simply the shorter of the source and destination
(sub)sequences.  The function returns @var{seq1}.

If @var{seq1} and @var{seq2} are @code{eq}, then the replacement
will work correctly even if the regions indicated by the start
and end arguments overlap.  However, if @var{seq1} and @var{seq2}
are lists which share storage but are not @code{eq}, and the
start and end arguments specify overlapping regions, the effect
is undefined.
@end defun

@defun remove* item seq @t{&key :test :test-not :key :count :start :end :from-end}
This returns a copy of @var{seq} with all elements matching
@var{item} removed.  The result may share storage with or be
@code{eq} to @var{seq} in some circumstances, but the original
@var{seq} will not be modified.  The @code{:test}, @code{:test-not},
and @code{:key} arguments define the matching test that is used;
by default, elements @code{eql} to @var{item} are removed.  The
@code{:count} argument specifies the maximum number of matching
elements that can be removed (only the leftmost @var{count} matches
are removed).  The @code{:start} and @code{:end} arguments specify
a region in @var{seq} in which elements will be removed; elements
outside that region are not matched or removed.  The @code{:from-end}
argument, if true, says that elements should be deleted from the
end of the sequence rather than the beginning (this matters only
if @var{count} was also specified).
@end defun

@defun delete* item seq @t{&key :test :test-not :key :count :start :end :from-end}
This deletes all elements of @var{seq} which match @var{item}.
It is a destructive operation.  Since Emacs Lisp does not support
stretchable strings or vectors, this is the same as @code{remove*}
for those sequence types.  On lists, @code{remove*} will copy the
list if necessary to preserve the original list, whereas
@code{delete*} will splice out parts of the argument list.
Compare @code{append} and @code{nconc}, which are analogous
non-destructive and destructive list operations in Emacs Lisp.
@end defun

@findex remove-if
@findex remove-if-not
@findex delete-if
@findex delete-if-not
The predicate-oriented functions @code{remove-if}, @code{remove-if-not},
@code{delete-if}, and @code{delete-if-not} are defined similarly.

@defun delete item list
This MacLisp-compatible function deletes from @var{list} all elements
which are @code{equal} to @var{item}.  The @code{delete} function is
built-in to Emacs 19; this package defines it equivalently in Emacs 18.
@end defun

@defun remove item list
This function removes from @var{list} all elements which are
@code{equal} to @var{item}.  This package defines it for symmetry
with @code{delete}, even though @code{remove} is not built-in to
Emacs 19.
@end defun

@defun remq item list
This function removes from @var{list} all elements which are
@code{eq} to @var{item}.  This package defines it for symmetry
with @code{delq}, even though @code{remq} is not built-in to
Emacs 19.
@end defun

@defun remove-duplicates seq @t{&key :test :test-not :key :start :end :from-end}
This function returns a copy of @var{seq} with duplicate elements
removed.  Specifically, if two elements from the sequence match
according to the @code{:test}, @code{:test-not}, and @code{:key}
arguments, only the rightmost one is retained.  If @code{:from-end}
is true, the leftmost one is retained instead.  If @code{:start} or
@code{:end} is specified, only elements within that subsequence are
examined or removed.
@end defun

@defun delete-duplicates seq @t{&key :test :test-not :key :start :end :from-end}
This function deletes duplicate elements from @var{seq}.  It is
a destructive version of @code{remove-duplicates}.
@end defun

@defun substitute new old seq @t{&key :test :test-not :key :count :start :end :from-end}
This function returns a copy of @var{seq}, with all elements
matching @var{old} replaced with @var{new}.  The @code{:count},
@code{:start}, @code{:end}, and @code{:from-end} arguments may be
used to limit the number of substitutions made.
@end defun

@defun nsubstitute new old seq @t{&key :test :test-not :key :count :start :end :from-end}
This is a destructive version of @code{substitute}; it performs
the substitution using @code{setcar} or @code{aset} rather than
by returning a changed copy of the sequence.
@end defun

@findex substitute-if
@findex substitute-if-not
@findex nsubstitute-if
@findex nsubstitute-if-not
The @code{substitute-if}, @code{substitute-if-not}, @code{nsubstitute-if},
and @code{nsubstitute-if-not} functions are defined similarly.  For
these, a @var{predicate} is given in place of the @var{old} argument.

@node Searching Sequences, Sorting Sequences, Sequence Functions, Sequences
@section Searching Sequences

@noindent
These functions search for elements or subsequences in a sequence.
(See also @code{member*} and @code{assoc*}; @pxref{Lists}.)

@defun find item seq @t{&key :test :test-not :key :start :end :from-end}
This function searches @var{seq} for an element matching @var{item}.
If it finds a match, it returns the matching element.  Otherwise,
it returns @code{nil}.  It returns the leftmost match, unless
@code{:from-end} is true, in which case it returns the rightmost
match.  The @code{:start} and @code{:end} arguments may be used to
limit the range of elements that are searched.
@end defun

@defun position item seq @t{&key :test :test-not :key :start :end :from-end}
This function is like @code{find}, except that it returns the
integer position in the sequence of the matching item rather than
the item itself.  The position is relative to the start of the
sequence as a whole, even if @code{:start} is non-zero.  The function
returns @code{nil} if no matching element was found.
@end defun

@defun count item seq @t{&key :test :test-not :key :start :end}
This function returns the number of elements of @var{seq} which
match @var{item}.  The result is always a nonnegative integer.
@end defun

@findex find-if
@findex find-if-not
@findex position-if
@findex position-if-not
@findex count-if
@findex count-if-not
The @code{find-if}, @code{find-if-not}, @code{position-if},
@code{position-if-not}, @code{count-if}, and @code{count-if-not}
functions are defined similarly.

@defun mismatch seq1 seq2 @t{&key :test :test-not :key :start1 :end1 :start2 :end2 :from-end}
This function compares the specified parts of @var{seq1} and
@var{seq2}.  If they are the same length and the corresponding
elements match (according to @code{:test}, @code{:test-not},
and @code{:key}), the function returns @code{nil}.  If there is
a mismatch, the function returns the index (relative to @var{seq1})
of the first mismatching element.  This will be the leftmost pair of
elements which do not match, or the position at which the shorter of
the two otherwise-matching sequences runs out.

If @code{:from-end} is true, then the elements are compared from right
to left starting at @code{(1- @var{end1})} and @code{(1- @var{end2})}.
If the sequences differ, then one plus the index of the rightmost
difference (relative to @var{seq1}) is returned.

An interesting example is @code{(mismatch str1 str2 :key 'upcase)},
which compares two strings case-insensitively.
@end defun

@defun search seq1 seq2 @t{&key :test :test-not :key :from-end :start1 :end1 :start2 :end2}
This function searches @var{seq2} for a subsequence that matches
@var{seq1} (or part of it specified by @code{:start1} and
@code{:end1}.)  Only matches which fall entirely within the region
defined by @code{:start2} and @code{:end2} will be considered.
The return value is the index of the leftmost element of the
leftmost match, relative to the start of @var{seq2}, or @code{nil}
if no matches were found.  If @code{:from-end} is true, the
function finds the @emph{rightmost} matching subsequence.
@end defun

@node Sorting Sequences, , Searching Sequences, Sequences
@section Sorting Sequences

@defun sort* seq predicate @t{&key :key}
This function sorts @var{seq} into increasing order as determined
by using @var{predicate} to compare pairs of elements.  @var{predicate}
should return true (non-@code{nil}) if and only if its first argument
is less than (not equal to) its second argument.  For example,
@code{<} and @code{string-lessp} are suitable predicate functions
for sorting numbers and strings, respectively; @code{>} would sort
numbers into decreasing rather than increasing order.

This function differs from Emacs' built-in @code{sort} in that it
can operate on any type of sequence, not just lists.  Also, it
accepts a @code{:key} argument which is used to preprocess data
fed to the @var{predicate} function.  For example,

@example
(setq data (sort data 'string-lessp :key 'downcase))
@end example

@noindent
sorts @var{data}, a sequence of strings, into increasing alphabetical
order without regard to case.  A @code{:key} function of @code{car}
would be useful for sorting association lists.

The @code{sort*} function is destructive; it sorts lists by actually
rearranging the @code{cdr} pointers in suitable fashion.
@end defun

@defun stable-sort seq predicate @t{&key :key}
This function sorts @var{seq} @dfn{stably}, meaning two elements
which are equal in terms of @var{predicate} are guaranteed not to
be rearranged out of their original order by the sort.

In practice, @code{sort*} and @code{stable-sort} are equivalent
in Emacs Lisp because the underlying @code{sort} function is
stable by default.  However, this package reserves the right to
use non-stable methods for @code{sort*} in the future.
@end defun

@defun merge type seq1 seq2 predicate @t{&key :key}
This function merges two sequences @var{seq1} and @var{seq2} by
interleaving their elements.  The result sequence, of type @var{type}
(in the sense of @code{concatenate}), has length equal to the sum
of the lengths of the two input sequences.  The sequences may be
modified destructively.  Order of elements within @var{seq1} and
@var{seq2} is preserved in the interleaving; elements of the two
sequences are compared by @var{predicate} (in the sense of
@code{sort}) and the lesser element goes first in the result.
When elements are equal, those from @var{seq1} precede those from
@var{seq2} in the result.  Thus, if @var{seq1} and @var{seq2} are
both sorted according to @var{predicate}, then the result will be
a merged sequence which is (stably) sorted according to
@var{predicate}.
@end defun

@node Lists, Hash Tables, Sequences, Top
@chapter Lists

@noindent
The functions described here operate on lists.

@menu
* List Functions::                `caddr', `first', `last', `list*', etc.
* Substitution of Expressions::   `subst', `sublis', etc.
* Lists as Sets::                 `member*', `adjoin', `union', etc.
* Association Lists::             `assoc*', `rassoc*', `acons', `pairlis'
@end menu

@node List Functions, Substitution of Expressions, Lists, Lists
@section List Functions

@noindent
This section describes a number of simple operations on lists,
i.e., chains of cons cells.

@defun caddr x
This function is equivalent to @code{(car (cdr (cdr @var{x})))}.
Likewise, this package defines all 28 @code{c@var{xxx}r} functions
where @var{xxx} is up to four @samp{a}s and/or @samp{d}s.
All of these functions are @code{setf}-able, and calls to them
are expanded inline by the byte-compiler for maximum efficiency.
@end defun

@defun first x
This function is a synonym for @code{(car @var{x})}.  Likewise,
the functions @code{second}, @code{third}, @dots{}, through
@code{tenth} return the given element of the list @var{x}.
@end defun

@defun rest x
This function is a synonym for @code{(cdr @var{x})}.
@end defun

@defun endp x
Common Lisp defines this function to act like @code{null}, but
signalling an error if @code{x} is neither a @code{nil} nor a
cons cell.  This package simply defines @code{endp} as a synonym
for @code{null}.
@end defun

@defun list-length x
This function returns the length of list @var{x}, exactly like
@code{(length @var{x})}, except that if @var{x} is a circular
list (where the cdr-chain forms a loop rather than terminating
with @code{nil}), this function returns @code{nil}.  (The regular
@code{length} function would get stuck if given a circular list.)
@end defun

@defun last x &optional n
This function returns the last cons, or the @var{n}th-to-last cons,
of the list @var{x}.  If @var{n} is omitted it defaults to 1.
The ``last cons'' means the first cons cell of the list whose
@code{cdr} is not another cons cell.  (For normal lists, the
@code{cdr} of the last cons will be @code{nil}.)  This function
returns @code{nil} if @var{x} is @code{nil} or shorter than
@var{n}.  Note that the last @emph{element} of the list is
@code{(car (last @var{x}))}.
@end defun

@defun butlast x &optional n
This function returns the list @var{x} with the last element,
or the last @var{n} elements, removed.  If @var{n} is greater
than zero it makes a copy of the list so as not to damage the
original list.  In general, @code{(append (butlast @var{x} @var{n})
(last @var{x} @var{n}))} will return a list equal to @var{x}.
@end defun

@defun nbutlast x &optional n
This is a version of @code{butlast} that works by destructively
modifying the @code{cdr} of the appropriate element, rather than
making a copy of the list.
@end defun

@defun list* arg &rest others
This function constructs a list of its arguments.  The final
argument becomes the @code{cdr} of the last cell constructed.
Thus, @code{(list* @var{a} @var{b} @var{c})} is equivalent to
@code{(cons @var{a} (cons @var{b} @var{c}))}, and
@code{(list* @var{a} @var{b} nil)} is equivalent to
@code{(list @var{a} @var{b})}.

(Note that this function really is called @code{list*} in Common
Lisp; it is not a name invented for this package like @code{member*}
or @code{defun*}.)
@end defun

@defun ldiff list sublist
If @var{sublist} is a sublist of @var{list}, i.e., is @code{eq} to
one of the cons cells of @var{list}, then this function returns
a copy of the part of @var{list} up to but not including
@var{sublist}.  For example, @code{(ldiff x (cddr x))} returns
the first two elements of the list @code{x}.  The result is a
copy; the original @var{list} is not modified.  If @var{sublist}
is not a sublist of @var{list}, a copy of the entire @var{list}
is returned.
@end defun

@defun copy-list list
This function returns a copy of the list @var{list}.  It copies
dotted lists like @code{(1 2 . 3)} correctly.
@end defun

@defun copy-tree x &optional vecp
This function returns a copy of the tree of cons cells @var{x}.
Unlike @code{copy-sequence} (and its alias @code{copy-list}),
which copies only along the @code{cdr} direction, this function
copies (recursively) along both the @code{car} and the @code{cdr}
directions.  If @var{x} is not a cons cell, the function simply
returns @var{x} unchanged.  If the optional @var{vecp} argument
is true, this function copies vectors (recursively) as well as
cons cells.
@end defun

@defun tree-equal x y @t{&key :test :test-not :key}
This function compares two trees of cons cells.  If @var{x} and
@var{y} are both cons cells, their @code{car}s and @code{cdr}s are
compared recursively.  If neither @var{x} nor @var{y} is a cons
cell, they are compared by @code{eql}, or according to the
specified test.  The @code{:key} function, if specified, is
applied to the elements of both trees.  @xref{Sequences}.
@end defun

@iftex
@secno=3
@end iftex

@node Substitution of Expressions, Lists as Sets, List Functions, Lists
@section Substitution of Expressions

@noindent
These functions substitute elements throughout a tree of cons
cells.  (@xref{Sequence Functions}, for the @code{substitute}
function, which works on just the top-level elements of a list.)

@defun subst new old tree @t{&key :test :test-not :key}
This function substitutes occurrences of @var{old} with @var{new}
in @var{tree}, a tree of cons cells.  It returns a substituted
tree, which will be a copy except that it may share storage with
the argument @var{tree} in parts where no substitutions occurred.
The original @var{tree} is not modified.  This function recurses
on, and compares against @var{old}, both @code{car}s and @code{cdr}s
of the component cons cells.  If @var{old} is itself a cons cell,
then matching cells in the tree are substituted as usual without
recursively substituting in that cell.  Comparisons with @var{old}
are done according to the specified test (@code{eql} by default).
The @code{:key} function is applied to the elements of the tree
but not to @var{old}.
@end defun

@defun nsubst new old tree @t{&key :test :test-not :key}
This function is like @code{subst}, except that it works by
destructive modification (by @code{setcar} or @code{setcdr})
rather than copying.
@end defun

@findex subst-if
@findex subst-if-not
@findex nsubst-if
@findex nsubst-if-not
The @code{subst-if}, @code{subst-if-not}, @code{nsubst-if}, and
@code{nsubst-if-not} functions are defined similarly.

@defun sublis alist tree @t{&key :test :test-not :key}
This function is like @code{subst}, except that it takes an
association list @var{alist} of @var{old}-@var{new} pairs.
Each element of the tree (after applying the @code{:key}
function, if any), is compared with the @code{car}s of
@var{alist}; if it matches, it is replaced by the corresponding
@code{cdr}.
@end defun

@defun nsublis alist tree @t{&key :test :test-not :key}
This is a destructive version of @code{sublis}.
@end defun

@node Lists as Sets, Association Lists, Substitution of Expressions, Lists
@section Lists as Sets

@noindent
These functions perform operations on lists which represent sets
of elements.

@defun member item list
This MacLisp-compatible function searches @var{list} for an element
which is @code{equal} to @var{item}.  The @code{member} function is
built-in to Emacs 19; this package defines it equivalently in Emacs 18.
See the following function for a Common-Lisp compatible version.
@end defun

@defun member* item list @t{&key :test :test-not :key}
This function searches @var{list} for an element matching @var{item}.
If a match is found, it returns the cons cell whose @code{car} was
the matching element.  Otherwise, it returns @code{nil}.  Elements
are compared by @code{eql} by default; you can use the @code{:test},
@code{:test-not}, and @code{:key} arguments to modify this behavior.
@xref{Sequences}.

Note that this function's name is suffixed by @samp{*} to avoid
the incompatible @code{member} function defined in Emacs 19.
(That function uses @code{equal} for comparisons; it is equivalent
to @code{(member* @var{item} @var{list} :test 'equal)}.)
@end defun

@findex member-if
@findex member-if-not
The @code{member-if} and @code{member-if-not} functions
analogously search for elements which satisfy a given predicate.

@defun tailp sublist list
This function returns @code{t} if @var{sublist} is a sublist of
@var{list}, i.e., if @var{sublist} is @code{eql} to @var{list} or to
any of its @code{cdr}s.
@end defun

@defun adjoin item list @t{&key :test :test-not :key}
This function conses @var{item} onto the front of @var{list},
like @code{(cons @var{item} @var{list})}, but only if @var{item}
is not already present on the list (as determined by @code{member*}).
If a @code{:key} argument is specified, it is applied to
@var{item} as well as to the elements of @var{list} during
the search, on the reasoning that @var{item} is ``about'' to
become part of the list.
@end defun

@defun union list1 list2 @t{&key :test :test-not :key}
This function combines two lists which represent sets of items,
returning a list that represents the union of those two sets.
The result list will contain all items which appear in @var{list1}
or @var{list2}, and no others.  If an item appears in both
@var{list1} and @var{list2} it will be copied only once.  If
an item is duplicated in @var{list1} or @var{list2}, it is
undefined whether or not that duplication will survive in the
result list.  The order of elements in the result list is also
undefined.
@end defun

@defun nunion list1 list2 @t{&key :test :test-not :key}
This is a destructive version of @code{union}; rather than copying,
it tries to reuse the storage of the argument lists if possible.
@end defun

@defun intersection list1 list2 @t{&key :test :test-not :key}
This function computes the intersection of the sets represented
by @var{list1} and @var{list2}.  It returns the list of items
which appear in both @var{list1} and @var{list2}.
@end defun

@defun nintersection list1 list2 @t{&key :test :test-not :key}
This is a destructive version of @code{intersection}.  It
tries to reuse storage of @var{list1} rather than copying.
It does @emph{not} reuse the storage of @var{list2}.
@end defun

@defun set-difference list1 list2 @t{&key :test :test-not :key}
This function computes the ``set difference'' of @var{list1}
and @var{list2}, i.e., the set of elements that appear in
@var{list1} but @emph{not} in @var{list2}.
@end defun

@defun nset-difference list1 list2 @t{&key :test :test-not :key}
This is a destructive @code{set-difference}, which will try
to reuse @var{list1} if possible.
@end defun

@defun set-exclusive-or list1 list2 @t{&key :test :test-not :key}
This function computes the ``set exclusive or'' of @var{list1}
and @var{list2}, i.e., the set of elements that appear in
exactly one of @var{list1} and @var{list2}.
@end defun

@defun nset-exclusive-or list1 list2 @t{&key :test :test-not :key}
This is a destructive @code{set-exclusive-or}, which will try
to reuse @var{list1} and @var{list2} if possible.
@end defun

@defun subsetp list1 list2 @t{&key :test :test-not :key}
This function checks whether @var{list1} represents a subset
of @var{list2}, i.e., whether every element of @var{list1}
also appears in @var{list2}.
@end defun

@node Association Lists, , Lists as Sets, Lists
@section Association Lists

@noindent
An @dfn{association list} is a list representing a mapping from
one set of values to another; any list whose elements are cons
cells is an association list.

@defun assoc* item a-list @t{&key :test :test-not :key}
This function searches the association list @var{a-list} for an
element whose @code{car} matches (in the sense of @code{:test},
@code{:test-not}, and @code{:key}, or by comparison with @code{eql})
a given @var{item}.  It returns the matching element, if any,
otherwise @code{nil}.  It ignores elements of @var{a-list} which
are not cons cells.  (This corresponds to the behavior of
@code{assq} and @code{assoc} in Emacs Lisp; Common Lisp's
@code{assoc} ignores @code{nil}s but considers any other non-cons
elements of @var{a-list} to be an error.)
@end defun

@defun rassoc* item a-list @t{&key :test :test-not :key}
This function searches for an element whose @code{cdr} matches
@var{item}.  If @var{a-list} represents a mapping, this applies
the inverse of the mapping to @var{item}.
@end defun

@defun rassoc item a-list
This function searches like @code{rassoc*} with a @code{:test}
argument of @code{equal}.  It is analogous to Emacs Lisp's
standard @code{assoc} function, which derives from the MacLisp
rather than the Common Lisp tradition.
@end defun

@findex assoc-if
@findex assoc-if-not
@findex rassoc-if
@findex rassoc-if-not
The @code{assoc-if}, @code{assoc-if-not}, @code{rassoc-if},
and @code{rassoc-if-not} functions are defined similarly.

Two simple functions for constructing association lists are:

@defun acons key value alist
This is equivalent to @code{(cons (cons @var{key} @var{value}) @var{alist})}.
@end defun

@defun pairlis keys values &optional alist
This is equivalent to @code{(nconc (mapcar* 'cons @var{keys} @var{values})
@var{alist})}.
@end defun

@node Hash Tables, Structures, Lists, Top
@chapter Hash Tables

@noindent
Hash tables are now implemented directly in the C code and documented in
@ref{Hash Tables,,, lispref, XEmacs Lisp Reference Manual}.

@ignore
A @dfn{hash table} is a data structure that maps ``keys'' onto
``values.''  Keys and values can be arbitrary Lisp data objects.
Hash tables have the property that the time to search for a given
key is roughly constant; simpler data structures like association
lists take time proportional to the number of entries in the list.

@defun make-hash-table @t{&key :test :size}
This function creates and returns a hash-table object whose
function for comparing elements is @code{:test} (@code{eql}
by default), and which is allocated to fit about @code{:size}
elements.  The @code{:size} argument is purely advisory; the
table will stretch automatically if you store more elements in
it.  If @code{:size} is omitted, a reasonable default is used.

Common Lisp allows only @code{eq}, @code{eql}, @code{equal},
and @code{equalp} as legal values for the @code{:test} argument.
In this package, any reasonable predicate function will work,
though if you use something else you should check the details of
the hashing function described below to make sure it is suitable
for your predicate.

Some versions of Emacs (like XEmacs) include a built-in hash
table type; in these versions, @code{make-hash-table} with a test of
@code{eq}, @code{eql}, or @code{equal} will use these built-in hash
tables.  In all other cases, it will return a hash-table object which
takes the form of a list with an identifying ``tag'' symbol at the
front.  All of the hash table functions in this package can operate on
both types of hash table; normally you will never know which type is
being used.

This function accepts the additional Common Lisp keywords
@code{:rehash-size} and @code{:rehash-threshold}, but it ignores
their values.
@end defun

@defun gethash key table &optional default
This function looks up @var{key} in @var{table}.  If @var{key}
exists in the table, in the sense that it matches any of the existing
keys according to the table's test function, then the associated value
is returned.  Otherwise, @var{default} (or @code{nil}) is returned.

To store new data in the hash table, use @code{setf} on a call to
@code{gethash}.  If @var{key} already exists in the table, the
corresponding value is changed to the stored value.  If @var{key}
does not already exist, a new entry is added to the table and the
table is reallocated to a larger size if necessary.  The @var{default}
argument is allowed but ignored in this case.  The situation is
exactly analogous to that of @code{get}; @pxref{Property Lists}.
@end defun

@defun remhash key table
This function removes the entry for @var{key} from @var{table}.
If an entry was removed, it returns @code{t}.  If @var{key} does
not appear in the table, it does nothing and returns @code{nil}.
@end defun

@defun clrhash table
This function removes all the entries from @var{table}, leaving
an empty hash table.
@end defun

@defun maphash function table
This function calls @var{function} for each entry in @var{table}.
It passes two arguments to @var{function}, the key and the value
of the given entry.  The return value of @var{function} is ignored;
@var{maphash} itself returns @code{nil}.  @xref{Loop Facility}, for
an alternate way of iterating over hash tables.
@end defun

@defun hash-table-count table This function returns the number of
entries in @var{table}.  @strong{Warning:} The current implementation of
XEmacs hash-tables does not decrement the stored @code{count}
when @code{remhash} removes an entry.  Therefore, the return value of
this function is not dependable if you have used @code{remhash} on the
table and the table's test is @code{eq}, @code{eql}, or @code{equal}.
A slower, but reliable, way to count the entries is
@code{(loop for x being the hash-keys of @var{table} count t)}.
@end defun

@defun hash-table-p object This function returns @code{t} if
@var{object} is a hash table, @code{nil} otherwise.  It recognizes both
types of hash tables (both XEmacs built-in tables and tables implemented
with special lists.)
@end defun

Sometimes when dealing with hash tables it is useful to know the
exact ``hash function'' that is used.  This package implements
hash tables using Emacs Lisp ``obarrays,'' which are the same
data structure that Emacs Lisp uses to keep track of symbols.
Each hash table includes an embedded obarray.  Key values given
to @code{gethash} are converted by various means into strings,
which are then looked up in the obarray using @code{intern} and
@code{intern-soft}.  The symbol, or ``bucket,'' corresponding to
a given key string includes as its @code{symbol-value} an association
list of all key-value pairs which hash to that string.  Depending
on the test function, it is possible for many entries to hash to
the same bucket.  For example, if the test is @code{eql}, then the
symbol @code{foo} and two separately built strings @code{"foo"} will
create three entries in the same bucket.  Search time is linear
within buckets, so hash tables will be most effective if you arrange
not to store too many things that hash the same.

The following algorithm is used to convert Lisp objects to hash
strings:

@itemize @bullet
@item
Strings are used directly as hash strings.  (However, if the test
function is @code{equalp}, strings are @code{downcase}d first.)

@item
Symbols are hashed according to their @code{symbol-name}.

@item
Integers are hashed into one of 16 buckets depending on their value
modulo 16.  Floating-point numbers are truncated to integers and
hashed modulo 16.

@item
Cons cells are hashed according to their @code{car}s; nonempty vectors
are hashed according to their first element.

@item
All other types of objects hash into a single bucket named @code{"*"}.
@end itemize

@noindent
Thus, for example, searching among many buffer objects in a hash table
will devolve to a (still fairly fast) linear-time search through a
single bucket, whereas searching for different symbols will be very
fast since each symbol will, in general, hash into its own bucket.

The size of the obarray in a hash table is automatically adjusted
as the number of elements increases.

As a special case, @code{make-hash-table} with a @code{:size} argument
of 0 or 1 will create a hash-table object that uses a single association
list rather than an obarray of many lists.  For very small tables this
structure will be more efficient since lookup does not require
converting the key to a string or looking it up in an obarray.
However, such tables are guaranteed to take time proportional to
their size to do a search.
@end ignore

@iftex
@chapno=18
@end iftex

@node Structures, Assertions, Hash Tables, Top
@chapter Structures

@noindent
The Common Lisp @dfn{structure} mechanism provides a general way
to define data types similar to C's @code{struct} types.  A
structure is a Lisp object containing some number of @dfn{slots},
each of which can hold any Lisp data object.  Functions are
provided for accessing and setting the slots, creating or copying
structure objects, and recognizing objects of a particular structure
type.

In true Common Lisp, each structure type is a new type distinct
from all existing Lisp types.  Since the underlying Emacs Lisp
system provides no way to create new distinct types, this package
implements structures as vectors (or lists upon request) with a
special ``tag'' symbol to identify them.

@defspec defstruct name slots@dots{}
The @code{defstruct} form defines a new structure type called
@var{name}, with the specified @var{slots}.  (The @var{slots}
may begin with a string which documents the structure type.)
In the simplest case, @var{name} and each of the @var{slots}
are symbols.  For example,

@example
(defstruct person name age sex)
@end example

@noindent
defines a struct type called @code{person} which contains three
slots.  Given a @code{person} object @var{p}, you can access those
slots by calling @code{(person-name @var{p})}, @code{(person-age @var{p})},
and @code{(person-sex @var{p})}.  You can also change these slots by
using @code{setf} on any of these place forms:

@example
(incf (person-age birthday-boy))
@end example

You can create a new @code{person} by calling @code{make-person},
which takes keyword arguments @code{:name}, @code{:age}, and
@code{:sex} to specify the initial values of these slots in the
new object.  (Omitting any of these arguments leaves the corresponding
slot ``undefined,'' according to the Common Lisp standard; in Emacs
Lisp, such uninitialized slots are filled with @code{nil}.)

Given a @code{person}, @code{(copy-person @var{p})} makes a new
object of the same type whose slots are @code{eq} to those of @var{p}.

Given any Lisp object @var{x}, @code{(person-p @var{x})} returns
true if @var{x} looks like a @code{person}, false otherwise.  (Again,
in Common Lisp this predicate would be exact; in Emacs Lisp the
best it can do is verify that @var{x} is a vector of the correct
length which starts with the correct tag symbol.)

Accessors like @code{person-name} normally check their arguments
(effectively using @code{person-p}) and signal an error if the
argument is the wrong type.  This check is affected by
@code{(optimize (safety @dots{}))} declarations.  Safety level 1,
the default, uses a somewhat optimized check that will detect all
incorrect arguments, but may use an uninformative error message
(e.g., ``expected a vector'' instead of ``expected a @code{person}'').
Safety level 0 omits all checks except as provided by the underlying
@code{aref} call; safety levels 2 and 3 do rigorous checking that will
always print a descriptive error message for incorrect inputs.
@xref{Declarations}.

@example
(setq dave (make-person :name "Dave" :sex 'male))
     @result{} [cl-struct-person "Dave" nil male]
(setq other (copy-person dave))
     @result{} [cl-struct-person "Dave" nil male]
(eq dave other)
     @result{} nil
(eq (person-name dave) (person-name other))
     @result{} t
(person-p dave)
     @result{} t
(person-p [1 2 3 4])
     @result{} nil
(person-p "Bogus")
     @result{} nil
(person-p '[cl-struct-person counterfeit person object])
     @result{} t
@end example

In general, @var{name} is either a name symbol or a list of a name
symbol followed by any number of @dfn{struct options}; each @var{slot}
is either a slot symbol or a list of the form @samp{(@var{slot-name}
@var{default-value} @var{slot-options}@dots{})}.  The @var{default-value}
is a Lisp form which is evaluated any time an instance of the
structure type is created without specifying that slot's value.

Common Lisp defines several slot options, but the only one
implemented in this package is @code{:read-only}.  A non-@code{nil}
value for this option means the slot should not be @code{setf}-able;
the slot's value is determined when the object is created and does
not change afterward.

@example
(defstruct person
  (name nil :read-only t)
  age
  (sex 'unknown))
@end example

Any slot options other than @code{:read-only} are ignored.

For obscure historical reasons, structure options take a different
form than slot options.  A structure option is either a keyword
symbol, or a list beginning with a keyword symbol possibly followed
by arguments.  (By contrast, slot options are key-value pairs not
enclosed in lists.)

@example
(defstruct (person (:constructor create-person)
                   (:type list)
                   :named)
  name age sex)
@end example

The following structure options are recognized.

@table @code
@iftex
@itemmax=0 in
@advance@leftskip-.5@tableindent
@end iftex
@item :conc-name
The argument is a symbol whose print name is used as the prefix for
the names of slot accessor functions.  The default is the name of
the struct type followed by a hyphen.  The option @code{(:conc-name p-)}
would change this prefix to @code{p-}.  Specifying @code{nil} as an
argument means no prefix, so that the slot names themselves are used
to name the accessor functions.

@item :constructor
In the simple case, this option takes one argument which is an
alternate name to use for the constructor function.  The default
is @code{make-@var{name}}, e.g., @code{make-person}.  The above
example changes this to @code{create-person}.  Specifying @code{nil}
as an argument means that no standard constructor should be
generated at all.

In the full form of this option, the constructor name is followed
by an arbitrary argument list.  @xref{Program Structure}, for a
description of the format of Common Lisp argument lists.  All
options, such as @code{&rest} and @code{&key}, are supported.
The argument names should match the slot names; each slot is
initialized from the corresponding argument.  Slots whose names
do not appear in the argument list are initialized based on the
@var{default-value} in their slot descriptor.  Also, @code{&optional}
and @code{&key} arguments which don't specify defaults take their
defaults from the slot descriptor.  It is legal to include arguments
which don't correspond to slot names; these are useful if they are
referred to in the defaults for optional, keyword, or @code{&aux}
arguments which @emph{do} correspond to slots.

You can specify any number of full-format @code{:constructor}
options on a structure.  The default constructor is still generated
as well unless you disable it with a simple-format @code{:constructor}
option.

@example
(defstruct
 (person
  (:constructor nil)   ; no default constructor
  (:constructor new-person (name sex &optional (age 0)))
  (:constructor new-hound (&key (name "Rover")
                                (dog-years 0)
                           &aux (age (* 7 dog-years))
                                (sex 'canine))))
 name age sex)
@end example

The first constructor here takes its arguments positionally rather
than by keyword.  (In official Common Lisp terminology, constructors
that work By Order of Arguments instead of by keyword are called
``BOA constructors.''  No, I'm not making this up.)  For example,
@code{(new-person "Jane" 'female)} generates a person whose slots
are @code{"Jane"}, 0, and @code{female}, respectively.

The second constructor takes two keyword arguments, @code{:name},
which initializes the @code{name} slot and defaults to @code{"Rover"},
and @code{:dog-years}, which does not itself correspond to a slot
but which is used to initialize the @code{age} slot.  The @code{sex}
slot is forced to the symbol @code{canine} with no syntax for
overriding it.

@item :copier
The argument is an alternate name for the copier function for
this type.  The default is @code{copy-@var{name}}.  @code{nil}
means not to generate a copier function.  (In this implementation,
all copier functions are simply synonyms for @code{copy-sequence}.)

@item :predicate
The argument is an alternate name for the predicate which recognizes
objects of this type.  The default is @code{@var{name}-p}.  @code{nil}
means not to generate a predicate function.  (If the @code{:type}
option is used without the @code{:named} option, no predicate is
ever generated.)

In true Common Lisp, @code{typep} is always able to recognize a
structure object even if @code{:predicate} was used.  In this
package, @code{typep} simply looks for a function called
@code{@var{typename}-p}, so it will work for structure types
only if they used the default predicate name.

@item :include
This option implements a very limited form of C++-style inheritance.
The argument is the name of another structure type previously
created with @code{defstruct}.  The effect is to cause the new
structure type to inherit all of the included structure's slots
(plus, of course, any new slots described by this struct's slot
descriptors).  The new structure is considered a ``specialization''
of the included one.  In fact, the predicate and slot accessors
for the included type will also accept objects of the new type.

If there are extra arguments to the @code{:include} option after
the included-structure name, these options are treated as replacement
slot descriptors for slots in the included structure, possibly with
modified default values.  Borrowing an example from Steele:

@example
(defstruct person name (age 0) sex)
     @result{} person
(defstruct (astronaut (:include person (age 45)))
  helmet-size
  (favorite-beverage 'tang))
     @result{} astronaut

(setq joe (make-person :name "Joe"))
     @result{} [cl-struct-person "Joe" 0 nil]
(setq buzz (make-astronaut :name "Buzz"))
     @result{} [cl-struct-astronaut "Buzz" 45 nil nil tang]

(list (person-p joe) (person-p buzz))
     @result{} (t t)
(list (astronaut-p joe) (astronaut-p buzz))
     @result{} (nil t)

(person-name buzz)
     @result{} "Buzz"
(astronaut-name joe)
     @result{} error: "astronaut-name accessing a non-astronaut"
@end example

Thus, if @code{astronaut} is a specialization of @code{person},
then every @code{astronaut} is also a @code{person} (but not the
other way around).  Every @code{astronaut} includes all the slots
of a @code{person}, plus extra slots that are specific to
astronauts.  Operations that work on people (like @code{person-name})
work on astronauts just like other people.

@item :print-function
In full Common Lisp, this option allows you to specify a function
which is called to print an instance of the structure type.  The
Emacs Lisp system offers no hooks into the Lisp printer which would
allow for such a feature, so this package simply ignores
@code{:print-function}.

@item :type
The argument should be one of the symbols @code{vector} or @code{list}.
This tells which underlying Lisp data type should be used to implement
the new structure type.  Vectors are used by default, but
@code{(:type list)} will cause structure objects to be stored as
lists instead.

The vector representation for structure objects has the advantage
that all structure slots can be accessed quickly, although creating
vectors is a bit slower in Emacs Lisp.  Lists are easier to create,
but take a relatively long time accessing the later slots.

@item :named
This option, which takes no arguments, causes a characteristic ``tag''
symbol to be stored at the front of the structure object.  Using
@code{:type} without also using @code{:named} will result in a
structure type stored as plain vectors or lists with no identifying
features.

The default, if you don't specify @code{:type} explicitly, is to
use named vectors.  Therefore, @code{:named} is only useful in
conjunction with @code{:type}.

@example
(defstruct (person1) name age sex)
(defstruct (person2 (:type list) :named) name age sex)
(defstruct (person3 (:type list)) name age sex)

(setq p1 (make-person1))
     @result{} [cl-struct-person1 nil nil nil]
(setq p2 (make-person2))
     @result{} (person2 nil nil nil)
(setq p3 (make-person3))
     @result{} (nil nil nil)

(person1-p p1)
     @result{} t
(person2-p p2)
     @result{} t
(person3-p p3)
     @result{} error: function person3-p undefined
@end example

Since unnamed structures don't have tags, @code{defstruct} is not
able to make a useful predicate for recognizing them.  Also,
accessors like @code{person3-name} will be generated but they
will not be able to do any type checking.  The @code{person3-name}
function, for example, will simply be a synonym for @code{car} in
this case.  By contrast, @code{person2-name} is able to verify
that its argument is indeed a @code{person2} object before
proceeding.

@item :initial-offset
The argument must be a nonnegative integer.  It specifies a
number of slots to be left ``empty'' at the front of the
structure.  If the structure is named, the tag appears at the
specified position in the list or vector; otherwise, the first
slot appears at that position.  Earlier positions are filled
with @code{nil} by the constructors and ignored otherwise.  If
the type @code{:include}s another type, then @code{:initial-offset}
specifies a number of slots to be skipped between the last slot
of the included type and the first new slot.
@end table
@end defspec

Except as noted, the @code{defstruct} facility of this package is
entirely compatible with that of Common Lisp.

@iftex
@chapno=23
@end iftex

@node Assertions, Efficiency Concerns, Structures, Top
@chapter Assertions and Errors

@noindent
This section describes two macros that test @dfn{assertions}, i.e.,
conditions which must be true if the program is operating correctly.
Assertions never add to the behavior of a Lisp program; they simply
make ``sanity checks'' to make sure everything is as it should be.

If the optimization property @code{speed} has been set to 3, and
@code{safety} is less than 3, then the byte-compiler will optimize
away the following assertions.  Because assertions might be optimized
away, it is a bad idea for them to include side-effects.

@defspec assert test-form [show-args string args@dots{}]
This form verifies that @var{test-form} is true (i.e., evaluates to
a non-@code{nil} value).  If so, it returns @code{nil}.  If the test
is not satisfied, @code{assert} signals an error.

A default error message will be supplied which includes @var{test-form}.
You can specify a different error message by including a @var{string}
argument plus optional extra arguments.  Those arguments are simply
passed to @code{error} to signal the error.

If the optional second argument @var{show-args} is @code{t} instead
of @code{nil}, then the error message (with or without @var{string})
will also include all non-constant arguments of the top-level
@var{form}.  For example:

@example
(assert (> x 10) t "x is too small: %d")
@end example

This usage of @var{show-args} is a change to Common Lisp.  In
true Common Lisp, the second argument gives a list of @var{places}
which can be @code{setf}'d by the user before continuing from the
error.
@end defspec

@defspec check-type place type &optional string
This form verifies that @var{place} evaluates to a value of type
@var{type}.  If so, it returns @code{nil}.  If not, @code{check-type}
signals a continuable @code{wrong-type-argument} error.  The default
error message lists the erroneous value along with @var{type} and
@var{place} themselves.  If @var{string} is specified, it is included in
the error message in place of @var{type}.  For example:

@example
(check-type x (integer 1 *) "a positive integer")
@end example

@xref{Type Predicates}, for a description of the type specifiers
that may be used for @var{type}.

Note that as in Common Lisp, the first argument to @code{check-type}
should be a @var{place} suitable for use by @code{setf}, because
@code{check-type} signals a continuable error that allows the user to
modify @var{place}, most simply by returning a value from the debugger.
@end defspec

The following error-related macro is also defined:

@defspec ignore-errors forms@dots{}
This executes @var{forms} exactly like a @code{progn}, except that
errors are ignored during the @var{forms}.  More precisely, if
an error is signalled then @code{ignore-errors} immediately
aborts execution of the @var{forms} and returns @code{nil}.
If the @var{forms} complete successfully, @code{ignore-errors}
returns the result of the last @var{form}.
@end defspec

@node Efficiency Concerns, Common Lisp Compatibility, Assertions, Top
@appendix Efficiency Concerns

@appendixsec Macros

@noindent
Many of the advanced features of this package, such as @code{defun*},
@code{loop}, and @code{setf}, are implemented as Lisp macros.  In
byte-compiled code, these complex notations will be expanded into
equivalent Lisp code which is simple and efficient.  For example,
the forms

@example
(incf i n)
(push x (car p))
@end example

@noindent
are expanded at compile-time to the Lisp forms

@example
(setq i (+ i n))
(setcar p (cons x (car p)))
@end example

@noindent
which are the most efficient ways of doing these respective operations
in Lisp.  Thus, there is no performance penalty for using the more
readable @code{incf} and @code{push} forms in your compiled code.

@emph{Interpreted} code, on the other hand, must expand these macros
every time they are executed.  For this reason it is strongly
recommended that code making heavy use of macros be compiled.
(The features labelled ``Special Form'' instead of ``Function'' in
this manual are macros.)  A loop using @code{incf} a hundred times
will execute considerably faster if compiled, and will also
garbage-collect less because the macro expansion will not have
to be generated, used, and thrown away a hundred times.

You can find out how a macro expands by using the
@code{cl-prettyexpand} function.

@defun cl-prettyexpand form &optional full
This function takes a single Lisp form as an argument and inserts
a nicely formatted copy of it in the current buffer (which must be
in Lisp mode so that indentation works properly).  It also expands
all Lisp macros which appear in the form.  The easiest way to use
this function is to go to the @code{*scratch*} buffer and type, say,

@example
(cl-prettyexpand '(loop for x below 10 collect x))
@end example

@noindent
and type @kbd{C-x C-e} immediately after the closing parenthesis;
the expansion

@example
(block nil
  (let* ((x 0)
         (G1004 nil))
    (while (< x 10)
      (setq G1004 (cons x G1004))
      (setq x (+ x 1)))
    (nreverse G1004)))
@end example

@noindent
will be inserted into the buffer.  (The @code{block} macro is
expanded differently in the interpreter and compiler, so
@code{cl-prettyexpand} just leaves it alone.  The temporary
variable @code{G1004} was created by @code{gensym}.)

If the optional argument @var{full} is true, then @emph{all}
macros are expanded, including @code{block}, @code{eval-when},
and compiler macros.  Expansion is done as if @var{form} were
a top-level form in a file being compiled.  For example,

@example
(cl-prettyexpand '(pushnew 'x list))
     @print{} (setq list (adjoin 'x list))
(cl-prettyexpand '(pushnew 'x list) t)
     @print{} (setq list (if (memq 'x list) list (cons 'x list)))
(cl-prettyexpand '(caddr (member* 'a list)) t)
     @print{} (car (cdr (cdr (memq 'a list))))
@end example

Note that @code{adjoin}, @code{caddr}, and @code{member*} all
have built-in compiler macros to optimize them in common cases.
@end defun

@ifinfo
@example

@end example
@end ifinfo
@appendixsec Error Checking

@noindent
Common Lisp compliance has in general not been sacrificed for the
sake of efficiency.  A few exceptions have been made for cases
where substantial gains were possible at the expense of marginal
incompatibility.  One example is the use of @code{memq} (which is
treated very efficiently by the byte-compiler) to scan for keyword
arguments; this can become confused in rare cases when keyword
symbols are used as both keywords and data values at once.  This
is extremely unlikely to occur in practical code, and the use of
@code{memq} allows functions with keyword arguments to be nearly
as fast as functions that use @code{&optional} arguments.

The Common Lisp standard (as embodied in Steele's book) uses the
phrase ``it is an error if'' to indicate a situation which is not
supposed to arise in complying programs; implementations are strongly
encouraged but not required to signal an error in these situations.
This package sometimes omits such error checking in the interest of
compactness and efficiency.  For example, @code{do} variable
specifiers are supposed to be lists of one, two, or three forms;
extra forms are ignored by this package rather than signalling a
syntax error.  The @code{endp} function is simply a synonym for
@code{null} in this package.  Functions taking keyword arguments
will accept an odd number of arguments, treating the trailing
keyword as if it were followed by the value @code{nil}.

Argument lists (as processed by @code{defun*} and friends)
@emph{are} checked rigorously except for the minor point just
mentioned; in particular, keyword arguments are checked for
validity, and @code{&allow-other-keys} and @code{:allow-other-keys}
are fully implemented.  Keyword validity checking is slightly
time consuming (though not too bad in byte-compiled code);
you can use @code{&allow-other-keys} to omit this check.  Functions
defined in this package such as @code{find} and @code{member*}
do check their keyword arguments for validity.

@ifinfo
@example

@end example
@end ifinfo
@appendixsec Optimizing Compiler

@noindent
The byte-compiler that comes with Emacs 18 normally fails to expand
macros that appear in top-level positions in the file (i.e., outside
of @code{defun}s or other enclosing forms).  This would have
disastrous consequences to programs that used such top-level macros
as @code{defun*}, @code{eval-when}, and @code{defstruct}.  To
work around this problem, the @dfn{CL} package patches the Emacs
18 compiler to expand top-level macros.  This patch will apply to
your own macros, too, if they are used in a top-level context.
The patch will not harm versions of the Emacs 18 compiler which
have already had a similar patch applied, nor will it affect the
optimizing Emacs 19 byte-compiler written by Jamie Zawinski and
Hallvard Furuseth.  The patch is applied to the byte compiler's
code in Emacs' memory, @emph{not} to the @file{bytecomp.elc} file
stored on disk.

The Emacs 19 compiler (for Emacs 18) is available from various
Emacs Lisp archive sites such as @code{archive.cis.ohio-state.edu}.
Its use is highly recommended; many of the Common Lisp macros emit
code which can be improved by optimization.  In particular,
@code{block}s (whether explicit or implicit in constructs like
@code{defun*} and @code{loop}) carry a fair run-time penalty; the
optimizing compiler removes @code{block}s which are not actually
referenced by @code{return} or @code{return-from} inside the block.

@node Common Lisp Compatibility, Old CL Compatibility, Efficiency Concerns, Top
@appendix Common Lisp Compatibility

@noindent
Following is a list of all known incompatibilities between this
package and Common Lisp as documented in Steele (2nd edition).

Certain function names, such as @code{member}, @code{assoc}, and
@code{floor}, were already taken by (incompatible) Emacs Lisp
functions; this package appends @samp{*} to the names of its
Common Lisp versions of these functions.

The word @code{defun*} is required instead of @code{defun} in order
to use extended Common Lisp argument lists in a function.  Likewise,
@code{defmacro*} and @code{function*} are versions of those forms
which understand full-featured argument lists.  The @code{&whole}
keyword does not work in @code{defmacro} argument lists (except
inside recursive argument lists).

In order to allow an efficient implementation, keyword arguments use
a slightly cheesy parser which may be confused if a keyword symbol
is passed as the @emph{value} of another keyword argument.
(Specifically, @code{(memq :@var{keyword} @var{rest-of-arguments})}
is used to scan for @code{:@var{keyword}} among the supplied
keyword arguments.)

The @code{eql} and @code{equal} predicates do not distinguish
between IEEE floating-point plus and minus zero.  The @code{equalp}
predicate has several differences with Common Lisp; @pxref{Predicates}.

The @code{setf} mechanism is entirely compatible, except that
setf-methods return a list of five values rather than five
values directly.  Also, the new ``@code{setf} function'' concept
(typified by @code{(defun (setf foo) @dots{})}) is not implemented.

The @code{do-all-symbols} form is the same as @code{do-symbols}
with no @var{obarray} argument.  In Common Lisp, this form would
iterate over all symbols in all packages.  Since Emacs obarrays
are not a first-class package mechanism, there is no way for
@code{do-all-symbols} to locate any but the default obarray.

The @code{loop} macro is complete except that @code{loop-finish}
and type specifiers are unimplemented.

The multiple-value return facility treats lists as multiple
values, since Emacs Lisp cannot support multiple return values
directly.  The macros will be compatible with Common Lisp if
@code{values} or @code{values-list} is always used to return to
a @code{multiple-value-bind} or other multiple-value receiver;
if @code{values} is used without @code{multiple-value-@dots{}}
or vice-versa the effect will be different from Common Lisp.

Many Common Lisp declarations are ignored, and others match
the Common Lisp standard in concept but not in detail.  For
example, local @code{special} declarations, which are purely
advisory in Emacs Lisp, do not rigorously obey the scoping rules
set down in Steele's book.

The variable @code{*gensym-counter*} starts out with a pseudo-random
value rather than with zero.  This is to cope with the fact that
generated symbols become interned when they are written to and
loaded back from a file.

The @code{defstruct} facility is compatible, except that structures
are of type @code{:type vector :named} by default rather than some
special, distinct type.  Also, the @code{:type} slot option is ignored.

The second argument of @code{check-type} is treated differently.

@node Old CL Compatibility, Porting Common Lisp, Common Lisp Compatibility, Top
@appendix Old CL Compatibility

@noindent
Following is a list of all known incompatibilities between this package
and the older Quiroz @file{cl.el} package.

This package's emulation of multiple return values in functions is
incompatible with that of the older package.  That package attempted
to come as close as possible to true Common Lisp multiple return
values; unfortunately, it could not be 100% reliable and so was prone
to occasional surprises if used freely.  This package uses a simpler
method, namely replacing multiple values with lists of values, which
is more predictable though more noticeably different from Common Lisp.

The @code{defkeyword} form and @code{keywordp} function are not
implemented in this package.

The @code{member}, @code{floor}, @code{ceiling}, @code{truncate},
@code{round}, @code{mod}, and @code{rem} functions are suffixed
by @samp{*} in this package to avoid collision with existing
functions in Emacs 18 or Emacs 19.  The older package simply
redefined these functions, overwriting the built-in meanings and
causing serious portability problems with Emacs 19.  (Some more
recent versions of the Quiroz package changed the names to
@code{cl-member}, etc.; this package defines the latter names as
aliases for @code{member*}, etc.)

Certain functions in the old package which were buggy or inconsistent
with the Common Lisp standard are incompatible with the conforming
versions in this package.  For example, @code{eql} and @code{member}
were synonyms for @code{eq} and @code{memq} in that package, @code{setf}
failed to preserve correct order of evaluation of its arguments, etc.

Finally, unlike the older package, this package is careful to
prefix all of its internal names with @code{cl-}.  Except for a
few functions which are explicitly defined as additional features
(such as @code{floatp-safe} and @code{letf}), this package does not
export any non-@samp{cl-} symbols which are not also part of Common
Lisp.

@ifinfo
@example

@end example
@end ifinfo
@appendixsec The @code{cl-compat} package

@noindent
The @dfn{CL} package includes emulations of some features of the
old @file{cl.el}, in the form of a compatibility package
@code{cl-compat}.  To use it, put @code{(require 'cl-compat)} in
your program.

The old package defined a number of internal routines without
@code{cl-} prefixes or other annotations.  Call to these routines
may have crept into existing Lisp code.  @code{cl-compat}
provides emulations of the following internal routines:
@code{pair-with-newsyms}, @code{zip-lists}, @code{unzip-lists},
@code{reassemble-arglists}, @code{duplicate-symbols-p},
@code{safe-idiv}.

Some @code{setf} forms translated into calls to internal
functions that user code might call directly.  The functions
@code{setnth}, @code{setnthcdr}, and @code{setelt} fall in
this category; they are defined by @code{cl-compat}, but the
best fix is to change to use @code{setf} properly.

The @code{cl-compat} file defines the keyword functions
@code{keywordp}, @code{keyword-of}, and @code{defkeyword},
which are not defined by the new @dfn{CL} package because the
use of keywords as data is discouraged.

The @code{build-klist} mechanism for parsing keyword arguments
is emulated by @code{cl-compat}; the @code{with-keyword-args}
macro is not, however, and in any case it's best to change to
use the more natural keyword argument processing offered by
@code{defun*}.

Multiple return values are treated differently by the two
Common Lisp packages.  The old package's method was more
compatible with true Common Lisp, though it used heuristics
that caused it to report spurious multiple return values in
certain cases.  The @code{cl-compat} package defines a set
of multiple-value macros that are compatible with the old
CL package; again, they are heuristic in nature, but they
are guaranteed to work in any case where the old package's
macros worked.  To avoid name collision with the ``official''
multiple-value facilities, the ones in @code{cl-compat} have
capitalized names:  @code{Values}, @code{Values-list},
@code{Multiple-value-bind}, etc.

The functions @code{cl-floor}, @code{cl-ceiling}, @code{cl-truncate},
and @code{cl-round} are defined by @code{cl-compat} to use the
old-style multiple-value mechanism, just as they did in the old
package.  The newer @code{floor*} and friends return their two
results in a list rather than as multiple values.  Note that
older versions of the old package used the unadorned names
@code{floor}, @code{ceiling}, etc.; @code{cl-compat} cannot use
these names because they conflict with Emacs 19 built-ins.

@node Porting Common Lisp, Function Index, Old CL Compatibility, Top
@appendix Porting Common Lisp

@noindent
This package is meant to be used as an extension to Emacs Lisp,
not as an Emacs implementation of true Common Lisp.  Some of the
remaining differences between Emacs Lisp and Common Lisp make it
difficult to port large Common Lisp applications to Emacs.  For
one, some of the features in this package are not fully compliant
with ANSI or Steele; @pxref{Common Lisp Compatibility}.  But there
are also quite a few features that this package does not provide
at all.  Here are some major omissions that you will want watch out
for when bringing Common Lisp code into Emacs.

@itemize @bullet
@item
Case-insensitivity.  Symbols in Common Lisp are case-insensitive
by default.  Some programs refer to a function or variable as
@code{foo} in one place and @code{Foo} or @code{FOO} in another.
Emacs Lisp will treat these as three distinct symbols.

Some Common Lisp code is written in all upper-case.  While Emacs
is happy to let the program's own functions and variables use
this convention, calls to Lisp builtins like @code{if} and
@code{defun} will have to be changed to lower-case.

@item
Lexical scoping.  In Common Lisp, function arguments and @code{let}
bindings apply only to references physically within their bodies
(or within macro expansions in their bodies).  Emacs Lisp, by
contrast, uses @dfn{dynamic scoping} wherein a binding to a
variable is visible even inside functions called from the body.

Variables in Common Lisp can be made dynamically scoped by
declaring them @code{special} or using @code{defvar}.  In Emacs
Lisp it is as if all variables were declared @code{special}.

Often you can use code that was written for lexical scoping
even in a dynamically scoped Lisp, but not always.  Here is
an example of a Common Lisp code fragment that would fail in
Emacs Lisp:

@example
(defun map-odd-elements (func list)
  (loop for x in list
        for flag = t then (not flag)
        collect (if flag x (funcall func x))))

(defun add-odd-elements (list x)
  (map-odd-elements (function (lambda (a) (+ a x))) list))
@end example

@noindent
In Common Lisp, the two functions' usages of @code{x} are completely
independent.  In Emacs Lisp, the binding to @code{x} made by
@code{add-odd-elements} will have been hidden by the binding
in @code{map-odd-elements} by the time the @code{(+ a x)} function
is called.

(This package avoids such problems in its own mapping functions
by using names like @code{cl-x} instead of @code{x} internally;
as long as you don't use the @code{cl-} prefix for your own
variables no collision can occur.)

@xref{Lexical Bindings}, for a description of the @code{lexical-let}
form which establishes a Common Lisp-style lexical binding, and some
examples of how it differs from Emacs' regular @code{let}.

@item
Common Lisp allows the shorthand @code{#'x} to stand for
@code{(function x)}, just as @code{'x} stands for @code{(quote x)}.
In Common Lisp, one traditionally uses @code{#'} notation when
referring to the name of a function.  In Emacs Lisp, it works
just as well to use a regular quote:

@example
(loop for x in y by #'cddr collect (mapcar #'plusp x)) ; Common Lisp
(loop for x in y by 'cddr collect (mapcar 'plusp x))   ; Emacs Lisp
@end example

When @code{#'} introduces a @code{lambda} form, it is best to
write out @code{(function ...)} longhand in Emacs Lisp.  You can
use a regular quote, but then the byte-compiler won't know that
the @code{lambda} expression is code that can be compiled.

@example
(mapcar #'(lambda (x) (* x 2)) list)            ; Common Lisp
(mapcar (function (lambda (x) (* x 2))) list)   ; Emacs Lisp
@end example

XEmacs supports @code{#'} notation starting with version 19.8.

@item
Reader macros.  Common Lisp includes a second type of macro that
works at the level of individual characters.  For example, Common
Lisp implements the quote notation by a reader macro called @code{'},
whereas Emacs Lisp's parser just treats quote as a special case.
Some Lisp packages use reader macros to create special syntaxes
for themselves, which the Emacs parser is incapable of reading.

@item
Other syntactic features.  Common Lisp provides a number of
notations beginning with @code{#} that the Emacs Lisp parser
won't understand.  For example, @samp{#| ... |#} is an
alternate comment notation, and @samp{#+lucid (foo)} tells
the parser to ignore the @code{(foo)} except in Lucid Common
Lisp.

The number prefixes `#b', `#o', and `#x', however, are supported
by the Emacs Lisp parser to represent numbers in binary, octal,
and hexadecimal notation (or radix), just like in Common Lisp.

@item
Packages.  In Common Lisp, symbols are divided into @dfn{packages}.
Symbols that are Lisp built-ins are typically stored in one package;
symbols that are vendor extensions are put in another, and each
application program would have a package for its own symbols.
Certain symbols are ``exported'' by a package and others are
internal; certain packages ``use'' or import the exported symbols
of other packages.  To access symbols that would not normally be
visible due to this importing and exporting, Common Lisp provides
a syntax like @code{package:symbol} or @code{package::symbol}.

Emacs Lisp has a single namespace for all interned symbols, and
then uses a naming convention of putting a prefix like @code{cl-}
in front of the name.  Some Emacs packages adopt the Common Lisp-like
convention of using @code{cl:} or @code{cl::} as the prefix.
However, the Emacs parser does not understand colons and just
treats them as part of the symbol name.  Thus, while @code{mapcar}
and @code{lisp:mapcar} may refer to the same symbol in Common
Lisp, they are totally distinct in Emacs Lisp.  Common Lisp
programs which refer to a symbol by the full name sometimes
and the short name other times will not port cleanly to Emacs.

Emacs Lisp does have a concept of ``obarrays,'' which are
package-like collections of symbols, but this feature is not
strong enough to be used as a true package mechanism.

@item
Keywords.  The notation @code{:test-not} in Common Lisp really
is a shorthand for @code{keyword:test-not}; keywords are just
symbols in a built-in @code{keyword} package with the special
property that all its symbols are automatically self-evaluating.
Common Lisp programs often use keywords liberally to avoid
having to use quotes.

In Emacs Lisp a keyword is just a symbol whose name begins with
a colon; since the Emacs parser does not treat them specially,
they have to be explicitly made self-evaluating by a statement
like @code{(setq :test-not ':test-not)}.  This package arranges
to execute such a statement whenever @code{defun*} or some
other form sees a keyword being used as an argument.  Common
Lisp code that assumes that a symbol @code{:mumble} will be
self-evaluating even though it was never introduced by a
@code{defun*} will have to be fixed.

@item
The @code{format} function is quite different between Common
Lisp and Emacs Lisp.  It takes an additional ``destination''
argument before the format string.  A destination of @code{nil}
means to format to a string as in Emacs Lisp; a destination
of @code{t} means to write to the terminal (similar to
@code{message} in Emacs).  Also, format control strings are
utterly different; @code{~} is used instead of @code{%} to
introduce format codes, and the set of available codes is
much richer.  There are no notations like @code{\n} for
string literals; instead, @code{format} is used with the
``newline'' format code, @code{~%}.  More advanced formatting
codes provide such features as paragraph filling, case
conversion, and even loops and conditionals.

While it would have been possible to implement most of Common
Lisp @code{format} in this package (under the name @code{format*},
of course), it was not deemed worthwhile.  It would have required
a huge amount of code to implement even a decent subset of
@code{format*}, yet the functionality it would provide over
Emacs Lisp's @code{format} would rarely be useful.

@item
Vector constants use square brackets in Emacs Lisp, but
@code{#(a b c)} notation in Common Lisp.  To further complicate
matters, Emacs 19 introduces its own @code{#(} notation for
something entirely different---strings with properties.

@item
Characters are distinct from integers in Common Lisp.  The
notation for character constants is also different:  @code{#\A}
instead of @code{?A}.  Also, @code{string=} and @code{string-equal}
are synonyms in Emacs Lisp whereas the latter is case-insensitive
in Common Lisp.

@item
Data types.  Some Common Lisp data types do not exist in Emacs
Lisp.  Rational numbers and complex numbers are not present,
nor are large integers (all integers are ``fixnums'').  All
arrays are one-dimensional.  There are no readtables or pathnames;
streams are a set of existing data types rather than a new data
type of their own.  Hash tables, random-states, structures, and
packages (obarrays) are built from Lisp vectors or lists rather
than being distinct types.

@item
The Common Lisp Object System (CLOS) is not implemented,
nor is the Common Lisp Condition System.

@item
Common Lisp features that are completely redundant with Emacs
Lisp features of a different name generally have not been
implemented.  For example, Common Lisp writes @code{defconstant}
where Emacs Lisp uses @code{defconst}.  Similarly, @code{make-list}
takes its arguments in different ways in the two Lisps but does
exactly the same thing, so this package has not bothered to
implement a Common Lisp-style @code{make-list}.

@item
A few more notable Common Lisp features not included in this
package:  @code{compiler-let}, @code{tagbody}, @code{prog},
@code{ldb/dpb}, @code{parse-integer}, @code{cerror}.

@item
Recursion.  While recursion works in Emacs Lisp just like it
does in Common Lisp, various details of the Emacs Lisp system
and compiler make recursion much less efficient than it is in
most Lisps.  Some schools of thought prefer to use recursion
in Lisp over other techniques; they would sum a list of
numbers using something like

@example
(defun sum-list (list)
  (if list
      (+ (car list) (sum-list (cdr list)))
    0))
@end example

@noindent
where a more iteratively-minded programmer might write one of
these forms:

@example
(let ((total 0)) (dolist (x my-list) (incf total x)) total)
(loop for x in my-list sum x)
@end example

While this would be mainly a stylistic choice in most Common Lisps,
in Emacs Lisp you should be aware that the iterative forms are
much faster than recursion.  Also, Lisp programmers will want to
note that the current Emacs Lisp compiler does not optimize tail
recursion.
@end itemize

@node Function Index, Variable Index, Porting Common Lisp, Top
@unnumbered Function Index

@printindex fn

@node Variable Index, , Function Index, Top
@unnumbered Variable Index

@printindex vr

@contents
@bye