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
|
@c -*-texinfo-*-
@c This is part of the GNU Emacs Lisp Reference Manual.
@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998 Free Software Foundation, Inc.
@c See the file elisp.texi for copying conditions.
@setfilename ../info/variables
@node Variables, Functions, Control Structures, Top
@c @chapter Variables
@chapter $BJQ?t(B
@c @cindex variable
@cindex $BJQ?t(B
@c A @dfn{variable} is a name used in a program to stand for a value.
@c Nearly all programming languages have variables of some sort. In the
@c text of a Lisp program, variables are written using the syntax for
@c symbols.
@dfn{$BJQ?t(B}$B!J(Bvariable$B!K$O!"%W%m%0%i%`$K$*$$$FCM$rI=$9$?$a$K;H$&L>A0$G$9!#(B
$B$[$H$s$I$9$Y$F$N%W%m%0%i%`8@8l$K$O!"$"$k<o$NJQ?t$,$"$j$^$9!#(B
Lisp$B%W%m%0%i%`$N%F%-%9%H$G$O!"%7%s%\%k$N9=J8$r;H$C$FJQ?t$r=q$-$^$9!#(B
@c In Lisp, unlike most programming languages, programs are represented
@c primarily as Lisp objects and only secondarily as text. The Lisp
@c objects used for variables are symbols: the symbol name is the variable
@c name, and the variable's value is stored in the value cell of the
@c symbol. The use of a symbol as a variable is independent of its use as
@c a function name. @xref{Symbol Components}.
$B$[$H$s$I$N%W%m%0%i%`8@8l$H0c$C$F!"(BLisp$B$G$O!"(B
$B%W%m%0%i%`$O(BLisp$B%*%V%8%'%/%H$GI=8=$7!"%F%-%9%HI=8=$OI{<!E*$J$b$N$G$9!#(B
$BJQ?t$H$7$F;H$&(BLisp$B%*%V%8%'%/%H$O%7%s%\%k$G$9!#(B
$B%7%s%\%kL>$,JQ?tL>$G$"$j!"JQ?t$NCM$O%7%s%\%k$NCM%;%k$K3JG<$5$l$F$$$^$9!#(B
$BJQ?t$H$7$F$N%7%s%\%k$N;H$$J}$O!"4X?tL>$H$7$F$N;H$$J}$H$OFHN)$7$F$$$^$9!#(B
@xref{Symbol Components}$B!#(B
@c The Lisp objects that constitute a Lisp program determine the textual
@c form of the program---it is simply the read syntax for those Lisp
@c objects. This is why, for example, a variable in a textual Lisp program
@c is written using the read syntax for the symbol that represents the
@c variable.
Lisp$B%W%m%0%i%`$r9=@.$9$k(BLisp$B%*%V%8%'%/%H72$O!"(B
$B%W%m%0%i%`$N%F%-%9%HI=8=$r7hDj$7$^$9!#(B
$B$D$^$j!"(BLisp$B%*%V%8%'%/%H72$KBP$9$kC1$J$kF~NO9=J8$G$9!#(B
$B$3$l$O!"$?$H$($P!"(BLisp$B%W%m%0%i%`$N%F%-%9%HI=8=$G$O!"(B
$BJQ?t$rI=8=$9$k%7%s%\%k$NF~NO9=J8$GJQ?t$r=q$/M}M3$G$9!#(B
@menu
* Global Variables:: Variable values that exist permanently, everywhere.
* Constant Variables:: Certain "variables" have values that never change.
* Local Variables:: Variable values that exist only temporarily.
* Void Variables:: Symbols that lack values.
* Defining Variables:: A definition says a symbol is used as a variable.
* Tips for Defining:: How to avoid bad results from quitting
within the code to initialize a variable.
* Accessing Variables:: Examining values of variables whose names
are known only at run time.
* Setting Variables:: Storing new values in variables.
* Variable Scoping:: How Lisp chooses among local and global values.
* Buffer-Local Variables:: Variable values in effect only in one buffer.
* Frame-Local Variables:: Variable values in effect only in one frame.
* Future Local Variables:: New kinds of local values we might add some day.
@end menu
@node Global Variables
@c @section Global Variables
@section $B%0%m!<%P%kJQ?t(B
@c @cindex global variable
@cindex $B%0%m!<%P%kJQ?t(B
@cindex $BJQ?t!"%0%m!<%P%k(B
@c The simplest way to use a variable is @dfn{globally}. This means that
@c the variable has just one value at a time, and this value is in effect
@c (at least for the moment) throughout the Lisp system. The value remains
@c in effect until you specify a new one. When a new value replaces the
@c old one, no trace of the old value remains in the variable.
$BJQ?t$r;H$&$b$C$H$b4JC1$JJ}K!$O!"(B
@dfn{$B%0%m!<%P%k$K(B}$B!J(Bglobally$B!"Bg6IE*$K!K;H$&$3$H$G$9!#(B
$B$D$^$j!"$I$s$J$H$-$K$bJQ?t$K$O$?$C$?(B1$B$D$NCM$@$1$,$"$j!"(B
$B!J>/$J$/$H$b$3$3$G$O!K(BLisp$B%7%9%F%`A4BN$K$=$NCM$,M-8z$K$J$j$^$9!#(B
$B?7$?$JCM$r@_Dj$9$k$^$G!"$=$NCM$,M-8z$G$"$jB3$1$^$9!#(B
$B?7$?$JCM$G8E$$CM$rCV$-49$($k$H!"JQ?t$K$O8E$$CM$N:/@W$O$J$K$b;D$j$^$;$s!#(B
@c You specify a value for a symbol with @code{setq}. For example,
$B%7%s%\%k$NCM$O(B@code{setq}$B$G;XDj$7$^$9!#(B
$B$?$H$($P!"(B
@example
(setq x '(a b))
@end example
@noindent
@c gives the variable @code{x} the value @code{(a b)}. Note that
@c @code{setq} does not evaluate its first argument, the name of the
@c variable, but it does evaluate the second argument, the new value.
$B$O!"JQ?t(B@code{x}$B$KCM(B@code{(a b)}$B$rM?$($^$9!#(B
@code{setq}$B$O!":G=i$N0z?t!"$D$^$j!"JQ?t$NL>A0$rI>2A$;$:!"(B
$B?7$7$$CM$G$"$kBh(B2$B0z?t$rI>2A$9$k$3$H$KCm0U$7$F$/$@$5$$!#(B
@c Once the variable has a value, you can refer to it by using the symbol
@c by itself as an expression. Thus,
$BJQ?t$K$$$C$?$sCM$rM?$($l$P!"(B
$B<0$H$7$F%7%s%\%k$=$N$b$N$r;H$&$3$H$K$h$j$=$NCM$r;2>H$G$-$^$9!#(B
$B$D$^$j!"$D$.$N$H$*$j$G$9!#(B
@example
@group
x @result{} (a b)
@end group
@end example
@noindent
@c assuming the @code{setq} form shown above has already been executed.
$B$?$@$7!">e$K<($7$?%U%)!<%`(B@code{setq}$B$r<B9T$7$F$"$k$H2>Dj$7$^$9!#(B
@c If you do set the same variable again, the new value replaces the old
@c one:
$BF1$8JQ?t$KCM$r@_Dj$7D>$9$H!"(B
$B?7$7$$CM$G8E$$CM$rCV$-49$($^$9!#(B
@example
@group
x
@result{} (a b)
@end group
@group
(setq x 4)
@result{} 4
@end group
@group
x
@result{} 4
@end group
@end example
@node Constant Variables
@c @section Variables That Never Change
@section $BJQ99IT2DG=$JJQ?t(B
@vindex nil
@vindex t
@kindex setting-constant
@c In Emacs Lisp, certain symbols normally evaluate to themselves. These
@c include @code{nil} and @code{t}, as well as any symbol whose name starts
@c with @samp{:}. These symbols cannot be rebound, nor can their values be
@c changed. Any attempt to set or bind @code{nil} or @code{t} signals a
@c @code{setting-constant} error. The same is true for a symbol whose name
@c starts with @samp{:}, except that you are allowed to set such a symbol to
@c itself.
Emacs Lisp$B$K$O!"DL>o$=$l<+?H$KI>2A$5$l$k$"$k<o$N%7%s%\%k$,$"$j$^$9!#(B
@samp{:}$B$G;O$^$kL>A0$NG$0U$NJQ?t!"$*$h$S!"(B@code{nil}$B$H(B@code{t}$B$G$9!#(B
$B$3$l$i$N%7%s%\%k$r:FB+G{$9$k$3$H$O$G$-$:!"(B
$B$=$l$i$NCM$rJQ99$9$k$3$H$b$G$-$^$;$s!#(B
@code{nil}$B$d(B@code{t}$B$r@_Dj$7$h$&$H$7$?$jB+G{$7$h$&$H$9$k$H!"(B
$B%(%i!<(B@code{setting-constant}$B$rDLCN$7$^$9!#(B
@samp{:}$B$G;O$^$kL>A0$N%7%s%\%k$K4X$7$F$b$=$&$G$9$,!"(B
$B$=$N$h$&$J%7%s%\%k$K$=$l<+?H$r@_Dj$9$k$3$H$O$G$-$^$9!#(B
@example
@group
nil @equiv{} 'nil
@result{} nil
@end group
@group
(setq nil 500)
@error{} Attempt to set constant symbol: nil
@end group
@end example
@defvar keyword-symbols-constant-flag
@tindex keyword-symbols-constant-flag
@c If this variable is @code{nil}, you are allowed to set and bind symbols
@c whose names start with @samp{:} as you wish. This is to make it
@c possible to run old Lisp programs which do that.
$B$3$NJQ?t$,(B@code{nil}$B$G$"$k$H!"(B
@samp{:}$B$G;O$^$kL>A0$NJQ?t$rK>$_$NCM$K@_Dj$7$?$jB+G{$7$?$j$G$-$k!#(B
$B$3$l$O!"$=$N$h$&$J$3$H$r9T$&8E$$(BLisp$B%W%m%0%i%`$N<B9T$r2DG=$K$9$k$?$a$G$"$k!#(B
@end defvar
@node Local Variables
@c @section Local Variables
@section $B%m!<%+%kJQ?t(B
@c @cindex binding local variables
@c @cindex local variables
@c @cindex local binding
@c @cindex global binding
@cindex $B%m!<%+%kJQ?tB+G{(B
@cindex $B%m!<%+%kJQ?t(B
@cindex $BJQ?t!"%m!<%+%k(B
@cindex $B%m!<%+%kB+G{(B
@cindex $B%0%m!<%P%kB+G{(B
@c Global variables have values that last until explicitly superseded
@c with new values. Sometimes it is useful to create variable values that
@c exist temporarily---only until a certain part of the program finishes.
@c These values are called @dfn{local}, and the variables so used are
@c called @dfn{local variables}.
$B%0%m!<%P%kJQ?t$O!"(B
$BL@<(E*$K?7$7$$CM$GCV$-49$($J$$8B$jB8B3$9$kCM$r;}$A$^$9!#(B
$B0l;~E*$K$7$+B8:_$7$J$$JQ?tCM!"(B
$B$D$^$j!"%W%m%0%i%`$N$"$kItJ,$r40N;$9$k$^$G$N$_B8:_$9$kJQ?tCM$r(B
$B:n$l$k$HJXMx$J$3$H$,$"$j$^$9!#(B
$B$3$N$h$&$JCM$r(B@dfn{$B%m!<%+%k(B}$B!J(Blocal$B!"6I=jE*!K$H8F$S!"(B
$B$=$N$h$&$K;H$o$l$kJQ?t$r(B@dfn{$B%m!<%+%kJQ?t(B}$B!J(Blocal variables$B!K$H8F$S$^$9!#(B
@c For example, when a function is called, its argument variables receive
@c new local values that last until the function exits. The @code{let}
@c special form explicitly establishes new local values for specified
@c variables; these last until exit from the @code{let} form.
$B$?$H$($P!"4X?t$r8F$S=P$7$?$H$-!"$=$N0z?tJQ?t$O!"(B
$B4X?t$rH4$1$k$^$GB8B3$9$k?7$?$J%m!<%+%k$JCM$r<u$1<h$j$^$9!#(B
$B%9%Z%7%c%k%U%)!<%`(B@code{let}$B$O!";XDj$7$?JQ?t$N?7$?$J%m!<%+%kCM$r(B
$BL@<(E*$K3NN)$7$^$9!#(B
$B$3$l$i$O%U%)!<%`(B@code{let}$B$rH4$1$k$^$GB8B3$7$^$9!#(B
@c @cindex shadowing of variables
@cindex $BJQ?t$r1#$9(B
@c Establishing a local value saves away the previous value (or lack of
@c one) of the variable. When the life span of the local value is over,
@c the previous value is restored. In the mean time, we say that the
@c previous value is @dfn{shadowed} and @dfn{not visible}. Both global and
@c local values may be shadowed (@pxref{Scope}).
$B%m!<%+%kCM$r3NN)$9$k$H!"(B
$BJQ?t$N0JA0$NCM!J$"$k$$$OCM$,$J$$$3$H!K$rJ]B8$7$^$9!#(B
$B%m!<%+%kCM$NB8B34|4V$,=*N;$9$k$H!"0JA0$NCM$rI|85$7$^$9!#(B
$B$3$N4|4V$O!"0JA0$NCM$r(B@dfn{$B1#$7$F(B}$B!J(Bshadowed$B!K$$$F(B
$B0JA0$NCM$O(B@dfn{$B8+$($^$;$s(B}$B!#(B
$B%0%m!<%P%kCM$G$b%m!<%+%kCM$G$b1#$;$^$9!J(B@pxref{Scope}$B!K!#(B
@c If you set a variable (such as with @code{setq}) while it is local,
@c this replaces the local value; it does not alter the global value, or
@c previous local values, that are shadowed. To model this behavior, we
@c speak of a @dfn{local binding} of the variable as well as a local value.
$BJQ?t$,%m!<%+%k$J$H$-$K!J(B@code{setq}$B$J$I$G!K$=$NJQ?t$r@_Dj$9$k$H!"(B
$B%m!<%+%kCM$rCV$-49$($^$9!#(B
$B1#$5$l$F$$$k%0%m!<%P%kCM$d0JA0$N%m!<%+%kCM$rJQ99$7$^$;$s!#(B
$B$3$N$U$k$^$$$r%b%G%k2=$9$k$?$a$K!"(B
$BJQ?t$N%m!<%+%kCM$K2C$($FJQ?t$N(B@dfn{$B%m!<%+%kB+G{(B}$B!J(Blocal binding$B!K$r(B
$B9M$($^$9!#(B
@c The local binding is a conceptual place that holds a local value.
@c Entry to a function, or a special form such as @code{let}, creates the
@c local binding; exit from the function or from the @code{let} removes the
@c local binding. As long as the local binding lasts, the variable's value
@c is stored within it. Use of @code{setq} or @code{set} while there is a
@c local binding stores a different value into the local binding; it does
@c not create a new binding.
$B%m!<%+%kB+G{$H$O!"%m!<%+%kCM$rJ];}$9$k35G0E*$J>l=j$G$9!#(B
$B4X?t$d(B@code{let}$B$J$I$N%9%Z%7%c%k%U%)!<%`$KF~$k$?$S$K(B
$B%m!<%+%kB+G{$r:n@.$7$^$9!#(B
$B4X?t$d%U%)!<%`(B@code{let}$B$+$iH4$1$k$H%m!<%+%kB+G{$r:o=|$7$^$9!#(B
$B%m!<%+%kB+G{$,B8B3$9$k8B$j!"JQ?t$NCM$O$=$3$KJ];}$5$l$F$$$^$9!#(B
$B%m!<%+%kB+G{$,B8:_$9$k$H$-$K(B@code{setq}$B$d(B@code{set}$B$r;H$&$H!"(B
$B%m!<%+%kB+G{$NCf$KJL$NCM$r3JG<$7$^$9!#(B
$B?7$?$JB+G{$r:n$k$N$G$O$"$j$^$;$s!#(B
@c We also speak of the @dfn{global binding}, which is where
@c (conceptually) the global value is kept.
$B%0%m!<%P%kCM$rJ];}$9$k35G0E*$J>l=j$r(B
@dfn{$B%0%m!<%P%kB+G{(B}$B!J(Bglobal binding$B!K$H$b$$$$$^$9!#(B
@c @cindex current binding
@cindex $B8=:_$NB+G{(B
@c A variable can have more than one local binding at a time (for
@c example, if there are nested @code{let} forms that bind it). In such a
@c case, the most recently created local binding that still exists is the
@c @dfn{current binding} of the variable. (This rule is called
@c @dfn{dynamic scoping}; see @ref{Variable Scoping}.) If there are no
@c local bindings, the variable's global binding is its current binding.
@c We sometimes call the current binding the @dfn{most-local existing
@c binding}, for emphasis. Ordinary evaluation of a symbol always returns
@c the value of its current binding.
$BJQ?t$K$O0lEY$KJ#?t$N%m!<%+%kB+G{$,$"$j$($^$9(B
$B!J$?$H$($P!"F1$8JQ?t$rB+G{$9$kF~$l;R$K$J$C$?%U%)!<%`(B@code{let}$B$,$"$k$H$-!K!#(B
$B$=$N$h$&$J>l9g!"4{B8$N$b$C$H$b:G6a$K:n@.$5$l$?%m!<%+%kB+G{$,!"(B
$BJQ?t$N(B@dfn{$B8=:_$NB+G{(B}$B!J(Bcurrent binding$B!K$G$9!#(B
$B!J$3$N5,B'$r(B@dfn{$BF0E*%9%3!<%W(B}$B!J(Bdynamic scoping$B!K$H8F$S$^$9!#(B
@pxref{Variable Scoping}$B!K(B
$B%m!<%+%kB+G{$,$^$C$?$/$J$1$l$P!"JQ?t$N%0%m!<%P%kB+G{$,8=:_$NB+G{$G$9!#(B
$B8=:_$NB+G{$N$3$H$r6/D4$7$F(B@dfn{$B4{B8$N:G%m!<%+%kB+G{(B}$B$H8F$V$3$H$b$"$j$^$9!#(B
$B%7%s%\%k$NDL>o$NI>2A$G$O!"$=$N8=:_$NB+G{$NCM$rJV$7$^$9!#(B
@c The special forms @code{let} and @code{let*} exist to create
@c local bindings.
$B%9%Z%7%c%k%U%)!<%`(B@code{let}$B$d(B@code{let*}$B$O!"(B
$B%m!<%+%kB+G{$r:n$k$?$a$K$"$j$^$9!#(B
@defspec let (bindings@dots{}) forms@dots{}
@c This special form binds variables according to @var{bindings} and then
@c evaluates all of the @var{forms} in textual order. The @code{let}-form
@c returns the value of the last form in @var{forms}.
$B$3$N%9%Z%7%c%k%U%)!<%`$O!"(B@var{bindings}$B$K=>$C$FJQ?t$rB+G{$7!"(B
@var{forms}$B$N$9$Y$F$r%F%-%9%H>e$N=g$KI>2A$9$k!#(B
@code{let}$B%U%)!<%`$O!"(B@var{forms}$B$N:G8e$N%U%)!<%`$NCM$rJV$9!#(B
@c Each of the @var{bindings} is either @w{(i) a} symbol, in which case
@c that symbol is bound to @code{nil}; or @w{(ii) a} list of the form
@c @code{(@var{symbol} @var{value-form})}, in which case @var{symbol} is
@c bound to the result of evaluating @var{value-form}. If @var{value-form}
@c is omitted, @code{nil} is used.
@var{bindings}$B$N$*$N$*$N$O!"(B@w{(i)}$B%7%s%\%k$G$"$k$+!"(B
@w{(ii)}$B%U%)!<%`(B@code{(@var{symbol} @var{value-form})}$B$N%j%9%H$G$"$k!#(B
$BA0<T$O!"%7%s%\%k$K(B@code{nil}$B$rB+G{$9$k!#(B
$B8e<T$O!"(B@var{symbol}$B$K(B@var{value-form}$B$NI>2A7k2L$rB+G{$9$k!#(B
@var{value-form}$B$r>JN,$9$k$H(B@code{nil}$B$r;H$&!#(B
@c All of the @var{value-form}s in @var{bindings} are evaluated in the
@c order they appear and @emph{before} binding any of the symbols to them.
@c Here is an example of this: @code{Z} is bound to the old value of
@c @code{Y}, which is 2, not the new value of @code{Y}, which is 1.
@var{bindings}$B$N(B@var{value-form}$B72$9$Y$F$r8=$l$k=g$KI>2A$7$F(B@emph{$B$+$i(B}$B!"(B
$B%7%s%\%k$K$=$l$i$NCM$rB+G{$9$k!#(B
$BNc$r$D$.$K<($9!#(B
@code{Z}$B$O!"(B@code{Y}$B$N8E$$CM(B2$B$KB+G{$5$l!"(B@code{Y}$B$N?7$7$$CM(B1$B$G$O$J$$!#(B
@example
@group
(setq Y 2)
@result{} 2
@end group
@group
(let ((Y 1)
(Z Y))
(list Y Z))
@result{} (1 2)
@end group
@end example
@end defspec
@defspec let* (bindings@dots{}) forms@dots{}
@c This special form is like @code{let}, but it binds each variable right
@c after computing its local value, before computing the local value for
@c the next variable. Therefore, an expression in @var{bindings} can
@c reasonably refer to the preceding symbols bound in this @code{let*}
@c form. Compare the following example with the example above for
@c @code{let}.
$B$3$N%9%Z%7%c%k%U%)!<%`$O(B@code{let}$B$K;w$F$$$k$,!"(B
$BJQ?t$N%m!<%+%kCM$r7W;;$7=*$($?D>8e$K$=$NJQ?t$rB+G{$7!"(B
$B$D$.$NJQ?t$N%m!<%+%kCM$N7W;;$K?J$`!#(B
$B$7$?$,$C$F!"(B@var{bindings}$BFb$N<0$G$O!"$3$N(B@code{let*}$B%U%)!<%`Fb$G(B
$B$^$($K$"$k%7%s%\%k$r;2>H$G$-$k!#(B
$B$D$.$NNc$r>e$N(B@code{let}$B$NNc$HHf3S$7$F$[$7$$!#(B
@example
@group
(setq Y 2)
@result{} 2
@end group
@group
(let* ((Y 1)
@c (Z Y)) ; @r{Use the just-established value of @code{Y}.}
(Z Y)) ; @r{$B@_Dj$7=*$($?$P$+$j$N(B@code{Y}$B$NCM$r;H$&(B}
(list Y Z))
@result{} (1 1)
@end group
@end example
@end defspec
@c Here is a complete list of the other facilities that create local
@c bindings:
$B0J2<$K%m!<%+%kB+G{$r:n@.$9$k$=$NB>$N5!G=$N40A4$J0lMw$r$"$2$F$*$-$^$9!#(B
@itemize @bullet
@item
@c Function calls (@pxref{Functions}).
$B4X?t8F$S=P$7!J(B@pxref{Functions}$B!K!#(B
@item
@c Macro calls (@pxref{Macros}).
$B%^%/%m8F$S=P$7!J(B@pxref{Macros}$B!K!#(B
@item
@c @code{condition-case} (@pxref{Errors}).
@code{condition-case}$B!J(B@pxref{Errors}$B!K!#(B
@end itemize
@c Variables can also have buffer-local bindings (@pxref{Buffer-Local
@c Variables}) and frame-local bindings (@pxref{Frame-Local Variables}); a
@c few variables have terminal-local bindings (@pxref{Multiple Displays}).
@c These kinds of bindings work somewhat like ordinary local bindings, but
@c they are localized depending on ``where'' you are in Emacs, rather than
@c localized in time.
$BJQ?t$O!"%P%C%U%!%m!<%+%k$JB+G{!J(B@pxref{Buffer-Local Variables}$B$d(B
$B%U%l!<%`%m!<%+%k$JB+G{!J(B@pxref{Frame-Local Variables}$B!K$r;}$D$3$H$,$G$-$^$9!#(B
$B>/?t$NJQ?t$O!"C<Kv$K%m!<%+%k$JB+G{!J(B@pxref{Multiple Displays}$B!K(B
$B$r;}$D$3$H$b$G$-$^$9!#(B
$B$3$N<o$NB+G{$OIaDL$N%m!<%+%kB+G{$HF1$8$h$&$KF/$-$^$9$,!"(B
$B$3$l$i$O(BEmacs$B$N!X$I$NItJ,!Y$K$$$k$+$K0MB8$7$?%m!<%+%k2=$G$"$j!"(B
$B;~4VE*$J%m!<%+%k2=$G$O$"$j$^$;$s!#(B
@defvar max-specpdl-size
@c @cindex variable limit error
@c @cindex evaluation error
@c @cindex infinite recursion
@cindex $BJQ?t@)8B%(%i!<(B
@cindex $BI>2A%(%i!<(B
@cindex $BL58B:F5"(B
@c This variable defines the limit on the total number of local variable
@c bindings and @code{unwind-protect} cleanups (@pxref{Nonlocal Exits})
@c that are allowed before signaling an error (with data @code{"Variable
@c binding depth exceeds max-specpdl-size"}).
$B$3$NJQ?t$O!"!J(B@code{"Variable binding depth exceeds max-specpdl-size"}$B$r(B
$BH<$C$?!K%(%i!<$rDLCN$9$k$^$G$K5v$5$l$k!"(B
$B%m!<%+%kJQ?tB+G{$H(B
@code{unwind-protect}$B$K$h$k8e;OKv!J(B@pxref{Nonlocal Exits}$B!K$N(B
$BA4BN$N8D?t$N@)8B$rDj5A$9$k!#(B
@c This limit, with the associated error when it is exceeded, is one way
@c that Lisp avoids infinite recursion on an ill-defined function.
@c @code{max-lisp-eval-depth} provides another limit on depth of nesting.
@c @xref{Eval}.
$B$3$N@)8B!"$*$h$S!"$3$l$rD6$($?$H$-$N%(%i!<$O!"(B
$BIT@5$KDj5A$5$l$?4X?t$K$h$C$F(BLisp$B$,L58B$K:F5"$9$k$3$H$rKI;_$9$k(B
1$B$D$NJ}K!$G$"$k!#(B
@c The default value is 600. Entry to the Lisp debugger increases the
@c value, if there is little room left, to make sure the debugger itself
@c has room to execute.
$B%G%U%)%k%HCM$O(B600$B$G$"$k!#(B
Lisp$B%G%P%C%,$KF~$C$?$H$-!"(B
$B@)8B$K6a$$>l9g$K$O%G%P%C%,<+?H$,<B9T$G$-$k$3$H$rJ]>Z$9$k$?$a$KCM$rA}$d$9!#(B
@end defvar
@node Void Variables
@c @section When a Variable is ``Void''
@section $BJQ?t$,!X6u!Y$G$"$k$H$-(B
@kindex void-variable
@c @cindex void variable
@cindex $B6u$NJQ?t(B
@c If you have never given a symbol any value as a global variable, we
@c say that that symbol's global value is @dfn{void}. In other words, the
@c symbol's value cell does not have any Lisp object in it. If you try to
@c evaluate the symbol, you get a @code{void-variable} error rather than
@c a value.
$B%7%s%\%k$K%0%m!<%P%kJQ?t$H$7$F$NCM$r0lEY$bM?$($F$$$J$$$H$-!"(B
$B$=$N%7%s%\%k$N%0%m!<%P%kCM$O(B@dfn{$B6u(B}$B!J(Bvoid$B!K$G$"$k$H$$$$$^$9!#(B
$B$$$$$+$($l$P!"%7%s%\%k$NCM%;%k$K$O$I$s$J(BLisp$B%*%V%8%'%/%H$bF~$C$F$$$^$;$s!#(B
$B%7%s%\%k$rI>2A$7$h$&$H$9$k$H!"CM$G$O$J$/%(%i!<(B@code{void-variable}$B$rF@$^$9!#(B
@c Note that a value of @code{nil} is not the same as void. The symbol
@c @code{nil} is a Lisp object and can be the value of a variable just as any
@c other object can be; but it is @emph{a value}. A void variable does not
@c have any value.
@code{nil}$B$H$$$&CM$O6u$H$O0[$J$k$3$H$KCm0U$7$F$/$@$5$$!#(B
$B%7%s%\%k(B@code{nil}$B$O(BLisp$B%*%V%8%'%/%H$G$"$j!"B>$N%*%V%8%'%/%H$HF1MM$K(B
$BJQ?t$NCM$K$J$j$($^$9!#(B
$B$=$l$O(B@emph{$BCM(B}$B$J$N$G$9!#(B
$B6u$JJQ?t$O$$$+$J$kCM$b;}$?$J$$$N$G$9!#(B
@c After you have given a variable a value, you can make it void once more
@c using @code{makunbound}.
$BJQ?t$KCM$rM?$($?$"$H$G$O!"(B@code{makunbound}$B$r;H$C$F(B
$B:FEY$=$NJQ?t$r6u$K$G$-$^$9!#(B
@defun makunbound symbol
@c This function makes the current variable binding of @var{symbol} void.
@c Subsequent attempts to use this symbol's value as a variable will signal
@c the error @code{void-variable}, unless and until you set it again.
$B$3$N4X?t$O!"(B@var{symbol}$B$N8=:_$NJQ?tB+G{$r6u$K$9$k!#(B
$B$3$l0J9_$KJQ?t$H$7$F$3$N%7%s%\%k$NCM$r;H$*$&$H$9$k$H!"(B
$B:FEY@_Dj$7$F$$$J$$8B$j!"%(%i!<(B@code{void-variable}$B$rDLCN$9$k!#(B
@c @code{makunbound} returns @var{symbol}.
@code{makunbound}$B$O(B@var{symbol}$B$rJV$9!#(B
@example
@group
@c (makunbound 'x) ; @r{Make the global value of @code{x} void.}
(makunbound 'x) ; @r{$BJQ?t(B@code{x}$B$N%0%m!<%P%kCM$r6u$K$9$k(B}
@result{} x
@end group
@group
x
@error{} Symbol's value as variable is void: x
@end group
@end example
@c If @var{symbol} is locally bound, @code{makunbound} affects the most
@c local existing binding. This is the only way a symbol can have a void
@c local binding, since all the constructs that create local bindings
@c create them with values. In this case, the voidness lasts at most as
@c long as the binding does; when the binding is removed due to exit from
@c the construct that made it, the previous local or global binding is
@c reexposed as usual, and the variable is no longer void unless the newly
@c reexposed binding was void all along.
@var{symbol}$B$,%m!<%+%k$KB+G{$5$l$F$$$k$H!"(B
@code{makunbound}$B$O4{B8$N:G%m!<%+%kB+G{$K:nMQ$9$k!#(B
$B%m!<%+%kB+G{$r:n@.$9$k$9$Y$F$N9=J8$OJQ?t$KCM$rM?$($k$?$a!"(B
$B$3$l$O%7%s%\%k$N%m!<%+%kB+G{$r6u$K$9$kM#0l$NJ}K!$G$"$k!#(B
$B$3$N>lLL$G$O!"6u$N>uBV$O!"B+G{$,B8:_$9$k8B$jB8B3$9$k!#(B
$BB+G{$r:n@.$7$?9=B$$+$iH4$1=P$7$FB+G{$,:o=|$5$l$k$H!"(B
$BDL>o$I$*$j$=$l0JA0$N%m!<%+%kB+G{$+%0%m!<%P%kB+G{$,M-8z$K$J$j!"(B
$B$=$NB+G{$,6u$G$J$1$l$PJQ?t$O6u$G$O$J$$!#(B
@smallexample
@group
@c (setq x 1) ; @r{Put a value in the global binding.}
(setq x 1) ; @r{$B%0%m!<%P%kB+G{$KCM$rF~$l$k(B}
@result{} 1
@c (let ((x 2)) ; @r{Locally bind it.}
@c (makunbound 'x) ; @r{Void the local binding.}
(let ((x 2)) ; @r{$B%m!<%+%k$KB+G{$9$k(B}
(makunbound 'x) ; @r{$B%m!<%+%kB+G{$r6u$K$9$k(B}
x)
@error{} Symbol's value as variable is void: x
@end group
@group
@c x ; @r{The global binding is unchanged.}
x ; @r{$B%0%m!<%P%kB+G{$OJQ99$5$l$F$$$J$$(B}
@result{} 1
@c (let ((x 2)) ; @r{Locally bind it.}
@c (let ((x 3)) ; @r{And again.}
@c (makunbound 'x) ; @r{Void the innermost-local binding.}
@c x)) ; @r{And refer: it's void.}
(let ((x 2)) ; @r{$B%m!<%+%k$KB+G{$9$k(B}
(let ((x 3)) ; @r{$B$b$&0lEY(B}
(makunbound 'x) ; @r{$B$b$C$H$bFbB&$N%m!<%+%kB+G{$r6u$K$9$k(B}
x)) ; @r{$B;2>H$9$k$,!"$=$l$O6u(B}
@error{} Symbol's value as variable is void: x
@end group
@group
(let ((x 2))
(let ((x 3))
@c (makunbound 'x)) ; @r{Void inner binding, then remove it.}
@c x) ; @r{Now outer @code{let} binding is visible.}
(makunbound 'x)) ; @r{$BFbB&$NB+G{$r6u$K$7!"$=$l$r:o=|$9$k(B}
x) ; @r{$B30B&$N(B@code{let}$B$NB+G{$,8+$($k(B}
@result{} 2
@end group
@end smallexample
@end defun
@c A variable that has been made void with @code{makunbound} is
@c indistinguishable from one that has never received a value and has
@c always been void.
@code{makunbound}$B$G6u$K$7$?JQ?t$O!"(B
$B0lEY$bCM$r<u$1<h$C$?$3$H$,$J$/!"$=$N$?$a$K6u$G$"$kJQ?t$H6hJL$G$-$^$;$s!#(B
@c You can use the function @code{boundp} to test whether a variable is
@c currently void.
$BJQ?t$,8=:_!"6u$G$"$k$+$I$&$+$O4X?t(B@code{boundp}$B$r;H$C$FD4$Y$i$l$^$9!#(B
@defun boundp variable
@c @code{boundp} returns @code{t} if @var{variable} (a symbol) is not void;
@c more precisely, if its current binding is not void. It returns
@c @code{nil} otherwise.
@code{boundp}$B$O!"!J%7%s%\%k!K(B@var{variable}$B$,6u$G$J$1$l$P!"(B
$B$h$j@53N$K$$$($P!"8=:_$NB+G{$,6u$G$J$1$l$P(B@code{t}$B$rJV$9!#(B
$B$5$b$J$1$l$P(B@code{nil}$B$rJV$9!#(B
@smallexample
@group
@c (boundp 'abracadabra) ; @r{Starts out void.}
(boundp 'abracadabra) ; @r{$B6u$G;O$a$k(B}
@result{} nil
@end group
@group
@c (let ((abracadabra 5)) ; @r{Locally bind it.}
(let ((abracadabra 5)) ; @r{$B%m!<%+%k$KB+G{$9$k(B}
(boundp 'abracadabra))
@result{} t
@end group
@group
@c (boundp 'abracadabra) ; @r{Still globally void.}
(boundp 'abracadabra) ; @r{$B%0%m!<%P%k$K$O$^$@6u$G$"$k(B}
@result{} nil
@end group
@group
@c (setq abracadabra 5) ; @r{Make it globally nonvoid.}
(setq abracadabra 5) ; @r{$B%0%m!<%P%k$K6u$G$J$/$9$k(B}
@result{} 5
@end group
@group
(boundp 'abracadabra)
@result{} t
@end group
@end smallexample
@end defun
@node Defining Variables
@c @section Defining Global Variables
@section $B%0%m!<%P%kJQ?t$rDj5A$9$k(B
@c @cindex variable definition
@cindex $BJQ?tDj5A(B
@c You may announce your intention to use a symbol as a global variable
@c with a @dfn{variable definition}: a special form, either @code{defconst}
@c or @code{defvar}.
$B%9%Z%7%c%k%U%)!<%`(B@code{defconst}$B$d(B@code{defvar}$B$N(B@dfn{$BJQ?tDj5A(B}$B$r;H$C$F!"(B
$B%7%s%\%k$r%0%m!<%P%kJQ?t$H$7$F;H$&0U?^$rI=L@$G$-$^$9!#(B
@c In Emacs Lisp, definitions serve three purposes. First, they inform
@c people who read the code that certain symbols are @emph{intended} to be
@c used a certain way (as variables). Second, they inform the Lisp system
@c of these things, supplying a value and documentation. Third, they
@c provide information to utilities such as @code{etags} and
@c @code{make-docfile}, which create data bases of the functions and
@c variables in a program.
Emacs Lisp$B$G$O!"Dj5A$K$O(B3$B$D$NL\E*$,$"$j$^$9!#(B
$B$^$:!"%3!<%I$rFI$`?M8~$1$K!"FCDj$N%7%s%\%k$r!JJQ?t$H$7$F!KFCDjL\E*$K(B
$B;H$&(B@emph{$B0U?^(B}$B$,$"$k$3$H$rCN$i$;$^$9!#(B
$BBh(B2$B$K!"(BLisp$B%7%9%F%`$KBP$7$F$O!"CM$H@bL@J8;zNs$rDs6!$7$F(B
$B$3$l$i$N$3$H$rEA$($^$9!#(B
$BBh(B3$B$K!"%W%m%0%i%`Fb$N4X?t$dJQ?t$N%G!<%?%Y!<%9$r:n@.$9$k(B
@code{etags}$B$d(B@code{make-docfile}$B$J$I$N%f!<%F%#%j%F%#$K>pJs$rDs6!$7$^$9!#(B
@c The difference between @code{defconst} and @code{defvar} is primarily
@c a matter of intent, serving to inform human readers of whether the value
@c should ever change. Emacs Lisp does not restrict the ways in which a
@c variable can be used based on @code{defconst} or @code{defvar}
@c declarations. However, it does make a difference for initialization:
@c @code{defconst} unconditionally initializes the variable, while
@c @code{defvar} initializes it only if it is void.
@code{defconst}$B$H(B@code{defvar}$B$N0c$$$O!"<g$K9%$_$NLdBj$G$"$j!"(B
$BCM$,JQ99$5$l$k$+$I$&$+$r?M$KEA$($^$9!#(B
Emacs Lisp$B$O!"(B@code{defconst}$B$d(B@code{defvar}$B$N@k8@$K4p$E$$$F(B
$BJQ?t$N;H$$J}$r@)8B$9$k$3$H$O$7$^$;$s!#(B
$B$7$+$7$J$,$i!"=i4|2=$K4X$7$F$O0c$$$,$"$j$^$9!#(B
@code{defconst}$B$OL5>r7o$KJQ?t$r=i4|2=$7$^$9$,!"(B
@code{defvar}$B$OJQ?t$,6u$G$"$k>l9g$K$N$_=i4|2=$7$^$9!#(B
@ignore
One would expect user option variables to be defined with
@code{defconst}, since programs do not change them. Unfortunately, this
has bad results if the definition is in a library that is not preloaded:
@code{defconst} would override any prior value when the library is
loaded. Users would like to be able to set user options in their init
files, and override the default values given in the definitions. For
this reason, user options must be defined with @code{defvar}.
@end ignore
@defspec defvar symbol [value [doc-string]]
@c This special form defines @var{symbol} as a variable and can also
@c initialize and document it. The definition informs a person reading
@c your code that @var{symbol} is used as a variable that might be set or
@c changed. Note that @var{symbol} is not evaluated; the symbol to be
@c defined must appear explicitly in the @code{defvar}.
$B$3$N%9%Z%7%c%k%U%)!<%`$O!"(B@var{symbol}$B$rJQ?t$H$7$FDj5A$7!"(B
$B=i4|CM$d@bL@J8;zNs$r@_Dj$9$k!#(B
$B$3$NDj5A$O!"%3!<%I$rFI$`?M8~$1$K!"(B
$BCM$r@_Dj$7$?$jJQ99$9$kJQ?t$H$7$F(B@var{symbol}$B$r;H$&$3$H$rEA$($k!#(B
@var{symbol}$B$OI>2A$5$l$J$$$3$H$KCm0U!#(B
$BDj5A$9$k%7%s%\%k$O!"(B@code{defvar}$B$KL@<(E*$K8=$l$kI,MW$,$"$k!#(B
@c If @var{symbol} is void and @var{value} is specified, @code{defvar}
@c evaluates it and sets @var{symbol} to the result. But if @var{symbol}
@c already has a value (i.e., it is not void), @var{value} is not even
@c evaluated, and @var{symbol}'s value remains unchanged. If @var{value}
@c is omitted, the value of @var{symbol} is not changed in any case.
@var{symbol}$B$NCM$,6u$G$"$j(B@var{value}$B$r;XDj$7$F$"$k$H!"(B
@code{defvar}$B$O(B@var{value}$B$rI>2A$7!"$=$N7k2L$r(B@var{symbol}$B$K@_Dj$9$k!#(B
$B$7$+$7!"(B@var{symbol}$B$K$9$G$KCM$,$"$l$P!J$D$^$j!"6u$G$J$1$l$P!K!"(B
@var{value}$B$r$^$C$?$/I>2A$;$:!"(B@var{symbol}$B$NCM$bJQ99$7$J$$!#(B
@var{value}$B$r>JN,$7$?>l9g!"(B@var{symbol}$B$NCM$r$$$C$5$$JQ99$7$J$$!#(B
@c If @var{symbol} has a buffer-local binding in the current buffer,
@c @code{defvar} operates on the default value, which is buffer-independent,
@c not the current (buffer-local) binding. It sets the default value if
@c the default value is void. @xref{Buffer-Local Variables}.
@var{symbol}$B$K%+%l%s%H%P%C%U%!$G%P%C%U%!%m!<%+%k$JB+G{$,$"$k>l9g$K$O!"(B
@code{defvar}$B$O%G%U%)%k%HCM$K:nMQ$9$k!#(B
$B$=$l$O!"%P%C%U%!$K$OFHN)$G$"$j!"8=:_$N!J%P%C%U%!%m!<%+%k$J!KB+G{$G$O$J$$!#(B
@code{defvar}$B$O!"%G%U%)%k%HCM$,6u$N>l9g$K%G%U%)%k%HCM$r@_Dj$9$k!#(B
@pxref{Buffer-Local Variables}$B!#(B
@c When you evaluate a top-level @code{defvar} form with @kbd{C-M-x} in
@c Emacs Lisp mode (@code{eval-defun}), a special feature of
@c @code{eval-defun} arranges to set the variable unconditionally, without
@c testing whether its value is void.
emacs-lisp$B%b!<%I$K$*$$$F(B@kbd{C-M-x}$B!J(B@code{eval-defun}$B!K$G%H%C%W%l%Y%k$N(B
$B%U%)!<%`(B@code{defvar}$B$rI>2A$9$k$H!"(B
@code{eval-defun}$B$NFCJL$J5!G=$K$h$j!"(B
$BJQ?t$NCM$,6u$+$I$&$+$rD4$Y$:$KL5>r7o$KJQ?t$K@_Dj$9$k!#(B
@c If the @var{doc-string} argument appears, it specifies the documentation
@c for the variable. (This opportunity to specify documentation is one of
@c the main benefits of defining the variable.) The documentation is
@c stored in the symbol's @code{variable-documentation} property. The
@c Emacs help functions (@pxref{Documentation}) look for this property.
@var{doc-string}$B$,$"$l$P!"$=$l$OJQ?t$N@bL@J8$r;XDj$9$k!#(B
$B!J@bL@J8$r;XDj$G$-$k$N$O!"JQ?tDj5A$N<g$JMxE@$N(B1$B$D$G$"$k!#!K(B
$B@bL@J8$O%7%s%\%k$NB0@-(B@code{variable-documentation}$B$K3JG<$9$k!#(B
Emacs$B$N%X%k%W4X?t!J(B@pxref{Documentation}$B!K$O!"$3$NB0@-$rD4$Y$k!#(B
@c If the first character of @var{doc-string} is @samp{*}, it means that
@c this variable is considered a user option. This lets users set the
@c variable conveniently using the commands @code{set-variable} and
@c @code{edit-options}. However, it is better to use @code{defcustom}
@c instead of @code{defvar} for user option variables, so you can specify
@c customization information. @xref{Customization}.
@var{doc-string}$B$N:G=i$NJ8;z$,(B@samp{*}$B$G$"$k$H!"(B
$B$3$NJQ?t$r%f!<%6!<%*%W%7%g%s$H9M$($k$3$H$r0UL#$9$k!#(B
$B$3$l$K$h$j!"%f!<%6!<$O%3%^%s%I(B@code{set-variable}$B$d(B@code{edit-options}$B$r(B
$B;H$C$F4JC1$KJQ?t$r@_Dj$G$-$k!#(B
$B$7$+$7$J$,$i!"%f!<%6!<%*%W%7%g%s$NJQ?t$K$O!"(B
@code{defvar}$B$G$O$J$/(B@code{defcustom}$B$r;H$C$?$[$&$,$h$/!"(B
$B$=$&$9$l$P%+%9%?%^%$%:>pJs$r;XDj$G$-$k!#(B
@pxref{Customization}$B!#(B
@c Here are some examples. This form defines @code{foo} but does not
@c initialize it:
$B$$$/$D$+Nc$r$"$2$k!#(B
$B$D$.$N%U%)!<%`$O(B@code{foo}$B$rDj5A$9$k$,=i4|2=$O$7$J$$!#(B
@example
@group
(defvar foo)
@result{} foo
@end group
@end example
@c This example initializes the value of @code{bar} to @code{23}, and gives
@c it a documentation string:
$B$D$.$NNc$O!"(B@code{bar}$B$NCM$r(B@code{23}$B$K=i4|2=$7!"@bL@J8;zNs$rM?$($k!#(B
@example
@group
(defvar bar 23
"The normal weight of a bar.")
@result{} bar
@end group
@end example
@c The following form changes the documentation string for @code{bar},
@c making it a user option, but does not change the value, since @code{bar}
@c already has a value. (The addition @code{(1+ nil)} would get an error
@c if it were evaluated, but since it is not evaluated, there is no error.)
$B$D$.$NNc$O!"(B@code{bar}$B$N@bL@J8;zNs$rJQ99$7!"(B
$B$3$NJQ?t$r%f!<%6!<%*%W%7%g%s$K$9$k!#(B
$B$7$+$7!"(B@code{bar}$B$K$O$9$G$KCM$,@_Dj$7$F$"$k$N$G!"(B
$B$=$NCM$OJQ99$7$J$$!#(B
$B!J$5$i$K(B@code{(1+ nil)}$B$OI>2A$9$k$H%(%i!<$K$J$k$,!"(B
$BI>2A$5$l$J$$$N$G%(%i!<$O$J$$!#!K(B
@example
@group
(defvar bar (1+ nil)
"*The normal weight of a bar.")
@result{} bar
@end group
@group
bar
@result{} 23
@end group
@end example
@c Here is an equivalent expression for the @code{defvar} special form:
$B$D$.$NNc$O!"%9%Z%7%c%k%U%)!<%`(B@code{defvar}$B$KEy2A$J<0$G$"$k!#(B
@example
@group
(defvar @var{symbol} @var{value} @var{doc-string})
@equiv{}
(progn
(if (not (boundp '@var{symbol}))
(setq @var{symbol} @var{value}))
(if '@var{doc-string}
(put '@var{symbol} 'variable-documentation '@var{doc-string}))
'@var{symbol})
@end group
@end example
@c The @code{defvar} form returns @var{symbol}, but it is normally used
@c at top level in a file where its value does not matter.
$B%U%)!<%`(B@code{defvar}$B$O(B@var{symbol}$B$rJV$9$,!"(B
$BDL>o$3$N%U%)!<%`$O%U%!%$%k$N%H%C%W%l%Y%k$G;H$o$l!"$=$3$G$OCM$O4X78$J$$!#(B
@end defspec
@defspec defconst symbol [value [doc-string]]
@c This special form defines @var{symbol} as a value and initializes it.
@c It informs a person reading your code that @var{symbol} has a standard
@c global value, established here, that should not be changed by the user
@c or by other programs. Note that @var{symbol} is not evaluated; the
@c symbol to be defined must appear explicitly in the @code{defconst}.
$B$3$N%9%Z%7%c%k%U%)!<%`$O!"(B@var{symbol}$B$rJQ?t$H$7$FDj5A$7=i4|2=$9$k!#(B
$B$3$NDj5A$O!"%3!<%I$rFI$`?M8~$1$K!"(B
@var{symbol}$B$O$3$l0J9_I8=`$N%0%m!<%P%kCM$r;}$A!"(B
$B%f!<%6!<$dB>$N%W%m%0%i%`$,JQ99$9$Y$-$G$J$$$3$H$rEA$($k!#(B
@var{symbol}$B$OI>2A$5$l$J$$$3$H$KCm0U!#(B
$BDj5A$9$k%7%s%\%k$O!"(B@code{defconst}$B$KL@<(E*$K8=$l$kI,MW$,$"$k!#(B
@c @code{defconst} always evaluates @var{value}, and sets the value of
@c @var{symbol} to the result if @var{value} is given. If @var{symbol}
@c does have a buffer-local binding in the current buffer, @code{defconst}
@c sets the default value, not the buffer-local value. (But you should not
@c be making buffer-local bindings for a symbol that is defined with
@c @code{defconst}.)
@code{defconst}$B$O!"(B@var{value}$B$,$"$l$P$D$M$K(B@var{value}$B$rI>2A$7!"(B
$B$=$N7k2L$r(B@var{symbol}$B$K@_Dj$9$k!#(B
@var{symbol}$B$K%+%l%s%H%P%C%U%!$N%P%C%U%!%m!<%+%k$JB+G{$,$"$k>l9g$K$O!"(B
@code{defconst}$B$O%G%U%)%k%HCM$r@_Dj$7!"(B
$B%P%C%U%!%m!<%+%k$JCM$K$G$O$J$$!#(B
$B!J$7$+$7!"(B@code{defconst}$B$GDj5A$9$k%7%s%\%k$K$O!"(B
$B%P%C%U%!%m!<%+%k$JB+G{$r:n$k$Y$-$G$O$J$$!#!K(B
@c Here, @code{pi} is a constant that presumably ought not to be changed
@c by anyone (attempts by the Indiana State Legislature notwithstanding).
@c As the second form illustrates, however, this is only advisory.
$B$D$.$NNc$G$O!"(B@code{pi}$B$O!"!J%$%s%G%#%"%J=#N)K!I\$O$$$&$K$*$h$P$:!K(B
$B$@$l$bJQ99$9$Y$-$G$O$J$$$H9M$($i$l$kDj?t$G$"$k!#(B
$B$7$+$7!"(B2$BHVL\$N%U%)!<%`$+$i$o$+$k$h$&$K!"$3$l$OC1$K=u8@$G$7$+$J$$!#(B
@example
@group
(defconst pi 3.1415 "Pi to five places.")
@result{} pi
@end group
@group
(setq pi 3)
@result{} pi
@end group
@group
pi
@result{} 3
@end group
@end example
@end defspec
@defun user-variable-p variable
@c @cindex user option
@cindex $B%f!<%6!<%*%W%7%g%s(B
@c This function returns @code{t} if @var{variable} is a user option---a
@c variable intended to be set by the user for customization---and
@c @code{nil} otherwise. (Variables other than user options exist for the
@c internal purposes of Lisp programs, and users need not know about them.)
$B$3$N4X?t$O!"(B@var{variable}$B$,%f!<%6!<%*%W%7%g%s!"$D$^$j!"(B
$B%+%9%?%^%$%:$N$?$a$K%f!<%6!<$,@_Dj$9$k$3$H$r0U?^$7$?JQ?t$G$"$k$H!"(B
@code{t}$B$rJV$7!"$5$b$J$1$l$P(B@code{nil}$B$rJV$9!#(B
$B!J%f!<%6!<%*%W%7%g%s8~$10J30$NJQ?t$O!"(BLisp$B%W%m%0%i%`$NFbItL\E*MQ$K$"$j!"(B
$B$=$l$i$K$D$$$F%f!<%6!<$,CN$kI,MW$O$J$$!#!K(B
@c User option variables are distinguished from other variables by the
@c first character of the @code{variable-documentation} property. If the
@c property exists and is a string, and its first character is @samp{*},
@c then the variable is a user option.
$B%f!<%6!<%*%W%7%g%sJQ?t$O!"(B
$BB0@-(B@code{variable-documentation}$B$N:G=i$NJ8;z$GB>$NJQ?t$H6hJL$5$l$k!#(B
$B$=$NB0@-$,B8:_$7$FJ8;zNs$G$"$j!":G=i$NJ8;z$,(B@samp{*}$B$G$"$l$P!"(B
$B$=$NJQ?t$O%f!<%6!<%*%W%7%g%s$G$"$k!#(B
@end defun
@kindex variable-interactive
@c If a user option variable has a @code{variable-interactive} property,
@c the @code{set-variable} command uses that value to control reading the
@c new value for the variable. The property's value is used as if it were
@c to @code{interactive} (@pxref{Using Interactive}). However, this feature
@c is largely obsoleted by @code{defcustom} (@pxref{Customization}).
$B%f!<%6!<%*%W%7%g%sJQ?t$KB0@-(B@code{variable-interactive}$B$,$"$k$H!"(B
$B%3%^%s%I(B@code{set-variable}$B$O$=$NB0@-CM$r;H$C$F!"(B
$BJQ?t$N?7$7$$CM$NFI$_<h$j$r@)8f$7$^$9!#(B
$B$3$NB0@-CM$O!"(B@code{interactive}$B$N0z?t!J(B@pxref{Using Interactive}$B!K(B
$B$N$h$&$K;H$o$l$^$9!#(B
$B$7$+$7$J$,$i!"$3$N5!G=$O(B@code{defcustom}$B!J(B@pxref{Customization}$B!K$K$h$j(B
$B$[$H$s$IGQ$l$F$$$^$9!#(B
@c @strong{Warning:} If the @code{defconst} and @code{defvar} special
@c forms are used while the variable has a local binding, they set the
@c local binding's value; the global binding is not changed. This is not
@c what we really want. To prevent it, use these special forms at top
@c level in a file, where normally no local binding is in effect, and make
@c sure to load the file before making a local binding for the variable.
@strong{$B7Y9p!'(B}@code{ }
$BJQ?t$K%m!<%+%kB+G{$,$"$k$H$-$K(B
$B%9%Z%7%c%k%U%)!<%`(B@code{defconst}$B$d(B@code{defvar}$B$r;H$&$H!"(B
$B%m!<%+%kB+G{$NCM$rJQ99$7!"%0%m!<%P%kB+G{$OJQ99$7$J$$!#(B
$B$3$l$OK>$`8z2L$G$O$J$$!#(B
$B$3$l$rKI$0$K$O!"$3$l$i$N%9%Z%7%c%k%U%)!<%`$O%U%!%$%k$N%H%C%W%l%Y%k$G;H$&!#(B
$B$=$&$9$l$P!"IaDL$OM-8z$J%m!<%+%kB+G{$O$J$$!#(B
$B$5$i$K!"JQ?t$N%m!<%+%kB+G{$r:n$k$^$($K!"(B
$B3N<B$K%U%!%$%k$r%m!<%I$7$F$*$/!#(B
@node Tips for Defining
@c @section Tips for Defining Variables Robustly
@section $BJQ?t$r7xO4$KDj5A$9$k$?$a$N%R%s%H(B
@c When defining and initializing a variable that holds a complicated
@c value (such as a keymap with bindings in it), it's best to put the
@c entire computation of the value into the @code{defvar}, like this:
$B!JFbIt$KB+G{$r4^$`$h$&$J%-!<%^%C%W$J$I$N!KJ#;($JCM$rJ];}$9$kJQ?t$r(B
$BDj5A$7=i4|2=$9$k$H$-$K$O!"$D$.$N$h$&$K!"(B
$BCM$N7W;;A4BN$r(B@code{defvar}$B$NFbIt$KF~$l$F$*$/$N$,:GNI$G$9!#(B
@example
(defvar my-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "\C-c\C-a" 'my-command)
@dots{}
map)
@var{docstring})
@end example
@noindent
@c This method has several benefits. First, if the user quits while
@c loading the file, the variable is either still uninitialized or
@c initialized properly, never in-between. If it is still uninitialized,
@c reloading the file will initialize it properly. Second, reloading the
@c file once the variable is initialized will not alter it; that is
@c important if the user has run hooks to alter part of the contents (such
@c as, to rebind keys). Third, evaluating the @code{defvar} form with
@c @kbd{C-M-x} @emph{will} reinitialize the map completely.
$B$3$NJ}K!$K$O!"$$$/$D$+$NMxE@$,$"$j$^$9!#(B
$B$^$:!"%U%!%$%k$N%m!<%ICf$K%f!<%6!<$,CfCG$7$?>l9g!"(B
$BJQ?t$O=i4|2=$5$l$J$$$+@5$7$/=i4|2=$5$l$k$+$N$$$:$l$+$G$"$j!"(B
$B$=$NCf4V>uBV$H$$$&$3$H$O$"$j$^$;$s!#(B
$BBh(B2$B$K!"JQ?t$r$9$G$K=i4|2=$7$?$"$H$K%U%!%$%k$r%m!<%I$7D>$7$F$b!"(B
$BJQ?t$rJQ99$7$^$;$s!#(B
$B!J%-!<$r%P%$%s%I$7D>$9$J$I$N!KFbMF$N0lIt$rJQ99$9$k$?$a$K(B
$B%f!<%6!<$,%U%C%/$r<B9T$7$?>l9g$J$I$K$O!"$3$l$O=EMW$G$9!#(B
$BBh(B3$B$K!"(B@kbd{C-M-x}$B$G%U%)!<%`(B@code{defvar}$B$rI>2A$9$k$H!"(B
$B%^%C%W$r40A4$K=i4|2=$7(B@emph{$BD>$;$^$9(B}$B!#(B
@c Putting so much code in the @code{defvar} form has one disadvantage:
@c it puts the documentation string far away from the line which names the
@c variable. Here's a safe way to avoid that:
$B%U%)!<%`(B@code{defvar}$B$NFbB&$KB?$/$N%3!<%I$rCV$/$3$H$K$O!"7gE@$,(B1$B$D$"$j$^$9!#(B
$BJQ?t$NL>A0$r;XDj$7$?9T$+$i@bL@J8;zNs$,N%$l$9$.$F$7$^$&$3$H$G$9!#(B
$B$D$.$N$h$&$K$7$F$3$l$r0BA4$KKI$2$^$9!#(B
@example
(defvar my-mode-map nil
@var{docstring})
(if my-mode-map
nil
(let ((map (make-sparse-keymap)))
(define-key my-mode-map "\C-c\C-a" 'my-command)
@dots{}
(setq my-mode-map map)))
@end example
@noindent
@c This has all the same advantages as putting the initialization inside
@c the @code{defvar}, except that you must type @kbd{C-M-x} twice, once on
@c each form, if you do want to reinitialize the variable.
$B$3$l$K$O!"(B@code{defvar}$B$NFbB&$K=i4|2=$rF~$l$?$H$-$HF1$8MxE@$,$"$j$^$9$,!"(B
$BJQ?t$r:F=i4|2=$9$k$K$O!"3F%U%)!<%`$=$l$>$l$K$D$$$F(B
@kbd{C-M-x}$B$rBG$DI,MW$,$"$j$^$9!#(B
@c But be careful not to write the code like this:
$B$7$+$7!"$D$.$N$h$&$J%3!<%I$O=q$+$J$$$G$/$@$5$$!#(B
@example
(defvar my-mode-map nil
@var{docstring})
(if my-mode-map
nil
(setq my-mode-map (make-sparse-keymap))
(define-key my-mode-map "\C-c\C-a" 'my-command)
@dots{})
@end example
@noindent
@c This code sets the variable, then alters it, but it does so in more than
@c one step. If the user quits just after the @code{setq}, that leaves the
@c variable neither correctly initialized nor void nor @code{nil}. Once
@c that happens, reloading the file will not initialize the variable; it
@c will remain incomplete.
$B$3$N%3!<%I$G$O!"JQ?t$r@_Dj$7$F$+$iJQ99$7$^$9$,!"(B
$B$=$l$rJ#?t$N<j=g$G9T$$$^$9!#(B
@code{setq}$B$ND>8e$K%f!<%6!<$,CfCG$9$k$H!"(B
$BJQ?t$O@5$7$/=i4|2=$5$l$F$*$i$:!"6u$G$b(B@code{nil}$B$G$b$"$j$^$;$s!#(B
$B$3$&$J$C$?$H$-$K%U%!%$%k$r:F%m!<%I$7$F$bJQ?t$r=i4|2=$G$-$^$;$s!#(B
$BJQ?t$OIT40A4$J>uBV$N$^$^$G$9!#(B
@node Accessing Variables
@c @section Accessing Variable Values
@section $BJQ?tCM$N;2>H(B
@c The usual way to reference a variable is to write the symbol which
@c names it (@pxref{Symbol Forms}). This requires you to specify the
@c variable name when you write the program. Usually that is exactly what
@c you want to do. Occasionally you need to choose at run time which
@c variable to reference; then you can use @code{symbol-value}.
$BJQ?t$r;2>H$9$kIaDL$NJ}K!$O!"(B
$BJQ?t$r;XL>$9$k%7%s%\%k$r=q$/$3$H$G$9!J(B@pxref{Symbol Forms}$B!K!#(B
$B$3$l$K$O!"%W%m%0%i%`$r=q$/$H$-$KJQ?tL>$r;XDj$9$kI,MW$,$"$j$^$9!#(B
$BFI<T$O!"IaDL$3$N$h$&$K$9$k$G$7$g$&!#(B
$B>l9g$K$h$C$F$O!"<B9T;~$K$I$NJQ?t$r;2>H$9$k$+A*$VI,MW$,$"$j!"(B
$B$=$N$H$-$K$O(B@code{symbol-value}$B$r;H$$$^$9!#(B
@defun symbol-value symbol
@c This function returns the value of @var{symbol}. This is the value in
@c the innermost local binding of the symbol, or its global value if it
@c has no local bindings.
$B$3$N4X?t$O(B@var{symbol}$B$NCM$rJV$9!#(B
$B$3$l$O!"%7%s%\%k$N$b$C$H$bFbB&$N%m!<%+%kB+G{$NCM!"$"$k$$$O!"(B
$B%m!<%+%kB+G{$,$J$1$l$P%0%m!<%P%kCM$G$"$k!#(B
@example
@group
(setq abracadabra 5)
@result{} 5
@end group
@group
(setq foo 9)
@result{} 9
@end group
@group
@c ;; @r{Here the symbol @code{abracadabra}}
@c ;; @r{is the symbol whose value is examined.}
;; @r{$B$3$3$G!"(B@code{abracadabra}$B$O!"(B}
;; @r{$B$=$NCM$rD4$Y$k%7%s%\%k(B}
(let ((abracadabra 'foo))
(symbol-value 'abracadabra))
@result{} foo
@end group
@group
@c ;; @r{Here the value of @code{abracadabra},}
@c ;; @r{which is @code{foo},}
@c ;; @r{is the symbol whose value is examined.}
;; @r{$B$3$3$G!"(B@code{abracadabra}$B$NCM!"(B}
;; @r{$B$D$^$j(B@code{foo}$B$,!"(B}
;; @r{$B$=$NCM$rD4$Y$k%7%s%\%k(B}
(let ((abracadabra 'foo))
(symbol-value abracadabra))
@result{} 9
@end group
@group
(symbol-value 'abracadabra)
@result{} 5
@end group
@end example
@c A @code{void-variable} error is signaled if the current binding of
@c @var{symbol} is void.
@var{symbol}$B$N8=:_$NB+G{$,6u$G$"$k$H!"(B
$B%(%i!<(B@code{void-variable}$B$rDLCN$9$k!#(B
@end defun
@node Setting Variables
@c @section How to Alter a Variable Value
@section $BJQ?tCM$NJQ99(B
@c The usual way to change the value of a variable is with the special
@c form @code{setq}. When you need to compute the choice of variable at
@c run time, use the function @code{set}.
$BJQ?t$NCM$rJQ99$9$kIaDL$NJ}K!$O!"%9%Z%7%c%k%U%)!<%`(B@code{setq}$B$r;H$&$3$H$G$9!#(B
$B<B9T;~$KA*Br$9$kJQ?t$r7W;;$9$kI,MW$,$"$k$H$-$K$O!"(B
$B4X?t(B@code{set}$B$r;H$$$^$9!#(B
@defspec setq [symbol form]@dots{}
@c This special form is the most common method of changing a variable's
@c value. Each @var{symbol} is given a new value, which is the result of
@c evaluating the corresponding @var{form}. The most-local existing
@c binding of the symbol is changed.
$B$3$N%9%Z%7%c%k%U%)!<%`$O!"JQ?t$NCM$rJQ99$9$k$b$C$H$b0lHLE*$JJ}K!$G$"$k!#(B
$B3F(B@var{symbol}$B$K!"BP1~$9$k(B@var{form}$B$NI>2A7k2L$G$"$k?7$?$JCM$rM?$($k!#(B
$B%7%s%\%k$N4{B8$N:]!"%m!<%+%k$NB+G{$rJQ99$9$k!#(B
@c @code{setq} does not evaluate @var{symbol}; it sets the symbol that you
@c write. We say that this argument is @dfn{automatically quoted}. The
@c @samp{q} in @code{setq} stands for ``quoted.''
@code{setq}$B$O(B@var{symbol}$B$rI>2A$7$J$$!#(B
$BFI<T$,=q$$$?%7%s%\%k$K@_Dj$9$k!#(B
$B$3$NJQ?t$O(B@dfn{$B<+F0E*$K%/%)!<%H$5$l$k(B}$B$N$G$"$k!#(B
@code{setq}$B$N(B@samp{q}$B$O!"!X(Bquoted$B!J%/%)!<%H$9$k!K!Y$rI=$9!#(B
@c The value of the @code{setq} form is the value of the last @var{form}.
$B%U%)!<%`(B@code{setq}$B$NCM$O!":G8e$N(B@var{form}$B$NCM$G$"$k!#(B
@example
@group
(setq x (1+ 2))
@result{} 3
@end group
@c x ; @r{@code{x} now has a global value.}
x ; @r{@code{x}$B$O%0%m!<%P%kCM$r;}$D(B}
@result{} 3
@group
(let ((x 5))
@c (setq x 6) ; @r{The local binding of @code{x} is set.}
(setq x 6) ; @r{@code{x}$B$N%m!<%+%kB+G{$r@_Dj$9$k(B}
x)
@result{} 6
@end group
@c x ; @r{The global value is unchanged.}
x ; @r{$B%0%m!<%P%kCM$OJQ99$5$l$J$$(B}
@result{} 3
@end example
@c Note that the first @var{form} is evaluated, then the first
@c @var{symbol} is set, then the second @var{form} is evaluated, then the
@c second @var{symbol} is set, and so on:
$B:G=i$N(B@var{form}$B$rI>2A$7$F:G=i$N(B@var{symbol}$B$K@_Dj$7!"(B
$B$D$.$K!"(B2$BHVL\$N(B@var{form}$B$rI>2A$7$F(B2$BHVL\$N(B@var{symbol}$B$K@_Dj$7!"(B
$B$H$$$C$?6q9g$K$J$k$3$H$KCm0U!#(B
@example
@group
@c (setq x 10 ; @r{Notice that @code{x} is set before}
@c y (1+ x)) ; @r{the value of @code{y} is computed.}
(setq x 10 ; @r{@code{x}$B$O!"(B@code{y}$B$NCM$r7W;;$9$k$^$($K(B}
y (1+ x)) ; @r{$B@_Dj$5$l$k$3$H$KCm0U(B}
@result{} 11
@end group
@end example
@end defspec
@defun set symbol value
@c This function sets @var{symbol}'s value to @var{value}, then returns
@c @var{value}. Since @code{set} is a function, the expression written for
@c @var{symbol} is evaluated to obtain the symbol to set.
$B$3$N4X?t$O!"(B@var{symbol}$B$NCM$H$7$F(B@var{value}$B$r@_Dj$7!"(B@var{value}$B$rJV$9!#(B
@code{set}$B$O4X?t$J$N$G!"(B@var{symbol}$B$H$7$F=q$$$?<0$O!"(B
$B@_Dj$9$k%7%s%\%k$rF@$k$?$a$KI>2A$5$l$k!#(B
@c The most-local existing binding of the variable is the binding that is
@c set; shadowed bindings are not affected.
$BJQ?t$N4{B8$N:G%m!<%+%k$NB+G{$K@_Dj$9$k!#(B
$B1#$5$l$F$$$kB+G{$K$O1F6A$7$J$$!#(B
@example
@group
(set one 1)
@error{} Symbol's value as variable is void: one
@end group
@group
(set 'one 1)
@result{} 1
@end group
@group
(set 'two 'one)
@result{} one
@end group
@group
@c (set two 2) ; @r{@code{two} evaluates to symbol @code{one}.}
(set two 2) ; @r{@code{two}$B$O%7%s%\%k(B@code{one}$B$KI>2A$5$l$k(B}
@result{} 2
@end group
@group
@c one ; @r{So it is @code{one} that was set.}
one ; @r{$B$=$N$?$a!"(B@code{one}$B$K@_Dj$5$l$k(B}
@result{} 2
@c (let ((one 1)) ; @r{This binding of @code{one} is set,}
@c (set 'one 3) ; @r{not the global value.}
(let ((one 1)) ; @r{@code{one}$B$N$3$NB+G{$,@_Dj$5$l!"(B}
(set 'one 3) ; @r{$B%0%m!<%P%kCM$O@_Dj$5$l$J$$(B}
one)
@result{} 3
@end group
@group
one
@result{} 2
@end group
@end example
@c If @var{symbol} is not actually a symbol, a @code{wrong-type-argument}
@c error is signaled.
@var{symbol}$B!J$NI>2A7k2L!K$,<B:]$K$O%7%s%\%k$G$J$$$H!"(B
$B%(%i!<(B@code{wrong-type-argument}$B$rDLCN$9$k!#(B
@example
(set '(x y) 'z)
@error{} Wrong type argument: symbolp, (x y)
@end example
@c Logically speaking, @code{set} is a more fundamental primitive than
@c @code{setq}. Any use of @code{setq} can be trivially rewritten to use
@c @code{set}; @code{setq} could even be defined as a macro, given the
@c availability of @code{set}. However, @code{set} itself is rarely used;
@c beginners hardly need to know about it. It is useful only for choosing
@c at run time which variable to set. For example, the command
@c @code{set-variable}, which reads a variable name from the user and then
@c sets the variable, needs to use @code{set}.
$BO@M}E*$K$O!"(B@code{set}$B$O(B@code{setq}$B$h$j$b$5$i$K4pK\E*$JA`:n$G$"$k!#(B
$B$I$s$J(B@code{setq}$B$N;H$$J}$G$b!"(B@code{set}$B$GAGD>$K=q$-D>$;$k!#(B
@code{setq}$B$O!"(B@code{set}$B$r;H$C$F%^%/%m$H$7$FDj5A$9$k$3$H$b2DG=$G$"$k!#(B
$B$7$+$7!"(B@code{set}$B$=$N$b$N$r;H$&$3$H$O5)$G$"$j!"(B
$B=i?4<T$O(B@code{set}$B$rCN$kI,MW$,$[$H$s$I$J$$!#(B
$B@_Dj$9$kJQ?t$r<B9T;~$KA*$V$H$-$K$N$_M-MQ$G$"$k!#(B
$B$?$H$($P!"%3%^%s%I(B@code{set-variable}$B$O!"(B
$B%f!<%6!<$+$iJQ?tL>$rFI$_<h$j$=$NJQ?t$K@_Dj$9$k$N$G!"(B
@code{set}$B$r;H$&I,MW$,$"$k!#(B
@c @cindex CL note---@code{set} local
@cindex CL$B$K4X$7$?Cm0U!]!](B@code{set}$B$O%m!<%+%k(B
@quotation
@c @b{Common Lisp note:} In Common Lisp, @code{set} always changes the
@c symbol's ``special'' or dynamic value, ignoring any lexical bindings.
@c In Emacs Lisp, all variables and all bindings are dynamic, so @code{set}
@c always affects the most local existing binding.
@b{Common Lisp$B$K4X$7$?Cm0U!'(B}@code{ }
Common Lisp$B$G$O!"(B@code{set}$B$O$D$M$K%7%s%\%k$N!X%9%Z%7%c%k!Y$J!"$D$^$j!"(B
$BF0E*$JCM$rJQ99$7!"J8L.>e$NB+G{$rL5;k$9$k!#(B
Emacs Lisp$B$G$O!"$9$Y$F$NJQ?t$H$9$Y$F$NB+G{$OF0E*$G$"$j!"(B
@code{set}$B$O$D$M$K4{B8$N:G%m!<%+%k$NB+G{$K:nMQ$9$k!#(B
@end quotation
@end defun
@c One other function for setting a variable is designed to add
@c an element to a list if it is not already present in the list.
$BJQ?t$K@_Dj$9$kJL$N4X?t$O!"%j%9%H$K4{B8$G$J$$MWAG$rDI2C$9$k$h$&$K(B
$B@_7W$5$l$?$b$N$G$9!#(B
@defun add-to-list symbol element
@c This function sets the variable @var{symbol} by consing @var{element}
@c onto the old value, if @var{element} is not already a member of that
@c value. It returns the resulting list, whether updated or not. The
@c value of @var{symbol} had better be a list already before the call.
$B$3$N4X?t$O!"(B@var{element}$B$,JQ?t(B@var{symbol}$B$NCM$N%j%9%H$N%a%s%P$G$J$1$l$P!"(B
@var{element}$B$HJQ?t(B@var{symbol}$B$NCM$r%3%s%9$7$?CM$r(B
$BJQ?t(B@var{symbol}$B$K@_Dj$9$k!#(B
$B%j%9%H$rJQ99$7$F$b$7$J$/$F$b7k2L$N%j%9%H$rJV$9!#(B
$B8F$S=P$9$^$($K!"(B@var{symbol}$B$NCM$O%j%9%H$G$"$k$[$&$,$h$$!#(B
@c The argument @var{symbol} is not implicitly quoted; @code{add-to-list}
@c is an ordinary function, like @code{set} and unlike @code{setq}. Quote
@c the argument yourself if that is what you want.
$B0z?t(B@var{symbol}$B$O0EL[$K%/%)!<%H$5$l$J$$!#(B
@code{add-to-list}$B$O!"(B@code{set}$B$N$h$&$KIaDL$N4X?t$G$"$j!"(B
@code{setq}$B$H$O0c$&!#(B
$BI,MW$J$i$P!"FI<T<+?H$G%/%)!<%H$9$k!#(B
@end defun
@c Here's a scenario showing how to use @code{add-to-list}:
@code{add-to-list}$B$N;H$$J}$r0J2<$K<($7$^$9!#(B
@example
(setq foo '(a b))
@result{} (a b)
@c (add-to-list 'foo 'c) ;; @r{Add @code{c}.}
(add-to-list 'foo 'c) ;; @r{@code{c}$B$rDI2C$9$k(B}
@result{} (c a b)
@c (add-to-list 'foo 'b) ;; @r{No effect.}
(add-to-list 'foo 'b) ;; @r{$B$J$s$N8z2L$b$J$$(B}
@result{} (c a b)
@c foo ;; @r{@code{foo} was changed.}
foo ;; @r{@code{foo}$B$OJQ99$5$l$F$$$k(B}
@result{} (c a b)
@end example
@c An equivalent expression for @code{(add-to-list '@var{var}
@c @var{value})} is this:
@code{(add-to-list '@var{var} @var{value})}$B$KEy2A$J<0$O$D$.$N$H$*$j$G$9!#(B
@example
(or (member @var{value} @var{var})
(setq @var{var} (cons @var{value} @var{var})))
@end example
@node Variable Scoping
@c @section Scoping Rules for Variable Bindings
@section $BJQ?tB+G{$N%9%3!<%W%k!<%k(B
@c A given symbol @code{foo} can have several local variable bindings,
@c established at different places in the Lisp program, as well as a global
@c binding. The most recently established binding takes precedence over
@c the others.
$B$"$k%7%s%\%k(B@code{foo}$B$O!"$5$^$6$^$J%m!<%+%k$JJQ?tB+G{$r;}$D$3$H$,$G$-$^$9!#(B
Lisp$B%W%m%0%i%`$N0[$J$k>l=j$G3NN)$5$l$?$b$N$d(B
$B%0%m!<%P%kB+G{$G$9!#(B
$B$b$C$H$b:G6a$K3NN)$7$?B+G{$,B>$N$b$N$KM%@h$7$^$9!#(B
@c @cindex scope
@c @cindex extent
@c @cindex dynamic scoping
@cindex $B%9%3!<%W(B
@cindex $BB8B34|4V(B
@cindex $BF0E*%9%3!<%W(B
@c Local bindings in Emacs Lisp have @dfn{indefinite scope} and
@c @dfn{dynamic extent}. @dfn{Scope} refers to @emph{where} textually in
@c the source code the binding can be accessed. Indefinite scope means
@c that any part of the program can potentially access the variable
@c binding. @dfn{Extent} refers to @emph{when}, as the program is
@c executing, the binding exists. Dynamic extent means that the binding
@c lasts as long as the activation of the construct that established it.
Emacs Lisp$B$N%m!<%+%kB+G{$O!"(B@dfn{$BL58B$N%9%3!<%W(B}$B!J(Bindefinite scope$B!K$H(B
@dfn{$BF0E*B8B34|4V(B}$B!J(Bdynamic extent$B!K$r;}$A$^$9!#(B
@dfn{$B%9%3!<%W(B}$B!J(Bscope$B!K$H$O!"%=!<%9%3!<%I$N%F%-%9%H>e$N(B
@emph{$B$I$3(B}$B$+$iB+G{$r;2>H$G$-$k$+$rI=$7$^$9!#(B
$BL58B$N%9%3!<%W$H$O!"%W%m%0%i%`$N$I$3$+$i$G$bJQ?tB+G{$r;2>H$G$-$k$3$H$r(B
$B0UL#$7$^$9!#(B
@dfn{$BB8B34|4V(B}$B!J(Bextent$B!K$H$O!"%W%m%0%i%`$N<B9T$K$7$?$,$C$F!"(B
@emph{$B$$$D(B}$BB+G{$,B8:_$9$k$+$rI=$7$^$9!#(B
$BF0E*B8B34|4V$H$O!"B+G{$r:n@.$7$?9=B$$,M-8z$G$"$k8B$j!"(B
$BB+G{$,B8B3$9$k$3$H$r0UL#$7$^$9!#(B
@c The combination of dynamic extent and indefinite scope is called
@c @dfn{dynamic scoping}. By contrast, most programming languages use
@c @dfn{lexical scoping}, in which references to a local variable must be
@c located textually within the function or block that binds the variable.
$BF0E*B8B34|4V$HL58B$N%9%3!<%W$NAH$_9g$;$r(B
@dfn{$BF0E*%9%3!<%W(B}$B!J(Bdynamic scoping$B!K$H8F$S$^$9!#(B
$BBP>HE*$K!"$[$H$s$I$N%W%m%0%i%`8@8l$O!"(B
@dfn{$B%l%-%7%+%k%9%3!<%W(B}$B!J(Blexical scoping$B!K$rMQ$$$^$9!#(B
$B$D$^$j!"%m!<%+%kJQ?t$N;2>H$O!"(B
$B$=$NJQ?t$rB+G{$9$k4X?t$d%V%m%C%/$N%F%-%9%H>e$GFbB&$K$"$kI,MW$,$"$j$^$9!#(B
@c @cindex CL note---special variables
@cindex CL$B$K4X$7$?Cm0U!]!]%9%Z%7%c%kJQ?t(B
@quotation
@c @b{Common Lisp note:} Variables declared ``special'' in Common Lisp are
@c dynamically scoped, like all variables in Emacs Lisp.
@b{Common Lisp$B$K4X$7$?Cm0U!'(B}@code{ }
Common Lisp$B$G$O!"!X%9%Z%7%c%k!Y$H@k8@$7$?JQ?t$O!"(B
Emacs Lisp$B$N$9$Y$F$NJQ?t$HF1MM$K!"F0E*%9%3!<%W$G$"$k!#(B
@end quotation
@menu
* Scope:: Scope means where in the program a value is visible.
Comparison with other languages.
* Extent:: Extent means how long in time a value exists.
* Impl of Scope:: Two ways to implement dynamic scoping.
* Using Scoping:: How to use dynamic scoping carefully and avoid problems.
@end menu
@node Scope
@c @subsection Scope
@subsection $B%9%3!<%W(B
@c Emacs Lisp uses @dfn{indefinite scope} for local variable bindings.
@c This means that any function anywhere in the program text might access a
@c given binding of a variable. Consider the following function
@c definitions:
Emacs Lisp$B$G$O!"(B
$B%m!<%+%kJQ?tB+G{$O(B@dfn{$BL58B$N%9%3!<%W(B}$B!J(Bindefinite scope$B!K$G$9!#(B
$B$D$^$j!"%W%m%0%i%`%F%-%9%H>e$N$I$N4X?t$+$i$G$b!"(B
$B$"$kJQ?tB+G{$r;2>H$G$-$k$N$G$9!#(B
$B$D$.$N4X?tDj5A$r9M$($F$_$^$7$g$&!#(B
@example
@group
@c (defun binder (x) ; @r{@code{x} is bound in @code{binder}.}
@c (foo 5)) ; @r{@code{foo} is some other function.}
(defun binder (x) ; @r{@code{x}$B$O!"(B@code{binder}$B$GB+G{(B}
(foo 5)) ; @r{@code{foo}$B$OJL$N4X?t(B}
@end group
@group
@c (defun user () ; @r{@code{x} is used ``free'' in @code{user}.}
(defun user () ; @r{@code{x}$B$O!"(B@code{user}$B$K$*$$$F!X<+M3!Y(B}
(list x))
@end group
@end example
@c In a lexically scoped language, the binding of @code{x} in
@c @code{binder} would never be accessible in @code{user}, because
@c @code{user} is not textually contained within the function
@c @code{binder}. However, in dynamically scoped Emacs Lisp, @code{user}
@c may or may not refer to the binding of @code{x} established in
@c @code{binder}, depending on circumstances:
$B%F%-%9%H>e$N%9%3!<%W$rMQ$$$k8@8l$G$O!"(B
@code{binder}$BFb$N(B@code{x}$B$NB+G{$r!"(B@code{user}$B$G;2>H$9$k$3$H$O$G$-$^$;$s!#(B
$B$J$<$J$i!"(B@code{user}$B$O!"%F%-%9%H>e$G4X?t(B@code{binder}$B$NFbB&$K$O$J$$$+$i$G$9!#(B
$B$7$+$7$J$,$i!"F0E*%9%3!<%W$N(BEmacs Lisp$B$G$O!"(B
$B>u67$K1~$8$F!"(B@code{binder}$BFb$G3NN)$7$?(B@code{x}$B$NB+G{$r(B
@code{user}$B$+$i;2>H$7$F$b$7$J$/$F$b$h$$$N$G$9!#(B
@itemize @bullet
@item
@c If we call @code{user} directly without calling @code{binder} at all,
@c then whatever binding of @code{x} is found, it cannot come from
@c @code{binder}.
@code{binder}$B$r$^$C$?$/8F$S=P$5$:$K!"D>@\(B@code{user}$B$r8F$S=P$7$?$H$-$K$O!"(B
$B$H$K$+$/$_$D$+$C$?(B@code{x}$B$NB+G{$r;H$&$,!"(B
$B$=$l$O(B@code{binder}$B$N$b$N$G$O$"$j$($J$$!#(B
@item
@c If we define @code{foo} as follows and then call @code{binder}, then the
@c binding made in @code{binder} will be seen in @code{user}:
@code{foo}$B$r$D$.$N$h$&$KDj5A$7$F(B@code{binder}$B$r8F$S=P$7$?$H$-$K$O!"(B
@code{binder}$B$,:n$C$?B+G{$r(B@code{user}$B$G8+$($k!#(B
@example
@group
(defun foo (lose)
(user))
@end group
@end example
@item
@c However, if we define @code{foo} as follows and then call @code{binder},
@c then the binding made in @code{binder} @emph{will not} be seen in
@c @code{user}:
$B$7$+$7!"(B@code{foo}$B$r$D$.$N$h$&$KDj5A$7$F(B@code{binder}$B$r8F$S=P$7$?$H$-$K$O!"(B
@code{binder}$B$,:n$C$?B+G{$O(B@code{user}$B$G$O(B@emph{$B8+$($J$$(B}$B!#(B
@example
(defun foo (x)
(user))
@end example
@noindent
@c Here, when @code{foo} is called by @code{binder}, it binds @code{x}.
@c (The binding in @code{foo} is said to @dfn{shadow} the one made in
@c @code{binder}.) Therefore, @code{user} will access the @code{x} bound
@c by @code{foo} instead of the one bound by @code{binder}.
$B$3$3$G!"(B@code{binder}$B$,(B@code{foo}$B$r8F$S=P$9$H!"(B
@code{foo}$B$O(B@code{x}$B$rB+G{$9$k!#(B
$B!J(B@code{foo}$B$NB+G{$O(B@code{binder}$B$NB+G{$r(B@dfn{$B1#$9(B}$B!J(Bshadow$B!K$H$$$&!#!K(B
$B$7$?$,$C$F!"(B@code{user}$B$O!"(B@code{binder}$B$NB+G{$G$O$J$/!"(B
@code{foo}$B$NB+G{$r;2>H$9$k$3$H$K$J$k!#(B
@end itemize
@c Emacs Lisp uses dynamic scoping because simple implementations of
@c lexical scoping are slow. In addition, every Lisp system needs to offer
@c dynamic scoping at least as an option; if lexical scoping is the norm,
@c there must be a way to specify dynamic scoping instead for a particular
@c variable. It might not be a bad thing for Emacs to offer both, but
@c implementing it with dynamic scoping only was much easier.
Emacs Lisp$B$GF0E*%9%3!<%W$r;H$&$N$O!"(B
$B%F%-%9%H>e$N%9%3!<%W$NC1=c$J<BAu$OCY$$$+$i$G$9!#(B
$B$5$i$K!"$9$Y$F$N(BLisp$B%7%9%F%`$O!">/$J$/$H$b%*%W%7%g%s$H$7$F!"(B
$BF0E*%9%3!<%W$r;H$($k$h$&$K$9$kI,MW$,$"$j$^$9!#(B
$B%F%-%9%H>e$N%9%3!<%W$,I8=`$G$"$k$H!"(B
$BFCDj$NJQ?t$KBP$7$FF0E*%9%3!<%W$r;XDj$9$kJ}K!$,I,MW$K$J$j$^$9!#(B
Emacs$B$GN>J}$N%9%3!<%W$r;H$($k$h$&$K$7$F$b$h$$$N$G$9$,!"(B
$BF0E*%9%3!<%W$@$1$@$H<BAu$,$h$j4JC1$K$J$j$^$9!#(B
@node Extent
@c @subsection Extent
@subsection $BB8B34|4V(B
@c @dfn{Extent} refers to the time during program execution that a
@c variable name is valid. In Emacs Lisp, a variable is valid only while
@c the form that bound it is executing. This is called @dfn{dynamic
@c extent}. ``Local'' or ``automatic'' variables in most languages,
@c including C and Pascal, have dynamic extent.
@dfn{$BB8B34|4V(B}$B!J(BExtent$B!K$H$O!"%W%m%0%i%`$N<B9TCf$K$*$$$F!"(B
$BJQ?tL>$,M-8z$G$"$k4|4V$r;X$7$^$9!#(B
Emacs Lisp$B$G$O!"B+G{$r:n$C$?%U%)!<%`$r<B9T$7$F$$$k4|4VCf$@$1!"(B
$BJQ?t$OM-8z$G$9!#(B
$B$3$l$r(B@dfn{$BF0E*B8B34|4V(B}$B!J(Bdynamic extent$B!K$H8F$S$^$9!#(B
C$B$d(BPascal$B$J$I$N$[$H$s$I$N8@8l$N!X%m!<%+%k!YJQ?t$d!X<+F0!YJQ?t$b(B
$BF0E*B8B34|4V$G$9!#(B
@c One alternative to dynamic extent is @dfn{indefinite extent}. This
@c means that a variable binding can live on past the exit from the form
@c that made the binding. Common Lisp and Scheme, for example, support
@c this, but Emacs Lisp does not.
$BF0E*B8B34|4V$H$OJL$N$b$N$K(B@dfn{$BL58B$NB8B34|4V(B}$B!J(Bindefinite extent$B!K$,$"$j$^$9!#(B
$B$D$^$j!"JQ?tB+G{$O!"$=$NB+G{$r:n$C$?%U%)!<%`$+$iH4$1$F$bB8B3$9$k$N$G$9!#(B
$B$?$H$($P!"(BCommon Lisp$B$d(BScheme$B$K$O$3$l$,$"$j$^$9$,!"(BEmacs Lisp$B$K$O$"$j$^$;$s!#(B
@c To illustrate this, the function below, @code{make-add}, returns a
@c function that purports to add @var{n} to its own argument @var{m}. This
@c would work in Common Lisp, but it does not do the job in Emacs Lisp,
@c because after the call to @code{make-add} exits, the variable @code{n}
@c is no longer bound to the actual argument 2.
$B$3$l$r@bL@$9$k$?$a$K!"$D$.$N4X?t(B@code{make-add}$B$r9M$($^$9!#(B
$B$3$N4X?t$O!"(B@var{n}$B$K<+?H$N0z?t(B@var{m}$B$r2C;;$9$k4X?t$rJV$7$^$9!#(B
$B$3$N4X?t$O(BCommon Lisp$B$G$OF0:n$7$^$9$,!"(BEmacs Lisp$B$G$O$@$a$G$9!#(B
$B$H$$$&$N$O!"(B@code{make-add}$B$N8F$S=P$7$rH4$1$k$H!"(B
$BJQ?t(B@var{n}$B$O<B0z?t(B2$B$KB+G{$5$l$J$/$J$k$+$i$G$9!#(B
@example
(defun make-add (n)
@c (function (lambda (m) (+ n m)))) ; @r{Return a function.}
(function (lambda (m) (+ n m)))) ; @r{$B4X?t$rJV$9(B}
@result{} make-add
@c (fset 'add2 (make-add 2)) ; @r{Define function @code{add2}}
@c ; @r{with @code{(make-add 2)}.}
(fset 'add2 (make-add 2)) ; @r{$B4X?t(B@code{add2}$B$r(B}
; @r{@code{(make-add 2)}$B$r;H$C$FDj5A$9$k(B}
@result{} (lambda (m) (+ n m))
@c (add2 4) ; @r{Try to add 2 to 4.}
(add2 4) ; @r{4$B$K(B2$B$r2C;;$7$F$_$k(B}
@error{} Symbol's value as variable is void: n
@end example
@c @cindex closures not available
@cindex $B%/%m!<%8%c$O;H$($J$$(B
@c Some Lisp dialects have ``closures'', objects that are like functions
@c but record additional variable bindings. Emacs Lisp does not have
@c closures.
Lisp$B$NJ}8@$N$$$/$D$+$K$O!X%/%m!<%8%c!Y!J(Bclosure$B!K$,$"$j$^$9!#(B
$B$=$l$O4X?t$N$h$&$J%*%V%8%'%/%H$G$9$,!"DI2C$NJQ?tB+G{$r5-O?$7$^$9!#(B
Emacs Lisp$B$K$O%/%m!<%8%c$O$"$j$^$;$s!#(B
@node Impl of Scope
@c @subsection Implementation of Dynamic Scoping
@subsection $BF0E*%9%3!<%W$N<BAu(B
@c @cindex deep binding
@cindex $B?<$$B+G{!J%G%#!<%W%P%$%s%G%#%s%0!"(Bdeep binding$B!K(B
@c A simple sample implementation (which is not how Emacs Lisp actually
@c works) may help you understand dynamic binding. This technique is
@c called @dfn{deep binding} and was used in early Lisp systems.
$B!J(BEmacs Lisp$B$N<B:]$NF0:n$H$O0[$J$k$,!KC1=c$J<BAuNc$,!"(B
$BF0E*B+G{$rM}2r$9$k=u$1$K$J$k$G$7$g$&!#(B
$B$3$N5;K!$r(B@dfn{$B?<$$B+G{(B}$B!J%G%#!<%W%P%$%s%G%#%s%0!"(Bdeep binding$B!K$H8F$S!"(B
$B=i4|$N(BLisp$B%7%9%F%`$G;H$o$l$F$$$^$7$?!#(B
@c Suppose there is a stack of bindings, which are variable-value pairs.
@c At entry to a function or to a @code{let} form, we can push bindings
@c onto the stack for the arguments or local variables created there. We
@c can pop those bindings from the stack at exit from the binding
@c construct.
$BJQ?t!&CM$NBP$G$"$kB+G{$N%9%?%C%/$,$"$k$H$7$^$7$g$&!#(B
$B4X?t$d%U%)!<%`(B@code{let}$B$KF~$k$H!"(B
$B0z?t$d%m!<%+%kJQ?t$NB+G{$r%9%?%C%/$K@Q$_$^$9!#(B
$BB+G{$r:n$C$?9=B$$+$iH4$1$k$H$=$l$i$NB+G{$r<h$j$5$j$^$9!#(B
@c We can find the value of a variable by searching the stack from top to
@c bottom for a binding for that variable; the value from that binding is
@c the value of the variable. To set the variable, we search for the
@c current binding, then store the new value into that binding.
$BJQ?t$NCM$O!"%9%?%C%/$N@hF,$+$iDl$X8~$1$F$=$NJQ?t$NB+G{$rC5:w$7$^$9!#(B
$B$=$NB+G{$+$iF@$kCM$,JQ?t$NCM$K$J$j$^$9!#(B
$BJQ?t$K@_Dj$9$k$K$O!"8=:_$NB+G{$rC5$7$F!"$=$NB+G{$K?7$?$JCM$r3JG<$7$^$9!#(B
@c As you can see, a function's bindings remain in effect as long as it
@c continues execution, even during its calls to other functions. That is
@c why we say the extent of the binding is dynamic. And any other function
@c can refer to the bindings, if it uses the same variables while the
@c bindings are in effect. That is why we say the scope is indefinite.
$B$3$l$+$i$o$+$k$h$&$K!"4X?t$NB+G{$O!"$=$N4X?t$N<B9TCf$K$O!"(B
$B$?$H$(JL$N4X?t$r8F$S=P$7$F$$$F$b!"B8B3$7$F$$$^$9!#(B
$B$3$l$,B+G{$NB8B3$,F0E*$G$"$k$H$$$&M}M3$G$9!#(B
$B$^$?!"$=$NB+G{$,M-8z$G$"$k4|4VCf$J$i$P!"F1$8JQ?t$r;H$($PB>$N4X?t$+$i$b(B
$BB+G{$r;2>H$G$-$k$N$G$9!#(B
$B$3$l$,%9%3!<%W$,L58B$G$"$k$H$$$&M}M3$G$9!#(B
@c @cindex shallow binding
@cindex $B@u$$B+G{!J%7%c%m!<%P%$%s%G%#%s%0!"(Bshallow binding$B!K(B
@c The actual implementation of variable scoping in GNU Emacs Lisp uses a
@c technique called @dfn{shallow binding}. Each variable has a standard
@c place in which its current value is always found---the value cell of the
@c symbol.
GNU Emacs Lisp$B$K$*$$$F!"JQ?t$N%9%3!<%W$N<B:]$N<BAu$K$O!"(B
@dfn{$B@u$$B+G{(B}$B!J%7%c%m!<%P%$%s%G%#%s%0!"(Bshallow binding$B!K$H8F$P$l$k(B
$B5;K!$rMQ$$$F$$$^$9!#(B
$B3FJQ?t$K$O8=:_CM$rJ]B8$7$F$*$/I8=`$N>l=j!"%7%s%\%k$NCM%;%k$,$"$j$^$9!#(B
@c In shallow binding, setting the variable works by storing a value in
@c the value cell. Creating a new binding works by pushing the old value
@c (belonging to a previous binding) onto a stack, and storing the new
@c local value in the value cell. Eliminating a binding works by popping
@c the old value off the stack, into the value cell.
$B@u$$B+G{$G$O!"JQ?t$N@_Dj$OCM%;%k$KCM$r3JG<$9$k$3$H$GF0:n$7$^$9!#(B
$B?7$?$JB+G{$r:n@.$9$k$H!J0JA0$NB+G{$KB0$9$k!K8E$$CM$r%9%?%C%/$K@Q$_!"(B
$B?7$?$J%m!<%+%kCM$rCM%;%k$K3JG<$7$^$9!#(B
$BB+G{$r2r$/$H$-$K$O!"8E$$CM$r%9%?%C%/$+$i<h$j=P$7$FCM%;%k$K3JG<$7$^$9!#(B
@c We use shallow binding because it has the same results as deep
@c binding, but runs faster, since there is never a need to search for a
@c binding.
$B@u$$B+G{$rMQ$$$kM}M3$O!"B+G{$rC5:w$9$kI,MW$,$J$$$?$a!"(B
$B?<$$B+G{$HF1$87k2L$r;}$A$J$,$i9bB.$KF0:n$9$k$+$i$G$9!#(B
@node Using Scoping
@c @subsection Proper Use of Dynamic Scoping
@subsection $BF0E*%9%3!<%W$N@5$7$$;H$$J}(B
@c Binding a variable in one function and using it in another is a
@c powerful technique, but if used without restraint, it can make programs
@c hard to understand. There are two clean ways to use this technique:
$B$"$k4X?t$GJQ?t$rB+G{$7JL$N4X?t$G$=$l$r;H$&$3$H$O!"6/NO$J5;K!$G$9$,!"(B
$B$J$s$N@)8B$b$;$:$K;H$&$H%W%m%0%i%`$rM}2r$7Fq$$$b$N$K$7$F$7$^$$$^$9!#(B
$B$3$N5;K!$r8+DL$7$h$/;H$&$?$a$N(B2$B$D$NJ}K!$,$"$j$^$9!#(B
@itemize @bullet
@item
@c Use or bind the variable only in a few related functions, written close
@c together in one file. Such a variable is used for communication within
@c one program.
1$B$D$N%U%!%$%kFb$G6a$/$K=q$$$?4XO"$9$k>/?t$N4X?t$G$@$1!"(B
$BJQ?t$r;H$C$?$jB+G{$7$?$j$9$k!#(B
$B$=$N$h$&$JJQ?t$O!"(B1$B$D$N%W%m%0%i%`Fb$G$NDL?.$K;H$&!#(B
@c You should write comments to inform other programmers that they can see
@c all uses of the variable before them, and to advise them not to add uses
@c elsewhere.
$BB>$N%W%m%0%i%^$KBP$7$F!"H`$i$,$=$N$h$&$JJQ?t$rL\$K$9$k$^$($K!"(B
$B$=$N$h$&$JJQ?t$N;H$$J}$,$o$+$k$h$&$J%3%a%s%H$r=q$-!"(B
$BB>$N>l=j$G$O;H$o$J$$$h$&$K=u8@$7$F$*$/!#(B
@item
@c Give the variable a well-defined, documented meaning, and make all
@c appropriate functions refer to it (but not bind it or set it) wherever
@c that meaning is relevant. For example, the variable
@c @code{case-fold-search} is defined as ``non-@code{nil} means ignore case
@c when searching''; various search and replace functions refer to it
@c directly or through their subroutines, but do not bind or set it.
$BJQ?t$K$O$h$/$o$+$k0UL#$rM?$(!"(B
$B$=$l$K4XO"$9$kE,@Z$J$9$Y$F$N4X?t$,!JB+G{$b@_Dj$b$7$J$$$G!K;2>H$9$k$h$&$K$9$k!#(B
$B$?$H$($P!"JQ?t(B@code{case-fold-search}$B$O!"(B
$B!X(B@code{nil}$B0J30$G$"$l$PC5:w;~$KBgJ8;z>.J8;z$r6hJL$7$J$$!Y$HDj5A$5$l$F$$$k!#(B
$B$5$^$6$^$JC5:w4X?t$dCV494X?t$,!"$3$NJQ?t$rD>@\$K!"$"$k$$$O!"(B
$B%5%V%k!<%F%#%s$r2p$7$F;2>H$9$k$,!"(B
$B$3$NJQ?t$rB+G{$7$?$j@_Dj$7$?$j$7$J$$!#(B
@c Then you can bind the variable in other programs, knowing reliably what
@c the effect will be.
$B$3$&$7$F$*$$$FJL$N%W%m%0%i%`$GJQ?t$rB+G{$9$k$,!"(B
$B$=$l$K$I$N$h$&$J8z2L$,$"$k$+3N<B$KCN$C$F$+$i9T$($k!#(B
@end itemize
@c In either case, you should define the variable with @code{defvar}.
@c This helps other people understand your program by telling them to look
@c for inter-function usage. It also avoids a warning from the byte
@c compiler. Choose the variable's name to avoid name conflicts---don't
@c use short names like @code{x}.
$B$$$:$l$N>l9g$G$b!"JQ?t$O(B@code{defvar}$B$GDj5A$9$k$Y$-$G$9!#(B
$B$3$l$O!"4X?t4V$G$NJQ?t$N;H$$J}$r8+$k$h$&$KEA$($k$3$H$G!"(B
$BB>?M$,FI<T$N%W%m%0%i%`$rM}2r$9$k$N$r=u$1$^$9!#(B
$B$^$?!"%P%$%H%3%s%Q%$%i$+$i$N7Y9p$bKI$.$^$9!#(B
$BJQ?tL>$,>WFM$7$J$$$h$&$K$bCm0U$7$^$7$g$&!#(B
@code{x}$B$N$h$&$JC;$$L>A0$r;H$o$J$$$G$/$@$5$$!#(B
@node Buffer-Local Variables
@c @section Buffer-Local Variables
@section $B%P%C%U%!%m!<%+%k$JJQ?t(B
@c @cindex variables, buffer-local
@c @cindex buffer-local variables
@cindex $BJQ?t!"%P%C%U%!%m!<%+%k(B
@cindex $B%P%C%U%!%m!<%+%k$JJQ?t(B
@c Global and local variable bindings are found in most programming
@c languages in one form or another. Emacs also supports additional,
@c unusual kinds of variable binding: @dfn{buffer-local} bindings, which
@c apply only in one buffer, and frame-local bindings, which apply only in
@c one frame. Having different values for a variable in different buffers
@c and/or frames is an important customization method.
$B%0%m!<%P%k$H%m!<%+%k$NJQ?tB+G{$O!"(B
$B$[$H$s$I$N%W%m%0%i%`8@8l$K$$$m$$$m$J7A$G$"$j$^$9!#(B
Emacs$B$K$O!"$"$^$jIaDL$G$J$$DI2C$N<oN`$NJQ?tB+G{$,$"$j$^$9!#(B
1$B$D$N%P%C%U%!$@$1$KE,MQ$5$l$k(B@dfn{$B%P%C%U%!%m!<%+%k(B}$B$JB+G{!"(B
1$B$D$N%U%l!<%`$@$1$KE,MQ$5$l$k%U%l!<%`%m!<%+%k$JB+G{$G$9!#(B
$B0[$J$k%P%C%U%!$d%U%l!<%`$4$H$KJQ?t$K0[$J$kCM$,$"$k$H$$$&$3$H$O!"(B
$B=EMW$J%+%9%?%^%$%:5;K!$G$9!#(B
@c This section describes buffer-local bindings; for frame-local
@c bindings, see the following section, @ref{Frame-Local Variables}. (A few
@c variables have bindings that are local to each terminal; see
@c @ref{Multiple Displays}.)
$BK\@a$G$O!"%P%C%U%!%m!<%+%k$JB+G{$r@bL@$7$^$9!#(B
$B%U%l!<%`%m!<%+%k$JB+G{$K$D$$$F$O!"$D$.$N@a$H(B
@xref{Frame-Local Variables}$B!#(B
$B!J3FC<Kv$K%m!<%+%k$JB+G{$r;}$DJQ?t$b>/?t$"$k!#(B
@pxref{Multiple Displays}$B!#!K(B
@menu
* Intro to Buffer-Local:: Introduction and concepts.
* Creating Buffer-Local:: Creating and destroying buffer-local bindings.
* Default Value:: The default value is seen in buffers
that don't have their own buffer-local values.
@end menu
@node Intro to Buffer-Local
@c @subsection Introduction to Buffer-Local Variables
@subsection $B%P%C%U%!%m!<%+%k$JJQ?t$N>R2p(B
@c A buffer-local variable has a buffer-local binding associated with a
@c particular buffer. The binding is in effect when that buffer is
@c current; otherwise, it is not in effect. If you set the variable while
@c a buffer-local binding is in effect, the new value goes in that binding,
@c so its other bindings are unchanged. This means that the change is
@c visible only in the buffer where you made it.
$B%P%C%U%!%m!<%+%k$JJQ?t$K$O!"FCDj$N%P%C%U%!$K4XO"$7$?%P%C%U%!%m!<%+%k$J(B
$BB+G{$,$"$j$^$9!#(B
$B$3$NB+G{$O!"$=$N%P%C%U%!$,%+%l%s%H%P%C%U%!$G$"$k$H$-$KM-8z$K$J$j$^$9!#(B
$B$5$b$J$1$l$P$J$s$N8z2L$b$"$j$^$;$s!#(B
$B%P%C%U%!%m!<%+%k$JB+G{$,M-8z$J$H$-$KJQ?t$K@_Dj$9$k$H!"(B
$B?7$7$$CM$O$=$NB+G{$KF~$j!"B>$NB+G{$OJQ99$5$l$^$;$s!#(B
$B$D$^$j!"$=$NJQ99$O!"JQ99$r9T$C$?%P%C%U%!$@$1$G8+$k$3$H$,$G$-$k$N$G$9!#(B
@c The variable's ordinary binding, which is not associated with any
@c specific buffer, is called the @dfn{default binding}. In most cases,
@c this is the global binding.
$BJQ?t$NDL>o$NB+G{!"$D$^$j!"FCDj$N%P%C%U%!$K4XO"$7$F$$$J$$B+G{$r(B
@dfn{$B%G%U%)%k%H$NB+G{(B}$B!J(Bdefault binding$B!K$H8F$S$^$9!#(B
$BB?$/$N>l9g!"$3$l$O%0%m!<%P%kB+G{$G$9!#(B
@c A variable can have buffer-local bindings in some buffers but not in
@c other buffers. The default binding is shared by all the buffers that
@c don't have their own bindings for the variable. (This includes all
@c newly created buffers.) If you set the variable in a buffer that does
@c not have a buffer-local binding for it, this sets the default binding
@c (assuming there are no frame-local bindings to complicate the matter),
@c so the new value is visible in all the buffers that see the default
@c binding.
$BJQ?t$O!"$"$k%P%C%U%!72$G$O%P%C%U%!%m!<%+%k$JB+G{$r;}$A!"(B
$BB>$N%P%C%U%!$G$O$=$N$h$&$JB+G{$r;}$?$J$$$h$&$K$G$-$^$9!#(B
$BJQ?t$KBP$9$kFH<+$NB+G{$r;}$?$J$$%P%C%U%!$9$Y$F$G$O!"(B
$B%G%U%)%k%H$NB+G{$r6&M-$7$^$9!#(B
$B!J$3$l$K$O!"?7$?$K:n@.$5$l$k%P%C%U%!$b4^$`!#!K(B
$B%P%C%U%!%m!<%+%k$JB+G{$r;}$?$J$$%P%C%U%!$GJQ?t$K@_Dj$9$k$H!"(B
$B!J>u67$rJ#;($K$9$k%U%l!<%`%m!<%+%k$JB+G{$O$J$$$H2>Dj$7$F!K(B
$B%G%U%)%k%H$NB+G{$r;H$$$^$9!#(B
$B$7$?$,$C$F!"?7$?$JCM$O%G%U%)%k%H$NB+G{$r8+$k%P%C%U%!$9$Y$F$G8+$($^$9!#(B
@c The most common use of buffer-local bindings is for major modes to change
@c variables that control the behavior of commands. For example, C mode and
@c Lisp mode both set the variable @code{paragraph-start} to specify that only
@c blank lines separate paragraphs. They do this by making the variable
@c buffer-local in the buffer that is being put into C mode or Lisp mode, and
@c then setting it to the new value for that mode. @xref{Major Modes}.
$B%P%C%U%!%m!<%+%k$JB+G{$N$b$C$H$b0lHLE*$J;H$$J}$O!"(B
$B%a%8%c!<%b!<%I$G%3%^%s%I$N$U$k$^$$$r@)8f$9$kJQ?t$KJQ99$9$k$3$H$G$9!#(B
$B$?$H$($P!"(BC$B%b!<%I$d(BLisp$B%b!<%I$G$O!"JQ?t(B@code{paragraph-start}$B$r@_Dj$7$F!"(B
$B6u9T$@$1$,CJMn$r6h@Z$k$h$&$K;XDj$7$^$9!#(B
$B$3$l$K$O!"(BC$B%b!<%I$d(BLisp$B%b!<%I$K$J$C$?%P%C%U%!$G$O!"(B
$BJQ?t$r%P%C%U%!%m!<%+%k$K$7$F$+$i!"(B
$B$=$N%b!<%IMQ$N?7$?$JCM$rJQ?t$K@_Dj$9$k$N$G$9!#(B
@xref{Major Modes}$B!#(B
@c The usual way to make a buffer-local binding is with
@c @code{make-local-variable}, which is what major mode commands typically
@c use. This affects just the current buffer; all other buffers (including
@c those yet to be created) will continue to share the default value unless
@c they are explicitly given their own buffer-local bindings.
$B%P%C%U%!%m!<%+%k$JB+G{$r:n$kIaDL$NJ}K!$O!"(B
@code{make-local-variable}$B$G$9!#(B
$B%a%8%c!<%b!<%I$N%3%^%s%I$OE57?E*$K$3$l$r;H$$$^$9!#(B
$B$3$l$O%+%l%s%H%P%C%U%!$@$1$K1F6A$7$^$9!#(B
$B!J$3$l$+$i:n@.$9$k$b$N$b4^$a$F!KB>$N$9$Y$F$N%P%C%U%!$O!"(B
$B$=$l@lMQ$N%P%C%U%!%m!<%+%k$JB+G{$rL@<(E*$KM?$($J$$8B$j!"(B
$B%G%U%)%k%HCM$r6&M-$7B3$1$^$9!#(B
@c @cindex automatically buffer-local
@cindex $B<+F0E*$K%P%C%U%!%m!<%+%k$K$9$k(B
@c A more powerful operation is to mark the variable as
@c @dfn{automatically buffer-local} by calling
@c @code{make-variable-buffer-local}. You can think of this as making the
@c variable local in all buffers, even those yet to be created. More
@c precisely, the effect is that setting the variable automatically makes
@c the variable local to the current buffer if it is not already so. All
@c buffers start out by sharing the default value of the variable as usual,
@c but setting the variable creates a buffer-local binding for the current
@c buffer. The new value is stored in the buffer-local binding, leaving
@c the default binding untouched. This means that the default value cannot
@c be changed with @code{setq} in any buffer; the only way to change it is
@c with @code{setq-default}.
$B$h$j6/NO$JA`:n$O!"(B@code{make-variable-buffer-local}$B$r8F$S=P$7$F(B
$BJQ?t$r(B@dfn{$B<+F0E*$K%P%C%U%!%m!<%+%k$K(B}$B$9$k$h$&$K0u$rIU$1$k$3$H$G$9!#(B
$B$3$l$O!"$3$l$+$i:n@.$9$k$b$N$b4^$a$?%P%C%U%!$9$Y$F$G!"(B
$BJQ?t$r%P%C%U%!%m!<%+%k$K$9$k$H9M$($k$3$H$,$G$-$^$9!#(B
$B$h$j@53N$K$O!"JQ?t$,%+%l%s%H%P%C%U%!$K%m!<%+%k$G$J$1$l$P!"(B
$B<+F0E*$KJQ?t$r%+%l%s%H%P%C%U%!$K%m!<%+%k$K$9$k$h$&$K@_Dj$9$k8z2L$,$"$j$^$9!#(B
$B$9$Y$F$N%P%C%U%!$ODL>o$I$*$jJQ?t$N%G%U%)%k%HCM$r6&M-$7$F;O$^$j$^$9$,!"(B
$BJQ?t$K@_Dj$9$k$H%+%l%s%H%P%C%U%!$K%P%C%U%!%m!<%+%k$JB+G{$r:n$j$^$9!#(B
$B?7$?$JCM$O%P%C%U%!%m!<%+%k$JB+G{$K3JG<$5$l!"%G%U%)%k%H$NB+G{$OJQ99$7$^$;$s!#(B
$B$D$^$j!"$I$N%P%C%U%!$G$b%G%U%)%k%HCM$r(B@code{setq}$B$G$OJQ99$G$-$^$;$s!#(B
$B%G%U%)%k%HCM$rJQ99$9$kM#0l$NJ}K!$O!"(B@code{setq-default}$B$r;H$&$3$H$G$9!#(B
@c @strong{Warning:} When a variable has buffer-local values in one or
@c more buffers, you can get Emacs very confused by binding the variable
@c with @code{let}, changing to a different current buffer in which a
@c different binding is in effect, and then exiting the @code{let}. This
@c can scramble the values of the buffer-local and default bindings.
@strong{$B7Y9p!'(B}@code{ }
$BJ#?t$N%P%C%U%!$K$*$$$FJQ?t$K%P%C%U%!%m!<%+%k$JCM$,$"$k$H$-$K!"(B
$BJQ?t$r(B@code{let}$B$GB+G{$7$F$+$i!"(B
$BJL$NB+G{$,M-8z$G$"$kJL$N%P%C%U%!$K@Z$jBX$($F(B@code{let}$B$rH4$1$k$H!"(B
Emacs$B$r$H$F$b:.Mp$5$;$k$3$H$K$J$k!#(B
$B$3$&$9$k$H!"%P%C%U%!%m!<%+%k$JB+G{$H%G%U%)%k%H$NB+G{$r:.$<9g$o$;$F$7$^$&!#(B
@c To preserve your sanity, avoid using a variable in that way. If you
@c use @code{save-excursion} around each piece of code that changes to a
@c different current buffer, you will not have this problem
@c (@pxref{Excursions}). Here is an example of what to avoid:
$B:.Mp$rHr$1$k$?$a$K!"$3$N$h$&$JJQ?t$N;H$$J}$OHr$1$F$/$@$5$$!#(B
$BJL$N%P%C%U%!$K@Z$jBX$($k3F%3!<%IItJ,$r(B@code{save-excursion}$B$G0O$a$P!"(B
$B$3$N$h$&$JLdBj$O$"$j$^$;$s!#(B
@example
@group
(setq foo 'b)
(set-buffer "a")
(make-local-variable 'foo)
@end group
(setq foo 'a)
(let ((foo 'temp))
(set-buffer "b")
@var{body}@dots{})
@group
@c foo @result{} 'a ; @r{The old buffer-local value from buffer @samp{a}}
@c ; @r{is now the default value.}
foo @result{} 'a ; @r{$B%P%C%U%!(B@samp{a}$B$N8E$$%P%C%U%!%m!<%+%k$JCM$,(B}
; @r{$B8=:_$N%G%U%)%k%HCM(B}
@end group
@group
(set-buffer "a")
@c foo @result{} 'temp ; @r{The local @code{let} value that should be gone}
@c ; @r{is now the buffer-local value in buffer @samp{a}.}
foo @result{} 'temp ; @r{$B>C$($F$$$k$Y$-%m!<%+%k$J(B@code{let}$B$NCM$,(B}
; @r{$B%P%C%U%!(B@samp{a}$B$N8=:_$N%P%C%U%!%m!<%+%k$JCM(B}
@end group
@end example
@noindent
@c But @code{save-excursion} as shown here avoids the problem:
$B$7$+$7!"$D$.$K<($9$h$&$K(B@code{save-excursion}$B$r;H$($P!"$3$NLdBj$r2sHr$G$-$^$9!#(B
@example
@group
(let ((foo 'temp))
(save-excursion
(set-buffer "b")
@var{body}@dots{}))
@end group
@end example
@c Note that references to @code{foo} in @var{body} access the
@c buffer-local binding of buffer @samp{b}.
@var{body}$BFb$G$N(B@code{foo}$B$X$N;2>H$O!"(B
$B%P%C%U%!(B@samp{b}$B$N%P%C%U%!%m!<%+%k$JB+G{$r;H$$$^$9!#(B
@c When a file specifies local variable values, these become buffer-local
@c values when you visit the file. @xref{File Variables,,, emacs, The
@c GNU Emacs Manual}.
$B%U%!%$%k$G%m!<%+%kJQ?t$NCM$r;XDj$7$F$$$k$H!"(B
$B$=$N%U%!%$%k$rK,Ld$7$?$H$-$K!"$=$l$i$O%P%C%U%!%m!<%+%k$JCM$K$J$j$^$9!#(B
@xref{File Variables,, $B%U%!%$%k$K%m!<%+%k$JJQ?t(B, emacs, GNU Emacs $B%^%K%e%"%k(B}$B!#(B
@node Creating Buffer-Local
@c @subsection Creating and Deleting Buffer-Local Bindings
@subsection $B%P%C%U%!%m!<%+%k$JB+G{$N:n@.$H:o=|(B
@c @deffn Command make-local-variable variable
@deffn $B%3%^%s%I(B make-local-variable variable
@c This function creates a buffer-local binding in the current buffer for
@c @var{variable} (a symbol). Other buffers are not affected. The value
@c returned is @var{variable}.
$B$3$N4X?t$O!"%+%l%s%H%P%C%U%!$K$*$$$F!"(B
@var{variable}$B!J%7%s%\%k!K$N%P%C%U%!%m!<%+%k$JB+G{$r:n$k!#(B
$BB>$N%P%C%U%!$O1F6A$5$l$J$$!#(B
$BJV$9CM$O(B@var{variable}$B!#(B
@c @c Emacs 19 feature
@c The buffer-local value of @var{variable} starts out as the same value
@c @var{variable} previously had. If @var{variable} was void, it remains
@c void.
@var{variable}$B$N%P%C%U%!%m!<%+%k$JCM$O!"(B
@var{variable}$B$N0JA0$HF1$8CM$G;O$^$k!#(B
@var{variable}$B$,6u$G$"$l$P!"6u$N$^$^$G$"$k!#(B
@example
@group
@c ;; @r{In buffer @samp{b1}:}
@c (setq foo 5) ; @r{Affects all buffers.}
;; @r{$B%P%C%U%!(B@samp{b1}$B$G$O!"(B}
(setq foo 5) ; @r{$B$9$Y$F$N%P%C%U%!$K1F6A$9$k(B}
@result{} 5
@end group
@group
@c (make-local-variable 'foo) ; @r{Now it is local in @samp{b1}.}
(make-local-variable 'foo) ; @r{@samp{b1}$B$K%m!<%+%k(B}
@result{} foo
@end group
@group
@c foo ; @r{That did not change}
@c @result{} 5 ; @r{the value.}
foo ; @r{$B$3$l$OCM$r(B}
@result{} 5 ; @r{$BJQ$($J$$(B}
@end group
@group
@c (setq foo 6) ; @r{Change the value}
@c @result{} 6 ; @r{in @samp{b1}.}
(setq foo 6) ; @r{@samp{b1}$B$G$NCM$r(B}
@result{} 6 ; @r{$BJQ99$9$k(B}
@end group
@group
foo
@result{} 6
@end group
@group
@c ;; @r{In buffer @samp{b2}, the value hasn't changed.}
;; @r{$B%P%C%U%!(B@samp{b2}$B$G$O!"CM$OJQ$o$C$F$$$J$$(B}
(save-excursion
(set-buffer "b2")
foo)
@result{} 5
@end group
@end example
@c Making a variable buffer-local within a @code{let}-binding for that
@c variable does not work reliably, unless the buffer in which you do this
@c is not current either on entry to or exit from the @code{let}. This is
@c because @code{let} does not distinguish between different kinds of
@c bindings; it knows only which variable the binding was made for.
$BJQ?t$N(B@code{let}$BB+G{$NFbB&$G$=$NJQ?t$r%P%C%U%!%m!<%+%k$K$7$F$b!"(B
$B$=$N%P%C%U%!$,(B@code{let}$B$KF~$k$H$-$dH4$1$k$H$-$K(B
$B%+%l%s%H%P%C%U%!$K$J$C$F$$$J$$$H!"@5$7$/F0:n$7$J$$!#(B
$B$3$l$O!"(B@code{let}$B$,!"0[$J$k<oN`$NB+G{$r6hJL$7$J$$$+$i$G$"$j!"(B
$B$I$NJQ?t$NB+G{$r:n$k$+$@$1$rCN$C$F$$$k$+$i$G$"$k!#(B
@c If the variable is terminal-local, this function signals an error. Such
@c variables cannot have buffer-local bindings as well. @xref{Multiple
@c Displays}.
$BJQ?t$,C<Kv$K%m!<%+%k$J$H$-$K$O!"$3$N4X?t$O%(%i!<$rDLCN$9$k!#(B
$B$=$N$h$&$JJQ?t$O!"F1;~$K%P%C%U%!%m!<%+%k$JB+G{$r;}$F$J$$!#(B
@xref{Multiple Displays}$B!#(B
@c @strong{Note:} do not use @code{make-local-variable} for a hook
@c variable. Instead, use @code{make-local-hook}. @xref{Hooks}.
@strong{$BCm0U!'(B}@code{ }
$B%U%C%/JQ?t$KBP$7$F(B@code{make-local-variable}$B$r;H$o$J$$$3$H!#(B
$B$=$N$+$o$j$K(B@code{make-local-hook}$B$r;H$&!#(B
@pxref{Hooks}$B!#(B
@end deffn
@c @deffn Command make-variable-buffer-local variable
@deffn $B%3%^%s%I(B make-variable-buffer-local variable
@c This function marks @var{variable} (a symbol) automatically
@c buffer-local, so that any subsequent attempt to set it will make it
@c local to the current buffer at the time.
$B$3$N4X?t$O!"(B@var{variable}$B!J%7%s%\%k!K$r(B
$B<+F0E*$K%P%C%U%!%m!<%+%k$K$9$k$h$&$K0u$rIU$1!"(B
$B$3$l0J9_$K$=$NJQ?t$K@_Dj$7$h$&$H$9$k$H!"(B
$B$=$N;~E@$N%+%l%s%H%P%C%U%!$K%m!<%+%k$K$9$k!#(B
@c A peculiar wrinkle of this feature is that binding the variable (with
@c @code{let} or other binding constructs) does not create a buffer-local
@c binding for it. Only setting the variable (with @code{set} or
@c @code{setq}) does so.
$B$3$N5!G=$N=EMW$JE@$O!"(B
$B!J(B@code{let}$B$dB>$NB+G{$r:n$k9=J8$G!KJQ?t$rB+G{$7$F$b!"(B
$B$=$NJQ?t$N%P%C%U%!%m!<%+%k$JB+G{$r:n$i$J$$$3$H$G$"$k!#(B
$B!J(B@code{set}$B$d(B@code{setq}$B$G!KJQ?t$r@_Dj$7$F=i$a$F$=$N$h$&$K$9$k!#(B
@c The value returned is @var{variable}.
$BJV$9CM$O(B@var{variable}$B$G$"$k!#(B
@c @strong{Warning:} Don't assume that you should use
@c @code{make-variable-buffer-local} for user-option variables, simply
@c because users @emph{might} want to customize them differently in
@c different buffers. Users can make any variable local, when they wish
@c to. It is better to leave the choice to them.
@strong{$B7Y9p!'(B}@code{ }
$B%f!<%6!<$,0[$J$k%P%C%U%!$G$O0[$J$C$?%+%9%?%^%$%:$r$9$k$+$b(B@emph{$B$7$l$J$$(B}$B$H(B
$B$$$&$@$1$G!"%f!<%6!<%*%W%7%g%sJQ?t$K(B@code{make-variable-buffer-local}$B$r(B
$B;H$&$Y$-$@$H2>Dj$7$J$$$3$H!#(B
$B%f!<%6!<$O!"I,MW$J$i$P!"$I$s$JJQ?t$G$b%m!<%+%k$K$G$-$k!#(B
$BA*Br$O%f!<%6!<$KG$$;$k$N$,$h$$!#(B
@c The time to use @code{make-variable-buffer-local} is when it is crucial
@c that no two buffers ever share the same binding. For example, when a
@c variable is used for internal purposes in a Lisp program which depends
@c on having separate values in separate buffers, then using
@c @code{make-variable-buffer-local} can be the best solution.
2$B$D$N%P%C%U%!$,F1$8B+G{$r6&M-$7$J$$$3$H$,=EMW$J>lLL$G$O!"(B
@code{make-variable-buffer-local}$B$r;H$&!#(B
$B$?$H$($P!"0[$J$k%P%C%U%!$G$O0[$J$kCM$r;}$D$3$H$K0MB8$9$k$h$&$J(B
Lisp$B%W%m%0%i%`$GFbItL\E*$KJQ?t$r;H$&$H$-$K$O!"(B
@code{make-variable-buffer-local}$B$r;H$&$N$,:GNI$G$"$k!#(B
@end deffn
@defun local-variable-p variable &optional buffer
@c This returns @code{t} if @var{variable} is buffer-local in buffer
@c @var{buffer} (which defaults to the current buffer); otherwise,
@c @code{nil}.
$B$3$l$O!"(B@var{variable}$B$,%P%C%U%!(B@var{buffer}
$B!J%G%U%)%k%H$O%+%l%s%H%P%C%U%!!K$K$*$$$F(B
$B%P%C%U%!%m!<%+%k$G$"$l$P(B@code{t}$B$rJV$7!"(B
$B$5$b$J$1$l$P(B@code{nil}$B$rJV$9!#(B
@end defun
@defun buffer-local-variables &optional buffer
@c This function returns a list describing the buffer-local variables in
@c buffer @var{buffer}. (If @var{buffer} is omitted, the current buffer is
@c used.) It returns an association list (@pxref{Association Lists}) in
@c which each element contains one buffer-local variable and its value.
@c However, when a variable's buffer-local binding in @var{buffer} is void,
@c then the variable appears directly in the resulting list.
$B$3$N4X?t$O!"%P%C%U%!(B@var{buffer}$B$N%P%C%U%!%m!<%+%k$JJQ?t$r(B
$B5-=R$7$?%j%9%H$rJV$9!#(B
$B!J(B@var{buffer}$B$r>JN,$9$k$H%+%l%s%H%P%C%U%!$r;H$&!#!K(B
$B%P%C%U%!%m!<%+%k$JJQ?t$H$=$NCM$rF~$l$?MWAG$+$i@.$kO"A[%j%9%H(B
$B!J(B@pxref{Association Lists}$B!K$rJV$9!#(B
$B$7$+$7!"(B@var{buffer}$B$K$*$1$kJQ?t$N%P%C%U%!%m!<%+%k$JB+G{$,6u$G$"$k$H!"(B
$BJQ?t$O7k2L$N%j%9%H$KD>@\8=$l$k!#(B
@example
@group
(make-local-variable 'foobar)
(makunbound 'foobar)
(make-local-variable 'bind-me)
(setq bind-me 69)
@end group
(setq lcl (buffer-local-variables))
@c ;; @r{First, built-in variables local in all buffers:}
;; @r{$B$^$:!"$9$Y$F$N%P%C%U%!$G%m!<%+%k$JAH$_9~$_JQ?t(B}
@result{} ((mark-active . nil)
(buffer-undo-list . nil)
(mode-name . "Fundamental")
@dots{}
@group
@c ;; @r{Next, non-built-in buffer-local variables.}
@c ;; @r{This one is buffer-local and void:}
;; @r{$BB3$$$F!"AH$_9~$_$G$J$$%P%C%U%!%m!<%+%k$JJQ?t(B}
;; @r{$B$3$l$O%P%C%U%!%m!<%+%k$G!"$+$D!"6u(B}
foobar
@c ;; @r{This one is buffer-local and nonvoid:}
;; @r{$B$3$l$O%P%C%U%!%m!<%+%k$G!"$+$D!"6u$G$O$J$$(B}
(bind-me . 69))
@end group
@end example
@c Note that storing new values into the @sc{cdr}s of cons cells in this
@c list does @emph{not} change the buffer-local values of the variables.
$B$3$N%j%9%H$N%3%s%9%;%k$N(B@sc{cdr}$B$K?7$?$JCM$r3JG<$7$F$b!"(B
$BJQ?t$N%P%C%U%!%m!<%+%k$JCM$rJQ99(B@emph{$B$7$J$$(B}$B$3$H$KCm0U$7$F$[$7$$!#(B
@end defun
@c @deffn Command kill-local-variable variable
@deffn $B%3%^%s%I(B kill-local-variable variable
@c This function deletes the buffer-local binding (if any) for
@c @var{variable} (a symbol) in the current buffer. As a result, the
@c default binding of @var{variable} becomes visible in this buffer. This
@c typically results in a change in the value of @var{variable}, since the
@c default value is usually different from the buffer-local value just
@c eliminated.
$B$3$N4X?t$O!"%+%l%s%H%P%C%U%!$K$*$1$k(B@var{variable}$B!J%7%s%\%k!K$N(B
$B%P%C%U%!%m!<%+%k$JB+G{$r!J$"$l$P!K:o=|$9$k!#(B
$B$=$N7k2L!"$3$N%P%C%U%!$G$O!"(B@var{variable}$B$N%G%U%)%k%H$NB+G{$,(B
$B8+$($k$h$&$K$J$k!#(B
$BE57?E*$K$O!"(B@var{variable}$B$NCM$,JQ$o$k!#(B
$B$J$<$J$i!"%G%U%)%k%HCM$O!":o=|$7$?%P%C%U%!%m!<%+%k$JCM$H$O(B
$BIaDL$O0[$J$k$+$i$G$"$k!#(B
@c If you kill the buffer-local binding of a variable that automatically
@c becomes buffer-local when set, this makes the default value visible in
@c the current buffer. However, if you set the variable again, that will
@c once again create a buffer-local binding for it.
$B<+F0E*$K%P%C%U%!%m!<%+%k$K$9$k0u$,IU$$$?JQ?t$N%P%C%U%!%m!<%+%k$JB+G{$r(B
$B:o=|$9$k$H!"%+%l%s%H%P%C%U%!$G$O%G%U%)%k%HCM$,8+$($k$h$&$K$J$k!#(B
$B$7$+$7!"JQ?t$K:FEY@_Dj$9$k$H!"$=$l$KBP$9$k%P%C%U%!%m!<%+%k$JB+G{$,(B
$B:FEY:n@.$5$l$k!#(B
@c @code{kill-local-variable} returns @var{variable}.
@code{kill-local-variable}$B$O(B@var{variable}$B$rJV$9!#(B
@c This function is a command because it is sometimes useful to kill one
@c buffer-local variable interactively, just as it is useful to create
@c buffer-local variables interactively.
$B$3$N4X?t$,%3%^%s%I$G$"$k$N$O!"(B
$BBPOCE*$K%P%C%U%!%m!<%+%k$JJQ?t$r:n$k$N$,M-MQ$J$h$&$K!"(B
$BBPOCE*$K%P%C%U%!%m!<%+%k$JJQ?t$r:o=|$9$k$N$,M-MQ$J>l9g$,$"$k$+$i$G$"$k!#(B
@end deffn
@defun kill-all-local-variables
@c This function eliminates all the buffer-local variable bindings of the
@c current buffer except for variables marked as ``permanent''. As a
@c result, the buffer will see the default values of most variables.
$B$3$N4X?t$O!"%+%l%s%H%P%C%U%!$K$*$$$F!"(B
$B!X915WE*!Y$H0uIU$1$7$F$"$kJQ?t$r=|$$$F!"(B
$B$9$Y$F$N%P%C%U%!%m!<%+%k$JJQ?tB+G{$r:o=|$9$k!#(B
$B$=$N7k2L!"%P%C%U%!$G$O!"$[$H$s$I$NJQ?t$N%G%U%)%k%HCM$,8+$($k$h$&$K$J$k!#(B
@c This function also resets certain other information pertaining to the
@c buffer: it sets the local keymap to @code{nil}, the syntax table to the
@c value of @code{(standard-syntax-table)}, the case table to
@c @code{(standard-case-table)}, and the abbrev table to the value of
@c @code{fundamental-mode-abbrev-table}.
$B$3$N4X?t$O!"%P%C%U%!$KB0$9$kB>$N$"$k<o$N>pJs$b%j%;%C%H$9$k!#(B
$B$D$^$j!"%m!<%+%k%-!<%^%C%W$K(B@code{nil}$B!"(B
$B9=J8%F!<%V%k$K(B@code{(standard-syntax-table)}$B$NCM!"(B
$BBgJ8;z>.J8;z%F!<%V%k$K(B@code{(standard-case-table)}$B!"(B
$BN,8l%F!<%V%k$K(B@code{fundamental-mode-abbrev-table}$B$NCM$r@_Dj$9$k!#(B
@c The very first thing this function does is run the normal hook
@c @code{change-major-mode-hook} (see below).
$B$3$N4X?t$,:G=i$K9T$&$3$H$O!"(B
$B%N!<%^%k%U%C%/(B@code{change-major-mode-hook}$B!J2<5-;2>H!K$r(B
$B<B9T$9$k$3$H$G$"$k!#(B
@c Every major mode command begins by calling this function, which has the
@c effect of switching to Fundamental mode and erasing most of the effects
@c of the previous major mode. To ensure that this does its job, the
@c variables that major modes set should not be marked permanent.
$B3F%a%8%c!<%b!<%I%3%^%s%I$O$3$N4X?t$r8F$S=P$9$3$H$+$i;O$a$k!#(B
$B$D$^$j!"4pK\!J(Bfundamental$B!K%b!<%I$K@Z$jBX$(!"(B
$B$=$l0JA0$N%a%8%c!<%b!<%I$N$[$H$s$I$N8z2L$r>C$7$5$k!#(B
$B$3$N=hM}$rJ]>Z$9$k$?$a$K!"%a%8%c!<%b!<%I$G@_Dj$9$kJQ?t$K$O!"(B
$B915WE*$N0u$rIU$1$J$$$3$H!#(B
@c @code{kill-all-local-variables} returns @code{nil}.
@code{kill-all-local-variables}$B$O(B@code{nil}$B$rJV$9!#(B
@end defun
@defvar change-major-mode-hook
@c The function @code{kill-all-local-variables} runs this normal hook
@c before it does anything else. This gives major modes a way to arrange
@c for something special to be done if the user switches to a different
@c major mode. For best results, make this variable buffer-local, so that
@c it will disappear after doing its job and will not interfere with the
@c subsequent major mode. @xref{Hooks}.
$B4X?t(B@code{kill-all-local-variables}$B$O!":G=i$K$3$N%N!<%^%k%U%C%/$r<B9T$9$k!#(B
$B$3$N%U%C%/$O%a%8%c!<%b!<%I$KBP$7$F!"(B
$B%f!<%6!<$,JL$N%a%8%c!<%b!<%I$K@Z$jBX$($F$$$?>l9g$K$O!"(B
$B$J$K$+FCJL$J$3$H$r9T$&>pJs$rDs6!$9$k!#(B
$B:GNI$N7k2L$rF@$k$?$a$K$O!"$3$NJQ?t$r%P%C%U%!%m!<%+%k$K$7$F$*$/$H(B
$B$=$NLrL\$r=*$($k$HJQ?t$O>C$($F$7$^$$!"$=$l0J9_$N%a%8%c!<%b!<%I$K43>D$7$J$$!#(B
@pxref{Hooks}$B!#(B
@end defvar
@c @c Emacs 19 feature
@c @cindex permanent local variable
@cindex $B915WE*$J%m!<%+%kJQ?t(B
@c A buffer-local variable is @dfn{permanent} if the variable name (a
@c symbol) has a @code{permanent-local} property that is non-@code{nil}.
@c Permanent locals are appropriate for data pertaining to where the file
@c came from or how to save it, rather than with how to edit the contents.
$B%P%C%U%!%m!<%+%kJQ?t$O!"(B
$BJQ?tL>!J%7%s%\%k!K$NB0@-(B@code{permanent-local}$B$,(B@code{nil}$B0J30$G$"$k$H!"(B
@dfn{$B915WE*(B}$B!J(Bpermanent$B!K$G$9!#(B
$B915WE*$J%m!<%+%kJQ?t$O!"JT=8:n6H$NJ8L.$G$O$J$/!"(B
$B$I$N%U%!%$%k$rK,LdCf$G$"$k$H$+$I$N$h$&$KJ]B8$9$k$H$+$K4XO"$9$k>pJs$K(B
$BE,$7$F$$$^$9!#(B
@node Default Value
@c @subsection The Default Value of a Buffer-Local Variable
@subsection $B%P%C%U%!%m!<%+%kJQ?t$N%G%U%)%k%HCM(B
@c @cindex default value
@cindex $B%G%U%)%k%HCM(B
@c The global value of a variable with buffer-local bindings is also
@c called the @dfn{default} value, because it is the value that is in
@c effect whenever neither the current buffer nor the selected frame has
@c its own binding for the variable.
$B%P%C%U%!%m!<%+%k$JB+G{$,$"$kJQ?t$N%0%m!<%P%kCM$r!"(B
@dfn{$B%G%U%)%k%H(B}$BCM$H$b8F$S$^$9!#(B
$B%+%l%s%H%P%C%U%!$dA*Br$7$?%U%l!<%`$KJQ?t$NFH<+$NB+G{$,$J$$>l9g$K!"(B
$B%0%m!<%P%kCM$r;H$&$+$i$G$9!#(B
@c The functions @code{default-value} and @code{setq-default} access and
@c change a variable's default value regardless of whether the current
@c buffer has a buffer-local binding. For example, you could use
@c @code{setq-default} to change the default setting of
@c @code{paragraph-start} for most buffers; and this would work even when
@c you are in a C or Lisp mode buffer that has a buffer-local value for
@c this variable.
$B4X?t(B@code{default-value}$B$H4X?t(B@code{setq-default}$B$O!"(B
$B%+%l%s%H%P%C%U%!$K%P%C%U%!%m!<%+%k$JB+G{$,$"$k$+$I$&$+$K4X$o$i$:!"(B
$BJQ?t$N%G%U%)%k%HCM$r;2>H$7$?$jJQ99$7$?$j$7$^$9!#(B
$B$?$H$($P!"(B@code{setq-default}$B$r;H$C$F!"(B
$B$[$H$s$I$N%P%C%U%!$N(B@code{paragraph-start}$B$N%G%U%)%k%HCM$rJQ99$G$-$^$9!#(B
$B$3$NJQ?t$N%P%C%U%!%m!<%+%k$JCM$,$"$k(BC$B%b!<%I$d(BLisp$B%b!<%I$N%P%C%U%!$G(B
$B9T$C$F$b$3$l$OF0:n$7$^$9!#(B
@c @c Emacs 19 feature
@c The special forms @code{defvar} and @code{defconst} also set the
@c default value (if they set the variable at all), rather than any
@c buffer-local or frame-local value.
$B%9%Z%7%c%k%U%)!<%`(B@code{defvar}$B$d(B@code{defconst}$B$b!"(B
$B%P%C%U%!%m!<%+%k$d%U%l!<%`%m!<%+%k$JCM$G$O$J$/!"(B
$B!JJQ?t$K@_Dj$9$k>l9g$K$O!K%G%U%)%k%HCM$r@_Dj$7$^$9!#(B
@defun default-value symbol
@c This function returns @var{symbol}'s default value. This is the value
@c that is seen in buffers and frames that do not have their own values for
@c this variable. If @var{symbol} is not buffer-local, this is equivalent
@c to @code{symbol-value} (@pxref{Accessing Variables}).
$B$3$N4X?t$O!"(B@var{symbol}$B$N%G%U%)%k%HCM$rJV$9!#(B
$B$3$NCM$O!"$3$NJQ?t$KBP$7$FFH<+$NCM$r;}$?$J$$%P%C%U%!$d%U%l!<%`$G8+$($k(B
$BCM$G$"$k!#(B
@var{symbol}$B$,%P%C%U%!%m!<%+%k$G$J$1$l$P!"(B
$B$3$l$O!"(B@code{symbol-value}$B!J(B@pxref{Accessing Variables}$B!K$HEy2A!#(B
@end defun
@c Emacs 19 feature
@defun default-boundp symbol
@c The function @code{default-boundp} tells you whether @var{symbol}'s
@c default value is nonvoid. If @code{(default-boundp 'foo)} returns
@c @code{nil}, then @code{(default-value 'foo)} would get an error.
$B4X?t(B@code{default-boundp}$B$O!"(B
@var{symbol}$B$N%G%U%)%k%HCM$,6u$G$J$$$3$H$rD4$Y$k!#(B
@code{(default-boundp 'foo)}$B$,(B@code{nil}$B$rJV$;$P!"(B
@code{(default-value 'foo)}$B$O%(%i!<$K$J$k!#(B
@c @code{default-boundp} is to @code{default-value} as @code{boundp} is to
@c @code{symbol-value}.
@code{default-boundp}$B$O!"(B@code{boundp}$B$,(B@code{symbol-value}$B$KBP1~$9$k$h$&$K!"(B
@code{default-value}$B$KBP1~$9$k!#(B
@end defun
@defspec setq-default [symbol form]@dots{}
@c This special form gives each @var{symbol} a new default value, which is
@c the result of evaluating the corresponding @var{form}. It does not
@c evaluate @var{symbol}, but does evaluate @var{form}. The value of the
@c @code{setq-default} form is the value of the last @var{form}.
$B$3$N%9%Z%7%c%k%U%)!<%`$O!"3F(B@var{symbol}$B$K!"(B
$BBP1~$9$k(B@var{form}$B$NI>2A7k2L$G$"$k?7$?$J%G%U%)%k%HCM$rM?$($k!#(B
@var{symbol}$B$OI>2A$7$J$$$,!"(B@var{form}$B$OI>2A$9$k!#(B
$B%U%)!<%`(B@code{setq-default}$B$NCM$O!":G8e$N(B@var{form}$B$NCM$G$"$k!#(B
@c If a @var{symbol} is not buffer-local for the current buffer, and is not
@c marked automatically buffer-local, @code{setq-default} has the same
@c effect as @code{setq}. If @var{symbol} is buffer-local for the current
@c buffer, then this changes the value that other buffers will see (as long
@c as they don't have a buffer-local value), but not the value that the
@c current buffer sees.
@var{symbol}$B$,%+%l%s%H%P%C%U%!$G%P%C%U%!%m!<%+%k$G$O$J$/!"$+$D!"(B
$B<+F0E*$K%P%C%U%!%m!<%+%k$K$9$k0u$,IU$$$F$$$J$1$l$P!"(B
@code{setq-default}$B$O(B@code{setq}$B$HF1$88z2L$,$"$k!#(B
@var{symbol}$B$,%+%l%s%H%P%C%U%!$G%P%C%U%!%m!<%+%k$J$i$P!"(B
$B!J%P%C%U%!%m!<%+%k$JCM$r;}$?$J$$!KJL$N%P%C%U%!$,8+$kCM$rJQ99$7!"(B
$B%+%l%s%H%P%C%U%!$,8+$kCM$OJQ99$7$J$$!#(B
@example
@group
@c ;; @r{In buffer @samp{foo}:}
;; @r{$B%P%C%U%!(B@samp{foo}$B$K$*$$$F!"(B}
(make-local-variable 'buffer-local)
@result{} buffer-local
@end group
@group
(setq buffer-local 'value-in-foo)
@result{} value-in-foo
@end group
@group
(setq-default buffer-local 'new-default)
@result{} new-default
@end group
@group
buffer-local
@result{} value-in-foo
@end group
@group
(default-value 'buffer-local)
@result{} new-default
@end group
@group
@c ;; @r{In (the new) buffer @samp{bar}:}
;; @r{$B!J?7$?$J!K%P%C%U%!(B@samp{bar}$B$G$O!"(B}
buffer-local
@result{} new-default
@end group
@group
(default-value 'buffer-local)
@result{} new-default
@end group
@group
(setq buffer-local 'another-default)
@result{} another-default
@end group
@group
(default-value 'buffer-local)
@result{} another-default
@end group
@group
@c ;; @r{Back in buffer @samp{foo}:}
;; @r{$B%P%C%U%!(B@samp{foo}$B$KLa$C$F$_$k$H(B}
buffer-local
@result{} value-in-foo
(default-value 'buffer-local)
@result{} another-default
@end group
@end example
@end defspec
@defun set-default symbol value
@c This function is like @code{setq-default}, except that @var{symbol} is
@c an ordinary evaluated argument.
$B$3$N4X?t$O(B@code{setq-default}$B$K;w$F$$$k$,!"(B
@var{symbol}$B$OIaDL$I$*$j$KI>2A$5$l$k0z?t$G$"$k!#(B
@example
@group
(set-default (car '(a b c)) 23)
@result{} 23
@end group
@group
(default-value 'a)
@result{} 23
@end group
@end example
@end defun
@node Frame-Local Variables
@c @section Frame-Local Variables
@section $B%U%l!<%`%m!<%+%k$JJQ?t(B
@c Just as variables can have buffer-local bindings, they can also have
@c frame-local bindings. These bindings belong to one frame, and are in
@c effect when that frame is selected. Frame-local bindings are actually
@c frame parameters: you create a frame-local binding in a specific frame
@c by calling @code{modify-frame-parameters} and specifying the variable
@c name as the parameter name.
$BJQ?t$K%P%C%U%!%m!<%+%k$JB+G{$,$"$k$h$&$K!"(B
$BJQ?t$K$O%U%l!<%`%m!<%+%k$JB+G{$b$"$j$^$9!#(B
$B$3$l$i$NB+G{$O(B1$B$D$N%U%l!<%`$KB0$7!"(B
$B$=$N%U%l!<%`$rA*Br$7$F$$$k$H$-$KM-8z$K$J$j$^$9!#(B
$B%U%l!<%`%m!<%+%k$JB+G{$O!"<B:]$K$O%U%l!<%`%Q%i%a!<%?$G$9!#(B
$BFCDj$N%U%l!<%`$G%U%l!<%`%m!<%+%k$JB+G{$r:n$k$K$O(B
@code{modify-frame-parameters}$B$r8F$S=P$7!"(B
$B%Q%i%a!<%?L>$H$7$FJQ?tL>$r;XDj$7$^$9!#(B
@c To enable frame-local bindings for a certain variable, call the function
@c @code{make-variable-frame-local}.
$BFCDj$NJQ?t$KBP$9$k%U%l!<%`%m!<%+%k$JB+G{$rM-8z$K$9$k$K$O!"(B
$B4X?t(B@code{make-variable-frame-local}$B$r8F$S=P$7$^$9!#(B
@c @deffn Command make-variable-frame-local variable
@deffn $B%3%^%s%I(B make-variable-frame-local variable
@c Enable the use of frame-local bindings for @var{variable}. This does
@c not in itself create any frame-local bindings for the variable; however,
@c if some frame already has a value for @var{variable} as a frame
@c parameter, that value automatically becomes a frame-local binding.
@var{variable}$B$KBP$7$F%U%l!<%`%m!<%+%k$JB+G{$r;H$&$h$&$K$9$k!#(B
$B$3$N4X?t$=$N$b$N$O(B@var{variable}$B$KBP$7$F%U%l!<%`%m!<%+%k$JB+G{$r:n@.$7$J$$!#(B
$B$7$+$7!"%U%l!<%`%Q%i%a!<%?$H$7$F(B@var{variable}$B$NCM$r;}$D%U%l!<%`$,(B
$B$9$G$KB8:_$9$l$P!"$=$NCM$O<+F0E*$K%U%l!<%`%m!<%+%k$JB+G{$K$J$k!#(B
@c If the variable is terminal-local, this function signals an error,
@c because such variables cannot have frame-local bindings as well.
@c @xref{Multiple Displays}. A few variables that are implemented
@c specially in Emacs can be (and usually are) buffer-local, but can never
@c be frame-local.
$BJQ?t$,C<Kv$K%m!<%+%k$G$"$k$H!"$3$N4X?t$O%(%i!<$rDLCN$9$k!#(B
$B$=$N$h$&$JJQ?t$O%U%l!<%`%m!<%+%k$JB+G{$rF1;~$K$O;}$F$J$$$+$i$G$"$k!#(B
@pxref{Multiple Displays}$B!#(B
Emacs$B$GFCJL$K<BAu$5$l$F$$$k>/?t$NJQ?t$O!JIaDL!K(B
$B%P%C%U%!%m!<%+%k$K$J$k$3$H$,$G$-$k$,!"%U%l!<%`%m!<%+%k$K$O$J$i$J$$!#(B
@end deffn
@c Buffer-local bindings take precedence over frame-local bindings. Thus,
@c consider a variable @code{foo}: if the current buffer has a buffer-local
@c binding for @code{foo}, that binding is active; otherwise, if the
@c selected frame has a frame-local binding for @code{foo}, that binding is
@c active; otherwise, the default binding of @code{foo} is active.
$B%P%C%U%!%m!<%+%k$JB+G{$O%U%l!<%`%m!<%+%k$JB+G{$KM%@h$7$^$9!#(B
$BJQ?t(B@code{foo}$B$r9M$($F$_$^$7$g$&!#(B
$B%+%l%s%H%P%C%U%!$K(B@code{foo}$B$N%P%C%U%!%m!<%+%k$JB+G{$,$"$k$H!"(B
$B$=$NB+G{$,M-8z$K$J$j$^$9!#(B
$BA*Br$7$?%U%l!<%`$K(B@code{foo}$B$N%U%l!<%`%m!<%+%k$JB+G{$,$"$k$H!"(B
$B$=$NB+G{$,M-8z$K$J$j$^$9!#(B
$B$5$b$J$1$l$P!"(B@code{foo}$B$N%G%U%)%k%H$NB+G{$,M-8z$K$J$j$^$9!#(B
@c Here is an example. First we prepare a few bindings for @code{foo}:
$B$D$.$KNc$r<($7$^$9!#(B
$B$^$:!"(B@code{foo}$B$NB+G{$r=`Hw$7$F$*$-$^$9!#(B
@example
(setq f1 (selected-frame))
(make-variable-frame-local 'foo)
@c ;; @r{Make a buffer-local binding for @code{foo} in @samp{b1}.}
;; @r{@samp{b1}$B$K$*$$$F!"(B@code{foo}$B$N%P%C%U%!%m!<%+%k$JB+G{$r:n$k(B}
(set-buffer (get-buffer-create "b1"))
(make-local-variable 'foo)
(setq foo '(b 1))
@c ;; @r{Make a frame-local binding for @code{foo} in a new frame.}
@c ;; @r{Store that frame in @code{f2}.}
;; @r{$B?7$7$$%U%l!<%`$G(B@code{foo}$B$N%U%l!<%`%m!<%+%k$JB+G{$r:n$k(B}
;; @r{$B$=$N%U%l!<%`$r(B@code{f2}$B$K3JG<$9$k(B}
(setq f2 (make-frame))
(modify-frame-parameters f2 '((foo . (f 2))))
@end example
@c Now we examine @code{foo} in various contexts. Whenever the
@c buffer @samp{b1} is current, its buffer-local binding is in effect,
@c regardless of the selected frame:
$B$G$O!"$5$^$6$^$JJ8L.$G(B@code{foo}$B$rD4$Y$F$_$^$7$g$&!#(B
$B%P%C%U%!(B@samp{b1}$B$,%+%l%s%H%P%C%U%!$G$"$l$P!"(B
$BA*Br$7$?%U%l!<%`$K4X78$J$/!"(B
@samp{b1}$B$N%P%C%U%!%m!<%+%k$JB+G{$,M-8z$K$J$C$F$$$^$9!#(B
@example
(select-frame f1)
(set-buffer (get-buffer-create "b1"))
foo
@result{} (b 1)
(select-frame f2)
(set-buffer (get-buffer-create "b1"))
foo
@result{} (b 1)
@end example
@noindent
@c Otherwise, the frame gets a chance to provide the binding; when frame
@c @code{f2} is selected, its frame-local binding is in effect:
$B$5$b$J$1$l$P!"%U%l!<%`$NB+G{$r;H$&2DG=@-$,$"$j$^$9!#(B
$B%U%l!<%`(B@code{f2}$B$rA*Br$7$F$$$k$H!"(B
$B$=$N%U%l!<%`%m!<%+%k$JB+G{$,M-8z$K$J$j$^$9!#(B
@example
(select-frame f2)
(set-buffer (get-buffer "*scratch*"))
foo
@result{} (f 2)
@end example
@noindent
@c When neither the current buffer nor the selected frame provides
@c a binding, the default binding is used:
$B%+%l%s%H%P%C%U%!$K$b%U%l!<%`$K$bB+G{$,$J$1$l$P!"(B
$B%G%U%)%k%H$NB+G{$r;H$$$^$9!#(B
@example
(select-frame f1)
(set-buffer (get-buffer "*scratch*"))
foo
@result{} nil
@end example
@noindent
@c When the active binding of a variable is a frame-local binding, setting
@c the variable changes that binding. You can observe the result with
@c @code{frame-parameters}:
$BJQ?t$NM-8z$JB+G{$,%U%l!<%`%m!<%+%k$JB+G{$G$"$k$H$-!"(B
$BJQ?t$K@_Dj$9$k$H$=$NB+G{$rJQ99$7$^$9!#(B
@code{frame-parameters}$B$G$=$N7k2L$r8+$k$3$H$,$G$-$^$9!#(B
@example
(select-frame f2)
(set-buffer (get-buffer "*scratch*"))
(setq foo 'nobody)
(assq 'foo (frame-parameters f2))
@result{} (foo . nobody)
@end example
@node Future Local Variables
@c @section Possible Future Local Variables
@section $B>-Mh$N%m!<%+%kJQ?t(B
@c We have considered the idea of bindings that are local to a category
@c of frames---for example, all color frames, or all frames with dark
@c backgrounds. We have not implemented them because it is not clear that
@c this feature is really useful. You can get more or less the same
@c results by adding a function to @code{after-make-frame-hook}, set up to
@c define a particular frame parameter according to the appropriate
@c conditions for each frame.
$B%U%l!<%`$KJ,N`$5$l$k$b$N$G%m!<%+%k$JB+G{$H$$$&%"%$%G%"$r9M;!$7$F$$$^$9!#(B
$B$?$H$($P!"$9$Y$F$N%+%i!<%U%l!<%`!"0E$$GX7J?'$N$9$Y$F$N%U%l!<%`$J$I$G$9!#(B
$B$3$N5!G=$,K\Ev$KM-MQ$J$N$+L@$i$+$G$J$$$N$G!"$=$l$i$r$^$@<BAu$7$F$O$$$^$;$s!#(B
@code{after-make-frame-hook}$B$K4X?t$rDI2C$7$F!"(B
$B3F%U%l!<%`$NE,@Z$J>uBV$K1~$8$?%U%l!<%`%Q%i%a!<%?$r@_Dj$9$l$P!"(B
$BF1$8$h$&$J7k2L$rF@$i$l$^$9!#(B
@c It would also be possible to implement window-local bindings. We
@c don't know of many situations where they would be useful, and it seems
@c that indirect buffers (@pxref{Indirect Buffers}) with buffer-local
@c bindings offer a way to handle these situations more robustly.
$B%&%#%s%I%&%m!<%+%k$JB+G{$r<BAu$9$k$3$H$b2DG=$G$9!#(B
$B$3$l$,M-MQ$G$"$kB?$/$N>u67$rCN$j$^$;$s$,!"(B
$B%P%C%U%!%m!<%+%k$JB+G{$r;}$D4V@\%P%C%U%!!J(B@pxref{Indirect Buffers}$B!K$G!"(B
$B$=$N$h$&$J>u67$r$h$j7xO4$K07$($k$H;W$$$^$9!#(B
@c If sufficient application is found for either of these two kinds of
@c local bindings, we will provide it in a subsequent Emacs version.
$B$3$l$i(B2$B<oN`$N%m!<%+%kB+G{$N$$$:$l$+$rI,MW$H$9$k==J,$J?t$N%"%W%j%1!<%7%g%s$,(B
$B$_$D$+$l$P!"(BEmacs$B$N>-Mh$NHG$G$=$N$h$&$JB+G{$rDs6!$9$k$G$7$g$&!#(B
|